{"version":3,"file":"fullerstack-ngx-config.mjs","sources":["../../../../libs/ngx-config/src/lib/config.constant.ts","../../../../libs/ngx-config/src/lib/config.model.ts","../../../../libs/ngx-config/src/lib/config.default.ts","../../../../libs/ngx-config/src/lib/config.service.ts","../../../../libs/ngx-config/src/lib/config.util.ts","../../../../libs/ngx-config/src/lib/config.module.ts","../../../../libs/ngx-config/src/fullerstack-ngx-config.ts"],"sourcesContent":["/**\n * @license\n * Copyright Neekware Inc. All Rights Reserved.\n *\n * Use of this source code is governed by a proprietary notice\n * that can be found at http://neekware.com/license/PRI.html\n */\n\n/** Default remote http call timeout */\nexport const DEFAULT_HTTP_TIMEOUT = 3;\n","/**\n * @license\n * Copyright Neekware Inc. All Rights Reserved.\n *\n * Use of this source code is governed by a proprietary notice\n * that can be found at http://neekware.com/license/PRI.html\n */\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\n/**\n * Http Methods - GET / POST Supported\n */\nexport enum HttpMethod {\n  GET = 'GET',\n  POST = 'POST',\n}\n\nexport interface EnvironmentConfig {\n  // true for production build\n  production: boolean;\n  // application release version\n  version?: string;\n  // application name\n  appName?: string;\n  // extra attributes\n  [id: string]: any;\n}\n\nexport interface LocalConfig {\n  // if target supports multi-tab apps (browsers)\n  multiTab?: boolean;\n  // url to login page for user authentication (for anonymous users only)\n  loginPageUrl?: string;\n  // url to sign-up page for user to signup (for anonymous users only)\n  signupPageUrl?: string;\n  // url to landing page for authenticated users only\n  loggedInLandingPageUrl?: string;\n  // url to the page where users are redirected to after log-out\n  loggedOutRedirectUrl?: string;\n  // extra attributes\n  [id: string]: any;\n}\n\nexport interface RemoteConfig {\n  // url to fetch config file from\n  endpoint: string;\n  // http headers to be sent with request\n  headers?: { [id: string]: any };\n  // post body to be sent with request\n  body?: { [id: string]: any };\n  // http method (get, post) (if post, body will be ignored)\n  method?: HttpMethod;\n  // maximum time in seconds to wait for remote config response\n  timeout?: number;\n  // extra attributes\n  [id: string]: any;\n}\n\nexport interface ApplicationConfig extends EnvironmentConfig {\n  // local config\n  localConfig?: LocalConfig;\n  // remote config (json object)\n  remoteConfig?: RemoteConfig;\n  // received data from remote\n  remoteData?: { [id: string]: any };\n  // extra modules (ext.auth, ext.log)\n  [id: string]: any;\n}\n\nexport type RemoteType = Promise<any>;\n","/**\n * @license\n * Copyright Neekware Inc. All Rights Reserved.\n *\n * Use of this source code is governed by a proprietary notice\n * that can be found at http://neekware.com/license/PRI.html\n */\n\nimport { InjectionToken } from '@angular/core';\nimport { DeepReadonly } from 'ts-essentials';\n\nimport { DEFAULT_HTTP_TIMEOUT } from './config.constant';\nimport { ApplicationConfig, HttpMethod, LocalConfig, RemoteConfig } from './config.model';\n\n/** Default local config */\nexport const DefaultBaseConfig: DeepReadonly<LocalConfig> = {\n  multiTab: true,\n  loginPageUrl: '/auth/login',\n  signupPageUrl: '/auth/signup',\n  loggedInLandingPageUrl: '/',\n  loggedOutRedirectUrl: '/',\n};\n\n/** Default remote config fetch */\nexport const DefaultRemoteConfig: DeepReadonly<RemoteConfig> = {\n  endpoint: null,\n  method: HttpMethod.GET,\n  timeout: DEFAULT_HTTP_TIMEOUT,\n  headers: { 'Content-Type': 'application/json' },\n  body: {},\n};\n\n/** Default application-wide config */\nexport const DefaultApplicationConfig: DeepReadonly<ApplicationConfig> = {\n  production: false,\n  version: '1.0.0',\n  appName: '@fullerstack/ngx-config',\n  localConfig: DefaultBaseConfig,\n  remoteConfig: DefaultRemoteConfig,\n  remoteData: {},\n};\n\n/** App configuration options token */\nexport const CONFIG_TOKEN = new InjectionToken<string>('CONFIG_TOKEN');\n","/**\n * @license\n * Copyright Neekware Inc. All Rights Reserved.\n *\n * Use of this source code is governed by a proprietary notice\n * that can be found at http://neekware.com/license/PRI.html\n */\n\nimport { HttpClient, HttpHeaders } from '@angular/common/http';\nimport { Inject, Injectable } from '@angular/core';\nimport { cloneDeep as ldDeepClone, mergeWith as ldMergeWith } from 'lodash-es';\nimport { of } from 'rxjs';\nimport { catchError, timeout } from 'rxjs/operators';\nimport { DeepReadonly } from 'ts-essentials';\n\nimport { DEFAULT_HTTP_TIMEOUT } from './config.constant';\nimport { CONFIG_TOKEN, DefaultApplicationConfig } from './config.default';\nimport { ApplicationConfig, HttpMethod, RemoteType } from './config.model';\n\n@Injectable({ providedIn: 'root' })\nexport class ConfigService {\n  private nameSpace = 'CONFIG';\n  options: DeepReadonly<ApplicationConfig> = DefaultApplicationConfig;\n\n  constructor(readonly http: HttpClient, @Inject(CONFIG_TOKEN) readonly config: ApplicationConfig) {\n    this.options = ldMergeWith(ldDeepClone(this.options), config);\n    if (!this.options.production) {\n      /* istanbul ignore next */\n      console.log(\n        `%c${new Date().toISOString()} [INFO]`,\n        `color:teal`,\n        `[${this.nameSpace}] ConfigService ready ...`\n      );\n    }\n  }\n\n  /**\n   * Fetches remote configuration options via get or post\n   */\n  fetchRemoteConfig(): RemoteType {\n    const remoteConfig = this.options.remoteConfig;\n    if (remoteConfig) {\n      const url = remoteConfig.endpoint;\n      if (url) {\n        return new Promise((resolve) => {\n          let headers = remoteConfig.headers || {};\n          if (!Object.keys(headers).length) {\n            headers = new HttpHeaders(headers);\n          }\n          const httpMethod = remoteConfig.method || HttpMethod.GET;\n          let httpRequest = this.http.get(url, { headers });\n          if (httpMethod === HttpMethod.POST) {\n            const postBody = remoteConfig.body || {};\n            httpRequest = this.http.post(url, postBody, { headers });\n          }\n          const httpTimeout = (remoteConfig.timeout || DEFAULT_HTTP_TIMEOUT) * 1000;\n          httpRequest\n            .pipe(\n              timeout(httpTimeout),\n              catchError((err: Response) => {\n                console.warn(`ConfigService failed. (${err.statusText || 'unknown'})`);\n                return of({});\n              })\n            )\n            .toPromise()\n            .then((resp) => {\n              if (Object.keys(resp || {}).length) {\n                if (!this.options.production) {\n                  /* istanbul ignore next */\n                  console.log(`ConfigService remote config fetched ...`);\n                }\n                this.options = { ...this.options, remoteData: resp };\n              }\n              resolve(resp);\n            });\n        });\n      }\n    }\n    return new Promise((resolve) => resolve({}));\n  }\n}\n","/**\n * @license\n * Copyright Neekware Inc. All Rights Reserved.\n *\n * Use of this source code is governed by a proprietary notice\n * that can be found at http://neekware.com/license/PRI.html\n */\n\nimport { RemoteType } from './config.model';\nimport { ConfigService } from './config.service';\n\n/**\n * Remote Config Fetch Factory\n * @param config ConfigService Injectable\n */\nexport function remoteConfigFactory(config: ConfigService): () => RemoteType {\n  return () => config.fetchRemoteConfig();\n}\n","/**\n * @license\n * Copyright Neekware Inc. All Rights Reserved.\n *\n * Use of this source code is governed by a proprietary notice\n * that can be found at http://neekware.com/license/PRI.html\n */\n\nimport { CommonModule } from '@angular/common';\nimport { APP_INITIALIZER, ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';\nimport { DeepReadonly } from 'ts-essentials';\n\nimport { CONFIG_TOKEN } from './config.default';\nimport { ApplicationConfig } from './config.model';\nimport { ConfigService } from './config.service';\nimport { remoteConfigFactory } from './config.util';\n\n@NgModule({\n  imports: [CommonModule],\n})\nexport class ConfigModule {\n  constructor(\n    @Optional()\n    @SkipSelf()\n    parentModule: ConfigModule\n  ) {\n    if (parentModule) {\n      /* istanbul ignore next */\n      throw new Error('ConfigModule is already loaded. Import it in the AppModule only');\n    }\n  }\n\n  static forRoot(options?: DeepReadonly<ApplicationConfig>): ModuleWithProviders<ConfigModule> {\n    return {\n      ngModule: ConfigModule,\n      providers: [\n        ConfigService,\n        { provide: CONFIG_TOKEN, useValue: options },\n        {\n          provide: APP_INITIALIZER,\n          useFactory: remoteConfigFactory,\n          deps: [ConfigService],\n          multi: true,\n        },\n      ],\n    };\n  }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":["ldMergeWith","ldDeepClone"],"mappings":";;;;;;;;;AAAA;;;;;;;AAQA;MACa,oBAAoB,GAAG;;ACTpC;;;;;;;AAQA;AAEA;;;IAGY;AAAZ,WAAY,UAAU;IACpB,yBAAW,CAAA;IACX,2BAAa,CAAA;AACf,CAAC,EAHW,UAAU,KAAV,UAAU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