Skip to content

Commit

Permalink
Fix web implementation to return an object to be consistent with the …
Browse files Browse the repository at this point in the history
…native implementations
  • Loading branch information
pierredup committed Feb 17, 2021
1 parent b404186 commit 48b1ca7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 49 deletions.
13 changes: 12 additions & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface FirebaseRemoteConfigPlugin {
activate(): Promise<void>;
fetchAndActivate(): Promise<void>;
getBoolean(options: RCValueOption): Promise<RCReturnData>;
getByteArray(options: RCValueOption): Promise<RCReturnDataArray>;
getByteArray(options: RCValueOption): Promise<RCReturnData>;
getNumber(options: RCValueOption): Promise<RCReturnData>;
getString(options: RCValueOption): Promise<RCReturnData>;
}
Expand All @@ -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;
}
75 changes: 27 additions & 48 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
private readyResolver: Function;
Expand Down Expand Up @@ -152,62 +153,40 @@ export class FirebaseRemoteConfigWeb extends WebPlugin
}

getBoolean(options: RCValueOption): Promise<RCReturnData> {
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<RCReturnDataArray> {
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<RCReturnData> {
return this.getString(options);
}
getNumber(options: RCValueOption): Promise<RCReturnData> {
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<RCReturnData> {
return this.getValue(options, "Number");
}

getString(options: RCValueOption): Promise<RCReturnData> {
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<RCReturnData> {
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() {
Expand Down

0 comments on commit 48b1ca7

Please sign in to comment.