@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
125 lines (118 loc) • 5.36 kB
TypeScript
import { Api, ApiClient, ApiName } from '@ama-sdk/core';
import * as i0 from '@angular/core';
import { InjectionToken, ModuleWithProviders } 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;
/**
* Module that needs to be imported by the application to instantiate an SDK configuration.
*/
declare class ApiManagerModule {
/**
* Provide a custom {@link ApiManager}
* A factory can be provided via injection to the token {@link API_TOKEN}
* @param apiManager
* @deprecated Please use {@link provideApiManager} instead, will be removed in v14.
*/
static forRoot(apiManager: ApiManager): ModuleWithProviders<ApiManagerModule>;
static ɵfac: i0.ɵɵFactoryDeclaration<ApiManagerModule, never>;
static ɵmod: i0.ɵɵNgModuleDeclaration<ApiManagerModule, never, never, never>;
static ɵinj: i0.ɵɵInjectorDeclaration<ApiManagerModule>;
}
/**
* 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, ApiManagerModule, INITIAL_APIS_TOKEN, appendPreconnect, provideApiManager };
export type { ApiClassType };
//# sourceMappingURL=index.d.ts.map