Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions etc/firebase-admin.remote-config.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ export class RemoteConfig {
// (undocumented)
readonly app: App;
createTemplateFromJSON(json: string): RemoteConfigTemplate;
getServerTemplate(options?: RemoteConfigServerTemplateOptions): Promise<RemoteConfigServerTemplate>;
getTemplate(): Promise<RemoteConfigTemplate>;
getTemplateAtVersion(versionNumber: number | string): Promise<RemoteConfigTemplate>;
initServerTemplate(options?: RemoteConfigServerTemplateOptions): RemoteConfigServerTemplate;
listVersions(options?: ListVersionsOptions): Promise<ListVersionsResult>;
publishTemplate(template: RemoteConfigTemplate, options?: {
force: boolean;
Expand Down Expand Up @@ -84,6 +86,43 @@ export interface RemoteConfigParameterGroup {
// @public
export type RemoteConfigParameterValue = ExplicitParameterValue | InAppDefaultValue;

// @public
export interface RemoteConfigServerCondition {
expression: string;
name: string;
}

// @public
export type RemoteConfigServerConfig = {
[key: string]: string | boolean | number;
};

// @public
export interface RemoteConfigServerTemplate {
cache: RemoteConfigServerTemplateData;
defaultConfig: RemoteConfigServerConfig;
evaluate(): RemoteConfigServerConfig;
load(): Promise<void>;
}

// @public
export interface RemoteConfigServerTemplateData {
conditions: RemoteConfigServerCondition[];
readonly etag: string;
parameters: {
[key: string]: RemoteConfigParameter;
};
version?: Version;
}

// @public
export interface RemoteConfigServerTemplateOptions {
// (undocumented)
defaultConfig?: RemoteConfigServerConfig;
// (undocumented)
template?: RemoteConfigServerTemplateData;
}

// @public
export interface RemoteConfigTemplate {
conditions: RemoteConfigCondition[];
Expand Down
5 changes: 5 additions & 0 deletions src/remote-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export {
RemoteConfigParameterGroup,
RemoteConfigParameterValue,
RemoteConfigTemplate,
RemoteConfigServerCondition,
RemoteConfigServerConfig,
RemoteConfigServerTemplate,
RemoteConfigServerTemplateData,
RemoteConfigServerTemplateOptions,
RemoteConfigUser,
TagColor,
Version,
Expand Down
48 changes: 45 additions & 3 deletions src/remote-config/remote-config-api-client-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ import { PrefixedFirebaseError } from '../utils/error';
import * as utils from '../utils/index';
import * as validator from '../utils/validator';
import { deepCopy } from '../utils/deep-copy';
import { ListVersionsOptions, ListVersionsResult, RemoteConfigTemplate } from './remote-config-api';
import {
ListVersionsOptions,
ListVersionsResult,
RemoteConfigTemplate,
RemoteConfigServerTemplateData
} from './remote-config-api';

// Remote Config backend constants
const FIREBASE_REMOTE_CONFIG_V1_API = 'https://firebaseremoteconfig.googleapis.com/v1';
// Honors env param to enable URL override independent of binary change.
const FIREBASE_REMOTE_CONFIG_URL_BASE = process.env.FIREBASE_REMOTE_CONFIG_URL_BASE || 'https://firebaseremoteconfig.googleapis.com';
const FIREBASE_REMOTE_CONFIG_HEADERS = {
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`,
// There is a known issue in which the ETag is not properly returned in cases where the request
Expand Down Expand Up @@ -166,6 +172,24 @@ export class RemoteConfigApiClient {
});
}

public getServerTemplate(): Promise<RemoteConfigServerTemplateData> {
return this.getUrl()
.then((url) => {
const request: HttpRequestConfig = {
method: 'GET',
url: `${url}/namespaces/firebase-server/serverRemoteConfig`,
headers: FIREBASE_REMOTE_CONFIG_HEADERS
};
return this.httpClient.send(request);
})
.then((resp) => {
return this.toRemoteConfigServerTemplate(resp);
})
.catch((err) => {
throw this.toFirebaseError(err);
});
}

private sendPutRequest(template: RemoteConfigTemplate, etag: string, validateOnly?: boolean): Promise<HttpResponse> {
let path = 'remoteConfig';
if (validateOnly) {
Expand All @@ -191,7 +215,7 @@ export class RemoteConfigApiClient {
private getUrl(): Promise<string> {
return this.getProjectIdPrefix()
.then((projectIdPrefix) => {
return `${FIREBASE_REMOTE_CONFIG_V1_API}/${projectIdPrefix}`;
return `${FIREBASE_REMOTE_CONFIG_URL_BASE}/v1/${projectIdPrefix}`;
});
}

Expand Down Expand Up @@ -255,6 +279,24 @@ export class RemoteConfigApiClient {
};
}

/**
* Creates a RemoteConfigServerTemplate from the API response.
* If provided, customEtag is used instead of the etag returned in the API response.
*
* @param {HttpResponse} resp API response object.
* @param {string} customEtag A custom etag to replace the etag fom the API response (Optional).
*/
private toRemoteConfigServerTemplate(resp: HttpResponse, customEtag?: string): RemoteConfigServerTemplateData {
const etag = (typeof customEtag === 'undefined') ? resp.headers['etag'] : customEtag;
this.validateEtag(etag);
return {
conditions: resp.data.conditions,
parameters: resp.data.parameters,
etag,
version: resp.data.version,
};
}

/**
* Checks if the given RemoteConfigTemplate object is valid.
* The object must have valid parameters, parameter groups, conditions, and an etag.
Expand Down
88 changes: 87 additions & 1 deletion src/remote-config/remote-config-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ export interface RemoteConfigCondition {
tagColor?: TagColor;
}

/**
* Interface representing a Remote Config condition in the data-plane.
* A condition targets a specific group of users. A list of these conditions make up
* part of a Remote Config template.
*/
export interface RemoteConfigServerCondition {

/**
* A non-empty and unique name of this condition.
*/
name: string;

/**
* The logic of this condition.
* See the documentation on
* {@link https://firebase.google.com/docs/remote-config/condition-reference | condition expressions}
* for the expected syntax of this field.
*/
expression: string;
}

/**
* Interface representing an explicit parameter value.
*/
Expand Down Expand Up @@ -135,7 +156,7 @@ export interface RemoteConfigParameterGroup {
}

/**
* Interface representing a Remote Config template.
* Interface representing a Remote Config client template.
*/
export interface RemoteConfigTemplate {
/**
Expand Down Expand Up @@ -167,6 +188,66 @@ export interface RemoteConfigTemplate {
version?: Version;
}

/**
* Interface representing the data in a Remote Config server template.
*/
export interface RemoteConfigServerTemplateData {
/**
* A list of conditions in descending order by priority.
*/
conditions: RemoteConfigServerCondition[];

/**
* Map of parameter keys to their optional default values and optional conditional values.
*/
parameters: { [key: string]: RemoteConfigParameter };

/**
* ETag of the current Remote Config template (readonly).
*/
readonly etag: string;

/**
* Version information for the current Remote Config template.
*/
version?: Version;
}

/**
* Represents optional arguments that can be used when instantiating {@link RemoteConfigServerTemplate}.
*/
export interface RemoteConfigServerTemplateOptions {
defaultConfig?: RemoteConfigServerConfig,
template?: RemoteConfigServerTemplateData,
}

/**
* Interface representing a stateful abstraction for a Remote Config server template.
*/
export interface RemoteConfigServerTemplate {

/**
* Cached {@link RemoteConfigServerTemplateData}
*/
cache: RemoteConfigServerTemplateData;

/**
* A {@link RemoteConfigServerConfig} containing default values for Config
*/
defaultConfig: RemoteConfigServerConfig;

/**
* Evaluates the current template to produce a {@link RemoteConfigServerConfig}
*/
evaluate(): RemoteConfigServerConfig;

/**
* Fetches and caches the current active version of the
* {@link RemoteConfigServerTemplate} of the project.
*/
load(): Promise<void>;
}

/**
* Interface representing a Remote Config user.
*/
Expand Down Expand Up @@ -289,3 +370,8 @@ export interface ListVersionsOptions {
*/
endTime?: Date | string;
}

/**
* Type representing the configuration produced by evaluating a server template.
*/
export type RemoteConfigServerConfig = { [key: string]: string | boolean | number }
Loading