UNPKG

@shixinde/apifox-swagger

Version:

从 Apifox 导出 Swagger/OpenAPI 文档并生成 TypeScript 类型定义的工具

2 lines (1 loc) 3.83 kB
"use strict";function R(n,e){return[n,e]}function g(n,e){return[n,e]}function q(n,e){if(!e)return n;let t=n;for(const[r,c]of Object.entries(e))t=t.replace(`{${r}}`,String(c));return t}function b(n){if(!n)return"";const e=new URLSearchParams;for(const[r,c]of Object.entries(n))c!=null&&e.append(r,String(c));const t=e.toString();return t?`?${t}`:""}function y(n,e,t){const r=q(n,e),c=b(t);return`${r}${c}`}function C(n){return n}function F(n,e){return{endpoint:n,...e}}class l{constructor(e,t=""){this.axiosInstance=e,this.baseURL=t}async request(e,t){const[r,c]=e,{pathParams:o,queryParams:a,body:i,...u}=t||{},s=this.baseURL+y(r,o,a);return this.axiosInstance.request({url:s,method:c,data:i,...u})}async get(e,t){return this.request(e,t)}async post(e,t){return this.request(e,t)}async put(e,t){return this.request(e,t)}async delete(e,t){return this.request(e,t)}async patch(e,t){return this.request(e,t)}}function h(n,e){return new l(n,e)}function L(n,e){if(!n||typeof n.create!="function")throw new Error('Invalid axios instance provided. Please install axios and pass axios object: import axios from "axios"');const t=n.create(e);return new l(t,e.baseURL)}function U(n,e){if(!n||typeof n.create!="function")throw new Error("Invalid axios instance provided. Please pass a valid axios object.");const t=n.create(e);return new l(t,e.baseURL)}async function Q(n,e,t=""){return h(n,t).request(e.endpoint,{pathParams:e.pathParams,queryParams:e.queryParams,body:e.body})}function S(n,e=""){const t=h(n,e);return{call:(r,c)=>t.request(r,c),get:(r,c)=>t.get(r,c),post:(r,c)=>t.post(r,c),put:(r,c)=>t.put(r,c),delete:(r,c)=>t.delete(r,c),patch:(r,c)=>t.patch(r,c)}}function f(n,e,t){const[r,c]=n;return[r,c,e,t].filter(Boolean)}function d(n,e,t=""){return async({queryKey:r})=>{const[,,c,o]=r,[a,i]=e,u=t+y(a,c,o);return n.request({url:u,method:i})}}function x(n,e,t=""){return async r=>{const{pathParams:c,queryParams:o,body:a}=r,[i,u]=e,s=t+y(i,c,o);return n.request({url:s,method:u,data:a})}}class P{constructor(e,t=""){this.axiosInstance=e,this.baseURL=t}createQuery(e,t,r){const c=f(e,t==null?void 0:t.pathParams,t==null?void 0:t.queryParams),o=d(this.axiosInstance,e,this.baseURL);return{queryKey:c,queryFn:o,select:a=>a.data,...r}}createMutation(e,t){return{mutationFn:x(this.axiosInstance,e,this.baseURL),...t}}createInfiniteQuery(e,t,r){const c=f(e,t==null?void 0:t.pathParams,t==null?void 0:t.queryParams),o=d(this.axiosInstance,e,this.baseURL);return{queryKey:c,queryFn:({queryKey:a,pageParam:i})=>{const[,,u,s]=a;return o({queryKey:[a[0],a[1],u,{...s,...i}]})},select:a=>({...a,pages:a.pages.map(i=>i.data)}),...r}}}function p(n,e=""){return new P(n,e)}function m(n,e,t,r=""){return p(n,r).createQuery(e,t)}function A(n,e,t=""){return p(n,t).createMutation(e)}const I=Object.freeze(Object.defineProperty({__proto__:null,ReactQueryFactory:P,TypedAxiosClient:l,buildFullURL:y,buildQueryString:b,buildURL:q,callApi:Q,createApiFactory:S,createEndpoint:C,createMutationConfig:A,createMutationFunction:x,createQueryConfig:m,createQueryFunction:d,createQueryKey:f,createReactQueryFactory:p,createSafeCall:F,createTypedAxiosClient:h,createTypedAxiosClientSync:U,createTypedAxiosClientWithConfig:L,makeURL:R,makeURLGeneric:g},Symbol.toStringTag,{value:"Module"}));exports.ReactQueryFactory=P;exports.TypedAxiosClient=l;exports.buildFullURL=y;exports.buildQueryString=b;exports.buildURL=q;exports.callApi=Q;exports.createApiFactory=S;exports.createEndpoint=C;exports.createMutationConfig=A;exports.createMutationFunction=x;exports.createQueryConfig=m;exports.createQueryFunction=d;exports.createQueryKey=f;exports.createReactQueryFactory=p;exports.createSafeCall=F;exports.createTypedAxiosClient=h;exports.createTypedAxiosClientSync=U;exports.createTypedAxiosClientWithConfig=L;exports.index=I;exports.makeURL=R;exports.makeURLGeneric=g;