api-farmer 0.1.2 → 0.1.4
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 +1 -1
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +13 -0
- package/dist/generate-C4lplT2F.mjs +320 -0
- package/dist/index.d.mts +377 -0
- package/dist/index.mjs +4 -0
- package/dist/utils-DachPo4u.mjs +119 -0
- package/package.json +24 -42
- package/dist/chunk-2227C45C.js +0 -131
- package/dist/chunk-FOIQWD3Q.js +0 -332
- package/dist/cli.cjs +0 -465
- package/dist/cli.d.cts +0 -1
- package/dist/cli.d.ts +0 -1
- package/dist/cli.js +0 -14
- package/dist/generate-K6NGTXOH.js +0 -15
- package/dist/index.cjs +0 -515
- package/dist/index.d.cts +0 -327
- package/dist/index.d.ts +0 -327
- package/dist/index.js +0 -76
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import pluralize from "pluralize";
|
|
2
|
+
import { OpenAPI3, OpenAPITSOptions, OperationObject, ReferenceObject, RequestBodyObject } from "openapi-typescript";
|
|
3
|
+
|
|
4
|
+
//#region src/utils.d.ts
|
|
5
|
+
type Preset = 'axle' | 'axios';
|
|
6
|
+
type StatusCodeStrategy = 'strict' | 'loose' | 'smart';
|
|
7
|
+
interface StatusCodes {
|
|
8
|
+
get?: number;
|
|
9
|
+
post?: number;
|
|
10
|
+
put?: number;
|
|
11
|
+
delete?: number;
|
|
12
|
+
patch?: number;
|
|
13
|
+
options?: number;
|
|
14
|
+
head?: number;
|
|
15
|
+
}
|
|
16
|
+
declare function createStatusCodesByStrategy(strategy: StatusCodeStrategy): {
|
|
17
|
+
get: number;
|
|
18
|
+
post: number;
|
|
19
|
+
put: number;
|
|
20
|
+
delete: number;
|
|
21
|
+
patch: number;
|
|
22
|
+
options: number;
|
|
23
|
+
head: number;
|
|
24
|
+
} | {
|
|
25
|
+
get: number;
|
|
26
|
+
post: number;
|
|
27
|
+
put: number;
|
|
28
|
+
delete: number;
|
|
29
|
+
patch: number;
|
|
30
|
+
options: number;
|
|
31
|
+
head: number;
|
|
32
|
+
} | {
|
|
33
|
+
get: number;
|
|
34
|
+
post: number;
|
|
35
|
+
put: number;
|
|
36
|
+
delete: number;
|
|
37
|
+
patch: number;
|
|
38
|
+
options: number;
|
|
39
|
+
head: number;
|
|
40
|
+
};
|
|
41
|
+
declare function readSchema(input: string): Promise<OpenAPI3>;
|
|
42
|
+
declare function getSchemaNode(schema: OpenAPI3, path: string): any;
|
|
43
|
+
declare function readSchemaContent(input: string): Promise<string>;
|
|
44
|
+
declare function isRemoteSchema(path: string): boolean;
|
|
45
|
+
declare function readTemplateFile(preset?: Preset): string;
|
|
46
|
+
declare function getCliVersion(): any;
|
|
47
|
+
declare function isRequiredRequestBody(value: RequestBodyObject | ReferenceObject): boolean;
|
|
48
|
+
declare function findObjectKey(object: Record<string, any>, targetKeys: string[]): string | undefined;
|
|
49
|
+
declare function getRequestBodyContentType(value: RequestBodyObject | ReferenceObject): string | undefined;
|
|
50
|
+
type ResponseMetadataItem = {
|
|
51
|
+
status: number;
|
|
52
|
+
responseContentType: string;
|
|
53
|
+
};
|
|
54
|
+
declare function getResponseMetadataItems(operation: OperationObject, validateStatus: (status: number) => boolean): ResponseMetadataItem[];
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/transformer.d.ts
|
|
57
|
+
type TransformerBaseArgs = {
|
|
58
|
+
path: string;
|
|
59
|
+
fullPath: string;
|
|
60
|
+
base: string | undefined;
|
|
61
|
+
url: string;
|
|
62
|
+
method: string;
|
|
63
|
+
operation: OperationObject;
|
|
64
|
+
uncountableNouns: string[];
|
|
65
|
+
};
|
|
66
|
+
declare function transformModuleName({
|
|
67
|
+
name
|
|
68
|
+
}: {
|
|
69
|
+
name: string;
|
|
70
|
+
}): string;
|
|
71
|
+
declare function transformUrl({
|
|
72
|
+
path
|
|
73
|
+
}: {
|
|
74
|
+
path: string;
|
|
75
|
+
fullPath: string;
|
|
76
|
+
base: string | undefined;
|
|
77
|
+
}): string;
|
|
78
|
+
declare function transformComment({
|
|
79
|
+
summary,
|
|
80
|
+
description,
|
|
81
|
+
path,
|
|
82
|
+
method
|
|
83
|
+
}: {
|
|
84
|
+
summary?: string;
|
|
85
|
+
description?: string;
|
|
86
|
+
path: string;
|
|
87
|
+
method: string;
|
|
88
|
+
} & TransformerBaseArgs): string;
|
|
89
|
+
declare function transformVerb({
|
|
90
|
+
method
|
|
91
|
+
}: {
|
|
92
|
+
method: string;
|
|
93
|
+
}): string;
|
|
94
|
+
declare function transformEntity({
|
|
95
|
+
path,
|
|
96
|
+
method,
|
|
97
|
+
uncountableNouns
|
|
98
|
+
}: TransformerBaseArgs): string;
|
|
99
|
+
declare function transformFn({
|
|
100
|
+
verb,
|
|
101
|
+
entity
|
|
102
|
+
}: {
|
|
103
|
+
verb: string;
|
|
104
|
+
entity: string;
|
|
105
|
+
} & TransformerBaseArgs): string;
|
|
106
|
+
declare function transformType({
|
|
107
|
+
verb,
|
|
108
|
+
entity
|
|
109
|
+
}: {
|
|
110
|
+
verb: string;
|
|
111
|
+
entity: string;
|
|
112
|
+
} & TransformerBaseArgs): string;
|
|
113
|
+
declare function transformTypeValue({
|
|
114
|
+
fullPath,
|
|
115
|
+
method
|
|
116
|
+
}: {
|
|
117
|
+
verb: string;
|
|
118
|
+
entity: string;
|
|
119
|
+
} & TransformerBaseArgs): string;
|
|
120
|
+
declare function transformTypeQuery({
|
|
121
|
+
type
|
|
122
|
+
}: {
|
|
123
|
+
type: string;
|
|
124
|
+
verb: string;
|
|
125
|
+
entity: string;
|
|
126
|
+
} & TransformerBaseArgs): string;
|
|
127
|
+
declare function transformTypeQueryValue({
|
|
128
|
+
type
|
|
129
|
+
}: {
|
|
130
|
+
type: string;
|
|
131
|
+
verb: string;
|
|
132
|
+
entity: string;
|
|
133
|
+
} & TransformerBaseArgs): string;
|
|
134
|
+
declare function transformTypeRequestBody({
|
|
135
|
+
type
|
|
136
|
+
}: {
|
|
137
|
+
type: string;
|
|
138
|
+
verb: string;
|
|
139
|
+
entity: string;
|
|
140
|
+
} & TransformerBaseArgs): string;
|
|
141
|
+
declare function transformTypeRequestBodyValue({
|
|
142
|
+
type,
|
|
143
|
+
required,
|
|
144
|
+
requestContentType
|
|
145
|
+
}: {
|
|
146
|
+
type: string;
|
|
147
|
+
verb: string;
|
|
148
|
+
entity: string;
|
|
149
|
+
required: boolean;
|
|
150
|
+
requestContentType: string;
|
|
151
|
+
} & TransformerBaseArgs): string;
|
|
152
|
+
declare function transformTypeResponseBody({
|
|
153
|
+
type
|
|
154
|
+
}: {
|
|
155
|
+
type: string;
|
|
156
|
+
verb: string;
|
|
157
|
+
entity: string;
|
|
158
|
+
} & TransformerBaseArgs): string;
|
|
159
|
+
declare function transformTypeResponseBodyValue({
|
|
160
|
+
type,
|
|
161
|
+
responseMetadataItems
|
|
162
|
+
}: {
|
|
163
|
+
type: string;
|
|
164
|
+
verb: string;
|
|
165
|
+
entity: string;
|
|
166
|
+
responseMetadataItems: ResponseMetadataItem[];
|
|
167
|
+
} & TransformerBaseArgs): string;
|
|
168
|
+
interface Transformer {
|
|
169
|
+
moduleName: typeof transformModuleName;
|
|
170
|
+
verb: typeof transformVerb;
|
|
171
|
+
url: typeof transformUrl;
|
|
172
|
+
comment: typeof transformComment;
|
|
173
|
+
entity: typeof transformEntity;
|
|
174
|
+
fn: typeof transformFn;
|
|
175
|
+
type: typeof transformType;
|
|
176
|
+
typeValue: typeof transformTypeValue;
|
|
177
|
+
typeQuery: typeof transformTypeQuery;
|
|
178
|
+
typeQueryValue: typeof transformTypeQueryValue;
|
|
179
|
+
typeRequestBody: typeof transformTypeRequestBody;
|
|
180
|
+
typeRequestBodyValue: typeof transformTypeRequestBodyValue;
|
|
181
|
+
typeResponseBody: typeof transformTypeResponseBody;
|
|
182
|
+
typeResponseBodyValue: typeof transformTypeResponseBodyValue;
|
|
183
|
+
}
|
|
184
|
+
declare function createTransformer(): Transformer;
|
|
185
|
+
//#endregion
|
|
186
|
+
//#region src/generate.d.ts
|
|
187
|
+
interface ApiModuleTemplateData {
|
|
188
|
+
/**
|
|
189
|
+
* API module metadata
|
|
190
|
+
*/
|
|
191
|
+
apiModule: ApiModule;
|
|
192
|
+
/**
|
|
193
|
+
* The name of the generated api ts type aggregation file
|
|
194
|
+
*/
|
|
195
|
+
typesFilename: string;
|
|
196
|
+
/**
|
|
197
|
+
* Whether to generate ts code
|
|
198
|
+
*/
|
|
199
|
+
ts: boolean;
|
|
200
|
+
/**
|
|
201
|
+
* Whether to generate only types
|
|
202
|
+
*/
|
|
203
|
+
typesOnly: boolean;
|
|
204
|
+
}
|
|
205
|
+
interface ApiModule {
|
|
206
|
+
/**
|
|
207
|
+
* The name of the API module
|
|
208
|
+
*/
|
|
209
|
+
name: string;
|
|
210
|
+
/**
|
|
211
|
+
* API module payloads
|
|
212
|
+
*/
|
|
213
|
+
payloads: ApiModulePayload[];
|
|
214
|
+
}
|
|
215
|
+
interface ApiModulePayload {
|
|
216
|
+
/**
|
|
217
|
+
* The comment of the API endpoint, including summary, description, URL, and method.
|
|
218
|
+
*/
|
|
219
|
+
comment: string;
|
|
220
|
+
/**
|
|
221
|
+
* The name of the API function/dispatcher, such as apiGetUsers, apiCreatePost, apiUpdateComment, etc.
|
|
222
|
+
*/
|
|
223
|
+
fn: string;
|
|
224
|
+
/**
|
|
225
|
+
* The URL of the API endpoint, such as /users, /posts, /comments, etc.
|
|
226
|
+
*/
|
|
227
|
+
url: string;
|
|
228
|
+
/**
|
|
229
|
+
* The HTTP method of the API endpoint, such as get, post, put, delete, etc.
|
|
230
|
+
*/
|
|
231
|
+
method: string;
|
|
232
|
+
/**
|
|
233
|
+
* The HTTP verb of the API endpoint, such as Get, Create, Update, Delete, etc.
|
|
234
|
+
*/
|
|
235
|
+
verb: string;
|
|
236
|
+
/**
|
|
237
|
+
* The entity name of the API endpoint, such as User, Comment, Post, etc.
|
|
238
|
+
*/
|
|
239
|
+
entity: string;
|
|
240
|
+
/**
|
|
241
|
+
* The request content type of the API endpoint, such as 'application/json', 'application/x-www-form-urlencoded'.
|
|
242
|
+
*/
|
|
243
|
+
requestContentType?: string;
|
|
244
|
+
/**
|
|
245
|
+
* The type name of the API endpoint, such as ApiGetUsers, ApiCreatePost, ApiUpdateComment, etc.
|
|
246
|
+
*/
|
|
247
|
+
type: string;
|
|
248
|
+
/**
|
|
249
|
+
* The value of the type of the API endpoint, such as paths['/users']['get'], paths['/posts']['post'], paths['/comments']['put'], etc.
|
|
250
|
+
*/
|
|
251
|
+
typeValue: string;
|
|
252
|
+
/**
|
|
253
|
+
* The type name of the query parameters of the API endpoint, such as ApiGetUsersQuery, ApiCreatePostQuery, ApiUpdateCommentQuery, etc.
|
|
254
|
+
*/
|
|
255
|
+
typeQuery: string;
|
|
256
|
+
/**
|
|
257
|
+
* The value of the type of the query parameters of the API endpoint, such as ApiGetUsersQuery['parameters']['query'], ApiCreatePostQuery['parameters']['query'], ApiUpdateCommentQuery['parameters']['query'], etc.
|
|
258
|
+
*/
|
|
259
|
+
typeQueryValue: string;
|
|
260
|
+
/**
|
|
261
|
+
* The type name of the request body of the API endpoint, such as ApiGetUsersRequestBody, ApiCreatePostRequestBody, ApiUpdateCommentRequestBody, etc.
|
|
262
|
+
*/
|
|
263
|
+
typeRequestBody: string;
|
|
264
|
+
/**
|
|
265
|
+
* The value of the type of the request body of the API endpoint, such as ApiGetUsersRequestBody['requestBody']['content']['application/json'], ApiCreatePostRequestBody['requestBody']['content']['application/json'], ApiUpdateCommentRequestBody['requestBody']['content']['application/json'], etc.
|
|
266
|
+
*/
|
|
267
|
+
typeRequestBodyValue: string;
|
|
268
|
+
/**
|
|
269
|
+
* The type name of the response body of the API endpoint, such as ApiGetUsersResponseBody, ApiCreatePostResponseBody, ApiUpdateCommentResponseBody, etc.
|
|
270
|
+
*/
|
|
271
|
+
typeResponseBody: string;
|
|
272
|
+
/**
|
|
273
|
+
* The value of the type of the response body of the API endpoint, such as ApiGetUsersResponseBody['responses']['200']['content']['application/json'], ApiCreatePostResponseBody['responses']['201']['content']['application/json'], ApiUpdateCommentResponseBody['responses']['200']['content']['application/json'], etc.
|
|
274
|
+
*/
|
|
275
|
+
typeResponseBodyValue: string;
|
|
276
|
+
}
|
|
277
|
+
interface GenerateOptions {
|
|
278
|
+
/**
|
|
279
|
+
* The path to the OpenAPI/Swagger schema file.
|
|
280
|
+
* @default './schema.json'
|
|
281
|
+
*/
|
|
282
|
+
input?: string;
|
|
283
|
+
/**
|
|
284
|
+
* The path to the output directory.
|
|
285
|
+
* @default './src/apis/generated'
|
|
286
|
+
*/
|
|
287
|
+
output?: string;
|
|
288
|
+
/**
|
|
289
|
+
* The base path of the API endpoints.
|
|
290
|
+
*/
|
|
291
|
+
base?: string;
|
|
292
|
+
/**
|
|
293
|
+
* The filename of the generated openapi types file.
|
|
294
|
+
* @default '_types.ts'
|
|
295
|
+
*/
|
|
296
|
+
typesFilename?: string;
|
|
297
|
+
/**
|
|
298
|
+
* Whether to generate TypeScript code.
|
|
299
|
+
* @default true
|
|
300
|
+
*/
|
|
301
|
+
ts?: boolean;
|
|
302
|
+
/**
|
|
303
|
+
* Whether to generate only types.
|
|
304
|
+
* @default false
|
|
305
|
+
*/
|
|
306
|
+
typesOnly?: boolean;
|
|
307
|
+
/**
|
|
308
|
+
* Whether to override the existing files, or an array of filenames to override.
|
|
309
|
+
* @default true
|
|
310
|
+
*/
|
|
311
|
+
overrides?: boolean | string[];
|
|
312
|
+
/**
|
|
313
|
+
* The preset ejs template to use.
|
|
314
|
+
* @default 'axle'
|
|
315
|
+
*/
|
|
316
|
+
preset?: Preset;
|
|
317
|
+
/**
|
|
318
|
+
* Defines which return status codes will be typed
|
|
319
|
+
* @default (status) => status >= 200 && status < 300
|
|
320
|
+
*/
|
|
321
|
+
validateStatus?: (status: number) => boolean;
|
|
322
|
+
/**
|
|
323
|
+
* The transformer api options, used to override the default transformation rules.
|
|
324
|
+
*/
|
|
325
|
+
transformer?: Partial<Transformer>;
|
|
326
|
+
/**
|
|
327
|
+
* Certain uncountable nouns that do not change from singular to plural
|
|
328
|
+
*/
|
|
329
|
+
uncountableNouns?: string[];
|
|
330
|
+
/**
|
|
331
|
+
* Whether to clean the output directory before generating.
|
|
332
|
+
* @default false
|
|
333
|
+
*/
|
|
334
|
+
clean?: boolean;
|
|
335
|
+
/**
|
|
336
|
+
* A function to transform the generated types AST before printing to string.
|
|
337
|
+
*/
|
|
338
|
+
openapiTsOptions?: OpenAPITSOptions;
|
|
339
|
+
/**
|
|
340
|
+
* Whether to exclude deprecated API endpoints.
|
|
341
|
+
* @default false
|
|
342
|
+
*/
|
|
343
|
+
excludeDeprecated?: boolean;
|
|
344
|
+
}
|
|
345
|
+
declare function transformPayloads(pathItems: Record<string, OperationObject>, options: {
|
|
346
|
+
path: string;
|
|
347
|
+
fullPath: string;
|
|
348
|
+
base: string | undefined;
|
|
349
|
+
transformer: Transformer;
|
|
350
|
+
uncountableNouns: string[];
|
|
351
|
+
validateStatus: (status: number) => boolean;
|
|
352
|
+
excludeDeprecated?: boolean;
|
|
353
|
+
}): ApiModulePayload[];
|
|
354
|
+
declare function partitionApiModules(schema: OpenAPI3, options: {
|
|
355
|
+
transformer: Transformer;
|
|
356
|
+
base: string | undefined;
|
|
357
|
+
uncountableNouns: string[];
|
|
358
|
+
validateStatus: (status: number) => boolean;
|
|
359
|
+
excludeDeprecated?: boolean;
|
|
360
|
+
}): ApiModule[];
|
|
361
|
+
declare function renderApiModules(apiModules: ApiModule[], options: {
|
|
362
|
+
output: string;
|
|
363
|
+
typesFilename: string;
|
|
364
|
+
ts: boolean;
|
|
365
|
+
typesOnly: boolean;
|
|
366
|
+
overrides: boolean | string[];
|
|
367
|
+
preset: Preset;
|
|
368
|
+
}): Promise<unknown[]>;
|
|
369
|
+
declare function generateTypes(schema: OpenAPI3, output: string, typesFilename: string, openapiTsOptions: OpenAPITSOptions): Promise<void>;
|
|
370
|
+
declare function generate(userOptions?: GenerateOptions): Promise<void>;
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/config.d.ts
|
|
373
|
+
type Config = GenerateOptions;
|
|
374
|
+
declare function defineConfig(config: Config): GenerateOptions;
|
|
375
|
+
declare function getConfig(): Promise<Config>;
|
|
376
|
+
//#endregion
|
|
377
|
+
export { ApiModule, ApiModulePayload, ApiModuleTemplateData, Config, GenerateOptions, Preset, ResponseMetadataItem, StatusCodeStrategy, StatusCodes, Transformer, TransformerBaseArgs, createStatusCodesByStrategy, createTransformer, defineConfig, findObjectKey, generate, generateTypes, getCliVersion, getConfig, getRequestBodyContentType, getResponseMetadataItems, getSchemaNode, isRemoteSchema, isRequiredRequestBody, partitionApiModules, pluralize, readSchema, readSchemaContent, readTemplateFile, renderApiModules, transformComment, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { C as transformVerb, S as transformUrl, _ as transformTypeRequestBody, a as renderApiModules, b as transformTypeResponseBodyValue, c as getConfig, d as transformEntity, f as transformFn, g as transformTypeQueryValue, h as transformTypeQuery, i as partitionApiModules, l as createTransformer, m as transformType, n as generateTypes, o as transformPayloads, p as transformModuleName, s as defineConfig, t as generate, u as transformComment, v as transformTypeRequestBodyValue, x as transformTypeValue, y as transformTypeResponseBody } from "./generate-C4lplT2F.mjs";
|
|
2
|
+
import { a as getResponseMetadataItems, c as isRequiredRequestBody, d as readTemplateFile, i as getRequestBodyContentType, l as readSchema, n as findObjectKey, o as getSchemaNode, r as getCliVersion, s as isRemoteSchema, t as createStatusCodesByStrategy, u as readSchemaContent } from "./utils-DachPo4u.mjs";
|
|
3
|
+
import pluralize from "pluralize";
|
|
4
|
+
export { createStatusCodesByStrategy, createTransformer, defineConfig, findObjectKey, generate, generateTypes, getCliVersion, getConfig, getRequestBodyContentType, getResponseMetadataItems, getSchemaNode, isRemoteSchema, isRequiredRequestBody, partitionApiModules, pluralize, readSchema, readSchemaContent, readTemplateFile, renderApiModules, transformComment, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { tryParseJSON } from "rattail";
|
|
2
|
+
import { resolve } from "path";
|
|
3
|
+
import fse from "fs-extra";
|
|
4
|
+
import { logger } from "rslog";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { createAxle } from "rattail/axle";
|
|
8
|
+
import swagger from "swagger2openapi";
|
|
9
|
+
import yaml from "yaml";
|
|
10
|
+
//#region node_modules/.pnpm/@voidzero-dev+vite-plus-core@0.1.16_@types+node@22.19.17_esbuild@0.25.12_jiti@2.6.1_tsx_c6a4ec143665127b4e0039cf3983defb/node_modules/@voidzero-dev/vite-plus-core/dist/esm-shims.js
|
|
11
|
+
const getFilename = () => fileURLToPath(import.meta.url);
|
|
12
|
+
const getDirname = () => path.dirname(getFilename());
|
|
13
|
+
const __dirname = /* @__PURE__ */ getDirname();
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/constants.ts
|
|
16
|
+
const CWD = process.cwd();
|
|
17
|
+
const CUSTOM_TEMPLATE_FILE = resolve(CWD, `./api-farmer.ejs`);
|
|
18
|
+
const CLI_PACKAGE_JSON = resolve(__dirname, "../package.json");
|
|
19
|
+
const SUPPORTED_HTTP_METHODS = [
|
|
20
|
+
"get",
|
|
21
|
+
"post",
|
|
22
|
+
"put",
|
|
23
|
+
"delete",
|
|
24
|
+
"patch",
|
|
25
|
+
"options",
|
|
26
|
+
"head"
|
|
27
|
+
];
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/utils.ts
|
|
30
|
+
function createStatusCodesByStrategy(strategy) {
|
|
31
|
+
return {
|
|
32
|
+
strict: {
|
|
33
|
+
get: 200,
|
|
34
|
+
post: 201,
|
|
35
|
+
put: 200,
|
|
36
|
+
delete: 204,
|
|
37
|
+
patch: 200,
|
|
38
|
+
options: 204,
|
|
39
|
+
head: 200
|
|
40
|
+
},
|
|
41
|
+
loose: {
|
|
42
|
+
get: 200,
|
|
43
|
+
post: 200,
|
|
44
|
+
put: 200,
|
|
45
|
+
delete: 200,
|
|
46
|
+
patch: 200,
|
|
47
|
+
options: 200,
|
|
48
|
+
head: 200
|
|
49
|
+
},
|
|
50
|
+
smart: {
|
|
51
|
+
get: 200,
|
|
52
|
+
post: 200,
|
|
53
|
+
put: 200,
|
|
54
|
+
delete: 200,
|
|
55
|
+
patch: 200,
|
|
56
|
+
options: 200,
|
|
57
|
+
head: 200
|
|
58
|
+
}
|
|
59
|
+
}[strategy];
|
|
60
|
+
}
|
|
61
|
+
async function readSchema(input) {
|
|
62
|
+
const content = await readSchemaContent(input);
|
|
63
|
+
const jsonSchema = tryParseJSON(content);
|
|
64
|
+
const swaggerOrOpenapiSchema = jsonSchema ? jsonSchema : yaml.parse(content);
|
|
65
|
+
return swaggerOrOpenapiSchema.swagger ? (await swagger.convert(swaggerOrOpenapiSchema, {})).openapi : swaggerOrOpenapiSchema;
|
|
66
|
+
}
|
|
67
|
+
function getSchemaNode(schema, path) {
|
|
68
|
+
return path.split("/").reduce((node, path) => node[path], schema);
|
|
69
|
+
}
|
|
70
|
+
async function readSchemaContent(input) {
|
|
71
|
+
if (isRemoteSchema(input)) try {
|
|
72
|
+
logger.info("Fetching remote schema...");
|
|
73
|
+
const { data } = await createAxle().get(input);
|
|
74
|
+
return JSON.stringify(data);
|
|
75
|
+
} catch {
|
|
76
|
+
throw new Error("Failed to fetch remote schema");
|
|
77
|
+
}
|
|
78
|
+
const path = resolve(CWD, input);
|
|
79
|
+
return fse.readFileSync(path, "utf-8");
|
|
80
|
+
}
|
|
81
|
+
function isRemoteSchema(path) {
|
|
82
|
+
return path.startsWith("http://") || path.startsWith("https://");
|
|
83
|
+
}
|
|
84
|
+
function readTemplateFile(preset = "axle") {
|
|
85
|
+
if (fse.existsSync(CUSTOM_TEMPLATE_FILE)) return fse.readFileSync(CUSTOM_TEMPLATE_FILE, "utf-8");
|
|
86
|
+
return fse.readFileSync(resolve(__dirname, `../templates/${preset}.ejs`), "utf-8");
|
|
87
|
+
}
|
|
88
|
+
function getCliVersion() {
|
|
89
|
+
return fse.readJsonSync(CLI_PACKAGE_JSON).version;
|
|
90
|
+
}
|
|
91
|
+
function isRequiredRequestBody(value) {
|
|
92
|
+
return "required" in value && value.required === true;
|
|
93
|
+
}
|
|
94
|
+
function findObjectKey(object, targetKeys) {
|
|
95
|
+
return Object.keys(object).find((key) => targetKeys.includes(key));
|
|
96
|
+
}
|
|
97
|
+
function getRequestBodyContentType(value) {
|
|
98
|
+
if (!("content" in value)) return "";
|
|
99
|
+
return findObjectKey(value.content, [
|
|
100
|
+
"application/json",
|
|
101
|
+
"application/x-www-form-urlencoded",
|
|
102
|
+
"multipart/form-data"
|
|
103
|
+
]);
|
|
104
|
+
}
|
|
105
|
+
function getResponseMetadataItems(operation, validateStatus) {
|
|
106
|
+
const responses = operation.responses ?? {};
|
|
107
|
+
return Object.keys(responses).sort((a, b) => Number(a) - Number(b)).filter((key) => validateStatus(Number(key))).map(Number).map((status) => {
|
|
108
|
+
return {
|
|
109
|
+
status,
|
|
110
|
+
responseContentType: findObjectKey((operation.responses?.[status])?.content ?? {}, [
|
|
111
|
+
"application/json",
|
|
112
|
+
"application/octet-stream",
|
|
113
|
+
"*/*"
|
|
114
|
+
])
|
|
115
|
+
};
|
|
116
|
+
}).filter((result) => result.responseContentType);
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
export { getResponseMetadataItems as a, isRequiredRequestBody as c, readTemplateFile as d, CWD as f, getRequestBodyContentType as i, readSchema as l, findObjectKey as n, getSchemaNode as o, SUPPORTED_HTTP_METHODS as p, getCliVersion as r, isRemoteSchema as s, createStatusCodesByStrategy as t, readSchemaContent as u };
|
package/package.json
CHANGED
|
@@ -1,95 +1,77 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-farmer",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "API module generation tool based on Openapi3/Swagger2.",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"cli",
|
|
7
6
|
"api generator",
|
|
7
|
+
"cli",
|
|
8
|
+
"json schema",
|
|
8
9
|
"openapi",
|
|
9
|
-
"swagger"
|
|
10
|
-
"json schema"
|
|
10
|
+
"swagger"
|
|
11
11
|
],
|
|
12
12
|
"bugs": {
|
|
13
13
|
"url": "https://github.com/varletjs/api-farmer/issues"
|
|
14
14
|
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "haoziqaq <357229046@qq.com>",
|
|
15
17
|
"repository": {
|
|
16
18
|
"type": "git",
|
|
17
19
|
"url": "git+https://github.com/varletjs/api-farmer.git"
|
|
18
20
|
},
|
|
19
|
-
"license": "MIT",
|
|
20
|
-
"author": "haoziqaq <357229046@qq.com>",
|
|
21
|
-
"sideEffects": false,
|
|
22
|
-
"type": "module",
|
|
23
|
-
"exports": {
|
|
24
|
-
".": {
|
|
25
|
-
"import": "./dist/index.js",
|
|
26
|
-
"require": "./dist/index.cjs"
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
"main": "dist/index.cjs",
|
|
30
|
-
"module": "dist/index.js",
|
|
31
|
-
"types": "dist/index.d.ts",
|
|
32
21
|
"bin": {
|
|
33
|
-
"af": "dist/cli.
|
|
22
|
+
"af": "dist/cli.mjs"
|
|
34
23
|
},
|
|
35
24
|
"files": [
|
|
36
25
|
"dist",
|
|
37
26
|
"templates"
|
|
38
27
|
],
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
|
|
28
|
+
"type": "module",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"main": "dist/index.mjs",
|
|
31
|
+
"module": "dist/index.mjs",
|
|
32
|
+
"types": "dist/index.d.mts",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/index.d.mts",
|
|
36
|
+
"import": "./dist/index.mjs"
|
|
37
|
+
}
|
|
49
38
|
},
|
|
50
39
|
"dependencies": {
|
|
51
40
|
"@types/pluralize": "^0.0.33",
|
|
52
|
-
"@varlet/axle": "^0.10.3",
|
|
53
41
|
"commander": "^13.0.0",
|
|
54
42
|
"ejs": "^3.1.10",
|
|
55
43
|
"fs-extra": "^11.2.0",
|
|
56
44
|
"openapi-typescript": "^7.5.2",
|
|
57
45
|
"pluralize": "^8.0.0",
|
|
58
46
|
"prettier": "^3.4.2",
|
|
59
|
-
"rattail": "^1.
|
|
47
|
+
"rattail": "^1.8.3",
|
|
60
48
|
"rslog": "^1.2.3",
|
|
61
49
|
"swagger2openapi": "^7.0.8",
|
|
62
50
|
"unconfig": "^0.6.0",
|
|
63
51
|
"yaml": "^2.7.0"
|
|
64
52
|
},
|
|
65
53
|
"devDependencies": {
|
|
66
|
-
"@configurajs/eslint": "^0.1.2",
|
|
67
|
-
"@configurajs/prettier": "^0.1.4",
|
|
68
54
|
"@types/ejs": "^3.1.5",
|
|
69
55
|
"@types/fs-extra": "^11.0.4",
|
|
70
56
|
"@types/node": "^22.8.1",
|
|
71
57
|
"@types/swagger2openapi": "^7.0.4",
|
|
72
58
|
"@varlet/release": "^0.4.1",
|
|
73
|
-
"eslint": "^9.17.0",
|
|
74
|
-
"nano-staged": "0.8.0",
|
|
75
59
|
"rimraf": "^6.0.1",
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"typescript": "5.3.3"
|
|
60
|
+
"typescript": "5.3.3",
|
|
61
|
+
"vite-plus": "0.1.16"
|
|
79
62
|
},
|
|
80
63
|
"peerDependencies": {
|
|
81
64
|
"typescript": "^5.3.3"
|
|
82
65
|
},
|
|
83
66
|
"engines": {
|
|
84
|
-
"pnpm": ">=
|
|
67
|
+
"pnpm": ">=10.0.0"
|
|
85
68
|
},
|
|
86
69
|
"scripts": {
|
|
87
|
-
"build": "
|
|
70
|
+
"build": "vp pack",
|
|
88
71
|
"clean": "rimraf node_modules dist",
|
|
89
|
-
"
|
|
90
|
-
"format": "prettier --write .",
|
|
72
|
+
"format": "vp fmt",
|
|
91
73
|
"gen": "rimraf ./fixtures/axios/src/apis/generated ./fixtures/axle/src/apis/generated && pnpm --dir ./fixtures/axios gen & pnpm --dir ./fixtures/axle gen",
|
|
92
|
-
"lint": "
|
|
74
|
+
"lint": "vp lint --fix",
|
|
93
75
|
"release": "pnpm build && vr release"
|
|
94
76
|
}
|
|
95
77
|
}
|