api-farmer 0.0.4 → 0.0.6

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 CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  ### Intro
4
4
 
5
- API module generation tool based on `Openapi3 / Swagger2`.
5
+ API module generation tool based on `Openapi3/Swagger2`.
6
6
 
7
7
  ### Features
8
8
 
9
- - 🌐   Supports generating all API modules from `OpenAPI 3/Swagger 2 schemas`
9
+ - 🌐   Supports generating all API modules from `OpenAPI3/Swagger2 schemas`
10
10
  - 📦   Supports generating `ts/js` modules
11
11
  - 🛠️   Comprehensive `ts type` generation
12
12
  - ✏️   Supports custom `ejs` templates for tailored content generation
@@ -38,7 +38,7 @@ export default defineConfig({
38
38
  input: './schema.yaml',
39
39
  // generated codes output path, defaults './src/apis'
40
40
  output: './src/apis',
41
- // axle or axios, defaults axle.
41
+ // 'axle' or 'axios', defaults 'axle'.
42
42
  preset: 'axios',
43
43
  })
44
44
  ```
@@ -52,15 +52,34 @@ npx af
52
52
  > [!TIP]
53
53
  > The generated content does not include the integration of the request client.
54
54
 
55
+ #### Some Examples
56
+
57
+ Some simple usage examples can be found [here](fixtures)
58
+
55
59
  ### Custom EJS Template
56
60
 
57
- Create api-farmer.ejs in the project root path. The template content can refer to the following:
61
+ Create `api-farmer.ejs` in the project root, which will replace the `preset` template.
62
+ The template format can refer to the preset template listed below:
58
63
 
59
64
  - [Axle](templates/axle.ejs)
60
65
  - [Axios](templates/axios.ejs)
61
66
 
62
67
  See the bottom of the document for template variable definitions.
63
68
 
69
+ ### Status Code Strategy
70
+
71
+ `Restful API` recommends using different successful http status codes for different methods, such as `get: 200`, `post: 201`, etc. If you don't need this strategy, you can set `statusCodeStrategy` to `loose`
72
+
73
+ ```ts
74
+ // api-farmer.config.ts
75
+ import { defineConfig } from 'api-farmer'
76
+
77
+ export default defineConfig({
78
+ // 'strict' or 'loose', defaults 'strict'
79
+ statusCodeStrategy: 'loose',
80
+ })
81
+ ```
82
+
64
83
  ### Transformer API
65
84
 
66
85
  You can use the Transformer API to further define template variables, which will override the default transformation rules.
@@ -72,6 +91,7 @@ import { defineConfig } from 'api-farmer'
72
91
  export default defineConfig({
73
92
  transformer: {
74
93
  moduleName({ name }) {
94
+ // The new module name.
75
95
  return `${name}.generated`
76
96
  },
77
97
  verb() {},
@@ -127,7 +147,8 @@ export interface Config {
127
147
  */
128
148
  preset?: Preset
129
149
  /**
130
- * The status code strategy to use. loose: all success status codes are 200, strict: use the openapi recommended success status codes.
150
+ * The status code strategy to use.
151
+ * loose: all success status codes are 200, strict: use the openapi recommended success status codes.
131
152
  */
132
153
  statusCodeStrategy?: StatusCodeStrategy
133
154
  /**
@@ -176,55 +197,73 @@ export interface ApiModule {
176
197
 
177
198
  export interface ApiModulePayload {
178
199
  /**
179
- * The name of the API function/dispatcher, such as apiGetUsers, apiCreatePost, apiUpdateComment, etc.
200
+ * The name of the API function/dispatcher,
201
+ * such as apiGetUsers, apiCreatePost, apiUpdateComment, etc.
180
202
  */
181
203
  fn: string
182
204
  /**
183
- * The URL of the API endpoint, such as /users, /posts, /comments, etc.
205
+ * The URL of the API endpoint,
206
+ * such as /users, /posts, /comments, etc.
184
207
  */
185
208
  url: string
186
209
  /**
187
- * The HTTP method of the API endpoint, such as get, post, put, delete, etc.
210
+ * The HTTP method of the API endpoint,
211
+ * such as get, post, put, delete, etc.
188
212
  */
189
213
  method: string
190
214
  /**
191
- * The HTTP verb of the API endpoint, such as Get, Create, Update, Delete, etc.
215
+ * The HTTP verb of the API endpoint,
216
+ * such as Get, Create, Update, Delete, etc.
192
217
  */
193
218
  verb: string
194
219
  /**
195
- * The entity name of the API endpoint, such as User, Comment, Post, etc.
220
+ * The entity name of the API endpoint,
221
+ * such as User, Comment, Post, etc.
196
222
  */
197
223
  entity: string
198
224
  /**
199
- * The type name of the API endpoint, such as ApiGetUsers, ApiCreatePost, ApiUpdateComment, etc.
225
+ * The type name of the API endpoint,
226
+ * such as ApiGetUsers, ApiCreatePost, ApiUpdateComment, etc.
200
227
  */
201
228
  type: string
202
229
  /**
203
- * The value of the type of the API endpoint, such as paths['/users']['get'], paths['/posts']['post'], paths['/comments']['put'], etc.
230
+ * The value of the type of the API endpoint,
231
+ * such as paths['/users']['get'], paths['/posts']['post'], paths['/comments']['put'], etc.
204
232
  */
205
233
  typeValue: string
206
234
  /**
207
- * The type name of the query parameters of the API endpoint, such as ApiGetUsersQuery, ApiCreatePostQuery, ApiUpdateCommentQuery, etc.
235
+ * The type name of the query parameters of the API endpoint,
236
+ * such as ApiGetUsersQuery, ApiCreatePostQuery, ApiUpdateCommentQuery, etc.
208
237
  */
209
238
  typeQuery: string
210
239
  /**
211
- * The value of the type of the query parameters of the API endpoint, such as ApiGetUsersQuery['parameters']['query'], ApiCreatePostQuery['parameters']['query'], ApiUpdateCommentQuery['parameters']['query'], etc.
240
+ * The value of the type of the query parameters of the API endpoint,
241
+ * such as ApiGetUsersQuery['parameters']['query'], ApiCreatePostQuery['parameters']['query'],
242
+ * ApiUpdateCommentQuery['parameters']['query'], etc.
212
243
  */
213
244
  typeQueryValue: string
214
245
  /**
215
- * The type name of the request body of the API endpoint, such as ApiGetUsersRequestBody, ApiCreatePostRequestBody, ApiUpdateCommentRequestBody, etc.
246
+ * The type name of the request body of the API endpoint,
247
+ * such as ApiGetUsersRequestBody, ApiCreatePostRequestBody, ApiUpdateCommentRequestBody, etc.
216
248
  */
217
249
  typeRequestBody: string
218
250
  /**
219
- * The value of the type of the request body of the API endpoint, such as ApiGetUsersRequestBody['requestBody']['content']['application/json'], ApiCreatePostRequestBody['requestBody']['content']['application/json'], ApiUpdateCommentRequestBody['requestBody']['content']['application/json'], etc.
251
+ * The value of the type of the request body of the API endpoint,
252
+ * such as ApiGetUsersRequestBody['requestBody']['content']['application/json'],
253
+ * ApiCreatePostRequestBody['requestBody']['content']['application/json'],
254
+ * ApiUpdateCommentRequestBody['requestBody']['content']['application/json'], etc.
220
255
  */
221
256
  typeRequestBodyValue: string
222
257
  /**
223
- * The type name of the response body of the API endpoint, such as ApiGetUsersResponseBody, ApiCreatePostResponseBody, ApiUpdateCommentResponseBody, etc.
258
+ * The type name of the response body of the API endpoint,
259
+ * such as ApiGetUsersResponseBody, ApiCreatePostResponseBody, ApiUpdateCommentResponseBody, etc.
224
260
  */
225
261
  typeResponseBody: string
226
262
  /**
227
- * The value of the type of the response body of the API endpoint, such as ApiGetUsersResponseBody['responses']['200']['content']['application/json'], ApiCreatePostResponseBody['responses']['201']['content']['application/json'], ApiUpdateCommentResponseBody['responses']['200']['content']['application/json'], etc.
263
+ * The value of the type of the response body of the API endpoint,
264
+ * such as ApiGetUsersResponseBody['responses']['200']['content']['application/json'],
265
+ * ApiCreatePostResponseBody['responses']['201']['content']['application/json'],
266
+ * ApiUpdateCommentResponseBody['responses']['200']['content']['application/json'], etc.
228
267
  */
229
268
  typeResponseBodyValue: string
230
269
  }
@@ -1,11 +1,13 @@
1
1
  import {
2
2
  CWD,
3
3
  createStatusCodesByStrategy,
4
+ getResponseMime,
4
5
  hasQueryParameter,
5
6
  hasResponseBody,
7
+ isRequiredRequestBody,
6
8
  readSchema,
7
9
  readTemplateFile
8
- } from "./chunk-6N4OHGAC.js";
10
+ } from "./chunk-TOPFUKIL.js";
9
11
  import {
10
12
  __export
11
13
  } from "./chunk-6OIOYGN7.js";
@@ -22784,8 +22786,8 @@ function transformTypeQueryValue({ type: type2 }) {
22784
22786
  function transformTypeRequestBody({ verb, entity }) {
22785
22787
  return `Api${verb}${entity}RequestBody`;
22786
22788
  }
22787
- function transformTypeRequestBodyValue({ type: type2 }) {
22788
- return `${type2}['requestBody']['content']['application/json']`;
22789
+ function transformTypeRequestBodyValue({ type: type2, required }) {
22790
+ return required ? `${type2}['requestBody']['content']['application/json']` : `NonNullable<${type2}['requestBody']>['content']['application/json'] | undefined`;
22789
22791
  }
22790
22792
  function transformTypeResponseBody({ verb, entity }) {
22791
22793
  return `Api${verb}${entity}ResponseBody`;
@@ -22835,11 +22837,14 @@ function partitionApiModules(schema2, transformer, options8) {
22835
22837
  const typeQuery = transformer.typeQuery({ verb, entity });
22836
22838
  const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue({ type: type2 }) : "never";
22837
22839
  const typeRequestBody = transformer.typeRequestBody({ verb, entity });
22838
- const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({ type: type2 }) : "never";
22839
- const typeResponseBody = transformer.typeResponseBody({ verb, entity });
22840
+ const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({
22841
+ type: type2,
22842
+ required: isRequiredRequestBody(operation.requestBody)
22843
+ }) : "never";
22840
22844
  const statusCode = statusCodes[method] ?? 200;
22841
- const mime = (operation.responses?.[statusCode]).content?.["application/json"] ? "application/json" : "*/*";
22842
- const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
22845
+ const mime = getResponseMime(operation, statusCode);
22846
+ const typeResponseBody = transformer.typeResponseBody({ verb, entity });
22847
+ const typeResponseBodyValue = hasResponseBody(operation) && mime ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
22843
22848
  payloads3.push({
22844
22849
  fn,
22845
22850
  url: url2,
@@ -60,6 +60,13 @@ function hasResponseBody(operation) {
60
60
  function getCliVersion() {
61
61
  return fse.readJsonSync(CLI_PACKAGE_JSON).version;
62
62
  }
63
+ function isRequiredRequestBody(value) {
64
+ return "required" in value && value.required === true;
65
+ }
66
+ function getResponseMime(operation, statusCode) {
67
+ const content = operation.responses?.[statusCode]?.content;
68
+ return content?.["application/json"] ? "application/json" : content?.["*/*"] ? "*/*" : void 0;
69
+ }
63
70
 
64
71
  export {
65
72
  CWD,
@@ -68,5 +75,7 @@ export {
68
75
  readTemplateFile,
69
76
  hasQueryParameter,
70
77
  hasResponseBody,
71
- getCliVersion
78
+ getCliVersion,
79
+ isRequiredRequestBody,
80
+ getResponseMime
72
81
  };
package/dist/cli.cjs CHANGED
@@ -101,6 +101,13 @@ function hasResponseBody(operation) {
101
101
  function getCliVersion() {
102
102
  return import_fs_extra.default.readJsonSync(CLI_PACKAGE_JSON).version;
103
103
  }
104
+ function isRequiredRequestBody(value) {
105
+ return "required" in value && value.required === true;
106
+ }
107
+ function getResponseMime(operation, statusCode) {
108
+ const content = operation.responses?.[statusCode]?.content;
109
+ return content?.["application/json"] ? "application/json" : content?.["*/*"] ? "*/*" : void 0;
110
+ }
104
111
  var import_path2, import_fs_extra, import_swagger2openapi, import_yaml;
105
112
  var init_utils = __esm({
106
113
  "src/utils.ts"() {
@@ -102043,8 +102050,8 @@ function transformTypeQueryValue({ type: type2 }) {
102043
102050
  function transformTypeRequestBody({ verb, entity }) {
102044
102051
  return `Api${verb}${entity}RequestBody`;
102045
102052
  }
102046
- function transformTypeRequestBodyValue({ type: type2 }) {
102047
- return `${type2}['requestBody']['content']['application/json']`;
102053
+ function transformTypeRequestBodyValue({ type: type2, required }) {
102054
+ return required ? `${type2}['requestBody']['content']['application/json']` : `NonNullable<${type2}['requestBody']>['content']['application/json'] | undefined`;
102048
102055
  }
102049
102056
  function transformTypeResponseBody({ verb, entity }) {
102050
102057
  return `Api${verb}${entity}ResponseBody`;
@@ -102110,11 +102117,14 @@ function partitionApiModules(schema2, transformer, options8) {
102110
102117
  const typeQuery = transformer.typeQuery({ verb, entity });
102111
102118
  const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue({ type: type2 }) : "never";
102112
102119
  const typeRequestBody = transformer.typeRequestBody({ verb, entity });
102113
- const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({ type: type2 }) : "never";
102114
- const typeResponseBody = transformer.typeResponseBody({ verb, entity });
102120
+ const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({
102121
+ type: type2,
102122
+ required: isRequiredRequestBody(operation.requestBody)
102123
+ }) : "never";
102115
102124
  const statusCode = statusCodes[method] ?? 200;
102116
- const mime = (operation.responses?.[statusCode]).content?.["application/json"] ? "application/json" : "*/*";
102117
- const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
102125
+ const mime = getResponseMime(operation, statusCode);
102126
+ const typeResponseBody = transformer.typeResponseBody({ verb, entity });
102127
+ const typeResponseBodyValue = hasResponseBody(operation) && mime ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
102118
102128
  payloads3.push({
102119
102129
  fn: fn8,
102120
102130
  url: url2,
package/dist/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  getCliVersion
4
- } from "./chunk-6N4OHGAC.js";
4
+ } from "./chunk-TOPFUKIL.js";
5
5
  import "./chunk-6OIOYGN7.js";
6
6
 
7
7
  // src/cli.ts
@@ -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-23Q2CMLJ.js");
12
+ const { generate } = await import("./generate-45FVXFDO.js");
13
13
  return generate();
14
14
  });
15
15
  program.parse();
@@ -3,8 +3,8 @@ import {
3
3
  generateTypes,
4
4
  partitionApiModules,
5
5
  renderApiModules
6
- } from "./chunk-ZLLNTCL2.js";
7
- import "./chunk-6N4OHGAC.js";
6
+ } from "./chunk-BGSEKRRW.js";
7
+ import "./chunk-TOPFUKIL.js";
8
8
  import "./chunk-6OIOYGN7.js";
9
9
  export {
10
10
  generate,
package/dist/index.cjs CHANGED
@@ -79232,8 +79232,10 @@ __export(index_exports, {
79232
79232
  generateTypes: () => generateTypes,
79233
79233
  getCliVersion: () => getCliVersion,
79234
79234
  getConfig: () => getConfig,
79235
+ getResponseMime: () => getResponseMime,
79235
79236
  hasQueryParameter: () => hasQueryParameter,
79236
79237
  hasResponseBody: () => hasResponseBody,
79238
+ isRequiredRequestBody: () => isRequiredRequestBody,
79237
79239
  partitionApiModules: () => partitionApiModules,
79238
79240
  pluralize: () => import_pluralize2.default,
79239
79241
  readSchema: () => readSchema,
@@ -79308,8 +79310,8 @@ function transformTypeQueryValue({ type: type2 }) {
79308
79310
  function transformTypeRequestBody({ verb, entity }) {
79309
79311
  return `Api${verb}${entity}RequestBody`;
79310
79312
  }
79311
- function transformTypeRequestBodyValue({ type: type2 }) {
79312
- return `${type2}['requestBody']['content']['application/json']`;
79313
+ function transformTypeRequestBodyValue({ type: type2, required }) {
79314
+ return required ? `${type2}['requestBody']['content']['application/json']` : `NonNullable<${type2}['requestBody']>['content']['application/json'] | undefined`;
79313
79315
  }
79314
79316
  function transformTypeResponseBody({ verb, entity }) {
79315
79317
  return `Api${verb}${entity}ResponseBody`;
@@ -102126,6 +102128,13 @@ function hasResponseBody(operation) {
102126
102128
  function getCliVersion() {
102127
102129
  return import_fs_extra.default.readJsonSync(CLI_PACKAGE_JSON).version;
102128
102130
  }
102131
+ function isRequiredRequestBody(value) {
102132
+ return "required" in value && value.required === true;
102133
+ }
102134
+ function getResponseMime(operation, statusCode) {
102135
+ const content = operation.responses?.[statusCode]?.content;
102136
+ return content?.["application/json"] ? "application/json" : content?.["*/*"] ? "*/*" : void 0;
102137
+ }
102129
102138
 
102130
102139
  // src/generate.ts
102131
102140
  function partitionApiModules(schema2, transformer, options8) {
@@ -102147,11 +102156,14 @@ function partitionApiModules(schema2, transformer, options8) {
102147
102156
  const typeQuery = transformer.typeQuery({ verb, entity });
102148
102157
  const typeQueryValue = hasQueryParameter(operation) ? transformer.typeQueryValue({ type: type2 }) : "never";
102149
102158
  const typeRequestBody = transformer.typeRequestBody({ verb, entity });
102150
- const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({ type: type2 }) : "never";
102151
- const typeResponseBody = transformer.typeResponseBody({ verb, entity });
102159
+ const typeRequestBodyValue = operation.requestBody ? transformer.typeRequestBodyValue({
102160
+ type: type2,
102161
+ required: isRequiredRequestBody(operation.requestBody)
102162
+ }) : "never";
102152
102163
  const statusCode = statusCodes[method] ?? 200;
102153
- const mime = (operation.responses?.[statusCode]).content?.["application/json"] ? "application/json" : "*/*";
102154
- const typeResponseBodyValue = hasResponseBody(operation) ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
102164
+ const mime = getResponseMime(operation, statusCode);
102165
+ const typeResponseBody = transformer.typeResponseBody({ verb, entity });
102166
+ const typeResponseBodyValue = hasResponseBody(operation) && mime ? transformer.typeResponseBodyValue({ type: type2, statusCode, mime }) : "never";
102155
102167
  payloads3.push({
102156
102168
  fn: fn8,
102157
102169
  url: url2,
@@ -102257,8 +102269,10 @@ var import_pluralize2 = __toESM(require("pluralize"), 1);
102257
102269
  generateTypes,
102258
102270
  getCliVersion,
102259
102271
  getConfig,
102272
+ getResponseMime,
102260
102273
  hasQueryParameter,
102261
102274
  hasResponseBody,
102275
+ isRequiredRequestBody,
102262
102276
  partitionApiModules,
102263
102277
  pluralize,
102264
102278
  readSchema,
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { OpenAPI3, OperationObject } from 'openapi-typescript';
1
+ import { OpenAPI3, OperationObject, RequestBodyObject, ReferenceObject } from 'openapi-typescript';
2
2
  export { default as pluralize } from 'pluralize';
3
3
 
4
4
  declare function transformModuleName({ name }: {
@@ -39,8 +39,9 @@ declare function transformTypeRequestBody({ verb, entity }: {
39
39
  verb: string;
40
40
  entity: string;
41
41
  }): string;
42
- declare function transformTypeRequestBodyValue({ type }: {
42
+ declare function transformTypeRequestBodyValue({ type, required }: {
43
43
  type: string;
44
+ required: boolean;
44
45
  }): string;
45
46
  declare function transformTypeResponseBody({ verb, entity }: {
46
47
  verb: string;
@@ -101,6 +102,8 @@ declare function readTemplateFile(preset?: Preset): string;
101
102
  declare function hasQueryParameter(operation: OperationObject): boolean;
102
103
  declare function hasResponseBody(operation: OperationObject): boolean;
103
104
  declare function getCliVersion(): any;
105
+ declare function isRequiredRequestBody(value: RequestBodyObject | ReferenceObject): boolean;
106
+ declare function getResponseMime(operation: OperationObject, statusCode: number): "application/json" | "*/*" | undefined;
104
107
 
105
108
  interface ApiModuleTemplateData {
106
109
  /**
@@ -249,4 +252,4 @@ type Config = GenerateOptions;
249
252
  declare function defineConfig(config: Config): GenerateOptions;
250
253
  declare function getConfig(): Promise<Config>;
251
254
 
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 };
255
+ 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, getResponseMime, hasQueryParameter, hasResponseBody, isRequiredRequestBody, partitionApiModules, readSchema, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { OpenAPI3, OperationObject } from 'openapi-typescript';
1
+ import { OpenAPI3, OperationObject, RequestBodyObject, ReferenceObject } from 'openapi-typescript';
2
2
  export { default as pluralize } from 'pluralize';
3
3
 
4
4
  declare function transformModuleName({ name }: {
@@ -39,8 +39,9 @@ declare function transformTypeRequestBody({ verb, entity }: {
39
39
  verb: string;
40
40
  entity: string;
41
41
  }): string;
42
- declare function transformTypeRequestBodyValue({ type }: {
42
+ declare function transformTypeRequestBodyValue({ type, required }: {
43
43
  type: string;
44
+ required: boolean;
44
45
  }): string;
45
46
  declare function transformTypeResponseBody({ verb, entity }: {
46
47
  verb: string;
@@ -101,6 +102,8 @@ declare function readTemplateFile(preset?: Preset): string;
101
102
  declare function hasQueryParameter(operation: OperationObject): boolean;
102
103
  declare function hasResponseBody(operation: OperationObject): boolean;
103
104
  declare function getCliVersion(): any;
105
+ declare function isRequiredRequestBody(value: RequestBodyObject | ReferenceObject): boolean;
106
+ declare function getResponseMime(operation: OperationObject, statusCode: number): "application/json" | "*/*" | undefined;
104
107
 
105
108
  interface ApiModuleTemplateData {
106
109
  /**
@@ -249,4 +252,4 @@ type Config = GenerateOptions;
249
252
  declare function defineConfig(config: Config): GenerateOptions;
250
253
  declare function getConfig(): Promise<Config>;
251
254
 
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 };
255
+ 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, getResponseMime, hasQueryParameter, hasResponseBody, isRequiredRequestBody, partitionApiModules, readSchema, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
package/dist/index.js CHANGED
@@ -19,15 +19,17 @@ import {
19
19
  transformTypeValue,
20
20
  transformUrl,
21
21
  transformVerb
22
- } from "./chunk-ZLLNTCL2.js";
22
+ } from "./chunk-BGSEKRRW.js";
23
23
  import {
24
24
  createStatusCodesByStrategy,
25
25
  getCliVersion,
26
+ getResponseMime,
26
27
  hasQueryParameter,
27
28
  hasResponseBody,
29
+ isRequiredRequestBody,
28
30
  readSchema,
29
31
  readTemplateFile
30
- } from "./chunk-6N4OHGAC.js";
32
+ } from "./chunk-TOPFUKIL.js";
31
33
  import "./chunk-6OIOYGN7.js";
32
34
 
33
35
  // src/index.ts
@@ -40,8 +42,10 @@ export {
40
42
  generateTypes,
41
43
  getCliVersion,
42
44
  getConfig,
45
+ getResponseMime,
43
46
  hasQueryParameter,
44
47
  hasResponseBody,
48
+ isRequiredRequestBody,
45
49
  partitionApiModules,
46
50
  default2 as pluralize,
47
51
  readSchema,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "api-farmer",
3
- "version": "0.0.4",
4
- "description": "A cli to generate apis from a schema",
3
+ "version": "0.0.6",
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
+