api-farmer 0.0.3 → 0.0.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 +231 -0
- package/dist/{chunk-JWKB7AQQ.js → chunk-ZLLNTCL2.js} +35 -31
- package/dist/cli.cjs +35 -31
- package/dist/cli.js +1 -1
- package/dist/{generate-LOGO7XCM.js → generate-23Q2CMLJ.js} +1 -1
- package/dist/index.cjs +35 -31
- package/dist/index.d.cts +145 -25
- package/dist/index.d.ts +145 -25
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# api-farmer
|
|
2
|
+
|
|
3
|
+
### Intro
|
|
4
|
+
|
|
5
|
+
API module generation tool based on `Openapi3 / Swagger2`.
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
|
|
9
|
+
- 🌐 Supports generating all API modules from `OpenAPI 3/Swagger 2 schemas`
|
|
10
|
+
- 📦 Supports generating `ts/js` modules
|
|
11
|
+
- 🛠️ Comprehensive `ts type` generation
|
|
12
|
+
- ✏️ Supports custom `ejs` templates for tailored content generation
|
|
13
|
+
- 🔄 Allows using a `transformer` to modify all variables in templates for fine-grained customization
|
|
14
|
+
- 💻 Supports both `cli` and `node.js api`
|
|
15
|
+
- 📋 Includes built-in presets for [axle](https://github.com/varletjs/axle) and [axios](https://axios-http.com/docs/intro) templates
|
|
16
|
+
|
|
17
|
+
### Quick Start
|
|
18
|
+
|
|
19
|
+
#### 1. Installation
|
|
20
|
+
|
|
21
|
+
```shell
|
|
22
|
+
# npm
|
|
23
|
+
npm i api-farmer -D
|
|
24
|
+
# yarn
|
|
25
|
+
yarn add api-farmer -D
|
|
26
|
+
# pnpm
|
|
27
|
+
pnpm i api-farmer -D
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
#### 2. Setup
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
// in project root path api-farmer.config.ts or api-farmer.config.js
|
|
34
|
+
import { defineConfig } from 'api-farmer'
|
|
35
|
+
|
|
36
|
+
export default defineConfig({
|
|
37
|
+
// openapi or swagger schema path, defaults './schema.json'
|
|
38
|
+
input: './schema.yaml',
|
|
39
|
+
// generated codes output path, defaults './src/apis'
|
|
40
|
+
output: './src/apis',
|
|
41
|
+
// axle or axios, defaults axle.
|
|
42
|
+
preset: 'axios',
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
#### 3. Run Command
|
|
47
|
+
|
|
48
|
+
```shell
|
|
49
|
+
npx af
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
> [!TIP]
|
|
53
|
+
> The generated content does not include the integration of the request client.
|
|
54
|
+
|
|
55
|
+
### Custom EJS Template
|
|
56
|
+
|
|
57
|
+
Create api-farmer.ejs in the project root path. The template content can refer to the following:
|
|
58
|
+
|
|
59
|
+
- [Axle](templates/axle.ejs)
|
|
60
|
+
- [Axios](templates/axios.ejs)
|
|
61
|
+
|
|
62
|
+
See the bottom of the document for template variable definitions.
|
|
63
|
+
|
|
64
|
+
### Transformer API
|
|
65
|
+
|
|
66
|
+
You can use the Transformer API to further define template variables, which will override the default transformation rules.
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
// api-farmer.config.ts
|
|
70
|
+
import { defineConfig } from 'api-farmer'
|
|
71
|
+
|
|
72
|
+
export default defineConfig({
|
|
73
|
+
transformer: {
|
|
74
|
+
moduleName({ name }) {
|
|
75
|
+
return `${name}.generated`
|
|
76
|
+
},
|
|
77
|
+
verb() {},
|
|
78
|
+
url() {},
|
|
79
|
+
entity() {},
|
|
80
|
+
fn() {},
|
|
81
|
+
type() {},
|
|
82
|
+
typeValue() {},
|
|
83
|
+
typeQuery() {},
|
|
84
|
+
typeQueryValue() {},
|
|
85
|
+
typeRequestBody() {},
|
|
86
|
+
typeRequestBodyValue() {},
|
|
87
|
+
typeResponseBody() {},
|
|
88
|
+
typeResponseBodyValue() {},
|
|
89
|
+
},
|
|
90
|
+
})
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Configuration Options
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
export interface Config {
|
|
97
|
+
/**
|
|
98
|
+
* The path to the OpenAPI/Swagger schema file.
|
|
99
|
+
*/
|
|
100
|
+
input?: string
|
|
101
|
+
/**
|
|
102
|
+
* The path to the output directory.
|
|
103
|
+
*/
|
|
104
|
+
output?: string
|
|
105
|
+
/**
|
|
106
|
+
* The base path of the API endpoints.
|
|
107
|
+
*/
|
|
108
|
+
base?: string
|
|
109
|
+
/**
|
|
110
|
+
* The filename of the generated openapi types file.
|
|
111
|
+
*/
|
|
112
|
+
typesFilename?: string
|
|
113
|
+
/**
|
|
114
|
+
* The transformer api options, used to override the default transformation rules.
|
|
115
|
+
*/
|
|
116
|
+
transformer?: Partial<Transformer>
|
|
117
|
+
/**
|
|
118
|
+
* Whether to generate TypeScript code.
|
|
119
|
+
*/
|
|
120
|
+
ts?: boolean
|
|
121
|
+
/**
|
|
122
|
+
* Whether to override the existing files, or an array of filenames to override.
|
|
123
|
+
*/
|
|
124
|
+
overrides?: boolean | string[]
|
|
125
|
+
/**
|
|
126
|
+
* The preset ejs template to use.
|
|
127
|
+
*/
|
|
128
|
+
preset?: Preset
|
|
129
|
+
/**
|
|
130
|
+
* The status code strategy to use. loose: all success status codes are 200, strict: use the openapi recommended success status codes.
|
|
131
|
+
*/
|
|
132
|
+
statusCodeStrategy?: StatusCodeStrategy
|
|
133
|
+
/**
|
|
134
|
+
* The status codes to override the default status codes.
|
|
135
|
+
*/
|
|
136
|
+
statusCodes?: {
|
|
137
|
+
get?: number
|
|
138
|
+
post?: number
|
|
139
|
+
put?: number
|
|
140
|
+
delete?: number
|
|
141
|
+
patch?: number
|
|
142
|
+
options?: number
|
|
143
|
+
head?: number
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Template Variable Definitions
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
export interface ApiModuleTemplateData {
|
|
152
|
+
/**
|
|
153
|
+
* API module metadata
|
|
154
|
+
*/
|
|
155
|
+
apiModule: ApiModule
|
|
156
|
+
/**
|
|
157
|
+
* The name of the generated api ts type aggregation file
|
|
158
|
+
*/
|
|
159
|
+
typesFilename: string
|
|
160
|
+
/**
|
|
161
|
+
* Whether to generate ts code
|
|
162
|
+
*/
|
|
163
|
+
ts: boolean
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export interface ApiModule {
|
|
167
|
+
/**
|
|
168
|
+
* The name of the API module
|
|
169
|
+
*/
|
|
170
|
+
name: string
|
|
171
|
+
/**
|
|
172
|
+
* API module payloads
|
|
173
|
+
*/
|
|
174
|
+
payloads: ApiModulePayload[]
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface ApiModulePayload {
|
|
178
|
+
/**
|
|
179
|
+
* The name of the API function/dispatcher, such as apiGetUsers, apiCreatePost, apiUpdateComment, etc.
|
|
180
|
+
*/
|
|
181
|
+
fn: string
|
|
182
|
+
/**
|
|
183
|
+
* The URL of the API endpoint, such as /users, /posts, /comments, etc.
|
|
184
|
+
*/
|
|
185
|
+
url: string
|
|
186
|
+
/**
|
|
187
|
+
* The HTTP method of the API endpoint, such as get, post, put, delete, etc.
|
|
188
|
+
*/
|
|
189
|
+
method: string
|
|
190
|
+
/**
|
|
191
|
+
* The HTTP verb of the API endpoint, such as Get, Create, Update, Delete, etc.
|
|
192
|
+
*/
|
|
193
|
+
verb: string
|
|
194
|
+
/**
|
|
195
|
+
* The entity name of the API endpoint, such as User, Comment, Post, etc.
|
|
196
|
+
*/
|
|
197
|
+
entity: string
|
|
198
|
+
/**
|
|
199
|
+
* The type name of the API endpoint, such as ApiGetUsers, ApiCreatePost, ApiUpdateComment, etc.
|
|
200
|
+
*/
|
|
201
|
+
type: string
|
|
202
|
+
/**
|
|
203
|
+
* The value of the type of the API endpoint, such as paths['/users']['get'], paths['/posts']['post'], paths['/comments']['put'], etc.
|
|
204
|
+
*/
|
|
205
|
+
typeValue: string
|
|
206
|
+
/**
|
|
207
|
+
* The type name of the query parameters of the API endpoint, such as ApiGetUsersQuery, ApiCreatePostQuery, ApiUpdateCommentQuery, etc.
|
|
208
|
+
*/
|
|
209
|
+
typeQuery: string
|
|
210
|
+
/**
|
|
211
|
+
* 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.
|
|
212
|
+
*/
|
|
213
|
+
typeQueryValue: string
|
|
214
|
+
/**
|
|
215
|
+
* The type name of the request body of the API endpoint, such as ApiGetUsersRequestBody, ApiCreatePostRequestBody, ApiUpdateCommentRequestBody, etc.
|
|
216
|
+
*/
|
|
217
|
+
typeRequestBody: string
|
|
218
|
+
/**
|
|
219
|
+
* 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.
|
|
220
|
+
*/
|
|
221
|
+
typeRequestBodyValue: string
|
|
222
|
+
/**
|
|
223
|
+
* The type name of the response body of the API endpoint, such as ApiGetUsersResponseBody, ApiCreatePostResponseBody, ApiUpdateCommentResponseBody, etc.
|
|
224
|
+
*/
|
|
225
|
+
typeResponseBody: string
|
|
226
|
+
/**
|
|
227
|
+
* 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.
|
|
228
|
+
*/
|
|
229
|
+
typeResponseBodyValue: string
|
|
230
|
+
}
|
|
231
|
+
```
|
|
@@ -22736,10 +22736,10 @@ async function getConfig() {
|
|
|
22736
22736
|
// src/transformer.ts
|
|
22737
22737
|
import pluralize from "pluralize";
|
|
22738
22738
|
import { camelize, pascalCase } from "rattail";
|
|
22739
|
-
function transformModuleName(name) {
|
|
22739
|
+
function transformModuleName({ name }) {
|
|
22740
22740
|
return camelize(name);
|
|
22741
22741
|
}
|
|
22742
|
-
function transformVerb(method) {
|
|
22742
|
+
function transformVerb({ method }) {
|
|
22743
22743
|
switch (method) {
|
|
22744
22744
|
case "post":
|
|
22745
22745
|
return "Create";
|
|
@@ -22749,10 +22749,10 @@ function transformVerb(method) {
|
|
|
22749
22749
|
return pascalCase(method);
|
|
22750
22750
|
}
|
|
22751
22751
|
}
|
|
22752
|
-
function transformUrl(path13, base) {
|
|
22752
|
+
function transformUrl({ path: path13, base }) {
|
|
22753
22753
|
return (base ? path13.replace(base, "") : path13).replace(/{/g, ":").replace(/}/g, "");
|
|
22754
22754
|
}
|
|
22755
|
-
function transformEntity(path13, method, base) {
|
|
22755
|
+
function transformEntity({ path: path13, method, base }) {
|
|
22756
22756
|
path13 = base ? path13.replace(base, "") : path13;
|
|
22757
22757
|
const words = path13.split("/").filter(Boolean);
|
|
22758
22758
|
return words.reduce((entity, word, index) => {
|
|
@@ -22766,31 +22766,35 @@ function transformEntity(path13, method, base) {
|
|
|
22766
22766
|
return `${entity}${word}`;
|
|
22767
22767
|
}, "");
|
|
22768
22768
|
}
|
|
22769
|
-
function transformFn(verb, entity) {
|
|
22769
|
+
function transformFn({ verb, entity }) {
|
|
22770
22770
|
return `api${verb}${entity}`;
|
|
22771
22771
|
}
|
|
22772
|
-
function transformType(verb, entity) {
|
|
22772
|
+
function transformType({ verb, entity }) {
|
|
22773
22773
|
return `Api${verb}${entity}`;
|
|
22774
22774
|
}
|
|
22775
|
-
function transformTypeValue(path13, method) {
|
|
22775
|
+
function transformTypeValue({ path: path13, method }) {
|
|
22776
22776
|
return `paths['${path13}']['${method}']`;
|
|
22777
22777
|
}
|
|
22778
|
-
function transformTypeQuery(verb, entity) {
|
|
22778
|
+
function transformTypeQuery({ verb, entity }) {
|
|
22779
22779
|
return `Api${verb}${entity}Query`;
|
|
22780
22780
|
}
|
|
22781
|
-
function transformTypeQueryValue(type2) {
|
|
22781
|
+
function transformTypeQueryValue({ type: type2 }) {
|
|
22782
22782
|
return `${type2}['parameters']['query']`;
|
|
22783
22783
|
}
|
|
22784
|
-
function transformTypeRequestBody(verb, entity) {
|
|
22784
|
+
function transformTypeRequestBody({ verb, entity }) {
|
|
22785
22785
|
return `Api${verb}${entity}RequestBody`;
|
|
22786
22786
|
}
|
|
22787
|
-
function transformTypeRequestBodyValue(type2) {
|
|
22787
|
+
function transformTypeRequestBodyValue({ type: type2 }) {
|
|
22788
22788
|
return `${type2}['requestBody']['content']['application/json']`;
|
|
22789
22789
|
}
|
|
22790
|
-
function transformTypeResponseBody(verb, entity) {
|
|
22790
|
+
function transformTypeResponseBody({ verb, entity }) {
|
|
22791
22791
|
return `Api${verb}${entity}ResponseBody`;
|
|
22792
22792
|
}
|
|
22793
|
-
function transformTypeResponseBodyValue(
|
|
22793
|
+
function transformTypeResponseBodyValue({
|
|
22794
|
+
type: type2,
|
|
22795
|
+
statusCode,
|
|
22796
|
+
mime
|
|
22797
|
+
}) {
|
|
22794
22798
|
return `${type2}['responses']['${statusCode}']['content']['${mime}']`;
|
|
22795
22799
|
}
|
|
22796
22800
|
function createTransformer() {
|
|
@@ -22822,23 +22826,22 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
22822
22826
|
path13 = base ? base + path13 : path13;
|
|
22823
22827
|
const pathItems = schemaPaths[path13];
|
|
22824
22828
|
const childPayloads = Object.entries(pathItems).reduce((payloads3, [method, operation]) => {
|
|
22825
|
-
const url2 = transformer.url(path13, base);
|
|
22826
|
-
const entity = transformer.entity(path13, method, base);
|
|
22827
|
-
const verb = transformer.verb(method);
|
|
22828
|
-
const fn = transformer.fn(verb, entity);
|
|
22829
|
-
const type2 = transformer.type(verb, entity);
|
|
22830
|
-
const typeValue = transformer.typeValue(path13, method);
|
|
22831
|
-
const typeQuery = transformer.typeQuery(verb, entity);
|
|
22832
|
-
const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue(type2) : "never";
|
|
22833
|
-
const typeRequestBody = transformer.typeRequestBody(verb, entity);
|
|
22834
|
-
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue(type2) : "never";
|
|
22835
|
-
const typeResponseBody = transformer.typeResponseBody(verb, entity);
|
|
22829
|
+
const url2 = transformer.url({ path: path13, base });
|
|
22830
|
+
const entity = transformer.entity({ path: path13, method, base });
|
|
22831
|
+
const verb = transformer.verb({ method });
|
|
22832
|
+
const fn = transformer.fn({ verb, entity });
|
|
22833
|
+
const type2 = transformer.type({ verb, entity });
|
|
22834
|
+
const typeValue = transformer.typeValue({ path: path13, method });
|
|
22835
|
+
const typeQuery = transformer.typeQuery({ verb, entity });
|
|
22836
|
+
const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue({ type: type2 }) : "never";
|
|
22837
|
+
const typeRequestBody = transformer.typeRequestBody({ verb, entity });
|
|
22838
|
+
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({ type: type2 }) : "never";
|
|
22839
|
+
const typeResponseBody = transformer.typeResponseBody({ verb, entity });
|
|
22836
22840
|
const statusCode = statusCodes[method] ?? 200;
|
|
22837
22841
|
const mime = (operation.responses?.[statusCode]).content?.["application/json"] ? "application/json" : "*/*";
|
|
22838
|
-
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue(type2, statusCode, mime) : "never";
|
|
22842
|
+
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
|
|
22839
22843
|
payloads3.push({
|
|
22840
22844
|
fn,
|
|
22841
|
-
path: path13,
|
|
22842
22845
|
url: url2,
|
|
22843
22846
|
method,
|
|
22844
22847
|
verb,
|
|
@@ -22857,13 +22860,13 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
22857
22860
|
payloads2.push(...childPayloads);
|
|
22858
22861
|
return payloads2;
|
|
22859
22862
|
}, []);
|
|
22860
|
-
apiModules2.push({ name: transformer.moduleName(name), payloads });
|
|
22863
|
+
apiModules2.push({ name: transformer.moduleName({ name }), payloads });
|
|
22861
22864
|
return apiModules2;
|
|
22862
22865
|
}, []);
|
|
22863
22866
|
return apiModules;
|
|
22864
22867
|
}
|
|
22865
22868
|
function renderApiModules(apiModules, options8) {
|
|
22866
|
-
const { output, ts,
|
|
22869
|
+
const { output, ts, overrides, preset } = options8;
|
|
22867
22870
|
const templateFile = readTemplateFile(preset);
|
|
22868
22871
|
const typesFilename = options8.typesFilename.replace(".ts", "");
|
|
22869
22872
|
return Promise.all(
|
|
@@ -22881,7 +22884,8 @@ function renderApiModules(apiModules, options8) {
|
|
|
22881
22884
|
printWidth: 120
|
|
22882
22885
|
}).then((content) => {
|
|
22883
22886
|
const path13 = resolve3(output, `${apiModule.name}.${ts ? "ts" : "js"}`);
|
|
22884
|
-
|
|
22887
|
+
const shouldSkip = (!overrides || isArray(overrides) && !overrides.includes(apiModule.name)) && fse.existsSync(path13);
|
|
22888
|
+
if (shouldSkip) {
|
|
22885
22889
|
logger.warn(`File already exists, skip: ${path13}`);
|
|
22886
22890
|
promiseResolve(content);
|
|
22887
22891
|
return;
|
|
@@ -22907,7 +22911,7 @@ async function generate(userOptions = {}) {
|
|
|
22907
22911
|
const {
|
|
22908
22912
|
base,
|
|
22909
22913
|
ts = true,
|
|
22910
|
-
|
|
22914
|
+
overrides = true,
|
|
22911
22915
|
preset = "axle",
|
|
22912
22916
|
statusCodeStrategy = "strict",
|
|
22913
22917
|
input = "./schema.json",
|
|
@@ -22926,7 +22930,7 @@ async function generate(userOptions = {}) {
|
|
|
22926
22930
|
await generateTypes(schema2, output, typesFilename);
|
|
22927
22931
|
}
|
|
22928
22932
|
const apiModules = partitionApiModules(schema2, mergedTransformer, { statusCodes, ts, base });
|
|
22929
|
-
await renderApiModules(apiModules, { output, typesFilename, ts,
|
|
22933
|
+
await renderApiModules(apiModules, { output, typesFilename, ts, overrides, preset });
|
|
22930
22934
|
logger.success("Done");
|
|
22931
22935
|
}
|
|
22932
22936
|
|
package/dist/cli.cjs
CHANGED
|
@@ -101995,10 +101995,10 @@ var init_config = __esm({
|
|
|
101995
101995
|
});
|
|
101996
101996
|
|
|
101997
101997
|
// src/transformer.ts
|
|
101998
|
-
function transformModuleName(name) {
|
|
101998
|
+
function transformModuleName({ name }) {
|
|
101999
101999
|
return (0, import_rattail.camelize)(name);
|
|
102000
102000
|
}
|
|
102001
|
-
function transformVerb(method) {
|
|
102001
|
+
function transformVerb({ method }) {
|
|
102002
102002
|
switch (method) {
|
|
102003
102003
|
case "post":
|
|
102004
102004
|
return "Create";
|
|
@@ -102008,10 +102008,10 @@ function transformVerb(method) {
|
|
|
102008
102008
|
return (0, import_rattail.pascalCase)(method);
|
|
102009
102009
|
}
|
|
102010
102010
|
}
|
|
102011
|
-
function transformUrl(path13, base) {
|
|
102011
|
+
function transformUrl({ path: path13, base }) {
|
|
102012
102012
|
return (base ? path13.replace(base, "") : path13).replace(/{/g, ":").replace(/}/g, "");
|
|
102013
102013
|
}
|
|
102014
|
-
function transformEntity(path13, method, base) {
|
|
102014
|
+
function transformEntity({ path: path13, method, base }) {
|
|
102015
102015
|
path13 = base ? path13.replace(base, "") : path13;
|
|
102016
102016
|
const words = path13.split("/").filter(Boolean);
|
|
102017
102017
|
return words.reduce((entity, word, index) => {
|
|
@@ -102025,31 +102025,35 @@ function transformEntity(path13, method, base) {
|
|
|
102025
102025
|
return `${entity}${word}`;
|
|
102026
102026
|
}, "");
|
|
102027
102027
|
}
|
|
102028
|
-
function transformFn(verb, entity) {
|
|
102028
|
+
function transformFn({ verb, entity }) {
|
|
102029
102029
|
return `api${verb}${entity}`;
|
|
102030
102030
|
}
|
|
102031
|
-
function transformType(verb, entity) {
|
|
102031
|
+
function transformType({ verb, entity }) {
|
|
102032
102032
|
return `Api${verb}${entity}`;
|
|
102033
102033
|
}
|
|
102034
|
-
function transformTypeValue(path13, method) {
|
|
102034
|
+
function transformTypeValue({ path: path13, method }) {
|
|
102035
102035
|
return `paths['${path13}']['${method}']`;
|
|
102036
102036
|
}
|
|
102037
|
-
function transformTypeQuery(verb, entity) {
|
|
102037
|
+
function transformTypeQuery({ verb, entity }) {
|
|
102038
102038
|
return `Api${verb}${entity}Query`;
|
|
102039
102039
|
}
|
|
102040
|
-
function transformTypeQueryValue(type2) {
|
|
102040
|
+
function transformTypeQueryValue({ type: type2 }) {
|
|
102041
102041
|
return `${type2}['parameters']['query']`;
|
|
102042
102042
|
}
|
|
102043
|
-
function transformTypeRequestBody(verb, entity) {
|
|
102043
|
+
function transformTypeRequestBody({ verb, entity }) {
|
|
102044
102044
|
return `Api${verb}${entity}RequestBody`;
|
|
102045
102045
|
}
|
|
102046
|
-
function transformTypeRequestBodyValue(type2) {
|
|
102046
|
+
function transformTypeRequestBodyValue({ type: type2 }) {
|
|
102047
102047
|
return `${type2}['requestBody']['content']['application/json']`;
|
|
102048
102048
|
}
|
|
102049
|
-
function transformTypeResponseBody(verb, entity) {
|
|
102049
|
+
function transformTypeResponseBody({ verb, entity }) {
|
|
102050
102050
|
return `Api${verb}${entity}ResponseBody`;
|
|
102051
102051
|
}
|
|
102052
|
-
function transformTypeResponseBodyValue(
|
|
102052
|
+
function transformTypeResponseBodyValue({
|
|
102053
|
+
type: type2,
|
|
102054
|
+
statusCode,
|
|
102055
|
+
mime
|
|
102056
|
+
}) {
|
|
102053
102057
|
return `${type2}['responses']['${statusCode}']['content']['${mime}']`;
|
|
102054
102058
|
}
|
|
102055
102059
|
function createTransformer() {
|
|
@@ -102097,23 +102101,22 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
102097
102101
|
path13 = base ? base + path13 : path13;
|
|
102098
102102
|
const pathItems = schemaPaths[path13];
|
|
102099
102103
|
const childPayloads = Object.entries(pathItems).reduce((payloads3, [method, operation]) => {
|
|
102100
|
-
const url2 = transformer.url(path13, base);
|
|
102101
|
-
const entity = transformer.entity(path13, method, base);
|
|
102102
|
-
const verb = transformer.verb(method);
|
|
102103
|
-
const fn8 = transformer.fn(verb, entity);
|
|
102104
|
-
const type2 = transformer.type(verb, entity);
|
|
102105
|
-
const typeValue = transformer.typeValue(path13, method);
|
|
102106
|
-
const typeQuery = transformer.typeQuery(verb, entity);
|
|
102107
|
-
const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue(type2) : "never";
|
|
102108
|
-
const typeRequestBody = transformer.typeRequestBody(verb, entity);
|
|
102109
|
-
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue(type2) : "never";
|
|
102110
|
-
const typeResponseBody = transformer.typeResponseBody(verb, entity);
|
|
102104
|
+
const url2 = transformer.url({ path: path13, base });
|
|
102105
|
+
const entity = transformer.entity({ path: path13, method, base });
|
|
102106
|
+
const verb = transformer.verb({ method });
|
|
102107
|
+
const fn8 = transformer.fn({ verb, entity });
|
|
102108
|
+
const type2 = transformer.type({ verb, entity });
|
|
102109
|
+
const typeValue = transformer.typeValue({ path: path13, method });
|
|
102110
|
+
const typeQuery = transformer.typeQuery({ verb, entity });
|
|
102111
|
+
const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue({ type: type2 }) : "never";
|
|
102112
|
+
const typeRequestBody = transformer.typeRequestBody({ verb, entity });
|
|
102113
|
+
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({ type: type2 }) : "never";
|
|
102114
|
+
const typeResponseBody = transformer.typeResponseBody({ verb, entity });
|
|
102111
102115
|
const statusCode = statusCodes[method] ?? 200;
|
|
102112
102116
|
const mime = (operation.responses?.[statusCode]).content?.["application/json"] ? "application/json" : "*/*";
|
|
102113
|
-
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue(type2, statusCode, mime) : "never";
|
|
102117
|
+
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
|
|
102114
102118
|
payloads3.push({
|
|
102115
102119
|
fn: fn8,
|
|
102116
|
-
path: path13,
|
|
102117
102120
|
url: url2,
|
|
102118
102121
|
method,
|
|
102119
102122
|
verb,
|
|
@@ -102132,13 +102135,13 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
102132
102135
|
payloads2.push(...childPayloads);
|
|
102133
102136
|
return payloads2;
|
|
102134
102137
|
}, []);
|
|
102135
|
-
apiModules2.push({ name: transformer.moduleName(name), payloads });
|
|
102138
|
+
apiModules2.push({ name: transformer.moduleName({ name }), payloads });
|
|
102136
102139
|
return apiModules2;
|
|
102137
102140
|
}, []);
|
|
102138
102141
|
return apiModules;
|
|
102139
102142
|
}
|
|
102140
102143
|
function renderApiModules(apiModules, options8) {
|
|
102141
|
-
const { output, ts: ts9,
|
|
102144
|
+
const { output, ts: ts9, overrides, preset } = options8;
|
|
102142
102145
|
const templateFile = readTemplateFile(preset);
|
|
102143
102146
|
const typesFilename = options8.typesFilename.replace(".ts", "");
|
|
102144
102147
|
return Promise.all(
|
|
@@ -102156,7 +102159,8 @@ function renderApiModules(apiModules, options8) {
|
|
|
102156
102159
|
printWidth: 120
|
|
102157
102160
|
}).then((content) => {
|
|
102158
102161
|
const path13 = (0, import_path14.resolve)(output, `${apiModule.name}.${ts9 ? "ts" : "js"}`);
|
|
102159
|
-
|
|
102162
|
+
const shouldSkip = (!overrides || (0, import_rattail2.isArray)(overrides) && !overrides.includes(apiModule.name)) && import_fs_extra2.default.existsSync(path13);
|
|
102163
|
+
if (shouldSkip) {
|
|
102160
102164
|
import_rslog.logger.warn(`File already exists, skip: ${path13}`);
|
|
102161
102165
|
promiseResolve(content);
|
|
102162
102166
|
return;
|
|
@@ -102182,7 +102186,7 @@ async function generate(userOptions = {}) {
|
|
|
102182
102186
|
const {
|
|
102183
102187
|
base,
|
|
102184
102188
|
ts: ts9 = true,
|
|
102185
|
-
|
|
102189
|
+
overrides = true,
|
|
102186
102190
|
preset = "axle",
|
|
102187
102191
|
statusCodeStrategy = "strict",
|
|
102188
102192
|
input = "./schema.json",
|
|
@@ -102201,7 +102205,7 @@ async function generate(userOptions = {}) {
|
|
|
102201
102205
|
await generateTypes(schema2, output, typesFilename);
|
|
102202
102206
|
}
|
|
102203
102207
|
const apiModules = partitionApiModules(schema2, mergedTransformer, { statusCodes, ts: ts9, base });
|
|
102204
|
-
await renderApiModules(apiModules, { output, typesFilename, ts: ts9,
|
|
102208
|
+
await renderApiModules(apiModules, { output, typesFilename, ts: ts9, overrides, preset });
|
|
102205
102209
|
import_rslog.logger.success("Done");
|
|
102206
102210
|
}
|
|
102207
102211
|
var import_path14, import_ejs, import_fs_extra2, import_openapi_typescript, import_rattail2, import_rslog;
|
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-23Q2CMLJ.js");
|
|
13
13
|
return generate();
|
|
14
14
|
});
|
|
15
15
|
program.parse();
|
package/dist/index.cjs
CHANGED
|
@@ -79260,10 +79260,10 @@ init_cjs_shims();
|
|
|
79260
79260
|
init_cjs_shims();
|
|
79261
79261
|
var import_pluralize = __toESM(require("pluralize"), 1);
|
|
79262
79262
|
var import_rattail = require("rattail");
|
|
79263
|
-
function transformModuleName(name) {
|
|
79263
|
+
function transformModuleName({ name }) {
|
|
79264
79264
|
return (0, import_rattail.camelize)(name);
|
|
79265
79265
|
}
|
|
79266
|
-
function transformVerb(method) {
|
|
79266
|
+
function transformVerb({ method }) {
|
|
79267
79267
|
switch (method) {
|
|
79268
79268
|
case "post":
|
|
79269
79269
|
return "Create";
|
|
@@ -79273,10 +79273,10 @@ function transformVerb(method) {
|
|
|
79273
79273
|
return (0, import_rattail.pascalCase)(method);
|
|
79274
79274
|
}
|
|
79275
79275
|
}
|
|
79276
|
-
function transformUrl(path13, base) {
|
|
79276
|
+
function transformUrl({ path: path13, base }) {
|
|
79277
79277
|
return (base ? path13.replace(base, "") : path13).replace(/{/g, ":").replace(/}/g, "");
|
|
79278
79278
|
}
|
|
79279
|
-
function transformEntity(path13, method, base) {
|
|
79279
|
+
function transformEntity({ path: path13, method, base }) {
|
|
79280
79280
|
path13 = base ? path13.replace(base, "") : path13;
|
|
79281
79281
|
const words = path13.split("/").filter(Boolean);
|
|
79282
79282
|
return words.reduce((entity, word, index) => {
|
|
@@ -79290,31 +79290,35 @@ function transformEntity(path13, method, base) {
|
|
|
79290
79290
|
return `${entity}${word}`;
|
|
79291
79291
|
}, "");
|
|
79292
79292
|
}
|
|
79293
|
-
function transformFn(verb, entity) {
|
|
79293
|
+
function transformFn({ verb, entity }) {
|
|
79294
79294
|
return `api${verb}${entity}`;
|
|
79295
79295
|
}
|
|
79296
|
-
function transformType(verb, entity) {
|
|
79296
|
+
function transformType({ verb, entity }) {
|
|
79297
79297
|
return `Api${verb}${entity}`;
|
|
79298
79298
|
}
|
|
79299
|
-
function transformTypeValue(path13, method) {
|
|
79299
|
+
function transformTypeValue({ path: path13, method }) {
|
|
79300
79300
|
return `paths['${path13}']['${method}']`;
|
|
79301
79301
|
}
|
|
79302
|
-
function transformTypeQuery(verb, entity) {
|
|
79302
|
+
function transformTypeQuery({ verb, entity }) {
|
|
79303
79303
|
return `Api${verb}${entity}Query`;
|
|
79304
79304
|
}
|
|
79305
|
-
function transformTypeQueryValue(type2) {
|
|
79305
|
+
function transformTypeQueryValue({ type: type2 }) {
|
|
79306
79306
|
return `${type2}['parameters']['query']`;
|
|
79307
79307
|
}
|
|
79308
|
-
function transformTypeRequestBody(verb, entity) {
|
|
79308
|
+
function transformTypeRequestBody({ verb, entity }) {
|
|
79309
79309
|
return `Api${verb}${entity}RequestBody`;
|
|
79310
79310
|
}
|
|
79311
|
-
function transformTypeRequestBodyValue(type2) {
|
|
79311
|
+
function transformTypeRequestBodyValue({ type: type2 }) {
|
|
79312
79312
|
return `${type2}['requestBody']['content']['application/json']`;
|
|
79313
79313
|
}
|
|
79314
|
-
function transformTypeResponseBody(verb, entity) {
|
|
79314
|
+
function transformTypeResponseBody({ verb, entity }) {
|
|
79315
79315
|
return `Api${verb}${entity}ResponseBody`;
|
|
79316
79316
|
}
|
|
79317
|
-
function transformTypeResponseBodyValue(
|
|
79317
|
+
function transformTypeResponseBodyValue({
|
|
79318
|
+
type: type2,
|
|
79319
|
+
statusCode,
|
|
79320
|
+
mime
|
|
79321
|
+
}) {
|
|
79318
79322
|
return `${type2}['responses']['${statusCode}']['content']['${mime}']`;
|
|
79319
79323
|
}
|
|
79320
79324
|
function createTransformer() {
|
|
@@ -102134,23 +102138,22 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
102134
102138
|
path13 = base ? base + path13 : path13;
|
|
102135
102139
|
const pathItems = schemaPaths[path13];
|
|
102136
102140
|
const childPayloads = Object.entries(pathItems).reduce((payloads3, [method, operation]) => {
|
|
102137
|
-
const url2 = transformer.url(path13, base);
|
|
102138
|
-
const entity = transformer.entity(path13, method, base);
|
|
102139
|
-
const verb = transformer.verb(method);
|
|
102140
|
-
const fn8 = transformer.fn(verb, entity);
|
|
102141
|
-
const type2 = transformer.type(verb, entity);
|
|
102142
|
-
const typeValue = transformer.typeValue(path13, method);
|
|
102143
|
-
const typeQuery = transformer.typeQuery(verb, entity);
|
|
102144
|
-
const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue(type2) : "never";
|
|
102145
|
-
const typeRequestBody = transformer.typeRequestBody(verb, entity);
|
|
102146
|
-
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue(type2) : "never";
|
|
102147
|
-
const typeResponseBody = transformer.typeResponseBody(verb, entity);
|
|
102141
|
+
const url2 = transformer.url({ path: path13, base });
|
|
102142
|
+
const entity = transformer.entity({ path: path13, method, base });
|
|
102143
|
+
const verb = transformer.verb({ method });
|
|
102144
|
+
const fn8 = transformer.fn({ verb, entity });
|
|
102145
|
+
const type2 = transformer.type({ verb, entity });
|
|
102146
|
+
const typeValue = transformer.typeValue({ path: path13, method });
|
|
102147
|
+
const typeQuery = transformer.typeQuery({ verb, entity });
|
|
102148
|
+
const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue({ type: type2 }) : "never";
|
|
102149
|
+
const typeRequestBody = transformer.typeRequestBody({ verb, entity });
|
|
102150
|
+
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({ type: type2 }) : "never";
|
|
102151
|
+
const typeResponseBody = transformer.typeResponseBody({ verb, entity });
|
|
102148
102152
|
const statusCode = statusCodes[method] ?? 200;
|
|
102149
102153
|
const mime = (operation.responses?.[statusCode]).content?.["application/json"] ? "application/json" : "*/*";
|
|
102150
|
-
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue(type2, statusCode, mime) : "never";
|
|
102154
|
+
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
|
|
102151
102155
|
payloads3.push({
|
|
102152
102156
|
fn: fn8,
|
|
102153
|
-
path: path13,
|
|
102154
102157
|
url: url2,
|
|
102155
102158
|
method,
|
|
102156
102159
|
verb,
|
|
@@ -102169,13 +102172,13 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
102169
102172
|
payloads2.push(...childPayloads);
|
|
102170
102173
|
return payloads2;
|
|
102171
102174
|
}, []);
|
|
102172
|
-
apiModules2.push({ name: transformer.moduleName(name), payloads });
|
|
102175
|
+
apiModules2.push({ name: transformer.moduleName({ name }), payloads });
|
|
102173
102176
|
return apiModules2;
|
|
102174
102177
|
}, []);
|
|
102175
102178
|
return apiModules;
|
|
102176
102179
|
}
|
|
102177
102180
|
function renderApiModules(apiModules, options8) {
|
|
102178
|
-
const { output, ts: ts9,
|
|
102181
|
+
const { output, ts: ts9, overrides, preset } = options8;
|
|
102179
102182
|
const templateFile = readTemplateFile(preset);
|
|
102180
102183
|
const typesFilename = options8.typesFilename.replace(".ts", "");
|
|
102181
102184
|
return Promise.all(
|
|
@@ -102193,7 +102196,8 @@ function renderApiModules(apiModules, options8) {
|
|
|
102193
102196
|
printWidth: 120
|
|
102194
102197
|
}).then((content) => {
|
|
102195
102198
|
const path13 = (0, import_path14.resolve)(output, `${apiModule.name}.${ts9 ? "ts" : "js"}`);
|
|
102196
|
-
|
|
102199
|
+
const shouldSkip = (!overrides || (0, import_rattail2.isArray)(overrides) && !overrides.includes(apiModule.name)) && import_fs_extra2.default.existsSync(path13);
|
|
102200
|
+
if (shouldSkip) {
|
|
102197
102201
|
import_rslog.logger.warn(`File already exists, skip: ${path13}`);
|
|
102198
102202
|
promiseResolve(content);
|
|
102199
102203
|
return;
|
|
@@ -102219,7 +102223,7 @@ async function generate(userOptions = {}) {
|
|
|
102219
102223
|
const {
|
|
102220
102224
|
base,
|
|
102221
102225
|
ts: ts9 = true,
|
|
102222
|
-
|
|
102226
|
+
overrides = true,
|
|
102223
102227
|
preset = "axle",
|
|
102224
102228
|
statusCodeStrategy = "strict",
|
|
102225
102229
|
input = "./schema.json",
|
|
@@ -102238,7 +102242,7 @@ async function generate(userOptions = {}) {
|
|
|
102238
102242
|
await generateTypes(schema2, output, typesFilename);
|
|
102239
102243
|
}
|
|
102240
102244
|
const apiModules = partitionApiModules(schema2, mergedTransformer, { statusCodes, ts: ts9, base });
|
|
102241
|
-
await renderApiModules(apiModules, { output, typesFilename, ts: ts9,
|
|
102245
|
+
await renderApiModules(apiModules, { output, typesFilename, ts: ts9, overrides, preset });
|
|
102242
102246
|
import_rslog.logger.success("Done");
|
|
102243
102247
|
}
|
|
102244
102248
|
|
package/dist/index.d.cts
CHANGED
|
@@ -1,19 +1,56 @@
|
|
|
1
1
|
import { OpenAPI3, OperationObject } from 'openapi-typescript';
|
|
2
2
|
export { default as pluralize } from 'pluralize';
|
|
3
3
|
|
|
4
|
-
declare function transformModuleName(name
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
declare function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
declare function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
declare function
|
|
15
|
-
|
|
16
|
-
|
|
4
|
+
declare function transformModuleName({ name }: {
|
|
5
|
+
name: string;
|
|
6
|
+
}): string;
|
|
7
|
+
declare function transformVerb({ method }: {
|
|
8
|
+
method: string;
|
|
9
|
+
}): string;
|
|
10
|
+
declare function transformUrl({ path, base }: {
|
|
11
|
+
path: string;
|
|
12
|
+
base?: string;
|
|
13
|
+
}): string;
|
|
14
|
+
declare function transformEntity({ path, method, base }: {
|
|
15
|
+
path: string;
|
|
16
|
+
method: string;
|
|
17
|
+
base?: string;
|
|
18
|
+
}): string;
|
|
19
|
+
declare function transformFn({ verb, entity }: {
|
|
20
|
+
verb: string;
|
|
21
|
+
entity: string;
|
|
22
|
+
}): string;
|
|
23
|
+
declare function transformType({ verb, entity }: {
|
|
24
|
+
verb: string;
|
|
25
|
+
entity: string;
|
|
26
|
+
}): string;
|
|
27
|
+
declare function transformTypeValue({ path, method }: {
|
|
28
|
+
path: string;
|
|
29
|
+
method: string;
|
|
30
|
+
}): string;
|
|
31
|
+
declare function transformTypeQuery({ verb, entity }: {
|
|
32
|
+
verb: string;
|
|
33
|
+
entity: string;
|
|
34
|
+
}): string;
|
|
35
|
+
declare function transformTypeQueryValue({ type }: {
|
|
36
|
+
type: string;
|
|
37
|
+
}): string;
|
|
38
|
+
declare function transformTypeRequestBody({ verb, entity }: {
|
|
39
|
+
verb: string;
|
|
40
|
+
entity: string;
|
|
41
|
+
}): string;
|
|
42
|
+
declare function transformTypeRequestBodyValue({ type }: {
|
|
43
|
+
type: string;
|
|
44
|
+
}): string;
|
|
45
|
+
declare function transformTypeResponseBody({ verb, entity }: {
|
|
46
|
+
verb: string;
|
|
47
|
+
entity: string;
|
|
48
|
+
}): string;
|
|
49
|
+
declare function transformTypeResponseBodyValue({ type, statusCode, mime, }: {
|
|
50
|
+
type: string;
|
|
51
|
+
statusCode: number;
|
|
52
|
+
mime: string;
|
|
53
|
+
}): string;
|
|
17
54
|
interface Transformer {
|
|
18
55
|
moduleName: typeof transformModuleName;
|
|
19
56
|
verb: typeof transformVerb;
|
|
@@ -65,41 +102,124 @@ declare function hasQueryParameter(operation: OperationObject): boolean;
|
|
|
65
102
|
declare function hasResponseBody(operation: OperationObject): boolean;
|
|
66
103
|
declare function getCliVersion(): any;
|
|
67
104
|
|
|
105
|
+
interface ApiModuleTemplateData {
|
|
106
|
+
/**
|
|
107
|
+
* API module metadata
|
|
108
|
+
*/
|
|
109
|
+
apiModule: ApiModule;
|
|
110
|
+
/**
|
|
111
|
+
* The name of the generated api ts type aggregation file
|
|
112
|
+
*/
|
|
113
|
+
typesFilename: string;
|
|
114
|
+
/**
|
|
115
|
+
* Whether to generate ts code
|
|
116
|
+
*/
|
|
117
|
+
ts: boolean;
|
|
118
|
+
}
|
|
119
|
+
interface ApiModule {
|
|
120
|
+
/**
|
|
121
|
+
* The name of the API module
|
|
122
|
+
*/
|
|
123
|
+
name: string;
|
|
124
|
+
/**
|
|
125
|
+
* API module payloads
|
|
126
|
+
*/
|
|
127
|
+
payloads: ApiModulePayload[];
|
|
128
|
+
}
|
|
68
129
|
interface ApiModulePayload {
|
|
130
|
+
/**
|
|
131
|
+
* The name of the API function/dispatcher, such as apiGetUsers, apiCreatePost, apiUpdateComment, etc.
|
|
132
|
+
*/
|
|
69
133
|
fn: string;
|
|
70
|
-
|
|
134
|
+
/**
|
|
135
|
+
* The URL of the API endpoint, such as /users, /posts, /comments, etc.
|
|
136
|
+
*/
|
|
71
137
|
url: string;
|
|
138
|
+
/**
|
|
139
|
+
* The HTTP method of the API endpoint, such as get, post, put, delete, etc.
|
|
140
|
+
*/
|
|
72
141
|
method: string;
|
|
142
|
+
/**
|
|
143
|
+
* The HTTP verb of the API endpoint, such as Get, Create, Update, Delete, etc.
|
|
144
|
+
*/
|
|
73
145
|
verb: string;
|
|
146
|
+
/**
|
|
147
|
+
* The entity name of the API endpoint, such as User, Comment, Post, etc.
|
|
148
|
+
*/
|
|
74
149
|
entity: string;
|
|
150
|
+
/**
|
|
151
|
+
* The type name of the API endpoint, such as ApiGetUsers, ApiCreatePost, ApiUpdateComment, etc.
|
|
152
|
+
*/
|
|
75
153
|
type: string;
|
|
154
|
+
/**
|
|
155
|
+
* The value of the type of the API endpoint, such as paths['/users']['get'], paths['/posts']['post'], paths['/comments']['put'], etc.
|
|
156
|
+
*/
|
|
76
157
|
typeValue: string;
|
|
158
|
+
/**
|
|
159
|
+
* The type name of the query parameters of the API endpoint, such as ApiGetUsersQuery, ApiCreatePostQuery, ApiUpdateCommentQuery, etc.
|
|
160
|
+
*/
|
|
77
161
|
typeQuery: string;
|
|
162
|
+
/**
|
|
163
|
+
* 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.
|
|
164
|
+
*/
|
|
78
165
|
typeQueryValue: string;
|
|
166
|
+
/**
|
|
167
|
+
* The type name of the request body of the API endpoint, such as ApiGetUsersRequestBody, ApiCreatePostRequestBody, ApiUpdateCommentRequestBody, etc.
|
|
168
|
+
*/
|
|
79
169
|
typeRequestBody: string;
|
|
170
|
+
/**
|
|
171
|
+
* 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.
|
|
172
|
+
*/
|
|
80
173
|
typeRequestBodyValue: string;
|
|
174
|
+
/**
|
|
175
|
+
* The type name of the response body of the API endpoint, such as ApiGetUsersResponseBody, ApiCreatePostResponseBody, ApiUpdateCommentResponseBody, etc.
|
|
176
|
+
*/
|
|
81
177
|
typeResponseBody: string;
|
|
178
|
+
/**
|
|
179
|
+
* 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.
|
|
180
|
+
*/
|
|
82
181
|
typeResponseBodyValue: string;
|
|
83
182
|
}
|
|
84
|
-
interface ApiModule {
|
|
85
|
-
name: string;
|
|
86
|
-
payloads: ApiModulePayload[];
|
|
87
|
-
}
|
|
88
|
-
interface ApiModuleTemplateData {
|
|
89
|
-
apiModule: ApiModule;
|
|
90
|
-
typesFilename: string;
|
|
91
|
-
ts: boolean;
|
|
92
|
-
}
|
|
93
183
|
interface GenerateOptions {
|
|
184
|
+
/**
|
|
185
|
+
* The path to the OpenAPI/Swagger schema file.
|
|
186
|
+
*/
|
|
94
187
|
input?: string;
|
|
188
|
+
/**
|
|
189
|
+
* The path to the output directory.
|
|
190
|
+
*/
|
|
95
191
|
output?: string;
|
|
192
|
+
/**
|
|
193
|
+
* The base path of the API endpoints.
|
|
194
|
+
*/
|
|
96
195
|
base?: string;
|
|
196
|
+
/**
|
|
197
|
+
* The filename of the generated openapi types file.
|
|
198
|
+
*/
|
|
97
199
|
typesFilename?: string;
|
|
200
|
+
/**
|
|
201
|
+
* The transformer api options, used to override the default transformation rules.
|
|
202
|
+
*/
|
|
98
203
|
transformer?: Partial<Transformer>;
|
|
204
|
+
/**
|
|
205
|
+
* Whether to generate TypeScript code.
|
|
206
|
+
*/
|
|
99
207
|
ts?: boolean;
|
|
100
|
-
|
|
208
|
+
/**
|
|
209
|
+
* Whether to override the existing files, or an array of filenames to override.
|
|
210
|
+
*/
|
|
211
|
+
overrides?: boolean | string[];
|
|
212
|
+
/**
|
|
213
|
+
* The preset ejs template to use.
|
|
214
|
+
*/
|
|
101
215
|
preset?: Preset;
|
|
216
|
+
/**
|
|
217
|
+
* The status code strategy to use. loose: all success status codes are 200, strict: use the openapi recommended success status codes.
|
|
218
|
+
*/
|
|
102
219
|
statusCodeStrategy?: StatusCodeStrategy;
|
|
220
|
+
/**
|
|
221
|
+
* The status codes to override the default status codes.
|
|
222
|
+
*/
|
|
103
223
|
statusCodes?: {
|
|
104
224
|
get?: number;
|
|
105
225
|
post?: number;
|
|
@@ -119,7 +239,7 @@ declare function renderApiModules(apiModules: ApiModule[], options: {
|
|
|
119
239
|
output: string;
|
|
120
240
|
typesFilename: string;
|
|
121
241
|
ts: boolean;
|
|
122
|
-
|
|
242
|
+
overrides: boolean | string[];
|
|
123
243
|
preset: Preset;
|
|
124
244
|
}): Promise<unknown[]>;
|
|
125
245
|
declare function generateTypes(schema: OpenAPI3, output: string, typesFilename: string): Promise<void>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,19 +1,56 @@
|
|
|
1
1
|
import { OpenAPI3, OperationObject } from 'openapi-typescript';
|
|
2
2
|
export { default as pluralize } from 'pluralize';
|
|
3
3
|
|
|
4
|
-
declare function transformModuleName(name
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
declare function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
declare function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
declare function
|
|
15
|
-
|
|
16
|
-
|
|
4
|
+
declare function transformModuleName({ name }: {
|
|
5
|
+
name: string;
|
|
6
|
+
}): string;
|
|
7
|
+
declare function transformVerb({ method }: {
|
|
8
|
+
method: string;
|
|
9
|
+
}): string;
|
|
10
|
+
declare function transformUrl({ path, base }: {
|
|
11
|
+
path: string;
|
|
12
|
+
base?: string;
|
|
13
|
+
}): string;
|
|
14
|
+
declare function transformEntity({ path, method, base }: {
|
|
15
|
+
path: string;
|
|
16
|
+
method: string;
|
|
17
|
+
base?: string;
|
|
18
|
+
}): string;
|
|
19
|
+
declare function transformFn({ verb, entity }: {
|
|
20
|
+
verb: string;
|
|
21
|
+
entity: string;
|
|
22
|
+
}): string;
|
|
23
|
+
declare function transformType({ verb, entity }: {
|
|
24
|
+
verb: string;
|
|
25
|
+
entity: string;
|
|
26
|
+
}): string;
|
|
27
|
+
declare function transformTypeValue({ path, method }: {
|
|
28
|
+
path: string;
|
|
29
|
+
method: string;
|
|
30
|
+
}): string;
|
|
31
|
+
declare function transformTypeQuery({ verb, entity }: {
|
|
32
|
+
verb: string;
|
|
33
|
+
entity: string;
|
|
34
|
+
}): string;
|
|
35
|
+
declare function transformTypeQueryValue({ type }: {
|
|
36
|
+
type: string;
|
|
37
|
+
}): string;
|
|
38
|
+
declare function transformTypeRequestBody({ verb, entity }: {
|
|
39
|
+
verb: string;
|
|
40
|
+
entity: string;
|
|
41
|
+
}): string;
|
|
42
|
+
declare function transformTypeRequestBodyValue({ type }: {
|
|
43
|
+
type: string;
|
|
44
|
+
}): string;
|
|
45
|
+
declare function transformTypeResponseBody({ verb, entity }: {
|
|
46
|
+
verb: string;
|
|
47
|
+
entity: string;
|
|
48
|
+
}): string;
|
|
49
|
+
declare function transformTypeResponseBodyValue({ type, statusCode, mime, }: {
|
|
50
|
+
type: string;
|
|
51
|
+
statusCode: number;
|
|
52
|
+
mime: string;
|
|
53
|
+
}): string;
|
|
17
54
|
interface Transformer {
|
|
18
55
|
moduleName: typeof transformModuleName;
|
|
19
56
|
verb: typeof transformVerb;
|
|
@@ -65,41 +102,124 @@ declare function hasQueryParameter(operation: OperationObject): boolean;
|
|
|
65
102
|
declare function hasResponseBody(operation: OperationObject): boolean;
|
|
66
103
|
declare function getCliVersion(): any;
|
|
67
104
|
|
|
105
|
+
interface ApiModuleTemplateData {
|
|
106
|
+
/**
|
|
107
|
+
* API module metadata
|
|
108
|
+
*/
|
|
109
|
+
apiModule: ApiModule;
|
|
110
|
+
/**
|
|
111
|
+
* The name of the generated api ts type aggregation file
|
|
112
|
+
*/
|
|
113
|
+
typesFilename: string;
|
|
114
|
+
/**
|
|
115
|
+
* Whether to generate ts code
|
|
116
|
+
*/
|
|
117
|
+
ts: boolean;
|
|
118
|
+
}
|
|
119
|
+
interface ApiModule {
|
|
120
|
+
/**
|
|
121
|
+
* The name of the API module
|
|
122
|
+
*/
|
|
123
|
+
name: string;
|
|
124
|
+
/**
|
|
125
|
+
* API module payloads
|
|
126
|
+
*/
|
|
127
|
+
payloads: ApiModulePayload[];
|
|
128
|
+
}
|
|
68
129
|
interface ApiModulePayload {
|
|
130
|
+
/**
|
|
131
|
+
* The name of the API function/dispatcher, such as apiGetUsers, apiCreatePost, apiUpdateComment, etc.
|
|
132
|
+
*/
|
|
69
133
|
fn: string;
|
|
70
|
-
|
|
134
|
+
/**
|
|
135
|
+
* The URL of the API endpoint, such as /users, /posts, /comments, etc.
|
|
136
|
+
*/
|
|
71
137
|
url: string;
|
|
138
|
+
/**
|
|
139
|
+
* The HTTP method of the API endpoint, such as get, post, put, delete, etc.
|
|
140
|
+
*/
|
|
72
141
|
method: string;
|
|
142
|
+
/**
|
|
143
|
+
* The HTTP verb of the API endpoint, such as Get, Create, Update, Delete, etc.
|
|
144
|
+
*/
|
|
73
145
|
verb: string;
|
|
146
|
+
/**
|
|
147
|
+
* The entity name of the API endpoint, such as User, Comment, Post, etc.
|
|
148
|
+
*/
|
|
74
149
|
entity: string;
|
|
150
|
+
/**
|
|
151
|
+
* The type name of the API endpoint, such as ApiGetUsers, ApiCreatePost, ApiUpdateComment, etc.
|
|
152
|
+
*/
|
|
75
153
|
type: string;
|
|
154
|
+
/**
|
|
155
|
+
* The value of the type of the API endpoint, such as paths['/users']['get'], paths['/posts']['post'], paths['/comments']['put'], etc.
|
|
156
|
+
*/
|
|
76
157
|
typeValue: string;
|
|
158
|
+
/**
|
|
159
|
+
* The type name of the query parameters of the API endpoint, such as ApiGetUsersQuery, ApiCreatePostQuery, ApiUpdateCommentQuery, etc.
|
|
160
|
+
*/
|
|
77
161
|
typeQuery: string;
|
|
162
|
+
/**
|
|
163
|
+
* 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.
|
|
164
|
+
*/
|
|
78
165
|
typeQueryValue: string;
|
|
166
|
+
/**
|
|
167
|
+
* The type name of the request body of the API endpoint, such as ApiGetUsersRequestBody, ApiCreatePostRequestBody, ApiUpdateCommentRequestBody, etc.
|
|
168
|
+
*/
|
|
79
169
|
typeRequestBody: string;
|
|
170
|
+
/**
|
|
171
|
+
* 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.
|
|
172
|
+
*/
|
|
80
173
|
typeRequestBodyValue: string;
|
|
174
|
+
/**
|
|
175
|
+
* The type name of the response body of the API endpoint, such as ApiGetUsersResponseBody, ApiCreatePostResponseBody, ApiUpdateCommentResponseBody, etc.
|
|
176
|
+
*/
|
|
81
177
|
typeResponseBody: string;
|
|
178
|
+
/**
|
|
179
|
+
* 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.
|
|
180
|
+
*/
|
|
82
181
|
typeResponseBodyValue: string;
|
|
83
182
|
}
|
|
84
|
-
interface ApiModule {
|
|
85
|
-
name: string;
|
|
86
|
-
payloads: ApiModulePayload[];
|
|
87
|
-
}
|
|
88
|
-
interface ApiModuleTemplateData {
|
|
89
|
-
apiModule: ApiModule;
|
|
90
|
-
typesFilename: string;
|
|
91
|
-
ts: boolean;
|
|
92
|
-
}
|
|
93
183
|
interface GenerateOptions {
|
|
184
|
+
/**
|
|
185
|
+
* The path to the OpenAPI/Swagger schema file.
|
|
186
|
+
*/
|
|
94
187
|
input?: string;
|
|
188
|
+
/**
|
|
189
|
+
* The path to the output directory.
|
|
190
|
+
*/
|
|
95
191
|
output?: string;
|
|
192
|
+
/**
|
|
193
|
+
* The base path of the API endpoints.
|
|
194
|
+
*/
|
|
96
195
|
base?: string;
|
|
196
|
+
/**
|
|
197
|
+
* The filename of the generated openapi types file.
|
|
198
|
+
*/
|
|
97
199
|
typesFilename?: string;
|
|
200
|
+
/**
|
|
201
|
+
* The transformer api options, used to override the default transformation rules.
|
|
202
|
+
*/
|
|
98
203
|
transformer?: Partial<Transformer>;
|
|
204
|
+
/**
|
|
205
|
+
* Whether to generate TypeScript code.
|
|
206
|
+
*/
|
|
99
207
|
ts?: boolean;
|
|
100
|
-
|
|
208
|
+
/**
|
|
209
|
+
* Whether to override the existing files, or an array of filenames to override.
|
|
210
|
+
*/
|
|
211
|
+
overrides?: boolean | string[];
|
|
212
|
+
/**
|
|
213
|
+
* The preset ejs template to use.
|
|
214
|
+
*/
|
|
101
215
|
preset?: Preset;
|
|
216
|
+
/**
|
|
217
|
+
* The status code strategy to use. loose: all success status codes are 200, strict: use the openapi recommended success status codes.
|
|
218
|
+
*/
|
|
102
219
|
statusCodeStrategy?: StatusCodeStrategy;
|
|
220
|
+
/**
|
|
221
|
+
* The status codes to override the default status codes.
|
|
222
|
+
*/
|
|
103
223
|
statusCodes?: {
|
|
104
224
|
get?: number;
|
|
105
225
|
post?: number;
|
|
@@ -119,7 +239,7 @@ declare function renderApiModules(apiModules: ApiModule[], options: {
|
|
|
119
239
|
output: string;
|
|
120
240
|
typesFilename: string;
|
|
121
241
|
ts: boolean;
|
|
122
|
-
|
|
242
|
+
overrides: boolean | string[];
|
|
123
243
|
preset: Preset;
|
|
124
244
|
}): Promise<unknown[]>;
|
|
125
245
|
declare function generateTypes(schema: OpenAPI3, output: string, typesFilename: string): Promise<void>;
|
package/dist/index.js
CHANGED