Skip to content

Commit 6e5d68b

Browse files
committed
Add generic type parameter to defineJsonSecret for type safety
1 parent 00c6245 commit 6e5d68b

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/params/index.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export {
5151

5252
export { ParamOptions, Expression };
5353

54-
type SecretOrExpr = Param<any> | SecretParam | JsonSecretParam;
54+
type SecretOrExpr = Param<any> | SecretParam | JsonSecretParam<any>;
5555
export const declaredParams: SecretOrExpr[] = [];
5656

5757
/**
@@ -129,27 +129,38 @@ export function defineSecret(name: string): SecretParam {
129129
* This is useful for managing groups of related configuration values, such as all settings
130130
* for a third-party API, as a single unit.
131131
*
132-
* The secret value must be valid JSON. At runtime, the value will be automatically parsed
133-
* and returned as a JavaScript object. If the value is not valid JSON, an error will be thrown.
132+
* The secret value must be a valid JSON string. At runtime, the value will be automatically parsed
133+
* and returned as a JavaScript object. If the value is not set or is not valid JSON, an error will be thrown.
134134
*
135135
* @param name The name of the environment variable to use to load the parameter.
136136
* @returns A parameter whose `.value()` method returns the parsed JSON object.
137137
*
138138
* @example
139139
* ```typescript
140+
* // Without type parameter
140141
* const stripeConfig = defineJsonSecret("STRIPE_CONFIG");
142+
* const { apiKey, webhookSecret, clientId } = stripeConfig.value();
143+
*
144+
* // With type parameter for type safety
145+
* interface StripeConfig {
146+
* apiKey: string;
147+
* webhookSecret: string;
148+
* clientId: string;
149+
* }
150+
* const stripeConfig = defineJsonSecret<StripeConfig>("STRIPE_CONFIG");
151+
* const { apiKey } = stripeConfig.value(); // apiKey is typed as string
141152
*
142153
* exports.myApi = onRequest(
143154
* { secrets: [stripeConfig] },
144155
* (req, res) => {
145-
* const { apiKey, webhookSecret, clientId } = stripeConfig.value();
156+
* const config = stripeConfig.value();
146157
* // ... use the configuration values
147158
* }
148159
* );
149160
* ```
150161
*/
151-
export function defineJsonSecret(name: string): JsonSecretParam {
152-
const param = new JsonSecretParam(name);
162+
export function defineJsonSecret<T = any>(name: string): JsonSecretParam<T> {
163+
const param = new JsonSecretParam<T>(name);
153164
registerParam(param);
154165
return param;
155166
}

src/params/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ export class SecretParam {
474474
* secrets array while defining a Function to make their values accessible during execution
475475
* of that Function.
476476
*/
477-
export class JsonSecretParam {
477+
export class JsonSecretParam<T = any> {
478478
static type: ParamValueType = "secret";
479479
name: string;
480480

@@ -483,7 +483,7 @@ export class JsonSecretParam {
483483
}
484484

485485
/** @internal */
486-
runtimeValue(): any {
486+
runtimeValue(): T {
487487
const val = process.env[this.name];
488488
if (val === undefined) {
489489
throw new Error(
@@ -492,7 +492,7 @@ export class JsonSecretParam {
492492
}
493493

494494
try {
495-
return JSON.parse(val);
495+
return JSON.parse(val) as T;
496496
} catch (error) {
497497
throw new Error(
498498
`"${this.name}" could not be parsed as JSON. Please verify its value in Secret Manager.`
@@ -510,7 +510,7 @@ export class JsonSecretParam {
510510
}
511511

512512
/** Returns the secret's parsed JSON value at runtime. Throws an error if accessed during deployment or if the value is not valid JSON. */
513-
value(): any {
513+
value(): T {
514514
if (process.env.FUNCTIONS_CONTROL_API === "true") {
515515
throw new Error(
516516
`Cannot access the value of secret "${this.name}" during function deployment. Secret values are only available at runtime.`

0 commit comments

Comments
 (0)