UNPKG

@shixinde/apifox-swagger

Version:

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

271 lines (270 loc) 5.22 kB
function R(n, t) { return [n, t]; } function g(n, t) { return [n, t]; } function b(n, t) { if (!t) return n; let e = n; for (const [r, s] of Object.entries(t)) e = e.replace(`{${r}}`, String(s)); return e; } function p(n) { if (!n) return ""; const t = new URLSearchParams(); for (const [r, s] of Object.entries(n)) s != null && t.append(r, String(s)); const e = t.toString(); return e ? `?${e}` : ""; } function y(n, t, e) { const r = b(n, t), s = p(e); return `${r}${s}`; } function L(n) { return n; } function U(n, t) { return { endpoint: n, ...t }; } class l { constructor(t, e = "") { this.axiosInstance = t, this.baseURL = e; } /** * 执行类型安全的 HTTP 请求 */ async request(t, e) { const [r, s] = t, { pathParams: o, queryParams: a, body: c, ...u } = e || {}, i = this.baseURL + y( r, o, a ); return this.axiosInstance.request({ url: i, method: s, data: c, ...u }); } /** * GET 请求 */ async get(t, e) { return this.request(t, e); } /** * POST 请求 */ async post(t, e) { return this.request(t, e); } /** * PUT 请求 */ async put(t, e) { return this.request(t, e); } /** * DELETE 请求 */ async delete(t, e) { return this.request(t, e); } /** * PATCH 请求 */ async patch(t, e) { return this.request(t, e); } } function d(n, t) { return new l(n, t); } function m(n, t) { 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 e = n.create(t); return new l(e, t.baseURL); } function F(n, t) { if (!n || typeof n.create != "function") throw new Error("Invalid axios instance provided. Please pass a valid axios object."); const e = n.create(t); return new l(e, t.baseURL); } async function I(n, t, e = "") { return d(n, e).request(t.endpoint, { pathParams: t.pathParams, queryParams: t.queryParams, body: t.body }); } function S(n, t = "") { const e = d(n, t); return { /** * 调用 API 端点 */ call: (r, s) => e.request(r, s), /** * GET 请求 */ get: (r, s) => e.get(r, s), /** * POST 请求 */ post: (r, s) => e.post(r, s), /** * PUT 请求 */ put: (r, s) => e.put(r, s), /** * DELETE 请求 */ delete: (r, s) => e.delete(r, s), /** * PATCH 请求 */ patch: (r, s) => e.patch(r, s) }; } function f(n, t, e) { const [r, s] = n; return [r, s, t, e].filter(Boolean); } function h(n, t, e = "") { return async ({ queryKey: r }) => { const [, , s, o] = r, [a, c] = t, u = e + y( a, s, o ); return n.request({ url: u, method: c }); }; } function P(n, t, e = "") { return async (r) => { const { pathParams: s, queryParams: o, body: a } = r, [c, u] = t, i = e + y( c, s, o ); return n.request({ url: i, method: u, data: a }); }; } class x { constructor(t, e = "") { this.axiosInstance = t, this.baseURL = e; } /** * 创建查询配置 */ createQuery(t, e, r) { const s = f(t, e == null ? void 0 : e.pathParams, e == null ? void 0 : e.queryParams), o = h(this.axiosInstance, t, this.baseURL); return { queryKey: s, queryFn: o, select: (a) => a.data, ...r }; } /** * 创建变更配置 */ createMutation(t, e) { return { mutationFn: P(this.axiosInstance, t, this.baseURL), ...e }; } /** * 创建无限查询配置 */ createInfiniteQuery(t, e, r) { const s = f(t, e == null ? void 0 : e.pathParams, e == null ? void 0 : e.queryParams), o = h(this.axiosInstance, t, this.baseURL); return { queryKey: s, queryFn: ({ queryKey: a, pageParam: c }) => { const [, , u, i] = a; return o({ queryKey: [a[0], a[1], u, { ...i, ...c }] }); }, select: (a) => ({ ...a, pages: a.pages.map((c) => c.data) }), ...r }; } } function q(n, t = "") { return new x(n, t); } function v(n, t, e, r = "") { return q(n, r).createQuery(t, e); } function w(n, t, e = "") { return q(n, e).createMutation(t); } const C = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({ __proto__: null, ReactQueryFactory: x, TypedAxiosClient: l, buildFullURL: y, buildQueryString: p, buildURL: b, callApi: I, createApiFactory: S, createEndpoint: L, createMutationConfig: w, createMutationFunction: P, createQueryConfig: v, createQueryFunction: h, createQueryKey: f, createReactQueryFactory: q, createSafeCall: U, createTypedAxiosClient: d, createTypedAxiosClientSync: F, createTypedAxiosClientWithConfig: m, makeURL: R, makeURLGeneric: g }, Symbol.toStringTag, { value: "Module" })); export { x as R, l as T, g as a, b, p as c, y as d, L as e, U as f, d as g, m as h, C as i, F as j, I as k, S as l, R as m, q as n, v as o, w as p, f as q, h as r, P as s };