UNPKG

4.42 kBJavaScriptView Raw
1"use strict";
2// The MIT License (MIT)
3//
4// Copyright (c) 2017 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._onCallWithOptions = exports._onRequestWithOptions = exports.onCall = exports.onRequest = exports.HttpsError = void 0;
25const encoding_1 = require("../../common/encoding");
26const https_1 = require("../../common/providers/https");
27Object.defineProperty(exports, "HttpsError", { enumerable: true, get: function () { return https_1.HttpsError; } });
28const cloud_functions_1 = require("../cloud-functions");
29const manifest_1 = require("../../runtime/manifest");
30const onInit_1 = require("../../common/onInit");
31const trace_1 = require("../../v2/trace");
32/**
33 * Handle HTTP requests.
34 * @param handler A function that takes a request and response object,
35 * same signature as an Express app.
36 */
37function onRequest(handler) {
38 return _onRequestWithOptions(handler, {});
39}
40exports.onRequest = onRequest;
41/**
42 * Declares a callable method for clients to call using a Firebase SDK.
43 * @param handler A method that takes a data and context and returns a value.
44 */
45function onCall(handler) {
46 return _onCallWithOptions(handler, {});
47}
48exports.onCall = onCall;
49/** @internal */
50function _onRequestWithOptions(handler, options) {
51 // lets us add __endpoint without altering handler:
52 const cloudFunction = (req, res) => {
53 return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(req, res);
54 };
55 cloudFunction.__trigger = {
56 ...(0, cloud_functions_1.optionsToTrigger)(options),
57 httpsTrigger: {},
58 };
59 (0, encoding_1.convertIfPresent)(cloudFunction.__trigger.httpsTrigger, options, "invoker", "invoker", encoding_1.convertInvoker);
60 // TODO parse the options
61 cloudFunction.__endpoint = {
62 platform: "gcfv1",
63 ...(0, manifest_1.initV1Endpoint)(options),
64 ...(0, cloud_functions_1.optionsToEndpoint)(options),
65 httpsTrigger: {},
66 };
67 (0, encoding_1.convertIfPresent)(cloudFunction.__endpoint.httpsTrigger, options, "invoker", "invoker", encoding_1.convertInvoker);
68 return cloudFunction;
69}
70exports._onRequestWithOptions = _onRequestWithOptions;
71/** @internal */
72function _onCallWithOptions(handler, options) {
73 // onCallHandler sniffs the function length of the passed-in callback
74 // and the user could have only tried to listen to data. Wrap their handler
75 // in another handler to avoid accidentally triggering the v2 API
76 const fixedLen = (data, context) => {
77 return (0, onInit_1.withInit)(handler)(data, context);
78 };
79 const func = (0, trace_1.wrapTraceContext)((0, https_1.onCallHandler)({
80 enforceAppCheck: options.enforceAppCheck,
81 consumeAppCheckToken: options.consumeAppCheckToken,
82 cors: { origin: true, methods: "POST" },
83 }, fixedLen));
84 func.__trigger = {
85 labels: {},
86 ...(0, cloud_functions_1.optionsToTrigger)(options),
87 httpsTrigger: {},
88 };
89 func.__trigger.labels["deployment-callable"] = "true";
90 func.__endpoint = {
91 platform: "gcfv1",
92 labels: {},
93 ...(0, manifest_1.initV1Endpoint)(options),
94 ...(0, cloud_functions_1.optionsToEndpoint)(options),
95 callableTrigger: {},
96 };
97 func.run = fixedLen;
98 return func;
99}
100exports._onCallWithOptions = _onCallWithOptions;