Skip to content

Commit 7d2c8dc

Browse files
authored
Allow users to present String and Integer params in a ManifestSpec (#1166)
1 parent c9c55cf commit 7d2c8dc

File tree

7 files changed

+50
-14
lines changed

7 files changed

+50
-14
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const functions = require("../../../../src/index");
2+
const functionsv2 = require("../../../../src/v2/index");
3+
const { defineString } = require("../../../../src/v2/params");
4+
5+
defineString("FOO");
6+
7+
exports.v1http = functions.https.onRequest((req, resp) => {
8+
resp.status(200).send("PASS");
9+
});
10+
11+
exports.v1callable = functions.https.onCall(() => {
12+
return "PASS";
13+
});
14+
15+
exports.v2http = functionsv2.https.onRequest((req, resp) => {
16+
resp.status(200).send("PASS");
17+
});
18+
19+
exports.v2callable = functionsv2.https.onCall(() => {
20+
return "PASS";
21+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "commonjs-params"
3+
}

spec/runtime/loader.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ describe('loadStack', () => {
310310
},
311311
},
312312
},
313+
{
314+
name: 'has params',
315+
modulePath: './spec/fixtures/sources/commonjs-params',
316+
expected: { ...expected, params: [{ name: 'FOO', type: 'string' }] },
317+
},
313318
];
314319

315320
for (const tc of testcases) {

src/runtime/loader.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828
ManifestStack,
2929
} from './manifest';
3030

31+
import * as params from '../v2/params';
32+
3133
/**
3234
* Dynamically load import function to prevent TypeScript from
3335
* transpiling into a require.
@@ -112,9 +114,13 @@ export async function loadStack(functionsDir: string): Promise<ManifestStack> {
112114

113115
extractStack(mod, endpoints, requiredAPIs);
114116

115-
return {
117+
const stack: ManifestStack = {
116118
endpoints,
117119
specVersion: 'v1alpha1',
118120
requiredAPIs: mergeRequiredAPIs(requiredAPIs),
119121
};
122+
if (params.declaredParams.length > 0) {
123+
stack.params = params.declaredParams.map((p) => p.toSpec());
124+
}
125+
return stack;
120126
}

src/runtime/manifest.ts

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

23+
import { ParamSpec } from '../v2/params/types';
24+
2325
/**
2426
* An definition of a function as appears in the Manifest.
2527
*/
@@ -87,6 +89,7 @@ export interface ManifestRequiredAPI {
8789
*/
8890
export interface ManifestStack {
8991
specVersion: 'v1alpha1';
92+
params?: ParamSpec[];
9093
requiredAPIs: ManifestRequiredAPI[];
9194
endpoints: Record<string, ManifestEndpoint>;
9295
}

src/v2/params/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* @hidden
2525
* @alpha
2626
*/
27+
2728
import {
2829
BooleanParam,
2930
FloatParam,

src/v2/params/types.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,13 @@ export interface ParamSpec<T = unknown> {
2828
default?: T;
2929
label?: string;
3030
description?: string;
31-
valueType?: ParamValueType;
31+
type: ParamValueType;
3232
}
3333

34-
export type ParamOptions<T = unknown> = Omit<
35-
ParamSpec<T>,
36-
'name' | 'valueType'
37-
>;
34+
export type ParamOptions<T = unknown> = Omit<ParamSpec<T>, 'name' | 'type'>;
3835

3936
export class Param<T = unknown> {
40-
static valueType: ParamValueType = 'string';
37+
static type: ParamValueType = 'string';
4138

4239
constructor(readonly name: string, readonly options: ParamOptions<T> = {}) {}
4340

@@ -61,7 +58,7 @@ export class Param<T = unknown> {
6158
const out: ParamSpec = {
6259
name: this.name,
6360
...this.options,
64-
valueType: (this.constructor as typeof Param).valueType,
61+
type: (this.constructor as typeof Param).type,
6562
};
6663
if (this.options.default && typeof this.options.default !== 'string') {
6764
out.default = (this.options.default as
@@ -78,7 +75,7 @@ export class StringParam extends Param<string> {
7875
}
7976

8077
export class IntParam extends Param<number> {
81-
static valueType: ParamValueType = 'int';
78+
static type: ParamValueType = 'int';
8279

8380
get value(): number {
8481
const intVal = parseInt(
@@ -97,7 +94,7 @@ export class IntParam extends Param<number> {
9794
}
9895

9996
export class FloatParam extends Param<number> {
100-
static valueType: ParamValueType = 'float';
97+
static type: ParamValueType = 'float';
10198

10299
get value(): number {
103100
const floatVal = parseFloat(
@@ -115,7 +112,7 @@ export class FloatParam extends Param<number> {
115112
}
116113

117114
export class BooleanParam extends Param {
118-
static valueType: ParamValueType = 'boolean';
115+
static type: ParamValueType = 'boolean';
119116

120117
get value(): boolean {
121118
const lowerVal = (
@@ -137,7 +134,7 @@ export class BooleanParam extends Param {
137134
}
138135

139136
export class ListParam extends Param<string[]> {
140-
static valueType: ParamValueType = 'list';
137+
static type: ParamValueType = 'list';
141138

142139
get value(): string[] {
143140
return typeof this.rawValue === 'string'
@@ -148,7 +145,7 @@ export class ListParam extends Param<string[]> {
148145
toSpec(): ParamSpec<string> {
149146
const out: ParamSpec = {
150147
name: this.name,
151-
valueType: 'list',
148+
type: 'list',
152149
...this.options,
153150
};
154151
if (this.options.default && this.options.default.length > 0) {
@@ -160,7 +157,7 @@ export class ListParam extends Param<string[]> {
160157
}
161158

162159
export class JSONParam<T = any> extends Param<T> {
163-
static valueType: ParamValueType = 'json';
160+
static type: ParamValueType = 'json';
164161

165162
get value(): T {
166163
if (this.rawValue) {

0 commit comments

Comments
 (0)