UNPKG

@ima/core

Version:

IMA.js framework for isomorphic javascript application

219 lines 9.44 kB
import * as Helpers from '@ima/helpers'; import { HttpAgent, HttpAgentRequestOptions, HttpAgentResponse } from './HttpAgent'; import { HttpProxy, HttpProxyErrorParams } from './HttpProxy'; import { Cache } from '../cache/Cache'; import { GenericError } from '../error/GenericError'; import { CookieStorage } from '../storage/CookieStorage'; import { UnknownParameters } from '../types'; export interface HttpAgentImplCacheOptions { prefix: string; } export interface HttpAgentImplConfig { cacheOptions: HttpAgentImplCacheOptions; defaultRequestOptions: HttpAgentRequestOptions; } /** * Implementation of the {@link HttpAgent} interface with internal caching * of completed and ongoing HTTP requests and cookie storage. */ export declare class HttpAgentImpl extends HttpAgent { protected _proxy: HttpProxy; protected _cache: Cache<HttpAgentResponse<unknown>>; protected _cookie: CookieStorage; protected _cacheOptions: HttpAgentImplCacheOptions; protected _defaultRequestOptions: HttpAgentRequestOptions; protected _Helper: typeof Helpers; protected _internalCacheOfPromises: Map<any, any>; /** * Initializes the HTTP handler. * * @param proxy The low-level HTTP proxy for sending the HTTP * requests. * @param cache Cache to use for caching ongoing and completed * requests. * @param cookie The cookie storage to use internally. * @param Helper The IMA.js helper methods. * @param config Configuration of the HTTP handler for * the current application environment, specifying the various * default request option values and cache option values. * @example * http * .get('url', { data: data }, { * ttl: 2000, * repeatRequest: 1, * withCredentials: true, * timeout: 2000, * accept: 'application/json', * language: 'en' * }) * .then((response) => { * //resolve * } * .catch((error) => { * //catch * }); * @example * http * .setDefaultHeader('Accept-Language', 'en') * .clearDefaultHeaders(); */ constructor(proxy: HttpProxy, cache: Cache<HttpAgentResponse<unknown>>, cookie: CookieStorage, config: HttpAgentImplConfig, Helper: typeof Helpers); /** * @inheritDoc */ get<B = unknown>(url: string, data?: UnknownParameters, options?: Partial<HttpAgentRequestOptions>): Promise<HttpAgentResponse<B>>; /** * @inheritDoc */ post<B = unknown>(url: string, data?: UnknownParameters, options?: Partial<HttpAgentRequestOptions>): Promise<HttpAgentResponse<B>>; /** * @inheritDoc */ put<B = unknown>(url: string, data?: UnknownParameters, options?: Partial<HttpAgentRequestOptions>): Promise<HttpAgentResponse<B>>; /** * @inheritDoc */ patch<B = unknown>(url: string, data?: UnknownParameters, options?: Partial<HttpAgentRequestOptions>): Promise<HttpAgentResponse<B>>; /** * @inheritDoc */ delete<B = unknown>(url: string, data?: UnknownParameters, options?: Partial<HttpAgentRequestOptions>): Promise<HttpAgentResponse<B>>; /** * @inheritDoc */ getCacheKey(method: string, url: string, data?: UnknownParameters): string; /** * @inheritDoc */ invalidateCache(method: string, url: string, data?: UnknownParameters): void; /** * @inheritDoc */ setDefaultHeader(header: string, value: string): this; /** * @inheritDoc */ clearDefaultHeaders(): this; /** * Attempts to clone the provided value, if possible. Values that cannot be * cloned (e.g. promises) will be simply returned. * * @param value The value to clone. * @return The created clone, or the provided value if the value cannot be * cloned. */ _clone<V>(value: V): V; /** * Check cache and if data isn’t available then make real request. * * @param method The HTTP method to use. * @param url The URL to which the request should be sent. * @param data The data to send with the request. * @param options Optional request options. * @return A promise that resolves to the response * with body parsed as JSON. */ _requestWithCheckCache<B>(method: string, url: string, data?: UnknownParameters, options?: Partial<HttpAgentRequestOptions>): Promise<HttpAgentResponse<B>>; /** * Tests whether an ongoing or completed HTTP request for the specified URL * and data is present in the internal cache and, if it is, the method * returns a promise that resolves to the response body parsed as JSON. * * The method returns `null` if no such request is present in the * cache. * * @param method The HTTP method used by the request. * @param url The URL to which the request was made. * @param data The data sent * to the server with the request. * @return {?Promise<HttpAgent~Response>} A promise that will resolve to the * server response with the body parsed as JSON, or `null` if * no such request is present in the cache. */ _getCachedData<B>(method: string, url: string, data?: UnknownParameters): Promise<HttpAgentResponse<B>> | null; /** * Sends a new HTTP request using the specified method to the specified * url. The request will carry the provided data as query parameters if the * HTTP method is GET, but the data will be sent as request body for any * other request method. * * @param method HTTP method to use. * @param url The URL to which the request is sent. * @param data The data sent * with the request. * @param options Optional request options. * @return {Promise<HttpAgent~Response>} A promise that resolves to the response * with the body parsed as JSON. */ _request<B>(method: string, url: string, data: UnknownParameters | undefined, options: HttpAgentRequestOptions): Promise<HttpAgentResponse<B>>; /** * Handles successful completion of an HTTP request by the HTTP proxy. * * The method also updates the internal cookie storage with the cookies * received from the server. * * @param {HttpAgent~Response} response Server response. * @return {HttpAgent~Response} The post-processed server response. */ _proxyResolved<B>(response: HttpAgentResponse<B>): HttpAgentResponse<B>; /** * Handles rejection of the HTTP request by the HTTP proxy. The method * tests whether there are any remaining tries for the request, and if * there are any, it attempts re-send the request. * * The method rejects the internal request promise if there are no tries * left. * * @param error The error provided by the HttpProxy, * carrying the error parameters, such as the request url, data, * method, options and other useful data. * @return {Promise<HttpAgent~Response>} A promise that will either resolve to a * server's response (with the body parsed as JSON) if there are * any tries left and the re-tried request succeeds, or rejects * with an error containing details of the cause of the request's * failure. */ _proxyRejected<B>(error: GenericError<HttpProxyErrorParams>): Promise<HttpAgentResponse<B>>; /** * Prepares the provided request options object by filling in missing * options with default values and adding extra options used internally. * * @param options Optional request options. * @return Request options with set filled-in * default values for missing fields, and extra options used * internally. */ _prepareOptions(options: Partial<HttpAgentRequestOptions> | undefined, url: string): HttpAgentRequestOptions; /** * Generates cache key suffix for an HTTP request to the specified URL with * the specified data. * * @param method The HTTP method used by the request. * @param url The URL to which the request is sent. * @param data The data sent * with the request. * @return The suffix of a cache key to use for a request to the * specified URL, carrying the specified data. */ _getCacheKeySuffix(method: string, url: string, data?: UnknownParameters): string; /** * Sets all cookies from the `Set-Cookie` response header to the * cookie storage. * * @param agentResponse The response of the server. */ _setCookiesFromResponse<B>(agentResponse: HttpAgentResponse<B>): void; /** * Saves the server response to the cache to be used as the result of the * next request of the same properties. * * @param agentResponse The response of the server. */ _saveAgentResponseToCache<B>(agentResponse: HttpAgentResponse<B>): void; /** * Cleans cache response from data (abort controller, postProcessors), that cannot be persisted, * before saving the data to the cache. */ _cleanResponse<B>(response: HttpAgentResponse<B>): HttpAgentResponse<B>; } //# sourceMappingURL=HttpAgentImpl.d.ts.map