fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #3186 from hey-api/copilot/update-bundling-to-non-minified

Disable minification in packaged code

authored by

Lubos and committed by
GitHub
6869a398 ce2cbd43

+562 -13
+7
.changeset/silly-numbers-watch.md
··· 1 + --- 2 + "@hey-api/codegen-core": patch 3 + "@hey-api/openapi-ts": patch 4 + "@hey-api/vite-plugin": patch 5 + --- 6 + 7 + **build**: do not minify bundles for better code readability and debugging
+3 -3
packages/codegen-core/tsdown.config.ts
··· 1 1 import { defineConfig } from 'tsdown'; 2 2 3 - export default defineConfig((options) => ({ 3 + export default defineConfig({ 4 4 banner(ctx) { 5 5 /** 6 6 * fix dynamic require in ESM ··· 18 18 dts: true, 19 19 entry: ['src/index.ts'], 20 20 format: ['cjs', 'esm'], 21 - minify: !options.watch, 21 + minify: false, 22 22 shims: false, 23 23 sourcemap: true, 24 24 treeshake: true, 25 - })); 25 + });
+3 -3
packages/custom-client/tsdown.config.ts
··· 1 1 import { defineConfig } from 'tsdown'; 2 2 3 - export default defineConfig((options) => ({ 3 + export default defineConfig({ 4 4 clean: true, 5 5 dts: true, 6 6 entry: ['src/index.ts', 'src/plugin.ts'], 7 7 format: ['cjs', 'esm'], 8 - minify: !options.watch, 8 + minify: false, 9 9 shims: false, 10 10 sourcemap: true, 11 11 treeshake: true, 12 - })); 12 + });
+543 -1
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/client-custom/bundle/client/index.cjs
··· 1 - const e=async(e,t)=>{let n=typeof t==`function`?await t(e):t;if(n)return e.scheme===`bearer`?`Bearer ${n}`:e.scheme===`basic`?`Basic ${btoa(n)}`:n},t=(e,t,n)=>{typeof n==`string`||n instanceof Blob?e.append(t,n):e.append(t,JSON.stringify(n))},n=(e,t,n)=>{typeof n==`string`?e.append(t,n):e.append(t,JSON.stringify(n))},r={bodySerializer:e=>{let n=new FormData;return Object.entries(e).forEach(([e,r])=>{r!=null&&(Array.isArray(r)?r.forEach(r=>t(n,e,r)):t(n,e,r))}),n}},i={bodySerializer:e=>JSON.stringify(e,(e,t)=>typeof t==`bigint`?t.toString():t)},a={bodySerializer:e=>{let t=new URLSearchParams;return Object.entries(e).forEach(([e,r])=>{r!=null&&(Array.isArray(r)?r.forEach(r=>n(t,e,r)):n(t,e,r))}),t.toString()}},o=e=>{switch(e){case`label`:return`.`;case`matrix`:return`;`;case`simple`:return`,`;default:return`&`}},s=e=>{switch(e){case`form`:return`,`;case`pipeDelimited`:return`|`;case`spaceDelimited`:return`%20`;default:return`,`}},c=e=>{switch(e){case`label`:return`.`;case`matrix`:return`;`;case`simple`:return`,`;default:return`&`}},l=({allowReserved:e,explode:t,name:n,style:r,value:i})=>{if(!t){let t=(e?i:i.map(e=>encodeURIComponent(e))).join(s(r));switch(r){case`label`:return`.${t}`;case`matrix`:return`;${n}=${t}`;case`simple`:return t;default:return`${n}=${t}`}}let a=o(r),c=i.map(t=>r===`label`||r===`simple`?e?t:encodeURIComponent(t):u({allowReserved:e,name:n,value:t})).join(a);return r===`label`||r===`matrix`?a+c:c},u=({allowReserved:e,name:t,value:n})=>{if(n==null)return``;if(typeof n==`object`)throw Error("Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.");return`${t}=${e?n:encodeURIComponent(n)}`},d=({allowReserved:e,explode:t,name:n,style:r,value:i,valueOnly:a})=>{if(i instanceof Date)return a?i.toISOString():`${n}=${i.toISOString()}`;if(r!==`deepObject`&&!t){let t=[];Object.entries(i).forEach(([n,r])=>{t=[...t,n,e?r:encodeURIComponent(r)]});let a=t.join(`,`);switch(r){case`form`:return`${n}=${a}`;case`label`:return`.${a}`;case`matrix`:return`;${n}=${a}`;default:return a}}let o=c(r),s=Object.entries(i).map(([t,i])=>u({allowReserved:e,name:r===`deepObject`?`${n}[${t}]`:t,value:i})).join(o);return r===`label`||r===`matrix`?o+s:s},f=/\{[^{}]+\}/g,p=({path:e,url:t})=>{let n=t,r=t.match(f);if(r)for(let t of r){let r=!1,i=t.substring(1,t.length-1),a=`simple`;i.endsWith(`*`)&&(r=!0,i=i.substring(0,i.length-1)),i.startsWith(`.`)?(i=i.substring(1),a=`label`):i.startsWith(`;`)&&(i=i.substring(1),a=`matrix`);let o=e[i];if(o==null)continue;if(Array.isArray(o)){n=n.replace(t,l({explode:r,name:i,style:a,value:o}));continue}if(typeof o==`object`){n=n.replace(t,d({explode:r,name:i,style:a,value:o,valueOnly:!0}));continue}if(a===`matrix`){n=n.replace(t,`;${u({name:i,value:o})}`);continue}let s=encodeURIComponent(a===`label`?`.${o}`:o);n=n.replace(t,s)}return n},m=({allowReserved:e,array:t,object:n}={})=>r=>{let i=[];if(r&&typeof r==`object`)for(let a in r){let o=r[a];if(o!=null)if(Array.isArray(o)){let n=l({allowReserved:e,explode:!0,name:a,style:`form`,value:o,...t});n&&i.push(n)}else if(typeof o==`object`){let t=d({allowReserved:e,explode:!0,name:a,style:`deepObject`,value:o,...n});t&&i.push(t)}else{let t=u({allowReserved:e,name:a,value:o});t&&i.push(t)}}return i.join(`&`)},h=e=>{if(!e)return`stream`;let t=e.split(`;`)[0]?.trim();if(t){if(t.startsWith(`application/json`)||t.endsWith(`+json`))return`json`;if(t===`multipart/form-data`)return`formData`;if([`application/`,`audio/`,`image/`,`video/`].some(e=>t.startsWith(e)))return`blob`;if(t.startsWith(`text/`))return`text`}},g=(e,t)=>t?!!(e.headers.has(t)||e.query?.[t]||e.headers.get(`Cookie`)?.includes(`${t}=`)):!1,_=async({security:t,...n})=>{for(let r of t){if(g(n,r.name))continue;let t=await e(r,n.auth);if(!t)continue;let i=r.name??`Authorization`;switch(r.in){case`query`:n.query||={},n.query[i]=t;break;case`cookie`:n.headers.append(`Cookie`,`${i}=${t}`);break;case`header`:default:n.headers.set(i,t);break}}},v=e=>y({baseUrl:e.baseUrl,path:e.path,query:e.query,querySerializer:typeof e.querySerializer==`function`?e.querySerializer:m(e.querySerializer),url:e.url}),y=({baseUrl:e,path:t,query:n,querySerializer:r,url:i})=>{let a=i.startsWith(`/`)?i:`/${i}`,o=(e??``)+a;t&&(o=p({path:t,url:o}));let s=n?r(n):``;return s.startsWith(`?`)&&(s=s.substring(1)),s&&(o+=`?${s}`),o},b=(e,t)=>{let n={...e,...t};return n.baseUrl?.endsWith(`/`)&&(n.baseUrl=n.baseUrl.substring(0,n.baseUrl.length-1)),n.headers=x(e.headers,t.headers),n},x=(...e)=>{let t=new Headers;for(let n of e){if(!n||typeof n!=`object`)continue;let e=n instanceof Headers?n.entries():Object.entries(n);for(let[n,r]of e)if(r===null)t.delete(n);else if(Array.isArray(r))for(let e of r)t.append(n,e);else r!==void 0&&t.set(n,typeof r==`object`?JSON.stringify(r):r)}return t};var S=class{fns=[];clear(){this.fns=[]}eject(e){let t=this.getInterceptorIndex(e);this.fns[t]&&(this.fns[t]=null)}exists(e){let t=this.getInterceptorIndex(e);return!!this.fns[t]}getInterceptorIndex(e){return typeof e==`number`?this.fns[e]?e:-1:this.fns.indexOf(e)}update(e,t){let n=this.getInterceptorIndex(e);return this.fns[n]?(this.fns[n]=t,e):!1}use(e){return this.fns.push(e),this.fns.length-1}};const C=()=>({error:new S,request:new S,response:new S}),w=m({allowReserved:!1,array:{explode:!0,style:`form`},object:{explode:!0,style:`deepObject`}}),T={"Content-Type":`application/json`},E=(e={})=>({...i,headers:T,parseAs:`auto`,querySerializer:w,...e}),D=(e={})=>{let t=b(E(),e),n=()=>({...t}),r=e=>(t=b(t,e),n()),i=C(),a=async e=>{let n={...t,...e,fetch:e.fetch??t.fetch??globalThis.fetch,headers:x(t.headers,e.headers)};n.security&&await _({...n,security:n.security}),n.requestValidator&&await n.requestValidator(n),n.body&&n.bodySerializer&&(n.body=n.bodySerializer(n.body)),(n.body===void 0||n.body===``)&&n.headers.delete(`Content-Type`);let r=v(n),a={redirect:`follow`,...n},o=new Request(r,a);for(let e of i.request.fns)e&&(o=await e(o,n));let s=n.fetch,c=await s(o);for(let e of i.response.fns)e&&(c=await e(c,o,n));let l={request:o,response:c};if(c.ok){if(c.status===204||c.headers.get(`Content-Length`)===`0`)return{data:{},...l};let e=(n.parseAs===`auto`?h(c.headers.get(`Content-Type`)):n.parseAs)??`json`,t;switch(e){case`arrayBuffer`:case`blob`:case`formData`:case`json`:case`text`:t=await c[e]();break;case`stream`:return{data:c.body,...l}}return e===`json`&&(n.responseValidator&&await n.responseValidator(t),n.responseTransformer&&(t=await n.responseTransformer(t))),{data:t,...l}}let u=await c.text();try{u=JSON.parse(u)}catch{}let d=u;for(let e of i.error.fns)e&&(d=await e(u,c,o,n));if(d||={},n.throwOnError)throw d;return{error:d,...l}};return{buildUrl:v,connect:e=>a({...e,method:`CONNECT`}),delete:e=>a({...e,method:`DELETE`}),get:e=>a({...e,method:`GET`}),getConfig:n,head:e=>a({...e,method:`HEAD`}),interceptors:i,options:e=>a({...e,method:`OPTIONS`}),patch:e=>a({...e,method:`PATCH`}),post:e=>a({...e,method:`POST`}),put:e=>a({...e,method:`PUT`}),request:a,setConfig:r,trace:e=>a({...e,method:`TRACE`})}},O=Object.entries({$body_:`body`,$headers_:`headers`,$path_:`path`,$query_:`query`}),k=(e,t)=>{t||=new Map;for(let n of e)`in`in n?n.key&&t.set(n.key,{in:n.in,map:n.map}):n.args&&k(n.args,t);return t},A=e=>{for(let[t,n]of Object.entries(e))n&&typeof n==`object`&&!Object.keys(n).length&&delete e[t]},j=(e,t)=>{let n={body:{},headers:{},path:{},query:{}},r=k(t),i;for(let[a,o]of e.entries())if(t[a]&&(i=t[a]),i)if(`in`in i)if(i.key){let e=r.get(i.key),t=e.map||i.key;n[e.in][t]=o}else n.body=o;else for(let[e,t]of Object.entries(o??{})){let a=r.get(e);if(a){let r=a.map||e;n[a.in][r]=t}else{let r=O.find(([t])=>e.startsWith(t));if(r){let[i,a]=r;n[a][e.slice(i.length)]=t}else for(let[r,a]of Object.entries(i.allowExtra??{}))if(a){n[r][e]=t;break}}}return A(n),n};exports.buildClientParams=j,exports.createClient=D,exports.createConfig=E,exports.formDataBodySerializer=r,exports.jsonBodySerializer=i,exports.urlSearchParamsBodySerializer=a; 1 + 2 + //#region src/core/auth.ts 3 + const getAuthToken = async (auth, callback) => { 4 + const token = typeof callback === "function" ? await callback(auth) : callback; 5 + if (!token) return; 6 + if (auth.scheme === "bearer") return `Bearer ${token}`; 7 + if (auth.scheme === "basic") return `Basic ${btoa(token)}`; 8 + return token; 9 + }; 10 + 11 + //#endregion 12 + //#region src/core/bodySerializer.ts 13 + const serializeFormDataPair = (data, key, value) => { 14 + if (typeof value === "string" || value instanceof Blob) data.append(key, value); 15 + else data.append(key, JSON.stringify(value)); 16 + }; 17 + const serializeUrlSearchParamsPair = (data, key, value) => { 18 + if (typeof value === "string") data.append(key, value); 19 + else data.append(key, JSON.stringify(value)); 20 + }; 21 + const formDataBodySerializer = { bodySerializer: (body) => { 22 + const data = new FormData(); 23 + Object.entries(body).forEach(([key, value]) => { 24 + if (value === void 0 || value === null) return; 25 + if (Array.isArray(value)) value.forEach((v) => serializeFormDataPair(data, key, v)); 26 + else serializeFormDataPair(data, key, value); 27 + }); 28 + return data; 29 + } }; 30 + const jsonBodySerializer = { bodySerializer: (body) => JSON.stringify(body, (_key, value) => typeof value === "bigint" ? value.toString() : value) }; 31 + const urlSearchParamsBodySerializer = { bodySerializer: (body) => { 32 + const data = new URLSearchParams(); 33 + Object.entries(body).forEach(([key, value]) => { 34 + if (value === void 0 || value === null) return; 35 + if (Array.isArray(value)) value.forEach((v) => serializeUrlSearchParamsPair(data, key, v)); 36 + else serializeUrlSearchParamsPair(data, key, value); 37 + }); 38 + return data.toString(); 39 + } }; 40 + 41 + //#endregion 42 + //#region src/core/pathSerializer.ts 43 + const separatorArrayExplode = (style) => { 44 + switch (style) { 45 + case "label": return "."; 46 + case "matrix": return ";"; 47 + case "simple": return ","; 48 + default: return "&"; 49 + } 50 + }; 51 + const separatorArrayNoExplode = (style) => { 52 + switch (style) { 53 + case "form": return ","; 54 + case "pipeDelimited": return "|"; 55 + case "spaceDelimited": return "%20"; 56 + default: return ","; 57 + } 58 + }; 59 + const separatorObjectExplode = (style) => { 60 + switch (style) { 61 + case "label": return "."; 62 + case "matrix": return ";"; 63 + case "simple": return ","; 64 + default: return "&"; 65 + } 66 + }; 67 + const serializeArrayParam = ({ allowReserved, explode, name, style, value }) => { 68 + if (!explode) { 69 + const joinedValues$1 = (allowReserved ? value : value.map((v) => encodeURIComponent(v))).join(separatorArrayNoExplode(style)); 70 + switch (style) { 71 + case "label": return `.${joinedValues$1}`; 72 + case "matrix": return `;${name}=${joinedValues$1}`; 73 + case "simple": return joinedValues$1; 74 + default: return `${name}=${joinedValues$1}`; 75 + } 76 + } 77 + const separator = separatorArrayExplode(style); 78 + const joinedValues = value.map((v) => { 79 + if (style === "label" || style === "simple") return allowReserved ? v : encodeURIComponent(v); 80 + return serializePrimitiveParam({ 81 + allowReserved, 82 + name, 83 + value: v 84 + }); 85 + }).join(separator); 86 + return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues; 87 + }; 88 + const serializePrimitiveParam = ({ allowReserved, name, value }) => { 89 + if (value === void 0 || value === null) return ""; 90 + if (typeof value === "object") throw new Error("Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these."); 91 + return `${name}=${allowReserved ? value : encodeURIComponent(value)}`; 92 + }; 93 + const serializeObjectParam = ({ allowReserved, explode, name, style, value, valueOnly }) => { 94 + if (value instanceof Date) return valueOnly ? value.toISOString() : `${name}=${value.toISOString()}`; 95 + if (style !== "deepObject" && !explode) { 96 + let values = []; 97 + Object.entries(value).forEach(([key, v]) => { 98 + values = [ 99 + ...values, 100 + key, 101 + allowReserved ? v : encodeURIComponent(v) 102 + ]; 103 + }); 104 + const joinedValues$1 = values.join(","); 105 + switch (style) { 106 + case "form": return `${name}=${joinedValues$1}`; 107 + case "label": return `.${joinedValues$1}`; 108 + case "matrix": return `;${name}=${joinedValues$1}`; 109 + default: return joinedValues$1; 110 + } 111 + } 112 + const separator = separatorObjectExplode(style); 113 + const joinedValues = Object.entries(value).map(([key, v]) => serializePrimitiveParam({ 114 + allowReserved, 115 + name: style === "deepObject" ? `${name}[${key}]` : key, 116 + value: v 117 + })).join(separator); 118 + return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues; 119 + }; 120 + 121 + //#endregion 122 + //#region src/utils.ts 123 + const PATH_PARAM_RE = /\{[^{}]+\}/g; 124 + const defaultPathSerializer = ({ path, url: _url }) => { 125 + let url = _url; 126 + const matches = _url.match(PATH_PARAM_RE); 127 + if (matches) for (const match of matches) { 128 + let explode = false; 129 + let name = match.substring(1, match.length - 1); 130 + let style = "simple"; 131 + if (name.endsWith("*")) { 132 + explode = true; 133 + name = name.substring(0, name.length - 1); 134 + } 135 + if (name.startsWith(".")) { 136 + name = name.substring(1); 137 + style = "label"; 138 + } else if (name.startsWith(";")) { 139 + name = name.substring(1); 140 + style = "matrix"; 141 + } 142 + const value = path[name]; 143 + if (value === void 0 || value === null) continue; 144 + if (Array.isArray(value)) { 145 + url = url.replace(match, serializeArrayParam({ 146 + explode, 147 + name, 148 + style, 149 + value 150 + })); 151 + continue; 152 + } 153 + if (typeof value === "object") { 154 + url = url.replace(match, serializeObjectParam({ 155 + explode, 156 + name, 157 + style, 158 + value, 159 + valueOnly: true 160 + })); 161 + continue; 162 + } 163 + if (style === "matrix") { 164 + url = url.replace(match, `;${serializePrimitiveParam({ 165 + name, 166 + value 167 + })}`); 168 + continue; 169 + } 170 + const replaceValue = encodeURIComponent(style === "label" ? `.${value}` : value); 171 + url = url.replace(match, replaceValue); 172 + } 173 + return url; 174 + }; 175 + const createQuerySerializer = ({ allowReserved, array, object } = {}) => { 176 + const querySerializer = (queryParams) => { 177 + const search = []; 178 + if (queryParams && typeof queryParams === "object") for (const name in queryParams) { 179 + const value = queryParams[name]; 180 + if (value === void 0 || value === null) continue; 181 + if (Array.isArray(value)) { 182 + const serializedArray = serializeArrayParam({ 183 + allowReserved, 184 + explode: true, 185 + name, 186 + style: "form", 187 + value, 188 + ...array 189 + }); 190 + if (serializedArray) search.push(serializedArray); 191 + } else if (typeof value === "object") { 192 + const serializedObject = serializeObjectParam({ 193 + allowReserved, 194 + explode: true, 195 + name, 196 + style: "deepObject", 197 + value, 198 + ...object 199 + }); 200 + if (serializedObject) search.push(serializedObject); 201 + } else { 202 + const serializedPrimitive = serializePrimitiveParam({ 203 + allowReserved, 204 + name, 205 + value 206 + }); 207 + if (serializedPrimitive) search.push(serializedPrimitive); 208 + } 209 + } 210 + return search.join("&"); 211 + }; 212 + return querySerializer; 213 + }; 214 + /** 215 + * Infers parseAs value from provided Content-Type header. 216 + */ 217 + const getParseAs = (contentType) => { 218 + if (!contentType) return "stream"; 219 + const cleanContent = contentType.split(";")[0]?.trim(); 220 + if (!cleanContent) return; 221 + if (cleanContent.startsWith("application/json") || cleanContent.endsWith("+json")) return "json"; 222 + if (cleanContent === "multipart/form-data") return "formData"; 223 + if ([ 224 + "application/", 225 + "audio/", 226 + "image/", 227 + "video/" 228 + ].some((type) => cleanContent.startsWith(type))) return "blob"; 229 + if (cleanContent.startsWith("text/")) return "text"; 230 + }; 231 + const checkForExistence = (options, name) => { 232 + if (!name) return false; 233 + if (options.headers.has(name) || options.query?.[name] || options.headers.get("Cookie")?.includes(`${name}=`)) return true; 234 + return false; 235 + }; 236 + const setAuthParams = async ({ security, ...options }) => { 237 + for (const auth of security) { 238 + if (checkForExistence(options, auth.name)) continue; 239 + const token = await getAuthToken(auth, options.auth); 240 + if (!token) continue; 241 + const name = auth.name ?? "Authorization"; 242 + switch (auth.in) { 243 + case "query": 244 + if (!options.query) options.query = {}; 245 + options.query[name] = token; 246 + break; 247 + case "cookie": 248 + options.headers.append("Cookie", `${name}=${token}`); 249 + break; 250 + case "header": 251 + default: 252 + options.headers.set(name, token); 253 + break; 254 + } 255 + } 256 + }; 257 + const buildUrl = (options) => { 258 + return getUrl({ 259 + baseUrl: options.baseUrl, 260 + path: options.path, 261 + query: options.query, 262 + querySerializer: typeof options.querySerializer === "function" ? options.querySerializer : createQuerySerializer(options.querySerializer), 263 + url: options.url 264 + }); 265 + }; 266 + const getUrl = ({ baseUrl, path, query, querySerializer, url: _url }) => { 267 + const pathUrl = _url.startsWith("/") ? _url : `/${_url}`; 268 + let url = (baseUrl ?? "") + pathUrl; 269 + if (path) url = defaultPathSerializer({ 270 + path, 271 + url 272 + }); 273 + let search = query ? querySerializer(query) : ""; 274 + if (search.startsWith("?")) search = search.substring(1); 275 + if (search) url += `?${search}`; 276 + return url; 277 + }; 278 + const mergeConfigs = (a, b) => { 279 + const config = { 280 + ...a, 281 + ...b 282 + }; 283 + if (config.baseUrl?.endsWith("/")) config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1); 284 + config.headers = mergeHeaders(a.headers, b.headers); 285 + return config; 286 + }; 287 + const mergeHeaders = (...headers) => { 288 + const mergedHeaders = new Headers(); 289 + for (const header of headers) { 290 + if (!header || typeof header !== "object") continue; 291 + const iterator = header instanceof Headers ? header.entries() : Object.entries(header); 292 + for (const [key, value] of iterator) if (value === null) mergedHeaders.delete(key); 293 + else if (Array.isArray(value)) for (const v of value) mergedHeaders.append(key, v); 294 + else if (value !== void 0) mergedHeaders.set(key, typeof value === "object" ? JSON.stringify(value) : value); 295 + } 296 + return mergedHeaders; 297 + }; 298 + var Interceptors = class { 299 + fns = []; 300 + clear() { 301 + this.fns = []; 302 + } 303 + eject(id) { 304 + const index = this.getInterceptorIndex(id); 305 + if (this.fns[index]) this.fns[index] = null; 306 + } 307 + exists(id) { 308 + const index = this.getInterceptorIndex(id); 309 + return Boolean(this.fns[index]); 310 + } 311 + getInterceptorIndex(id) { 312 + if (typeof id === "number") return this.fns[id] ? id : -1; 313 + return this.fns.indexOf(id); 314 + } 315 + update(id, fn) { 316 + const index = this.getInterceptorIndex(id); 317 + if (this.fns[index]) { 318 + this.fns[index] = fn; 319 + return id; 320 + } 321 + return false; 322 + } 323 + use(fn) { 324 + this.fns.push(fn); 325 + return this.fns.length - 1; 326 + } 327 + }; 328 + const createInterceptors = () => ({ 329 + error: new Interceptors(), 330 + request: new Interceptors(), 331 + response: new Interceptors() 332 + }); 333 + const defaultQuerySerializer = createQuerySerializer({ 334 + allowReserved: false, 335 + array: { 336 + explode: true, 337 + style: "form" 338 + }, 339 + object: { 340 + explode: true, 341 + style: "deepObject" 342 + } 343 + }); 344 + const defaultHeaders = { "Content-Type": "application/json" }; 345 + const createConfig = (override = {}) => ({ 346 + ...jsonBodySerializer, 347 + headers: defaultHeaders, 348 + parseAs: "auto", 349 + querySerializer: defaultQuerySerializer, 350 + ...override 351 + }); 352 + 353 + //#endregion 354 + //#region src/client.ts 355 + const createClient = (config = {}) => { 356 + let _config = mergeConfigs(createConfig(), config); 357 + const getConfig = () => ({ ..._config }); 358 + const setConfig = (config$1) => { 359 + _config = mergeConfigs(_config, config$1); 360 + return getConfig(); 361 + }; 362 + const interceptors = createInterceptors(); 363 + const request = async (options) => { 364 + const opts = { 365 + ..._config, 366 + ...options, 367 + fetch: options.fetch ?? _config.fetch ?? globalThis.fetch, 368 + headers: mergeHeaders(_config.headers, options.headers) 369 + }; 370 + if (opts.security) await setAuthParams({ 371 + ...opts, 372 + security: opts.security 373 + }); 374 + if (opts.requestValidator) await opts.requestValidator(opts); 375 + if (opts.body && opts.bodySerializer) opts.body = opts.bodySerializer(opts.body); 376 + if (opts.body === void 0 || opts.body === "") opts.headers.delete("Content-Type"); 377 + const url = buildUrl(opts); 378 + const requestInit = { 379 + redirect: "follow", 380 + ...opts 381 + }; 382 + let request$1 = new Request(url, requestInit); 383 + for (const fn of interceptors.request.fns) if (fn) request$1 = await fn(request$1, opts); 384 + const _fetch = opts.fetch; 385 + let response = await _fetch(request$1); 386 + for (const fn of interceptors.response.fns) if (fn) response = await fn(response, request$1, opts); 387 + const result = { 388 + request: request$1, 389 + response 390 + }; 391 + if (response.ok) { 392 + if (response.status === 204 || response.headers.get("Content-Length") === "0") return { 393 + data: {}, 394 + ...result 395 + }; 396 + const parseAs = (opts.parseAs === "auto" ? getParseAs(response.headers.get("Content-Type")) : opts.parseAs) ?? "json"; 397 + let data; 398 + switch (parseAs) { 399 + case "arrayBuffer": 400 + case "blob": 401 + case "formData": 402 + case "json": 403 + case "text": 404 + data = await response[parseAs](); 405 + break; 406 + case "stream": return { 407 + data: response.body, 408 + ...result 409 + }; 410 + } 411 + if (parseAs === "json") { 412 + if (opts.responseValidator) await opts.responseValidator(data); 413 + if (opts.responseTransformer) data = await opts.responseTransformer(data); 414 + } 415 + return { 416 + data, 417 + ...result 418 + }; 419 + } 420 + let error = await response.text(); 421 + try { 422 + error = JSON.parse(error); 423 + } catch {} 424 + let finalError = error; 425 + for (const fn of interceptors.error.fns) if (fn) finalError = await fn(error, response, request$1, opts); 426 + finalError = finalError || {}; 427 + if (opts.throwOnError) throw finalError; 428 + return { 429 + error: finalError, 430 + ...result 431 + }; 432 + }; 433 + return { 434 + buildUrl, 435 + connect: (options) => request({ 436 + ...options, 437 + method: "CONNECT" 438 + }), 439 + delete: (options) => request({ 440 + ...options, 441 + method: "DELETE" 442 + }), 443 + get: (options) => request({ 444 + ...options, 445 + method: "GET" 446 + }), 447 + getConfig, 448 + head: (options) => request({ 449 + ...options, 450 + method: "HEAD" 451 + }), 452 + interceptors, 453 + options: (options) => request({ 454 + ...options, 455 + method: "OPTIONS" 456 + }), 457 + patch: (options) => request({ 458 + ...options, 459 + method: "PATCH" 460 + }), 461 + post: (options) => request({ 462 + ...options, 463 + method: "POST" 464 + }), 465 + put: (options) => request({ 466 + ...options, 467 + method: "PUT" 468 + }), 469 + request, 470 + setConfig, 471 + trace: (options) => request({ 472 + ...options, 473 + method: "TRACE" 474 + }) 475 + }; 476 + }; 477 + 478 + //#endregion 479 + //#region src/core/params.ts 480 + const extraPrefixes = Object.entries({ 481 + $body_: "body", 482 + $headers_: "headers", 483 + $path_: "path", 484 + $query_: "query" 485 + }); 486 + const buildKeyMap = (fields, map) => { 487 + if (!map) map = /* @__PURE__ */ new Map(); 488 + for (const config of fields) if ("in" in config) { 489 + if (config.key) map.set(config.key, { 490 + in: config.in, 491 + map: config.map 492 + }); 493 + } else if (config.args) buildKeyMap(config.args, map); 494 + return map; 495 + }; 496 + const stripEmptySlots = (params) => { 497 + for (const [slot, value] of Object.entries(params)) if (value && typeof value === "object" && !Object.keys(value).length) delete params[slot]; 498 + }; 499 + const buildClientParams = (args, fields) => { 500 + const params = { 501 + body: {}, 502 + headers: {}, 503 + path: {}, 504 + query: {} 505 + }; 506 + const map = buildKeyMap(fields); 507 + let config; 508 + for (const [index, arg] of args.entries()) { 509 + if (fields[index]) config = fields[index]; 510 + if (!config) continue; 511 + if ("in" in config) if (config.key) { 512 + const field = map.get(config.key); 513 + const name = field.map || config.key; 514 + params[field.in][name] = arg; 515 + } else params.body = arg; 516 + else for (const [key, value] of Object.entries(arg ?? {})) { 517 + const field = map.get(key); 518 + if (field) { 519 + const name = field.map || key; 520 + params[field.in][name] = value; 521 + } else { 522 + const extra = extraPrefixes.find(([prefix]) => key.startsWith(prefix)); 523 + if (extra) { 524 + const [prefix, slot] = extra; 525 + params[slot][key.slice(prefix.length)] = value; 526 + } else for (const [slot, allowed] of Object.entries(config.allowExtra ?? {})) if (allowed) { 527 + params[slot][key] = value; 528 + break; 529 + } 530 + } 531 + } 532 + } 533 + stripEmptySlots(params); 534 + return params; 535 + }; 536 + 537 + //#endregion 538 + exports.buildClientParams = buildClientParams; 539 + exports.createClient = createClient; 540 + exports.createConfig = createConfig; 541 + exports.formDataBodySerializer = formDataBodySerializer; 542 + exports.jsonBodySerializer = jsonBodySerializer; 543 + exports.urlSearchParamsBodySerializer = urlSearchParamsBodySerializer; 2 544 //# sourceMappingURL=index.cjs.map
+3 -3
packages/openapi-ts/tsdown.config.ts
··· 13 13 fs.writeFileSync(filePath, content, 'utf8'); 14 14 }; 15 15 16 - export default defineConfig((options) => ({ 16 + export default defineConfig({ 17 17 banner(ctx) { 18 18 /** 19 19 * fix dynamic require in ESM ··· 31 31 dts: true, 32 32 entry: ['./src/{index,internal,run}.ts'], 33 33 format: ['cjs', 'esm'], 34 - minify: !options.watch, 34 + minify: false, 35 35 onSuccess: async () => { 36 36 // Copy client files to dist folder for runtime access 37 37 const pluginNames = [ ··· 74 74 shims: false, 75 75 sourcemap: true, 76 76 treeshake: true, 77 - })); 77 + });
+3 -3
packages/vite-plugin/tsdown.config.ts
··· 1 1 import { defineConfig } from 'tsdown'; 2 2 3 - export default defineConfig((options) => ({ 3 + export default defineConfig({ 4 4 banner(ctx) { 5 5 /** 6 6 * fix dynamic require in ESM ··· 16 16 dts: true, 17 17 entry: ['src/index.ts'], 18 18 format: ['cjs', 'esm'], 19 - minify: !options.watch, 19 + minify: false, 20 20 shims: false, 21 21 sourcemap: true, 22 22 treeshake: true, 23 - })); 23 + });