api-farmer 0.0.4 → 0.0.5
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 +57 -18
- package/dist/{chunk-ZLLNTCL2.js → chunk-6T5EWOBD.js} +1 -1
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/{generate-23Q2CMLJ.js → generate-544AKFJX.js} +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +4 -3
- package/templates/axios.ejs +42 -0
- package/templates/axle.ejs +36 -0
package/README.md
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
### Intro
|
|
4
4
|
|
|
5
|
-
API module generation tool based on `Openapi3
|
|
5
|
+
API module generation tool based on `Openapi3/Swagger2`.
|
|
6
6
|
|
|
7
7
|
### Features
|
|
8
8
|
|
|
9
|
-
- 🌐 Supports generating all API modules from `
|
|
9
|
+
- 🌐 Supports generating all API modules from `OpenAPI3/Swagger2 schemas`
|
|
10
10
|
- 📦 Supports generating `ts/js` modules
|
|
11
11
|
- 🛠️ Comprehensive `ts type` generation
|
|
12
12
|
- ✏️ Supports custom `ejs` templates for tailored content generation
|
|
@@ -38,7 +38,7 @@ export default defineConfig({
|
|
|
38
38
|
input: './schema.yaml',
|
|
39
39
|
// generated codes output path, defaults './src/apis'
|
|
40
40
|
output: './src/apis',
|
|
41
|
-
// axle or axios, defaults axle.
|
|
41
|
+
// 'axle' or 'axios', defaults 'axle'.
|
|
42
42
|
preset: 'axios',
|
|
43
43
|
})
|
|
44
44
|
```
|
|
@@ -52,15 +52,34 @@ npx af
|
|
|
52
52
|
> [!TIP]
|
|
53
53
|
> The generated content does not include the integration of the request client.
|
|
54
54
|
|
|
55
|
+
#### Some Examples
|
|
56
|
+
|
|
57
|
+
Some simple usage examples can be found [here](fixtures)
|
|
58
|
+
|
|
55
59
|
### Custom EJS Template
|
|
56
60
|
|
|
57
|
-
Create api-farmer.ejs in the project root
|
|
61
|
+
Create `api-farmer.ejs` in the project root, which will replace the `preset` template.
|
|
62
|
+
The template format can refer to the preset template listed below:
|
|
58
63
|
|
|
59
64
|
- [Axle](templates/axle.ejs)
|
|
60
65
|
- [Axios](templates/axios.ejs)
|
|
61
66
|
|
|
62
67
|
See the bottom of the document for template variable definitions.
|
|
63
68
|
|
|
69
|
+
### Status Code Strategy
|
|
70
|
+
|
|
71
|
+
`Restful API` recommends using different successful http status codes for different methods, such as `get: 200`, `post: 201`, etc. If you don't need this strategy, you can set `statusCodeStrategy` to `loose`
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
// api-farmer.config.ts
|
|
75
|
+
import { defineConfig } from 'api-farmer'
|
|
76
|
+
|
|
77
|
+
export default defineConfig({
|
|
78
|
+
// 'strict' or 'loose', defaults 'strict'
|
|
79
|
+
statusCodeStrategy: 'loose',
|
|
80
|
+
})
|
|
81
|
+
```
|
|
82
|
+
|
|
64
83
|
### Transformer API
|
|
65
84
|
|
|
66
85
|
You can use the Transformer API to further define template variables, which will override the default transformation rules.
|
|
@@ -72,6 +91,7 @@ import { defineConfig } from 'api-farmer'
|
|
|
72
91
|
export default defineConfig({
|
|
73
92
|
transformer: {
|
|
74
93
|
moduleName({ name }) {
|
|
94
|
+
// The new module name.
|
|
75
95
|
return `${name}.generated`
|
|
76
96
|
},
|
|
77
97
|
verb() {},
|
|
@@ -127,7 +147,8 @@ export interface Config {
|
|
|
127
147
|
*/
|
|
128
148
|
preset?: Preset
|
|
129
149
|
/**
|
|
130
|
-
* The status code strategy to use.
|
|
150
|
+
* The status code strategy to use.
|
|
151
|
+
* loose: all success status codes are 200, strict: use the openapi recommended success status codes.
|
|
131
152
|
*/
|
|
132
153
|
statusCodeStrategy?: StatusCodeStrategy
|
|
133
154
|
/**
|
|
@@ -176,55 +197,73 @@ export interface ApiModule {
|
|
|
176
197
|
|
|
177
198
|
export interface ApiModulePayload {
|
|
178
199
|
/**
|
|
179
|
-
* The name of the API function/dispatcher,
|
|
200
|
+
* The name of the API function/dispatcher,
|
|
201
|
+
* such as apiGetUsers, apiCreatePost, apiUpdateComment, etc.
|
|
180
202
|
*/
|
|
181
203
|
fn: string
|
|
182
204
|
/**
|
|
183
|
-
* The URL of the API endpoint,
|
|
205
|
+
* The URL of the API endpoint,
|
|
206
|
+
* such as /users, /posts, /comments, etc.
|
|
184
207
|
*/
|
|
185
208
|
url: string
|
|
186
209
|
/**
|
|
187
|
-
* The HTTP method of the API endpoint,
|
|
210
|
+
* The HTTP method of the API endpoint,
|
|
211
|
+
* such as get, post, put, delete, etc.
|
|
188
212
|
*/
|
|
189
213
|
method: string
|
|
190
214
|
/**
|
|
191
|
-
* The HTTP verb of the API endpoint,
|
|
215
|
+
* The HTTP verb of the API endpoint,
|
|
216
|
+
* such as Get, Create, Update, Delete, etc.
|
|
192
217
|
*/
|
|
193
218
|
verb: string
|
|
194
219
|
/**
|
|
195
|
-
* The entity name of the API endpoint,
|
|
220
|
+
* The entity name of the API endpoint,
|
|
221
|
+
* such as User, Comment, Post, etc.
|
|
196
222
|
*/
|
|
197
223
|
entity: string
|
|
198
224
|
/**
|
|
199
|
-
* The type name of the API endpoint,
|
|
225
|
+
* The type name of the API endpoint,
|
|
226
|
+
* such as ApiGetUsers, ApiCreatePost, ApiUpdateComment, etc.
|
|
200
227
|
*/
|
|
201
228
|
type: string
|
|
202
229
|
/**
|
|
203
|
-
* The value of the type of the API endpoint,
|
|
230
|
+
* The value of the type of the API endpoint,
|
|
231
|
+
* such as paths['/users']['get'], paths['/posts']['post'], paths['/comments']['put'], etc.
|
|
204
232
|
*/
|
|
205
233
|
typeValue: string
|
|
206
234
|
/**
|
|
207
|
-
* The type name of the query parameters of the API endpoint,
|
|
235
|
+
* The type name of the query parameters of the API endpoint,
|
|
236
|
+
* such as ApiGetUsersQuery, ApiCreatePostQuery, ApiUpdateCommentQuery, etc.
|
|
208
237
|
*/
|
|
209
238
|
typeQuery: string
|
|
210
239
|
/**
|
|
211
|
-
* The value of the type of the query parameters of the API endpoint,
|
|
240
|
+
* The value of the type of the query parameters of the API endpoint,
|
|
241
|
+
* such as ApiGetUsersQuery['parameters']['query'], ApiCreatePostQuery['parameters']['query'],
|
|
242
|
+
* ApiUpdateCommentQuery['parameters']['query'], etc.
|
|
212
243
|
*/
|
|
213
244
|
typeQueryValue: string
|
|
214
245
|
/**
|
|
215
|
-
* The type name of the request body of the API endpoint,
|
|
246
|
+
* The type name of the request body of the API endpoint,
|
|
247
|
+
* such as ApiGetUsersRequestBody, ApiCreatePostRequestBody, ApiUpdateCommentRequestBody, etc.
|
|
216
248
|
*/
|
|
217
249
|
typeRequestBody: string
|
|
218
250
|
/**
|
|
219
|
-
* The value of the type of the request body of the API endpoint,
|
|
251
|
+
* The value of the type of the request body of the API endpoint,
|
|
252
|
+
* such as ApiGetUsersRequestBody['requestBody']['content']['application/json'],
|
|
253
|
+
* ApiCreatePostRequestBody['requestBody']['content']['application/json'],
|
|
254
|
+
* ApiUpdateCommentRequestBody['requestBody']['content']['application/json'], etc.
|
|
220
255
|
*/
|
|
221
256
|
typeRequestBodyValue: string
|
|
222
257
|
/**
|
|
223
|
-
* The type name of the response body of the API endpoint,
|
|
258
|
+
* The type name of the response body of the API endpoint,
|
|
259
|
+
* such as ApiGetUsersResponseBody, ApiCreatePostResponseBody, ApiUpdateCommentResponseBody, etc.
|
|
224
260
|
*/
|
|
225
261
|
typeResponseBody: string
|
|
226
262
|
/**
|
|
227
|
-
* The value of the type of the response body of the API endpoint,
|
|
263
|
+
* The value of the type of the response body of the API endpoint,
|
|
264
|
+
* such as ApiGetUsersResponseBody['responses']['200']['content']['application/json'],
|
|
265
|
+
* ApiCreatePostResponseBody['responses']['201']['content']['application/json'],
|
|
266
|
+
* ApiUpdateCommentResponseBody['responses']['200']['content']['application/json'], etc.
|
|
228
267
|
*/
|
|
229
268
|
typeResponseBodyValue: string
|
|
230
269
|
}
|
|
@@ -22838,7 +22838,7 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
22838
22838
|
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({ type: type2 }) : "never";
|
|
22839
22839
|
const typeResponseBody = transformer.typeResponseBody({ verb, entity });
|
|
22840
22840
|
const statusCode = statusCodes[method] ?? 200;
|
|
22841
|
-
const mime =
|
|
22841
|
+
const mime = operation.responses?.[statusCode]?.content?.["application/json"] ? "application/json" : "*/*";
|
|
22842
22842
|
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
|
|
22843
22843
|
payloads3.push({
|
|
22844
22844
|
fn,
|
package/dist/cli.cjs
CHANGED
|
@@ -102113,7 +102113,7 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
102113
102113
|
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({ type: type2 }) : "never";
|
|
102114
102114
|
const typeResponseBody = transformer.typeResponseBody({ verb, entity });
|
|
102115
102115
|
const statusCode = statusCodes[method] ?? 200;
|
|
102116
|
-
const mime =
|
|
102116
|
+
const mime = operation.responses?.[statusCode]?.content?.["application/json"] ? "application/json" : "*/*";
|
|
102117
102117
|
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
|
|
102118
102118
|
payloads3.push({
|
|
102119
102119
|
fn: fn8,
|
package/dist/cli.js
CHANGED
|
@@ -9,7 +9,7 @@ import { Command } from "commander";
|
|
|
9
9
|
var program = new Command();
|
|
10
10
|
program.version(getCliVersion());
|
|
11
11
|
program.action(async () => {
|
|
12
|
-
const { generate } = await import("./generate-
|
|
12
|
+
const { generate } = await import("./generate-544AKFJX.js");
|
|
13
13
|
return generate();
|
|
14
14
|
});
|
|
15
15
|
program.parse();
|
package/dist/index.cjs
CHANGED
|
@@ -102150,7 +102150,7 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
102150
102150
|
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({ type: type2 }) : "never";
|
|
102151
102151
|
const typeResponseBody = transformer.typeResponseBody({ verb, entity });
|
|
102152
102152
|
const statusCode = statusCodes[method] ?? 200;
|
|
102153
|
-
const mime =
|
|
102153
|
+
const mime = operation.responses?.[statusCode]?.content?.["application/json"] ? "application/json" : "*/*";
|
|
102154
102154
|
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
|
|
102155
102155
|
payloads3.push({
|
|
102156
102156
|
fn: fn8,
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-farmer",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"description": "API module generation tool based on Openapi3/Swagger2.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
7
7
|
"api generator",
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"af": "dist/cli.js"
|
|
34
34
|
},
|
|
35
35
|
"files": [
|
|
36
|
-
"dist"
|
|
36
|
+
"dist",
|
|
37
|
+
"templates"
|
|
37
38
|
],
|
|
38
39
|
"simple-git-hooks": {
|
|
39
40
|
"pre-commit": "pnpm exec nano-staged --allow-empty",
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
<% if (ts) { %> import { type AxiosRequestConfig } from 'axios' <% } %>
|
|
2
|
+
import { request } from '@/request'
|
|
3
|
+
<% if (ts) { %> import { type paths } from './<%- typesFilename %>' <% } %>
|
|
4
|
+
|
|
5
|
+
<% apiModule.payloads.forEach(payload => { %> -%>
|
|
6
|
+
export const <%- payload.fn %> = (config<% if (ts) { %>: AxiosRequestConfig<<%- payload.typeRequestBody %>> <% } %>)
|
|
7
|
+
=> request<% if (ts) { %><any, Res<<%- payload.typeResponseBody %>>><% } %>({
|
|
8
|
+
url: '<%- payload.url %>',
|
|
9
|
+
method: '<%- payload.method %>',
|
|
10
|
+
...config
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
<% }) %>
|
|
14
|
+
|
|
15
|
+
<% if (ts) { %>
|
|
16
|
+
<% apiModule.payloads.forEach(payload => { %> -%>
|
|
17
|
+
export type <%- payload.type %> = <%- payload.typeValue %>
|
|
18
|
+
|
|
19
|
+
<% }) %>
|
|
20
|
+
|
|
21
|
+
<% apiModule.payloads.forEach(payload => { %> -%>
|
|
22
|
+
<% if (payload.typeQueryValue) { %>
|
|
23
|
+
export type <%- payload.typeQuery %> = <%- payload.typeQueryValue %>
|
|
24
|
+
|
|
25
|
+
<% } %>
|
|
26
|
+
<% }) %>
|
|
27
|
+
|
|
28
|
+
<% apiModule.payloads.forEach(payload => { %> -%>
|
|
29
|
+
<% if (payload.typeRequestBodyValue) { %>
|
|
30
|
+
export type <%- payload.typeRequestBody %> = <%- payload.typeRequestBodyValue %>
|
|
31
|
+
|
|
32
|
+
<% } %>
|
|
33
|
+
<% }) %>
|
|
34
|
+
|
|
35
|
+
<% apiModule.payloads.forEach(payload => { %> -%>
|
|
36
|
+
<% if (payload.typeResponseBodyValue) { %>
|
|
37
|
+
export type <%- payload.typeResponseBody %> = <%- payload.typeResponseBodyValue %>
|
|
38
|
+
|
|
39
|
+
<% } %>
|
|
40
|
+
<% }) %>
|
|
41
|
+
<% } %>
|
|
42
|
+
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { api } from '@/request'
|
|
2
|
+
<% if (ts) { %> import { type paths } from './<%- typesFilename %>' <% } %>
|
|
3
|
+
|
|
4
|
+
<% apiModule.payloads.forEach(payload => { %> -%>
|
|
5
|
+
export const <%- payload.fn %> = api<% if (ts) { %><Res<<%- payload.typeResponseBody %>>, <%- payload.typeRequestBody %>><% } %>('<%- payload.url %>', '<%- payload.method %>')
|
|
6
|
+
|
|
7
|
+
<% }) %>
|
|
8
|
+
|
|
9
|
+
<% if (ts) { %>
|
|
10
|
+
<% apiModule.payloads.forEach(payload => { %> -%>
|
|
11
|
+
export type <%- payload.type %> = <%- payload.typeValue %>
|
|
12
|
+
|
|
13
|
+
<% }) %>
|
|
14
|
+
|
|
15
|
+
<% apiModule.payloads.forEach(payload => { %> -%>
|
|
16
|
+
<% if (payload.typeQueryValue) { %>
|
|
17
|
+
export type <%- payload.typeQuery %> = <%- payload.typeQueryValue %>
|
|
18
|
+
|
|
19
|
+
<% } %>
|
|
20
|
+
<% }) %>
|
|
21
|
+
|
|
22
|
+
<% apiModule.payloads.forEach(payload => { %> -%>
|
|
23
|
+
<% if (payload.typeRequestBodyValue) { %>
|
|
24
|
+
export type <%- payload.typeRequestBody %> = <%- payload.typeRequestBodyValue %>
|
|
25
|
+
|
|
26
|
+
<% } %>
|
|
27
|
+
<% }) %>
|
|
28
|
+
|
|
29
|
+
<% apiModule.payloads.forEach(payload => { %> -%>
|
|
30
|
+
<% if (payload.typeResponseBodyValue) { %>
|
|
31
|
+
export type <%- payload.typeResponseBody %> = <%- payload.typeResponseBodyValue %>
|
|
32
|
+
|
|
33
|
+
<% } %>
|
|
34
|
+
<% }) %>
|
|
35
|
+
<% } %>
|
|
36
|
+
|