simple-typed-fetch
Version:
Making HTTP requests human way
499 lines (495 loc) • 15.6 kB
TypeScript
import * as neverthrow from 'neverthrow';
import { Schema, z } from 'zod';
declare function fetchWithValidation<DataOut, DataIn, ErrorOut, ErrorIn>(url: string, schema: Schema<DataOut, z.ZodTypeDef, DataIn>, options?: RequestInit, errorSchema?: Schema<ErrorOut, z.ZodTypeDef, ErrorIn>): Promise<neverthrow.Err<never, {
message: string;
error: Error;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "fetchError";
}> | neverthrow.Err<never, {
message: string;
error: unknown;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "unknownFetchThrow";
}> | neverthrow.Err<never, {
message: string;
error: Error;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "unknownGetTextError";
}> | neverthrow.Err<never, {
message: string;
error: unknown;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "unknownGetTextUnknownError";
}> | neverthrow.Ok<{
data: DataOut;
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
}, never> | neverthrow.Err<never, {
message: string;
error: {
message: string;
error: Error;
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "jsonParseError";
} | {
message: string;
error: unknown;
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "jsonParseUnknownError";
};
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "jsonParseError";
}> | neverthrow.Err<never, {
message: string;
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "serverError";
}> | neverthrow.Err<never, {
message: string;
errorMetadata: ErrorOut | undefined;
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "clientError";
}> | neverthrow.Err<never, {
message: string;
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "payloadParseError";
}>>;
declare class Wrapper<DataOut, DataIn, ErrorOut, ErrorIn> {
wrapped(url: string, schema: Schema<DataOut, z.ZodTypeDef, DataIn>, options?: RequestInit, errorSchema?: Schema<ErrorOut, z.ZodTypeDef, ErrorIn>): Promise<neverthrow.Err<never, {
message: string;
error: Error;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "fetchError";
}> | neverthrow.Err<never, {
message: string;
error: unknown;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "unknownFetchThrow";
}> | neverthrow.Err<never, {
message: string;
error: Error;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "unknownGetTextError";
}> | neverthrow.Err<never, {
message: string;
error: unknown;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "unknownGetTextUnknownError";
}> | neverthrow.Ok<{
data: DataOut;
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
}, never> | neverthrow.Err<never, {
message: string;
error: {
message: string;
error: Error;
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "jsonParseError";
} | {
message: string;
error: unknown;
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "jsonParseUnknownError";
};
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "jsonParseError";
}> | neverthrow.Err<never, {
message: string;
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "serverError";
}> | neverthrow.Err<never, {
message: string;
errorMetadata: ErrorOut | undefined;
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "clientError";
}> | neverthrow.Err<never, {
message: string;
text: string;
response: Response;
requestOptions: {
headers: HeadersInit;
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
method?: string;
mode?: RequestMode;
priority?: RequestPriority;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
signal?: AbortSignal | null;
window?: null;
};
url: string;
type: "payloadParseError";
}>>;
}
type FetchWithValidationType<O, I, EO, EI> = ReturnType<Wrapper<O, I, EO, EI>['wrapped']>;
declare function simpleFetch<O, I, EO, EI, P extends unknown[]>(f: (...params: P) => FetchWithValidationType<O, I, EO, EI>): (...params: Parameters<typeof f>) => Promise<O>;
export { type FetchWithValidationType, fetchWithValidation, simpleFetch };