-
Notifications
You must be signed in to change notification settings - Fork 410
Add functionality to accept a JSON template string when initializing a RC server template #2520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
43e0f2e
dac5893
de597ee
0608aaf
e926fcd
48fe8a2
295636b
9ae7d96
c7d4c6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…ation
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,10 +35,10 @@ import { | |
| RemoteConfigParameterValue, | ||
| EvaluationContext, | ||
| ServerTemplateData, | ||
| ServerTemplateOptions, | ||
| NamedCondition, | ||
| GetServerTemplateOptions, | ||
| InitServerTemplateOptions, | ||
| } from './remote-config-api'; | ||
| import { isString } from 'lodash'; | ||
|
|
||
| /** | ||
| * The Firebase `RemoteConfig` service interface. | ||
|
|
@@ -185,7 +185,7 @@ export class RemoteConfig { | |
| * Instantiates {@link ServerTemplate} and then fetches and caches the latest | ||
| * template version of the project. | ||
| */ | ||
| public async getServerTemplate(options?: GetServerTemplateOptions): Promise<ServerTemplate> { | ||
| public async getServerTemplate(options?: ServerTemplateOptions): Promise<ServerTemplate> { | ||
| const template = this.initServerTemplate(options); | ||
| await template.load(); | ||
| return template; | ||
|
|
@@ -194,11 +194,24 @@ export class RemoteConfig { | |
| /** | ||
| * Synchronously instantiates {@link ServerTemplate}. | ||
| */ | ||
| public initServerTemplate(options?: InitServerTemplateOptions): ServerTemplate { | ||
| public initServerTemplate(options?: ServerTemplateOptions): ServerTemplate { | ||
| const template = new ServerTemplateImpl( | ||
| this.client, new ConditionEvaluator(), options?.defaultConfig); | ||
| if (options?.template) { | ||
| template.cache = options?.template; | ||
| // Check and instantiates via json string | ||
| if (isString(options?.template)) { | ||
| try { | ||
| template.cache = new ServerTemplateDataImpl(JSON.parse(options?.template)); | ||
| } catch (e) { | ||
| throw new FirebaseRemoteConfigError( | ||
| 'invalid-argument', | ||
| `Failed to parse the JSON string: ${options?.template}. ` + e | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the other errors we throw, we don't include the raw error ( Nit: If we do include the error, it might be more readable to differentiate it from the other error message, something like "... Raw error: ${e}".
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't include it for most errors, but we do have it for the createTemplateFromJSON:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah. I overlooked that. Makes sense. Shipit 🚢 |
||
| ); | ||
| } | ||
| } else { | ||
| // check and instantiates via ServerTemplateData | ||
| template.cache = options?.template; | ||
| } | ||
| } | ||
| return template; | ||
| } | ||
|
|
@@ -297,7 +310,7 @@ class ServerTemplateImpl implements ServerTemplate { | |
| constructor( | ||
| private readonly apiClient: RemoteConfigApiClient, | ||
| private readonly conditionEvaluator: ConditionEvaluator, | ||
| private readonly defaultConfig: ServerConfig = {} | ||
| public readonly defaultConfig: ServerConfig = {} | ||
| ) { } | ||
|
|
||
| /** | ||
|
|
@@ -387,6 +400,14 @@ class ServerTemplateImpl implements ServerTemplate { | |
| return new Proxy(mergedConfig, proxyHandler); | ||
| } | ||
|
|
||
| /** | ||
| * Convenient method that returns the JSON string of the cached template data | ||
| * @returns A JSON-string of this object. | ||
| */ | ||
| public toJSON(): string { | ||
| return JSON.stringify(this.cache); | ||
| } | ||
|
|
||
| /** | ||
| * Private helper method that coerces a parameter value string to the {@link ParameterValueType}. | ||
| */ | ||
|
|
@@ -430,7 +451,7 @@ class ServerTemplateDataImpl implements ServerTemplateData { | |
| } | ||
|
|
||
| this.etag = template.etag; | ||
|
|
||
| if (typeof template.parameters !== 'undefined') { | ||
| if (!validator.isNonNullObject(template.parameters)) { | ||
| throw new FirebaseRemoteConfigError( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.