Skip to content

Commit 6893b35

Browse files
committed
Extract configuration to break dependency cycle
1 parent 81764d6 commit 6893b35

File tree

13 files changed

+104
-85
lines changed

13 files changed

+104
-85
lines changed

src/cloud-functions.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import { Request, Response } from 'express';
2424
import * as _ from 'lodash';
2525
import { apps } from './apps';
26-
import { DeploymentOptions } from './function-builder';
26+
import { DeploymentOptions, Schedule } from './function-configuration';
2727
export { Request, Response };
2828

2929
const WILDCARD_REGEX = new RegExp('{[^/{}]*}', 'g');
@@ -168,20 +168,6 @@ export interface TriggerAnnotated {
168168
};
169169
}
170170

171-
export interface ScheduleRetryConfig {
172-
retryCount?: number;
173-
maxRetryDuration?: string;
174-
minBackoffDuration?: string;
175-
maxBackoffDuration?: string;
176-
maxDoublings?: number;
177-
}
178-
179-
export interface Schedule {
180-
schedule: string;
181-
timeZone?: string;
182-
retryConfig?: ScheduleRetryConfig;
183-
}
184-
185171
/** A Runnable has a `run` method which directly invokes the user-defined function - useful for unit testing. */
186172
export interface Runnable<T> {
187173
run: (data: T, context: any) => PromiseLike<any> | any;

src/function-builder.ts

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
import * as _ from 'lodash';
2423
import * as express from 'express';
24+
import * as _ from 'lodash';
2525

26+
import { CloudFunction, EventContext } from './cloud-functions';
27+
import {
28+
DeploymentOptions,
29+
MAX_TIMEOUT_SECONDS,
30+
RuntimeOptions,
31+
SUPPORTED_REGIONS,
32+
VALID_MEMORY_OPTIONS,
33+
} from './function-configuration';
2634
import * as analytics from './providers/analytics';
2735
import * as auth from './providers/auth';
2836
import * as crashlytics from './providers/crashlytics';
@@ -32,35 +40,6 @@ import * as https from './providers/https';
3240
import * as pubsub from './providers/pubsub';
3341
import * as remoteConfig from './providers/remoteConfig';
3442
import * as storage from './providers/storage';
35-
import { CloudFunction, EventContext, Schedule } from './cloud-functions';
36-
37-
/**
38-
* List of all regions supported by Cloud Functions.
39-
*/
40-
const SUPPORTED_REGIONS = [
41-
'us-central1',
42-
'us-east1',
43-
'us-east4',
44-
'europe-west1',
45-
'europe-west2',
46-
'asia-east2',
47-
'asia-northeast1',
48-
];
49-
50-
/**
51-
* List of available memory options supported by Cloud Functions.
52-
*/
53-
const VALID_MEMORY_OPTS = ['128MB', '256MB', '512MB', '1GB', '2GB'];
54-
55-
// Adding this memory type here to error on compile for TS users.
56-
// Unfortunately I have not found a way to merge this with VALID_MEMORY_OPS
57-
// without it being super ugly. But here they are right next to each other at least.
58-
type Memory = '128MB' | '256MB' | '512MB' | '1GB' | '2GB';
59-
60-
/**
61-
* Cloud Functions max timeout value.
62-
*/
63-
const MAX_TIMEOUT_SECONDS = 540;
6443

6544
/**
6645
* Assert that the runtime options passed in are valid.
@@ -70,10 +49,10 @@ const MAX_TIMEOUT_SECONDS = 540;
7049
function assertRuntimeOptionsValid(runtimeOptions: RuntimeOptions): boolean {
7150
if (
7251
runtimeOptions.memory &&
73-
!_.includes(VALID_MEMORY_OPTS, runtimeOptions.memory)
52+
!_.includes(VALID_MEMORY_OPTIONS, runtimeOptions.memory)
7453
) {
7554
throw new Error(
76-
`The only valid memory allocation values are: ${VALID_MEMORY_OPTS.join(
55+
`The only valid memory allocation values are: ${VALID_MEMORY_OPTIONS.join(
7756
', '
7857
)}`
7958
);
@@ -130,18 +109,6 @@ export function runWith(runtimeOptions: RuntimeOptions): FunctionBuilder {
130109
}
131110
}
132111

133-
export interface RuntimeOptions {
134-
timeoutSeconds?: number;
135-
memory?: Memory;
136-
}
137-
138-
export interface DeploymentOptions {
139-
regions?: string[];
140-
timeoutSeconds?: number;
141-
memory?: Memory;
142-
schedule?: Schedule;
143-
}
144-
145112
export class FunctionBuilder {
146113
constructor(private options: DeploymentOptions) {}
147114

src/function-configuration.ts

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

src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
// Providers:
2424
import * as analytics from './providers/analytics';
2525
import * as auth from './providers/auth';
26-
27-
import * as apps from './apps';
2826
import * as crashlytics from './providers/crashlytics';
2927
import * as database from './providers/database';
3028
import * as firestore from './providers/firestore';
3129
import * as https from './providers/https';
3230
import * as pubsub from './providers/pubsub';
3331
import * as remoteConfig from './providers/remoteConfig';
3432
import * as storage from './providers/storage';
33+
34+
import * as apps from './apps';
3535
import { handler } from './handler-builder';
3636
import { setup } from './setup';
3737

@@ -52,8 +52,9 @@ export {
5252
};
5353

5454
// Exported root types:
55-
export * from './config';
5655
export * from './cloud-functions';
56+
export * from './config';
5757
export * from './function-builder';
58+
export * from './function-configuration';
5859

5960
setup();

src/providers/analytics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
import * as _ from 'lodash';
2424

2525
import {
26-
makeCloudFunction,
2726
CloudFunction,
2827
Event,
2928
EventContext,
29+
makeCloudFunction,
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23+
import * as firebase from 'firebase-admin';
24+
import * as _ from 'lodash';
2325
import {
24-
makeCloudFunction,
2526
CloudFunction,
26-
EventContext,
2727
Event,
28+
EventContext,
29+
makeCloudFunction,
2830
} from '../cloud-functions';
29-
import * as firebase from 'firebase-admin';
30-
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
// SOFTWARE.
2222

2323
import {
24-
makeCloudFunction,
2524
CloudFunction,
2625
EventContext,
26+
makeCloudFunction,
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
import { posix } from 'path';
24-
import * as _ from 'lodash';
2523
import * as firebase from 'firebase-admin';
24+
import * as _ from 'lodash';
25+
import { posix } from 'path';
2626
import { apps } from '../apps';
2727
import {
28-
makeCloudFunction,
29-
CloudFunction,
3028
Change,
29+
CloudFunction,
3130
Event,
3231
EventContext,
32+
makeCloudFunction,
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23+
import * as cors from 'cors';
2324
import * as express from 'express';
2425
import * as firebase from 'firebase-admin';
2526
import * as _ from 'lodash';
26-
import * as cors from 'cors';
2727
import { apps } from '../apps';
2828
import { HttpsFunction, optsToTrigger, Runnable } from '../cloud-functions';
29-
import { DeploymentOptions } from '../function-builder';
29+
import { DeploymentOptions } from '../function-configuration';
3030

3131
/**
3232
*

0 commit comments

Comments
 (0)