本文整理汇总了TypeScript中firebase-functions.https.onCall方法的典型用法代码示例。如果您正苦于以下问题:TypeScript https.onCall方法的具体用法?TypeScript https.onCall怎么用?TypeScript https.onCall使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类firebase-functions.https
的用法示例。
在下文中一共展示了https.onCall方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: updateNextUrl
exists = (await db.collection('links').doc(nextUrl).get()).exists;
}
// Update db with new nextUrl
await db.doc('meta/meta').update({nextUrl: nextUrl});
}
export const onLinkCreateIncrementNextUrl =
functions.firestore.document('links/{linkId}')
.onCreate(async (_snapshot, _context) => {
await updateNextUrl();
});
export const callableIncrementNextUrl =
functions.https.onCall(async (_data, _context) => {
await updateNextUrl();
})
// Increment the hit count for the given short link. If the hits field does not
// exist on the link, it is created. If a v1Hits field exists on the link
// (legacy hit count), then its value is added to the hits field and the v1Hits
// field is deleted.
// This function is NOT idempotent.
async function incrementHitCount(short: string) {
const docRef = db.collection('links').doc(short);
await db.runTransaction(async transaction => {
const doc = await transaction.get(docRef);
if (doc.exists) {
const v1Hits: number|undefined = doc.get('v1Hits');
let hits: number = doc.get('hits') || 0;
示例2: verifyAuthentication
import { verifyAuthentication, verifyInstanceIdToken, instanceIdManager } from '../shared'
/* This file contains implementation of functions related to instanceId,
* which are used to send push notifications to client devices.
*/
/* Register a device instanceId to an user. This is called when the user sign-in in a device
*/
export const instanceId_register = functions.https.onCall(async (data, context) => {
verifyAuthentication(context);
verifyInstanceIdToken(context);
try {
await instanceIdManager.registerInstanceId(
context.auth.uid,
context.instanceIdToken)
return {}
} catch (err) {
console.error(err.message);
throw err;
}
})
/* Unregister a device instanceId to an user. This is called when the user sign-out in a device
*/
export const instanceId_unregister = functions.https.onCall(async (data, context) => {
verifyAuthentication(context);
verifyInstanceIdToken(context);
try {
示例3: requester
const makeFriendshipByFriendCode = https.onCall(async (data, context) => {
const heroId = context.auth.uid;
const friendCode = data['friendCode'];
if (typeof friendCode !== 'string') {
throw new https.HttpsError('invalid-argument', `data.friendCode must be a string. (given: ${friendCode})`, { hero: heroId, friendCode });
}
const heroRef = firestore.collection('users').doc(heroId);
let opponentRef: any;
let heroFriendshipRef: any;
let opponentFriendshipRef: any;
let chatRef: any;
await firestore.runTransaction(async (transaction) => {
const friendCodeDoc = await firestore.collection('friendCodes').doc(friendCode).get();
if (!friendCodeDoc.exists) {
throw new https.HttpsError('not-found', `The friend code (${friendCode}) is not found.`, { hero: heroId, friendCode });
}
opponentRef = friendCodeDoc.data()['user'];
if (!(opponentRef instanceof admin.firestore.DocumentReference)) {
throw new https.HttpsError('internal', 'Something has gone wrong in the process.', { hero: heroId, friendCode });
}
if (heroRef.id == opponentRef.id) {
throw new https.HttpsError('invalid-argument', `The friend code is pointing the requester himself.`, { hero: heroId, friendCode });
}
const otherFriendshipRefsWithSameOpponent = heroRef.collection('friendships').where('user', '==', opponentRef);
const otherFriendshipDocsWithSameOpponent = (await transaction.get(otherFriendshipRefsWithSameOpponent)).docs;
if (otherFriendshipDocsWithSameOpponent.length > 0) {
throw new https.HttpsError('already-exists', `The user is already your friend.`, { hero: heroId, opponent: opponentRef.id, friendCode });
}
heroFriendshipRef = heroRef.collection('friendships').doc();
opponentFriendshipRef = opponentRef.collection('friendships').doc();
chatRef = firestore.collection('chats').doc();
try {
transaction
.create(heroFriendshipRef, {
user: opponentRef,
chat: chatRef,
})
.create(opponentFriendshipRef, {
user: heroRef,
chat: chatRef,
})
.create(chatRef, {
members: [heroRef, opponentRef],
lastChatMessage: null,
lastMessageCreatedAt: null,
})
.delete(friendCodeDoc.ref);
} catch (err) {
console.error(err);
throw new https.HttpsError('internal', 'Something has gone wrong in the process.', { hero: heroId, friendCode });
}
});
console.info(
`The requester (id = ${heroRef.id}) and the user (id = ${opponentRef.id}) are now friends.
friendship:
The requester (id = ${heroRef.id}): ${heroFriendshipRef.id}
The user (id = ${opponentRef.id}): ${opponentFriendshipRef.id}
chat: ${chatRef.id}`
);
});
示例4:
const registerUser = https.onCall(async (_, context) => {
const heroId = context.auth.uid;
if (!(typeof heroId == 'string')) {
throw new https.HttpsError('unauthenticated', 'This function requires the user to be authenticated.', {
id: heroId,
});
}
const heroRef = firestore.collection('users').doc(heroId);
const heroDeviceDoc = heroRef.collection('devices').doc();
const friendCodeRef = firestore.collection('friendCodes').doc();
await firestore.runTransaction(async (transaction) => {
const heroDoc = await transaction.get(heroRef);
if (heroDoc.exists) {
throw new https.HttpsError('already-exists', '', {
id: heroId,
});
}
transaction.create(heroRef, {
name: 'No Name',
imageUrl: 'gs://caramel-b3766.appspot.com/profile_images/0000000000000000000000000000000000000000000000000000000000000000.png',
isProfileInitialized: false,
});
transaction.create(friendCodeRef, {
issuedAt: admin.firestore.FieldValue.serverTimestamp(),
user: heroRef,
});
});
console.info(
`The user has been created.
user: ${heroRef.id}
first friend code: ${friendCodeRef.id}`
);
});
示例5:
import * as functions from "firebase-functions";
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
export const helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
export const helloWorldJson = functions.https.onRequest((request, response) => {
response.json({"message": "Hello from Firebase!"});
});
// this is a proper callable function
export const helloName = functions.https.onCall((data, context) => {
return {
message: "Hello: " + data
}
});
示例6: verifyAuthentication
import * as functions from 'firebase-functions';
import { playBilling, verifyAuthentication, contentManager } from '../shared'
const BASIC_PLAN_SKU = functions.config().app.basic_plan_sku;
const PREMIUM_PLAN_SKU = functions.config().app.premium_plan_sku;
/* This file contains implementation of functions related to content serving.
* Each functions checks if the active user have access to the subscribed content,
* and then returns the content to client app.
*/
/* Callable that serves basic content to the client
*/
export const content_basic = functions.https.onCall(async (data, context) => {
verifyAuthentication(context);
await verifySubscriptionOwnershipAsync(context, [BASIC_PLAN_SKU, PREMIUM_PLAN_SKU]);
return contentManager.getBasicContent()
})
/* Callable that serves premium content to the client
*/
export const content_premium = functions.https.onCall(async (data, context) => {
verifyAuthentication(context);
await verifySubscriptionOwnershipAsync(context, [PREMIUM_PLAN_SKU]);
return contentManager.getPremiumContent()
})
// Util function that verifies if current user owns at least one active purchases listed in skus
async function verifySubscriptionOwnershipAsync(context: functions.https.CallableContext, skus: Array<string>): Promise<void> {
const purchaseList = await playBilling.users().queryCurrentSubscriptions(context.auth.uid)