UNPKG

@ima/plugin-rest-client

Version:

Generic REST API client plugin for the IMA application framework.

230 lines 9.66 kB
/** * Abstract implementation of the generic REST API client. Implementations of * the {@linkcode RestClient} interface should extend this class. * * @abstract */ export default class AbstractRestClient extends RestClient { /** * Initializes the abstract REST API client. * * @param {HttpAgent} httpAgent The IMA HTTP agent to use to execute * requests. * @param {?Configurator} configurator The configurator to use for fetching * the server-provided configuration. * @param {LinkGenerator} linkGenerator The link generator used to generate * request URLs. * @param {RequestPreProcessor[]} preProcessors The request pre-processors. * @param {ResponsePostProcessor[]} postProcessors The response * post-processors. */ constructor(httpAgent: HttpAgent, configurator: Configurator | null, linkGenerator: LinkGenerator, preProcessors: RequestPreProcessor[], postProcessors: ResponsePostProcessor[]); /** * The IMA HTTP agent to use to execute requests. * * @private * @type {HttpAgent} */ private _httpAgent; /** * The configurator to use for fetching the server-provided * configuration. * * @private * @type {?Configurator} */ private _configurator; /** * The link generator used to generate request URLs. * * @private * @type {LinkGenerator} */ private _linkGenerator; /** * The request pre-processors. * * @private * @type {RequestPreProcessor[]} */ private _preProcessors; /** * The response post-processors. * * @private * @type {ResponsePostProcessor[]} */ private _postProcessors; /** * The server-provided configuration, or {@code null} if the * configuration has not been fetched yet. * * @private * @type {?Object<string, *>} */ private _serverConfiguration; /** * Flag signalling whether the server-provided configuration has * already been fetched. * * @private * @type {boolean} */ private _serverConfigurationFetched; /** * @inheritdoc * @override */ override list(resource: any, parameters?: {}, options?: {}, parentEntity?: null): Promise<AbstractEntity | Response | AbstractEntity[] | null>; /** * @inheritdoc * @override */ override get(resource: any, id: any, parameters?: {}, options?: {}, parentEntity?: null): Promise<AbstractEntity | Response | AbstractEntity[] | null>; /** * @inheritdoc * @override */ override patch(resource: any, id: any, data: any, parameters?: {}, options?: {}, parentEntity?: null): Promise<AbstractEntity | Response | AbstractEntity[] | null>; /** * @inheritdoc * @override */ override replace(resource: any, id: any, data: any, parameters?: {}, options?: {}, parentEntity?: null): Promise<AbstractEntity | Response | AbstractEntity[] | null>; /** * @inheritdoc * @override */ override create(resource: any, data: any, parameters?: {}, options?: {}, parentEntity?: null): Promise<AbstractEntity | Response | AbstractEntity[] | null>; /** * @inheritdoc * @override */ override delete(resource: any, id: any, parameters?: {}, options?: {}, parentEntity?: null): Promise<AbstractEntity | Response | AbstractEntity[] | null>; /** * Prepares, pre-processes and (if necessary) executes the request, then * post-processes the response and resolves the returned promise to the * post-processed response. * * The method first fetches the server-provided configuration if a * configurator is set and the server-provided configuration has not been * retrieved yet. * * @private * @param {string} method The HTTP method to use when making the request. * @param {*} parentEntity The parent entity within which the specified * resource will be manipulated. It may be needed to determine the * parent resource from the entity. Use {@code null} if the * specified resource is a top-level resource within the REST API. * @param {*} resource The REST resource to access. * @param {?(number|string|(number|string)[])} id The ID(s) identifying the * entity or group of entities to access. * @param {Object<string, (number|string|(number|string)[])>} parameters * Additional parameters to use when generating the URL. * @param {*} data The data to send in the request's body. * @param {{ * timeout: number=, * ttl: number=, * repeatRequest: number=, * headers: Object<string, string>=, * cache: boolean=, * withCredentials: boolean= * }=} options The HTTP request option as they would be passed to * the IMA HTTP agent. * @returns {Promise<?(Response|AbstractEntity|AbstractEntity[])>} The * post-processed server's response. */ private _prepareAndExecuteRequest; /** * Creates the URL to which to send the request based on the provided * information. * * @private * @param {*} parentEntity The parent entity within which the specified * resource will be manipulated. It may be needed to determine the * parent resource from the entity. Use {@code null} if the * specified resource is a top-level resource within the REST API. * @param {*} resource The resource to be accessed in the REST API. * @param {?(number|string|(number|string)[])} id The ID of the entity or * entities to access. * @param {Object<string, (number|string|(number|string)[])>} parameters * Additional parameters to use when generating the URL. * @returns {string} The generated URL. */ private _generateUrl; /** * Creates a new request object from the provided data. * * @private * @param {*} parentEntity The parent entity within which the specified * resource will be manipulated. It may be needed to determine the * parent resource from the entity. Use {@code null} if the * specified resource is a top-level resource within the REST API. * @param {*} resource The resource to be accessed in the REST API. * @param {Object<string, (number|string|(number|string)[])>} parameters * Additional parameters that were used to generate the URL. * @param {string} method The HTTP method to use to send the request. * @param {string} url The URL to which the request should be made. * @param {*} data The data to send in the request's body. * @param {{ * timeout: number=, * ttl: number=, * repeatRequest: number=, * headers: Object<string, string>=, * cache: boolean=, * withCredentials: boolean= * }=} rawOptions The HTTP request option as they would be passed to * the IMA HTTP agent. * @returns {Request} The created request. */ private _createRequest; /** * Pre-processes the provided request into either a request to send to the * server or a response object if the no request to the server is needed. * If a request is produced, the request is then sent to the server, and * the server's response is turned into a response object. Finally, the * response object will be post-processed and become the resolved value of * the returned promise. * * @private * @param {Request} request The request to pre-process, and then send to * the server. * @returns {Promise<?(Response|AbstractEntity|AbstractEntity[])>} A promise * that will resolve to the post-processed server's response, or a * post-processed response provided by one of the pre-processors, * or an entity or array of entities or {@code null} if the * resource class has the {@code inlineResponseBody} flag set. */ private _executeRequest; /** * Converts the provided response to a response with the body set an * entity, an array of entities, or {@code null}. The entities will be * instances of the REST API resource-identifying class (a class extending * the {@linkcode AbstractEntity} class). * * The body of the resulting response object will be {@code null} if the * provided response contains no useful data in its body. * * @private * @param {Response} response The REST API response that should have its * body replaced with entity(ies). * @returns {Response} Response object with its body set to an entity, an * array of entities, or {@code null} if the original body did not * contain any usable data. */ private _convertResponseBodyToEntities; /** * Sends the provided request using the IMA HTTP agent, and returns a * promise that resolves to the server's response. * * @private * @param {Request} request The request to send to the server. * @returns {Promise<Response>} A promise that will resolve to a response * object containing the server's response. */ private _executeRequestUsingHttpAgent; } import RestClient from './RestClient'; import AbstractEntity from './AbstractEntity'; import Response from './Response'; //# sourceMappingURL=AbstractRestClient.d.ts.map