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