Skip to content

Commit 1bee93f

Browse files
committed
Extract functions config to avoid dependency cycles, assign failure policy to triggers.
1 parent 6a8e381 commit 1bee93f

File tree

13 files changed

+117
-85
lines changed

13 files changed

+117
-85
lines changed

src/cloud-functions.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222

2323
import { Request, Response } from 'express';
2424
import * as _ from 'lodash';
25-
import { DeploymentOptions, FailurePolicy } from './function-builder';
25+
import {
26+
DeploymentOptions,
27+
FailurePolicy,
28+
Schedule,
29+
} from './function-configuration';
2630
export { Request, Response };
2731

2832
const WILDCARD_REGEX = new RegExp('{[^/{}]*}', 'g');
@@ -99,7 +103,6 @@ export class Change<T> {
99103

100104
/**
101105
* ChangeJson is the JSON format used to construct a Change object.
102-
* @internal
103106
*/
104107
export interface ChangeJson {
105108
/**
@@ -185,20 +188,6 @@ export interface Resource {
185188
labels?: { [tag: string]: string };
186189
}
187190

188-
export interface ScheduleRetryConfig {
189-
retryCount?: number;
190-
maxRetryDuration?: string;
191-
minBackoffDuration?: string;
192-
maxBackoffDuration?: string;
193-
maxDoublings?: number;
194-
}
195-
196-
export interface Schedule {
197-
schedule: string;
198-
timeZone?: string;
199-
retryConfig?: ScheduleRetryConfig;
200-
}
201-
202191
/**
203192
* TriggerAnnotated is used internally by the firebase CLI to understand what
204193
* type of Cloud Function to deploy.
@@ -268,11 +257,16 @@ export interface MakeCloudFunctionArgs<EventData> {
268257
}
269258

270259
export function optionsToTrigger({
271-
regions,
272-
timeoutSeconds,
260+
failurePolicy,
273261
memory,
262+
regions,
274263
schedule,
264+
timeoutSeconds,
275265
}: DeploymentOptions): TriggerAnnotated['__trigger'] {
266+
const defaultFailurePolicy: FailurePolicy = {
267+
retry: {},
268+
};
269+
276270
const memoryLookup = {
277271
'128MB': 128,
278272
'256MB': 256,
@@ -282,6 +276,11 @@ export function optionsToTrigger({
282276
};
283277

284278
return {
279+
...(failurePolicy === undefined || failurePolicy === false
280+
? {}
281+
: failurePolicy === true
282+
? { failurePolicy: defaultFailurePolicy }
283+
: { failurePolicy }),
285284
...(memory === undefined
286285
? {}
287286
: { availableMemoryMb: memoryLookup[memory] }),

src/function-builder.ts

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,15 @@ import * as https from './providers/https';
3939
import * as pubsub from './providers/pubsub';
4040
import * as remoteConfig from './providers/remoteConfig';
4141
import * as storage from './providers/storage';
42-
import { CloudFunction, EventContext, Schedule } from './cloud-functions';
43-
44-
/**
45-
* List of all regions supported by Cloud Functions.
46-
*/
47-
const SUPPORTED_REGIONS = [
48-
'us-central1',
49-
'us-east1',
50-
'europe-west1',
51-
'europe-west2',
52-
'asia-east2',
53-
'asia-northeast1',
54-
];
55-
56-
/**
57-
* List of available memory options supported by Cloud Functions.
58-
*/
59-
const VALID_MEMORY_OPTS = ['128MB', '256MB', '512MB', '1GB', '2GB'] as const;
60-
61-
/**
62-
* Cloud Functions min timeout value.
63-
*/
64-
const MIN_TIMEOUT_SECONDS = 0;
65-
66-
/**
67-
* Cloud Functions max timeout value.
68-
*/
69-
const MAX_TIMEOUT_SECONDS = 540;
42+
import { CloudFunction, EventContext } from './cloud-functions';
43+
import {
44+
RuntimeOptions,
45+
VALID_MEMORY_OPTS,
46+
MIN_TIMEOUT_SECONDS,
47+
MAX_TIMEOUT_SECONDS,
48+
SUPPORTED_REGIONS,
49+
DeploymentOptions,
50+
} from './function-configuration';
7051

7152
/**
7253
* Assert that the runtime options passed in are valid.
@@ -174,31 +155,6 @@ export function runWith(runtimeOptions: RuntimeOptions): FunctionBuilder {
174155
return new FunctionBuilder(runtimeOptions);
175156
}
176157

177-
export interface FailurePolicy {
178-
retry: {};
179-
}
180-
181-
export interface RuntimeOptions {
182-
/**
183-
* Failure policy of the function, boolean `true` is equivalent to providing
184-
* an empty policy.
185-
*/
186-
failurePolicy?: FailurePolicy | boolean;
187-
/**
188-
* Amount of memory to allocate to the function.
189-
*/
190-
memory?: typeof VALID_MEMORY_OPTS[number];
191-
/**
192-
* Timeout for the function in seconds, possible values are 0 to 540.
193-
*/
194-
timeoutSeconds?: number;
195-
}
196-
197-
export interface DeploymentOptions extends RuntimeOptions {
198-
regions?: string[];
199-
schedule?: Schedule;
200-
}
201-
202158
export class FunctionBuilder {
203159
constructor(private options: DeploymentOptions) {}
204160

@@ -237,6 +193,12 @@ export class FunctionBuilder {
237193
}
238194

239195
get https() {
196+
if (this.options.failurePolicy !== undefined) {
197+
console.warn(
198+
'RuntimeOptions.failurePolicy is not supported in https functions.'
199+
);
200+
}
201+
240202
return {
241203
/**
242204
* Handle HTTP requests.

src/function-configuration.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* List of all regions supported by Cloud Functions.
3+
*/
4+
export const SUPPORTED_REGIONS = [
5+
'us-central1',
6+
'us-east1',
7+
'europe-west1',
8+
'europe-west2',
9+
'asia-east2',
10+
'asia-northeast1',
11+
];
12+
13+
/**
14+
* Cloud Functions min timeout value.
15+
*/
16+
export const MIN_TIMEOUT_SECONDS = 0;
17+
18+
/**
19+
* Cloud Functions max timeout value.
20+
*/
21+
export const MAX_TIMEOUT_SECONDS = 540;
22+
23+
/**
24+
* List of available memory options supported by Cloud Functions.
25+
*/
26+
export const VALID_MEMORY_OPTS = [
27+
'128MB',
28+
'256MB',
29+
'512MB',
30+
'1GB',
31+
'2GB',
32+
] as const;
33+
34+
export interface ScheduleRetryConfig {
35+
retryCount?: number;
36+
maxRetryDuration?: string;
37+
minBackoffDuration?: string;
38+
maxBackoffDuration?: string;
39+
maxDoublings?: number;
40+
}
41+
42+
export interface Schedule {
43+
schedule: string;
44+
timeZone?: string;
45+
retryConfig?: ScheduleRetryConfig;
46+
}
47+
48+
export interface FailurePolicy {
49+
retry: {};
50+
}
51+
52+
export interface RuntimeOptions {
53+
/**
54+
* Failure policy of the function, boolean `true` is equivalent to providing
55+
* an empty policy.
56+
*/
57+
failurePolicy?: FailurePolicy | boolean;
58+
/**
59+
* Amount of memory to allocate to the function.
60+
*/
61+
memory?: typeof VALID_MEMORY_OPTS[number];
62+
/**
63+
* Timeout for the function in seconds, possible values are 0 to 540.
64+
*/
65+
timeoutSeconds?: number;
66+
}
67+
68+
export interface DeploymentOptions extends RuntimeOptions {
69+
regions?: string[];
70+
schedule?: Schedule;
71+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ export {
5555
export * from './config';
5656
export * from './cloud-functions';
5757
export * from './function-builder';
58+
export * from './function-configuration';
5859

5960
setup();

src/providers/analytics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
Event,
2929
EventContext,
3030
} from '../cloud-functions';
31-
import { DeploymentOptions } from '../function-builder';
31+
import { DeploymentOptions } from '../function-configuration';
3232

3333
/** @internal */
3434
export const provider = 'google.analytics';

src/providers/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
} from '../cloud-functions';
2929
import * as firebase from 'firebase-admin';
3030
import * as _ from 'lodash';
31-
import { DeploymentOptions } from '../function-builder';
31+
import { DeploymentOptions } from '../function-configuration';
3232

3333
/** @internal */
3434
export const provider = 'google.firebase.auth';

src/providers/crashlytics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
CloudFunction,
2626
EventContext,
2727
} from '../cloud-functions';
28-
import { DeploymentOptions } from '../function-builder';
28+
import { DeploymentOptions } from '../function-configuration';
2929

3030
/** @internal */
3131
export const provider = 'google.firebase.crashlytics';

src/providers/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
import { normalizePath, applyChange, pathParts, joinPath } from '../utils';
3333
import * as firebase from 'firebase-admin';
3434
import { firebaseConfig } from '../config';
35-
import { DeploymentOptions } from '../function-builder';
35+
import { DeploymentOptions } from '../function-configuration';
3636

3737
/** @internal */
3838
export const provider = 'google.firebase.database';

src/providers/firestore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
EventContext,
3333
} from '../cloud-functions';
3434
import { dateToTimestampProto } from '../encoder';
35-
import { DeploymentOptions } from '../function-builder';
35+
import { DeploymentOptions } from '../function-configuration';
3636

3737
/** @internal */
3838
export const provider = 'google.firestore';

src/providers/https.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,12 @@ import * as _ from 'lodash';
2626
import * as cors from 'cors';
2727
import { apps } from '../apps';
2828
import { HttpsFunction, optionsToTrigger, Runnable } from '../cloud-functions';
29-
import { DeploymentOptions } from '../function-builder';
29+
import { DeploymentOptions } from '../function-configuration';
3030

31-
/**
32-
*
33-
*
34-
*/
3531
export interface Request extends express.Request {
3632
rawBody: Buffer;
3733
}
34+
3835
/**
3936
* Handle HTTP requests.
4037
* @param handler A function that takes a request and response object,

0 commit comments

Comments
 (0)