api-farmer 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,270 @@
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 `OpenAPI3/Swagger2 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
+ #### Some Examples
56
+
57
+ Some simple usage examples can be found [here](fixtures)
58
+
59
+ ### Custom EJS Template
60
+
61
+ Create `api-farmer.ejs` in the project root, which will replace the `preset` template.
62
+ The template format can refer to the preset template listed below:
63
+
64
+ - [Axle](templates/axle.ejs)
65
+ - [Axios](templates/axios.ejs)
66
+
67
+ See the bottom of the document for template variable definitions.
68
+
69
+ ### Status Code Strategy
70
+
71
+ `Restful API` recommends using different successful http status codes for different methods, such as `get: 200`, `post: 201`, etc. If you don't need this strategy, you can set `statusCodeStrategy` to `loose`
72
+
73
+ ```ts
74
+ // api-farmer.config.ts
75
+ import { defineConfig } from 'api-farmer'
76
+
77
+ export default defineConfig({
78
+ // 'strict' or 'loose', defaults 'strict'
79
+ statusCodeStrategy: 'loose',
80
+ })
81
+ ```
82
+
83
+ ### Transformer API
84
+
85
+ You can use the Transformer API to further define template variables, which will override the default transformation rules.
86
+
87
+ ```ts
88
+ // api-farmer.config.ts
89
+ import { defineConfig } from 'api-farmer'
90
+
91
+ export default defineConfig({
92
+ transformer: {
93
+ moduleName({ name }) {
94
+ // The new module name.
95
+ return `${name}.generated`
96
+ },
97
+ verb() {},
98
+ url() {},
99
+ entity() {},
100
+ fn() {},
101
+ type() {},
102
+ typeValue() {},
103
+ typeQuery() {},
104
+ typeQueryValue() {},
105
+ typeRequestBody() {},
106
+ typeRequestBodyValue() {},
107
+ typeResponseBody() {},
108
+ typeResponseBodyValue() {},
109
+ },
110
+ })
111
+ ```
112
+
113
+ ### Configuration Options
114
+
115
+ ```ts
116
+ export interface Config {
117
+ /**
118
+ * The path to the OpenAPI/Swagger schema file.
119
+ */
120
+ input?: string
121
+ /**
122
+ * The path to the output directory.
123
+ */
124
+ output?: string
125
+ /**
126
+ * The base path of the API endpoints.
127
+ */
128
+ base?: string
129
+ /**
130
+ * The filename of the generated openapi types file.
131
+ */
132
+ typesFilename?: string
133
+ /**
134
+ * The transformer api options, used to override the default transformation rules.
135
+ */
136
+ transformer?: Partial<Transformer>
137
+ /**
138
+ * Whether to generate TypeScript code.
139
+ */
140
+ ts?: boolean
141
+ /**
142
+ * Whether to override the existing files, or an array of filenames to override.
143
+ */
144
+ overrides?: boolean | string[]
145
+ /**
146
+ * The preset ejs template to use.
147
+ */
148
+ preset?: Preset
149
+ /**
150
+ * The status code strategy to use.
151
+ * loose: all success status codes are 200, strict: use the openapi recommended success status codes.
152
+ */
153
+ statusCodeStrategy?: StatusCodeStrategy
154
+ /**
155
+ * The status codes to override the default status codes.
156
+ */
157
+ statusCodes?: {
158
+ get?: number
159
+ post?: number
160
+ put?: number
161
+ delete?: number
162
+ patch?: number
163
+ options?: number
164
+ head?: number
165
+ }
166
+ }
167
+ ```
168
+
169
+ ### Template Variable Definitions
170
+
171
+ ```ts
172
+ export interface ApiModuleTemplateData {
173
+ /**
174
+ * API module metadata
175
+ */
176
+ apiModule: ApiModule
177
+ /**
178
+ * The name of the generated api ts type aggregation file
179
+ */
180
+ typesFilename: string
181
+ /**
182
+ * Whether to generate ts code
183
+ */
184
+ ts: boolean
185
+ }
186
+
187
+ export interface ApiModule {
188
+ /**
189
+ * The name of the API module
190
+ */
191
+ name: string
192
+ /**
193
+ * API module payloads
194
+ */
195
+ payloads: ApiModulePayload[]
196
+ }
197
+
198
+ export interface ApiModulePayload {
199
+ /**
200
+ * The name of the API function/dispatcher,
201
+ * such as apiGetUsers, apiCreatePost, apiUpdateComment, etc.
202
+ */
203
+ fn: string
204
+ /**
205
+ * The URL of the API endpoint,
206
+ * such as /users, /posts, /comments, etc.
207
+ */
208
+ url: string
209
+ /**
210
+ * The HTTP method of the API endpoint,
211
+ * such as get, post, put, delete, etc.
212
+ */
213
+ method: string
214
+ /**
215
+ * The HTTP verb of the API endpoint,
216
+ * such as Get, Create, Update, Delete, etc.
217
+ */
218
+ verb: string
219
+ /**
220
+ * The entity name of the API endpoint,
221
+ * such as User, Comment, Post, etc.
222
+ */
223
+ entity: string
224
+ /**
225
+ * The type name of the API endpoint,
226
+ * such as ApiGetUsers, ApiCreatePost, ApiUpdateComment, etc.
227
+ */
228
+ type: string
229
+ /**
230
+ * The value of the type of the API endpoint,
231
+ * such as paths['/users']['get'], paths['/posts']['post'], paths['/comments']['put'], etc.
232
+ */
233
+ typeValue: string
234
+ /**
235
+ * The type name of the query parameters of the API endpoint,
236
+ * such as ApiGetUsersQuery, ApiCreatePostQuery, ApiUpdateCommentQuery, etc.
237
+ */
238
+ typeQuery: string
239
+ /**
240
+ * The value of the type of the query parameters of the API endpoint,
241
+ * such as ApiGetUsersQuery['parameters']['query'], ApiCreatePostQuery['parameters']['query'],
242
+ * ApiUpdateCommentQuery['parameters']['query'], etc.
243
+ */
244
+ typeQueryValue: string
245
+ /**
246
+ * The type name of the request body of the API endpoint,
247
+ * such as ApiGetUsersRequestBody, ApiCreatePostRequestBody, ApiUpdateCommentRequestBody, etc.
248
+ */
249
+ typeRequestBody: string
250
+ /**
251
+ * The value of the type of the request body of the API endpoint,
252
+ * such as ApiGetUsersRequestBody['requestBody']['content']['application/json'],
253
+ * ApiCreatePostRequestBody['requestBody']['content']['application/json'],
254
+ * ApiUpdateCommentRequestBody['requestBody']['content']['application/json'], etc.
255
+ */
256
+ typeRequestBodyValue: string
257
+ /**
258
+ * The type name of the response body of the API endpoint,
259
+ * such as ApiGetUsersResponseBody, ApiCreatePostResponseBody, ApiUpdateCommentResponseBody, etc.
260
+ */
261
+ typeResponseBody: string
262
+ /**
263
+ * The value of the type of the response body of the API endpoint,
264
+ * such as ApiGetUsersResponseBody['responses']['200']['content']['application/json'],
265
+ * ApiCreatePostResponseBody['responses']['201']['content']['application/json'],
266
+ * ApiUpdateCommentResponseBody['responses']['200']['content']['application/json'], etc.
267
+ */
268
+ typeResponseBodyValue: string
269
+ }
270
+ ```
@@ -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(type2, statusCode, mime) {
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
- const mime = (operation.responses?.[statusCode]).content?.["application/json"] ? "application/json" : "*/*";
22838
- const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue(type2, statusCode, mime) : "never";
22841
+ const mime = operation.responses?.[statusCode]?.content?.["application/json"] ? "application/json" : "*/*";
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, override, preset } = options8;
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
- if ((!override || isArray(override) && !override.includes(apiModule.name)) && fse.existsSync(path13)) {
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
- override = true,
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, override, preset });
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(type2, statusCode, mime) {
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
- const mime = (operation.responses?.[statusCode]).content?.["application/json"] ? "application/json" : "*/*";
102113
- const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue(type2, statusCode, mime) : "never";
102116
+ const mime = operation.responses?.[statusCode]?.content?.["application/json"] ? "application/json" : "*/*";
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, override, preset } = options8;
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
- if ((!override || (0, import_rattail2.isArray)(override) && !override.includes(apiModule.name)) && import_fs_extra2.default.existsSync(path13)) {
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
- override = true,
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, override, preset });
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-LOGO7XCM.js");
12
+ const { generate } = await import("./generate-544AKFJX.js");
13
13
  return generate();
14
14
  });
15
15
  program.parse();
@@ -3,7 +3,7 @@ import {
3
3
  generateTypes,
4
4
  partitionApiModules,
5
5
  renderApiModules
6
- } from "./chunk-JWKB7AQQ.js";
6
+ } from "./chunk-6T5EWOBD.js";
7
7
  import "./chunk-6N4OHGAC.js";
8
8
  import "./chunk-6OIOYGN7.js";
9
9
  export {
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(type2, statusCode, mime) {
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
- const mime = (operation.responses?.[statusCode]).content?.["application/json"] ? "application/json" : "*/*";
102150
- const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue(type2, statusCode, mime) : "never";
102153
+ const mime = operation.responses?.[statusCode]?.content?.["application/json"] ? "application/json" : "*/*";
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, override, preset } = options8;
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
- if ((!override || (0, import_rattail2.isArray)(override) && !override.includes(apiModule.name)) && import_fs_extra2.default.existsSync(path13)) {
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
- override = true,
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, override, preset });
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: string): string;
5
- declare function transformVerb(method: string): string;
6
- declare function transformUrl(path: string, base?: string): string;
7
- declare function transformEntity(path: string, method: string, base?: string): string;
8
- declare function transformFn(verb: string, entity: string): string;
9
- declare function transformType(verb: string, entity: string): string;
10
- declare function transformTypeValue(path: string, method: string): string;
11
- declare function transformTypeQuery(verb: string, entity: string): string;
12
- declare function transformTypeQueryValue(type: string): string;
13
- declare function transformTypeRequestBody(verb: string, entity: string): string;
14
- declare function transformTypeRequestBodyValue(type: string): string;
15
- declare function transformTypeResponseBody(verb: string, entity: string): string;
16
- declare function transformTypeResponseBodyValue(type: string, statusCode: number, mime: string): string;
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
- path: string;
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
- override?: boolean | string[];
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
- override: boolean | string[];
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: string): string;
5
- declare function transformVerb(method: string): string;
6
- declare function transformUrl(path: string, base?: string): string;
7
- declare function transformEntity(path: string, method: string, base?: string): string;
8
- declare function transformFn(verb: string, entity: string): string;
9
- declare function transformType(verb: string, entity: string): string;
10
- declare function transformTypeValue(path: string, method: string): string;
11
- declare function transformTypeQuery(verb: string, entity: string): string;
12
- declare function transformTypeQueryValue(type: string): string;
13
- declare function transformTypeRequestBody(verb: string, entity: string): string;
14
- declare function transformTypeRequestBodyValue(type: string): string;
15
- declare function transformTypeResponseBody(verb: string, entity: string): string;
16
- declare function transformTypeResponseBodyValue(type: string, statusCode: number, mime: string): string;
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
- path: string;
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
- override?: boolean | string[];
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
- override: boolean | string[];
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
@@ -19,7 +19,7 @@ import {
19
19
  transformTypeValue,
20
20
  transformUrl,
21
21
  transformVerb
22
- } from "./chunk-JWKB7AQQ.js";
22
+ } from "./chunk-6T5EWOBD.js";
23
23
  import {
24
24
  createStatusCodesByStrategy,
25
25
  getCliVersion,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "api-farmer",
3
- "version": "0.0.3",
4
- "description": "A cli to generate apis from a schema",
3
+ "version": "0.0.5",
4
+ "description": "API module generation tool based on Openapi3/Swagger2.",
5
5
  "keywords": [
6
6
  "cli",
7
7
  "api generator",
@@ -33,7 +33,8 @@
33
33
  "af": "dist/cli.js"
34
34
  },
35
35
  "files": [
36
- "dist"
36
+ "dist",
37
+ "templates"
37
38
  ],
38
39
  "simple-git-hooks": {
39
40
  "pre-commit": "pnpm exec nano-staged --allow-empty",
@@ -0,0 +1,42 @@
1
+ <% if (ts) { %> import { type AxiosRequestConfig } from 'axios' <% } %>
2
+ import { request } from '@/request'
3
+ <% if (ts) { %> import { type paths } from './<%- typesFilename %>' <% } %>
4
+
5
+ <% apiModule.payloads.forEach(payload => { %> -%>
6
+ export const <%- payload.fn %> = (config<% if (ts) { %>: AxiosRequestConfig<<%- payload.typeRequestBody %>> <% } %>)
7
+ => request<% if (ts) { %><any, Res<<%- payload.typeResponseBody %>>><% } %>({
8
+ url: '<%- payload.url %>',
9
+ method: '<%- payload.method %>',
10
+ ...config
11
+ })
12
+
13
+ <% }) %>
14
+
15
+ <% if (ts) { %>
16
+ <% apiModule.payloads.forEach(payload => { %> -%>
17
+ export type <%- payload.type %> = <%- payload.typeValue %>
18
+
19
+ <% }) %>
20
+
21
+ <% apiModule.payloads.forEach(payload => { %> -%>
22
+ <% if (payload.typeQueryValue) { %>
23
+ export type <%- payload.typeQuery %> = <%- payload.typeQueryValue %>
24
+
25
+ <% } %>
26
+ <% }) %>
27
+
28
+ <% apiModule.payloads.forEach(payload => { %> -%>
29
+ <% if (payload.typeRequestBodyValue) { %>
30
+ export type <%- payload.typeRequestBody %> = <%- payload.typeRequestBodyValue %>
31
+
32
+ <% } %>
33
+ <% }) %>
34
+
35
+ <% apiModule.payloads.forEach(payload => { %> -%>
36
+ <% if (payload.typeResponseBodyValue) { %>
37
+ export type <%- payload.typeResponseBody %> = <%- payload.typeResponseBodyValue %>
38
+
39
+ <% } %>
40
+ <% }) %>
41
+ <% } %>
42
+
@@ -0,0 +1,36 @@
1
+ import { api } from '@/request'
2
+ <% if (ts) { %> import { type paths } from './<%- typesFilename %>' <% } %>
3
+
4
+ <% apiModule.payloads.forEach(payload => { %> -%>
5
+ export const <%- payload.fn %> = api<% if (ts) { %><Res<<%- payload.typeResponseBody %>>, <%- payload.typeRequestBody %>><% } %>('<%- payload.url %>', '<%- payload.method %>')
6
+
7
+ <% }) %>
8
+
9
+ <% if (ts) { %>
10
+ <% apiModule.payloads.forEach(payload => { %> -%>
11
+ export type <%- payload.type %> = <%- payload.typeValue %>
12
+
13
+ <% }) %>
14
+
15
+ <% apiModule.payloads.forEach(payload => { %> -%>
16
+ <% if (payload.typeQueryValue) { %>
17
+ export type <%- payload.typeQuery %> = <%- payload.typeQueryValue %>
18
+
19
+ <% } %>
20
+ <% }) %>
21
+
22
+ <% apiModule.payloads.forEach(payload => { %> -%>
23
+ <% if (payload.typeRequestBodyValue) { %>
24
+ export type <%- payload.typeRequestBody %> = <%- payload.typeRequestBodyValue %>
25
+
26
+ <% } %>
27
+ <% }) %>
28
+
29
+ <% apiModule.payloads.forEach(payload => { %> -%>
30
+ <% if (payload.typeResponseBodyValue) { %>
31
+ export type <%- payload.typeResponseBody %> = <%- payload.typeResponseBodyValue %>
32
+
33
+ <% } %>
34
+ <% }) %>
35
+ <% } %>
36
+