|
1 | 1 | import type { Client, Config, RequestOptions } from "./types.js"; |
2 | 2 | import { |
3 | | - buildUrl, |
4 | | - createConfig, |
5 | | - createInterceptors, |
6 | | - getParseAs, |
7 | | - mergeConfigs, |
8 | | - mergeHeaders, |
9 | | - setAuthParams, |
| 3 | + buildUrl, |
| 4 | + createConfig, |
| 5 | + createInterceptors, |
| 6 | + getParseAs, |
| 7 | + mergeConfigs, |
| 8 | + mergeHeaders, |
| 9 | + setAuthParams, |
10 | 10 | } from "./utils.js"; |
11 | 11 |
|
12 | 12 | type ReqInit = Omit<RequestInit, "body" | "headers"> & { |
13 | | - body?: any; |
14 | | - headers: ReturnType<typeof mergeHeaders>; |
| 13 | + body?: any; |
| 14 | + headers: ReturnType<typeof mergeHeaders>; |
15 | 15 | }; |
16 | 16 |
|
17 | 17 | export const createClient = (config: Config = {}): Client => { |
18 | | - let _config = mergeConfigs(createConfig(), config); |
19 | | - |
20 | | - const getConfig = (): Config => ({ ..._config }); |
21 | | - |
22 | | - const setConfig = (config: Config): Config => { |
23 | | - _config = mergeConfigs(_config, config); |
24 | | - return getConfig(); |
25 | | - }; |
26 | | - |
27 | | - const interceptors = createInterceptors< |
28 | | - Request, |
29 | | - Response, |
30 | | - unknown, |
31 | | - RequestOptions |
32 | | - >(); |
33 | | - |
34 | | - const request: Client["request"] = async (options) => { |
35 | | - const opts = { |
36 | | - ..._config, |
37 | | - ...options, |
38 | | - fetch: options.fetch ?? _config.fetch ?? globalThis.fetch, |
39 | | - headers: mergeHeaders(_config.headers, options.headers), |
40 | | - }; |
41 | | - |
42 | | - if (opts.security) { |
43 | | - await setAuthParams({ |
44 | | - ...opts, |
45 | | - security: opts.security, |
46 | | - }); |
47 | | - } |
48 | | - |
49 | | - if (opts.body && opts.bodySerializer) { |
50 | | - opts.body = opts.bodySerializer(opts.body); |
51 | | - } |
52 | | - |
53 | | - // remove Content-Type header if body is empty to avoid sending invalid requests |
54 | | - if (opts.body === undefined || opts.body === "") { |
55 | | - opts.headers.delete("Content-Type"); |
56 | | - } |
57 | | - |
58 | | - const url = buildUrl(opts); |
59 | | - const requestInit: ReqInit = { |
60 | | - redirect: "follow", |
61 | | - ...opts, |
62 | | - }; |
63 | | - |
64 | | - let request = new Request(url, requestInit); |
65 | | - |
66 | | - for (const fn of interceptors.request._fns) { |
67 | | - if (fn) { |
68 | | - request = await fn(request, opts); |
69 | | - } |
70 | | - } |
71 | | - |
72 | | - // fetch must be assigned here, otherwise it would throw the error: |
73 | | - // TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation |
74 | | - const _fetch = opts.fetch!; |
75 | | - let response = await _fetch(request); |
76 | | - |
77 | | - for (const fn of interceptors.response._fns) { |
78 | | - if (fn) { |
79 | | - response = await fn(response, request, opts); |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - const result = { |
84 | | - request, |
85 | | - response, |
86 | | - }; |
87 | | - |
88 | | - if (response.ok) { |
89 | | - if ( |
90 | | - response.status === 204 || |
91 | | - response.headers.get("Content-Length") === "0" |
92 | | - ) { |
93 | | - return opts.responseStyle === "data" |
94 | | - ? {} |
95 | | - : { |
96 | | - data: {}, |
97 | | - ...result, |
98 | | - }; |
99 | | - } |
100 | | - |
101 | | - const parseAs = |
102 | | - (opts.parseAs === "auto" |
103 | | - ? getParseAs(response.headers.get("Content-Type")) |
104 | | - : opts.parseAs) ?? "json"; |
105 | | - |
106 | | - let data: any; |
107 | | - switch (parseAs) { |
108 | | - case "arrayBuffer": |
109 | | - case "blob": |
110 | | - case "formData": |
111 | | - case "json": |
112 | | - case "text": |
113 | | - data = await response[parseAs](); |
114 | | - break; |
115 | | - case "stream": |
116 | | - return opts.responseStyle === "data" |
117 | | - ? response.body |
118 | | - : { |
119 | | - data: response.body, |
120 | | - ...result, |
121 | | - }; |
122 | | - } |
123 | | - |
124 | | - if (parseAs === "json") { |
125 | | - if (opts.responseValidator) { |
126 | | - await opts.responseValidator(data); |
127 | | - } |
128 | | - |
129 | | - if (opts.responseTransformer) { |
130 | | - data = await opts.responseTransformer(data); |
131 | | - } |
132 | | - } |
133 | | - |
134 | | - return opts.responseStyle === "data" |
135 | | - ? data |
136 | | - : { |
137 | | - data, |
138 | | - ...result, |
139 | | - }; |
140 | | - } |
141 | | - |
142 | | - let error = await response.text(); |
143 | | - |
144 | | - try { |
145 | | - error = JSON.parse(error); |
146 | | - } catch { |
147 | | - // noop |
148 | | - } |
149 | | - |
150 | | - let finalError = error; |
151 | | - |
152 | | - for (const fn of interceptors.error._fns) { |
153 | | - if (fn) { |
154 | | - finalError = (await fn(error, response, request, opts)) as string; |
155 | | - } |
156 | | - } |
157 | | - |
158 | | - finalError = finalError || ({} as string); |
159 | | - |
160 | | - if (opts.throwOnError) { |
161 | | - throw finalError; |
162 | | - } |
163 | | - |
164 | | - // TODO: we probably want to return error and improve types |
165 | | - return opts.responseStyle === "data" |
166 | | - ? undefined |
167 | | - : { |
168 | | - error: finalError, |
169 | | - ...result, |
170 | | - }; |
171 | | - }; |
172 | | - |
173 | | - return { |
174 | | - buildUrl, |
175 | | - connect: (options) => request({ ...options, method: "CONNECT" }), |
176 | | - delete: (options) => request({ ...options, method: "DELETE" }), |
177 | | - get: (options) => request({ ...options, method: "GET" }), |
178 | | - getConfig, |
179 | | - head: (options) => request({ ...options, method: "HEAD" }), |
180 | | - interceptors, |
181 | | - options: (options) => request({ ...options, method: "OPTIONS" }), |
182 | | - patch: (options) => request({ ...options, method: "PATCH" }), |
183 | | - post: (options) => request({ ...options, method: "POST" }), |
184 | | - put: (options) => request({ ...options, method: "PUT" }), |
185 | | - request, |
186 | | - setConfig, |
187 | | - trace: (options) => request({ ...options, method: "TRACE" }), |
188 | | - }; |
| 18 | + let _config = mergeConfigs(createConfig(), config); |
| 19 | + |
| 20 | + const getConfig = (): Config => ({ ..._config }); |
| 21 | + |
| 22 | + const setConfig = (config: Config): Config => { |
| 23 | + _config = mergeConfigs(_config, config); |
| 24 | + return getConfig(); |
| 25 | + }; |
| 26 | + |
| 27 | + const interceptors = createInterceptors< |
| 28 | + Request, |
| 29 | + Response, |
| 30 | + unknown, |
| 31 | + RequestOptions |
| 32 | + >(); |
| 33 | + |
| 34 | + const request: Client["request"] = async (options) => { |
| 35 | + const opts = { |
| 36 | + ..._config, |
| 37 | + ...options, |
| 38 | + fetch: options.fetch ?? _config.fetch ?? globalThis.fetch, |
| 39 | + headers: mergeHeaders(_config.headers, options.headers), |
| 40 | + }; |
| 41 | + |
| 42 | + if (opts.security) { |
| 43 | + await setAuthParams({ |
| 44 | + ...opts, |
| 45 | + security: opts.security, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + if (opts.body && opts.bodySerializer) { |
| 50 | + opts.body = opts.bodySerializer(opts.body); |
| 51 | + } |
| 52 | + |
| 53 | + // remove Content-Type header if body is empty to avoid sending invalid requests |
| 54 | + if (opts.body === undefined || opts.body === "") { |
| 55 | + opts.headers.delete("Content-Type"); |
| 56 | + } |
| 57 | + |
| 58 | + const url = buildUrl(opts); |
| 59 | + const requestInit: ReqInit = { |
| 60 | + redirect: "follow", |
| 61 | + ...opts, |
| 62 | + }; |
| 63 | + |
| 64 | + let request = new Request(url, requestInit); |
| 65 | + |
| 66 | + for (const fn of interceptors.request._fns) { |
| 67 | + if (fn) { |
| 68 | + request = await fn(request, opts); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // fetch must be assigned here, otherwise it would throw the error: |
| 73 | + // TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation |
| 74 | + const _fetch = opts.fetch!; |
| 75 | + let response = await _fetch(request); |
| 76 | + |
| 77 | + for (const fn of interceptors.response._fns) { |
| 78 | + if (fn) { |
| 79 | + response = await fn(response, request, opts); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + const result = { |
| 84 | + request, |
| 85 | + response, |
| 86 | + }; |
| 87 | + |
| 88 | + if (response.ok) { |
| 89 | + if ( |
| 90 | + response.status === 204 || |
| 91 | + response.headers.get("Content-Length") === "0" |
| 92 | + ) { |
| 93 | + return opts.responseStyle === "data" |
| 94 | + ? {} |
| 95 | + : { |
| 96 | + data: {}, |
| 97 | + ...result, |
| 98 | + }; |
| 99 | + } |
| 100 | + |
| 101 | + const parseAs = |
| 102 | + (opts.parseAs === "auto" |
| 103 | + ? getParseAs(response.headers.get("Content-Type")) |
| 104 | + : opts.parseAs) ?? "json"; |
| 105 | + |
| 106 | + let data: any; |
| 107 | + switch (parseAs) { |
| 108 | + case "arrayBuffer": |
| 109 | + case "blob": |
| 110 | + case "formData": |
| 111 | + case "json": |
| 112 | + case "text": |
| 113 | + data = await response[parseAs](); |
| 114 | + break; |
| 115 | + case "stream": |
| 116 | + return opts.responseStyle === "data" |
| 117 | + ? response.body |
| 118 | + : { |
| 119 | + data: response.body, |
| 120 | + ...result, |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + if (parseAs === "json") { |
| 125 | + if (opts.responseValidator) { |
| 126 | + await opts.responseValidator(data); |
| 127 | + } |
| 128 | + |
| 129 | + if (opts.responseTransformer) { |
| 130 | + data = await opts.responseTransformer(data); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return opts.responseStyle === "data" |
| 135 | + ? data |
| 136 | + : { |
| 137 | + data, |
| 138 | + ...result, |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + let error = await response.text(); |
| 143 | + |
| 144 | + try { |
| 145 | + error = JSON.parse(error); |
| 146 | + } catch { |
| 147 | + // noop |
| 148 | + } |
| 149 | + |
| 150 | + let finalError = error; |
| 151 | + |
| 152 | + for (const fn of interceptors.error._fns) { |
| 153 | + if (fn) { |
| 154 | + finalError = (await fn(error, response, request, opts)) as string; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + finalError = finalError || ({} as string); |
| 159 | + |
| 160 | + if (opts.throwOnError) { |
| 161 | + throw finalError; |
| 162 | + } |
| 163 | + |
| 164 | + // TODO: we probably want to return error and improve types |
| 165 | + return opts.responseStyle === "data" |
| 166 | + ? undefined |
| 167 | + : { |
| 168 | + error: finalError, |
| 169 | + ...result, |
| 170 | + }; |
| 171 | + }; |
| 172 | + |
| 173 | + return { |
| 174 | + buildUrl, |
| 175 | + connect: (options) => request({ ...options, method: "CONNECT" }), |
| 176 | + delete: (options) => request({ ...options, method: "DELETE" }), |
| 177 | + get: (options) => request({ ...options, method: "GET" }), |
| 178 | + getConfig, |
| 179 | + head: (options) => request({ ...options, method: "HEAD" }), |
| 180 | + interceptors, |
| 181 | + options: (options) => request({ ...options, method: "OPTIONS" }), |
| 182 | + patch: (options) => request({ ...options, method: "PATCH" }), |
| 183 | + post: (options) => request({ ...options, method: "POST" }), |
| 184 | + put: (options) => request({ ...options, method: "PUT" }), |
| 185 | + request, |
| 186 | + setConfig, |
| 187 | + trace: (options) => request({ ...options, method: "TRACE" }), |
| 188 | + }; |
189 | 189 | }; |
0 commit comments