-
-
Notifications
You must be signed in to change notification settings - Fork 529
/
Copy pathindex.ts
196 lines (178 loc) · 10 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import type { ErrorResponse, HttpMethod, SuccessResponse, FilterKeys, MediaType, PathsWithMethod, ResponseObjectMap, OperationRequestBodyContent, HasRequiredKeys } from "openapi-typescript-helpers";
// settings & const
const DEFAULT_HEADERS = {
"Content-Type": "application/json",
};
// Note: though "any" is considered bad practice in general, this library relies
// on "any" for type inference only it can give. Same goes for the "{}" type.
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types */
/** options for each client instance */
interface ClientOptions extends Omit<RequestInit, "headers"> {
/** set the common root URL for all API requests */
baseUrl?: string;
/** custom fetch (defaults to globalThis.fetch) */
fetch?: typeof fetch;
/** global querySerializer */
querySerializer?: QuerySerializer<unknown>;
/** global bodySerializer */
bodySerializer?: BodySerializer<unknown>;
// headers override to make typing friendlier
headers?: HeadersOptions;
}
export type HeadersOptions = HeadersInit | Record<string, string | number | boolean | null | undefined>;
export type QuerySerializer<T> = (query: T extends { parameters: any } ? NonNullable<T["parameters"]["query"]> : Record<string, unknown>) => string;
export type BodySerializer<T> = (body: OperationRequestBodyContent<T>) => any;
export type ParseAs = "json" | "text" | "blob" | "arrayBuffer" | "stream";
export interface DefaultParamsOption {
params?: { query?: Record<string, unknown> };
}
export type ParamsOption<T> = T extends { parameters: any } ? { params: NonNullable<T["parameters"]> } : DefaultParamsOption;
export type RequestBodyOption<T> = OperationRequestBodyContent<T> extends never ? { body?: never } : undefined extends OperationRequestBodyContent<T> ? { body?: OperationRequestBodyContent<T> } : { body: OperationRequestBodyContent<T> };
export type FetchOptions<T> = RequestOptions<T> & Omit<RequestInit, "body">;
export type FetchResponse<T> =
| { data: FilterKeys<SuccessResponse<ResponseObjectMap<T>>, MediaType>; error?: never; response: Response }
| { data?: never; error: FilterKeys<ErrorResponse<ResponseObjectMap<T>>, MediaType>; response: Response };
export type RequestOptions<T> = ParamsOption<T> &
RequestBodyOption<T> & {
querySerializer?: QuerySerializer<T>;
bodySerializer?: BodySerializer<T>;
parseAs?: ParseAs;
};
export default function createClient<Paths extends {}>(clientOptions: ClientOptions = {}) {
const { fetch = globalThis.fetch, querySerializer: globalQuerySerializer, bodySerializer: globalBodySerializer, ...options } = clientOptions;
let baseUrl = options.baseUrl ?? "";
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.slice(0, -1); // remove trailing slash
}
async function coreFetch<P extends keyof Paths, M extends HttpMethod>(url: P, fetchOptions: FetchOptions<M extends keyof Paths[P] ? Paths[P][M] : never>): Promise<FetchResponse<M extends keyof Paths[P] ? Paths[P][M] : unknown>> {
const { headers, body: requestBody, params = {}, parseAs = "json", querySerializer = globalQuerySerializer ?? defaultQuerySerializer, bodySerializer = globalBodySerializer ?? defaultBodySerializer, ...init } = fetchOptions || {};
// URL
const finalURL = createFinalURL(url as string, { baseUrl, params, querySerializer });
const finalHeaders = mergeHeaders(DEFAULT_HEADERS, clientOptions?.headers, headers, (params as any).header);
// fetch!
const requestInit: RequestInit = { redirect: "follow", ...options, ...init, headers: finalHeaders };
if (requestBody) requestInit.body = bodySerializer(requestBody as any);
// remove `Content-Type` if serialized body is FormData; browser will correctly set Content-Type & boundary expression
if (requestInit.body instanceof FormData) finalHeaders.delete("Content-Type");
const response = await fetch(finalURL, requestInit);
// handle empty content
// note: we return `{}` because we want user truthy checks for `.data` or `.error` to succeed
if (response.status === 204 || response.headers.get("Content-Length") === "0") {
return response.ok ? { data: {} as any, response: response as any } : { error: {} as any, response: response as any };
}
// parse response (falling back to .text() when necessary)
if (response.ok) {
let data: any; // we have to leave this empty here so that we don't consume the body
if (parseAs !== "stream") {
const cloned = response.clone();
data = typeof cloned[parseAs] === "function" ? await cloned[parseAs]() : await cloned.text();
} else {
// bun consumes the body when calling response.body, therefore we need to clone the response before accessing it
data = response.clone().body;
}
return { data, response: response as any };
}
// handle errors (always parse as .json() or .text())
let error: any = {};
try {
error = await response.clone().json();
} catch {
error = await response.clone().text();
}
return { error, response: response as any };
}
type GetPaths = PathsWithMethod<Paths, "get">;
type PutPaths = PathsWithMethod<Paths, "put">;
type PostPaths = PathsWithMethod<Paths, "post">;
type DeletePaths = PathsWithMethod<Paths, "delete">;
type OptionsPaths = PathsWithMethod<Paths, "options">;
type HeadPaths = PathsWithMethod<Paths, "head">;
type PatchPaths = PathsWithMethod<Paths, "patch">;
type TracePaths = PathsWithMethod<Paths, "trace">;
type GetFetchOptions<P extends GetPaths> = FetchOptions<FilterKeys<Paths[P], "get">>;
type PutFetchOptions<P extends PutPaths> = FetchOptions<FilterKeys<Paths[P], "put">>;
type PostFetchOptions<P extends PostPaths> = FetchOptions<FilterKeys<Paths[P], "post">>;
type DeleteFetchOptions<P extends DeletePaths> = FetchOptions<FilterKeys<Paths[P], "delete">>;
type OptionsFetchOptions<P extends OptionsPaths> = FetchOptions<FilterKeys<Paths[P], "options">>;
type HeadFetchOptions<P extends HeadPaths> = FetchOptions<FilterKeys<Paths[P], "head">>;
type PatchFetchOptions<P extends PatchPaths> = FetchOptions<FilterKeys<Paths[P], "patch">>;
type TraceFetchOptions<P extends TracePaths> = FetchOptions<FilterKeys<Paths[P], "trace">>;
return {
/** Call a GET endpoint */
async GET<P extends GetPaths>(url: P, ...init: HasRequiredKeys<GetFetchOptions<P>> extends never ? [GetFetchOptions<P>?] : [GetFetchOptions<P>]) {
return coreFetch<P, "get">(url, { ...init[0], method: "GET" } as any);
},
/** Call a PUT endpoint */
async PUT<P extends PutPaths>(url: P, ...init: HasRequiredKeys<PutFetchOptions<P>> extends never ? [PutFetchOptions<P>?] : [PutFetchOptions<P>]) {
return coreFetch<P, "put">(url, { ...init[0], method: "PUT" } as any);
},
/** Call a POST endpoint */
async POST<P extends PostPaths>(url: P, ...init: HasRequiredKeys<PostFetchOptions<P>> extends never ? [PostFetchOptions<P>?] : [PostFetchOptions<P>]) {
return coreFetch<P, "post">(url, { ...init[0], method: "POST" } as any);
},
/** Call a DELETE endpoint */
async DELETE<P extends DeletePaths>(url: P, ...init: HasRequiredKeys<DeleteFetchOptions<P>> extends never ? [DeleteFetchOptions<P>?] : [DeleteFetchOptions<P>]) {
return coreFetch<P, "delete">(url, { ...init[0], method: "DELETE" } as any);
},
/** Call a OPTIONS endpoint */
async OPTIONS<P extends OptionsPaths>(url: P, ...init: HasRequiredKeys<OptionsFetchOptions<P>> extends never ? [OptionsFetchOptions<P>?] : [OptionsFetchOptions<P>]) {
return coreFetch<P, "options">(url, { ...init[0], method: "OPTIONS" } as any);
},
/** Call a HEAD endpoint */
async HEAD<P extends HeadPaths>(url: P, ...init: HasRequiredKeys<HeadFetchOptions<P>> extends never ? [HeadFetchOptions<P>?] : [HeadFetchOptions<P>]) {
return coreFetch<P, "head">(url, { ...init[0], method: "HEAD" } as any);
},
/** Call a PATCH endpoint */
async PATCH<P extends PatchPaths>(url: P, ...init: HasRequiredKeys<PatchFetchOptions<P>> extends never ? [PatchFetchOptions<P>?] : [PatchFetchOptions<P>]) {
return coreFetch<P, "patch">(url, { ...init[0], method: "PATCH" } as any);
},
/** Call a TRACE endpoint */
async TRACE<P extends TracePaths>(url: P, ...init: HasRequiredKeys<TraceFetchOptions<P>> extends never ? [TraceFetchOptions<P>?] : [TraceFetchOptions<P>]) {
return coreFetch<P, "trace">(url, { ...init[0], method: "TRACE" } as any);
},
};
}
// utils
/** serialize query params to string */
export function defaultQuerySerializer<T = unknown>(q: T): string {
const search = new URLSearchParams();
if (q && typeof q === "object") {
for (const [k, v] of Object.entries(q)) {
if (v === undefined || v === null) continue;
search.set(k, v);
}
}
return search.toString();
}
/** serialize body object to string */
export function defaultBodySerializer<T>(body: T): string {
return JSON.stringify(body);
}
/** Construct URL string from baseUrl and handle path and query params */
export function createFinalURL<O>(pathname: string, options: { baseUrl: string; params: { query?: Record<string, unknown>; path?: Record<string, unknown> }; querySerializer: QuerySerializer<O> }): string {
let finalURL = `${options.baseUrl}${pathname}`;
if (options.params.path) {
for (const [k, v] of Object.entries(options.params.path)) finalURL = finalURL.replace(`{${k}}`, encodeURIComponent(String(v)));
}
if (options.params.query) {
const search = options.querySerializer(options.params.query as any);
if (search) finalURL += `?${search}`;
}
return finalURL;
}
/** merge headers a and b, with b taking priority */
export function mergeHeaders(...allHeaders: (HeadersOptions | undefined)[]): Headers {
const headers = new Headers();
for (const headerSet of allHeaders) {
if (!headerSet || typeof headerSet !== "object") continue;
const iterator = headerSet instanceof Headers ? headerSet.entries() : Object.entries(headerSet);
for (const [k, v] of iterator) {
if (v === null) {
headers.delete(k);
} else if (v !== undefined) {
headers.set(k, v as any);
}
}
}
return headers;
}