From 48b1ca736bb14c6f972581c97af23683d9ac761b Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Wed, 17 Feb 2021 16:48:36 +0200 Subject: [PATCH] Fix web implementation to return an object to be consistent with the native implementations --- src/definitions.ts | 13 +++++++- src/web.ts | 75 +++++++++++++++++----------------------------- 2 files changed, 39 insertions(+), 49 deletions(-) diff --git a/src/definitions.ts b/src/definitions.ts index 72cd73a..b5d1e57 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -12,7 +12,7 @@ export interface FirebaseRemoteConfigPlugin { activate(): Promise; fetchAndActivate(): Promise; getBoolean(options: RCValueOption): Promise; - getByteArray(options: RCValueOption): Promise; + getByteArray(options: RCValueOption): Promise; getNumber(options: RCValueOption): Promise; getString(options: RCValueOption): Promise; } @@ -32,3 +32,14 @@ export interface RCReturnDataArray { value: any[]; source: string; } + +export interface FirebaseInitOptions { + apiKey: string; + authDomain: string; + databaseURL: string; + projectId: string; + storageBucket: string; + messagingSenderId: string; + appId: string; + measurementId: string; +} diff --git a/src/web.ts b/src/web.ts index 1667487..c2b1a31 100644 --- a/src/web.ts +++ b/src/web.ts @@ -8,7 +8,8 @@ import { declare var window: any; -export class FirebaseRemoteConfigWeb extends WebPlugin +export class FirebaseRemoteConfigWeb + extends WebPlugin implements FirebaseRemoteConfigPlugin { public readonly ready: Promise; private readyResolver: Function; @@ -152,62 +153,40 @@ export class FirebaseRemoteConfigWeb extends WebPlugin } getBoolean(options: RCValueOption): Promise { - return new Promise(async (resolve, reject) => { - await this.ready; - - if (!this.remoteConfigRef) { - reject( - "Remote config is not initialized. Make sure initialize() is called at first." - ); - return; - } - - resolve(this.remoteConfigRef.getValue(options.key).asBoolean()); - }); + return this.getValue(options, "Boolean"); } - getByteArray(options: RCValueOption): Promise { - return new Promise(async (resolve, reject) => { - await this.ready; - - if (!this.remoteConfigRef) { - reject( - "Remote config is not initialized. Make sure initialize() is called at first." - ); - return; - } - - resolve(this.remoteConfigRef.getValue(options.key).asString()); - }); + getByteArray(options: RCValueOption): Promise { + return this.getString(options); } - getNumber(options: RCValueOption): Promise { - return new Promise(async (resolve, reject) => { - await this.ready; - - if (!this.remoteConfigRef) { - reject( - "Remote config is not initialized. Make sure initialize() is called at first." - ); - return; - } - resolve(this.remoteConfigRef.getValue(options.key).asNumber()); - }); + getNumber(options: RCValueOption): Promise { + return this.getValue(options, "Number"); } getString(options: RCValueOption): Promise { - return new Promise(async (resolve, reject) => { - await this.ready; + return this.getValue(options, "String"); + } - if (!this.remoteConfigRef) { - reject( - "Remote config is not initialized. Make sure initialize() is called at first." - ); - return; - } + private async getValue( + options: RCValueOption, + format: "String" | "Number" | "Boolean" = null + ): Promise { + await this.ready; - resolve(this.remoteConfigRef.getValue(options.key).asString()); - }); + if (!this.remoteConfigRef) { + throw new Error( + "Remote config is not initialized. Make sure initialize() is called at first." + ); + } + + const retVal = this.remoteConfigRef.getValue(options.key); + + return { + key: options.key, + value: format ? retVal[`as${format}`]() : retVal._value, + source: retVal._source, + }; } get remoteConfig() {