@@ -32,72 +32,112 @@ import * as https from './providers/https';
3232import * as pubsub from './providers/pubsub' ;
3333import * as remoteConfig from './providers/remoteConfig' ;
3434import * as storage from './providers/storage' ;
35- import {
36- CloudFunction ,
37- EventContext ,
38- Runnable ,
39- TriggerAnnotated ,
40- Schedule ,
41- } from './cloud-functions' ;
35+ import { CloudFunction , EventContext , Schedule } from './cloud-functions' ;
4236
4337/**
44- * Configure the regions that the function is deployed to.
45- * @param regions One of more region strings.
46- * For example: `functions.region('us-east1')` or `functions.region('us-east1', 'us-central1')`
38+ * List of all regions supported by Cloud Functions.
4739 */
48- export function region ( ...regions : string [ ] ) {
49- if ( ! regions . length ) {
50- throw new Error ( 'You must specify at least one region' ) ;
40+ const SUPPORTED_REGIONS = [
41+ 'us-central1' ,
42+ 'us-east1' ,
43+ 'europe-west1' ,
44+ 'europe-west2' ,
45+ 'asia-east2' ,
46+ 'asia-northeast1' ,
47+ ] ;
48+
49+ /**
50+ * List of available memory options supported by Cloud Functions.
51+ */
52+ const VALID_MEMORY_OPTS = [ '128MB' , '256MB' , '512MB' , '1GB' , '2GB' ] ;
53+
54+ // Adding this memory type here to error on compile for TS users.
55+ // Unfortunately I have not found a way to merge this with VALID_MEMORY_OPS
56+ // without it being super ugly. But here they are right next to each other at least.
57+ type Memory = '128MB' | '256MB' | '512MB' | '1GB' | '2GB' ;
58+
59+ /**
60+ * Cloud Functions max timeout value.
61+ */
62+ const MAX_TIMEOUT_SECONDS = 540 ;
63+
64+ /**
65+ * Assert that the runtime options passed in are valid.
66+ * @param runtimeOptions object containing memory and timeout information.
67+ * @throws { Error } Memory and TimeoutSeconds values must be valid.
68+ */
69+ function assertRuntimeOptionsValid ( runtimeOptions : RuntimeOptions ) : boolean {
70+ if (
71+ runtimeOptions . memory &&
72+ ! _ . includes ( VALID_MEMORY_OPTS , runtimeOptions . memory )
73+ ) {
74+ throw new Error (
75+ `The only valid memory allocation values are: ${ VALID_MEMORY_OPTS . join (
76+ ', '
77+ ) } `
78+ ) ;
5179 }
5280 if (
53- _ . difference ( regions , [
54- 'us-central1' ,
55- 'us-east1' ,
56- 'europe-west1' ,
57- 'asia-northeast1' ,
58- ] ) . length
81+ runtimeOptions . timeoutSeconds > MAX_TIMEOUT_SECONDS ||
82+ runtimeOptions . timeoutSeconds < 0
5983 ) {
6084 throw new Error (
61- "The only valid regions are 'us-central1', 'us-east1', 'europe-west1', and 'asia-northeast1'"
85+ `TimeoutSeconds must be between 0 and ${ MAX_TIMEOUT_SECONDS } `
6286 ) ;
6387 }
64- return new FunctionBuilder ( { regions } ) ;
88+ return true ;
6589}
90+
91+ /**
92+ * Assert regions specified are valid.
93+ * @param regions list of regions.
94+ * @throws { Error } Regions must be in list of supported regions.
95+ */
96+ function assertRegionsAreValid ( regions : string [ ] ) : boolean {
97+ if ( ! regions . length ) {
98+ throw new Error ( 'You must specify at least one region' ) ;
99+ }
100+ if ( _ . difference ( regions , SUPPORTED_REGIONS ) . length ) {
101+ throw new Error (
102+ `The only valid regions are: ${ SUPPORTED_REGIONS . join ( ', ' ) } `
103+ ) ;
104+ }
105+ return true ;
106+ }
107+
108+ /**
109+ * Configure the regions that the function is deployed to.
110+ * @param regions One of more region strings.
111+ * For example: `functions.region('us-east1')` or `functions.region('us-east1', 'us-central1')`
112+ */
113+ export function region ( ...regions : string [ ] ) : FunctionBuilder {
114+ if ( assertRegionsAreValid ( regions ) ) {
115+ return new FunctionBuilder ( { regions } ) ;
116+ }
117+ }
118+
66119/**
67120 * Configure runtime options for the function.
68121 * @param runtimeOptions Object with 2 optional fields:
69122 * 1. `timeoutSeconds`: timeout for the function in seconds, possible values are 0 to 540
70123 * 2. `memory`: amount of memory to allocate to the function,
71124 * possible values are: '128MB', '256MB', '512MB', '1GB', and '2GB'.
72125 */
73- export function runWith ( runtimeOptions : {
74- timeoutSeconds ?: number ;
75- memory ?: '128MB' | '256MB' | '512MB' | '1GB' | '2GB' ;
76- } ) {
77- if (
78- runtimeOptions . memory &&
79- ! _ . includes (
80- [ '128MB' , '256MB' , '512MB' , '1GB' , '2GB' ] ,
81- runtimeOptions . memory
82- )
83- ) {
84- throw new Error (
85- "The only valid memory allocation values are: '128MB', '256MB', '512MB', '1GB', and '2GB'"
86- ) ;
87- }
88- if (
89- runtimeOptions . timeoutSeconds > 540 ||
90- runtimeOptions . timeoutSeconds < 0
91- ) {
92- throw new Error ( 'TimeoutSeconds must be between 0 and 540' ) ;
126+ export function runWith ( runtimeOptions : RuntimeOptions ) : FunctionBuilder {
127+ if ( assertRuntimeOptionsValid ( runtimeOptions ) ) {
128+ return new FunctionBuilder ( runtimeOptions ) ;
93129 }
94- return new FunctionBuilder ( runtimeOptions ) ;
130+ }
131+
132+ export interface RuntimeOptions {
133+ timeoutSeconds ?: number ;
134+ memory ?: Memory ;
95135}
96136
97137export interface DeploymentOptions {
98138 regions ?: string [ ] ;
99139 timeoutSeconds ?: number ;
100- memory ?: string ;
140+ memory ?: Memory ;
101141 schedule ?: Schedule ;
102142}
103143
@@ -109,10 +149,12 @@ export class FunctionBuilder {
109149 * @param regions One or more region strings.
110150 * For example: `functions.region('us-east1')` or `functions.region('us-east1', 'us-central1')`
111151 */
112- region = ( ...regions : string [ ] ) => {
113- this . options . regions = regions ;
114- return this ;
115- } ;
152+ region ( ...regions : string [ ] ) : FunctionBuilder {
153+ if ( assertRegionsAreValid ( regions ) ) {
154+ this . options . regions = regions ;
155+ return this ;
156+ }
157+ }
116158
117159 /**
118160 * Configure runtime options for the function.
@@ -121,30 +163,12 @@ export class FunctionBuilder {
121163 * 2. memory: amount of memory to allocate to the function, possible values are:
122164 * '128MB', '256MB', '512MB', '1GB', and '2GB'.
123165 */
124- runWith = ( runtimeOptions : {
125- timeoutSeconds ?: number ;
126- memory ?: '128MB' | '256MB' | '512MB' | '1GB' | '2GB' ;
127- } ) => {
128- if (
129- runtimeOptions . memory &&
130- ! _ . includes (
131- [ '128MB' , '256MB' , '512MB' , '1GB' , '2GB' ] ,
132- runtimeOptions . memory
133- )
134- ) {
135- throw new Error (
136- "The only valid memory allocation values are: '128MB', '256MB', '512MB', '1GB', and '2GB'"
137- ) ;
138- }
139- if (
140- runtimeOptions . timeoutSeconds > 540 ||
141- runtimeOptions . timeoutSeconds < 0
142- ) {
143- throw new Error ( 'TimeoutSeconds must be between 0 and 540' ) ;
166+ runWith ( runtimeOptions : RuntimeOptions ) : FunctionBuilder {
167+ if ( assertRuntimeOptionsValid ( runtimeOptions ) ) {
168+ this . options = _ . assign ( this . options , runtimeOptions ) ;
169+ return this ;
144170 }
145- this . options = _ . assign ( this . options , runtimeOptions ) ;
146- return this ;
147- } ;
171+ }
148172
149173 get https ( ) {
150174 return {
0 commit comments