/** * Type safe wrapper around the fetch API */ /// /// import * as t from "io-ts"; /** * Describes the possible methods of a request */ export type RequestMethod = "get" | "post" | "put" | "delete" | "patch"; /** * Describes the possible header keys of a request * * @deprecated This type used to enforce a contraint on a specific set of headers to be allowed. We decided to relax such constraint and to accept any string as the friction of being constrained on which header to use is not worth the little value it adds. */ export type RequestHeaderKey = string; /** * Describes a set of headers whose keys are of type RequestHeaderKey */ export type RequestHeaders = Record; /** * Describes the query params for this request */ export type RequestQuery = Record; /** * Generates a set of headers with certain keys (KH) from a parameters object * of type P. */ export type RequestHeaderProducer = (params: P) => RequestHeaders; /** * Composes two RequestHeaderProducer(s) */ export declare function composeHeaderProducers(p0: RequestHeaderProducer, p1: RequestHeaderProducer): RequestHeaderProducer; export type BasicResponseHeaderKey = "Cache-Control" | "Content-Encoding" | "Content-Type" | "Date" | "ETag" | "Expires" | "Transfer-Encoding"; export type ResponseHeaders = { [key in BasicResponseHeaderKey | H]?: string; }; /** * Describes a possible response type: when the status code is S, the response * type is T. */ export interface IResponseType { readonly status: S; readonly value: T; readonly headers: ResponseHeaders; } /** * A function that generates a typed representation of a response. * It should return undefined in case the response cannot be decoded (e.g. * in case of a parsing error). */ export type ResponseDecoder = (response: Response) => Promise | undefined>; /** * Composes two ResponseDecoder(s) */ export declare function composeResponseDecoders(d1: ResponseDecoder, d2: ResponseDecoder): ResponseDecoder; /** * Fully describes an API request. * * @param M The request method * @param P The type of input parameters * @param H The headers that must be defined for this request * @param R The possible response types for this request */ export interface IBaseApiRequestType { readonly method: M; readonly url: (params: P) => string; readonly query: (params: P) => RequestQuery; readonly headers: RequestHeaderProducer; readonly response_decoder: ResponseDecoder; } /** * Fully describes a GET request. */ export interface IGetApiRequestType extends IBaseApiRequestType<"get", P, KH, Q, R> { readonly method: "get"; } /** * Fully describes a POST request. * * POST requests require to provide the "Content-Type" header. */ export interface IPostApiRequestType extends IBaseApiRequestType<"post", P, KH | "Content-Type", Q, R> { readonly method: "post"; readonly body: (params: P) => string | FormData | ReadableStream | Buffer; } /** * Fully describes a PUT request. * * PUT requests require to provide the "Content-Type" header. */ export interface IPutApiRequestType extends IBaseApiRequestType<"put", P, KH | "Content-Type", Q, R> { readonly method: "put"; readonly body: (params: P) => string | FormData | ReadableStream | Buffer; } /** * Fully describes a PATCH request. * * PATCH requests require to provide the "Content-Type" header. */ export interface IPatchApiRequestType extends IBaseApiRequestType<"patch", P, KH | "Content-Type", Q, R> { readonly method: "patch"; readonly body: (params: P) => string | FormData | ReadableStream | Buffer; } /** * Fully describes a DELETE request. */ export interface IDeleteApiRequestType extends IBaseApiRequestType<"delete", P, KH, Q, R> { readonly method: "delete"; } /** * The union of the possible ApiRequest types */ export type ApiRequestType = IGetApiRequestType | IPostApiRequestType | IPutApiRequestType | IPatchApiRequestType | IDeleteApiRequestType; /** * The type of the Params of an ApiRequestType */ export type TypeofApiParams = T extends ApiRequestType ? P : never; /** * The type of the Response of an ApiRequestType */ export type TypeofApiResponse = T extends ApiRequestType ? R : never; /** * A union type of the Response statuses of an ApiRequestType */ export type TypeOfApiResponseStatus = TypeofApiResponse extends IResponseType ? S : never; /** * The type of the method that runs an ApiRequestType */ export type TypeofApiCall = (params: TypeofApiParams) => Promise>>; export type ApiRequestTypeForMethod = M extends "get" ? IGetApiRequestType : M extends "post" ? IPostApiRequestType : M extends "put" ? IPutApiRequestType : never; export type EmptyApiRequestBuilder = ApiRequestBuilder>; /** * A class for building ApiRequest(s) */ export declare class ApiRequestBuilder> { private readonly _request; constructor(_request: T); /** * Creates a new empty request with the GET method. */ static ofGet(): EmptyApiRequestBuilder<"get">; /** * Creates a new empty request with the POST method. */ static ofPost(): EmptyApiRequestBuilder<"post", {}, "Content-Type">; /** * Creates a new empty request with the PUT method. */ static ofPut(): EmptyApiRequestBuilder<"put", {}, "Content-Type">; get(): T; /** * Adds query parameters */ withQuery(query: (params: P1) => RequestQuery): ApiRequestBuilder>; /** * Adds headers */ withHeaders(headers: RequestHeaderProducer): ApiRequestBuilder>; /** * Adds response decoder */ withResponseDecoder(response_decoder: ResponseDecoder): ApiRequestBuilder>; } /** * Returns an async method that implements the provided ApiRequestType backed * by the "fetch" API. */ export declare function createFetchRequestForApi(requestType: ApiRequestType, options?: { readonly baseUrl?: string; readonly fetchApi?: typeof fetch; }): (params: P) => Promise>; /** * An header producer that sets the Content-Type to application/json */ export declare const ApiHeaderJson: RequestHeaderProducer<{}, "Content-Type">; /** * An io-ts based ResponseDecoder, it checks the response status and the * payload against the provided type. * * @param status The response status handled by this decoder * @param type The response type corresponding to the status * @param preprocessor A function that takes the object corresponding to the json response as input * and returns the processed object (usefull when you want alterate the json body received) */ export declare function ioResponseDecoder(status: S, type: t.Type, preprocessor?: (i: any) => any): ResponseDecoder>; /** * An arrayBuffer ResponseDecoder * it returns an IResponse with a Buffer value * * @param status The response status handled by this decoder */ export declare const bufferArrayResponseDecoder: (status: S) => ResponseDecoder>; /** * A basic ResponseDecoder that returns an Error with the status text if the * response status is S. */ export declare function basicErrorResponseDecoder(status: S): ResponseDecoder>; /** * A basic set of responses where the 200 status corresponds to a payload of * type R and 404 and 500 to an Error */ export type BasicResponseType = IResponseType<200, R, H> | IResponseType<404, string, H> | IResponseType<500, string, H>; /** * Returns a ResponseDecoder for BasicResponseType */ export declare function basicResponseDecoder(type: t.Type, preprocessor?: (i: any) => any): ResponseDecoder>; /** * A response decoder that ignores the payload and returns a constant value */ export declare function constantResponseDecoder(status: S, value: T): ResponseDecoder>; /** * Returns a RequestHeaderProducer that produces an Authorization header of type * "bearer token" taking the value from the "token" parameter of each request. */ export declare function ParamAuthorizationBearerHeaderProducer

(): RequestHeaderProducer; /** * Returns a RequestHeaderProducer that produces an Authorization header of type * "bearer token" with a fixed token value. */ export declare function AuthorizationBearerHeaderProducer

(token: string): RequestHeaderProducer; type MapTypeInApiResponse = T extends IResponseType ? IResponseType : T; /** * Changes the response with status S to have type B */ export type MapResponseType = T extends IGetApiRequestType ? IGetApiRequestType> : T extends IPostApiRequestType ? IPostApiRequestType> : T extends IPutApiRequestType ? IPutApiRequestType> : T extends IDeleteApiRequestType ? IDeleteApiRequestType> : T extends IPatchApiRequestType ? IPatchApiRequestType> : never; /** * The parameters of the request T */ export type RequestParams = T extends IGetApiRequestType ? P1 : T extends IPostApiRequestType ? P2 : T extends IPutApiRequestType ? P3 : T extends IDeleteApiRequestType ? P4 : T extends IPatchApiRequestType ? P5 : never; /** * The IResponseType of the request T */ export type RequestResponseTypes = T extends IBaseApiRequestType ? R : never; /** * Replaces the parameters of the request T with the type P */ export type ReplaceRequestParams = T extends IGetApiRequestType ? IGetApiRequestType : T extends IPostApiRequestType ? IPostApiRequestType : T extends IPutApiRequestType ? IPutApiRequestType : T extends IDeleteApiRequestType ? IDeleteApiRequestType : T extends IPatchApiRequestType ? IPatchApiRequestType : never; /** * Adds the status S with response type A to the responses of the request */ export type AddResponseType = T extends IGetApiRequestType ? IGetApiRequestType> : T extends IPostApiRequestType ? IPostApiRequestType> : T extends IPutApiRequestType ? IPutApiRequestType> : T extends IDeleteApiRequestType ? IDeleteApiRequestType> : T extends IPatchApiRequestType ? IPatchApiRequestType> : never; /** * Removes a status from the union of IResponseType(s) */ export type OmitStatusFromResponse = T extends IResponseType ? never : T; export {};