diff --git a/spec/fixtures/sources/commonjs-params/index.js b/spec/fixtures/sources/commonjs-params/index.js new file mode 100644 index 000000000..d038a2f77 --- /dev/null +++ b/spec/fixtures/sources/commonjs-params/index.js @@ -0,0 +1,21 @@ +const functions = require("../../../../src/index"); +const functionsv2 = require("../../../../src/v2/index"); +const { defineString } = require("../../../../src/v2/params"); + +defineString("FOO"); + +exports.v1http = functions.https.onRequest((req, resp) => { + resp.status(200).send("PASS"); +}); + +exports.v1callable = functions.https.onCall(() => { + return "PASS"; +}); + +exports.v2http = functionsv2.https.onRequest((req, resp) => { + resp.status(200).send("PASS"); +}); + +exports.v2callable = functionsv2.https.onCall(() => { + return "PASS"; +}); diff --git a/spec/fixtures/sources/commonjs-params/package.json b/spec/fixtures/sources/commonjs-params/package.json new file mode 100644 index 000000000..91f8c11da --- /dev/null +++ b/spec/fixtures/sources/commonjs-params/package.json @@ -0,0 +1,3 @@ +{ + "name": "commonjs-params" +} diff --git a/spec/runtime/loader.spec.ts b/spec/runtime/loader.spec.ts index 9994557c4..bb959a3cb 100644 --- a/spec/runtime/loader.spec.ts +++ b/spec/runtime/loader.spec.ts @@ -310,6 +310,11 @@ describe('loadStack', () => { }, }, }, + { + name: 'has params', + modulePath: './spec/fixtures/sources/commonjs-params', + expected: { ...expected, params: [{ name: 'FOO', type: 'string' }] }, + }, ]; for (const tc of testcases) { diff --git a/src/runtime/loader.ts b/src/runtime/loader.ts index de31eefd3..4dd4b41d2 100644 --- a/src/runtime/loader.ts +++ b/src/runtime/loader.ts @@ -28,6 +28,8 @@ import { ManifestStack, } from './manifest'; +import * as params from '../v2/params'; + /** * Dynamically load import function to prevent TypeScript from * transpiling into a require. @@ -112,9 +114,13 @@ export async function loadStack(functionsDir: string): Promise { extractStack(mod, endpoints, requiredAPIs); - return { + const stack: ManifestStack = { endpoints, specVersion: 'v1alpha1', requiredAPIs: mergeRequiredAPIs(requiredAPIs), }; + if (params.declaredParams.length > 0) { + stack.params = params.declaredParams.map((p) => p.toSpec()); + } + return stack; } diff --git a/src/runtime/manifest.ts b/src/runtime/manifest.ts index 562d1a52f..d63088303 100644 --- a/src/runtime/manifest.ts +++ b/src/runtime/manifest.ts @@ -20,6 +20,8 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +import { ParamSpec } from '../v2/params/types'; + /** * An definition of a function as appears in the Manifest. */ @@ -87,6 +89,7 @@ export interface ManifestRequiredAPI { */ export interface ManifestStack { specVersion: 'v1alpha1'; + params?: ParamSpec[]; requiredAPIs: ManifestRequiredAPI[]; endpoints: Record; } diff --git a/src/v2/params/index.ts b/src/v2/params/index.ts index bf45e8c45..3f8a080b6 100644 --- a/src/v2/params/index.ts +++ b/src/v2/params/index.ts @@ -24,6 +24,7 @@ * @hidden * @alpha */ + import { BooleanParam, FloatParam, diff --git a/src/v2/params/types.ts b/src/v2/params/types.ts index 9612dc8dc..fc898c15c 100644 --- a/src/v2/params/types.ts +++ b/src/v2/params/types.ts @@ -28,16 +28,13 @@ export interface ParamSpec { default?: T; label?: string; description?: string; - valueType?: ParamValueType; + type: ParamValueType; } -export type ParamOptions = Omit< - ParamSpec, - 'name' | 'valueType' ->; +export type ParamOptions = Omit, 'name' | 'type'>; export class Param { - static valueType: ParamValueType = 'string'; + static type: ParamValueType = 'string'; constructor(readonly name: string, readonly options: ParamOptions = {}) {} @@ -61,7 +58,7 @@ export class Param { const out: ParamSpec = { name: this.name, ...this.options, - valueType: (this.constructor as typeof Param).valueType, + type: (this.constructor as typeof Param).type, }; if (this.options.default && typeof this.options.default !== 'string') { out.default = (this.options.default as @@ -78,7 +75,7 @@ export class StringParam extends Param { } export class IntParam extends Param { - static valueType: ParamValueType = 'int'; + static type: ParamValueType = 'int'; get value(): number { const intVal = parseInt( @@ -97,7 +94,7 @@ export class IntParam extends Param { } export class FloatParam extends Param { - static valueType: ParamValueType = 'float'; + static type: ParamValueType = 'float'; get value(): number { const floatVal = parseFloat( @@ -115,7 +112,7 @@ export class FloatParam extends Param { } export class BooleanParam extends Param { - static valueType: ParamValueType = 'boolean'; + static type: ParamValueType = 'boolean'; get value(): boolean { const lowerVal = ( @@ -137,7 +134,7 @@ export class BooleanParam extends Param { } export class ListParam extends Param { - static valueType: ParamValueType = 'list'; + static type: ParamValueType = 'list'; get value(): string[] { return typeof this.rawValue === 'string' @@ -148,7 +145,7 @@ export class ListParam extends Param { toSpec(): ParamSpec { const out: ParamSpec = { name: this.name, - valueType: 'list', + type: 'list', ...this.options, }; if (this.options.default && this.options.default.length > 0) { @@ -160,7 +157,7 @@ export class ListParam extends Param { } export class JSONParam extends Param { - static valueType: ParamValueType = 'json'; + static type: ParamValueType = 'json'; get value(): T { if (this.rawValue) {