You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add defineJsonSecret API for structured secret configuration (#1745)
* Add defineJsonSecret API for structured secret configuration
Implements defineJsonSecret() to store JSON objects in Cloud Secret Manager.
Useful for consolidating related secrets (e.g., API keys, webhooks, client IDs)
into a single secret, reducing costs and improving organization.
Features:
- Automatic JSON parsing with error handling
- Supports object destructuring
- Throws on missing or invalid JSON
Wire protocol changes (backward compatible):
- Added optional format field to ParamSpec/WireParamSpec
- JsonSecretParam.toSpec() returns format: "json" as CLI hint
- Old CLIs ignore unknown fields, new CLIs can enhance UX
- Format is NOT stored in Secret Manager (just in param spec)
* Add changelog entry for defineJsonSecret API
* Add generic type parameter to defineJsonSecret for type safety
* Update src/params/types.ts
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
* Update src/params/types.ts
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
* nit: doc comments.
---------
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
'No value found for secret parameter "NON_EXISTENT". A function can only access a secret if you include the secret in the function\'s dependency array.'
Copy file name to clipboardExpand all lines: src/params/types.ts
+56Lines changed: 56 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -307,6 +307,8 @@ export type ParamSpec<T extends string | number | boolean | string[]> = {
307
307
type: ParamValueType;
308
308
/** The way in which the Firebase CLI will prompt for the value of this parameter. Defaults to a TextInput. */
309
309
input?: ParamInput<T>;
310
+
/** Optional format annotation for additional type information (e.g., "json" for JSON-encoded secrets). */
311
+
format?: string;
310
312
};
311
313
312
314
/**
@@ -324,6 +326,7 @@ export type WireParamSpec<T extends string | number | boolean | string[]> = {
324
326
description?: string;
325
327
type: ParamValueType;
326
328
input?: ParamInput<T>;
329
+
format?: string;
327
330
};
328
331
329
332
/** Configuration options which can be used to customize the prompting behavior of a parameter. */
@@ -464,6 +467,59 @@ export class SecretParam {
464
467
}
465
468
}
466
469
470
+
/**
471
+
* A parametrized object whose value is stored as a JSON string in Cloud Secret Manager.
472
+
* This is useful for managing groups of related configuration values, such as all settings
473
+
* for a third-party API, as a single unit. Supply instances of JsonSecretParam to the
474
+
* secrets array while defining a Function to make their values accessible during execution
475
+
* of that Function.
476
+
*/
477
+
exportclassJsonSecretParam<T=any>{
478
+
statictype: ParamValueType="secret";
479
+
name: string;
480
+
481
+
constructor(name: string){
482
+
this.name=name;
483
+
}
484
+
485
+
/** @internal */
486
+
runtimeValue(): T{
487
+
constval=process.env[this.name];
488
+
if(val===undefined){
489
+
thrownewError(
490
+
`No value found for secret parameter "${this.name}". A function can only access a secret if you include the secret in the function's dependency array.`
491
+
);
492
+
}
493
+
494
+
try{
495
+
returnJSON.parse(val)asT;
496
+
}catch(error){
497
+
thrownewError(
498
+
`"${this.name}" could not be parsed as JSON. Please verify its value in Secret Manager. Details: ${error}`
499
+
);
500
+
}
501
+
}
502
+
503
+
/** @internal */
504
+
toSpec(): ParamSpec<string>{
505
+
return{
506
+
type: "secret",
507
+
name: this.name,
508
+
format: "json",
509
+
};
510
+
}
511
+
512
+
/** Returns the secret's parsed JSON value at runtime. Throws an error if accessed during deployment, if the secret is not set, or if the value is not valid JSON. */
513
+
value(): T{
514
+
if(process.env.FUNCTIONS_CONTROL_API==="true"){
515
+
thrownewError(
516
+
`Cannot access the value of secret "${this.name}" during function deployment. Secret values are only available at runtime.`
517
+
);
518
+
}
519
+
returnthis.runtimeValue();
520
+
}
521
+
}
522
+
467
523
/**
468
524
* A parametrized value of String type that will be read from .env files
469
525
* if present, or prompted for by the CLI if missing.
0 commit comments