{"version":3,"file":"ngx-easy-i18n-js-http-loader.mjs","sources":["../../../projects/http-loader/src/lib/http-easy-i18n.loader.ts","../../../projects/http-loader/src/lib/scoped-http-easy-i18n.loader.ts","../../../projects/http-loader/src/public-api.ts","../../../projects/http-loader/src/ngx-easy-i18n-js-http-loader.ts"],"sourcesContent":["import { HttpClient } from '@angular/common/http';\r\nimport { catchError, forkJoin, Observable, of } from 'rxjs';\r\nimport { EasyI18nMessages } from 'easy-i18n-js';\r\nimport { EasyI18nLoader } from '@ngx-easy-i18n-js/core';\r\nimport * as lodash from 'lodash';\r\nimport { map } from 'rxjs/operators';\r\n\r\nexport interface IHttpEasyI18nLoaderOptions {\r\n  /**\r\n   * Url prefix, default '/assets/i18n/' or Array of prefix\r\n   */\r\n  prefix?: string | string[];\r\n  /**\r\n   * Url suffix, default '.json'\r\n   */\r\n  suffix?: string;\r\n}\r\n\r\nexport class HttpEasyI18nLoader extends EasyI18nLoader {\r\n\r\n  private options: IHttpEasyI18nLoaderOptions;\r\n\r\n  constructor(\r\n    private httpClient: HttpClient,\r\n    options?: IHttpEasyI18nLoaderOptions\r\n  ) {\r\n    super();\r\n\r\n    this.options = {\r\n      prefix: '/assets/i18n/',\r\n      suffix: '.json',\r\n      ...(options ?? {})\r\n    };\r\n  }\r\n\r\n  /**\r\n   * Return messages with locale with HttpClient `${this.options.prefix ?? ''}${locale}${this.options.suffix ?? ''}`\r\n   * @param locale\r\n   */\r\n  getMessages(locale: string): Observable<EasyI18nMessages> {\r\n    const prefix = lodash.isArray(this.options.prefix) ? this.options.prefix : [this.options.prefix];\r\n    return forkJoin(\r\n      prefix.map(p => {\r\n          const url = `${p ?? ''}${locale}${this.options.suffix ?? ''}`;\r\n          return this.httpClient.get<EasyI18nMessages>(url).pipe(\r\n            catchError((err) => {\r\n              console.error(`Failed to load file ${url}`, err);\r\n              return of({});\r\n            })\r\n          );\r\n        }\r\n      )\r\n    ).pipe(\r\n      map(res => lodash.defaultsDeep({}, ...lodash.compact(res))),\r\n    );\r\n  }\r\n}\r\n","import { HttpClient } from '@angular/common/http';\nimport { forkJoin, from, Observable } from 'rxjs';\nimport { EasyI18nMessages } from 'easy-i18n-js';\nimport { EasyI18nLoader, EasyI18nService } from '@ngx-easy-i18n-js/core';\nimport * as lodash from 'lodash';\nimport { map } from 'rxjs/operators';\nimport { HttpEasyI18nLoader } from './http-easy-i18n.loader';\nimport { CanActivateFn } from '@angular/router';\nimport { inject } from '@angular/core';\n\nexport interface IScopedHttpEasyI18nLoaderScope {\n  /**\n   * Url prefix, Array of prefix\n   */\n  prefix: string | string[];\n  /**\n   * Alias\n   */\n  scope?: string;\n}\n\nexport interface IScopedHttpEasyI18nLoaderOptions {\n  /**\n   * Url suffix, default '.json'\n   */\n  suffix?: string;\n}\n\nexport class ScopedHttpEasyI18nLoader extends EasyI18nLoader {\n\n  private readonly list: { loader: EasyI18nLoader; scope: string; }[];\n\n  constructor(\n    readonly httpClient: HttpClient,\n    readonly scopes: IScopedHttpEasyI18nLoaderScope[],\n    readonly options?: IScopedHttpEasyI18nLoaderOptions\n  ) {\n    super();\n\n    this.list = scopes.map(s => ({\n      loader: new HttpEasyI18nLoader(httpClient, {\n        prefix: s.prefix,\n        suffix: options?.suffix ?? '.json'\n      }),\n      scope: s.scope ?? ''\n    }));\n  }\n\n  override getMessages(locale: string): Observable<EasyI18nMessages> {\n    return forkJoin(\n      this.list.map(l => l.loader.getMessages(locale).pipe(\n        map(res => {\n          if (!l.scope) {\n            return res;\n          }\n          return lodash.set({}, l.scope, res)\n        })\n      ))\n    ).pipe(\n      map(datas => lodash.defaultsDeep({}, ...lodash.compact(datas)))\n    );\n  }\n}\n\nexport function appendScopedHttpEasyI18nLoader(\n  scopes: IScopedHttpEasyI18nLoaderScope[],\n  options?: IScopedHttpEasyI18nLoaderOptions\n): CanActivateFn {\n  let alreadyAppened = false;\n  return () => {\n    if (alreadyAppened) {\n      return true;\n    }\n\n    alreadyAppened = true;\n\n    const httpClient = inject(HttpClient);\n    const easyI18nService = inject(EasyI18nService);\n    return from(easyI18nService.appendLoader(new ScopedHttpEasyI18nLoader(httpClient, scopes, options))).pipe(\n      map(() => true)\n    );\n  }\n}\n","import { Provider } from '@angular/core';\r\nimport { EasyI18nLoader } from '@ngx-easy-i18n-js/core';\r\nimport { HttpClient } from '@angular/common/http';\r\nimport { HttpEasyI18nLoader, IHttpEasyI18nLoaderOptions } from './lib/http-easy-i18n.loader';\r\nimport { IScopedHttpEasyI18nLoaderScope, ScopedHttpEasyI18nLoader } from './lib/scoped-http-easy-i18n.loader';\r\n\r\nexport * from './lib/http-easy-i18n.loader';\r\nexport * from './lib/scoped-http-easy-i18n.loader';\r\n\r\nexport function provideEasyI18nLoader(options?: IHttpEasyI18nLoaderOptions): Provider[] {\r\n  return [\r\n    {\r\n      provide: EasyI18nLoader,\r\n      deps: [HttpClient],\r\n      useFactory: (httpClient: HttpClient) => new HttpEasyI18nLoader(httpClient, options)\r\n    }\r\n  ]\r\n}\r\n\r\nexport function provideEasyI18nScopedLoader(scopes: IScopedHttpEasyI18nLoaderScope[], options?: IHttpEasyI18nLoaderOptions): Provider[] {\r\n  return [\r\n    {\r\n      provide: EasyI18nLoader,\r\n      deps: [HttpClient],\r\n      useFactory: (httpClient: HttpClient) => new ScopedHttpEasyI18nLoader(httpClient, scopes, options)\r\n    }\r\n  ]\r\n}","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;;;AAkBM,MAAO,kBAAmB,SAAQ,cAAc,CAAA;IAIpD,WAAA,CACU,UAAsB,EAC9B,OAAoC,EAAA;AAEpC,QAAA,KAAK,EAAE;QAHC,IAAA,CAAA,UAAU,GAAV,UAAU;QAKlB,IAAI,CAAC,OAAO,GAAG;AACb,YAAA,MAAM,EAAE,eAAe;AACvB,YAAA,MAAM,EAAE,OAAO;AACf,YAAA,IAAI,OAAO,IAAI,EAAE;SAClB;IACH;AAEA;;;AAGG;AACH,IAAA,WAAW,CAAC,MAAc,E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