export = APIClient; /** * @module shared/apiClient */ /** * This kind of dictionary is used for building stuff like query string parameters and * headers. * * @typedef {Object.} APIClientParametersDictionary * @parent module:shared/apiClient */ /** * @typedef {Object} APIClientFetchOptions * @property {string} [method] * The request method. * @property {APIClientParametersDictionary} [headers] * The request headers. * @property {string} [body] * The request body. * @property {boolean} [json] * Whether or not the response should _"JSON decoded"_. * @parent module:shared/apiClient */ /** * @callback APIClientFetchClient * @param {string} url The request URL. * @param {APIClientFetchOptions} [options] The request options. * @returns {Promise} * @see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API * @parent module:shared/apiClient */ /** * @typedef {APIClientFetchOptions & APIClientRequestOptionsProperties} APIClientRequestOptions * @parent module:shared/apiClient * @prettierignore */ /** * @typedef {Object} APIClientRequestOptionsProperties * @property {string} url * The request URL. * @parent module:shared/apiClient */ /** * @typedef {Object} APIClientEndpoint * @property {string} path The path to the endpoint relative to * the API entry point. It can include * placeholders with the format * `:placeholder-name` that are going to * be replaced when the endpoint gets * generated. * @property {?APIClientParametersDictionary} query A dictionary of query string * parameters that will be added when * the endpoint. If the value of a * parameter is `null`, it won't be * added. * @parent module:shared/apiClient */ /** * @typedef {string | APIClientEndpoint} APIClientEndpointValue * @parent module:shared/apiClient */ /** * @typedef {Object.} APIClientEndpoints * @example * * { // Endpoint path as a string. * endpointOne: 'endpoint-one', * // Endpoint as {APIClientEndpoint}. * endpointTwo: { * path: 'endpoint-two', * query: { * count: 20, * }, * }, * // Endpoint as a dictionary of endpoints ({APIClientEndpoints}). * endpointThree: { * subEndpointThreeOne: 'sub-endpoint-three-one', * subEndpointThreeTwo: { * path: 'sub-endpoint-three-two', * query: { * count: 20, * }, * }, * }, * } * * @parent module:shared/apiClient */ /** * An API client with configurable endpoints. * * @parent module:shared/apiClient * @tutorial APIClient */ declare class APIClient { /** * @param {string} url * The API entry point. * @param {APIClientEndpoints} endpoints * A dictionary of named endpoints relative to the API entry point. * @param {APIClientFetchClient} fetchClient * The fetch function that makes the requests. * @param {APIClientParametersDictionary} [defaultHeaders={}] * A dictionary of default headers to include on every request. */ constructor(url: string, endpoints: APIClientEndpoints, fetchClient: APIClientFetchClient, defaultHeaders?: APIClientParametersDictionary); /** * The API entry point. * * @type {string} * @access protected * @ignore */ _url: string; /** * A dictionary of named endpoints relative to the API entry point. * * @type {Object.} * @access protected * @ignore */ _endpoints: { [x: string]: string | APIClientEndpoint; }; /** * The fetch function that makes the requests. * * @type {APIClientFetchClient} * @access protected * @ignore */ _fetchClient: APIClientFetchClient; /** * A dictionary of default headers to include on every request. * * @type {APIClientParametersDictionary} * @access protected * @ignore */ _defaultHeaders: APIClientParametersDictionary; /** * An authorization token to include on the requests. * * @type {string} * @access protected * @ignore */ _authorizationToken: string; /** * Makes a `DELETE` request. * * @param {string} url The request URL. * @param {Object} body The request body. * @param {APIClientFetchOptions} [options={}] The request options. * @returns {Promise} */ delete(url: string, body?: any, options?: APIClientFetchOptions): Promise; /** * Generates an endpoint URL. * * @param {string} name * The name of the endpoint on the `endpoints` property. * @param {APIClientParametersDictionary} [parameters={}] * A dictionary of values that will replace placeholders on the endpoint definition. * @returns {string} * @throws {Error} * If the endpoint doesn't exist on the `endpoints` property. */ endpoint(name: string, parameters?: APIClientParametersDictionary): string; /** * Formats an error response into a proper Error object. This method should proabably be * overwritten to accomodate the error messages for the API it's being used for. * * @param {Object} response A received response from a request. * @param {?string} response.error An error message received on the response. * @param {number} status The HTTP status of the response. * @returns {Error} */ error(response: { error: string | null; }, status: number): Error; /** * Makes a request. * * @param {APIClientRequestOptions} options The request options. * @returns {Promise} */ fetch(options: APIClientRequestOptions): Promise; /** * Makes a `GET` request. * * @param {string} url The request URL. * @param {APIClientFetchOptions} [options={}] The request options. * @returns {Promise} */ get(url: string, options?: APIClientFetchOptions): Promise; /** * Makes a `HEAD` request. * * @param {string} url The request URL. * @param {APIClientFetchOptions} [options={}] The request options. * @returns {Promise} */ head(url: string, options?: APIClientFetchOptions): Promise; /** * Generates a dictionary of headers using the service `defaultHeaders` property as * base. * If a token was set using `setAuthorizationToken`, the method will add an * `Authorization` * header for the bearer token. * * @param {Object.} [overwrites={}] * Extra headers to add. * @returns {Object.} */ headers(overwrites?: { [x: string]: string | number; }): { [x: string]: string | number; }; /** * Makes a `PATCH` request. * * @param {string} url The request URL. * @param {Object} body The request body. * @param {APIClientFetchOptions} [options={}] The request options. * @returns {Promise} */ patch(url: string, body: any, options?: APIClientFetchOptions): Promise; /** * Makes a `POST` request. * * @param {string} url The request URL. * @param {Object} body The request body. * @param {APIClientFetchOptions} [options={}] The request options. * @returns {Promise} */ post(url: string, body: any, options?: APIClientFetchOptions): Promise; /** * Makes a `PUT` request. * * @param {string} url The request URL. * @param {Object} body The request body. * @param {APIClientFetchOptions} [options={}] The request options. * @returns {Promise} */ put(url: string, body: any, options?: APIClientFetchOptions): Promise; /** * Sets a bearer token for all the requests. * * @param {string} [token=''] The new authorization token. If the value is empty, it * will remove any token previously saved. */ setAuthorizationToken(token?: string): void; /** * Sets the default headers for the requests. * * @param {APIClientParametersDictionary} [headers={}] * The new default headers. * @param {boolean} [overwrite=true] * If `false`, it will merge the new default headers with the current ones. */ setDefaultHeaders(headers?: APIClientParametersDictionary, overwrite?: boolean): void; /** * An authorization token to include on the requests. * * @type {string} */ get authorizationToken(): string; /** * A dictionary of default headers to include on every request. * * @type {APIClientParametersDictionary} */ get defaultHeaders(): { [x: string]: string | number; }; /** * A dictionary of named endpoints relative to the API entry point. * * @type {Object.} */ get endpoints(): { [x: string]: string | APIClientEndpoint; }; /** * The fetch function that makes the requests. * * @type {APIClientFetchClient} */ get fetchClient(): APIClientFetchClient; /** * The API entry point. * * @type {string} */ get url(): string; } declare namespace APIClient { export { APIClientParametersDictionary, APIClientFetchOptions, APIClientFetchClient, APIClientRequestOptions, APIClientRequestOptionsProperties, APIClientEndpoint, APIClientEndpointValue, APIClientEndpoints }; } type APIClientEndpoint = { /** * The path to the endpoint relative to * the API entry point. It can include * placeholders with the format * `:placeholder-name` that are going to * be replaced when the endpoint gets * generated. */ path: string; /** * A dictionary of query string * parameters that will be added when * the endpoint. If the value of a * parameter is `null`, it won't be * added. */ query: APIClientParametersDictionary | null; }; type APIClientFetchClient = (url: string, options?: APIClientFetchOptions) => Promise; /** * This kind of dictionary is used for building stuff like query string parameters and * headers. */ type APIClientParametersDictionary = { [x: string]: string | number; }; type APIClientFetchOptions = { /** * The request method. */ method?: string; /** * The request headers. */ headers?: APIClientParametersDictionary; /** * The request body. */ body?: string; /** * Whether or not the response should _"JSON decoded"_. */ json?: boolean; }; type APIClientRequestOptions = APIClientFetchOptions & APIClientRequestOptionsProperties; type APIClientEndpoints = { [x: string]: APIClientEndpointValue; }; type APIClientRequestOptionsProperties = { /** * The request URL. */ url: string; }; type APIClientEndpointValue = string | APIClientEndpoint;