import { default as HttpClient } from "../httpClient/HttpClient.js"; import type { JsonReviver, RequestOptions, ResponseProcessor, TypedRequestOptions, TypedResponse } from "../types/TypedHttpClient.js"; /** * An HTTP client that returns typed responses. * * This client will assume the Content-Type headers on the requests and responses are indicative of how to * parse the provided content. If the Content-Type indicates the body is JSON, the client will attempt to * parse the response as such before passing it to caller-provided response parsers. If the bodies do not * contain JSON, a {@link ResponseBodyNotJSONError} will be thrown. * * It's recommended that [type assertion functions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) * are used in the provided response processor in order to ensure the responses are the anticipated shape. * The provided response parser is executed after some potential initial processing of the response. Each * response parser is provided with the original {@link Response} object, the response body as a string * (which may be empty), and the response body as an object (if the content-type indicates JSON) as * arguments. * * Generic typing is provided so TypeScript can be told what to expect from such responses and to give * downstream code access to helpful typing information. * * In general, this client is here to provide some convenience functionality and helpful typing information * in a way that doesn't clutter up the rest of the code base. It does not, on its own guarantee that what * it returns from its request methods will match the types specified, as this is the responsibility of the * response parser functions. Using robust parsers and assertions is highly recommended. * * For convenience, the client will assume that the primary mode of communication will be JSON where relevant, * unless otherwise specified. */ export default class TypedHttpClient { client: HttpClient; /** * Creates an instance of the HTTP client. * * @constructor * @param {string} userAgent - userAgent for requests */ constructor(userAgent?: string); /** * Performs a HEAD request to the provided URL. * * Note: HEAD requests cannot contain bodies, and so any body provided in the RequestInit will be ignored. * * @param requestOptions the details for the request to be made * @returns an object containing information about the response */ head(requestOptions: RequestOptions): Promise>; /** * Performs an OPTIONS request to the provided URL. * * @param requestOptions the details for the request to be made * @returns an object containing information about the response */ options(requestOptions: RequestOptions, responseProcessor: ResponseProcessor): Promise>; options(requestOptions: TypedRequestOptions, responseProcessor: ResponseProcessor): Promise>; /** * Performs a GET request to the provided URL. * * Note: GET requests cannot contain bodies, and so any body provided in the RequestInit will be ignored. * * @param requestOptions the details for the request to be made * @returns an object containing information about the response */ get(requestOptions: RequestOptions, responseProcessor: ResponseProcessor): Promise>; /** * Performs a POST request to the provided URL. * * @param requestOptions the details for the request to be made * @returns an object containing information about the response */ post(requestOptions: RequestOptions, responseProcessor: ResponseProcessor): Promise>; post(requestOptions: TypedRequestOptions, responseProcessor: ResponseProcessor): Promise>; /** * Performs a PUT request to the provided URL. * * @param requestOptions the details for the request to be made * @returns an object containing information about the response */ put(requestOptions: RequestOptions, responseProcessor: ResponseProcessor): Promise>; put(requestOptions: TypedRequestOptions, responseProcessor: ResponseProcessor): Promise>; /** * Performs a PATCH request to the provided URL. * * @param requestOptions the details for the request to be made * @returns an object containing information about the response */ patch(requestOptions: RequestOptions, responseProcessor: ResponseProcessor): Promise>; patch(requestOptions: TypedRequestOptions, responseProcessor: ResponseProcessor): Promise>; /** * Performs a DELETE request to the provided URL. * * @param requestOptions the details for the request to be made * @returns an object containing information about the response */ delete(requestOptions: RequestOptions, responseProcessor: ResponseProcessor): Promise>; delete(requestOptions: TypedRequestOptions, responseProcessor: ResponseProcessor): Promise>; /** * Forces a fallback to the `"application/json"` accept header if not provided, and the {@link JSONContentTypeHandler} if necessary. * * It's not required that a caller provides a additional headers or and accept header for their convenience. This * function is used to ensure that these are provided if the caller doesn't pass them. Additionally, if a payload * is provided, but no content type handler is, this will embed the JSON handler as a fallback. Without a fallback * content handler, it could potentially break things as the client may not be able to serialize the payload * provided (if there is one) or it may not know what to put for the content type header so the server can receive * it properly. * * @param requestOptions The original options object passed to the function * @returns the options with a content type handler explicitly defined */ private _getRequestOptionsWithAdditionalAndAccept; /** * Use the provided options to build the {@link Headers} that will be used for the request. * * This will defer to using the {@link JSONContentTypeHandler} if a payload is provided but a handler isn't. * * @param requestOptions request options to form headers from * @returns The {@link Headers} to use for the request */ private _headersFromOptions; /** * Format the data and request details into a digestible format for the base HttpClient. * * @param requestOptions The request options for the request * @returns The object the base HttpClient will use to set up and make the actual request */ private _getRequestResources; /** * Process the response and return the result. * * If the content-type of the response indicates the body has JSON, this will attempt to parse that JSON * and provide that along with the string version and the original response to the response processor. * * A "reviver" function can also be provided to fascilitate the JSON parsing attempt if relevant. * * @param response the {@link Response} object provided by cross-fetch * @param responseProcessor the function that will be used to * @param responseJsonReviver (optional) A function that transforms the results. This function is called for * each member of the object. If a member contains nested objects, the nested objects are transformed before * the parent object is. * @see {@link JSON.parse} for more information on the reviver. * @returns the processed response of the anticipated return type */ processResponse(response: Response, responseProcessor: ResponseProcessor, responseJsonReviver?: JsonReviver): Promise>; /** * Check if the content type of the request or response is "application/json". * * The Content-Type header may contain extra information beyond the media type, like `charset=utf-8`, so it must be * checked to see if the media type for JSON content ('application/json') is included, as oppposed to just checking if * it's equal to the media type. * * @param headers the request or response headers * @returns true, if the content type is "application/json", false, if not */ private _contentTypeIsJson; /** * Attempt to parse the body string as JSON. * * @throws {@link ResponseBodyNotJSONError} if body can't be parsed * * @param bodyContentsAsString the body contents of the response as a string * @param reviver (optional) A function that transforms the results. This function is called for each member * of the object. If a member contains nested objects, the nested objects are transformed before the parent * object is. * @see {@link JSON.parse} for more information on the reviver. * @returns the body parsed as JSON */ private _getParsedJSONFromResponseBodyContents; } //# sourceMappingURL=TypedHttpClient.d.ts.map