@devbie/apits 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -2
- package/bin/apits +4 -0
- package/dist/cjs/core/cli.d.ts +29 -0
- package/dist/cjs/core/cli.js +396 -0
- package/dist/cjs/core/generator.d.ts +44 -0
- package/dist/cjs/core/generator.js +301 -0
- package/dist/cjs/core/writeRequestClient.d.ts +2 -0
- package/dist/cjs/core/writeRequestClient.js +93 -0
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +25 -0
- package/dist/cjs/types.d.ts +330 -0
- package/dist/cjs/types.js +90 -0
- package/dist/cjs/utils/console.d.ts +16 -0
- package/dist/cjs/utils/console.js +79 -0
- package/dist/cjs/utils/getOutputPath.d.ts +5 -0
- package/dist/cjs/utils/getOutputPath.js +43 -0
- package/dist/cjs/utils/helpers.d.ts +65 -0
- package/dist/cjs/utils/helpers.js +166 -0
- package/dist/cjs/utils/openApiToInterfaces.d.ts +14 -0
- package/dist/cjs/utils/openApiToInterfaces.js +229 -0
- package/dist/cjs/utils/spinner.d.ts +15 -0
- package/dist/cjs/utils/spinner.js +69 -0
- package/dist/cjs/utils/utils.d.ts +56 -0
- package/dist/cjs/utils/utils.js +355 -0
- package/dist/cjs/utils/vtilsLite.d.ts +55 -0
- package/dist/cjs/utils/vtilsLite.js +123 -0
- package/dist/esm/core/cli.d.mts +29 -0
- package/dist/esm/core/cli.mjs +713 -0
- package/dist/esm/core/generator.d.mts +44 -0
- package/dist/esm/core/generator.mjs +463 -0
- package/dist/esm/core/writeRequestClient.d.mts +2 -0
- package/dist/esm/core/writeRequestClient.mjs +54 -0
- package/dist/esm/index.d.mts +2 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/types.d.mts +330 -0
- package/dist/esm/types.mjs +94 -0
- package/dist/esm/utils/console.d.mts +16 -0
- package/dist/esm/utils/console.mjs +65 -0
- package/dist/esm/utils/getOutputPath.d.mts +5 -0
- package/dist/esm/utils/getOutputPath.mjs +12 -0
- package/dist/esm/utils/helpers.d.mts +65 -0
- package/dist/esm/utils/helpers.mjs +226 -0
- package/dist/esm/utils/openApiToInterfaces.d.mts +14 -0
- package/dist/esm/utils/openApiToInterfaces.mjs +326 -0
- package/dist/esm/utils/spinner.d.mts +15 -0
- package/dist/esm/utils/spinner.mjs +58 -0
- package/dist/esm/utils/utils.d.mts +56 -0
- package/dist/esm/utils/utils.mjs +545 -0
- package/dist/esm/utils/vtilsLite.d.mts +55 -0
- package/dist/esm/utils/vtilsLite.mjs +142 -0
- package/package.json +68 -4
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/core/generator.ts
|
|
30
|
+
var generator_exports = {};
|
|
31
|
+
__export(generator_exports, {
|
|
32
|
+
Generator: () => Generator
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(generator_exports);
|
|
35
|
+
var changeCase = __toESM(require("change-case"));
|
|
36
|
+
var import_fs_extra = __toESM(require("fs-extra"));
|
|
37
|
+
var import_path = __toESM(require("path"));
|
|
38
|
+
var import_js_yaml = __toESM(require("js-yaml"));
|
|
39
|
+
var import_json5 = __toESM(require("json5"));
|
|
40
|
+
var conso = __toESM(require("../utils/console.js"));
|
|
41
|
+
var import_got = __toESM(require("got"));
|
|
42
|
+
var import_openApiToInterfaces = require("../utils/openApiToInterfaces.js");
|
|
43
|
+
var import_vtilsLite = require("../utils/vtilsLite.js");
|
|
44
|
+
var import_types = require("../types.js");
|
|
45
|
+
var import_utils = require("../utils/utils.js");
|
|
46
|
+
var import_getOutputPath = require("../utils/getOutputPath.js");
|
|
47
|
+
var import_writeRequestClient = __toESM(require("./writeRequestClient.js"));
|
|
48
|
+
function defaultClientImportTemplate(config) {
|
|
49
|
+
return `import request from './request'`;
|
|
50
|
+
}
|
|
51
|
+
var getDataKeySetStr = (method) => {
|
|
52
|
+
if (["head", "option", "get"].includes(method.toLowerCase())) {
|
|
53
|
+
return "params: data";
|
|
54
|
+
}
|
|
55
|
+
return "data";
|
|
56
|
+
};
|
|
57
|
+
function handlePathParam(path2) {
|
|
58
|
+
if (path2.match(/\{(\w+)\}/)) {
|
|
59
|
+
return `\`${path2.replace(/\{(\w+)\}/g, "${data.$1}")}\``;
|
|
60
|
+
}
|
|
61
|
+
return JSON.stringify(path2);
|
|
62
|
+
}
|
|
63
|
+
function isDegradedRequestType(typeCode) {
|
|
64
|
+
const code = typeCode.replace(/\s+/g, " ").trim();
|
|
65
|
+
const match = code.match(/^export type \w+ = (.*?);?$/);
|
|
66
|
+
if (!match)
|
|
67
|
+
return false;
|
|
68
|
+
const body = match[1].trim();
|
|
69
|
+
if (/^(string|unknown|number|boolean|null)$/.test(body))
|
|
70
|
+
return true;
|
|
71
|
+
if (/^\{\s*\}$/.test(body))
|
|
72
|
+
return true;
|
|
73
|
+
if (/^\{\s*\[\s*\w+\s*:\s*string\s*\]\s*:\s*unknown\s*\}$/.test(body))
|
|
74
|
+
return true;
|
|
75
|
+
if (/&\s*(string|unknown)\b/.test(body))
|
|
76
|
+
return true;
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
var DEFAULT_CLIENT_CONFIG_TYPE = "AxiosRequestConfig";
|
|
80
|
+
function defaultRequestFunctionTemplate(props, config) {
|
|
81
|
+
const { baseURL, requestFunctionName, requestDataTypeName, responseDataTypeName, extendedInterfaceInfo } = props;
|
|
82
|
+
const clientConfigType = (config == null ? void 0 : config.clientConfigType) || DEFAULT_CLIENT_CONFIG_TYPE;
|
|
83
|
+
const { req_params, req_query, req_body_type, req_body_multipart, req_body_form } = extendedInterfaceInfo;
|
|
84
|
+
const hasData = req_params.length || req_query.length || Array.isArray(req_body_form) && req_body_form.length;
|
|
85
|
+
const method = extendedInterfaceInfo.method.toLowerCase();
|
|
86
|
+
let finalBaseUrl = "";
|
|
87
|
+
if (baseURL == null ? void 0 : baseURL.match(/^\[code\]:/)) {
|
|
88
|
+
finalBaseUrl = baseURL.replace(/^\[code\]:/, "");
|
|
89
|
+
} else {
|
|
90
|
+
finalBaseUrl = `"${baseURL}"`;
|
|
91
|
+
}
|
|
92
|
+
const isForm = req_body_type === import_types.RequestBodyType.form;
|
|
93
|
+
const isMultipart = isForm && req_body_multipart === true;
|
|
94
|
+
const formBuilder = isForm ? `const form = new ${isMultipart ? "FormData" : "URLSearchParams"}();
|
|
95
|
+
Object.entries(data).forEach(([k, v]) => {
|
|
96
|
+
if (v == null) return;
|
|
97
|
+
if (Array.isArray(v)) v.forEach(i => form.append(k, ${isMultipart ? "i" : "String(i)"}));
|
|
98
|
+
else form.append(k, ${isMultipart ? "v" : "String(v)"});
|
|
99
|
+
});` : "";
|
|
100
|
+
const dataExpr = isForm ? "data: form" : getDataKeySetStr(method);
|
|
101
|
+
return `export const ${requestFunctionName} = (data${hasData ? "" : "?"}: ${requestDataTypeName}${`,extra?:${clientConfigType}`}) => {
|
|
102
|
+
${formBuilder}
|
|
103
|
+
return request.${method}<${requestDataTypeName},${responseDataTypeName}>(${handlePathParam(
|
|
104
|
+
extendedInterfaceInfo.path
|
|
105
|
+
)}, {
|
|
106
|
+
${dataExpr},
|
|
107
|
+
${baseURL ? `baseURL: ${finalBaseUrl},` : ""}
|
|
108
|
+
${`...extra`}
|
|
109
|
+
})
|
|
110
|
+
}`;
|
|
111
|
+
}
|
|
112
|
+
var Generator = class {
|
|
113
|
+
constructor(config) {
|
|
114
|
+
this.config = config;
|
|
115
|
+
}
|
|
116
|
+
/** Whether an input points to a remote http(s) resource. */
|
|
117
|
+
static isHttpInput(input) {
|
|
118
|
+
return /^https?:\/\//i.test(input);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Derive a file base name from the input: hostname for URLs, file name
|
|
122
|
+
* without extension for local paths. Falls back to `api`.
|
|
123
|
+
*/
|
|
124
|
+
static deriveName(input) {
|
|
125
|
+
let raw = "";
|
|
126
|
+
if (Generator.isHttpInput(input)) {
|
|
127
|
+
try {
|
|
128
|
+
raw = new URL(input).hostname.replace(/^www\./, "");
|
|
129
|
+
} catch {
|
|
130
|
+
raw = "";
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
raw = import_path.default.basename(input).replace(/\.(json5?|ya?ml)$/i, "");
|
|
134
|
+
}
|
|
135
|
+
return changeCase.camelCase(raw) || "api";
|
|
136
|
+
}
|
|
137
|
+
/** Parse a raw JSON / JSON5 / YAML document string. */
|
|
138
|
+
parseSpecDocument(raw) {
|
|
139
|
+
const head = raw.trimStart();
|
|
140
|
+
if (head.startsWith("{") || head.startsWith("[")) {
|
|
141
|
+
return import_json5.default.parse(raw);
|
|
142
|
+
}
|
|
143
|
+
return import_js_yaml.default.load(raw);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Load the OpenAPI document from a remote URL or a local file.
|
|
147
|
+
* Both JSON (.json/.json5) and YAML (.yaml/.yml) are supported.
|
|
148
|
+
*/
|
|
149
|
+
async loadSpec(input) {
|
|
150
|
+
if (Generator.isHttpInput(input)) {
|
|
151
|
+
const res = await import_got.default.get(input, { responseType: "text" });
|
|
152
|
+
return this.parseSpecDocument(res.body);
|
|
153
|
+
}
|
|
154
|
+
const filePath = import_path.default.resolve(input);
|
|
155
|
+
const raw = await import_fs_extra.default.readFile(filePath, "utf-8");
|
|
156
|
+
return this.parseSpecDocument(raw);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Generate all code from the OpenAPI document.
|
|
160
|
+
* @returns map of output file path to file contents
|
|
161
|
+
*/
|
|
162
|
+
async generate() {
|
|
163
|
+
var _a;
|
|
164
|
+
const outputFileList = /* @__PURE__ */ Object.create(null);
|
|
165
|
+
const { input, name } = this.config;
|
|
166
|
+
const typesName = name || Generator.deriveName(input);
|
|
167
|
+
const openApiV3Json = await this.loadSpec(input);
|
|
168
|
+
const componentsSchemas = ((_a = openApiV3Json.components) == null ? void 0 : _a.schemas) ?? {};
|
|
169
|
+
const componentsCode = [];
|
|
170
|
+
await Promise.all(
|
|
171
|
+
Object.keys(componentsSchemas).map(async (key) => {
|
|
172
|
+
const code = await (0, import_utils.jsonSchemaToTsCode)({ ...componentsSchemas[key], components: openApiV3Json.components }, key);
|
|
173
|
+
componentsCode.push(code);
|
|
174
|
+
})
|
|
175
|
+
);
|
|
176
|
+
const allApi = await (0, import_openApiToInterfaces.openApiToInterfaces)(openApiV3Json);
|
|
177
|
+
let interfaceList = allApi.interfaces;
|
|
178
|
+
const categoryCode = [...componentsCode];
|
|
179
|
+
for (let interfaceInfo of interfaceList) {
|
|
180
|
+
const code = await this.generateInterfaceCode(
|
|
181
|
+
{
|
|
182
|
+
...this.config,
|
|
183
|
+
components: openApiV3Json.components
|
|
184
|
+
},
|
|
185
|
+
interfaceInfo
|
|
186
|
+
);
|
|
187
|
+
categoryCode.push(code);
|
|
188
|
+
}
|
|
189
|
+
const catOutputFilePath = (0, import_getOutputPath.getOutputFilePath)(this.config, `/${typesName}.ts`);
|
|
190
|
+
if (categoryCode.length > 0) {
|
|
191
|
+
outputFileList[catOutputFilePath] = {
|
|
192
|
+
projectId: typesName,
|
|
193
|
+
categoryId: typesName,
|
|
194
|
+
syntheticalConfig: this.config,
|
|
195
|
+
content: categoryCode
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
return outputFileList;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Write all generated files to disk.
|
|
202
|
+
* @param outputFileList generated files
|
|
203
|
+
*/
|
|
204
|
+
async write(outputFileList) {
|
|
205
|
+
const config = this.config || {};
|
|
206
|
+
await (0, import_writeRequestClient.default)(config);
|
|
207
|
+
return Promise.all(
|
|
208
|
+
Object.keys(outputFileList).map(async (outputFilePath) => {
|
|
209
|
+
const { content, syntheticalConfig } = outputFileList[outputFilePath];
|
|
210
|
+
const resolvedPath = outputFilePath.replace(/\.js(x)?$/, ".ts$1");
|
|
211
|
+
const clientImportTemplate = syntheticalConfig.clientImportTemplate || defaultClientImportTemplate;
|
|
212
|
+
const clientConfigType = syntheticalConfig.clientConfigType || DEFAULT_CLIENT_CONFIG_TYPE;
|
|
213
|
+
const clientConfigImport = clientConfigType === DEFAULT_CLIENT_CONFIG_TYPE ? "import type { AxiosRequestConfig } from 'axios';" : "";
|
|
214
|
+
const rawOutputContent = import_vtilsLite.dedent`
|
|
215
|
+
${(0, import_utils.topNotesContent)()}
|
|
216
|
+
${clientImportTemplate(config)}
|
|
217
|
+
${clientConfigImport}
|
|
218
|
+
|
|
219
|
+
${content.join("\n\n").trim()}
|
|
220
|
+
`;
|
|
221
|
+
const outputContent = await (0, import_utils.formatContent)(import_vtilsLite.dedent`${rawOutputContent}`, resolvedPath);
|
|
222
|
+
await import_fs_extra.default.outputFile(resolvedPath, outputContent);
|
|
223
|
+
})
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
/** Generate a request function name from the extended interface info. */
|
|
227
|
+
requestFunctionNameGen(extendedInterfaceInfo) {
|
|
228
|
+
const path2 = extendedInterfaceInfo.parsedPath.dir;
|
|
229
|
+
const method = extendedInterfaceInfo.method;
|
|
230
|
+
const words = [method, ...path2.split("/"), extendedInterfaceInfo.parsedPath.name].join("_");
|
|
231
|
+
return changeCase.camelCase(words);
|
|
232
|
+
}
|
|
233
|
+
/** Generate TypeScript code (types + request function) for a single API. */
|
|
234
|
+
async generateInterfaceCode(syntheticalConfig, interfaceInfo) {
|
|
235
|
+
const extendedInterfaceInfo = {
|
|
236
|
+
...interfaceInfo,
|
|
237
|
+
parsedPath: import_path.default.parse(interfaceInfo.path)
|
|
238
|
+
};
|
|
239
|
+
const requestFunctionName = this.requestFunctionNameGen(extendedInterfaceInfo);
|
|
240
|
+
const requestDataTypeName = changeCase.pascalCase(`${requestFunctionName}Request`);
|
|
241
|
+
const responseDataTypeName = changeCase.pascalCase(`${requestFunctionName}Response`);
|
|
242
|
+
const requestDataJsonSchema = (0, import_utils.getRequestDataJsonSchema)(extendedInterfaceInfo);
|
|
243
|
+
const requestDataType = await (0, import_utils.jsonSchemaToTsCode)(
|
|
244
|
+
{ ...requestDataJsonSchema, components: syntheticalConfig.components },
|
|
245
|
+
requestDataTypeName
|
|
246
|
+
);
|
|
247
|
+
if (isDegradedRequestType(requestDataType)) {
|
|
248
|
+
conso.warn(
|
|
249
|
+
`[apits] Request type degraded for ${extendedInterfaceInfo.method.toUpperCase()} ${extendedInterfaceInfo.path} — check backend @Body()/@ApiBody decorator. Generated:
|
|
250
|
+
${requestDataType}`
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
const responseDataJsonSchema = (0, import_utils.getResponseDataJsonSchema)(extendedInterfaceInfo);
|
|
254
|
+
const responseDataType = await (0, import_utils.jsonSchemaToTsCode)(
|
|
255
|
+
{ ...responseDataJsonSchema, components: syntheticalConfig.components },
|
|
256
|
+
responseDataTypeName
|
|
257
|
+
);
|
|
258
|
+
const genComment = (genTitle) => {
|
|
259
|
+
const escapedTitle = String(extendedInterfaceInfo.title).replace(/\//g, "\\/");
|
|
260
|
+
return import_vtilsLite.dedent`
|
|
261
|
+
/**
|
|
262
|
+
* ${genTitle(escapedTitle)}
|
|
263
|
+
*/
|
|
264
|
+
`;
|
|
265
|
+
};
|
|
266
|
+
const requestFunctionTemplate = defaultRequestFunctionTemplate;
|
|
267
|
+
const baseURL = syntheticalConfig.baseURL;
|
|
268
|
+
let baseUrl;
|
|
269
|
+
try {
|
|
270
|
+
baseUrl = typeof baseURL === "string" ? baseURL : typeof baseURL === "function" ? baseURL(extendedInterfaceInfo.path) : "";
|
|
271
|
+
} catch (e) {
|
|
272
|
+
conso.error(e);
|
|
273
|
+
}
|
|
274
|
+
const code = import_vtilsLite.dedent`
|
|
275
|
+
${genComment((title) => `${title} request parameters`)}
|
|
276
|
+
${requestDataType.trim()}
|
|
277
|
+
|
|
278
|
+
${genComment((title) => `${title} response data`)}
|
|
279
|
+
${responseDataType.trim()}
|
|
280
|
+
|
|
281
|
+
${import_vtilsLite.dedent`
|
|
282
|
+
${genComment((title) => `${title}`)}
|
|
283
|
+
${requestFunctionTemplate(
|
|
284
|
+
{
|
|
285
|
+
baseURL: baseUrl,
|
|
286
|
+
requestFunctionName,
|
|
287
|
+
requestDataTypeName,
|
|
288
|
+
responseDataTypeName,
|
|
289
|
+
extendedInterfaceInfo
|
|
290
|
+
},
|
|
291
|
+
syntheticalConfig
|
|
292
|
+
)}
|
|
293
|
+
`}
|
|
294
|
+
`;
|
|
295
|
+
return code;
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
299
|
+
0 && (module.exports = {
|
|
300
|
+
Generator
|
|
301
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/core/writeRequestClient.ts
|
|
30
|
+
var writeRequestClient_exports = {};
|
|
31
|
+
__export(writeRequestClient_exports, {
|
|
32
|
+
default: () => writeRequestClient
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(writeRequestClient_exports);
|
|
35
|
+
var import_fs_extra = __toESM(require("fs-extra"));
|
|
36
|
+
var import_vtilsLite = require("../utils/vtilsLite.js");
|
|
37
|
+
var import_getOutputPath = require("../utils/getOutputPath.js");
|
|
38
|
+
var import_utils = require("../utils/utils.js");
|
|
39
|
+
async function writeRequestClient(config) {
|
|
40
|
+
const { client } = config;
|
|
41
|
+
if (client === false)
|
|
42
|
+
return;
|
|
43
|
+
const rawRequestFunctionFilePath = (0, import_getOutputPath.getOutputFilePath)(config, "request.ts");
|
|
44
|
+
if (await import_fs_extra.default.pathExists(rawRequestFunctionFilePath)) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const content = `
|
|
48
|
+
${(0, import_utils.topNotesContent)()}
|
|
49
|
+
|
|
50
|
+
import axios, { AxiosRequestConfig } from 'axios';
|
|
51
|
+
|
|
52
|
+
const instance = axios.create({
|
|
53
|
+
withCredentials: true,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Custom request interceptor.
|
|
57
|
+
instance.interceptors.request.use((config) => {
|
|
58
|
+
return { ...config };
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Custom response interceptor — return res.data so the generated request
|
|
62
|
+
// functions receive the response payload directly, matching the RP type.
|
|
63
|
+
instance.interceptors.response.use((res) => {
|
|
64
|
+
const { status } = res;
|
|
65
|
+
if (status >= 200 && status < 300) {
|
|
66
|
+
return res.data;
|
|
67
|
+
}
|
|
68
|
+
return Promise.reject(res);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
export default {
|
|
72
|
+
get: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {
|
|
73
|
+
return instance.get<RP>(url, config) as Promise<RP>;
|
|
74
|
+
},
|
|
75
|
+
post: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {
|
|
76
|
+
return instance.post<RP>(url, undefined, config) as Promise<RP>;
|
|
77
|
+
},
|
|
78
|
+
head: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {
|
|
79
|
+
return instance.head<RP>(url, config) as Promise<RP>;
|
|
80
|
+
},
|
|
81
|
+
put: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {
|
|
82
|
+
return instance.put<RP>(url, undefined, config) as Promise<RP>;
|
|
83
|
+
},
|
|
84
|
+
patch: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {
|
|
85
|
+
return instance.patch<RP>(url, undefined, config) as Promise<RP>;
|
|
86
|
+
},
|
|
87
|
+
delete: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {
|
|
88
|
+
return instance.delete<RP>(url, config) as Promise<RP>;
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
`;
|
|
92
|
+
await import_fs_extra.default.outputFile(rawRequestFunctionFilePath, await (0, import_utils.formatContent)(import_vtilsLite.dedent`${content}`, rawRequestFunctionFilePath));
|
|
93
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// src/index.ts
|
|
17
|
+
var src_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(src_exports);
|
|
19
|
+
__reExport(src_exports, require("./types.js"), module.exports);
|
|
20
|
+
__reExport(src_exports, require("./utils/helpers.js"), module.exports);
|
|
21
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
22
|
+
0 && (module.exports = {
|
|
23
|
+
...require("./types.js"),
|
|
24
|
+
...require("./utils/helpers.js")
|
|
25
|
+
});
|