api-farmer 0.0.2 → 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-HMKED3BC.js → chunk-ZLLNTCL2.js} +40 -31
- package/dist/cli.cjs +38 -30
- package/dist/cli.js +1 -1
- package/dist/{generate-3DZVV2U4.js → generate-23Q2CMLJ.js} +1 -1
- package/dist/index.cjs +40 -30
- package/dist/index.d.cts +147 -25
- package/dist/index.d.ts +147 -25
- package/dist/index.js +3 -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
|
+
```
|
|
@@ -22735,8 +22735,11 @@ async function getConfig() {
|
|
|
22735
22735
|
|
|
22736
22736
|
// src/transformer.ts
|
|
22737
22737
|
import pluralize from "pluralize";
|
|
22738
|
-
import { pascalCase } from "rattail";
|
|
22739
|
-
function
|
|
22738
|
+
import { camelize, pascalCase } from "rattail";
|
|
22739
|
+
function transformModuleName({ name }) {
|
|
22740
|
+
return camelize(name);
|
|
22741
|
+
}
|
|
22742
|
+
function transformVerb({ method }) {
|
|
22740
22743
|
switch (method) {
|
|
22741
22744
|
case "post":
|
|
22742
22745
|
return "Create";
|
|
@@ -22746,10 +22749,10 @@ function transformVerb(method) {
|
|
|
22746
22749
|
return pascalCase(method);
|
|
22747
22750
|
}
|
|
22748
22751
|
}
|
|
22749
|
-
function transformUrl(path13, base) {
|
|
22752
|
+
function transformUrl({ path: path13, base }) {
|
|
22750
22753
|
return (base ? path13.replace(base, "") : path13).replace(/{/g, ":").replace(/}/g, "");
|
|
22751
22754
|
}
|
|
22752
|
-
function transformEntity(path13, method, base) {
|
|
22755
|
+
function transformEntity({ path: path13, method, base }) {
|
|
22753
22756
|
path13 = base ? path13.replace(base, "") : path13;
|
|
22754
22757
|
const words = path13.split("/").filter(Boolean);
|
|
22755
22758
|
return words.reduce((entity, word, index) => {
|
|
@@ -22763,35 +22766,40 @@ function transformEntity(path13, method, base) {
|
|
|
22763
22766
|
return `${entity}${word}`;
|
|
22764
22767
|
}, "");
|
|
22765
22768
|
}
|
|
22766
|
-
function transformFn(verb, entity) {
|
|
22769
|
+
function transformFn({ verb, entity }) {
|
|
22767
22770
|
return `api${verb}${entity}`;
|
|
22768
22771
|
}
|
|
22769
|
-
function transformType(verb, entity) {
|
|
22772
|
+
function transformType({ verb, entity }) {
|
|
22770
22773
|
return `Api${verb}${entity}`;
|
|
22771
22774
|
}
|
|
22772
|
-
function transformTypeValue(path13, method) {
|
|
22775
|
+
function transformTypeValue({ path: path13, method }) {
|
|
22773
22776
|
return `paths['${path13}']['${method}']`;
|
|
22774
22777
|
}
|
|
22775
|
-
function transformTypeQuery(verb, entity) {
|
|
22778
|
+
function transformTypeQuery({ verb, entity }) {
|
|
22776
22779
|
return `Api${verb}${entity}Query`;
|
|
22777
22780
|
}
|
|
22778
|
-
function transformTypeQueryValue(type2) {
|
|
22781
|
+
function transformTypeQueryValue({ type: type2 }) {
|
|
22779
22782
|
return `${type2}['parameters']['query']`;
|
|
22780
22783
|
}
|
|
22781
|
-
function transformTypeRequestBody(verb, entity) {
|
|
22784
|
+
function transformTypeRequestBody({ verb, entity }) {
|
|
22782
22785
|
return `Api${verb}${entity}RequestBody`;
|
|
22783
22786
|
}
|
|
22784
|
-
function transformTypeRequestBodyValue(type2) {
|
|
22787
|
+
function transformTypeRequestBodyValue({ type: type2 }) {
|
|
22785
22788
|
return `${type2}['requestBody']['content']['application/json']`;
|
|
22786
22789
|
}
|
|
22787
|
-
function transformTypeResponseBody(verb, entity) {
|
|
22790
|
+
function transformTypeResponseBody({ verb, entity }) {
|
|
22788
22791
|
return `Api${verb}${entity}ResponseBody`;
|
|
22789
22792
|
}
|
|
22790
|
-
function transformTypeResponseBodyValue(
|
|
22793
|
+
function transformTypeResponseBodyValue({
|
|
22794
|
+
type: type2,
|
|
22795
|
+
statusCode,
|
|
22796
|
+
mime
|
|
22797
|
+
}) {
|
|
22791
22798
|
return `${type2}['responses']['${statusCode}']['content']['${mime}']`;
|
|
22792
22799
|
}
|
|
22793
22800
|
function createTransformer() {
|
|
22794
22801
|
return {
|
|
22802
|
+
moduleName: transformModuleName,
|
|
22795
22803
|
verb: transformVerb,
|
|
22796
22804
|
url: transformUrl,
|
|
22797
22805
|
entity: transformEntity,
|
|
@@ -22818,23 +22826,22 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
22818
22826
|
path13 = base ? base + path13 : path13;
|
|
22819
22827
|
const pathItems = schemaPaths[path13];
|
|
22820
22828
|
const childPayloads = Object.entries(pathItems).reduce((payloads3, [method, operation]) => {
|
|
22821
|
-
const url2 = transformer.url(path13, base);
|
|
22822
|
-
const entity = transformer.entity(path13, method, base);
|
|
22823
|
-
const verb = transformer.verb(method);
|
|
22824
|
-
const fn = transformer.fn(verb, entity);
|
|
22825
|
-
const type2 = transformer.type(verb, entity);
|
|
22826
|
-
const typeValue = transformer.typeValue(path13, method);
|
|
22827
|
-
const typeQuery = transformer.typeQuery(verb, entity);
|
|
22828
|
-
const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue(type2) : "never";
|
|
22829
|
-
const typeRequestBody = transformer.typeRequestBody(verb, entity);
|
|
22830
|
-
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue(type2) : "never";
|
|
22831
|
-
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 });
|
|
22832
22840
|
const statusCode = statusCodes[method] ?? 200;
|
|
22833
22841
|
const mime = (operation.responses?.[statusCode]).content?.["application/json"] ? "application/json" : "*/*";
|
|
22834
|
-
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue(type2, statusCode, mime) : "never";
|
|
22842
|
+
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
|
|
22835
22843
|
payloads3.push({
|
|
22836
22844
|
fn,
|
|
22837
|
-
path: path13,
|
|
22838
22845
|
url: url2,
|
|
22839
22846
|
method,
|
|
22840
22847
|
verb,
|
|
@@ -22853,13 +22860,13 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
22853
22860
|
payloads2.push(...childPayloads);
|
|
22854
22861
|
return payloads2;
|
|
22855
22862
|
}, []);
|
|
22856
|
-
apiModules2.push({ name, payloads });
|
|
22863
|
+
apiModules2.push({ name: transformer.moduleName({ name }), payloads });
|
|
22857
22864
|
return apiModules2;
|
|
22858
22865
|
}, []);
|
|
22859
22866
|
return apiModules;
|
|
22860
22867
|
}
|
|
22861
22868
|
function renderApiModules(apiModules, options8) {
|
|
22862
|
-
const { output, ts,
|
|
22869
|
+
const { output, ts, overrides, preset } = options8;
|
|
22863
22870
|
const templateFile = readTemplateFile(preset);
|
|
22864
22871
|
const typesFilename = options8.typesFilename.replace(".ts", "");
|
|
22865
22872
|
return Promise.all(
|
|
@@ -22877,7 +22884,8 @@ function renderApiModules(apiModules, options8) {
|
|
|
22877
22884
|
printWidth: 120
|
|
22878
22885
|
}).then((content) => {
|
|
22879
22886
|
const path13 = resolve3(output, `${apiModule.name}.${ts ? "ts" : "js"}`);
|
|
22880
|
-
|
|
22887
|
+
const shouldSkip = (!overrides || isArray(overrides) && !overrides.includes(apiModule.name)) && fse.existsSync(path13);
|
|
22888
|
+
if (shouldSkip) {
|
|
22881
22889
|
logger.warn(`File already exists, skip: ${path13}`);
|
|
22882
22890
|
promiseResolve(content);
|
|
22883
22891
|
return;
|
|
@@ -22903,7 +22911,7 @@ async function generate(userOptions = {}) {
|
|
|
22903
22911
|
const {
|
|
22904
22912
|
base,
|
|
22905
22913
|
ts = true,
|
|
22906
|
-
|
|
22914
|
+
overrides = true,
|
|
22907
22915
|
preset = "axle",
|
|
22908
22916
|
statusCodeStrategy = "strict",
|
|
22909
22917
|
input = "./schema.json",
|
|
@@ -22922,13 +22930,14 @@ async function generate(userOptions = {}) {
|
|
|
22922
22930
|
await generateTypes(schema2, output, typesFilename);
|
|
22923
22931
|
}
|
|
22924
22932
|
const apiModules = partitionApiModules(schema2, mergedTransformer, { statusCodes, ts, base });
|
|
22925
|
-
await renderApiModules(apiModules, { output, typesFilename, ts,
|
|
22933
|
+
await renderApiModules(apiModules, { output, typesFilename, ts, overrides, preset });
|
|
22926
22934
|
logger.success("Done");
|
|
22927
22935
|
}
|
|
22928
22936
|
|
|
22929
22937
|
export {
|
|
22930
22938
|
defineConfig,
|
|
22931
22939
|
getConfig,
|
|
22940
|
+
transformModuleName,
|
|
22932
22941
|
transformVerb,
|
|
22933
22942
|
transformUrl,
|
|
22934
22943
|
transformEntity,
|
package/dist/cli.cjs
CHANGED
|
@@ -101995,7 +101995,10 @@ var init_config = __esm({
|
|
|
101995
101995
|
});
|
|
101996
101996
|
|
|
101997
101997
|
// src/transformer.ts
|
|
101998
|
-
function
|
|
101998
|
+
function transformModuleName({ name }) {
|
|
101999
|
+
return (0, import_rattail.camelize)(name);
|
|
102000
|
+
}
|
|
102001
|
+
function transformVerb({ method }) {
|
|
101999
102002
|
switch (method) {
|
|
102000
102003
|
case "post":
|
|
102001
102004
|
return "Create";
|
|
@@ -102005,10 +102008,10 @@ function transformVerb(method) {
|
|
|
102005
102008
|
return (0, import_rattail.pascalCase)(method);
|
|
102006
102009
|
}
|
|
102007
102010
|
}
|
|
102008
|
-
function transformUrl(path13, base) {
|
|
102011
|
+
function transformUrl({ path: path13, base }) {
|
|
102009
102012
|
return (base ? path13.replace(base, "") : path13).replace(/{/g, ":").replace(/}/g, "");
|
|
102010
102013
|
}
|
|
102011
|
-
function transformEntity(path13, method, base) {
|
|
102014
|
+
function transformEntity({ path: path13, method, base }) {
|
|
102012
102015
|
path13 = base ? path13.replace(base, "") : path13;
|
|
102013
102016
|
const words = path13.split("/").filter(Boolean);
|
|
102014
102017
|
return words.reduce((entity, word, index) => {
|
|
@@ -102022,35 +102025,40 @@ function transformEntity(path13, method, base) {
|
|
|
102022
102025
|
return `${entity}${word}`;
|
|
102023
102026
|
}, "");
|
|
102024
102027
|
}
|
|
102025
|
-
function transformFn(verb, entity) {
|
|
102028
|
+
function transformFn({ verb, entity }) {
|
|
102026
102029
|
return `api${verb}${entity}`;
|
|
102027
102030
|
}
|
|
102028
|
-
function transformType(verb, entity) {
|
|
102031
|
+
function transformType({ verb, entity }) {
|
|
102029
102032
|
return `Api${verb}${entity}`;
|
|
102030
102033
|
}
|
|
102031
|
-
function transformTypeValue(path13, method) {
|
|
102034
|
+
function transformTypeValue({ path: path13, method }) {
|
|
102032
102035
|
return `paths['${path13}']['${method}']`;
|
|
102033
102036
|
}
|
|
102034
|
-
function transformTypeQuery(verb, entity) {
|
|
102037
|
+
function transformTypeQuery({ verb, entity }) {
|
|
102035
102038
|
return `Api${verb}${entity}Query`;
|
|
102036
102039
|
}
|
|
102037
|
-
function transformTypeQueryValue(type2) {
|
|
102040
|
+
function transformTypeQueryValue({ type: type2 }) {
|
|
102038
102041
|
return `${type2}['parameters']['query']`;
|
|
102039
102042
|
}
|
|
102040
|
-
function transformTypeRequestBody(verb, entity) {
|
|
102043
|
+
function transformTypeRequestBody({ verb, entity }) {
|
|
102041
102044
|
return `Api${verb}${entity}RequestBody`;
|
|
102042
102045
|
}
|
|
102043
|
-
function transformTypeRequestBodyValue(type2) {
|
|
102046
|
+
function transformTypeRequestBodyValue({ type: type2 }) {
|
|
102044
102047
|
return `${type2}['requestBody']['content']['application/json']`;
|
|
102045
102048
|
}
|
|
102046
|
-
function transformTypeResponseBody(verb, entity) {
|
|
102049
|
+
function transformTypeResponseBody({ verb, entity }) {
|
|
102047
102050
|
return `Api${verb}${entity}ResponseBody`;
|
|
102048
102051
|
}
|
|
102049
|
-
function transformTypeResponseBodyValue(
|
|
102052
|
+
function transformTypeResponseBodyValue({
|
|
102053
|
+
type: type2,
|
|
102054
|
+
statusCode,
|
|
102055
|
+
mime
|
|
102056
|
+
}) {
|
|
102050
102057
|
return `${type2}['responses']['${statusCode}']['content']['${mime}']`;
|
|
102051
102058
|
}
|
|
102052
102059
|
function createTransformer() {
|
|
102053
102060
|
return {
|
|
102061
|
+
moduleName: transformModuleName,
|
|
102054
102062
|
verb: transformVerb,
|
|
102055
102063
|
url: transformUrl,
|
|
102056
102064
|
entity: transformEntity,
|
|
@@ -102093,23 +102101,22 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
102093
102101
|
path13 = base ? base + path13 : path13;
|
|
102094
102102
|
const pathItems = schemaPaths[path13];
|
|
102095
102103
|
const childPayloads = Object.entries(pathItems).reduce((payloads3, [method, operation]) => {
|
|
102096
|
-
const url2 = transformer.url(path13, base);
|
|
102097
|
-
const entity = transformer.entity(path13, method, base);
|
|
102098
|
-
const verb = transformer.verb(method);
|
|
102099
|
-
const fn8 = transformer.fn(verb, entity);
|
|
102100
|
-
const type2 = transformer.type(verb, entity);
|
|
102101
|
-
const typeValue = transformer.typeValue(path13, method);
|
|
102102
|
-
const typeQuery = transformer.typeQuery(verb, entity);
|
|
102103
|
-
const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue(type2) : "never";
|
|
102104
|
-
const typeRequestBody = transformer.typeRequestBody(verb, entity);
|
|
102105
|
-
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue(type2) : "never";
|
|
102106
|
-
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 });
|
|
102107
102115
|
const statusCode = statusCodes[method] ?? 200;
|
|
102108
102116
|
const mime = (operation.responses?.[statusCode]).content?.["application/json"] ? "application/json" : "*/*";
|
|
102109
|
-
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue(type2, statusCode, mime) : "never";
|
|
102117
|
+
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
|
|
102110
102118
|
payloads3.push({
|
|
102111
102119
|
fn: fn8,
|
|
102112
|
-
path: path13,
|
|
102113
102120
|
url: url2,
|
|
102114
102121
|
method,
|
|
102115
102122
|
verb,
|
|
@@ -102128,13 +102135,13 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
102128
102135
|
payloads2.push(...childPayloads);
|
|
102129
102136
|
return payloads2;
|
|
102130
102137
|
}, []);
|
|
102131
|
-
apiModules2.push({ name, payloads });
|
|
102138
|
+
apiModules2.push({ name: transformer.moduleName({ name }), payloads });
|
|
102132
102139
|
return apiModules2;
|
|
102133
102140
|
}, []);
|
|
102134
102141
|
return apiModules;
|
|
102135
102142
|
}
|
|
102136
102143
|
function renderApiModules(apiModules, options8) {
|
|
102137
|
-
const { output, ts: ts9,
|
|
102144
|
+
const { output, ts: ts9, overrides, preset } = options8;
|
|
102138
102145
|
const templateFile = readTemplateFile(preset);
|
|
102139
102146
|
const typesFilename = options8.typesFilename.replace(".ts", "");
|
|
102140
102147
|
return Promise.all(
|
|
@@ -102152,7 +102159,8 @@ function renderApiModules(apiModules, options8) {
|
|
|
102152
102159
|
printWidth: 120
|
|
102153
102160
|
}).then((content) => {
|
|
102154
102161
|
const path13 = (0, import_path14.resolve)(output, `${apiModule.name}.${ts9 ? "ts" : "js"}`);
|
|
102155
|
-
|
|
102162
|
+
const shouldSkip = (!overrides || (0, import_rattail2.isArray)(overrides) && !overrides.includes(apiModule.name)) && import_fs_extra2.default.existsSync(path13);
|
|
102163
|
+
if (shouldSkip) {
|
|
102156
102164
|
import_rslog.logger.warn(`File already exists, skip: ${path13}`);
|
|
102157
102165
|
promiseResolve(content);
|
|
102158
102166
|
return;
|
|
@@ -102178,7 +102186,7 @@ async function generate(userOptions = {}) {
|
|
|
102178
102186
|
const {
|
|
102179
102187
|
base,
|
|
102180
102188
|
ts: ts9 = true,
|
|
102181
|
-
|
|
102189
|
+
overrides = true,
|
|
102182
102190
|
preset = "axle",
|
|
102183
102191
|
statusCodeStrategy = "strict",
|
|
102184
102192
|
input = "./schema.json",
|
|
@@ -102197,7 +102205,7 @@ async function generate(userOptions = {}) {
|
|
|
102197
102205
|
await generateTypes(schema2, output, typesFilename);
|
|
102198
102206
|
}
|
|
102199
102207
|
const apiModules = partitionApiModules(schema2, mergedTransformer, { statusCodes, ts: ts9, base });
|
|
102200
|
-
await renderApiModules(apiModules, { output, typesFilename, ts: ts9,
|
|
102208
|
+
await renderApiModules(apiModules, { output, typesFilename, ts: ts9, overrides, preset });
|
|
102201
102209
|
import_rslog.logger.success("Done");
|
|
102202
102210
|
}
|
|
102203
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
|
@@ -79241,6 +79241,7 @@ __export(index_exports, {
|
|
|
79241
79241
|
renderApiModules: () => renderApiModules,
|
|
79242
79242
|
transformEntity: () => transformEntity,
|
|
79243
79243
|
transformFn: () => transformFn,
|
|
79244
|
+
transformModuleName: () => transformModuleName,
|
|
79244
79245
|
transformType: () => transformType,
|
|
79245
79246
|
transformTypeQuery: () => transformTypeQuery,
|
|
79246
79247
|
transformTypeQueryValue: () => transformTypeQueryValue,
|
|
@@ -79259,7 +79260,10 @@ init_cjs_shims();
|
|
|
79259
79260
|
init_cjs_shims();
|
|
79260
79261
|
var import_pluralize = __toESM(require("pluralize"), 1);
|
|
79261
79262
|
var import_rattail = require("rattail");
|
|
79262
|
-
function
|
|
79263
|
+
function transformModuleName({ name }) {
|
|
79264
|
+
return (0, import_rattail.camelize)(name);
|
|
79265
|
+
}
|
|
79266
|
+
function transformVerb({ method }) {
|
|
79263
79267
|
switch (method) {
|
|
79264
79268
|
case "post":
|
|
79265
79269
|
return "Create";
|
|
@@ -79269,10 +79273,10 @@ function transformVerb(method) {
|
|
|
79269
79273
|
return (0, import_rattail.pascalCase)(method);
|
|
79270
79274
|
}
|
|
79271
79275
|
}
|
|
79272
|
-
function transformUrl(path13, base) {
|
|
79276
|
+
function transformUrl({ path: path13, base }) {
|
|
79273
79277
|
return (base ? path13.replace(base, "") : path13).replace(/{/g, ":").replace(/}/g, "");
|
|
79274
79278
|
}
|
|
79275
|
-
function transformEntity(path13, method, base) {
|
|
79279
|
+
function transformEntity({ path: path13, method, base }) {
|
|
79276
79280
|
path13 = base ? path13.replace(base, "") : path13;
|
|
79277
79281
|
const words = path13.split("/").filter(Boolean);
|
|
79278
79282
|
return words.reduce((entity, word, index) => {
|
|
@@ -79286,35 +79290,40 @@ function transformEntity(path13, method, base) {
|
|
|
79286
79290
|
return `${entity}${word}`;
|
|
79287
79291
|
}, "");
|
|
79288
79292
|
}
|
|
79289
|
-
function transformFn(verb, entity) {
|
|
79293
|
+
function transformFn({ verb, entity }) {
|
|
79290
79294
|
return `api${verb}${entity}`;
|
|
79291
79295
|
}
|
|
79292
|
-
function transformType(verb, entity) {
|
|
79296
|
+
function transformType({ verb, entity }) {
|
|
79293
79297
|
return `Api${verb}${entity}`;
|
|
79294
79298
|
}
|
|
79295
|
-
function transformTypeValue(path13, method) {
|
|
79299
|
+
function transformTypeValue({ path: path13, method }) {
|
|
79296
79300
|
return `paths['${path13}']['${method}']`;
|
|
79297
79301
|
}
|
|
79298
|
-
function transformTypeQuery(verb, entity) {
|
|
79302
|
+
function transformTypeQuery({ verb, entity }) {
|
|
79299
79303
|
return `Api${verb}${entity}Query`;
|
|
79300
79304
|
}
|
|
79301
|
-
function transformTypeQueryValue(type2) {
|
|
79305
|
+
function transformTypeQueryValue({ type: type2 }) {
|
|
79302
79306
|
return `${type2}['parameters']['query']`;
|
|
79303
79307
|
}
|
|
79304
|
-
function transformTypeRequestBody(verb, entity) {
|
|
79308
|
+
function transformTypeRequestBody({ verb, entity }) {
|
|
79305
79309
|
return `Api${verb}${entity}RequestBody`;
|
|
79306
79310
|
}
|
|
79307
|
-
function transformTypeRequestBodyValue(type2) {
|
|
79311
|
+
function transformTypeRequestBodyValue({ type: type2 }) {
|
|
79308
79312
|
return `${type2}['requestBody']['content']['application/json']`;
|
|
79309
79313
|
}
|
|
79310
|
-
function transformTypeResponseBody(verb, entity) {
|
|
79314
|
+
function transformTypeResponseBody({ verb, entity }) {
|
|
79311
79315
|
return `Api${verb}${entity}ResponseBody`;
|
|
79312
79316
|
}
|
|
79313
|
-
function transformTypeResponseBodyValue(
|
|
79317
|
+
function transformTypeResponseBodyValue({
|
|
79318
|
+
type: type2,
|
|
79319
|
+
statusCode,
|
|
79320
|
+
mime
|
|
79321
|
+
}) {
|
|
79314
79322
|
return `${type2}['responses']['${statusCode}']['content']['${mime}']`;
|
|
79315
79323
|
}
|
|
79316
79324
|
function createTransformer() {
|
|
79317
79325
|
return {
|
|
79326
|
+
moduleName: transformModuleName,
|
|
79318
79327
|
verb: transformVerb,
|
|
79319
79328
|
url: transformUrl,
|
|
79320
79329
|
entity: transformEntity,
|
|
@@ -102129,23 +102138,22 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
102129
102138
|
path13 = base ? base + path13 : path13;
|
|
102130
102139
|
const pathItems = schemaPaths[path13];
|
|
102131
102140
|
const childPayloads = Object.entries(pathItems).reduce((payloads3, [method, operation]) => {
|
|
102132
|
-
const url2 = transformer.url(path13, base);
|
|
102133
|
-
const entity = transformer.entity(path13, method, base);
|
|
102134
|
-
const verb = transformer.verb(method);
|
|
102135
|
-
const fn8 = transformer.fn(verb, entity);
|
|
102136
|
-
const type2 = transformer.type(verb, entity);
|
|
102137
|
-
const typeValue = transformer.typeValue(path13, method);
|
|
102138
|
-
const typeQuery = transformer.typeQuery(verb, entity);
|
|
102139
|
-
const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue(type2) : "never";
|
|
102140
|
-
const typeRequestBody = transformer.typeRequestBody(verb, entity);
|
|
102141
|
-
const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue(type2) : "never";
|
|
102142
|
-
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 });
|
|
102143
102152
|
const statusCode = statusCodes[method] ?? 200;
|
|
102144
102153
|
const mime = (operation.responses?.[statusCode]).content?.["application/json"] ? "application/json" : "*/*";
|
|
102145
|
-
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue(type2, statusCode, mime) : "never";
|
|
102154
|
+
const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
|
|
102146
102155
|
payloads3.push({
|
|
102147
102156
|
fn: fn8,
|
|
102148
|
-
path: path13,
|
|
102149
102157
|
url: url2,
|
|
102150
102158
|
method,
|
|
102151
102159
|
verb,
|
|
@@ -102164,13 +102172,13 @@ function partitionApiModules(schema2, transformer, options8) {
|
|
|
102164
102172
|
payloads2.push(...childPayloads);
|
|
102165
102173
|
return payloads2;
|
|
102166
102174
|
}, []);
|
|
102167
|
-
apiModules2.push({ name, payloads });
|
|
102175
|
+
apiModules2.push({ name: transformer.moduleName({ name }), payloads });
|
|
102168
102176
|
return apiModules2;
|
|
102169
102177
|
}, []);
|
|
102170
102178
|
return apiModules;
|
|
102171
102179
|
}
|
|
102172
102180
|
function renderApiModules(apiModules, options8) {
|
|
102173
|
-
const { output, ts: ts9,
|
|
102181
|
+
const { output, ts: ts9, overrides, preset } = options8;
|
|
102174
102182
|
const templateFile = readTemplateFile(preset);
|
|
102175
102183
|
const typesFilename = options8.typesFilename.replace(".ts", "");
|
|
102176
102184
|
return Promise.all(
|
|
@@ -102188,7 +102196,8 @@ function renderApiModules(apiModules, options8) {
|
|
|
102188
102196
|
printWidth: 120
|
|
102189
102197
|
}).then((content) => {
|
|
102190
102198
|
const path13 = (0, import_path14.resolve)(output, `${apiModule.name}.${ts9 ? "ts" : "js"}`);
|
|
102191
|
-
|
|
102199
|
+
const shouldSkip = (!overrides || (0, import_rattail2.isArray)(overrides) && !overrides.includes(apiModule.name)) && import_fs_extra2.default.existsSync(path13);
|
|
102200
|
+
if (shouldSkip) {
|
|
102192
102201
|
import_rslog.logger.warn(`File already exists, skip: ${path13}`);
|
|
102193
102202
|
promiseResolve(content);
|
|
102194
102203
|
return;
|
|
@@ -102214,7 +102223,7 @@ async function generate(userOptions = {}) {
|
|
|
102214
102223
|
const {
|
|
102215
102224
|
base,
|
|
102216
102225
|
ts: ts9 = true,
|
|
102217
|
-
|
|
102226
|
+
overrides = true,
|
|
102218
102227
|
preset = "axle",
|
|
102219
102228
|
statusCodeStrategy = "strict",
|
|
102220
102229
|
input = "./schema.json",
|
|
@@ -102233,7 +102242,7 @@ async function generate(userOptions = {}) {
|
|
|
102233
102242
|
await generateTypes(schema2, output, typesFilename);
|
|
102234
102243
|
}
|
|
102235
102244
|
const apiModules = partitionApiModules(schema2, mergedTransformer, { statusCodes, ts: ts9, base });
|
|
102236
|
-
await renderApiModules(apiModules, { output, typesFilename, ts: ts9,
|
|
102245
|
+
await renderApiModules(apiModules, { output, typesFilename, ts: ts9, overrides, preset });
|
|
102237
102246
|
import_rslog.logger.success("Done");
|
|
102238
102247
|
}
|
|
102239
102248
|
|
|
@@ -102257,6 +102266,7 @@ var import_pluralize2 = __toESM(require("pluralize"), 1);
|
|
|
102257
102266
|
renderApiModules,
|
|
102258
102267
|
transformEntity,
|
|
102259
102268
|
transformFn,
|
|
102269
|
+
transformModuleName,
|
|
102260
102270
|
transformType,
|
|
102261
102271
|
transformTypeQuery,
|
|
102262
102272
|
transformTypeQueryValue,
|
package/dist/index.d.cts
CHANGED
|
@@ -1,19 +1,58 @@
|
|
|
1
1
|
import { OpenAPI3, OperationObject } from 'openapi-typescript';
|
|
2
2
|
export { default as pluralize } from 'pluralize';
|
|
3
3
|
|
|
4
|
-
declare function
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
declare function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
declare function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
declare function
|
|
15
|
-
|
|
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;
|
|
16
54
|
interface Transformer {
|
|
55
|
+
moduleName: typeof transformModuleName;
|
|
17
56
|
verb: typeof transformVerb;
|
|
18
57
|
url: typeof transformUrl;
|
|
19
58
|
entity: typeof transformEntity;
|
|
@@ -63,41 +102,124 @@ declare function hasQueryParameter(operation: OperationObject): boolean;
|
|
|
63
102
|
declare function hasResponseBody(operation: OperationObject): boolean;
|
|
64
103
|
declare function getCliVersion(): any;
|
|
65
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
|
+
}
|
|
66
129
|
interface ApiModulePayload {
|
|
130
|
+
/**
|
|
131
|
+
* The name of the API function/dispatcher, such as apiGetUsers, apiCreatePost, apiUpdateComment, etc.
|
|
132
|
+
*/
|
|
67
133
|
fn: string;
|
|
68
|
-
|
|
134
|
+
/**
|
|
135
|
+
* The URL of the API endpoint, such as /users, /posts, /comments, etc.
|
|
136
|
+
*/
|
|
69
137
|
url: string;
|
|
138
|
+
/**
|
|
139
|
+
* The HTTP method of the API endpoint, such as get, post, put, delete, etc.
|
|
140
|
+
*/
|
|
70
141
|
method: string;
|
|
142
|
+
/**
|
|
143
|
+
* The HTTP verb of the API endpoint, such as Get, Create, Update, Delete, etc.
|
|
144
|
+
*/
|
|
71
145
|
verb: string;
|
|
146
|
+
/**
|
|
147
|
+
* The entity name of the API endpoint, such as User, Comment, Post, etc.
|
|
148
|
+
*/
|
|
72
149
|
entity: string;
|
|
150
|
+
/**
|
|
151
|
+
* The type name of the API endpoint, such as ApiGetUsers, ApiCreatePost, ApiUpdateComment, etc.
|
|
152
|
+
*/
|
|
73
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
|
+
*/
|
|
74
157
|
typeValue: string;
|
|
158
|
+
/**
|
|
159
|
+
* The type name of the query parameters of the API endpoint, such as ApiGetUsersQuery, ApiCreatePostQuery, ApiUpdateCommentQuery, etc.
|
|
160
|
+
*/
|
|
75
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
|
+
*/
|
|
76
165
|
typeQueryValue: string;
|
|
166
|
+
/**
|
|
167
|
+
* The type name of the request body of the API endpoint, such as ApiGetUsersRequestBody, ApiCreatePostRequestBody, ApiUpdateCommentRequestBody, etc.
|
|
168
|
+
*/
|
|
77
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
|
+
*/
|
|
78
173
|
typeRequestBodyValue: string;
|
|
174
|
+
/**
|
|
175
|
+
* The type name of the response body of the API endpoint, such as ApiGetUsersResponseBody, ApiCreatePostResponseBody, ApiUpdateCommentResponseBody, etc.
|
|
176
|
+
*/
|
|
79
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
|
+
*/
|
|
80
181
|
typeResponseBodyValue: string;
|
|
81
182
|
}
|
|
82
|
-
interface ApiModule {
|
|
83
|
-
name: string;
|
|
84
|
-
payloads: ApiModulePayload[];
|
|
85
|
-
}
|
|
86
|
-
interface ApiModuleTemplateData {
|
|
87
|
-
apiModule: ApiModule;
|
|
88
|
-
typesFilename: string;
|
|
89
|
-
ts: boolean;
|
|
90
|
-
}
|
|
91
183
|
interface GenerateOptions {
|
|
184
|
+
/**
|
|
185
|
+
* The path to the OpenAPI/Swagger schema file.
|
|
186
|
+
*/
|
|
92
187
|
input?: string;
|
|
188
|
+
/**
|
|
189
|
+
* The path to the output directory.
|
|
190
|
+
*/
|
|
93
191
|
output?: string;
|
|
192
|
+
/**
|
|
193
|
+
* The base path of the API endpoints.
|
|
194
|
+
*/
|
|
94
195
|
base?: string;
|
|
196
|
+
/**
|
|
197
|
+
* The filename of the generated openapi types file.
|
|
198
|
+
*/
|
|
95
199
|
typesFilename?: string;
|
|
200
|
+
/**
|
|
201
|
+
* The transformer api options, used to override the default transformation rules.
|
|
202
|
+
*/
|
|
96
203
|
transformer?: Partial<Transformer>;
|
|
204
|
+
/**
|
|
205
|
+
* Whether to generate TypeScript code.
|
|
206
|
+
*/
|
|
97
207
|
ts?: boolean;
|
|
98
|
-
|
|
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
|
+
*/
|
|
99
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
|
+
*/
|
|
100
219
|
statusCodeStrategy?: StatusCodeStrategy;
|
|
220
|
+
/**
|
|
221
|
+
* The status codes to override the default status codes.
|
|
222
|
+
*/
|
|
101
223
|
statusCodes?: {
|
|
102
224
|
get?: number;
|
|
103
225
|
post?: number;
|
|
@@ -117,7 +239,7 @@ declare function renderApiModules(apiModules: ApiModule[], options: {
|
|
|
117
239
|
output: string;
|
|
118
240
|
typesFilename: string;
|
|
119
241
|
ts: boolean;
|
|
120
|
-
|
|
242
|
+
overrides: boolean | string[];
|
|
121
243
|
preset: Preset;
|
|
122
244
|
}): Promise<unknown[]>;
|
|
123
245
|
declare function generateTypes(schema: OpenAPI3, output: string, typesFilename: string): Promise<void>;
|
|
@@ -127,4 +249,4 @@ type Config = GenerateOptions;
|
|
|
127
249
|
declare function defineConfig(config: Config): GenerateOptions;
|
|
128
250
|
declare function getConfig(): Promise<Config>;
|
|
129
251
|
|
|
130
|
-
export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type StatusCodeStrategy, type StatusCodes, type Transformer, createStatusCodesByStrategy, createTransformer, defineConfig, generate, generateTypes, getCliVersion, getConfig, hasQueryParameter, hasResponseBody, partitionApiModules, readSchema, readTemplateFile, renderApiModules, transformEntity, transformFn, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
|
|
252
|
+
export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type StatusCodeStrategy, type StatusCodes, type Transformer, createStatusCodesByStrategy, createTransformer, defineConfig, generate, generateTypes, getCliVersion, getConfig, hasQueryParameter, hasResponseBody, partitionApiModules, readSchema, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,19 +1,58 @@
|
|
|
1
1
|
import { OpenAPI3, OperationObject } from 'openapi-typescript';
|
|
2
2
|
export { default as pluralize } from 'pluralize';
|
|
3
3
|
|
|
4
|
-
declare function
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
declare function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
declare function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
declare function
|
|
15
|
-
|
|
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;
|
|
16
54
|
interface Transformer {
|
|
55
|
+
moduleName: typeof transformModuleName;
|
|
17
56
|
verb: typeof transformVerb;
|
|
18
57
|
url: typeof transformUrl;
|
|
19
58
|
entity: typeof transformEntity;
|
|
@@ -63,41 +102,124 @@ declare function hasQueryParameter(operation: OperationObject): boolean;
|
|
|
63
102
|
declare function hasResponseBody(operation: OperationObject): boolean;
|
|
64
103
|
declare function getCliVersion(): any;
|
|
65
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
|
+
}
|
|
66
129
|
interface ApiModulePayload {
|
|
130
|
+
/**
|
|
131
|
+
* The name of the API function/dispatcher, such as apiGetUsers, apiCreatePost, apiUpdateComment, etc.
|
|
132
|
+
*/
|
|
67
133
|
fn: string;
|
|
68
|
-
|
|
134
|
+
/**
|
|
135
|
+
* The URL of the API endpoint, such as /users, /posts, /comments, etc.
|
|
136
|
+
*/
|
|
69
137
|
url: string;
|
|
138
|
+
/**
|
|
139
|
+
* The HTTP method of the API endpoint, such as get, post, put, delete, etc.
|
|
140
|
+
*/
|
|
70
141
|
method: string;
|
|
142
|
+
/**
|
|
143
|
+
* The HTTP verb of the API endpoint, such as Get, Create, Update, Delete, etc.
|
|
144
|
+
*/
|
|
71
145
|
verb: string;
|
|
146
|
+
/**
|
|
147
|
+
* The entity name of the API endpoint, such as User, Comment, Post, etc.
|
|
148
|
+
*/
|
|
72
149
|
entity: string;
|
|
150
|
+
/**
|
|
151
|
+
* The type name of the API endpoint, such as ApiGetUsers, ApiCreatePost, ApiUpdateComment, etc.
|
|
152
|
+
*/
|
|
73
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
|
+
*/
|
|
74
157
|
typeValue: string;
|
|
158
|
+
/**
|
|
159
|
+
* The type name of the query parameters of the API endpoint, such as ApiGetUsersQuery, ApiCreatePostQuery, ApiUpdateCommentQuery, etc.
|
|
160
|
+
*/
|
|
75
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
|
+
*/
|
|
76
165
|
typeQueryValue: string;
|
|
166
|
+
/**
|
|
167
|
+
* The type name of the request body of the API endpoint, such as ApiGetUsersRequestBody, ApiCreatePostRequestBody, ApiUpdateCommentRequestBody, etc.
|
|
168
|
+
*/
|
|
77
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
|
+
*/
|
|
78
173
|
typeRequestBodyValue: string;
|
|
174
|
+
/**
|
|
175
|
+
* The type name of the response body of the API endpoint, such as ApiGetUsersResponseBody, ApiCreatePostResponseBody, ApiUpdateCommentResponseBody, etc.
|
|
176
|
+
*/
|
|
79
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
|
+
*/
|
|
80
181
|
typeResponseBodyValue: string;
|
|
81
182
|
}
|
|
82
|
-
interface ApiModule {
|
|
83
|
-
name: string;
|
|
84
|
-
payloads: ApiModulePayload[];
|
|
85
|
-
}
|
|
86
|
-
interface ApiModuleTemplateData {
|
|
87
|
-
apiModule: ApiModule;
|
|
88
|
-
typesFilename: string;
|
|
89
|
-
ts: boolean;
|
|
90
|
-
}
|
|
91
183
|
interface GenerateOptions {
|
|
184
|
+
/**
|
|
185
|
+
* The path to the OpenAPI/Swagger schema file.
|
|
186
|
+
*/
|
|
92
187
|
input?: string;
|
|
188
|
+
/**
|
|
189
|
+
* The path to the output directory.
|
|
190
|
+
*/
|
|
93
191
|
output?: string;
|
|
192
|
+
/**
|
|
193
|
+
* The base path of the API endpoints.
|
|
194
|
+
*/
|
|
94
195
|
base?: string;
|
|
196
|
+
/**
|
|
197
|
+
* The filename of the generated openapi types file.
|
|
198
|
+
*/
|
|
95
199
|
typesFilename?: string;
|
|
200
|
+
/**
|
|
201
|
+
* The transformer api options, used to override the default transformation rules.
|
|
202
|
+
*/
|
|
96
203
|
transformer?: Partial<Transformer>;
|
|
204
|
+
/**
|
|
205
|
+
* Whether to generate TypeScript code.
|
|
206
|
+
*/
|
|
97
207
|
ts?: boolean;
|
|
98
|
-
|
|
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
|
+
*/
|
|
99
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
|
+
*/
|
|
100
219
|
statusCodeStrategy?: StatusCodeStrategy;
|
|
220
|
+
/**
|
|
221
|
+
* The status codes to override the default status codes.
|
|
222
|
+
*/
|
|
101
223
|
statusCodes?: {
|
|
102
224
|
get?: number;
|
|
103
225
|
post?: number;
|
|
@@ -117,7 +239,7 @@ declare function renderApiModules(apiModules: ApiModule[], options: {
|
|
|
117
239
|
output: string;
|
|
118
240
|
typesFilename: string;
|
|
119
241
|
ts: boolean;
|
|
120
|
-
|
|
242
|
+
overrides: boolean | string[];
|
|
121
243
|
preset: Preset;
|
|
122
244
|
}): Promise<unknown[]>;
|
|
123
245
|
declare function generateTypes(schema: OpenAPI3, output: string, typesFilename: string): Promise<void>;
|
|
@@ -127,4 +249,4 @@ type Config = GenerateOptions;
|
|
|
127
249
|
declare function defineConfig(config: Config): GenerateOptions;
|
|
128
250
|
declare function getConfig(): Promise<Config>;
|
|
129
251
|
|
|
130
|
-
export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type StatusCodeStrategy, type StatusCodes, type Transformer, createStatusCodesByStrategy, createTransformer, defineConfig, generate, generateTypes, getCliVersion, getConfig, hasQueryParameter, hasResponseBody, partitionApiModules, readSchema, readTemplateFile, renderApiModules, transformEntity, transformFn, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
|
|
252
|
+
export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type StatusCodeStrategy, type StatusCodes, type Transformer, createStatusCodesByStrategy, createTransformer, defineConfig, generate, generateTypes, getCliVersion, getConfig, hasQueryParameter, hasResponseBody, partitionApiModules, readSchema, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
renderApiModules,
|
|
9
9
|
transformEntity,
|
|
10
10
|
transformFn,
|
|
11
|
+
transformModuleName,
|
|
11
12
|
transformType,
|
|
12
13
|
transformTypeQuery,
|
|
13
14
|
transformTypeQueryValue,
|
|
@@ -18,7 +19,7 @@ import {
|
|
|
18
19
|
transformTypeValue,
|
|
19
20
|
transformUrl,
|
|
20
21
|
transformVerb
|
|
21
|
-
} from "./chunk-
|
|
22
|
+
} from "./chunk-ZLLNTCL2.js";
|
|
22
23
|
import {
|
|
23
24
|
createStatusCodesByStrategy,
|
|
24
25
|
getCliVersion,
|
|
@@ -48,6 +49,7 @@ export {
|
|
|
48
49
|
renderApiModules,
|
|
49
50
|
transformEntity,
|
|
50
51
|
transformFn,
|
|
52
|
+
transformModuleName,
|
|
51
53
|
transformType,
|
|
52
54
|
transformTypeQuery,
|
|
53
55
|
transformTypeQueryValue,
|