UNPKG

foca-openapi

Version:

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

67 lines (66 loc) 2.44 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/taro.ts var taro_exports = {}; __export(taro_exports, { taroAdapter: () => taroAdapter }); module.exports = __toCommonJS(taro_exports); var taroAdapter = (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 = typeof opts2.credentials === "string" ? opts2.credentials : opts2.credentials === true ? "same-origin" : "omit"; 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, credentials, 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); } } }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { taroAdapter }); //# sourceMappingURL=taro.js.map