This repository was archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.ts
179 lines (166 loc) · 6.13 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
const DEFAULT_HEADERS = {
'Content-Type': 'application/json',
};
/** options for each client instance */
interface ClientOptions extends RequestInit {
/** set the common root URL for all API requests */
baseUrl?: string;
}
export interface BaseParams {
path?: Record<string, unknown>;
query?: Record<string, unknown>;
}
export type Method = 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch' | 'trace';
/** Gets a union of paths which have method */
type PathsWith<T, M extends Method> = {
[Path in keyof T]: T[Path] extends { [K in M]: unknown } ? Path : never;
}[keyof T];
type PathParams<T> = T extends { parameters: any } ? { params: T['parameters'] } : { params?: BaseParams };
type MethodParams<T> = T extends {
parameters: any;
}
? { params: T['parameters'] }
: { params?: BaseParams };
type Params<T> = PathParams<T> & MethodParams<T>;
type RequestBody<T> = T extends { requestBody: any } ? { body: Unwrap<T['requestBody']> } : { body?: never };
type FetchOptions<T> = Params<T> & RequestBody<T> & Omit<RequestInit, 'body'>;
type TruncatedResponse = Omit<Response, 'arrayBuffer' | 'blob' | 'body' | 'clone' | 'formData' | 'json' | 'text'>;
/** Infer request/response from content type */
type Unwrap<T> = T extends {
content: { 'application/json': any };
}
? T['content']['application/json']
: T extends { content: { 'application/json;charset=utf-8': any } }
? T['content']['application/json;charset=utf-8']
: T extends { content: { '*/*': any } }
? T['content']['*/*']
: T;
type Success<T> = T extends { 200: any } ? T[200] : T extends { 201: any } ? T[201] : T extends { 202: any } ? T[202] : T extends { default: any } ? T['default'] : unknown;
type Error<T> = T extends { 500: any }
? T[500]
: T extends { 404: any }
? T[404]
: T extends { 402: any }
? T[402]
: T extends { 401: any }
? T[401]
: T extends { 400: any }
? T[400]
: T extends { 422: any }
? T[422]
: T extends { 418: any }
? T[418]
: T extends { 417: any }
? T[417]
: T extends { 416: any }
? T[416]
: T extends { 415: any }
? T[415]
: T extends { 414: any }
? T[414]
: T extends { 413: any }
? T[413]
: T extends { 412: any }
? T[412]
: T extends { 411: any }
? T[411]
: T extends { 410: any }
? T[410]
: T extends { 409: any }
? T[409]
: T extends { 408: any }
? T[408]
: T extends { 407: any }
? T[407]
: T extends { 406: any }
? T[406]
: T extends { 405: any }
? T[405]
: T extends { default: any }
? T['default']
: unknown;
type FetchResponse<T> =
| {
data: T extends { responses: any } ? NonNullable<Unwrap<Success<T['responses']>>> : unknown;
error?: never;
response: TruncatedResponse;
}
| {
data?: never;
error: T extends { responses: any } ? NonNullable<Unwrap<Error<T['responses']>>> : unknown;
response: TruncatedResponse;
};
export default function createClient<T>(options?: ClientOptions) {
const defaultHeaders = new Headers({
...DEFAULT_HEADERS,
...(options?.headers ?? {}),
});
async function coreFetch<U extends keyof T, M extends keyof T[U]>(url: U, fetchOptions: FetchOptions<T[U][M]>): Promise<FetchResponse<T[U][M]>> {
let { headers, body, params = {}, ...init } = fetchOptions || {};
// URL
let finalURL = `${options?.baseUrl ?? ''}${url as string}`;
const { path, query } = (params as BaseParams | undefined) ?? {};
if (path) for (const [k, v] of Object.entries(path)) finalURL = finalURL.replace(`{${k}}`, encodeURIComponent(`${v}`.trim()));
if (query) finalURL = `${finalURL}?${new URLSearchParams(query as any).toString()}`;
// headers
const baseHeaders = new Headers(defaultHeaders); // clone defaults (don’t overwrite!)
const headerOverrides = new Headers(headers);
for (const [k, v] of headerOverrides.entries()) {
if (v === undefined || v === null) baseHeaders.delete(k); // allow `undefined` | `null` to erase value
else baseHeaders.set(k, v);
}
// fetch!
const res = await fetch(finalURL, {
redirect: 'follow',
...options,
...init,
headers: baseHeaders,
body: typeof body === 'string' ? body : JSON.stringify(body),
});
const response: TruncatedResponse = {
bodyUsed: res.bodyUsed,
headers: res.headers,
ok: res.ok,
redirected: res.redirected,
status: res.status,
statusText: res.statusText,
type: res.type,
url: res.url,
};
return res.ok ? { data: await res.json(), response } : { error: await res.json(), response };
}
return {
/** Call a GET endpoint */
async get<U extends PathsWith<T, 'get'>, M extends keyof T[U]>(url: U, init: FetchOptions<T[U][M]>) {
return coreFetch(url, { ...init, method: 'GET' });
},
/** Call a PUT endpoint */
async put<U extends PathsWith<T, 'put'>, M extends keyof T[U]>(url: U, init: FetchOptions<T[U][M]>) {
return coreFetch(url, { ...init, method: 'PUT' });
},
/** Call a POST endpoint */
async post<U extends PathsWith<T, 'post'>, M extends keyof T[U]>(url: U, init: FetchOptions<T[U][M]>) {
return coreFetch(url, { ...init, method: 'POST' });
},
/** Call a DELETE endpoint */
async del<U extends PathsWith<T, 'delete'>, M extends keyof T[U]>(url: U, init: FetchOptions<T[U][M]>) {
return coreFetch(url, { ...init, method: 'DELETE' });
},
/** Call a OPTIONS endpoint */
async options<U extends PathsWith<T, 'options'>, M extends keyof T[U]>(url: U, init: FetchOptions<T[U][M]>) {
return coreFetch(url, { ...init, method: 'OPTIONS' });
},
/** Call a HEAD endpoint */
async head<U extends PathsWith<T, 'head'>, M extends keyof T[U]>(url: U, init: FetchOptions<T[U][M]>) {
return coreFetch(url, { ...init, method: 'HEAD' });
},
/** Call a PATCH endpoint */
async patch<U extends PathsWith<T, 'patch'>, M extends keyof T[U]>(url: U, init: FetchOptions<T[U][M]>) {
return coreFetch(url, { ...init, method: 'PATCH' });
},
/** Call a TRACE endpoint */
async trace<U extends PathsWith<T, 'trace'>, M extends keyof T[U]>(url: U, init: FetchOptions<T[U][M]>) {
return coreFetch(url, { ...init, method: 'TRACE' });
},
};
}