api-farmer 0.0.20 → 0.0.21
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 +4 -0
- package/dist/{chunk-5YXY7GCE.js → chunk-GMB5TIVL.js} +13 -8
- package/dist/{chunk-NFK24PSA.js → chunk-PTGO2Y5X.js} +43 -12
- package/dist/cli.cjs +66 -34
- package/dist/cli.js +2 -2
- package/dist/{generate-IQIB2PJD.js → generate-HKA2M6ER.js} +2 -2
- package/dist/index.cjs +67 -29
- package/dist/index.d.cts +12 -4
- package/dist/index.d.ts +12 -4
- package/dist/index.js +10 -4
- package/package.json +2 -2
- package/templates/axios.ejs +8 -3
- package/templates/axle.ejs +4 -2
package/README.md
CHANGED
|
@@ -210,6 +210,10 @@ export interface ApiModulePayload {
|
|
|
210
210
|
* such as User, Comment, Post, etc.
|
|
211
211
|
*/
|
|
212
212
|
entity: string
|
|
213
|
+
/**
|
|
214
|
+
* The request content type of the API endpoint, such as 'application/json', 'application/x-www-form-urlencoded'.
|
|
215
|
+
*/
|
|
216
|
+
requestContentType?: string
|
|
213
217
|
/**
|
|
214
218
|
* The type name of the API endpoint,
|
|
215
219
|
* such as ApiGetUsers, ApiCreatePost, ApiUpdateComment, etc.
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CWD,
|
|
3
3
|
SUPPORTED_HTTP_METHODS,
|
|
4
|
-
|
|
4
|
+
getRequestBodyContentType,
|
|
5
|
+
getResponseMetadataItems,
|
|
5
6
|
hasQueryParameter,
|
|
6
7
|
isRequiredRequestBody,
|
|
7
8
|
readSchema,
|
|
8
9
|
readTemplateFile
|
|
9
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-PTGO2Y5X.js";
|
|
10
11
|
|
|
11
12
|
// src/generate.ts
|
|
12
13
|
import { resolve } from "path";
|
|
@@ -91,9 +92,10 @@ function transformTypeRequestBody({
|
|
|
91
92
|
}
|
|
92
93
|
function transformTypeRequestBodyValue({
|
|
93
94
|
type,
|
|
94
|
-
required
|
|
95
|
+
required,
|
|
96
|
+
requestContentType
|
|
95
97
|
}) {
|
|
96
|
-
return required ? `${type}['requestBody']['content']['
|
|
98
|
+
return required ? `${type}['requestBody']['content']['${requestContentType}']` : `NonNullable<${type}['requestBody']>['content']['${requestContentType}'] | undefined`;
|
|
97
99
|
}
|
|
98
100
|
function transformTypeResponseBody({
|
|
99
101
|
type
|
|
@@ -104,7 +106,7 @@ function transformTypeResponseBodyValue({
|
|
|
104
106
|
type,
|
|
105
107
|
responseMetadataItems
|
|
106
108
|
}) {
|
|
107
|
-
return responseMetadataItems.map(({ status,
|
|
109
|
+
return responseMetadataItems.map(({ status, responseContentType }) => `${type}['responses']['${status}']['content']['${responseContentType}']`).join(" | ");
|
|
108
110
|
}
|
|
109
111
|
function createTransformer() {
|
|
110
112
|
return {
|
|
@@ -132,21 +134,23 @@ function transformPayloads(pathItems, options) {
|
|
|
132
134
|
const args = { path, base, url, method, operation };
|
|
133
135
|
const entity = transformer.entity(args);
|
|
134
136
|
const verb = transformer.verb(args);
|
|
137
|
+
const requestContentType = operation.requestBody ? getRequestBodyContentType(operation.requestBody) : void 0;
|
|
138
|
+
const responseMetadataItems = getResponseMetadataItems(operation, validateStatus);
|
|
135
139
|
const fn = transformer.fn({ ...args, verb, entity });
|
|
136
140
|
const type = transformer.type({ ...args, verb, entity });
|
|
137
141
|
const typeValue = transformer.typeValue({ ...args, verb, entity });
|
|
138
142
|
const typeQuery = transformer.typeQuery({ ...args, type, verb, entity });
|
|
139
143
|
const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue({ ...args, type, verb, entity }) : "undefined";
|
|
140
144
|
const typeRequestBody = transformer.typeRequestBody({ ...args, type, verb, entity });
|
|
141
|
-
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({
|
|
145
|
+
const typeRequestBodyValue = operation.requestBody && requestContentType ? transformer.typeRequestBodyValue({
|
|
142
146
|
...args,
|
|
143
147
|
type,
|
|
144
148
|
verb,
|
|
145
149
|
entity,
|
|
146
|
-
required: isRequiredRequestBody(operation.requestBody)
|
|
150
|
+
required: isRequiredRequestBody(operation.requestBody),
|
|
151
|
+
requestContentType
|
|
147
152
|
}) : "undefined";
|
|
148
153
|
const typeResponseBody = transformer.typeResponseBody({ ...args, type, verb, entity });
|
|
149
|
-
const responseMetadataItems = getValidResponseMetadataItems(operation, validateStatus);
|
|
150
154
|
const typeResponseBodyValue = responseMetadataItems.length > 0 ? transformer.typeResponseBodyValue({ ...args, type, verb, entity, responseMetadataItems }) : "undefined";
|
|
151
155
|
payloads.push({
|
|
152
156
|
fn,
|
|
@@ -154,6 +158,7 @@ function transformPayloads(pathItems, options) {
|
|
|
154
158
|
method,
|
|
155
159
|
verb,
|
|
156
160
|
entity,
|
|
161
|
+
requestContentType,
|
|
157
162
|
type,
|
|
158
163
|
typeValue,
|
|
159
164
|
typeQuery,
|
|
@@ -14,7 +14,10 @@ var SUPPORTED_HTTP_METHODS = ["get", "post", "put", "delete", "patch", "options"
|
|
|
14
14
|
|
|
15
15
|
// src/utils.ts
|
|
16
16
|
import { resolve as resolve2 } from "path";
|
|
17
|
+
import { createAxle } from "@varlet/axle";
|
|
17
18
|
import fse from "fs-extra";
|
|
19
|
+
import { tryParseJSON } from "rattail";
|
|
20
|
+
import { logger } from "rslog";
|
|
18
21
|
import swagger from "swagger2openapi";
|
|
19
22
|
import yaml from "yaml";
|
|
20
23
|
function createStatusCodesByStrategy(strategy) {
|
|
@@ -49,13 +52,29 @@ function createStatusCodesByStrategy(strategy) {
|
|
|
49
52
|
}[strategy];
|
|
50
53
|
}
|
|
51
54
|
async function readSchema(input) {
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
const swaggerOrOpenapiSchema = isYaml ? yaml.parse(content) : JSON.parse(content);
|
|
55
|
+
const content = await readSchemaContent(input);
|
|
56
|
+
const jsonSchema = tryParseJSON(content);
|
|
57
|
+
const swaggerOrOpenapiSchema = jsonSchema ? jsonSchema : yaml.parse(content);
|
|
56
58
|
const schema = swaggerOrOpenapiSchema.swagger ? (await swagger.convert(swaggerOrOpenapiSchema, {})).openapi : swaggerOrOpenapiSchema;
|
|
57
59
|
return schema;
|
|
58
60
|
}
|
|
61
|
+
async function readSchemaContent(input) {
|
|
62
|
+
if (isRemoteSchema(input)) {
|
|
63
|
+
try {
|
|
64
|
+
logger.info("Fetching remote schema...");
|
|
65
|
+
const { data } = await createAxle().get(input);
|
|
66
|
+
return JSON.stringify(data);
|
|
67
|
+
} catch {
|
|
68
|
+
throw new Error("Failed to fetch remote schema");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const path2 = resolve2(CWD, input);
|
|
72
|
+
const content = fse.readFileSync(path2, "utf-8");
|
|
73
|
+
return content;
|
|
74
|
+
}
|
|
75
|
+
function isRemoteSchema(path2) {
|
|
76
|
+
return path2.startsWith("http://") || path2.startsWith("https://");
|
|
77
|
+
}
|
|
59
78
|
function readTemplateFile(preset = "axle") {
|
|
60
79
|
if (fse.existsSync(CUSTOM_TEMPLATE_FILE)) {
|
|
61
80
|
return fse.readFileSync(CUSTOM_TEMPLATE_FILE, "utf-8");
|
|
@@ -71,18 +90,27 @@ function getCliVersion() {
|
|
|
71
90
|
function isRequiredRequestBody(value) {
|
|
72
91
|
return "required" in value && value.required === true;
|
|
73
92
|
}
|
|
74
|
-
function
|
|
93
|
+
function getRequestBodyContentType(value) {
|
|
94
|
+
if (!("content" in value)) {
|
|
95
|
+
return "";
|
|
96
|
+
}
|
|
97
|
+
return value.content["application/json"] ? "application/json" : value.content["application/x-www-form-urlencoded"] ? "application/x-www-form-urlencoded" : void 0;
|
|
98
|
+
}
|
|
99
|
+
function getResponseMetadataItems(operation, validateStatus) {
|
|
75
100
|
const responses = operation.responses ?? {};
|
|
76
101
|
const validStatusResults = Object.keys(responses).sort((a, b) => Number(a) - Number(b)).filter((key) => validateStatus(Number(key))).map(Number);
|
|
77
|
-
const
|
|
78
|
-
const content = operation.responses?.[status]?.content;
|
|
79
|
-
const
|
|
102
|
+
const metadataItems = validStatusResults.map((status) => {
|
|
103
|
+
const content = operation.responses?.[status]?.content ?? {};
|
|
104
|
+
const responseContentType = findResponseContentType(content);
|
|
80
105
|
return {
|
|
81
106
|
status,
|
|
82
|
-
|
|
107
|
+
responseContentType
|
|
83
108
|
};
|
|
84
|
-
}).filter((result) => result.
|
|
85
|
-
|
|
109
|
+
}).filter((result) => result.responseContentType);
|
|
110
|
+
function findResponseContentType(content) {
|
|
111
|
+
return content["application/json"] ? "application/json" : content["*/*"] ? "*/*" : void 0;
|
|
112
|
+
}
|
|
113
|
+
return metadataItems;
|
|
86
114
|
}
|
|
87
115
|
|
|
88
116
|
export {
|
|
@@ -90,9 +118,12 @@ export {
|
|
|
90
118
|
SUPPORTED_HTTP_METHODS,
|
|
91
119
|
createStatusCodesByStrategy,
|
|
92
120
|
readSchema,
|
|
121
|
+
readSchemaContent,
|
|
122
|
+
isRemoteSchema,
|
|
93
123
|
readTemplateFile,
|
|
94
124
|
hasQueryParameter,
|
|
95
125
|
getCliVersion,
|
|
96
126
|
isRequiredRequestBody,
|
|
97
|
-
|
|
127
|
+
getRequestBodyContentType,
|
|
128
|
+
getResponseMetadataItems
|
|
98
129
|
};
|
package/dist/cli.cjs
CHANGED
|
@@ -53,13 +53,29 @@ var init_constants = __esm({
|
|
|
53
53
|
|
|
54
54
|
// src/utils.ts
|
|
55
55
|
async function readSchema(input) {
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
const swaggerOrOpenapiSchema = isYaml ? import_yaml.default.parse(content) : JSON.parse(content);
|
|
56
|
+
const content = await readSchemaContent(input);
|
|
57
|
+
const jsonSchema = (0, import_rattail.tryParseJSON)(content);
|
|
58
|
+
const swaggerOrOpenapiSchema = jsonSchema ? jsonSchema : import_yaml.default.parse(content);
|
|
60
59
|
const schema = swaggerOrOpenapiSchema.swagger ? (await import_swagger2openapi.default.convert(swaggerOrOpenapiSchema, {})).openapi : swaggerOrOpenapiSchema;
|
|
61
60
|
return schema;
|
|
62
61
|
}
|
|
62
|
+
async function readSchemaContent(input) {
|
|
63
|
+
if (isRemoteSchema(input)) {
|
|
64
|
+
try {
|
|
65
|
+
import_rslog.logger.info("Fetching remote schema...");
|
|
66
|
+
const { data } = await (0, import_axle.createAxle)().get(input);
|
|
67
|
+
return JSON.stringify(data);
|
|
68
|
+
} catch {
|
|
69
|
+
throw new Error("Failed to fetch remote schema");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const path = (0, import_path2.resolve)(CWD, input);
|
|
73
|
+
const content = import_fs_extra.default.readFileSync(path, "utf-8");
|
|
74
|
+
return content;
|
|
75
|
+
}
|
|
76
|
+
function isRemoteSchema(path) {
|
|
77
|
+
return path.startsWith("http://") || path.startsWith("https://");
|
|
78
|
+
}
|
|
63
79
|
function readTemplateFile(preset = "axle") {
|
|
64
80
|
if (import_fs_extra.default.existsSync(CUSTOM_TEMPLATE_FILE)) {
|
|
65
81
|
return import_fs_extra.default.readFileSync(CUSTOM_TEMPLATE_FILE, "utf-8");
|
|
@@ -75,26 +91,38 @@ function getCliVersion() {
|
|
|
75
91
|
function isRequiredRequestBody(value) {
|
|
76
92
|
return "required" in value && value.required === true;
|
|
77
93
|
}
|
|
78
|
-
function
|
|
94
|
+
function getRequestBodyContentType(value) {
|
|
95
|
+
if (!("content" in value)) {
|
|
96
|
+
return "";
|
|
97
|
+
}
|
|
98
|
+
return value.content["application/json"] ? "application/json" : value.content["application/x-www-form-urlencoded"] ? "application/x-www-form-urlencoded" : void 0;
|
|
99
|
+
}
|
|
100
|
+
function getResponseMetadataItems(operation, validateStatus) {
|
|
79
101
|
const responses = operation.responses ?? {};
|
|
80
102
|
const validStatusResults = Object.keys(responses).sort((a, b) => Number(a) - Number(b)).filter((key) => validateStatus(Number(key))).map(Number);
|
|
81
|
-
const
|
|
82
|
-
const content = operation.responses?.[status]?.content;
|
|
83
|
-
const
|
|
103
|
+
const metadataItems = validStatusResults.map((status) => {
|
|
104
|
+
const content = operation.responses?.[status]?.content ?? {};
|
|
105
|
+
const responseContentType = findResponseContentType(content);
|
|
84
106
|
return {
|
|
85
107
|
status,
|
|
86
|
-
|
|
108
|
+
responseContentType
|
|
87
109
|
};
|
|
88
|
-
}).filter((result) => result.
|
|
89
|
-
|
|
110
|
+
}).filter((result) => result.responseContentType);
|
|
111
|
+
function findResponseContentType(content) {
|
|
112
|
+
return content["application/json"] ? "application/json" : content["*/*"] ? "*/*" : void 0;
|
|
113
|
+
}
|
|
114
|
+
return metadataItems;
|
|
90
115
|
}
|
|
91
|
-
var import_path2, import_fs_extra, import_swagger2openapi, import_yaml;
|
|
116
|
+
var import_path2, import_axle, import_fs_extra, import_rattail, import_rslog, import_swagger2openapi, import_yaml;
|
|
92
117
|
var init_utils = __esm({
|
|
93
118
|
"src/utils.ts"() {
|
|
94
119
|
"use strict";
|
|
95
120
|
init_cjs_shims();
|
|
96
121
|
import_path2 = require("path");
|
|
122
|
+
import_axle = require("@varlet/axle");
|
|
97
123
|
import_fs_extra = __toESM(require("fs-extra"), 1);
|
|
124
|
+
import_rattail = require("rattail");
|
|
125
|
+
import_rslog = require("rslog");
|
|
98
126
|
import_swagger2openapi = __toESM(require("swagger2openapi"), 1);
|
|
99
127
|
import_yaml = __toESM(require("yaml"), 1);
|
|
100
128
|
init_constants();
|
|
@@ -123,7 +151,7 @@ var init_config = __esm({
|
|
|
123
151
|
|
|
124
152
|
// src/transformer.ts
|
|
125
153
|
function transformModuleName({ name }) {
|
|
126
|
-
return (0,
|
|
154
|
+
return (0, import_rattail2.camelize)(name);
|
|
127
155
|
}
|
|
128
156
|
function transformUrl({ path, base }) {
|
|
129
157
|
return (base ? path.replace(base, "") : path).replace(/{/g, ":").replace(/}/g, "");
|
|
@@ -135,7 +163,7 @@ function transformVerb({ method }) {
|
|
|
135
163
|
case "put":
|
|
136
164
|
return "Update";
|
|
137
165
|
default:
|
|
138
|
-
return (0,
|
|
166
|
+
return (0, import_rattail2.pascalCase)(method);
|
|
139
167
|
}
|
|
140
168
|
}
|
|
141
169
|
function transformEntity({ path, method, base }) {
|
|
@@ -146,7 +174,7 @@ function transformEntity({ path, method, base }) {
|
|
|
146
174
|
return entity;
|
|
147
175
|
}
|
|
148
176
|
word = word.replace(/\.([a-z])/g, (_, p) => p.toUpperCase());
|
|
149
|
-
word = import_pluralize.default.singular((0,
|
|
177
|
+
word = import_pluralize.default.singular((0, import_rattail2.pascalCase)(word));
|
|
150
178
|
if (method === "get" && index === words.length - 1) {
|
|
151
179
|
word = import_pluralize.default.plural(word);
|
|
152
180
|
}
|
|
@@ -177,9 +205,10 @@ function transformTypeRequestBody({
|
|
|
177
205
|
}
|
|
178
206
|
function transformTypeRequestBodyValue({
|
|
179
207
|
type,
|
|
180
|
-
required
|
|
208
|
+
required,
|
|
209
|
+
requestContentType
|
|
181
210
|
}) {
|
|
182
|
-
return required ? `${type}['requestBody']['content']['
|
|
211
|
+
return required ? `${type}['requestBody']['content']['${requestContentType}']` : `NonNullable<${type}['requestBody']>['content']['${requestContentType}'] | undefined`;
|
|
183
212
|
}
|
|
184
213
|
function transformTypeResponseBody({
|
|
185
214
|
type
|
|
@@ -190,7 +219,7 @@ function transformTypeResponseBodyValue({
|
|
|
190
219
|
type,
|
|
191
220
|
responseMetadataItems
|
|
192
221
|
}) {
|
|
193
|
-
return responseMetadataItems.map(({ status,
|
|
222
|
+
return responseMetadataItems.map(({ status, responseContentType }) => `${type}['responses']['${status}']['content']['${responseContentType}']`).join(" | ");
|
|
194
223
|
}
|
|
195
224
|
function createTransformer() {
|
|
196
225
|
return {
|
|
@@ -209,13 +238,13 @@ function createTransformer() {
|
|
|
209
238
|
typeResponseBodyValue: transformTypeResponseBodyValue
|
|
210
239
|
};
|
|
211
240
|
}
|
|
212
|
-
var import_pluralize,
|
|
241
|
+
var import_pluralize, import_rattail2;
|
|
213
242
|
var init_transformer = __esm({
|
|
214
243
|
"src/transformer.ts"() {
|
|
215
244
|
"use strict";
|
|
216
245
|
init_cjs_shims();
|
|
217
246
|
import_pluralize = __toESM(require("pluralize"), 1);
|
|
218
|
-
|
|
247
|
+
import_rattail2 = require("rattail");
|
|
219
248
|
}
|
|
220
249
|
});
|
|
221
250
|
|
|
@@ -235,21 +264,23 @@ function transformPayloads(pathItems, options) {
|
|
|
235
264
|
const args = { path, base, url, method, operation };
|
|
236
265
|
const entity = transformer.entity(args);
|
|
237
266
|
const verb = transformer.verb(args);
|
|
267
|
+
const requestContentType = operation.requestBody ? getRequestBodyContentType(operation.requestBody) : void 0;
|
|
268
|
+
const responseMetadataItems = getResponseMetadataItems(operation, validateStatus);
|
|
238
269
|
const fn = transformer.fn({ ...args, verb, entity });
|
|
239
270
|
const type = transformer.type({ ...args, verb, entity });
|
|
240
271
|
const typeValue = transformer.typeValue({ ...args, verb, entity });
|
|
241
272
|
const typeQuery = transformer.typeQuery({ ...args, type, verb, entity });
|
|
242
273
|
const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue({ ...args, type, verb, entity }) : "undefined";
|
|
243
274
|
const typeRequestBody = transformer.typeRequestBody({ ...args, type, verb, entity });
|
|
244
|
-
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({
|
|
275
|
+
const typeRequestBodyValue = operation.requestBody && requestContentType ? transformer.typeRequestBodyValue({
|
|
245
276
|
...args,
|
|
246
277
|
type,
|
|
247
278
|
verb,
|
|
248
279
|
entity,
|
|
249
|
-
required: isRequiredRequestBody(operation.requestBody)
|
|
280
|
+
required: isRequiredRequestBody(operation.requestBody),
|
|
281
|
+
requestContentType
|
|
250
282
|
}) : "undefined";
|
|
251
283
|
const typeResponseBody = transformer.typeResponseBody({ ...args, type, verb, entity });
|
|
252
|
-
const responseMetadataItems = getValidResponseMetadataItems(operation, validateStatus);
|
|
253
284
|
const typeResponseBodyValue = responseMetadataItems.length > 0 ? transformer.typeResponseBodyValue({ ...args, type, verb, entity, responseMetadataItems }) : "undefined";
|
|
254
285
|
payloads.push({
|
|
255
286
|
fn,
|
|
@@ -257,6 +288,7 @@ function transformPayloads(pathItems, options) {
|
|
|
257
288
|
method,
|
|
258
289
|
verb,
|
|
259
290
|
entity,
|
|
291
|
+
requestContentType,
|
|
260
292
|
type,
|
|
261
293
|
typeValue,
|
|
262
294
|
typeQuery,
|
|
@@ -273,7 +305,7 @@ function partitionApiModules(schema, options) {
|
|
|
273
305
|
const { base, transformer, validateStatus } = options;
|
|
274
306
|
const schemaPaths = schema.paths ?? {};
|
|
275
307
|
const schemaPathKeys = base ? Object.keys(schemaPaths).map((key) => key.replace(base, "")) : Object.keys(schemaPaths);
|
|
276
|
-
const keyToPaths = (0,
|
|
308
|
+
const keyToPaths = (0, import_rattail3.groupBy)(schemaPathKeys, (key) => key.split("/")[1]);
|
|
277
309
|
const apiModules = Object.entries(keyToPaths).reduce((apiModules2, [name, paths]) => {
|
|
278
310
|
const payloads = paths.reduce((payloads2, path) => {
|
|
279
311
|
const pathItems = schemaPaths[path];
|
|
@@ -307,14 +339,14 @@ function renderApiModules(apiModules, options) {
|
|
|
307
339
|
printWidth: 120
|
|
308
340
|
}).then((content) => {
|
|
309
341
|
const path = (0, import_path3.resolve)(output, `${apiModule.name}.${ts ? "ts" : "js"}`);
|
|
310
|
-
const shouldSkip = (!overrides || (0,
|
|
342
|
+
const shouldSkip = (!overrides || (0, import_rattail3.isArray)(overrides) && !overrides.includes(apiModule.name)) && import_fs_extra2.default.existsSync(path);
|
|
311
343
|
if (shouldSkip) {
|
|
312
|
-
|
|
344
|
+
import_rslog2.logger.warn(`File already exists, skip: ${path}`);
|
|
313
345
|
promiseResolve(content);
|
|
314
346
|
return;
|
|
315
347
|
}
|
|
316
348
|
import_fs_extra2.default.outputFileSync(path, content);
|
|
317
|
-
|
|
349
|
+
import_rslog2.logger.success(`Generated ${path}`);
|
|
318
350
|
promiseResolve(content);
|
|
319
351
|
});
|
|
320
352
|
})
|
|
@@ -326,11 +358,11 @@ async function generateTypes(schema, output, typesFilename) {
|
|
|
326
358
|
const contents = (0, import_openapi_typescript.astToString)(ast);
|
|
327
359
|
const typesFilepath = (0, import_path3.resolve)(CWD, output, typesFilename);
|
|
328
360
|
import_fs_extra2.default.outputFileSync(typesFilepath, contents);
|
|
329
|
-
|
|
361
|
+
import_rslog2.logger.success(`Generated ${typesFilepath}`);
|
|
330
362
|
}
|
|
331
363
|
async function generate(userOptions = {}) {
|
|
332
364
|
const config = await getConfig();
|
|
333
|
-
const options = (0,
|
|
365
|
+
const options = (0, import_rattail3.merge)(config, userOptions);
|
|
334
366
|
const {
|
|
335
367
|
base,
|
|
336
368
|
ts = true,
|
|
@@ -345,7 +377,7 @@ async function generate(userOptions = {}) {
|
|
|
345
377
|
} = options;
|
|
346
378
|
const mergedTransformer = { ...createTransformer(), ...transformer };
|
|
347
379
|
const schema = await readSchema(input);
|
|
348
|
-
|
|
380
|
+
import_rslog2.logger.info("Generating API modules...");
|
|
349
381
|
if (ts) {
|
|
350
382
|
await generateTypes(schema, output, typesFilename);
|
|
351
383
|
}
|
|
@@ -355,9 +387,9 @@ async function generate(userOptions = {}) {
|
|
|
355
387
|
validateStatus
|
|
356
388
|
});
|
|
357
389
|
await renderApiModules(apiModules, { output, typesFilename, ts, typesOnly, overrides, preset });
|
|
358
|
-
|
|
390
|
+
import_rslog2.logger.success("Done");
|
|
359
391
|
}
|
|
360
|
-
var import_path3, import_ejs, import_fs_extra2, import_openapi_typescript, import_prettier,
|
|
392
|
+
var import_path3, import_ejs, import_fs_extra2, import_openapi_typescript, import_prettier, import_rattail3, import_rslog2;
|
|
361
393
|
var init_generate = __esm({
|
|
362
394
|
"src/generate.ts"() {
|
|
363
395
|
"use strict";
|
|
@@ -367,8 +399,8 @@ var init_generate = __esm({
|
|
|
367
399
|
import_fs_extra2 = __toESM(require("fs-extra"), 1);
|
|
368
400
|
import_openapi_typescript = __toESM(require("openapi-typescript"), 1);
|
|
369
401
|
import_prettier = __toESM(require("prettier"), 1);
|
|
370
|
-
|
|
371
|
-
|
|
402
|
+
import_rattail3 = require("rattail");
|
|
403
|
+
import_rslog2 = require("rslog");
|
|
372
404
|
init_config();
|
|
373
405
|
init_constants();
|
|
374
406
|
init_transformer();
|
package/dist/cli.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
getCliVersion
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-PTGO2Y5X.js";
|
|
5
5
|
|
|
6
6
|
// src/cli.ts
|
|
7
7
|
import { Command } from "commander";
|
|
8
8
|
var program = new Command();
|
|
9
9
|
program.version(getCliVersion());
|
|
10
10
|
program.action(async () => {
|
|
11
|
-
const { generate } = await import("./generate-
|
|
11
|
+
const { generate } = await import("./generate-HKA2M6ER.js");
|
|
12
12
|
return generate();
|
|
13
13
|
});
|
|
14
14
|
program.parse();
|
package/dist/index.cjs
CHANGED
|
@@ -37,12 +37,15 @@ __export(index_exports, {
|
|
|
37
37
|
generateTypes: () => generateTypes,
|
|
38
38
|
getCliVersion: () => getCliVersion,
|
|
39
39
|
getConfig: () => getConfig,
|
|
40
|
-
|
|
40
|
+
getRequestBodyContentType: () => getRequestBodyContentType,
|
|
41
|
+
getResponseMetadataItems: () => getResponseMetadataItems,
|
|
41
42
|
hasQueryParameter: () => hasQueryParameter,
|
|
43
|
+
isRemoteSchema: () => isRemoteSchema,
|
|
42
44
|
isRequiredRequestBody: () => isRequiredRequestBody,
|
|
43
45
|
partitionApiModules: () => partitionApiModules,
|
|
44
46
|
pluralize: () => import_pluralize2.default,
|
|
45
47
|
readSchema: () => readSchema,
|
|
48
|
+
readSchemaContent: () => readSchemaContent,
|
|
46
49
|
readTemplateFile: () => readTemplateFile,
|
|
47
50
|
renderApiModules: () => renderApiModules,
|
|
48
51
|
transformEntity: () => transformEntity,
|
|
@@ -120,9 +123,10 @@ function transformTypeRequestBody({
|
|
|
120
123
|
}
|
|
121
124
|
function transformTypeRequestBodyValue({
|
|
122
125
|
type,
|
|
123
|
-
required
|
|
126
|
+
required,
|
|
127
|
+
requestContentType
|
|
124
128
|
}) {
|
|
125
|
-
return required ? `${type}['requestBody']['content']['
|
|
129
|
+
return required ? `${type}['requestBody']['content']['${requestContentType}']` : `NonNullable<${type}['requestBody']>['content']['${requestContentType}'] | undefined`;
|
|
126
130
|
}
|
|
127
131
|
function transformTypeResponseBody({
|
|
128
132
|
type
|
|
@@ -133,7 +137,7 @@ function transformTypeResponseBodyValue({
|
|
|
133
137
|
type,
|
|
134
138
|
responseMetadataItems
|
|
135
139
|
}) {
|
|
136
|
-
return responseMetadataItems.map(({ status,
|
|
140
|
+
return responseMetadataItems.map(({ status, responseContentType }) => `${type}['responses']['${status}']['content']['${responseContentType}']`).join(" | ");
|
|
137
141
|
}
|
|
138
142
|
function createTransformer() {
|
|
139
143
|
return {
|
|
@@ -159,8 +163,8 @@ var import_ejs = __toESM(require("ejs"), 1);
|
|
|
159
163
|
var import_fs_extra2 = __toESM(require("fs-extra"), 1);
|
|
160
164
|
var import_openapi_typescript = __toESM(require("openapi-typescript"), 1);
|
|
161
165
|
var import_prettier = __toESM(require("prettier"), 1);
|
|
162
|
-
var
|
|
163
|
-
var
|
|
166
|
+
var import_rattail3 = require("rattail");
|
|
167
|
+
var import_rslog2 = require("rslog");
|
|
164
168
|
|
|
165
169
|
// src/config.ts
|
|
166
170
|
var import_unconfig = require("unconfig");
|
|
@@ -187,7 +191,10 @@ var SUPPORTED_HTTP_METHODS = ["get", "post", "put", "delete", "patch", "options"
|
|
|
187
191
|
|
|
188
192
|
// src/utils.ts
|
|
189
193
|
var import_path2 = require("path");
|
|
194
|
+
var import_axle = require("@varlet/axle");
|
|
190
195
|
var import_fs_extra = __toESM(require("fs-extra"), 1);
|
|
196
|
+
var import_rattail2 = require("rattail");
|
|
197
|
+
var import_rslog = require("rslog");
|
|
191
198
|
var import_swagger2openapi = __toESM(require("swagger2openapi"), 1);
|
|
192
199
|
var import_yaml = __toESM(require("yaml"), 1);
|
|
193
200
|
function createStatusCodesByStrategy(strategy) {
|
|
@@ -222,13 +229,29 @@ function createStatusCodesByStrategy(strategy) {
|
|
|
222
229
|
}[strategy];
|
|
223
230
|
}
|
|
224
231
|
async function readSchema(input) {
|
|
225
|
-
const
|
|
226
|
-
const
|
|
227
|
-
const
|
|
228
|
-
const swaggerOrOpenapiSchema = isYaml ? import_yaml.default.parse(content) : JSON.parse(content);
|
|
232
|
+
const content = await readSchemaContent(input);
|
|
233
|
+
const jsonSchema = (0, import_rattail2.tryParseJSON)(content);
|
|
234
|
+
const swaggerOrOpenapiSchema = jsonSchema ? jsonSchema : import_yaml.default.parse(content);
|
|
229
235
|
const schema = swaggerOrOpenapiSchema.swagger ? (await import_swagger2openapi.default.convert(swaggerOrOpenapiSchema, {})).openapi : swaggerOrOpenapiSchema;
|
|
230
236
|
return schema;
|
|
231
237
|
}
|
|
238
|
+
async function readSchemaContent(input) {
|
|
239
|
+
if (isRemoteSchema(input)) {
|
|
240
|
+
try {
|
|
241
|
+
import_rslog.logger.info("Fetching remote schema...");
|
|
242
|
+
const { data } = await (0, import_axle.createAxle)().get(input);
|
|
243
|
+
return JSON.stringify(data);
|
|
244
|
+
} catch {
|
|
245
|
+
throw new Error("Failed to fetch remote schema");
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const path = (0, import_path2.resolve)(CWD, input);
|
|
249
|
+
const content = import_fs_extra.default.readFileSync(path, "utf-8");
|
|
250
|
+
return content;
|
|
251
|
+
}
|
|
252
|
+
function isRemoteSchema(path) {
|
|
253
|
+
return path.startsWith("http://") || path.startsWith("https://");
|
|
254
|
+
}
|
|
232
255
|
function readTemplateFile(preset = "axle") {
|
|
233
256
|
if (import_fs_extra.default.existsSync(CUSTOM_TEMPLATE_FILE)) {
|
|
234
257
|
return import_fs_extra.default.readFileSync(CUSTOM_TEMPLATE_FILE, "utf-8");
|
|
@@ -244,18 +267,27 @@ function getCliVersion() {
|
|
|
244
267
|
function isRequiredRequestBody(value) {
|
|
245
268
|
return "required" in value && value.required === true;
|
|
246
269
|
}
|
|
247
|
-
function
|
|
270
|
+
function getRequestBodyContentType(value) {
|
|
271
|
+
if (!("content" in value)) {
|
|
272
|
+
return "";
|
|
273
|
+
}
|
|
274
|
+
return value.content["application/json"] ? "application/json" : value.content["application/x-www-form-urlencoded"] ? "application/x-www-form-urlencoded" : void 0;
|
|
275
|
+
}
|
|
276
|
+
function getResponseMetadataItems(operation, validateStatus) {
|
|
248
277
|
const responses = operation.responses ?? {};
|
|
249
278
|
const validStatusResults = Object.keys(responses).sort((a, b) => Number(a) - Number(b)).filter((key) => validateStatus(Number(key))).map(Number);
|
|
250
|
-
const
|
|
251
|
-
const content = operation.responses?.[status]?.content;
|
|
252
|
-
const
|
|
279
|
+
const metadataItems = validStatusResults.map((status) => {
|
|
280
|
+
const content = operation.responses?.[status]?.content ?? {};
|
|
281
|
+
const responseContentType = findResponseContentType(content);
|
|
253
282
|
return {
|
|
254
283
|
status,
|
|
255
|
-
|
|
284
|
+
responseContentType
|
|
256
285
|
};
|
|
257
|
-
}).filter((result) => result.
|
|
258
|
-
|
|
286
|
+
}).filter((result) => result.responseContentType);
|
|
287
|
+
function findResponseContentType(content) {
|
|
288
|
+
return content["application/json"] ? "application/json" : content["*/*"] ? "*/*" : void 0;
|
|
289
|
+
}
|
|
290
|
+
return metadataItems;
|
|
259
291
|
}
|
|
260
292
|
|
|
261
293
|
// src/generate.ts
|
|
@@ -266,21 +298,23 @@ function transformPayloads(pathItems, options) {
|
|
|
266
298
|
const args = { path, base, url, method, operation };
|
|
267
299
|
const entity = transformer.entity(args);
|
|
268
300
|
const verb = transformer.verb(args);
|
|
301
|
+
const requestContentType = operation.requestBody ? getRequestBodyContentType(operation.requestBody) : void 0;
|
|
302
|
+
const responseMetadataItems = getResponseMetadataItems(operation, validateStatus);
|
|
269
303
|
const fn = transformer.fn({ ...args, verb, entity });
|
|
270
304
|
const type = transformer.type({ ...args, verb, entity });
|
|
271
305
|
const typeValue = transformer.typeValue({ ...args, verb, entity });
|
|
272
306
|
const typeQuery = transformer.typeQuery({ ...args, type, verb, entity });
|
|
273
307
|
const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue({ ...args, type, verb, entity }) : "undefined";
|
|
274
308
|
const typeRequestBody = transformer.typeRequestBody({ ...args, type, verb, entity });
|
|
275
|
-
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({
|
|
309
|
+
const typeRequestBodyValue = operation.requestBody && requestContentType ? transformer.typeRequestBodyValue({
|
|
276
310
|
...args,
|
|
277
311
|
type,
|
|
278
312
|
verb,
|
|
279
313
|
entity,
|
|
280
|
-
required: isRequiredRequestBody(operation.requestBody)
|
|
314
|
+
required: isRequiredRequestBody(operation.requestBody),
|
|
315
|
+
requestContentType
|
|
281
316
|
}) : "undefined";
|
|
282
317
|
const typeResponseBody = transformer.typeResponseBody({ ...args, type, verb, entity });
|
|
283
|
-
const responseMetadataItems = getValidResponseMetadataItems(operation, validateStatus);
|
|
284
318
|
const typeResponseBodyValue = responseMetadataItems.length > 0 ? transformer.typeResponseBodyValue({ ...args, type, verb, entity, responseMetadataItems }) : "undefined";
|
|
285
319
|
payloads.push({
|
|
286
320
|
fn,
|
|
@@ -288,6 +322,7 @@ function transformPayloads(pathItems, options) {
|
|
|
288
322
|
method,
|
|
289
323
|
verb,
|
|
290
324
|
entity,
|
|
325
|
+
requestContentType,
|
|
291
326
|
type,
|
|
292
327
|
typeValue,
|
|
293
328
|
typeQuery,
|
|
@@ -304,7 +339,7 @@ function partitionApiModules(schema, options) {
|
|
|
304
339
|
const { base, transformer, validateStatus } = options;
|
|
305
340
|
const schemaPaths = schema.paths ?? {};
|
|
306
341
|
const schemaPathKeys = base ? Object.keys(schemaPaths).map((key) => key.replace(base, "")) : Object.keys(schemaPaths);
|
|
307
|
-
const keyToPaths = (0,
|
|
342
|
+
const keyToPaths = (0, import_rattail3.groupBy)(schemaPathKeys, (key) => key.split("/")[1]);
|
|
308
343
|
const apiModules = Object.entries(keyToPaths).reduce((apiModules2, [name, paths]) => {
|
|
309
344
|
const payloads = paths.reduce((payloads2, path) => {
|
|
310
345
|
const pathItems = schemaPaths[path];
|
|
@@ -338,14 +373,14 @@ function renderApiModules(apiModules, options) {
|
|
|
338
373
|
printWidth: 120
|
|
339
374
|
}).then((content) => {
|
|
340
375
|
const path = (0, import_path3.resolve)(output, `${apiModule.name}.${ts ? "ts" : "js"}`);
|
|
341
|
-
const shouldSkip = (!overrides || (0,
|
|
376
|
+
const shouldSkip = (!overrides || (0, import_rattail3.isArray)(overrides) && !overrides.includes(apiModule.name)) && import_fs_extra2.default.existsSync(path);
|
|
342
377
|
if (shouldSkip) {
|
|
343
|
-
|
|
378
|
+
import_rslog2.logger.warn(`File already exists, skip: ${path}`);
|
|
344
379
|
promiseResolve(content);
|
|
345
380
|
return;
|
|
346
381
|
}
|
|
347
382
|
import_fs_extra2.default.outputFileSync(path, content);
|
|
348
|
-
|
|
383
|
+
import_rslog2.logger.success(`Generated ${path}`);
|
|
349
384
|
promiseResolve(content);
|
|
350
385
|
});
|
|
351
386
|
})
|
|
@@ -357,11 +392,11 @@ async function generateTypes(schema, output, typesFilename) {
|
|
|
357
392
|
const contents = (0, import_openapi_typescript.astToString)(ast);
|
|
358
393
|
const typesFilepath = (0, import_path3.resolve)(CWD, output, typesFilename);
|
|
359
394
|
import_fs_extra2.default.outputFileSync(typesFilepath, contents);
|
|
360
|
-
|
|
395
|
+
import_rslog2.logger.success(`Generated ${typesFilepath}`);
|
|
361
396
|
}
|
|
362
397
|
async function generate(userOptions = {}) {
|
|
363
398
|
const config = await getConfig();
|
|
364
|
-
const options = (0,
|
|
399
|
+
const options = (0, import_rattail3.merge)(config, userOptions);
|
|
365
400
|
const {
|
|
366
401
|
base,
|
|
367
402
|
ts = true,
|
|
@@ -376,7 +411,7 @@ async function generate(userOptions = {}) {
|
|
|
376
411
|
} = options;
|
|
377
412
|
const mergedTransformer = { ...createTransformer(), ...transformer };
|
|
378
413
|
const schema = await readSchema(input);
|
|
379
|
-
|
|
414
|
+
import_rslog2.logger.info("Generating API modules...");
|
|
380
415
|
if (ts) {
|
|
381
416
|
await generateTypes(schema, output, typesFilename);
|
|
382
417
|
}
|
|
@@ -386,7 +421,7 @@ async function generate(userOptions = {}) {
|
|
|
386
421
|
validateStatus
|
|
387
422
|
});
|
|
388
423
|
await renderApiModules(apiModules, { output, typesFilename, ts, typesOnly, overrides, preset });
|
|
389
|
-
|
|
424
|
+
import_rslog2.logger.success("Done");
|
|
390
425
|
}
|
|
391
426
|
|
|
392
427
|
// src/index.ts
|
|
@@ -400,12 +435,15 @@ var import_pluralize2 = __toESM(require("pluralize"), 1);
|
|
|
400
435
|
generateTypes,
|
|
401
436
|
getCliVersion,
|
|
402
437
|
getConfig,
|
|
403
|
-
|
|
438
|
+
getRequestBodyContentType,
|
|
439
|
+
getResponseMetadataItems,
|
|
404
440
|
hasQueryParameter,
|
|
441
|
+
isRemoteSchema,
|
|
405
442
|
isRequiredRequestBody,
|
|
406
443
|
partitionApiModules,
|
|
407
444
|
pluralize,
|
|
408
445
|
readSchema,
|
|
446
|
+
readSchemaContent,
|
|
409
447
|
readTemplateFile,
|
|
410
448
|
renderApiModules,
|
|
411
449
|
transformEntity,
|
package/dist/index.d.cts
CHANGED
|
@@ -38,15 +38,18 @@ declare function createStatusCodesByStrategy(strategy: StatusCodeStrategy): {
|
|
|
38
38
|
head: number;
|
|
39
39
|
};
|
|
40
40
|
declare function readSchema(input: string): Promise<OpenAPI3>;
|
|
41
|
+
declare function readSchemaContent(input: string): Promise<string>;
|
|
42
|
+
declare function isRemoteSchema(path: string): boolean;
|
|
41
43
|
declare function readTemplateFile(preset?: Preset): string;
|
|
42
44
|
declare function hasQueryParameter(operation: OperationObject): boolean;
|
|
43
45
|
declare function getCliVersion(): any;
|
|
44
46
|
declare function isRequiredRequestBody(value: RequestBodyObject | ReferenceObject): boolean;
|
|
47
|
+
declare function getRequestBodyContentType(value: RequestBodyObject | ReferenceObject): "" | "application/json" | "application/x-www-form-urlencoded" | undefined;
|
|
45
48
|
type ResponseMetadataItem = {
|
|
46
49
|
status: number;
|
|
47
|
-
|
|
50
|
+
responseContentType: string;
|
|
48
51
|
};
|
|
49
|
-
declare function
|
|
52
|
+
declare function getResponseMetadataItems(operation: OperationObject, validateStatus: (status: number) => boolean): ResponseMetadataItem[];
|
|
50
53
|
|
|
51
54
|
type TransformerBaseArgs = {
|
|
52
55
|
path: string;
|
|
@@ -93,11 +96,12 @@ declare function transformTypeRequestBody({ type, }: {
|
|
|
93
96
|
verb: string;
|
|
94
97
|
entity: string;
|
|
95
98
|
} & TransformerBaseArgs): string;
|
|
96
|
-
declare function transformTypeRequestBodyValue({ type, required, }: {
|
|
99
|
+
declare function transformTypeRequestBodyValue({ type, required, requestContentType, }: {
|
|
97
100
|
type: string;
|
|
98
101
|
verb: string;
|
|
99
102
|
entity: string;
|
|
100
103
|
required: boolean;
|
|
104
|
+
requestContentType: string;
|
|
101
105
|
} & TransformerBaseArgs): string;
|
|
102
106
|
declare function transformTypeResponseBody({ type, }: {
|
|
103
107
|
type: string;
|
|
@@ -176,6 +180,10 @@ interface ApiModulePayload {
|
|
|
176
180
|
* The entity name of the API endpoint, such as User, Comment, Post, etc.
|
|
177
181
|
*/
|
|
178
182
|
entity: string;
|
|
183
|
+
/**
|
|
184
|
+
* The request content type of the API endpoint, such as 'application/json', 'application/x-www-form-urlencoded'.
|
|
185
|
+
*/
|
|
186
|
+
requestContentType?: string;
|
|
179
187
|
/**
|
|
180
188
|
* The type name of the API endpoint, such as ApiGetUsers, ApiCreatePost, ApiUpdateComment, etc.
|
|
181
189
|
*/
|
|
@@ -285,4 +293,4 @@ type Config = GenerateOptions;
|
|
|
285
293
|
declare function defineConfig(config: Config): GenerateOptions;
|
|
286
294
|
declare function getConfig(): Promise<Config>;
|
|
287
295
|
|
|
288
|
-
export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type ResponseMetadataItem, type StatusCodeStrategy, type StatusCodes, type Transformer, type TransformerBaseArgs, createStatusCodesByStrategy, createTransformer, defineConfig, generate, generateTypes, getCliVersion, getConfig,
|
|
296
|
+
export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type ResponseMetadataItem, type StatusCodeStrategy, type StatusCodes, type Transformer, type TransformerBaseArgs, createStatusCodesByStrategy, createTransformer, defineConfig, generate, generateTypes, getCliVersion, getConfig, getRequestBodyContentType, getResponseMetadataItems, hasQueryParameter, isRemoteSchema, isRequiredRequestBody, partitionApiModules, readSchema, readSchemaContent, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
|
package/dist/index.d.ts
CHANGED
|
@@ -38,15 +38,18 @@ declare function createStatusCodesByStrategy(strategy: StatusCodeStrategy): {
|
|
|
38
38
|
head: number;
|
|
39
39
|
};
|
|
40
40
|
declare function readSchema(input: string): Promise<OpenAPI3>;
|
|
41
|
+
declare function readSchemaContent(input: string): Promise<string>;
|
|
42
|
+
declare function isRemoteSchema(path: string): boolean;
|
|
41
43
|
declare function readTemplateFile(preset?: Preset): string;
|
|
42
44
|
declare function hasQueryParameter(operation: OperationObject): boolean;
|
|
43
45
|
declare function getCliVersion(): any;
|
|
44
46
|
declare function isRequiredRequestBody(value: RequestBodyObject | ReferenceObject): boolean;
|
|
47
|
+
declare function getRequestBodyContentType(value: RequestBodyObject | ReferenceObject): "" | "application/json" | "application/x-www-form-urlencoded" | undefined;
|
|
45
48
|
type ResponseMetadataItem = {
|
|
46
49
|
status: number;
|
|
47
|
-
|
|
50
|
+
responseContentType: string;
|
|
48
51
|
};
|
|
49
|
-
declare function
|
|
52
|
+
declare function getResponseMetadataItems(operation: OperationObject, validateStatus: (status: number) => boolean): ResponseMetadataItem[];
|
|
50
53
|
|
|
51
54
|
type TransformerBaseArgs = {
|
|
52
55
|
path: string;
|
|
@@ -93,11 +96,12 @@ declare function transformTypeRequestBody({ type, }: {
|
|
|
93
96
|
verb: string;
|
|
94
97
|
entity: string;
|
|
95
98
|
} & TransformerBaseArgs): string;
|
|
96
|
-
declare function transformTypeRequestBodyValue({ type, required, }: {
|
|
99
|
+
declare function transformTypeRequestBodyValue({ type, required, requestContentType, }: {
|
|
97
100
|
type: string;
|
|
98
101
|
verb: string;
|
|
99
102
|
entity: string;
|
|
100
103
|
required: boolean;
|
|
104
|
+
requestContentType: string;
|
|
101
105
|
} & TransformerBaseArgs): string;
|
|
102
106
|
declare function transformTypeResponseBody({ type, }: {
|
|
103
107
|
type: string;
|
|
@@ -176,6 +180,10 @@ interface ApiModulePayload {
|
|
|
176
180
|
* The entity name of the API endpoint, such as User, Comment, Post, etc.
|
|
177
181
|
*/
|
|
178
182
|
entity: string;
|
|
183
|
+
/**
|
|
184
|
+
* The request content type of the API endpoint, such as 'application/json', 'application/x-www-form-urlencoded'.
|
|
185
|
+
*/
|
|
186
|
+
requestContentType?: string;
|
|
179
187
|
/**
|
|
180
188
|
* The type name of the API endpoint, such as ApiGetUsers, ApiCreatePost, ApiUpdateComment, etc.
|
|
181
189
|
*/
|
|
@@ -285,4 +293,4 @@ type Config = GenerateOptions;
|
|
|
285
293
|
declare function defineConfig(config: Config): GenerateOptions;
|
|
286
294
|
declare function getConfig(): Promise<Config>;
|
|
287
295
|
|
|
288
|
-
export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type ResponseMetadataItem, type StatusCodeStrategy, type StatusCodes, type Transformer, type TransformerBaseArgs, createStatusCodesByStrategy, createTransformer, defineConfig, generate, generateTypes, getCliVersion, getConfig,
|
|
296
|
+
export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type ResponseMetadataItem, type StatusCodeStrategy, type StatusCodes, type Transformer, type TransformerBaseArgs, createStatusCodesByStrategy, createTransformer, defineConfig, generate, generateTypes, getCliVersion, getConfig, getRequestBodyContentType, getResponseMetadataItems, hasQueryParameter, isRemoteSchema, isRequiredRequestBody, partitionApiModules, readSchema, readSchemaContent, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
|
package/dist/index.js
CHANGED
|
@@ -20,16 +20,19 @@ import {
|
|
|
20
20
|
transformTypeValue,
|
|
21
21
|
transformUrl,
|
|
22
22
|
transformVerb
|
|
23
|
-
} from "./chunk-
|
|
23
|
+
} from "./chunk-GMB5TIVL.js";
|
|
24
24
|
import {
|
|
25
25
|
createStatusCodesByStrategy,
|
|
26
26
|
getCliVersion,
|
|
27
|
-
|
|
27
|
+
getRequestBodyContentType,
|
|
28
|
+
getResponseMetadataItems,
|
|
28
29
|
hasQueryParameter,
|
|
30
|
+
isRemoteSchema,
|
|
29
31
|
isRequiredRequestBody,
|
|
30
32
|
readSchema,
|
|
33
|
+
readSchemaContent,
|
|
31
34
|
readTemplateFile
|
|
32
|
-
} from "./chunk-
|
|
35
|
+
} from "./chunk-PTGO2Y5X.js";
|
|
33
36
|
|
|
34
37
|
// src/index.ts
|
|
35
38
|
import { default as default2 } from "pluralize";
|
|
@@ -41,12 +44,15 @@ export {
|
|
|
41
44
|
generateTypes,
|
|
42
45
|
getCliVersion,
|
|
43
46
|
getConfig,
|
|
44
|
-
|
|
47
|
+
getRequestBodyContentType,
|
|
48
|
+
getResponseMetadataItems,
|
|
45
49
|
hasQueryParameter,
|
|
50
|
+
isRemoteSchema,
|
|
46
51
|
isRequiredRequestBody,
|
|
47
52
|
partitionApiModules,
|
|
48
53
|
default2 as pluralize,
|
|
49
54
|
readSchema,
|
|
55
|
+
readSchemaContent,
|
|
50
56
|
readTemplateFile,
|
|
51
57
|
renderApiModules,
|
|
52
58
|
transformEntity,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-farmer",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.21",
|
|
4
4
|
"description": "API module generation tool based on Openapi3/Swagger2.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"@types/pluralize": "^0.0.33",
|
|
52
|
-
"@varlet/axle": "^0.10.
|
|
52
|
+
"@varlet/axle": "^0.10.3",
|
|
53
53
|
"commander": "^13.0.0",
|
|
54
54
|
"ejs": "^3.1.10",
|
|
55
55
|
"fs-extra": "^11.2.0",
|
package/templates/axios.ejs
CHANGED
|
@@ -4,11 +4,16 @@
|
|
|
4
4
|
|
|
5
5
|
<% if (!typesOnly) { %>
|
|
6
6
|
<% apiModule.payloads.forEach(payload => { %> -%>
|
|
7
|
-
export const <%- payload.fn %> = (config<% if (ts) { %>: RequestConfig<<%- payload.typeRequestBody %>> <% } %> = {})
|
|
8
|
-
=> request<% if (ts) {
|
|
7
|
+
export const <%- payload.fn %> = (config<% if (ts) { %>: RequestConfig<<%- payload.typeQuery %>, <%- payload.typeRequestBody %>> <% } %> = {})
|
|
8
|
+
=> request<% if (ts) { %><<%- payload.typeResponseBody %>><% } %>({
|
|
9
9
|
url: '<%- payload.url %>',
|
|
10
10
|
method: '<%- payload.method %>',
|
|
11
|
-
...config
|
|
11
|
+
...config,
|
|
12
|
+
<% if (payload.requestContentType === 'application/x-www-form-urlencoded') { %>headers: {
|
|
13
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
14
|
+
...config.headers,
|
|
15
|
+
},
|
|
16
|
+
<% } %>
|
|
12
17
|
})
|
|
13
18
|
|
|
14
19
|
<% }) %>
|
package/templates/axle.ejs
CHANGED
|
@@ -10,9 +10,11 @@ export const <%- payload.fn %> = api
|
|
|
10
10
|
<%- payload.typeQuery %>
|
|
11
11
|
<% } else { %>
|
|
12
12
|
<%- payload.typeRequestBody %>
|
|
13
|
-
<% } %>,
|
|
13
|
+
<% } %>,
|
|
14
|
+
<%- payload.typeRequestBody %>>
|
|
14
15
|
<% } %>
|
|
15
|
-
('<%- payload.url %>',
|
|
16
|
+
('<%- payload.url %>',
|
|
17
|
+
<% if (payload.requestContentType === 'application/x-www-form-urlencoded' && payload.method === 'post') { %> 'postUrlEncode'<% } else { %> '<%- payload.method %>' <% } %>)
|
|
16
18
|
|
|
17
19
|
<% }) %>
|
|
18
20
|
<% } %>
|