|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2022 Firebase |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +import { ManifestEndpoint } from "../../runtime/manifest"; |
| 24 | +import { CloudEvent, CloudFunction } from "../core"; |
| 25 | +import { EventHandlerOptions, getGlobalOptions, optionsToEndpoint } from "../options"; |
| 26 | + |
| 27 | +/** @internal */ |
| 28 | +export const eventType = "google.firebase.remoteconfig.remoteConfig.v1.updated"; |
| 29 | + |
| 30 | +/* All the fields associated with the person/service account that wrote a Remote Config template. */ |
| 31 | +export interface ConfigUser { |
| 32 | + /* Display name. */ |
| 33 | + name: string; |
| 34 | + |
| 35 | + /* Email address. */ |
| 36 | + email: string; |
| 37 | + |
| 38 | + /* Image URL. */ |
| 39 | + imageUrl: string; |
| 40 | +} |
| 41 | + |
| 42 | +/* What type of update was associated with the Remote Config template version. */ |
| 43 | +export type ConfigUpdateOrigin = |
| 44 | + /* Catch-all for unrecognized values. */ |
| 45 | + | "REMOTE_CONFIG_UPDATE_ORIGIN_UNSPECIFIED" |
| 46 | + /* The update came from the Firebase UI. */ |
| 47 | + | "CONSOLE" |
| 48 | + /* The update came from the Remote Config REST API. */ |
| 49 | + | "REST_API" |
| 50 | + /* The update came from the Firebase Admin Node SDK. */ |
| 51 | + | "ADMIN_SDK_NODE"; |
| 52 | + |
| 53 | +/* Where the Remote Config update action originated. */ |
| 54 | +export type ConfigUpdateType = |
| 55 | + /* Catch-all for unrecognized enum values */ |
| 56 | + | "REMOTE_CONFIG_UPDATE_TYPE_UNSPECIFIED" |
| 57 | + /* A regular incremental update */ |
| 58 | + | "INCREMENTAL_UPDATE" |
| 59 | + /* A forced update. The ETag was specified as "*" in an UpdateRemoteConfigRequest request or the "Force Update" button was pressed on the console */ |
| 60 | + | "FORCED_UPDATE" |
| 61 | + /* A rollback to a previous Remote Config template */ |
| 62 | + | "ROLLBACK"; |
| 63 | + |
| 64 | +/* The data within Firebase Remote Config update events. */ |
| 65 | +export interface ConfigUpdateData { |
| 66 | + /* The version number of the version's corresponding Remote Config template. */ |
| 67 | + versionNumber: number; |
| 68 | + |
| 69 | + /* When the Remote Config template was written to the Remote Config server. */ |
| 70 | + updateTime: string; |
| 71 | + |
| 72 | + /* Aggregation of all metadata fields about the account that performed the update. */ |
| 73 | + updateUser: ConfigUser; |
| 74 | + |
| 75 | + /* The user-provided description of the corresponding Remote Config template. */ |
| 76 | + description: string; |
| 77 | + |
| 78 | + /* Where the update action originated. */ |
| 79 | + updateOrigin: ConfigUpdateOrigin; |
| 80 | + |
| 81 | + /* What type of update was made. */ |
| 82 | + updateType: ConfigUpdateType; |
| 83 | + |
| 84 | + /* Only present if this version is the result of a rollback, and will be the version number of the Remote Config template that was rolled-back to. */ |
| 85 | + rollbackSource: number; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Event handler which triggers when data is updated in a Remote Config. |
| 90 | + * |
| 91 | + * @param handler - Event handler which is run every time a Remote Config update occurs. |
| 92 | + * @returns A Cloud Function that you can export and deploy. |
| 93 | + * @alpha |
| 94 | + */ |
| 95 | +export function onConfigUpdated( |
| 96 | + handler: (event: CloudEvent<ConfigUpdateData>) => any | Promise<any> |
| 97 | +): CloudFunction<CloudEvent<ConfigUpdateData>>; |
| 98 | + |
| 99 | +/** |
| 100 | + * Event handler which triggers when data is updated in a Remote Config. |
| 101 | + * |
| 102 | + * @param opts - Options that can be set on an individual event-handling function. |
| 103 | + * @param handler - Event handler which is run every time a Remote Config update occurs. |
| 104 | + * @returns A Cloud Function that you can export and deploy. |
| 105 | + * @alpha |
| 106 | + */ |
| 107 | +export function onConfigUpdated( |
| 108 | + opts: EventHandlerOptions, |
| 109 | + handler: (event: CloudEvent<ConfigUpdateData>) => any | Promise<any> |
| 110 | +): CloudFunction<CloudEvent<ConfigUpdateData>>; |
| 111 | + |
| 112 | +/** |
| 113 | + * Event handler which triggers when data is updated in a Remote Config. |
| 114 | + * |
| 115 | + * @param optsOrHandler - Options or an event handler. |
| 116 | + * @param handler - Event handler which is run every time a Remote Config update occurs. |
| 117 | + * @returns A Cloud Function that you can export and deploy. |
| 118 | + * @alpha |
| 119 | + */ |
| 120 | +export function onConfigUpdated( |
| 121 | + optsOrHandler: |
| 122 | + | EventHandlerOptions |
| 123 | + | ((event: CloudEvent<ConfigUpdateData>) => any | Promise<any>), |
| 124 | + handler?: (event: CloudEvent<ConfigUpdateData>) => any | Promise<any> |
| 125 | +): CloudFunction<CloudEvent<ConfigUpdateData>> { |
| 126 | + if (typeof optsOrHandler === "function") { |
| 127 | + handler = optsOrHandler as (event: CloudEvent<ConfigUpdateData>) => any | Promise<any>; |
| 128 | + optsOrHandler = {}; |
| 129 | + } |
| 130 | + |
| 131 | + const baseOpts = optionsToEndpoint(getGlobalOptions()); |
| 132 | + const specificOpts = optionsToEndpoint(optsOrHandler); |
| 133 | + |
| 134 | + const func: any = (raw: CloudEvent<unknown>) => { |
| 135 | + return handler(raw as CloudEvent<ConfigUpdateData>); |
| 136 | + }; |
| 137 | + func.run = handler; |
| 138 | + |
| 139 | + const ep: ManifestEndpoint = { |
| 140 | + platform: "gcfv2", |
| 141 | + ...baseOpts, |
| 142 | + ...specificOpts, |
| 143 | + labels: { |
| 144 | + ...baseOpts?.labels, |
| 145 | + ...specificOpts?.labels, |
| 146 | + }, |
| 147 | + eventTrigger: { |
| 148 | + eventType, |
| 149 | + eventFilters: {}, |
| 150 | + retry: !!optsOrHandler.retry, |
| 151 | + }, |
| 152 | + }; |
| 153 | + func.__endpoint = ep; |
| 154 | + |
| 155 | + return func; |
| 156 | +} |
0 commit comments