@dagonmetric/ng-config-firebase-remote-config
Version:
Google Firebase Remote Config integration of @dagonMetric/ng-config for Angular applications.
206 lines • 7.13 kB
JavaScript
/**
* @fileoverview added by tsickle
* Generated from: src/firebase-remote-config-provider.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @license
* Copyright DagonMetric. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found under the LICENSE file in the root directory of this source tree.
*/
import { Inject, Injectable, NgZone, PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { EMPTY, of } from 'rxjs';
import { filter, map, observeOn, shareReplay, startWith, switchMap, tap } from 'rxjs/operators';
import { FIREBASE_REMOTE_CONFIG_PROVIDER_OPTIONS } from './firebase-remote-config-provider-options';
import { firebaseAppFactory } from './firebase-app-factory';
import { ZoneScheduler } from './zone-helpers';
import * as i0 from "@angular/core";
import * as i1 from "./firebase-remote-config-provider-options";
/**
* @record
*/
function NestedConfigSection() { }
/** @type {?} */
const doubleUnderscoreRegExp = /__/g;
/** @type {?} */
const validKeyRegExp = /^[_]?[a-zA-Z]/;
/** @type {?} */
const isValidKeys = (/**
* @param {?} keys
* @return {?}
*/
(keys) => {
for (const key of keys) {
if (!validKeyRegExp.test(key)) {
return false;
}
}
return true;
});
const ɵ0 = isValidKeys;
export class FirebaseRemoteConfigProvider {
/**
* @param {?} options
* @param {?} platformId
* @param {?} ngZone
*/
constructor(options,
// eslint-disable-next-line @typescript-eslint/ban-types
platformId, ngZone) {
this.options = options;
this.ngZone = ngZone;
this.isBrowser = isPlatformBrowser(platformId);
/** @type {?} */
const rc$ = of(undefined).pipe(observeOn(this.ngZone.runOutsideAngular((/**
* @return {?}
*/
() => new ZoneScheduler(Zone.current)))), switchMap((/**
* @return {?}
*/
() => (this.isBrowser ? import('firebase/remote-config') : EMPTY))), map((/**
* @return {?}
*/
() => firebaseAppFactory(this.options.firebaseConfig, this.ngZone, this.options.appName))), map((/**
* @param {?} app
* @return {?}
*/
(app) => app.remoteConfig())), tap((/**
* @param {?} rc
* @return {?}
*/
(rc) => {
if (this.options.remoteConfigSettings) {
rc.settings = (/** @type {?} */ (this.options.remoteConfigSettings));
}
})), startWith((/** @type {?} */ (((/** @type {?} */ (undefined)))))), shareReplay({ bufferSize: 1, refCount: false }));
this.rc = rc$.pipe(filter((/**
* @param {?} rc
* @return {?}
*/
(rc) => !!rc)));
}
/**
* @return {?}
*/
get name() {
return 'FirebaseRemoteConfigProvider';
}
/**
* @return {?}
*/
load() {
return this.rc.pipe(switchMap((/**
* @param {?} rc
* @return {?}
*/
(rc) => this.ngZone.runOutsideAngular((/**
* @return {?}
*/
async () => {
if (!this.isBrowser) {
return {};
}
await rc.activate();
try {
await rc.fetch();
await rc.activate();
}
catch (fetchError) {
if (this.options.throwIfLoadError) {
throw fetchError;
}
}
await rc.ensureInitialized();
return rc.getAll();
})))), map((/**
* @param {?} config
* @return {?}
*/
(config) => {
/** @type {?} */
const allkeys = Object.keys(config);
/** @type {?} */
const mappedConfig = {};
for (const key of allkeys) {
/** @type {?} */
const valueStr = config[key].asString();
/** @type {?} */
let normalizedKey = key;
if (this.options.prefix) {
if (!key.toLowerCase().startsWith(this.options.prefix.toLowerCase())) {
continue;
}
else {
normalizedKey = key.substr(this.options.prefix.length);
}
}
/** @type {?} */
const nestedKeys = normalizedKey.split(doubleUnderscoreRegExp);
if (nestedKeys.length > 1 && isValidKeys(nestedKeys)) {
/** @type {?} */
const firstKey = nestedKeys[0];
if (!mappedConfig[firstKey] || typeof mappedConfig[firstKey] !== 'object') {
mappedConfig[firstKey] = {};
}
/** @type {?} */
let accObj = (/** @type {?} */ (mappedConfig[firstKey]));
for (let i = 1; i < nestedKeys.length; i++) {
/** @type {?} */
const currentKey = nestedKeys[i];
if (i === nestedKeys.length - 1) {
accObj[currentKey] = valueStr;
break;
}
if (!accObj[currentKey] || typeof accObj[currentKey] !== 'object') {
accObj[currentKey] = {};
}
accObj = (/** @type {?} */ (accObj[currentKey]));
}
}
else {
mappedConfig[normalizedKey] = valueStr;
}
}
return mappedConfig;
})));
}
}
/** @nocollapse */ FirebaseRemoteConfigProvider.ɵprov = i0.ɵɵdefineInjectable({ factory: function FirebaseRemoteConfigProvider_Factory() { return new FirebaseRemoteConfigProvider(i0.ɵɵinject(i1.FIREBASE_REMOTE_CONFIG_PROVIDER_OPTIONS), i0.ɵɵinject(i0.PLATFORM_ID), i0.ɵɵinject(i0.NgZone)); }, token: FirebaseRemoteConfigProvider, providedIn: "any" });
FirebaseRemoteConfigProvider.decorators = [
{ type: Injectable, args: [{
providedIn: 'any'
},] }
];
/** @nocollapse */
FirebaseRemoteConfigProvider.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Inject, args: [FIREBASE_REMOTE_CONFIG_PROVIDER_OPTIONS,] }] },
{ type: Object, decorators: [{ type: Inject, args: [PLATFORM_ID,] }] },
{ type: NgZone }
];
if (false) {
/**
* @type {?}
* @private
*/
FirebaseRemoteConfigProvider.prototype.isBrowser;
/**
* @type {?}
* @private
*/
FirebaseRemoteConfigProvider.prototype.rc;
/**
* @type {?}
* @private
*/
FirebaseRemoteConfigProvider.prototype.options;
/**
* @type {?}
* @private
*/
FirebaseRemoteConfigProvider.prototype.ngZone;
}
export { ɵ0 };
//# sourceMappingURL=firebase-remote-config-provider.js.map