Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: clients base URL #1661

Merged
merged 6 commits into from
Feb 2, 2025
Merged

feat: clients base URL #1661

merged 6 commits into from
Feb 2, 2025

Conversation

mrlubos
Copy link
Member

@mrlubos mrlubos commented Feb 1, 2025

Closes #1549

  • option to remove (string & {}) (strictBaseUrl?)
  • option to set baseUrl from plugins (false disables the default one, support index number to pick from a list)
  • don't forget to add snapshot tests

Copy link

stackblitz bot commented Feb 1, 2025

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

changeset-bot bot commented Feb 1, 2025

🦋 Changeset detected

Latest commit: ccefe43

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 15 packages
Name Type
@hey-api/openapi-ts Minor
@hey-api/client-axios Minor
@hey-api/client-fetch Minor
@hey-api/client-next Minor
@hey-api/client-nuxt Minor
@example/openapi-ts-axios Patch
@example/openapi-ts-fastify Patch
@example/openapi-ts-fetch Patch
@example/openapi-ts-next Patch
@example/openapi-ts-nuxt Patch
@example/openapi-ts-sample Patch
@example/openapi-ts-tanstack-angular-query-experimental Patch
@example/openapi-ts-tanstack-react-query Patch
@example/openapi-ts-tanstack-svelte-query Patch
@example/openapi-ts-tanstack-vue-query Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Feb 1, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hey-api-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 2, 2025 10:22am

Copy link

codecov bot commented Feb 1, 2025

Codecov Report

Attention: Patch coverage is 79.54545% with 108 lines in your changes missing coverage. Please review.

Project coverage is 57.13%. Comparing base (c928e80) to head (ccefe43).
Report is 21 commits behind head on main.

Files with missing lines Patch % Lines
packages/openapi-ts/src/compiler/types.ts 7.14% 26 Missing ⚠️
...napi-ts/src/plugins/@hey-api/client-core/client.ts 76.85% 25 Missing ⚠️
...ages/openapi-ts/src/openApi/3.0.x/parser/server.ts 34.61% 17 Missing ⚠️
...ages/openapi-ts/src/openApi/3.1.x/parser/server.ts 34.61% 17 Missing ⚠️
...s/src/plugins/@hey-api/typescript/clientOptions.ts 91.35% 7 Missing ⚠️
.../src/plugins/@tanstack/query-core/plugin-legacy.ts 57.14% 3 Missing ⚠️
...pi-ts/src/plugins/@tanstack/query-core/queryKey.ts 25.00% 3 Missing ⚠️
...ages/openapi-ts/src/openApi/2.0.x/parser/server.ts 94.44% 2 Missing ⚠️
...s/openapi-ts/src/openApi/3.0.x/parser/operation.ts 50.00% 2 Missing ⚠️
...s/openapi-ts/src/openApi/3.1.x/parser/operation.ts 50.00% 2 Missing ⚠️
... and 2 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1661      +/-   ##
==========================================
+ Coverage   56.42%   57.13%   +0.70%     
==========================================
  Files         175      184       +9     
  Lines       24966    25571     +605     
  Branches     1847     1909      +62     
==========================================
+ Hits        14088    14609     +521     
- Misses      10866    10953      +87     
+ Partials       12        9       -3     
Flag Coverage Δ
unittests 57.13% <79.54%> (+0.70%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

pkg-pr-new bot commented Feb 1, 2025

Open in Stackblitz

@hey-api/client-axios

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/client-axios@1661

@hey-api/client-next

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/client-next@1661

@hey-api/client-fetch

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/client-fetch@1661

@hey-api/client-nuxt

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/client-nuxt@1661

@hey-api/openapi-ts

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/openapi-ts@1661

commit: ccefe43

@@ -1,2 +1,2 @@
'use strict';var app=require('nuxt/app'),vue=require('vue');var E=async(r,t)=>{let e=typeof t=="function"?await t(r):t;if(e)return r.scheme==="bearer"?`Bearer ${e}`:r.scheme==="basic"?`Basic ${btoa(e)}`:e},$=(r,t,e)=>{typeof e=="string"||e instanceof Blob?r.append(t,e):r.append(t,JSON.stringify(e));},k=(r,t,e)=>{typeof e=="string"?r.append(t,e):r.append(t,JSON.stringify(e));},F={bodySerializer:r=>{let t=new FormData;return Object.entries(r).forEach(([e,i])=>{i!=null&&(Array.isArray(i)?i.forEach(o=>$(t,e,o)):$(t,e,i));}),t}},C={bodySerializer:r=>JSON.stringify(r,(t,e)=>typeof e=="bigint"?e.toString():e)},N={bodySerializer:r=>{let t=new URLSearchParams;return Object.entries(r).forEach(([e,i])=>{i!=null&&(Array.isArray(i)?i.forEach(o=>k(t,e,o)):k(t,e,i));}),t}},I=r=>{switch(r){case "label":return ".";case "matrix":return ";";case "simple":return ",";default:return "&"}},Q=r=>{switch(r){case "form":return ",";case "pipeDelimited":return "|";case "spaceDelimited":return "%20";default:return ","}},V=r=>{switch(r){case "label":return ".";case "matrix":return ";";case "simple":return ",";default:return "&"}},S=({allowReserved:r,explode:t,name:e,style:i,value:o})=>{if(!t){let n=(r?o:o.map(u=>encodeURIComponent(u))).join(Q(i));switch(i){case "label":return `.${n}`;case "matrix":return `;${e}=${n}`;case "simple":return n;default:return `${e}=${n}`}}let a=I(i),s=o.map(n=>i==="label"||i==="simple"?r?n:encodeURIComponent(n):p({allowReserved:r,name:e,value:n})).join(a);return i==="label"||i==="matrix"?a+s:s},p=({allowReserved:r,name:t,value:e})=>{if(e==null)return "";if(typeof e=="object")throw new Error("Deeply-nested arrays/objects aren\u2019t supported. Provide your own `querySerializer()` to handle these.");return `${t}=${r?e:encodeURIComponent(e)}`},z=({allowReserved:r,explode:t,name:e,style:i,value:o})=>{if(o instanceof Date)return `${e}=${o.toISOString()}`;if(i!=="deepObject"&&!t){let n=[];Object.entries(o).forEach(([l,d])=>{n=[...n,l,r?d:encodeURIComponent(d)];});let u=n.join(",");switch(i){case "form":return `${e}=${u}`;case "label":return `.${u}`;case "matrix":return `;${e}=${u}`;default:return u}}let a=V(i),s=Object.entries(o).map(([n,u])=>p({allowReserved:r,name:i==="deepObject"?`${e}[${n}]`:n,value:u})).join(a);return i==="label"||i==="matrix"?a+s:s};var W=/\{[^{}]+\}/g,J=({path:r,url:t})=>{let e=t,i=t.match(W);if(i)for(let o of i){let a=false,s=o.substring(1,o.length-1),n="simple";s.endsWith("*")&&(a=true,s=s.substring(0,s.length-1)),s.startsWith(".")?(s=s.substring(1),n="label"):s.startsWith(";")&&(s=s.substring(1),n="matrix");let u=vue.toValue(vue.toValue(r)[s]);if(u==null)continue;if(Array.isArray(u)){e=e.replace(o,S({explode:a,name:s,style:n,value:u}));continue}if(typeof u=="object"){e=e.replace(o,z({explode:a,name:s,style:n,value:u}));continue}if(n==="matrix"){e=e.replace(o,`;${p({name:s,value:u})}`);continue}let l=encodeURIComponent(n==="label"?`.${u}`:u);e=e.replace(o,l);}return e},v=({allowReserved:r,array:t,object:e}={})=>o=>{let a=[],s=vue.toValue(o);if(s&&typeof s=="object")for(let n in s){let u=vue.toValue(s[n]);if(u!=null){if(Array.isArray(u)){a=[...a,S({allowReserved:r,explode:true,name:n,style:"form",value:u,...t})];continue}if(typeof u=="object"){a=[...a,z({allowReserved:r,explode:true,name:n,style:"deepObject",value:u,...e})];continue}a=[...a,p({allowReserved:r,name:n,value:u})];}}return a.join("&")},B=async({security:r,...t})=>{for(let e of r){let i=await E(e,t.auth);if(!i)continue;let o=e.name??"Authorization";switch(e.in){case "query":t.query||(t.query={}),vue.toValue(t.query)[o]=i;break;case "header":default:t.headers.set(o,i);break}return}},h=r=>M({baseUrl:r.baseURL??"",path:r.path,query:r.query,querySerializer:typeof r.querySerializer=="function"?r.querySerializer:v(r.querySerializer),url:r.url}),M=({baseUrl:r,path:t,query:e,querySerializer:i,url:o})=>{let a=o.startsWith("/")?o:`/${o}`,s=r+a;t&&(s=J({path:t,url:s}));let n=e?i(e):"";return n.startsWith("?")&&(n=n.substring(1)),n&&(s+=`?${n}`),s},T=(r,t)=>{let e={...r,...t};return e.baseURL?.endsWith("/")&&(e.baseURL=e.baseURL.substring(0,e.baseURL.length-1)),e.headers=w(r.headers,t.headers),e},w=(...r)=>{let t=new Headers;for(let e of r){if(!e||typeof e!="object")continue;let i=e;vue.isRef(i)&&(i=vue.unref(i));let o=i instanceof Headers?i.entries():Object.entries(i);for(let[a,s]of o)if(s===null)t.delete(a);else if(Array.isArray(s))for(let n of s)t.append(a,y(n));else if(s!==undefined){let n=y(s);t.set(a,typeof n=="object"?JSON.stringify(n):n);}}return t},q=(...r)=>r.reduce((t,e)=>{if(typeof e=="function")t.push(e);else if(Array.isArray(e))return t.concat(e);return t},[]),_=v({allowReserved:false,array:{explode:true,style:"form"},object:{explode:true,style:"deepObject"}}),K={"Content-Type":"application/json"},P=(r={})=>({...C,baseURL:"",headers:K,querySerializer:_,...r}),y=r=>{if(r===null||typeof r!="object"||r instanceof Headers)return vue.isRef(r)?vue.unref(r):r;if(Array.isArray(r))return r.map(e=>y(e));if(vue.isRef(r))return y(vue.unref(r));let t={};for(let e in r)t[e]=y(r[e]);return t},g=r=>r.body&&r.bodySerializer?r.bodySerializer(r.body):r.body,O=(r,t)=>{let e=y(r);return e.body=g(e),t(h(r),e)};var re=(r={})=>{let t=T(P(),r),e=()=>({...t}),i=a=>(t=T(t,a),e()),o=({asyncDataOptions:a,composable:s,key:n,...u})=>{let l={...t,...u,$fetch:u.$fetch??t.$fetch??$fetch,headers:w(t.headers,u.headers),onRequest:q(t.onRequest,u.onRequest),onResponse:q(t.onResponse,u.onResponse)},{responseTransformer:d,responseValidator:R,security:j}=l;j&&(l.onRequest=[async({options:c})=>{await B({auth:l.auth,headers:c.headers,query:c.query,security:j});},...l.onRequest]),(d||R)&&(l.onResponse=[...l.onResponse,async({options:c,response:f})=>{c.responseType&&c.responseType!=="json"||f.ok&&(R&&await R(f._data),d&&(f._data=await d(f._data)));}]),l.body||l.headers.delete("Content-Type");let U=l.$fetch;if(s==="$fetch")return O(l,U);if(s==="useFetch"||s==="useLazyFetch"){let c=vue.reactive({body:l.body,bodySerializer:l.bodySerializer}),f=vue.ref(g(l));return l.body=f,vue.watch(c,D=>{f.value=g(D);}),s==="useLazyFetch"?app.useLazyFetch(()=>h(l),l):app.useFetch(()=>h(l),l)}let b=()=>O(l,U);if(s==="useAsyncData")return n?app.useAsyncData(n,b,a):app.useAsyncData(b,a);if(s==="useLazyAsyncData")return n?app.useLazyAsyncData(n,b,a):app.useLazyAsyncData(b,a)};return {buildUrl:h,connect:a=>o({...a,method:"CONNECT"}),delete:a=>o({...a,method:"DELETE"}),get:a=>o({...a,method:"GET"}),getConfig:e,head:a=>o({...a,method:"HEAD"}),options:a=>o({...a,method:"OPTIONS"}),patch:a=>o({...a,method:"PATCH"}),post:a=>o({...a,method:"POST"}),put:a=>o({...a,method:"PUT"}),request:o,setConfig:i,trace:a=>o({...a,method:"TRACE"})}};exports.createClient=re;exports.createConfig=P;exports.formDataBodySerializer=F;exports.jsonBodySerializer=C;exports.urlSearchParamsBodySerializer=N;//# sourceMappingURL=index.cjs.map
'use strict';var app=require('nuxt/app'),vue=require('vue');var E=async(r,t)=>{let e=typeof t=="function"?await t(r):t;if(e)return r.scheme==="bearer"?`Bearer ${e}`:r.scheme==="basic"?`Basic ${btoa(e)}`:e},U=(r,t,e)=>{typeof e=="string"||e instanceof Blob?r.append(t,e):r.append(t,JSON.stringify(e));},k=(r,t,e)=>{typeof e=="string"?r.append(t,e):r.append(t,JSON.stringify(e));},F={bodySerializer:r=>{let t=new FormData;return Object.entries(r).forEach(([e,i])=>{i!=null&&(Array.isArray(i)?i.forEach(o=>U(t,e,o)):U(t,e,i));}),t}},C={bodySerializer:r=>JSON.stringify(r,(t,e)=>typeof e=="bigint"?e.toString():e)},N={bodySerializer:r=>{let t=new URLSearchParams;return Object.entries(r).forEach(([e,i])=>{i!=null&&(Array.isArray(i)?i.forEach(o=>k(t,e,o)):k(t,e,i));}),t}},I=r=>{switch(r){case "label":return ".";case "matrix":return ";";case "simple":return ",";default:return "&"}},Q=r=>{switch(r){case "form":return ",";case "pipeDelimited":return "|";case "spaceDelimited":return "%20";default:return ","}},V=r=>{switch(r){case "label":return ".";case "matrix":return ";";case "simple":return ",";default:return "&"}},S=({allowReserved:r,explode:t,name:e,style:i,value:o})=>{if(!t){let a=(r?o:o.map(u=>encodeURIComponent(u))).join(Q(i));switch(i){case "label":return `.${a}`;case "matrix":return `;${e}=${a}`;case "simple":return a;default:return `${e}=${a}`}}let n=I(i),s=o.map(a=>i==="label"||i==="simple"?r?a:encodeURIComponent(a):p({allowReserved:r,name:e,value:a})).join(n);return i==="label"||i==="matrix"?n+s:s},p=({allowReserved:r,name:t,value:e})=>{if(e==null)return "";if(typeof e=="object")throw new Error("Deeply-nested arrays/objects aren\u2019t supported. Provide your own `querySerializer()` to handle these.");return `${t}=${r?e:encodeURIComponent(e)}`},z=({allowReserved:r,explode:t,name:e,style:i,value:o})=>{if(o instanceof Date)return `${e}=${o.toISOString()}`;if(i!=="deepObject"&&!t){let a=[];Object.entries(o).forEach(([l,d])=>{a=[...a,l,r?d:encodeURIComponent(d)];});let u=a.join(",");switch(i){case "form":return `${e}=${u}`;case "label":return `.${u}`;case "matrix":return `;${e}=${u}`;default:return u}}let n=V(i),s=Object.entries(o).map(([a,u])=>p({allowReserved:r,name:i==="deepObject"?`${e}[${a}]`:a,value:u})).join(n);return i==="label"||i==="matrix"?n+s:s};var W=/\{[^{}]+\}/g,J=({path:r,url:t})=>{let e=t,i=t.match(W);if(i)for(let o of i){let n=false,s=o.substring(1,o.length-1),a="simple";s.endsWith("*")&&(n=true,s=s.substring(0,s.length-1)),s.startsWith(".")?(s=s.substring(1),a="label"):s.startsWith(";")&&(s=s.substring(1),a="matrix");let u=vue.toValue(vue.toValue(r)[s]);if(u==null)continue;if(Array.isArray(u)){e=e.replace(o,S({explode:n,name:s,style:a,value:u}));continue}if(typeof u=="object"){e=e.replace(o,z({explode:n,name:s,style:a,value:u}));continue}if(a==="matrix"){e=e.replace(o,`;${p({name:s,value:u})}`);continue}let l=encodeURIComponent(a==="label"?`.${u}`:u);e=e.replace(o,l);}return e},v=({allowReserved:r,array:t,object:e}={})=>o=>{let n=[],s=vue.toValue(o);if(s&&typeof s=="object")for(let a in s){let u=vue.toValue(s[a]);if(u!=null){if(Array.isArray(u)){n=[...n,S({allowReserved:r,explode:true,name:a,style:"form",value:u,...t})];continue}if(typeof u=="object"){n=[...n,z({allowReserved:r,explode:true,name:a,style:"deepObject",value:u,...e})];continue}n=[...n,p({allowReserved:r,name:a,value:u})];}}return n.join("&")},B=async({security:r,...t})=>{for(let e of r){let i=await E(e,t.auth);if(!i)continue;let o=e.name??"Authorization";switch(e.in){case "query":t.query||(t.query={}),vue.toValue(t.query)[o]=i;break;case "header":default:t.headers.set(o,i);break}return}},h=r=>M({baseUrl:r.baseURL,path:r.path,query:r.query,querySerializer:typeof r.querySerializer=="function"?r.querySerializer:v(r.querySerializer),url:r.url}),M=({baseUrl:r,path:t,query:e,querySerializer:i,url:o})=>{let n=o.startsWith("/")?o:`/${o}`,s=(r??"")+n;t&&(s=J({path:t,url:s}));let a=e?i(e):"";return a.startsWith("?")&&(a=a.substring(1)),a&&(s+=`?${a}`),s},A=(r,t)=>{let e={...r,...t};return e.baseURL?.endsWith("/")&&(e.baseURL=e.baseURL.substring(0,e.baseURL.length-1)),e.headers=O(r.headers,t.headers),e},O=(...r)=>{let t=new Headers;for(let e of r){if(!e||typeof e!="object")continue;let i=e;vue.isRef(i)&&(i=vue.unref(i));let o=i instanceof Headers?i.entries():Object.entries(i);for(let[n,s]of o)if(s===null)t.delete(n);else if(Array.isArray(s))for(let a of s)t.append(n,y(a));else if(s!==undefined){let a=y(s);t.set(n,typeof a=="object"?JSON.stringify(a):a);}}return t},w=(...r)=>r.reduce((t,e)=>{if(typeof e=="function")t.push(e);else if(Array.isArray(e))return t.concat(e);return t},[]),_=v({allowReserved:false,array:{explode:true,style:"form"},object:{explode:true,style:"deepObject"}}),K={"Content-Type":"application/json"},q=(r={})=>({...C,headers:K,querySerializer:_,...r}),y=r=>{if(r===null||typeof r!="object"||r instanceof Headers)return vue.isRef(r)?vue.unref(r):r;if(Array.isArray(r))return r.map(e=>y(e));if(vue.isRef(r))return y(vue.unref(r));let t={};for(let e in r)t[e]=y(r[e]);return t},g=r=>r.body&&r.bodySerializer?r.bodySerializer(r.body):r.body,P=(r,t)=>{let e=y(r);return e.body=g(e),t(h(r),e)};var re=(r={})=>{let t=A(q(),r),e=()=>({...t}),i=n=>(t=A(t,n),e()),o=({asyncDataOptions:n,composable:s,key:a,...u})=>{let l={...t,...u,$fetch:u.$fetch??t.$fetch??$fetch,headers:O(t.headers,u.headers),onRequest:w(t.onRequest,u.onRequest),onResponse:w(t.onResponse,u.onResponse)},{responseTransformer:d,responseValidator:R,security:j}=l;j&&(l.onRequest=[async({options:c})=>{await B({auth:l.auth,headers:c.headers,query:c.query,security:j});},...l.onRequest]),(d||R)&&(l.onResponse=[...l.onResponse,async({options:c,response:f})=>{c.responseType&&c.responseType!=="json"||f.ok&&(R&&await R(f._data),d&&(f._data=await d(f._data)));}]),l.body||l.headers.delete("Content-Type");let $=l.$fetch;if(s==="$fetch")return P(l,$);if(s==="useFetch"||s==="useLazyFetch"){let c=vue.reactive({body:l.body,bodySerializer:l.bodySerializer}),f=vue.ref(g(l));return l.body=f,vue.watch(c,D=>{f.value=g(D);}),s==="useLazyFetch"?app.useLazyFetch(()=>h(l),l):app.useFetch(()=>h(l),l)}let b=()=>P(l,$);if(s==="useAsyncData")return a?app.useAsyncData(a,b,n):app.useAsyncData(b,n);if(s==="useLazyAsyncData")return a?app.useLazyAsyncData(a,b,n):app.useLazyAsyncData(b,n)};return {buildUrl:h,connect:n=>o({...n,method:"CONNECT"}),delete:n=>o({...n,method:"DELETE"}),get:n=>o({...n,method:"GET"}),getConfig:e,head:n=>o({...n,method:"HEAD"}),options:n=>o({...n,method:"OPTIONS"}),patch:n=>o({...n,method:"PATCH"}),post:n=>o({...n,method:"POST"}),put:n=>o({...n,method:"PUT"}),request:o,setConfig:i,trace:n=>o({...n,method:"TRACE"})}};exports.createClient=re;exports.createConfig=q;exports.formDataBodySerializer=F;exports.jsonBodySerializer=C;exports.urlSearchParamsBodySerializer=N;//# sourceMappingURL=index.cjs.map

Check notice

Code scanning / CodeQL

Semicolon insertion Note test

Avoid automated semicolon insertion (90% of all statements in
the enclosing function
have an explicit semicolon).
@mrlubos mrlubos changed the title chore: use compatibilityVersion 4 in Nuxt example feat: clients base URL Feb 2, 2025
@mrlubos mrlubos force-pushed the feat/parser-servers branch from 629571e to ccefe43 Compare February 2, 2025 10:21
@mrlubos mrlubos marked this pull request as ready for review February 2, 2025 10:21
@mrlubos mrlubos merged commit 52a86fe into main Feb 2, 2025
17 checks passed
@mrlubos mrlubos deleted the feat/parser-servers branch February 2, 2025 10:27
@github-actions github-actions bot mentioned this pull request Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Get baseUrl from spec for default client
1 participant