{"version":3,"file":"o3r-apis-manager.mjs","sources":["../../src/apis-manager/api-manager.ts","../../src/apis-manager/api-manager.token.ts","../../src/apis-manager/api-factory.service.ts","../../src/apis-manager/api-manager.helpers.ts","../../src/apis-manager/api-manager.module.ts","../../src/o3r-apis-manager.ts"],"sourcesContent":["import type {\n  ApiClient,\n  ApiName,\n} from '@ama-sdk/core';\n\n/**\n * Api manager is responsible to provide an api configuration to a service factory, so that it could instantiate an API\n * with the right parameters. It contains a default configuration and a map of specific configurations for API / set of\n * API. Configurations are only exposed through the method getConfiguration, which will merge the default configuration\n * and the requested one.\n */\nexport class ApiManager {\n  private defaultConfiguration: ApiClient;\n  private apiConfigurations: { [key: string]: ApiClient };\n\n  /**\n   * Map of registered Api Client associated to specific API\n   * Warning: This should not be used to get the ApiClient for an API, the function getConfiguration() should be used instead\n   */\n  public get registeredApiConfigurations() {\n    return { ...this.apiConfigurations } as const;\n  }\n\n  /**\n   * Create an API manager using a custom ApiClient\n   * @param defaultConfiguration\n   */\n  constructor(defaultConfiguration: ApiClient, apiConfigurations: { [key: string]: ApiClient } = {}) {\n    this.defaultConfiguration = defaultConfiguration;\n    this.apiConfigurations = apiConfigurations;\n  }\n\n  /**\n   * Retrieve a configuration for a specific API\n   * @param api API to get the configuration for\n   * @note When passing a string the configuration is expecting to exist else an error is thrown\n   * @note when passing an Api instance that does not match a registered configuration, the default one will be returned\n   */\n  public getConfiguration(api?: string | ApiName): ApiClient {\n    if (typeof api === 'string') {\n      if (this.apiConfigurations[api]) {\n        return this.apiConfigurations[api];\n      } else {\n        throw new Error(`Unknown API configuration: ${api}\\nKnown API configurations: ${Object.keys(this.apiConfigurations).join(', ')}`);\n      }\n    }\n    return (api && this.apiConfigurations[api.apiName]) || this.defaultConfiguration;\n  }\n\n  /**\n   * Set or override API configuration\n   * @param apiClient API configuration to override to the given api\n   * @param api API name to override, the default configuration will be used if not specified\n   */\n  public setConfiguration(apiClient: ApiClient, api?: string | ApiName): void {\n    if (api) {\n      this.apiConfigurations[typeof api === 'string' ? api : api.apiName] = apiClient;\n    } else {\n      this.defaultConfiguration = apiClient;\n    }\n  }\n}\n","import {\n  InjectionToken,\n} from '@angular/core';\nimport type {\n  ApiManager,\n} from './api-manager';\n\n/**\n * Token used by the core library to provide an Api manager to services. It can be provided in the app.\n */\nexport const API_TOKEN = new InjectionToken<ApiManager>('Custom API manager token');\n","import type {\n  Api,\n  ApiClient,\n  ApiName,\n} from '@ama-sdk/core';\nimport {\n  Inject,\n  Injectable,\n  InjectionToken,\n  Optional,\n} from '@angular/core';\nimport {\n  ApiManager,\n} from './api-manager';\nimport {\n  API_TOKEN,\n} from './api-manager.token';\n\n/** Type of the Class of an SDK Api */\nexport type ApiClassType<T extends Api = Api> = (new (client: ApiClient) => T) & ApiName;\n\n/**\n * Initial APIs instantiations\n */\nexport const INITIAL_APIS_TOKEN = new InjectionToken<(Api | ApiClassType)[]>('Initial APIs token');\n\n@Injectable()\nexport class ApiFactoryService {\n  /** Map of loaded APIs */\n  private loadedApis: Record<string, Api> = {};\n\n  constructor(@Inject(API_TOKEN) private readonly apiManager: ApiManager, @Optional() @Inject(INITIAL_APIS_TOKEN) apis?: (Api | ApiClassType)[]) {\n    if (apis) {\n      this.updateApiMapping(apis);\n    }\n  }\n\n  /**\n   * Determine if the given parameter is a API class\n   * @param apiClass object to check\n   */\n  private isApiClass<T extends Api = Api>(apiClass: any): apiClass is ApiClassType<T> {\n    return !!apiClass.apiName && typeof apiClass === 'function';\n  }\n\n  /**\n   * Retrieve a specific API with loaded configuration\n   * @param apiClass class of the API to retrieve\n   * @param refreshCache Ignore cached API instance and refresh it\n   * @param customApiName override the `apiName` set in the `apiClass`\n   * @note When passing `customApiName` the configuration is expecting to exist else an error is thrown\n   * @note When passing an Api instance that does not match a registered configuration without `customApiName`, the default one will be returned\n   */\n  public getApi<T extends Api>(apiClass: (new (client: ApiClient) => T) & ApiName, refreshCache = false, customApiName?: string): T {\n    const apiName = customApiName ?? apiClass.apiName;\n    const cache = this.loadedApis[apiName];\n    if (!refreshCache && cache) {\n      return cache as T;\n    }\n\n    const instance = new apiClass(this.getConfigFor(customApiName ?? apiClass));\n    this.loadedApis[apiName] = instance;\n    return instance;\n  }\n\n  /**\n   * Update the Map of loaded APIs.\n   * Note: Can be used to override the a specific API\n   * @param map Map of loaded APIs to update\n   */\n  public updateApiMapping(map: (Api | ApiClassType)[] | Record<string, (Api | ApiClassType)>) {\n    const newItems: Record<string, (Api | ApiClassType)> = Array.isArray(map)\n      ? map\n        .reduce<Record<string, Api | ApiClassType<Api>>>((acc, curr) => {\n          acc[curr.apiName] = curr;\n          return acc;\n        }, {})\n      : map;\n\n    this.loadedApis = {\n      ...this.loadedApis,\n      ...Object.entries(newItems)\n        .reduce<Record<string, Api>>((acc, [apiName, api]) => {\n          acc[apiName] = this.isApiClass(api) ? new api(this.getConfigFor(api)) : api;\n          return acc;\n        }, {})\n    };\n  }\n\n  /**\n   * Clear the cache of loaded APIs\n   * @param apis Whitelist of APIs to clear from the cache, if specified only these apis will be removed from the cache\n   */\n  public clearCache(apis?: (ApiName | string)[]) {\n    if (apis) {\n      apis.forEach((api) => delete this.loadedApis[typeof api === 'string' ? api : api.apiName]);\n    } else {\n      this.loadedApis = {};\n    }\n  }\n\n  /**\n   * Retrieve the configuration for a specific API\n   * @param api API for which retrieving the configuration\n   */\n  public getConfigFor(api: string | ApiName): ApiClient {\n    return this.apiManager.getConfiguration(api);\n  }\n}\n","/**\n * Add a preconnect `<link>` element to the DOM\n * @param baseUrl the origin href\n * @param supportCrossOrigin add crossorigin attribute to the link element\n */\nexport function appendPreconnect(baseUrl: string, supportCrossOrigin = true): void {\n  const preConnectLink = document.createElement('link');\n  preConnectLink.setAttribute('rel', 'preconnect');\n  preConnectLink.setAttribute('href', baseUrl);\n  if (supportCrossOrigin) {\n    preConnectLink.setAttribute('crossorigin', '');\n  }\n  document.head.append(preConnectLink);\n}\n","import {\n  makeEnvironmentProviders,\n  ModuleWithProviders,\n  NgModule,\n} from '@angular/core';\nimport {\n  ApiFactoryService,\n} from './api-factory.service';\nimport {\n  ApiManager,\n} from './api-manager';\nimport {\n  API_TOKEN,\n} from './api-manager.token';\n\n/**\n * Module that needs to be imported by the application to instantiate an SDK configuration.\n */\n@NgModule({\n  providers: [\n    ApiFactoryService\n  ]\n})\nexport class ApiManagerModule {\n  /**\n   * Provide a custom {@link ApiManager}\n   * A factory can be provided via injection to the token {@link API_TOKEN}\n   * @param apiManager\n   * @deprecated Please use {@link provideApiManager} instead, will be removed in v14.\n   */\n  public static forRoot(apiManager: ApiManager): ModuleWithProviders<ApiManagerModule> {\n    return {\n      ngModule: ApiManagerModule,\n      providers: [\n        { provide: API_TOKEN, useValue: apiManager }\n      ]\n    };\n  }\n}\n\n/**\n * Provide a custom {@link ApiManager}\n * A factory can be provided via injection to the token {@link API_TOKEN}\n * @param apiManager\n */\nexport function provideApiManager(apiManager: ApiManager) {\n  return makeEnvironmentProviders([\n    ApiFactoryService,\n    { provide: API_TOKEN, useValue: apiManager }\n  ]);\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public_api';\n"],"names":[],"mappings":";;;AAKA;;;;;AAKG;MACU,UAAU,CAAA;AAIrB;;;AAGG;AACH,IAAA,IAAW,2BAA2B,GAAA;AACpC,QAAA,OAAO,EAAE,GAAG,IAAI,CAAC,iBAAiB,EAAW;;AAG/C;;;AAGG;IACH,WAAY,CAAA,oBAA+B,EAAE,iBAAA,GAAkD,EAAE,EAAA;AAC/F,QAAA,IAAI,CAAC,oBAAoB,GAAG,oBAAoB;AAChD,QAAA,IAAI,CAAC,iBAAiB,GAAG,iBAAiB;;AAG5C;;;;;AAKG;AACI,IAAA,gBAAgB,CAAC,GAAsB,EAAA;AAC5C,QAAA,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;AAC3B,YAAA,IAAI,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,EAAE;AAC/B,gBAAA,OAAO,IAAI,CAAC,iBAAiB,CAAC,GA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