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