@pbpeterson/typed-fetch
Version:
A type-safe HTTP client that never throws. Inspired by Go's error handling pattern.
443 lines (396 loc) • 16.5 kB
text/typescript
declare abstract class BaseHttpError extends Error {
protected readonly response: Response;
readonly headers: Headers;
abstract readonly status: number;
abstract readonly statusText: string;
constructor(response: Response);
json(): Promise<any>;
text(): Promise<string>;
blob(): Promise<Blob>;
arrayBuffer(): Promise<ArrayBuffer>;
abstract clone(): BaseHttpError;
}
declare class InternalServerError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): InternalServerError;
}
declare class NetworkError extends Error {
constructor(message?: string);
}
declare class BadRequestError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): BadRequestError;
}
declare class PaymentRequiredError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): PaymentRequiredError;
}
declare class UnauthorizedError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): UnauthorizedError;
}
declare class ForbiddenError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): ForbiddenError;
}
declare class NotFoundError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): NotFoundError;
}
declare class MethodNotAllowedError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): MethodNotAllowedError;
}
declare class NotAcceptableError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): NotAcceptableError;
}
declare class ProxyAuthenticationRequiredError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): ProxyAuthenticationRequiredError;
}
declare class RequestTimeoutError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): RequestTimeoutError;
}
declare class ConflictError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): ConflictError;
}
declare class GoneError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): GoneError;
}
declare class LengthRequiredError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): LengthRequiredError;
}
declare class PreconditionFailedError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): PreconditionFailedError;
}
declare class RequestTooLongError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): RequestTooLongError;
}
declare class RequestUriTooLongError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): RequestUriTooLongError;
}
declare class UnsupportedMediaTypeError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): UnsupportedMediaTypeError;
}
declare class RequestedRangeNotSatisfiableError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): RequestedRangeNotSatisfiableError;
}
declare class ExpectationFailedError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): ExpectationFailedError;
}
declare class ImATeapotError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): ImATeapotError;
}
declare class MisdirectedRequestError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): MisdirectedRequestError;
}
declare class UnprocessableEntityError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): UnprocessableEntityError;
}
declare class LockedError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): LockedError;
}
declare class FailedDependencyError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): FailedDependencyError;
}
declare class TooEarlyError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): TooEarlyError;
}
declare class UpgradeRequiredError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): UpgradeRequiredError;
}
declare class PreconditionRequiredError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): PreconditionRequiredError;
}
declare class TooManyRequestsError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): TooManyRequestsError;
}
declare class RequestHeaderFieldsTooLargeError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): RequestHeaderFieldsTooLargeError;
}
declare class UnavailableForLegalReasonsError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): UnavailableForLegalReasonsError;
}
declare class NotImplementedError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): NotImplementedError;
}
declare class BadGatewayError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): BadGatewayError;
}
declare class ServiceUnavailableError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): ServiceUnavailableError;
}
declare class GatewayTimeoutError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): GatewayTimeoutError;
}
declare class HttpVersionNotSupportedError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): HttpVersionNotSupportedError;
}
declare class VariantAlsoNegotiatesError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): VariantAlsoNegotiatesError;
}
declare class InsufficientStorageError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): InsufficientStorageError;
}
declare class LoopDetectedError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): LoopDetectedError;
}
declare class NotExtendedError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): NotExtendedError;
}
declare class NetworkAuthenticationRequiredError extends BaseHttpError {
readonly status: number;
readonly statusText: string;
static readonly status: number;
static readonly statusText: string;
constructor(response: Response);
clone(): NetworkAuthenticationRequiredError;
}
type ClientErrors = BadRequestError | ConflictError | ExpectationFailedError | FailedDependencyError | ForbiddenError | GoneError | ImATeapotError | LengthRequiredError | LockedError | MethodNotAllowedError | MisdirectedRequestError | NotAcceptableError | NotFoundError | PaymentRequiredError | PreconditionFailedError | PreconditionRequiredError | ProxyAuthenticationRequiredError | RequestedRangeNotSatisfiableError | RequestHeaderFieldsTooLargeError | RequestTimeoutError | RequestTooLongError | RequestUriTooLongError | TooEarlyError | TooManyRequestsError | UnauthorizedError | UnavailableForLegalReasonsError | UnprocessableEntityError | UnsupportedMediaTypeError | UpgradeRequiredError;
type ServerErrors = BadGatewayError | GatewayTimeoutError | HttpVersionNotSupportedError | InsufficientStorageError | InternalServerError | LoopDetectedError | NetworkAuthenticationRequiredError | NotExtendedError | NotImplementedError | ServiceUnavailableError | VariantAlsoNegotiatesError;
type ContentType = "application/json" | "application/xml" | "application/x-www-form-urlencoded" | "text/plain" | "text/html" | "text/css" | "text/javascript" | "multipart/form-data" | "application/octet-stream" | "image/jpeg" | "image/png" | "image/gif" | "image/svg+xml" | (string & {});
type Canonical<T extends string> = T | Lowercase<T>;
type CacheControlDirective = "no-cache" | "no-store" | "max-age=0" | "public" | "private" | "must-revalidate";
type StrictHeaders = {
"Content-Type"?: ContentType;
Authorization?: `${string} ${string}`;
Accept?: ContentType | "*/*";
"Accept-Encoding"?: "gzip" | "deflate" | "br" | "identity" | "*" | (string & {});
"Accept-Language"?: "en" | "en-US" | "en-GB" | "fr" | "de" | "es" | "*" | (string & {});
"Cache-Control"?: CacheControlDirective | `${CacheControlDirective}, ${CacheControlDirective}` | (string & {});
Connection?: "keep-alive" | "close" | "upgrade";
"Content-Encoding"?: "gzip" | "deflate" | "br" | "identity";
"Content-Length"?: `${number}` | (string & {});
Cookie?: string;
"Set-Cookie"?: string;
ETag?: `"${string}"` | `W/"${string}"` | (string & {});
Host?: string;
"If-Modified-Since"?: string;
"If-None-Match"?: `"${string}"` | `W/"${string}"` | "*" | (string & {});
"Last-Modified"?: string;
Location?: string;
Origin?: string;
Range?: `bytes=${string}` | (string & {});
Referer?: string;
"User-Agent"?: string;
"WWW-Authenticate"?: `Bearer realm="${string}"` | `Basic realm="${string}"` | (string & {});
"X-Requested-With"?: "XMLHttpRequest";
"Access-Control-Allow-Origin"?: "*" | (string & {});
"Access-Control-Allow-Methods"?: "GET" | "POST" | "PUT" | "DELETE" | "OPTIONS" | "PATCH" | "HEAD" | (string & {});
"Access-Control-Allow-Headers"?: "Content-Type" | "Authorization" | "X-Requested-With" | "*" | (string & {});
"Access-Control-Allow-Credentials"?: "true" | "false";
"Content-Security-Policy"?: string;
"X-Frame-Options"?: "DENY" | "SAMEORIGIN" | `ALLOW-FROM ${string}`;
"X-Content-Type-Options"?: "nosniff";
"Strict-Transport-Security"?: `max-age=${number}` | `max-age=${number}; includeSubDomains` | (string & {});
[key: string]: string | undefined;
};
type TypedHeaders = {
[K in keyof StrictHeaders as Canonical<K & string>]?: StrictHeaders[K];
} | HeadersInit;
type HttpMethods = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS" | "CONNECT" | "TRACE";
interface TypedResponse<JsonReturnType> extends Response {
json(): Promise<JsonReturnType>;
}
type TypedFetchReturnType<JsonReturnType, ErrorType extends ClientErrors = ClientErrors> = {
response: TypedResponse<JsonReturnType>;
error: null;
} | {
response: null;
error: ErrorType | ServerErrors | NetworkError;
};
type FetchParams = Parameters<typeof fetch>;
type URL = FetchParams[0];
type Options = FetchParams[1] & {
headers?: TypedHeaders;
method?: HttpMethods;
};
declare function typedFetch<JsonReturnType, ErrorType extends ClientErrors = ClientErrors>(url: URL, options?: Options): Promise<TypedFetchReturnType<JsonReturnType, ErrorType>>;
export { BadGatewayError, BadRequestError, ConflictError, ExpectationFailedError, FailedDependencyError, ForbiddenError, GatewayTimeoutError, GoneError, HttpVersionNotSupportedError, ImATeapotError, InsufficientStorageError, InternalServerError, LengthRequiredError, LockedError, LoopDetectedError, MethodNotAllowedError, MisdirectedRequestError, NetworkAuthenticationRequiredError, NetworkError, NotAcceptableError, NotExtendedError, NotFoundError, NotImplementedError, PaymentRequiredError, PreconditionFailedError, PreconditionRequiredError, ProxyAuthenticationRequiredError, RequestHeaderFieldsTooLargeError, RequestTimeoutError, RequestTooLongError, RequestUriTooLongError, RequestedRangeNotSatisfiableError, ServiceUnavailableError, TooEarlyError, TooManyRequestsError, UnauthorizedError, UnavailableForLegalReasonsError, UnprocessableEntityError, UnsupportedMediaTypeError, UpgradeRequiredError, VariantAlsoNegotiatesError, typedFetch };