@o3r/apis-manager
Version:
This module provides services to help you communicate with your APIs. Its responsibility is to provide an API configuration to a service factory so that it could instantiate an API with the right configurations. It contains a default configuration and a m
110 lines (103 loc) • 4.65 kB
TypeScript
import { Api, ApiClient, ApiName } from '@ama-sdk/core';
import * as i0 from '@angular/core';
import { InjectionToken } from '@angular/core';
/** Type of the Class of an SDK Api */
type ApiClassType<T extends Api = Api> = (new (client: ApiClient) => T) & ApiName;
/**
* Initial APIs instantiations
*/
declare const INITIAL_APIS_TOKEN: InjectionToken<(Api | ApiClassType<Api>)[]>;
declare class ApiFactoryService {
private readonly apiManager;
/** Map of loaded APIs */
private loadedApis;
constructor();
/**
* Determine if the given parameter is a API class
* @param apiClass object to check
*/
private isApiClass;
/**
* Retrieve a specific API with loaded configuration
* @param apiClass class of the API to retrieve
* @param refreshCache Ignore cached API instance and refresh it
* @param customApiName override the `apiName` set in the `apiClass`
* @note When passing `customApiName` the configuration is expecting to exist else an error is thrown
* @note When passing an Api instance that does not match a registered configuration without `customApiName`, the default one will be returned
*/
getApi<T extends Api>(apiClass: (new (client: ApiClient) => T) & ApiName, refreshCache?: boolean, customApiName?: string): T;
/**
* Update the Map of loaded APIs.
* Note: Can be used to override the a specific API
* @param map Map of loaded APIs to update
*/
updateApiMapping(map: (Api | ApiClassType)[] | Record<string, (Api | ApiClassType)>): void;
/**
* Clear the cache of loaded APIs
* @param apis Whitelist of APIs to clear from the cache, if specified only these apis will be removed from the cache
*/
clearCache(apis?: (ApiName | string)[]): void;
/**
* Retrieve the configuration for a specific API
* @param api API for which retrieving the configuration
*/
getConfigFor(api: string | ApiName): ApiClient;
static ɵfac: i0.ɵɵFactoryDeclaration<ApiFactoryService, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<ApiFactoryService>;
}
/**
* Api manager is responsible to provide an api configuration to a service factory, so that it could instantiate an API
* with the right parameters. It contains a default configuration and a map of specific configurations for API / set of
* API. Configurations are only exposed through the method getConfiguration, which will merge the default configuration
* and the requested one.
*/
declare class ApiManager {
private defaultConfiguration;
private apiConfigurations;
/**
* Map of registered Api Client associated to specific API
* Warning: This should not be used to get the ApiClient for an API, the function getConfiguration() should be used instead
*/
get registeredApiConfigurations(): {
readonly [key: string]: ApiClient;
};
/**
* Create an API manager using a custom ApiClient
* @param defaultConfiguration
*/
constructor(defaultConfiguration: ApiClient, apiConfigurations?: {
[key: string]: ApiClient;
});
/**
* Retrieve a configuration for a specific API
* @param api API to get the configuration for
* @note When passing a string the configuration is expecting to exist else an error is thrown
* @note when passing an Api instance that does not match a registered configuration, the default one will be returned
*/
getConfiguration(api?: string | ApiName): ApiClient;
/**
* Set or override API configuration
* @param apiClient API configuration to override to the given api
* @param api API name to override, the default configuration will be used if not specified
*/
setConfiguration(apiClient: ApiClient, api?: string | ApiName): void;
}
/**
* Add a preconnect `<link>` element to the DOM
* @param baseUrl the origin href
* @param supportCrossOrigin add crossorigin attribute to the link element
*/
declare function appendPreconnect(baseUrl: string, supportCrossOrigin?: boolean): void;
/**
* Provide a custom {@link ApiManager}
* A factory can be provided via injection to the token {@link API_TOKEN}
* @param apiManager
*/
declare function provideApiManager(apiManager: ApiManager): i0.EnvironmentProviders;
/**
* Token used by the core library to provide an Api manager to services. It can be provided in the app.
*/
declare const API_TOKEN: InjectionToken<ApiManager>;
export { API_TOKEN, ApiFactoryService, ApiManager, INITIAL_APIS_TOKEN, appendPreconnect, provideApiManager };
export type { ApiClassType };
//# sourceMappingURL=o3r-apis-manager.d.ts.map