UNPKG

10 kBTypeScriptView Raw
1import * as express from "express";
2import { ResetValue } from "../../common/options";
3import { CallableRequest, CallableResponse, FunctionsErrorCode, HttpsError, Request, AuthData } from "../../common/providers/https";
4import { ManifestEndpoint } from "../../runtime/manifest";
5import { GlobalOptions, SupportedRegion } from "../options";
6import { Expression } from "../../params";
7import { SecretParam } from "../../params/types";
8import * as options from "../options";
9export { Request, CallableRequest, FunctionsErrorCode, HttpsError };
10/**
11 * Options that can be set on an onRequest HTTPS function.
12 */
13export interface HttpsOptions extends Omit<GlobalOptions, "region" | "enforceAppCheck"> {
14 /**
15 * If true, do not deploy or emulate this function.
16 */
17 omit?: boolean | Expression<boolean>;
18 /** HTTP functions can override global options and can specify multiple regions to deploy to. */
19 region?: SupportedRegion | string | Array<SupportedRegion | string> | Expression<string> | ResetValue;
20 /** If true, allows CORS on requests to this function.
21 * If this is a `string` or `RegExp`, allows requests from domains that match the provided value.
22 * If this is an `Array`, allows requests from domains matching at least one entry of the array.
23 * Defaults to true for {@link https.CallableFunction} and false otherwise.
24 */
25 cors?: string | boolean | RegExp | Array<string | RegExp>;
26 /**
27 * Amount of memory to allocate to a function.
28 */
29 memory?: options.MemoryOption | Expression<number> | ResetValue;
30 /**
31 * Timeout for the function in seconds, possible values are 0 to 540.
32 * HTTPS functions can specify a higher timeout.
33 *
34 * @remarks
35 * The minimum timeout for a gen 2 function is 1s. The maximum timeout for a
36 * function depends on the type of function: Event handling functions have a
37 * maximum timeout of 540s (9 minutes). HTTPS and callable functions have a
38 * maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
39 * timeout of 1,800s (30 minutes)
40 */
41 timeoutSeconds?: number | Expression<number> | ResetValue;
42 /**
43 * Min number of actual instances to be running at a given time.
44 *
45 * @remarks
46 * Instances will be billed for memory allocation and 10% of CPU allocation
47 * while idle.
48 */
49 minInstances?: number | Expression<number> | ResetValue;
50 /**
51 * Max number of instances to be running in parallel.
52 */
53 maxInstances?: number | Expression<number> | ResetValue;
54 /**
55 * Number of requests a function can serve at once.
56 *
57 * @remarks
58 * Can only be applied to functions running on Cloud Functions v2.
59 * A value of null restores the default concurrency (80 when CPU >= 1, 1 otherwise).
60 * Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
61 * The maximum value for concurrency is 1,000.
62 */
63 concurrency?: number | Expression<number> | ResetValue;
64 /**
65 * Fractional number of CPUs to allocate to a function.
66 *
67 * @remarks
68 * Defaults to 1 for functions with <= 2GB RAM and increases for larger memory sizes.
69 * This is different from the defaults when using the gcloud utility and is different from
70 * the fixed amount assigned in Google Cloud Functions generation 1.
71 * To revert to the CPU amounts used in gcloud or in Cloud Functions generation 1, set this
72 * to the value "gcf_gen1"
73 */
74 cpu?: number | "gcf_gen1";
75 /**
76 * Connect cloud function to specified VPC connector.
77 */
78 vpcConnector?: string | Expression<string> | ResetValue;
79 /**
80 * Egress settings for VPC connector.
81 */
82 vpcConnectorEgressSettings?: options.VpcEgressSetting | ResetValue;
83 /**
84 * Specific service account for the function to run as.
85 */
86 serviceAccount?: string | Expression<string> | ResetValue;
87 /**
88 * Ingress settings which control where this function can be called from.
89 */
90 ingressSettings?: options.IngressSetting | ResetValue;
91 /**
92 * User labels to set on the function.
93 */
94 labels?: Record<string, string>;
95 secrets?: (string | SecretParam)[];
96 /**
97 * Invoker to set access control on https functions.
98 */
99 invoker?: "public" | "private" | string | string[];
100}
101/**
102 * Options that can be set on a callable HTTPS function.
103 */
104export interface CallableOptions<T = any> extends HttpsOptions {
105 /**
106 * Determines whether Firebase AppCheck is enforced.
107 * When true, requests with invalid tokens autorespond with a 401
108 * (Unauthorized) error.
109 * When false, requests with invalid tokens set event.app to undefiend.
110 */
111 enforceAppCheck?: boolean;
112 /**
113 * Determines whether Firebase App Check token is consumed on request. Defaults to false.
114 *
115 * @remarks
116 * Set this to true to enable the App Check replay protection feature by consuming the App Check token on callable
117 * request. Tokens that are found to be already consumed will have request.app.alreadyConsumed property set true.
118 *
119 *
120 * Tokens are only considered to be consumed if it is sent to the App Check service by setting this option to true.
121 * Other uses of the token do not consume it.
122 *
123 * This replay protection feature requires an additional network call to the App Check backend and forces the clients
124 * to obtain a fresh attestation from the chosen attestation providers. This can therefore negatively impact
125 * performance and can potentially deplete your attestation providers' quotas faster. Use this feature only for
126 * protecting low volume, security critical, or expensive operations.
127 *
128 * This option does not affect the enforceAppCheck option. Setting the latter to true will cause the callable function
129 * to automatically respond with a 401 Unauthorized status code when request includes an invalid App Check token.
130 * When request includes valid but consumed App Check tokens, requests will not be automatically rejected. Instead,
131 * the request.app.alreadyConsumed property will be set to true and pass the execution to the handler code for making
132 * further decisions, such as requiring additional security checks or rejecting the request.
133 */
134 consumeAppCheckToken?: boolean;
135 /**
136 * Time in seconds between sending heartbeat messages to keep the connection
137 * alive. Set to `null` to disable heartbeats.
138 *
139 * Defaults to 30 seconds.
140 */
141 heartbeatSeconds?: number | null;
142 /**
143 * Callback for whether a request is authorized.
144 *
145 * Designed to allow reusable auth policies to be passed as an options object. Two built-in reusable policies exist:
146 * isSignedIn and hasClaim.
147 */
148 authPolicy?: (auth: AuthData | null, data: T) => boolean | Promise<boolean>;
149}
150/**
151 * An auth policy that requires a user to be signed in.
152 */
153export declare const isSignedIn: () => (auth: AuthData | null) => boolean;
154/**
155 * An auth policy that requires a user to be both signed in and have a specific claim (optionally with a specific value)
156 */
157export declare const hasClaim: (claim: string, value?: string) => (auth: AuthData | null) => boolean;
158/**
159 * Handles HTTPS requests.
160 */
161export type HttpsFunction = ((
162/** An Express request object representing the HTTPS call to the function. */
163req: Request,
164/** An Express response object, for this function to respond to callers. */
165res: express.Response) => void | Promise<void>) & {
166 /** @alpha */
167 __trigger?: unknown;
168 /** @alpha */
169 __endpoint: ManifestEndpoint;
170};
171/**
172 * Creates a callable method for clients to call using a Firebase SDK.
173 */
174export interface CallableFunction<T, Return, Stream = unknown> extends HttpsFunction {
175 /** Executes the handler function with the provided data as input. Used for unit testing.
176 * @param data - An input for the handler function.
177 * @returns The output of the handler function.
178 */
179 run(request: CallableRequest<T>): Return;
180 stream(request: CallableRequest<T>, response: CallableResponse<Stream>): {
181 stream: AsyncIterator<Stream>;
182 output: Return;
183 };
184}
185/**
186 * Handles HTTPS requests.
187 * @param opts - Options to set on this function
188 * @param handler - A function that takes a {@link https.Request} and response object, same signature as an Express app.
189 * @returns A function that you can export and deploy.
190 */
191export declare function onRequest(opts: HttpsOptions, handler: (request: Request, response: express.Response) => void | Promise<void>): HttpsFunction;
192/**
193 * Handles HTTPS requests.
194 * @param handler - A function that takes a {@link https.Request} and response object, same signature as an Express app.
195 * @returns A function that you can export and deploy.
196 */
197export declare function onRequest(handler: (request: Request, response: express.Response) => void | Promise<void>): HttpsFunction;
198/**
199 * Declares a callable method for clients to call using a Firebase SDK.
200 * @param opts - Options to set on this function.
201 * @param handler - A function that takes a {@link https.CallableRequest}.
202 * @returns A function that you can export and deploy.
203 */
204export declare function onCall<T = any, Return = any | Promise<any>, Stream = unknown>(opts: CallableOptions<T>, handler: (request: CallableRequest<T>, response?: CallableResponse<Stream>) => Return): CallableFunction<T, Return extends Promise<unknown> ? Return : Promise<Return>, Stream>;
205/**
206 * Declares a callable method for clients to call using a Firebase SDK.
207 * @param handler - A function that takes a {@link https.CallableRequest}.
208 * @returns A function that you can export and deploy.
209 */
210export declare function onCall<T = any, Return = any | Promise<any>, Stream = unknown>(handler: (request: CallableRequest<T>, response?: CallableResponse<Stream>) => Return): CallableFunction<T, Return extends Promise<unknown> ? Return : Promise<Return>>;