type JSON = { [key: string]: JSON; } | JSON[] | string | number | boolean | null | undefined; declare namespace JSON { type Loose = Record | Loose[] | string | number | boolean | null | undefined; } /** * Represents or validates a type that is compatible with JSON. * * **IMPORTANT**: the input of `JSONValue` and all of its internal types must be declared inline or as a type aliases * (`type`). They cannot be interfaces. * * @example * import { type JSONValue } from '@zimic/http'; * * // Can be used as a standalone type: * const value: JSONValue = { * name: 'example', * tags: ['one', 'two'], * }; * * @example * import { type JSONValue } from '@zimic/http'; * * // Can be used with a type argument to validate a JSON value: * type ValidJSON = JSONValue<{ * id: string; * email: string; * createdAt: string; * }>; * * // This results in a type error: * type InvalidJSON = JSONValue<{ * id: string; * email: string; * createdAt: Date; // `Date` is not a valid JSON value. * save: () => Promise; // Functions are not valid JSON values. * }>; */ type JSONValue = Type; declare namespace JSONValue { /** A loose version of the JSON value type. JSON objects are not strictly typed. */ type Loose = Type; } /** * Recursively converts a type to its JSON-serialized version. Dates are converted to strings and keys with non-JSON * values are excluded. * * @example * import { type JSONSerialized } from '@zimic/http'; * * type SerializedUser = JSONSerialized<{ * id: string; * email: string; * createdAt: Date; * save: () => Promise; * }>; * // { * // id: string; * // email: string; * // createdAt: string; * // } */ type JSONSerialized = Type extends JSONValue ? Type : Type extends Date ? string : Type extends (...parameters: never[]) => unknown ? never : Type extends symbol ? never : Type extends Map ? Record : Type extends Set ? Record : Type extends (infer ArrayItem)[] ? JSONSerialized[] : Type extends object ? { [Key in keyof Type as [JSONSerialized] extends [never] ? never : Key]: JSONSerialized; } : never; declare global { interface JSON { readonly value: unique symbol; stringify(value: Value, replacer?: ((this: any, key: string, value: Value) => any) | (number | string)[] | null, space?: string | number): JSONStringified; parse(text: JSONStringified, reviver?: (this: any, key: string, value: any) => any): JSONSerialized; } } type JSONStringified = string & { [JSON.value]: JSONSerialized; }; type Default = [undefined | void] extends [Type] ? IfEmpty : Exclude; type DefaultNoExclude = [undefined | void] extends Type ? IfEmpty : Type; type IfAny = 0 extends 1 & Type ? Yes : No; type IfNever = [Type] extends [never] ? Yes : No; /** Converts a union type to an intersection type. For example, `A | B` becomes `A & B`. */ type UnionToIntersection = (Union extends unknown ? (union: Union) => void : never) extends (intersectedUnion: infer IntersectedUnion) => void ? IntersectedUnion : never; /** * Determines if a union type has more than one type in it. If the union is a single type, it evaluates to `false`; * otherwise, it evaluates to `true`. */ type UnionHasMoreThanOneType = [UnionToIntersection] extends [never] ? true : false; type Prettify = { [Key in keyof Type]: Type[Key]; }; type ArrayItemIfArray = Type extends (infer Item)[] ? Item : Type; type PickArrayProperties = { [Key in keyof Type as never[] extends Type[Key] ? Key : never]: Type[Key]; }; type ArrayKey = keyof PickArrayProperties; type NonArrayKey = string | number extends ArrayKey ? keyof Type : Exclude>; type NonEmptyArray = [Type, ...Type[]]; type Replace = Type extends Source ? Target : Type; declare const brand: unique symbol; /** * A utility type to create a branded type. This is useful for creating types that are distinct from each other even if * they have the same underlying structure. It also helps the TypeScript compiler to reference the type in the generated * declaration files, rather than inlining it. */ type Branded = Type & { [brand]?: Brand; }; /** A schema for strict HTTP form data. */ interface HttpFormDataSchema { [fieldName: string]: string | string[] | Blob | Blob[] | null | undefined; } declare namespace HttpFormDataSchema { /** A schema for loose HTTP form data. Field values are not strictly typed. */ type Loose = Record; } declare namespace HttpFormDataSchemaName { /** Extracts the names of the form data fields defined in a {@link HttpFormDataSchema} that are arrays. */ type Array = IfNever & string>; /** Extracts the names of the form data fields defined in a {@link HttpFormDataSchema} that are not arrays. */ type NonArray = IfNever & string>; } /** * Extracts the names of the form data fields defined in a {@link HttpFormDataSchema}. Each key is considered a field * name. `HttpFormDataSchemaName.Array` can be used to extract the names of array form data fields, whereas * `HttpFormDataSchemaName.NonArray` extracts the names of non-array form data fields. * * @example * import { type HttpFormDataSchemaName } from '@zimic/http'; * * type FormDataName = HttpFormDataSchemaName<{ * title: string; * descriptions: string[]; * content: Blob; * }>; * // "title" | "descriptions" | "content" * * type ArrayFormDataName = HttpFormDataSchemaName.Array<{ * title: string; * descriptions: string[]; * content: Blob; * }>; * // "descriptions" * * type NonArrayFormDataName = HttpFormDataSchemaName.NonArray<{ * title: string; * descriptions: string[]; * content: Blob; * }>; * // "title" | "content" */ type HttpFormDataSchemaName = IfNever; type PrimitiveHttpFormDataSerialized = [Type] extends [never] ? never : Type extends number ? `${number}` : Type extends boolean ? `${boolean}` : Type extends null ? 'null' : Type extends symbol ? never : Type extends HttpFormDataSchema[string] ? Type : Type extends (infer ArrayItem)[] ? ArrayItem extends (infer _InternalArrayItem)[] ? never : PrimitiveHttpFormDataSerialized[] : string; /** * Recursively converts a schema to its {@link https://developer.mozilla.org/docs/Web/API/FormData FormData}-serialized * version. Numbers, booleans, and null are converted to `${number}`, `${boolean}`, and 'null' respectively, and other * values become strings. * * @example * import { type HttpFormDataSerialized } from '@zimic/http'; * * type Schema = HttpFormDataSerialized<{ * contentTitle: string; * contentSize: number | null; * content: Blob; * full?: boolean; * }>; * // { * // contentTitle: string; * // contentSize? `${number}` | 'null'; * // content: Blob; * // full?: "false" | "true"; * // } */ type HttpFormDataSerialized = [Type] extends [never] ? never : Type extends HttpFormDataSchema ? Type : Type extends object ? { [Key in keyof Type as IfNever, never, Key>]: PrimitiveHttpFormDataSerialized; } : never; /** @see {@link https://zimic.dev/docs/http/api/http-form-data `HttpFormData` API reference} */ declare class HttpFormData extends FormData { readonly _schema: HttpFormDataSerialized; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdataset `formData.set()` API reference} */ set>(name: Name, value: Exclude>, Blob>): void; set>(name: Name, blob: Exclude>, string>, fileName?: string): void; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdataappend `formData.append()` API reference} */ append>(name: Name, value: Exclude>, Blob>): void; append>(name: Name, blob: Exclude>, string>, fileName?: string): void; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdataget `formData.get()` API reference} */ get>(name: Name): Replace, undefined, null>, Blob, File>; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdatagetall `formData.getAll()` API reference} */ getAll>(name: Name): Replace>, Blob, File>[]; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdatahas `formData.has()` API reference} */ has>(name: Name): boolean; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdatadelete `formData.delete()` API reference} */ delete>(name: Name): void; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdataforeach `formData.forEach()` API reference} */ forEach>(callback: >(value: Replace>, Blob, File>, key: Key, formData: HttpFormData) => void, thisArg?: This): void; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdatakeys `formData.keys()` API reference} */ keys(): FormDataIterator>; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdatavalues `formData.values()` API reference} */ values(): FormDataIterator]>>, Blob, File>>; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdataentries `formData.entries()` API reference} */ entries(): FormDataIterator<[ HttpFormDataSchemaName, Replace]>>, Blob, File> ]>; [Symbol.iterator](): FormDataIterator<[ HttpFormDataSchemaName, Replace]>>, Blob, File> ]>; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdataequals `formData.equals()` API reference} */ equals(otherData: HttpFormData): Promise; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdatacontains `formData.contains()` API reference} */ contains(otherData: HttpFormData): Promise; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdataassign `formData.assign()` API reference} */ assign(...otherDataArray: HttpFormData[]): void; /** @see {@link https://zimic.dev/docs/http/api/http-form-data#formdatatoobject `formData.toObject()` API reference} */ toObject(): this["_schema"]; } interface HttpPathParamsSchema { [paramName: string]: string | undefined; } declare namespace HttpPathParamsSchema { /** A schema for loose HTTP path parameters. Parameter values are not strictly typed. */ type Loose = Record; } type PrimitiveHttpPathParamsSerialized = [Type] extends [never] ? never : Type extends number ? `${number}` : Type extends boolean ? `${boolean}` : Type extends null ? 'null' : Type extends symbol ? never : Type extends HttpPathParamsSchema[string] ? Type : string; /** * Recursively converts a schema to its path parameters-serialized version. Numbers, booleans, and null are converted to * `${number}`, `${boolean}`, and 'null' respectively, and other values become strings. * * @example * import { type HttpPathParamsSerialized } from '@zimic/http'; * * type Params = HttpPathParamsSerialized<{ * userId: string; * notificationId: number | null; * full?: boolean; * }>; * // { * // userId: string; * // notificationId: `${number}` | 'null'; * // full?: "false" | "true"; * // } */ type HttpPathParamsSerialized = [Type] extends [never] ? never : Type extends HttpPathParamsSchema ? Type : Type extends object ? { [Key in keyof Type as IfNever, never, Key>]: PrimitiveHttpPathParamsSerialized; } : never; /** A schema for strict HTTP headers. */ interface HttpHeadersSchema { [headerName: string]: string | undefined; } declare namespace HttpHeadersSchema { /** A schema for loose HTTP headers. Header values are not strictly typed. */ type Loose = Record; } /** A strict tuple representation of a {@link HttpHeadersSchema}. */ type HttpHeadersSchemaTuple = { [Key in keyof Schema & string]: [Key, NonNullable]; }[keyof Schema & string]; /** An initialization value for {@link https://zimic.dev/docs/http/api/http-headers `HttpHeaders`}. */ type HttpHeadersInit = Headers | Schema | HttpHeaders | HttpHeadersSchemaTuple[]; /** * Extracts the names of the headers defined in a {@link HttpHeadersSchema}. Each key is considered a header name. * * @example * import { type HttpHeadersSchemaName } from '@zimic/http'; * * type HeaderName = HttpHeadersSchemaName<{ * 'content-type': string; * 'content-length'?: string; * }>; * // "content-type" | "content-length" */ type HttpHeadersSchemaName = IfNever; /** * Recursively converts a schema to its * {@link https://developer.mozilla.org/docs/Web/API/Headers HTTP headers}-serialized version. Numbers and booleans are * converted to `${number}` and `${boolean}` respectively, null becomes undefined and not serializable values are * excluded, such as functions and dates. * * @example * import { type HttpHeadersSerialized } from '@zimic/http'; * * type Params = HttpHeadersSerialized<{ * 'content-type': string; * 'x-remaining-tries': number; * 'x-full'?: boolean; * 'x-date': Date; * method: () => void; * }>; * // { * // 'content-type': string; * // 'x-remaining-tries': `${number}`; * // 'x-full'?: "false" | "true"; * // } */ type HttpHeadersSerialized = HttpPathParamsSerialized; /** @see {@link https://zimic.dev/docs/http/api/http-headers `HttpHeaders` API reference} */ declare class HttpHeaders extends Headers { readonly _schema: HttpHeadersSerialized; constructor(init?: HttpHeadersInit); /** @see {@link https://zimic.dev/docs/http/api/http-headers#headersset `headers.set()` API reference} */ set>(name: Name, value: NonNullable): void; /** @see {@link https://zimic.dev/docs/http/api/http-headers#headersappend `headers.append()` API reference} */ append>(name: Name, value: NonNullable): void; /** @see {@link https://zimic.dev/docs/http/api/http-headers#headersget `headers.get()` API reference} */ get>(name: Name): Replace; /** @see {@link https://zimic.dev/docs/http/api/http-headers#headersgetsetcookie `headers.getSetCookie()` API reference} */ getSetCookie(): NonNullable>[]; /** @see {@link https://zimic.dev/docs/http/api/http-headers#headershas `headers.has()` API reference} */ has>(name: Name): boolean; /** @see {@link https://zimic.dev/docs/http/api/http-headers#headersdelete `headers.delete()` API reference} */ delete>(name: Name): void; /** @see {@link https://zimic.dev/docs/http/api/http-headers#headersforeach `headers.forEach()` API reference} */ forEach>(callback: >(value: NonNullable & string, key: Key, headers: Headers) => void, thisArg?: This): void; /** @see {@link https://zimic.dev/docs/http/api/http-headers#headerskeys `headers.keys()` API reference} */ keys(): HeadersIterator>; /** @see {@link https://zimic.dev/docs/http/api/http-headers#headersvalues `headers.values()` API reference} */ values(): HeadersIterator]> & string>; /** @see {@link https://zimic.dev/docs/http/api/http-headers#headersentries `headers.entries()` API reference} */ entries(): HeadersIterator<[ HttpHeadersSchemaName, NonNullable]> & string ]>; [Symbol.iterator](): HeadersIterator<[ HttpHeadersSchemaName, NonNullable]> & string ]>; /** @see {@link https://zimic.dev/docs/http/api/http-headers#headersequals `headers.equals()` API reference} */ equals(otherHeaders: HttpHeaders): boolean; /** @see {@link https://zimic.dev/docs/http/api/http-headers#headerscontains `headers.contains()` API reference} */ contains(otherHeaders: HttpHeaders): boolean; /** @see {@link https://zimic.dev/docs/http/api/http-headers#headersassign `headers.assign()` API reference} */ assign(...otherHeadersArray: HttpHeaders[]): void; /** @see {@link https://zimic.dev/docs/http/api/http-headers#headerstoobject `headers.toObject()` API reference} */ toObject(): this['_schema']; private splitHeaderValues; } /** A schema for strict HTTP URL search parameters. */ interface HttpSearchParamsSchema { [paramName: string]: string | string[] | undefined; } declare namespace HttpSearchParamsSchema { /** A schema for loose HTTP URL search parameters. Parameter values are not strictly typed. */ type Loose = Record; } /** A strict tuple representation of a {@link HttpSearchParamsSchema}. */ type HttpSearchParamsSchemaTuple = { [Key in keyof Schema & string]: [Key, ArrayItemIfArray>]; }[keyof Schema & string]; /** An initialization value for {@link https://zimic.dev/docs/http/api/http-search-params `HttpSearchParams`}. */ type HttpSearchParamsInit = string | URLSearchParams | Schema | HttpSearchParams | HttpSearchParamsSchemaTuple[]; declare namespace HttpSearchParamsSchemaName { /** Extracts the names of the search params defined in a {@link HttpSearchParamsSchema} that are arrays. */ type Array = IfNever & string>; /** Extracts the names of the search params defined in a {@link HttpSearchParamsSchema} that are not arrays. */ type NonArray = IfNever & string>; } /** * Extracts the names of the search params defined in a {@link HttpSearchParamsSchema}. Each key is considered a search * param name. `HttpSearchParamsSchemaName.Array` can be used to extract the names of array search params, whereas * `HttpSearchParamsSchemaName.NonArray` extracts the names of non-array search params. * * @example * import { type HttpSearchParamsSchemaName } from '@zimic/http'; * * type SearchParamsName = HttpSearchParamsSchemaName<{ * query?: string[]; * page?: `${number}`; * perPage?: `${number}`; * }>; * // "query" | "page" | "perPage" * * type ArraySearchParamsName = HttpSearchParamsSchemaName.Array<{ * query?: string[]; * page?: `${number}`; * perPage?: `${number}`; * }>; * // "query" * * type NonArraySearchParamsName = HttpSearchParamsSchemaName.NonArray<{ * query?: string[]; * page?: `${number}`; * perPage?: `${number}`; * }>; * // "page" | "perPage" */ type HttpSearchParamsSchemaName = IfNever; type PrimitiveHttpSearchParamsSerialized = [Type] extends [never] ? never : Type extends number ? `${number}` : Type extends boolean ? `${boolean}` : Type extends null ? 'null' : Type extends symbol ? never : Type extends HttpSearchParamsSchema[string] ? Type : Type extends (infer ArrayItem)[] ? ArrayItem extends (infer _InternalArrayItem)[] ? string : PrimitiveHttpSearchParamsSerialized[] : string; /** * Recursively converts a schema to its * {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams URLSearchParams}-serialized version. Numbers, * booleans, and null are converted to `${number}`, `${boolean}`, and 'null' respectively, and other values become * strings. * * @example * import { type HttpSearchParamsSerialized } from '@zimic/http'; * * type Params = HttpSearchParamsSerialized<{ * query?: string; * order: 'asc' | 'desc' | null; * page?: number; * full?: boolean; * }>; * // { * // query?: string; * // order: 'asc' | 'desc' | 'null'; * // page?: `${number}`; * // full?: "false" | "true"; * // } */ type HttpSearchParamsSerialized = [Type] extends [never] ? never : Type extends HttpSearchParamsSchema ? Type : Type extends object ? { [Key in keyof Type as IfNever, never, Key>]: PrimitiveHttpSearchParamsSerialized; } : never; /** @see {@link https://zimic.dev/docs/http/api/http-search-params `HttpSearchParams` API reference} */ declare class HttpSearchParams extends URLSearchParams { readonly _schema: HttpSearchParamsSerialized; constructor(init?: HttpSearchParamsInit); private populateInitArrayProperties; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamsset `searchParams.set()` API reference} */ set>(name: Name, value: ArrayItemIfArray>): void; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamsappend `searchParams.append()` API reference} */ append>(name: Name, value: ArrayItemIfArray>): void; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamsget `searchParams.get()` API reference} */ get>(name: Name): Replace, undefined, null>; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamsgetall `searchParams.getAll()` API reference} */ getAll>(name: Name): ArrayItemIfArray>[]; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamshas `searchParams.has()` API reference} */ has>(name: Name, value?: ArrayItemIfArray>): boolean; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamsdelete `searchParams.delete()` API reference} */ delete>(name: Name, value?: ArrayItemIfArray>): void; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamsforeach `searchParams.forEach()` API reference} */ forEach>(callback: >(value: ArrayItemIfArray>, key: Key, searchParams: HttpSearchParams) => void, thisArg?: This): void; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamskeys `searchParams.keys()` API reference} */ keys(): URLSearchParamsIterator>; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamsvalues `searchParams.values()` API reference} */ values(): URLSearchParamsIterator]>>>; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamsentries `searchParams.entries()` API reference} */ entries(): URLSearchParamsIterator<[ HttpSearchParamsSchemaName, ArrayItemIfArray]>> ]>; [Symbol.iterator](): URLSearchParamsIterator<[ HttpSearchParamsSchemaName, ArrayItemIfArray]>> ]>; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamsequals `searchParams.equals()` API reference} */ equals(otherParams: HttpSearchParams): boolean; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamscontains `searchParams.contains()` API reference} */ contains(otherParams: HttpSearchParams): boolean; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamsassign `searchParams.assign()` API reference} */ assign(...otherParamsArray: HttpSearchParams[]): void; /** @see {@link https://zimic.dev/docs/http/api/http-search-params#searchparamstoobject `searchParams.toObject()` API reference} */ toObject(): this["_schema"]; } declare const HTTP_METHODS: readonly ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"]; /** * A type representing the currently supported * {@link https://developer.mozilla.org/docs/Web/HTTP/Methods `HTTP methods`}. */ type HttpMethod = (typeof HTTP_METHODS)[number]; /** * A schema representing the structure of an HTTP request. * * @see {@link https://zimic.dev/docs/http/api/http-schema `HttpSchema` API reference} */ interface HttpRequestSchema { headers?: HttpHeadersSchema.Loose; searchParams?: HttpSearchParamsSchema.Loose; body?: HttpBody.Loose; } /** * A schema representing the structure of an HTTP response. * * @see {@link https://zimic.dev/docs/http/api/http-schema `HttpSchema` API reference} */ interface HttpResponseSchema { headers?: HttpHeadersSchema.Loose; body?: HttpBody.Loose; } /** * The status codes used in HTTP responses, as defined by * {@link https://httpwg.org/specs/rfc9110.html#overview.of.status.codes RFC-9110}. * * - `HttpStatusCode.Information`: {@link https://developer.mozilla.org/docs/Web/HTTP/Status#information_responses `1XX`} * - `HttpStatusCode.Success`: {@link https://developer.mozilla.org/docs/Web/HTTP/Status#successful_responses `2XX`} * - `HttpStatusCode.Redirection`: {@link https://developer.mozilla.org/docs/Web/HTTP/Status#redirection_messages `3XX`} * - `HttpStatusCode.ClientError`: {@link https://developer.mozilla.org/docs/Web/HTTP/Status#client_error_responses `4XX`} * - `HttpStatusCode.ServerError`: {@link https://developer.mozilla.org/docs/Web/HTTP/Status#server_error_responses `5XX`} */ type HttpStatusCode = HttpStatusCode.Information | HttpStatusCode.Success | HttpStatusCode.Redirection | HttpStatusCode.ClientError | HttpStatusCode.ServerError; declare namespace HttpStatusCode { /** * An HTTP status code in the `1XX` range, representing an informational response. * * @see {@link https://developer.mozilla.org/docs/Web/HTTP/Status#information_responses `1XX`} */ type Information = 100 | 101 | 102 | 103; /** * An HTTP status code in the `2XX` range, representing a successful response. * * @see {@link https://developer.mozilla.org/docs/Web/HTTP/Status#successful_responses `2XX`} */ type Success = 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226; /** * An HTTP status code in the `3XX` range, representing a redirection response. * * @see {@link https://developer.mozilla.org/docs/Web/HTTP/Status#redirection_messages `3XX`} */ type Redirection = 300 | 301 | 302 | 303 | 304 | 307 | 308; /** * An HTTP status code in the `4XX` range, representing a client error response. * * @see {@link https://developer.mozilla.org/docs/Web/HTTP/Status#client_error_responses `4XX`} */ type ClientError = 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451; /** * An HTTP status code in the `5XX` range, representing a server error response. * * @see {@link https://developer.mozilla.org/docs/Web/HTTP/Status#server_error_responses `5XX`} */ type ServerError = 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511; } /** * A schema representing the structure of HTTP responses by status code. * * @see {@link https://zimic.dev/docs/http/api/http-schema `HttpSchema` API reference} */ type HttpResponseSchemaByStatusCode = { [StatusCode in HttpStatusCode]?: HttpResponseSchema; }; /** * Extracts the status codes used in a response schema by status code. * * @see {@link https://zimic.dev/docs/http/api/http-schema `HttpSchema` API reference} */ type HttpResponseSchemaStatusCode = keyof ResponseSchemaByStatusCode & HttpStatusCode; /** * A schema representing the structure of an HTTP request and response for a given method. * * @see {@link https://zimic.dev/docs/http/api/http-schema `HttpSchema` API reference} */ interface HttpMethodSchema { request?: HttpRequestSchema; response?: HttpResponseSchemaByStatusCode; } /** * A schema representing the structure of HTTP request and response by method. * * @see {@link https://zimic.dev/docs/http/api/http-schema `HttpSchema` API reference} */ interface HttpMethodsSchema { GET?: HttpMethodSchema; POST?: HttpMethodSchema; PUT?: HttpMethodSchema; PATCH?: HttpMethodSchema; DELETE?: HttpMethodSchema; HEAD?: HttpMethodSchema; OPTIONS?: HttpMethodSchema; } interface BaseHttpSchema { [path: string]: HttpMethodsSchema; } /** @see {@link https://zimic.dev/docs/http/api/http-schema `HttpSchema` API reference} */ type HttpSchema = Branded; declare namespace HttpSchema { /** @see {@link https://zimic.dev/docs/http/api/http-schema#httpschemamethods `HttpSchema.Methods` API reference} */ type Methods = Schema; /** @see {@link https://zimic.dev/docs/http/api/http-schema#httpschemamethod `HttpSchema.Method` API reference} */ type Method = Schema; /** @see {@link https://zimic.dev/docs/http/api/http-schema#httpschemarequest `HttpSchema.Request` API reference} */ type Request = Schema; /** @see {@link https://zimic.dev/docs/http/api/http-schema#httpschemaresponsebystatuscode `HttpSchema.ResponseByStatusCode` API reference} */ type ResponseByStatusCode = Schema; /** @see {@link https://zimic.dev/docs/http/api/http-schema#httpschemaresponse `HttpSchema.Response` API reference} */ type Response = Schema; /** @see {@link https://zimic.dev/docs/http/api/http-schema#httpschemabody `HttpSchema.Body` API reference} */ type Body = Schema; /** @see {@link https://zimic.dev/docs/http/api/http-schema#httpschemaheaders `HttpSchema.Headers` API reference} */ type Headers = Schema; /** @see {@link https://zimic.dev/docs/http/api/http-schema#httpschemasearchparams `HttpSchema.SearchParams` API reference} */ type SearchParams = Schema; /** @see {@link https://zimic.dev/docs/http/api/http-schema#httpschemapathparams `HttpSchema.PathParams` API reference} */ type PathParams = Schema; /** @see {@link https://zimic.dev/docs/http/api/http-schema#httpschemaformdata `HttpSchema.FormData` API reference} */ type FormData = Schema; } /** * Extracts the methods from an HTTP service schema. * * @see {@link https://zimic.dev/docs/http/api/http-schema `HttpSchema` API reference} */ type HttpSchemaMethod = IfAny & HttpMethod>; type RepeatingPathParamModifier = '+'; type OptionalPathParamModifier = '?' | '*'; type ConvertPathParamToRecord = PathParam extends `${infer PathParamWithoutSlash}/` ? ConvertPathParamToRecord : PathParam extends `${infer PathParamWithoutSlash}\\:` ? ConvertPathParamToRecord : PathParam extends `${infer PathParamWithoutModifier}${OptionalPathParamModifier}` ? { [Name in PathParamWithoutModifier]?: string; } : PathParam extends `${infer PathParamWithoutModifier}${RepeatingPathParamModifier}` ? { [Name in PathParamWithoutModifier]: string; } : { [Name in PathParam]: string; }; type RecursiveInferPathParams = Path extends `${infer Prefix}:${infer PathParamWithRemainingPath}` ? PathParamWithRemainingPath extends `${infer PathParam}/${infer RemainingPath}` ? Prefix extends `${string}\\` ? RecursiveInferPathParams : ConvertPathParamToRecord & RecursiveInferPathParams : PathParamWithRemainingPath extends `${infer PathParam}\\:${infer RemainingPath}` ? Prefix extends `${string}\\` ? RecursiveInferPathParams<`\\:${RemainingPath}`> : ConvertPathParamToRecord & RecursiveInferPathParams<`\\:${RemainingPath}`> : PathParamWithRemainingPath extends `${infer PathParam}:${infer RemainingPath}` ? Prefix extends `${string}\\` ? RecursiveInferPathParams : ConvertPathParamToRecord & RecursiveInferPathParams<`:${RemainingPath}`> : Prefix extends `${string}\\` ? {} : ConvertPathParamToRecord : {}; /** @see {@link https://zimic.dev/docs/http/api/http-schema#inferpathparams `InferPathParams` API reference} */ type InferPathParams : never) = never> = Prettify>; type WithoutEscapedColons = Path extends `${infer Prefix}\\:${infer Suffix}` ? WithoutEscapedColons<`${Prefix}:${Suffix}`> : Path; type ConvertPathParamToString = PathParam extends `${infer PathParamWithoutSlash}/` ? `${ConvertPathParamToString}/` : PathParam extends `${infer PathParamWithoutSlash}\\:` ? `${ConvertPathParamToString}:` : string; type AllowAnyStringInPathParams = Path extends `${infer Prefix}:${infer PathParamWithRemainingPath}` ? PathParamWithRemainingPath extends `${infer PathParam}/${infer RemainingPath}` ? Prefix extends `${infer PrefixPrefix}\\` ? `${PrefixPrefix}:${AllowAnyStringInPathParams}` : `${Prefix}${ConvertPathParamToString}/${AllowAnyStringInPathParams}` : PathParamWithRemainingPath extends `${infer PathParam}\\:${infer RemainingPath}` ? Prefix extends `${infer PrefixPrefix}\\` ? `${PrefixPrefix}:${AllowAnyStringInPathParams}` : `${Prefix}${ConvertPathParamToString}:${AllowAnyStringInPathParams}` : PathParamWithRemainingPath extends `${infer PathParam}:${infer RemainingPath}` ? Prefix extends `${infer PrefixPrefix}\\` ? `${PrefixPrefix}:${AllowAnyStringInPathParams}` : `${Prefix}${ConvertPathParamToString}${AllowAnyStringInPathParams<`:${RemainingPath}`>}` : Prefix extends `${infer PrefixPrefix}\\` ? `${PrefixPrefix}:${PathParamWithRemainingPath}` : `${Prefix}${ConvertPathParamToString}` : Path; /** @see {@link https://zimic.dev/docs/http/api/http-schema#httpschemapath `HttpSchemaPath` API reference} */ declare namespace HttpSchemaPath { type LooseLiteral = { [Path in keyof Schema & string]: Method extends keyof Schema[Path] ? Path : never; }[keyof Schema & string]; /** @see {@link https://zimic.dev/docs/http/api/http-schema#httpschemapathliteral `HttpSchemaPath.Literal` API reference} */ export type Literal = HttpSchemaMethod> = LooseLiteral; /** @see {@link https://zimic.dev/docs/http/api/http-schema#httpschemapathnonliteral `HttpSchemaPath.NonLiteral` API reference} */ export type NonLiteral = HttpSchemaMethod> = WithoutEscapedColons>>; export { }; } type HttpSchemaPath = HttpSchemaMethod> = HttpSchemaPath.Literal | HttpSchemaPath.NonLiteral; type LargestPathPrefix = Path extends `${infer Prefix}/${infer Suffix}` ? `${Prefix}/${Suffix extends `${string}/${string}` ? LargestPathPrefix : ''}` : Path; type ExcludeNonLiteralPathsSupersededByLiteralPath = Path extends `${LargestPathPrefix}:${string}` ? never : Path; type PreferMostStaticLiteralPath = UnionHasMoreThanOneType extends true ? ExcludeNonLiteralPathsSupersededByLiteralPath : Path; type RecursiveInferHttpSchemaPath, NonLiteralPath extends string, LiteralPath extends HttpSchemaPath.Literal> = NonLiteralPath extends AllowAnyStringInPathParams ? NonLiteralPath extends `${AllowAnyStringInPathParams}/${string}` ? never : LiteralPath : never; type LiteralHttpSchemaPathFromNonLiteral, NonLiteralPath extends string, LiteralPath extends HttpSchemaPath.Literal = HttpSchemaPath.Literal> = PreferMostStaticLiteralPath : never>; type OmitPastHttpStatusCodes = PastSchemas extends NonEmptyArray ? Omit> : Schema; /** @see {@link https://zimic.dev/docs/http/api/http-schema#mergehttpresponsesbystatuscode `MergeHttpResponsesByStatusCode` API reference} */ type MergeHttpResponsesByStatusCode = Schemas extends [ infer FirstSchema extends HttpResponseSchemaByStatusCode, ...infer RestSchemas extends HttpResponseSchemaByStatusCode[] ] ? RestSchemas extends NonEmptyArray ? OmitPastHttpStatusCodes & MergeHttpResponsesByStatusCode : OmitPastHttpStatusCodes : never; /** The body type for HTTP requests and responses. */ type HttpBody = JSONValue | HttpFormData | HttpSearchParams | Blob | ArrayBuffer | ReadableStream; declare namespace HttpBody { /** A loose version of the HTTP body type. JSON values are not strictly typed. */ type Loose = Replace; } /** * An HTTP headers object with a strictly-typed schema. Fully compatible with the built-in * {@link https://developer.mozilla.org/docs/Web/API/Headers `Headers`} class. */ type StrictHeaders = Pick, keyof Headers>; /** * An HTTP search params object with a strictly-typed schema. Fully compatible with the built-in * {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams `URLSearchParams`} class. */ type StrictURLSearchParams = Pick, keyof URLSearchParams>; /** * An HTTP form data object with a strictly-typed schema. Fully compatible with the built-in * {@link https://developer.mozilla.org/docs/Web/API/FormData `FormData`} class. */ type StrictFormData = Pick, keyof FormData>; /** * An HTTP request with a strictly-typed JSON body. Fully compatible with the built-in * {@link https://developer.mozilla.org/docs/Web/API/Request `Request`} class. */ interface HttpRequest extends Request { headers: StrictHeaders; text: () => Promise; json: () => Promise ? never : Replace>; formData: () => Promise ? StrictFormData : StrictBody extends HttpSearchParams ? StrictFormData : StrictBody extends null | undefined ? never : FormData>; clone: () => HttpRequest; } /** * An HTTP response with a strictly-typed JSON body and status code. Fully compatible with the built-in * {@link https://developer.mozilla.org/docs/Web/API/Response `Response`} class. */ interface HttpResponse extends Response { ok: StatusCode extends HttpStatusCode.Information | HttpStatusCode.Success | HttpStatusCode.Redirection ? true : false; status: StatusCode; headers: StrictHeaders; text: () => Promise; json: () => Promise ? never : Replace>; formData: () => Promise ? StrictFormData : StrictBody extends HttpSearchParams ? StrictFormData : StrictBody extends null | undefined ? never : FormData>; clone: () => HttpResponse; } type HttpRequestHeadersSchemaFromBody = 'body' extends keyof RequestSchema ? [RequestSchema['body']] extends [never] ? DefaultHeadersSchema : [Extract] extends [never] ? 'headers' extends keyof RequestSchema ? [RequestSchema['headers']] extends [never] ? DefaultHeadersSchema : 'content-type' extends keyof Default ? DefaultHeadersSchema : { 'content-type': 'application/json'; } : { 'content-type': 'application/json'; } : DefaultHeadersSchema : DefaultHeadersSchema; type HttpRequestHeadersSchema = 'headers' extends keyof MethodSchema['request'] ? [MethodSchema['request']['headers']] extends [never] ? HttpRequestHeadersSchemaFromBody, never> : (MethodSchema['request']['headers'] & HttpRequestHeadersSchemaFromBody, {}>) | Extract : HttpRequestHeadersSchemaFromBody, never>; type HttpRequestSearchParamsSchema = 'searchParams' extends keyof MethodSchema['request'] ? Default['searchParams'] : never; type HttpRequestBodySchema = Replace['body']>, null>, undefined, null>; type HttpResponseHeadersSchemaFromBody = 'body' extends keyof ResponseSchema ? [ResponseSchema['body']] extends [never] ? DefaultHeadersSchema : [Extract] extends [never] ? 'headers' extends keyof ResponseSchema ? [ResponseSchema['headers']] extends [never] ? DefaultHeadersSchema : 'content-type' extends keyof Default ? DefaultHeadersSchema : { 'content-type': 'application/json'; } : { 'content-type': 'application/json'; } : DefaultHeadersSchema : DefaultHeadersSchema; type HttpResponseHeadersSchema = 'headers' extends keyof Default[StatusCode] ? [Default[StatusCode]] extends [never] ? HttpResponseHeadersSchemaFromBody[StatusCode]>, never> : (Default[StatusCode]>['headers'] & HttpResponseHeadersSchemaFromBody[StatusCode]>, {}>) | Extract[StatusCode]>['headers'], undefined> : HttpResponseHeadersSchemaFromBody[StatusCode]>, never>; type HttpResponseBodySchema = Replace[StatusCode]>['body']>, null>, undefined, null>; /** * Error thrown when a value is not valid {@link https://developer.mozilla.org/docs/Web/API/FormData FormData}. HTTP * interceptors might throw this error when trying to parse the body of a request or response with the header * `'content-type': 'multipart/form-data'`, if the content cannot be parsed to form data. */ declare class InvalidFormDataError extends SyntaxError { constructor(value: string); } /** * Error thrown when a value is not valid JSON. HTTP interceptors might throw this error when trying to parse the body * of a request or response with the header `'content-type': 'application/json'`, if the content cannot be parsed to * JSON. */ declare class InvalidJSONError extends SyntaxError { constructor(value: string); } /** * Parses the body of a {@link https://developer.mozilla.org/docs/Web/API/Request request} or * {@link https://developer.mozilla.org/docs/Web/API/Response response} based on its `content-type` header. * * If the body is empty, `null` is returned. If the `content-type` header is not present or not recognized, an attempt * is made to parse the body as JSON, and if that fails, it is returned as a `Blob`. * * | `content-type` | Parsed as | * | ----------------------------------- | ------------------------------------------------------------------------ | * | `application/json` | `JSON` (object) | * | `application/xml` | `string` | * | `application/x-www-form-urlencoded` | [`HttpSearchParams`](https://zimic.dev/docs/http/api/http-search-params) | * | `application/*` (others) | `Blob` | * | `multipart/form-data` | [`HttpFormData`](https://zimic.dev/docs/http/api/http-form-data) | * | `multipart/*` (others) | `Blob` | * | `text/*` | `string` | * | `image/*` | `Blob` | * | `audio/*` | `Blob` | * | `font/*` | `Blob` | * | `video/*` | `Blob` | * | Others | `JSON` if possible, otherwise `Blob` | * * @throws {InvalidJSONError} If the `content-type` starts with `application/json` but the body cannot be parsed to * JSON. * @throws {InvalidFormDataError} If the `content-type` starts with `multipart/form-data` but the body cannot be parsed * to form data. */ declare function parseHttpBody(resource: Request | Response): Promise; export { type AllowAnyStringInPathParams, HTTP_METHODS, HttpBody, HttpFormData, HttpFormDataSchema, HttpFormDataSchemaName, type HttpFormDataSerialized, HttpHeaders, type HttpHeadersInit, HttpHeadersSchema, type HttpHeadersSchemaName, type HttpHeadersSchemaTuple, type HttpHeadersSerialized, type HttpMethod, type HttpMethodSchema, type HttpMethodsSchema, HttpPathParamsSchema, type HttpPathParamsSerialized, type HttpRequest, type HttpRequestBodySchema, type HttpRequestHeadersSchema, type HttpRequestSchema, type HttpRequestSearchParamsSchema, type HttpResponse, type HttpResponseBodySchema, type HttpResponseHeadersSchema, type HttpResponseSchema, type HttpResponseSchemaByStatusCode, type HttpResponseSchemaStatusCode, HttpSchema, type HttpSchemaMethod, HttpSchemaPath, HttpSearchParams, type HttpSearchParamsInit, HttpSearchParamsSchema, HttpSearchParamsSchemaName, type HttpSearchParamsSchemaTuple, type HttpSearchParamsSerialized, HttpStatusCode, type InferPathParams, InvalidFormDataError, InvalidJSONError, type JSONSerialized, JSONValue, type LiteralHttpSchemaPathFromNonLiteral, type MergeHttpResponsesByStatusCode, type StrictFormData, type StrictHeaders, type StrictURLSearchParams, parseHttpBody };