Skip to content
10 changes: 5 additions & 5 deletions spec/function-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('FunctionBuilder', () => {
expect(() => {
functions
.runWith({ timeoutSeconds: 90, memory: '256MB' })
.region('unsupported');
.region('unsupported' as any);
}).to.throw(Error, 'region');
});

Expand Down Expand Up @@ -165,21 +165,21 @@ describe('FunctionBuilder', () => {

it('should throw an error if user chooses an invalid region', () => {
expect(() => {
return functions.region('unsupported');
return functions.region('unsupported' as any);
}).to.throw(Error, 'region');

expect(() => {
return functions.region('unsupported').runWith({
return functions.region('unsupported' as any).runWith({
timeoutSeconds: 500,
} as any);
}).to.throw(Error, 'region');

expect(() => {
return functions.region('unsupported', 'us-east1');
return functions.region('unsupported' as any, 'us-east1');
}).to.throw(Error, 'region');

expect(() => {
return functions.region('unsupported', 'us-east1').runWith({
return functions.region('unsupported' as any, 'us-east1').runWith({
timeoutSeconds: 500,
} as any);
}).to.throw(Error, 'region');
Expand Down
16 changes: 1 addition & 15 deletions src/cloud-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import { Request, Response } from 'express';
import * as _ from 'lodash';
import { apps } from './apps';
import { DeploymentOptions } from './function-builder';
import { DeploymentOptions, Schedule } from './function-configuration';
export { Request, Response };

const WILDCARD_REGEX = new RegExp('{[^/{}]*}', 'g');
Expand Down Expand Up @@ -168,20 +168,6 @@ export interface TriggerAnnotated {
};
}

export interface ScheduleRetryConfig {
retryCount?: number;
maxRetryDuration?: string;
minBackoffDuration?: string;
maxBackoffDuration?: string;
maxDoublings?: number;
}

export interface Schedule {
schedule: string;
timeZone?: string;
retryConfig?: ScheduleRetryConfig;
}

/** A Runnable has a `run` method which directly invokes the user-defined function - useful for unit testing. */
export interface Runnable<T> {
run: (data: T, context: any) => PromiseLike<any> | any;
Expand Down
59 changes: 14 additions & 45 deletions src/function-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
import * as express from 'express';
import * as _ from 'lodash';

import { CloudFunction, EventContext, Schedule } from './cloud-functions';
import { CloudFunction, EventContext } from './cloud-functions';
import {
DeploymentOptions,
MAX_TIMEOUT_SECONDS,
RuntimeOptions,
SUPPORTED_REGIONS,
VALID_MEMORY_OPTIONS,
} from './function-configuration';
import * as analytics from './providers/analytics';
import * as auth from './providers/auth';
import * as crashlytics from './providers/crashlytics';
Expand All @@ -34,34 +41,6 @@ import * as pubsub from './providers/pubsub';
import * as remoteConfig from './providers/remoteConfig';
import * as storage from './providers/storage';

/**
* List of all regions supported by Cloud Functions.
*/
const SUPPORTED_REGIONS = [
'us-central1',
'us-east1',
'us-east4',
'europe-west1',
'europe-west2',
'asia-east2',
'asia-northeast1',
];

/**
* List of available memory options supported by Cloud Functions.
*/
const VALID_MEMORY_OPTS = ['128MB', '256MB', '512MB', '1GB', '2GB'];

// Adding this memory type here to error on compile for TS users.
// Unfortunately I have not found a way to merge this with VALID_MEMORY_OPS
// without it being super ugly. But here they are right next to each other at least.
type Memory = '128MB' | '256MB' | '512MB' | '1GB' | '2GB';

/**
* Cloud Functions max timeout value.
*/
const MAX_TIMEOUT_SECONDS = 540;

/**
* Assert that the runtime options passed in are valid.
* @param runtimeOptions object containing memory and timeout information.
Expand All @@ -70,10 +49,10 @@ const MAX_TIMEOUT_SECONDS = 540;
function assertRuntimeOptionsValid(runtimeOptions: RuntimeOptions): boolean {
if (
runtimeOptions.memory &&
!_.includes(VALID_MEMORY_OPTS, runtimeOptions.memory)
!_.includes(VALID_MEMORY_OPTIONS, runtimeOptions.memory)
) {
throw new Error(
`The only valid memory allocation values are: ${VALID_MEMORY_OPTS.join(
`The only valid memory allocation values are: ${VALID_MEMORY_OPTIONS.join(
', '
)}`
);
Expand Down Expand Up @@ -111,7 +90,9 @@ function assertRegionsAreValid(regions: string[]): boolean {
* @param regions One of more region strings.
* For example: `functions.region('us-east1')` or `functions.region('us-east1', 'us-central1')`
*/
export function region(...regions: string[]): FunctionBuilder {
export function region(
...regions: Array<typeof SUPPORTED_REGIONS[number]>
): FunctionBuilder {
if (assertRegionsAreValid(regions)) {
return new FunctionBuilder({ regions });
}
Expand All @@ -130,18 +111,6 @@ export function runWith(runtimeOptions: RuntimeOptions): FunctionBuilder {
}
}

export interface RuntimeOptions {
timeoutSeconds?: number;
memory?: Memory;
}

export interface DeploymentOptions {
regions?: string[];
timeoutSeconds?: number;
memory?: Memory;
schedule?: Schedule;
}

export class FunctionBuilder {
constructor(private options: DeploymentOptions) {}

Expand All @@ -150,7 +119,7 @@ export class FunctionBuilder {
* @param regions One or more region strings.
* For example: `functions.region('us-east1')` or `functions.region('us-east1', 'us-central1')`
*/
region(...regions: string[]): FunctionBuilder {
region(...regions: Array<typeof SUPPORTED_REGIONS[number]>): FunctionBuilder {
if (assertRegionsAreValid(regions)) {
this.options.regions = regions;
return this;
Expand Down
69 changes: 69 additions & 0 deletions src/function-configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* List of all regions supported by Cloud Functions.
*/
export const SUPPORTED_REGIONS = [
'us-central1',
'us-east1',
'us-east4',
'europe-west1',
'europe-west2',
'asia-east2',
'asia-northeast1',
] as const;

/**
* Cloud Functions min timeout value.
*/
export const MIN_TIMEOUT_SECONDS = 0;

/**
* Cloud Functions max timeout value.
*/
export const MAX_TIMEOUT_SECONDS = 540;

/**
* List of available memory options supported by Cloud Functions.
*/
export const VALID_MEMORY_OPTIONS = [
'128MB',
'256MB',
'512MB',
'1GB',
'2GB',
] as const;

/**
* Scheduler retry options. Applies only to scheduled functions.
*/
export interface ScheduleRetryConfig {
retryCount?: number;
maxRetryDuration?: string;
minBackoffDuration?: string;
maxBackoffDuration?: string;
maxDoublings?: number;
}

/**
* Configuration options for scheduled functions.
*/
export interface Schedule {
schedule: string;
timeZone?: string;
retryConfig?: ScheduleRetryConfig;
}

export interface RuntimeOptions {
/**
* Amount of memory to allocate to the function.
*/
memory?: typeof VALID_MEMORY_OPTIONS[number];
/**
* Timeout for the function in seconds, possible values are 0 to 540.
*/
timeoutSeconds?: number;
}

export interface DeploymentOptions extends RuntimeOptions {
regions?: string[];
schedule?: Schedule;
}
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
// Providers:
import * as analytics from './providers/analytics';
import * as auth from './providers/auth';

import * as apps from './apps';
import { handler } from './handler-builder';
import * as crashlytics from './providers/crashlytics';
import * as database from './providers/database';
import * as firestore from './providers/firestore';
import * as https from './providers/https';
import * as pubsub from './providers/pubsub';
import * as remoteConfig from './providers/remoteConfig';
import * as storage from './providers/storage';

import * as apps from './apps';
import { handler } from './handler-builder';
import { setup } from './setup';

const app = apps.apps();
Expand All @@ -52,8 +52,9 @@ export {
};

// Exported root types:
export * from './config';
export * from './cloud-functions';
export * from './config';
export * from './function-builder';
export * from './function-configuration';

setup();
2 changes: 1 addition & 1 deletion src/providers/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
EventContext,
makeCloudFunction,
} from '../cloud-functions';
import { DeploymentOptions } from '../function-builder';
import { DeploymentOptions } from '../function-configuration';

/** @internal */
export const provider = 'google.analytics';
Expand Down
2 changes: 1 addition & 1 deletion src/providers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
EventContext,
makeCloudFunction,
} from '../cloud-functions';
import { DeploymentOptions } from '../function-builder';
import { DeploymentOptions } from '../function-configuration';

/** @internal */
export const provider = 'google.firebase.auth';
Expand Down
2 changes: 1 addition & 1 deletion src/providers/crashlytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
EventContext,
makeCloudFunction,
} from '../cloud-functions';
import { DeploymentOptions } from '../function-builder';
import { DeploymentOptions } from '../function-configuration';

/** @internal */
export const provider = 'google.firebase.crashlytics';
Expand Down
2 changes: 1 addition & 1 deletion src/providers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
makeCloudFunction,
} from '../cloud-functions';
import { firebaseConfig } from '../config';
import { DeploymentOptions } from '../function-builder';
import { DeploymentOptions } from '../function-configuration';
import { applyChange, joinPath, normalizePath, pathParts } from '../utils';

/** @internal */
Expand Down
2 changes: 1 addition & 1 deletion src/providers/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
makeCloudFunction,
} from '../cloud-functions';
import { dateToTimestampProto } from '../encoder';
import { DeploymentOptions } from '../function-builder';
import { DeploymentOptions } from '../function-configuration';

/** @internal */
export const provider = 'google.firestore';
Expand Down
2 changes: 1 addition & 1 deletion src/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import * as firebase from 'firebase-admin';
import * as _ from 'lodash';
import { apps } from '../apps';
import { HttpsFunction, optsToTrigger, Runnable } from '../cloud-functions';
import { DeploymentOptions } from '../function-builder';
import { DeploymentOptions } from '../function-configuration';

/**
*
Expand Down
6 changes: 4 additions & 2 deletions src/providers/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import {
CloudFunction,
EventContext,
makeCloudFunction,
} from '../cloud-functions';
import {
DeploymentOptions,
Schedule,
ScheduleRetryConfig,
} from '../cloud-functions';
import { DeploymentOptions } from '../function-builder';
} from '../function-configuration';

/** @internal */
export const provider = 'google.pubsub';
Expand Down
2 changes: 1 addition & 1 deletion src/providers/remoteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
EventContext,
makeCloudFunction,
} from '../cloud-functions';
import { DeploymentOptions } from '../function-builder';
import { DeploymentOptions } from '../function-configuration';

/** @internal */
export const provider = 'google.firebase.remoteconfig';
Expand Down
2 changes: 1 addition & 1 deletion src/providers/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
makeCloudFunction,
} from '../cloud-functions';
import { firebaseConfig } from '../config';
import { DeploymentOptions } from '../function-builder';
import { DeploymentOptions } from '../function-configuration';

/** @internal */
export const provider = 'google.storage';
Expand Down