UNPKG

3.3 kBJavaScriptView Raw
1"use strict";
2// The MIT License (MIT)
3//
4// Copyright (c) 2022 Firebase
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.onCustomEventPublished = void 0;
25/**
26 * Cloud functions to integrate directly with Eventarc.
27 * @packageDocumentation
28 */
29const encoding_1 = require("../../common/encoding");
30const manifest_1 = require("../../runtime/manifest");
31const trace_1 = require("../trace");
32const options = require("../options");
33const onInit_1 = require("../../common/onInit");
34function onCustomEventPublished(eventTypeOrOpts, handler) {
35 var _a, _b;
36 let opts;
37 if (typeof eventTypeOrOpts === "string") {
38 opts = {
39 eventType: eventTypeOrOpts,
40 };
41 }
42 else if (typeof eventTypeOrOpts === "object") {
43 opts = eventTypeOrOpts;
44 }
45 const func = (raw) => {
46 return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(raw);
47 };
48 func.run = handler;
49 const channel = (_a = opts.channel) !== null && _a !== void 0 ? _a : "locations/us-central1/channels/firebase";
50 const baseOpts = options.optionsToEndpoint(options.getGlobalOptions());
51 const specificOpts = options.optionsToEndpoint(opts);
52 const endpoint = {
53 ...(0, manifest_1.initV2Endpoint)(options.getGlobalOptions(), opts),
54 platform: "gcfv2",
55 ...baseOpts,
56 ...specificOpts,
57 labels: {
58 ...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
59 ...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
60 },
61 eventTrigger: {
62 eventType: opts.eventType,
63 eventFilters: {},
64 retry: (_b = opts.retry) !== null && _b !== void 0 ? _b : false,
65 channel,
66 },
67 };
68 (0, encoding_1.convertIfPresent)(endpoint.eventTrigger, opts, "eventFilters", "filters");
69 (0, encoding_1.copyIfPresent)(endpoint.eventTrigger, opts, "retry");
70 func.__endpoint = endpoint;
71 func.__requiredAPIs = [
72 {
73 api: "eventarcpublishing.googleapis.com",
74 reason: "Needed for custom event functions",
75 },
76 ];
77 return func;
78}
79exports.onCustomEventPublished = onCustomEventPublished;