-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyped-fetch.ts
185 lines (145 loc) · 7.47 KB
/
typed-fetch.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
export interface ClientOptions extends RequestInit {
baseUrl?: string;
// Override fetch function (useful for testing)
fetch?: (input: Request) => Promise<Response>;
// Global headers -- these will be added to every request
headers?: Record<string, string>;
// global body serializer -- allows you to customize how the body is serialized before sending
// normally not needed unless you are using something like XML instead of JSON
bodySerializer?: (body: any) => BodyInit | null;
// global query serializer -- allows you to customize how the query is serialized before sending
// normally not needed unless you are using some custom array serialization like {foo: [1,2,3,4]} => ?foo=1;2;3;4
querySerializer?: (query: any) => string;
}
export default function createClient<T>(options?: ClientOptions): T {
return new ClientImpl(options) as T;
}
///////////////////////////////////////////////////////////////////
// Like RequestInit but with some custom fields
type RequestInitExtended = Omit<RequestInit, "body" | "headers"> & {
params?: object;
body?: object | BodyInit | null;
headers?: Record<string, string>;
parseAs?: "json" | "text" | "blob" | "arrayBuffer" | "formData";
// local body serializer -- allows you to customize how the body is serialized before sending
// normally not needed unless you are using something like XML instead of JSON
bodySerializer?: (body: any) => BodyInit | null;
// local query serializer -- allows you to customize how the query is serialized before sending
// normally not needed unless you are using some custom array serialization like {foo: [1,2,3,4]} => ?foo=1;2;3;4
querySerializer?: (query: any) => string;
};
type TypedFetchResponse = {
data: any;
error: any;
response: Response;
};
type TypedFetchParams = {
path?: Record<string, any>;
query?: Record<string, any>;
headers?: Record<string, string>;
cookies?: Record<string, string>;
};
function defaultBodySerializer(contentType: string, body: any): BodyInit | null {
if (contentType.includes("application/json"))
return JSON.stringify(body);
if (contentType.includes("application/x-www-form-urlencoded"))
return new URLSearchParams(body as Record<string, string>);
if (contentType.includes("multipart/form-data")) {
const formData = new FormData();
for (const [key, value] of Object.entries(body as Record<string, any>))
formData.append(key, value);
return formData;
}
return body;
}
function defaultQuerySerializer(query: Record<string, any>): string {
return new URLSearchParams(query).toString();
}
function resolveParams(url: string, init: RequestInitExtended, params: TypedFetchParams, querySerializer: (query: Record<string, any>) => string): string {
if (params["path"]) {
for (const [key, value] of Object.entries(params["path"]))
url = url.replace(`{${key}}`, "" + value);
}
if (params["query"]) {
url += "?" + querySerializer(params["query"]);
}
if (params["headers"]) {
init.headers = { ...init.headers, ...params["headers"] };
}
if (params["cookies"]) {
// Add cookies to the "Cookie" header
const cookies = Object.entries(params["cookies"]).map(([key, value]) => `${key}=${value}`).join("; ");
init.headers = { ...init.headers, "Cookie": cookies };
}
return url;
}
function resolveBody(init: RequestInitExtended, contentType: string, bodySerializer: (contentType: string, body: any) => BodyInit | null) {
init.body = bodySerializer(contentType, init.body as any);
}
function resolveHeaders(init: RequestInitExtended, globalHeaders: Record<string, string> = {}) {
let defaultHeaders: Record<string, string> = {}
if (init.body) {
let defaultContentType = "application/json";
if (init.body instanceof Blob || init.body instanceof File || init.body instanceof ArrayBuffer)
defaultContentType = "application/octet-stream";
defaultHeaders["Content-Type"] = defaultContentType;
}
// default headers have the lowest priority, followed by globalHeaders, and finally init.headers
return { ...defaultHeaders, ...globalHeaders, ...init.headers };
}
class ClientImpl {
#options: ClientOptions;
#fetchFn: (input: Request) => Promise<Response>;
constructor(options?: ClientOptions) {
this.#options = {};
this.#fetchFn = options?.fetch || globalThis.fetch.bind(globalThis);
this.#options.baseUrl = options?.baseUrl || ""; // Make sure baseUrl is always a string
this.#options.headers = options?.headers || {};
}
async #fetch(method: string, url: string, init?: RequestInitExtended): Promise<TypedFetchResponse> {
if (!init)
init = {};
init.method = method;
init.headers = resolveHeaders(init, this.#options.headers);
if (init?.params) {
const querySerializer = init?.querySerializer || this.#options.querySerializer || defaultQuerySerializer;
url = resolveParams(url, init, init.params, querySerializer);
}
if (init?.body) {
const bodySerializer = init?.bodySerializer || this.#options.bodySerializer || defaultBodySerializer;
resolveBody(init, init.headers["Content-Type"] || "", bodySerializer);
}
const requestUrl = this.#options.baseUrl ? new URL(url, this.#options.baseUrl) : url;
const request = new Request(requestUrl, init as RequestInit);
const response = await this.#fetchFn(request);
init.parseAs = init.parseAs || "json";
// Return {} for "no content" responses to match openapi-fetch truthy behavior
if (response.headers.get("Content-Length") === "0") {
return { data: undefined, error: {}, response };
}
// Return {} for "no content" responses to match openapi-fetch truthy behavior
if (response.status === 204) {
return { data: {}, error: undefined, response };
}
if (response.ok) {
return { data: await response[init.parseAs](), error: undefined, response };
} else {
// Mimic openapi-fetch error handling by falling back to text
let error = await response.text();
try {
error = JSON.parse(error); // attempt to parse as JSON
} catch {
// noop
}
return { data: undefined, error, response };
}
}
async GET(url: string, init?: RequestInitExtended): Promise<TypedFetchResponse> { return this.#fetch("GET", url, init); }
async PUT(url: string, init?: RequestInitExtended): Promise<TypedFetchResponse> { return this.#fetch("PUT", url, init); }
async POST(url: string, init?: RequestInitExtended): Promise<TypedFetchResponse> { return this.#fetch("POST", url, init); }
async DELETE(url: string, init?: RequestInitExtended): Promise<TypedFetchResponse> { return this.#fetch("DELETE", url, init); }
async OPTIONS(url: string, init?: RequestInitExtended): Promise<TypedFetchResponse> { return this.#fetch("OPTIONS", url, init); }
async HEAD(url: string, init?: RequestInitExtended): Promise<TypedFetchResponse> { return this.#fetch("HEAD", url, init); }
async PATCH(url: string, init?: RequestInitExtended): Promise<TypedFetchResponse> { return this.#fetch("PATCH", url, init); }
async TRACE(url: string, init?: RequestInitExtended): Promise<TypedFetchResponse> { return this.#fetch("TRACE", url, init); }
}