foca-openapi
Version:
根据openapi文档生成请求客户端
116 lines (111 loc) • 3.8 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
BaseOpenapiClient: () => BaseOpenapiClient,
defineConfig: () => defineConfig,
methods: () => methods
});
module.exports = __toCommonJS(index_exports);
// src/lib/adapter.ts
var methods = ["get", "post", "put", "patch", "delete"];
// src/utils.ts
var import_query_string = __toESM(require("query-string"));
var import_object_to_formdata = __toESM(require("object-to-formdata"));
var utils = {
/**
* 路由拼接查询字符串
*/
uriConcatQuery(uri, query) {
if (!query) return uri;
const querystring = import_query_string.default.stringify(query, { arrayFormat: "bracket" });
if (!querystring) return uri;
if (uri.includes("?")) {
if (!uri.endsWith("?")) {
uri += "&";
}
} else {
uri += "?";
}
return uri += querystring;
},
formatBody(contentType, body, formData) {
if (!body) return;
return contentType === "multipart/form-data" ? import_object_to_formdata.default.serialize(body, {}, formData) : body;
}
};
// src/base-openapi-client.ts
var BaseOpenapiClient = class {
constructor(adapter) {
this.adapter = adapter;
}
replaceURI(uri, params) {
if (!params) return uri;
Object.entries(params).forEach(([key, value]) => {
uri = uri.replace(new RegExp(`{${key}}`), value);
});
return uri;
}
request(uri, method, opts = {}) {
const contentTypes = this.pickContentTypes(uri, method);
const requestBodyType = opts.requestBodyType || contentTypes[0] || "application/json";
const responseType = opts.responseType || contentTypes[1] || "json";
const headers = opts.headers || {};
if (!Object.hasOwn(headers, "Content-Type") && !Object.hasOwn(headers, "content-type")) {
headers["Content-Type"] = requestBodyType;
}
const formattedUri = this.replaceURI(uri, opts.params);
return this.adapter.request(
{
uri: formattedUri,
method,
...opts,
requestBodyType,
responseType,
headers
},
utils
);
}
pickContentTypes(_uri, _method) {
return [void 0, void 0];
}
};
// src/define-config.ts
var defineConfig = (options) => {
return options;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BaseOpenapiClient,
defineConfig,
methods
});
//# sourceMappingURL=index.js.map
;