UNPKG

foca-openapi

Version:

根据openapi文档生成请求客户端

69 lines (68 loc) 2.61 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/adapters/uniapp.ts var uniapp_exports = {}; __export(uniapp_exports, { uniappAdapter: () => uniappAdapter }); module.exports = __toCommonJS(uniapp_exports); var uniappAdapter = (opts) => { const { returningData = (result) => result.data, getStatusCode = (result) => result.statusCode, onFail = (_, config) => { throw new Error(`\u8BF7\u6C42\u63A5\u53E3"${config.uri}"\u5931\u8D25\uFF0C\u72B6\u6001\u7801\uFF1A${config.statusCode}`); }, request, baseURL = "", requestOptions } = opts; return { async request(opts2, utils) { const body = utils.formatBody(opts2.requestBodyType, opts2.body); const credentials = opts2.credentials === "omit" || opts2.credentials === false ? false : typeof opts2.credentials === "string" || opts2.credentials === true ? true : void 0; const config = { ...requestOptions, url: baseURL + utils.uriConcatQuery(opts2.uri, opts2.query), method: opts2.method.toUpperCase(), data: body, header: { ...requestOptions?.header, ...opts2.headers }, timeout: opts2.timeout, withCredentials: credentials, dataType: opts2.responseType === "json" ? "json" : void 0, responseType: opts2.responseType === "text" ? "text" : void 0 }; const result = await request(opts2.onBeforeRequest?.(config) || config); const statusCode = getStatusCode(result); if (statusCode >= 400) { return Promise.reject( onFail(result, { uri: opts2.uri, method: opts2.method, statusCode }) ); } else { return returningData(result); } } }; }; uniappAdapter(uni); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { uniappAdapter }); //# sourceMappingURL=uniapp.js.map