|
| 1 | +import { getEndpointAnnotation, FirebaseAlertData } from './alerts'; |
| 2 | +import { CloudEvent, CloudFunction } from '../../core'; |
| 3 | +import * as options from '../../options'; |
| 4 | + |
| 5 | +/** |
| 6 | + * The internal payload object for adding a new tester device to app distribution. |
| 7 | + * Payload is wrapped inside a FirebaseAlertData object. |
| 8 | + */ |
| 9 | +export interface NewTesterDevicePayload { |
| 10 | + ['@type']: 'com.google.firebase.firebasealerts.NewTesterDevicePayload'; |
| 11 | + testerName: string; |
| 12 | + testerEmail: string; |
| 13 | + testerDeviceModelName: string; |
| 14 | + testerDeviceIdentifier: string; |
| 15 | +} |
| 16 | + |
| 17 | +interface WithAlertTypeAndApp { |
| 18 | + alertType: string; |
| 19 | + appId: string; |
| 20 | +} |
| 21 | +/** |
| 22 | + * A custom CloudEvent for Firebase Alerts (with custom extension attributes). |
| 23 | + */ |
| 24 | +export type AppDistributionEvent<T> = CloudEvent< |
| 25 | + FirebaseAlertData<T>, |
| 26 | + WithAlertTypeAndApp |
| 27 | +>; |
| 28 | + |
| 29 | +/** @internal */ |
| 30 | +export const newTesterIosDeviceAlert = 'appDistribution.newTesterIosDevice'; |
| 31 | + |
| 32 | +/** |
| 33 | + * Configuration for app distribution functions. |
| 34 | + */ |
| 35 | +export interface AppDistributionOptions extends options.EventHandlerOptions { |
| 36 | + appId?: string; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Declares a function that can handle adding a new tester iOS device. |
| 41 | + */ |
| 42 | +export function onNewTesterIosDevicePublished( |
| 43 | + handler: ( |
| 44 | + event: AppDistributionEvent<NewTesterDevicePayload> |
| 45 | + ) => any | Promise<any> |
| 46 | +): CloudFunction<FirebaseAlertData<NewTesterDevicePayload>>; |
| 47 | +export function onNewTesterIosDevicePublished( |
| 48 | + appId: string, |
| 49 | + handler: ( |
| 50 | + event: AppDistributionEvent<NewTesterDevicePayload> |
| 51 | + ) => any | Promise<any> |
| 52 | +): CloudFunction<FirebaseAlertData<NewTesterDevicePayload>>; |
| 53 | +export function onNewTesterIosDevicePublished( |
| 54 | + opts: AppDistributionOptions, |
| 55 | + handler: ( |
| 56 | + event: AppDistributionEvent<NewTesterDevicePayload> |
| 57 | + ) => any | Promise<any> |
| 58 | +): CloudFunction<FirebaseAlertData<NewTesterDevicePayload>>; |
| 59 | +export function onNewTesterIosDevicePublished( |
| 60 | + appIdOrOptsOrHandler: |
| 61 | + | string |
| 62 | + | AppDistributionOptions |
| 63 | + | (( |
| 64 | + event: AppDistributionEvent<NewTesterDevicePayload> |
| 65 | + ) => any | Promise<any>), |
| 66 | + handler?: ( |
| 67 | + event: AppDistributionEvent<NewTesterDevicePayload> |
| 68 | + ) => any | Promise<any> |
| 69 | +): CloudFunction<FirebaseAlertData<NewTesterDevicePayload>> { |
| 70 | + if (typeof appIdOrOptsOrHandler === 'function') { |
| 71 | + handler = appIdOrOptsOrHandler as ( |
| 72 | + event: AppDistributionEvent<NewTesterDevicePayload> |
| 73 | + ) => any | Promise<any>; |
| 74 | + appIdOrOptsOrHandler = {}; |
| 75 | + } |
| 76 | + |
| 77 | + const [opts, appId] = getOptsAndApp(appIdOrOptsOrHandler); |
| 78 | + |
| 79 | + const func = (raw: CloudEvent<unknown>) => { |
| 80 | + return handler(raw as AppDistributionEvent<NewTesterDevicePayload>); |
| 81 | + }; |
| 82 | + |
| 83 | + func.run = handler; |
| 84 | + func.__endpoint = getEndpointAnnotation(opts, newTesterIosDeviceAlert, appId); |
| 85 | + |
| 86 | + return func; |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * @internal |
| 91 | + * Helper function to parse the function opts and appId. |
| 92 | + */ |
| 93 | +export function getOptsAndApp( |
| 94 | + appIdOrOpts: string | AppDistributionOptions |
| 95 | +): [options.EventHandlerOptions, string | undefined] { |
| 96 | + let opts: options.EventHandlerOptions; |
| 97 | + let appId: string | undefined; |
| 98 | + if (typeof appIdOrOpts === 'string') { |
| 99 | + opts = {}; |
| 100 | + appId = appIdOrOpts; |
| 101 | + } else { |
| 102 | + appId = appIdOrOpts.appId; |
| 103 | + opts = { ...appIdOrOpts }; |
| 104 | + delete (opts as any).appId; |
| 105 | + } |
| 106 | + return [opts, appId]; |
| 107 | +} |
0 commit comments