api-farmer 0.0.15 → 0.0.17

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
@@ -51,38 +51,11 @@ npx af
51
51
 
52
52
  > [!TIP]
53
53
  > The generated content does not include the integration of the request client.
54
- > If you allow ts code output, you will see a custom type called `Res`. This type needs to be defined in `global.d.ts` for use.
55
54
 
56
55
  #### Some Examples
57
56
 
58
57
  Some simple usage examples can be found [here](fixtures)
59
58
 
60
- ### Custom EJS Template
61
-
62
- Create `api-farmer.ejs` in the project root, which will replace the `preset` template.
63
- The template format can refer to the preset template listed below:
64
-
65
- - [Axle](templates/axle.ejs)
66
- - [Axios](templates/axios.ejs)
67
-
68
- See the bottom of the document for template variable definitions.
69
-
70
- ### Status Code Strategy
71
-
72
- `smart`: find a valid status code between [`200`, `299`] that is closest to `200` <br>
73
- `loose`: all success status codes are `200` <br>
74
- `strict`: `Restful API` recommends using different successful http status codes for different methods, such as `get: 200`, `post: 201`, etc. <br>
75
-
76
- ```ts
77
- // api-farmer.config.ts
78
- import { defineConfig } from 'api-farmer'
79
-
80
- export default defineConfig({
81
- // 'strict' or 'loose' or 'smart', defaults 'smart'
82
- statusCodeStrategy: 'strict',
83
- })
84
- ```
85
-
86
59
  ### Transformer API
87
60
 
88
61
  You can use the Transformer API to further define template variables, which will override the default transformation rules.
@@ -113,6 +86,16 @@ export default defineConfig({
113
86
  })
114
87
  ```
115
88
 
89
+ ### Custom EJS Template
90
+
91
+ Create `api-farmer.ejs` in the project root, which will replace the `preset` template.
92
+ The template format can refer to the preset template listed below:
93
+
94
+ - [Axle](templates/axle.ejs)
95
+ - [Axios](templates/axios.ejs)
96
+
97
+ See the bottom of the document for template variable definitions.
98
+
116
99
  ### Configuration Options
117
100
 
118
101
  ```ts
@@ -141,6 +124,10 @@ export interface Config {
141
124
  * Whether to generate TypeScript code.
142
125
  */
143
126
  ts?: boolean
127
+ /**
128
+ * Whether to generate only types.
129
+ */
130
+ typesOnly?: boolean
144
131
  /**
145
132
  * Whether to override the existing files, or an array of filenames to override.
146
133
  */
@@ -149,25 +136,6 @@ export interface Config {
149
136
  * The preset ejs template to use.
150
137
  */
151
138
  preset?: Preset
152
- /**
153
- * The status code strategy to use.
154
- * loose: all success status codes are 200,
155
- * strict: use the openapi recommended success status codes.
156
- * smart: find a valid status code between [200, 299] that is closest to 200
157
- */
158
- statusCodeStrategy?: StatusCodeStrategy
159
- /**
160
- * The status codes to override the default status codes.
161
- */
162
- statusCodes?: {
163
- get?: number
164
- post?: number
165
- put?: number
166
- delete?: number
167
- patch?: number
168
- options?: number
169
- head?: number
170
- }
171
139
  }
172
140
  ```
173
141
 
@@ -187,6 +155,10 @@ export interface ApiModuleTemplateData {
187
155
  * Whether to generate ts code
188
156
  */
189
157
  ts: boolean
158
+ /**
159
+ * Whether to generate only types.
160
+ */
161
+ typesOnly?: boolean
190
162
  }
191
163
 
192
164
  export interface ApiModule {
@@ -1,13 +1,12 @@
1
1
  import {
2
2
  CWD,
3
3
  SUPPORTED_HTTP_METHODS,
4
- createStatusCodesByStrategy,
5
- doStatusCodeStrategy,
4
+ getSuccessfulResponseMeme,
6
5
  hasQueryParameter,
7
6
  isRequiredRequestBody,
8
7
  readSchema,
9
8
  readTemplateFile
10
- } from "./chunk-LYOTF4II.js";
9
+ } from "./chunk-MLHGCJ6R.js";
11
10
  import {
12
11
  __export
13
12
  } from "./chunk-6OIOYGN7.js";
@@ -22829,7 +22828,7 @@ function createTransformer() {
22829
22828
 
22830
22829
  // src/generate.ts
22831
22830
  function transformPayloads(pathItems, options8) {
22832
- const { transformer, path: path13, base, statusCodeStrategy, statusCodes } = options8;
22831
+ const { transformer, path: path13, base } = options8;
22833
22832
  return Object.entries(pathItems).filter(([key2]) => SUPPORTED_HTTP_METHODS.includes(key2)).reduce((payloads, [method, operation]) => {
22834
22833
  const url2 = transformer.url({ path: path13, base });
22835
22834
  const args = { path: path13, base, url: url2, method, operation };
@@ -22848,11 +22847,7 @@ function transformPayloads(pathItems, options8) {
22848
22847
  entity,
22849
22848
  required: isRequiredRequestBody(operation.requestBody)
22850
22849
  }) : "undefined";
22851
- const { mime, statusCode } = doStatusCodeStrategy(
22852
- operation,
22853
- statusCodes[method] ?? 200,
22854
- statusCodeStrategy
22855
- );
22850
+ const { mime, statusCode } = getSuccessfulResponseMeme(operation);
22856
22851
  const typeResponseBody = transformer.typeResponseBody({ ...args, type: type2, verb, entity });
22857
22852
  const typeResponseBodyValue = mime && statusCode ? transformer.typeResponseBodyValue({ ...args, type: type2, verb, entity, statusCode, mime }) : "undefined";
22858
22853
  payloads.push({
@@ -22890,7 +22885,7 @@ function partitionApiModules(schema2, options8) {
22890
22885
  return apiModules;
22891
22886
  }
22892
22887
  function renderApiModules(apiModules, options8) {
22893
- const { output, ts, overrides, preset } = options8;
22888
+ const { output, ts, typesOnly, overrides, preset } = options8;
22894
22889
  const templateFile = readTemplateFile(preset);
22895
22890
  const typesFilename = options8.typesFilename.replace(".ts", "");
22896
22891
  return Promise.all(
@@ -22899,7 +22894,8 @@ function renderApiModules(apiModules, options8) {
22899
22894
  const data = {
22900
22895
  apiModule,
22901
22896
  typesFilename,
22902
- ts
22897
+ ts,
22898
+ typesOnly
22903
22899
  };
22904
22900
  src_default.format(ejs.render(templateFile, data), {
22905
22901
  parser: "typescript",
@@ -22935,18 +22931,14 @@ async function generate(userOptions = {}) {
22935
22931
  const {
22936
22932
  base,
22937
22933
  ts = true,
22934
+ typesOnly = false,
22938
22935
  overrides = true,
22939
22936
  preset = "axle",
22940
- statusCodeStrategy = "smart",
22941
22937
  input = "./schema.json",
22942
22938
  output = "./src/apis/generated",
22943
22939
  typesFilename = "_types.ts",
22944
22940
  transformer = {}
22945
22941
  } = options8;
22946
- const statusCodes = {
22947
- ...createStatusCodesByStrategy(statusCodeStrategy),
22948
- ...options8.statusCodes ?? {}
22949
- };
22950
22942
  const mergedTransformer = { ...createTransformer(), ...transformer };
22951
22943
  const schema2 = await readSchema(input);
22952
22944
  logger.info("Generating API modules...");
@@ -22954,12 +22946,10 @@ async function generate(userOptions = {}) {
22954
22946
  await generateTypes(schema2, output, typesFilename);
22955
22947
  }
22956
22948
  const apiModules = partitionApiModules(schema2, {
22957
- statusCodes,
22958
- statusCodeStrategy,
22959
22949
  base,
22960
22950
  transformer: mergedTransformer
22961
22951
  });
22962
- await renderApiModules(apiModules, { output, typesFilename, ts, overrides, preset });
22952
+ await renderApiModules(apiModules, { output, typesFilename, ts, typesOnly, overrides, preset });
22963
22953
  logger.success("Done");
22964
22954
  }
22965
22955
 
@@ -68,18 +68,16 @@ function getCliVersion() {
68
68
  function isRequiredRequestBody(value) {
69
69
  return "required" in value && value.required === true;
70
70
  }
71
- function doStatusCodeStrategy(operation, statusCode, strategy) {
72
- if (strategy === "smart") {
73
- const responses = operation.responses ?? {};
74
- const codeKey = Object.keys(responses).sort((a, b) => Number(a) - Number(b)).find((codeKey2) => Number(codeKey2) >= 200 && Number(codeKey2) <= 299);
75
- if (!codeKey) {
76
- return {
77
- statusCode: void 0,
78
- mime: void 0
79
- };
80
- }
81
- statusCode = Number(codeKey);
71
+ function getSuccessfulResponseMeme(operation) {
72
+ const responses = operation.responses ?? {};
73
+ const codeKey = Object.keys(responses).sort((a, b) => Number(a) - Number(b)).find((codeKey2) => Number(codeKey2) >= 200 && Number(codeKey2) <= 299);
74
+ if (!codeKey) {
75
+ return {
76
+ statusCode: void 0,
77
+ mime: void 0
78
+ };
82
79
  }
80
+ const statusCode = Number(codeKey);
83
81
  const content = operation.responses?.[statusCode]?.content;
84
82
  const mime = content?.["application/json"] ? "application/json" : content?.["*/*"] ? "*/*" : void 0;
85
83
  return {
@@ -97,5 +95,5 @@ export {
97
95
  hasQueryParameter,
98
96
  getCliVersion,
99
97
  isRequiredRequestBody,
100
- doStatusCodeStrategy
98
+ getSuccessfulResponseMeme
101
99
  };
package/dist/cli.cjs CHANGED
@@ -55,37 +55,6 @@ var init_constants = __esm({
55
55
  });
56
56
 
57
57
  // src/utils.ts
58
- function createStatusCodesByStrategy(strategy) {
59
- return {
60
- strict: {
61
- get: 200,
62
- post: 201,
63
- put: 200,
64
- delete: 204,
65
- patch: 200,
66
- options: 204,
67
- head: 200
68
- },
69
- loose: {
70
- get: 200,
71
- post: 200,
72
- put: 200,
73
- delete: 200,
74
- patch: 200,
75
- options: 200,
76
- head: 200
77
- },
78
- smart: {
79
- get: 200,
80
- post: 200,
81
- put: 200,
82
- delete: 200,
83
- patch: 200,
84
- options: 200,
85
- head: 200
86
- }
87
- }[strategy];
88
- }
89
58
  async function readSchema(input) {
90
59
  const isYaml = input.endsWith(".yaml");
91
60
  const path13 = (0, import_path2.resolve)(CWD, input);
@@ -109,18 +78,16 @@ function getCliVersion() {
109
78
  function isRequiredRequestBody(value) {
110
79
  return "required" in value && value.required === true;
111
80
  }
112
- function doStatusCodeStrategy(operation, statusCode, strategy) {
113
- if (strategy === "smart") {
114
- const responses = operation.responses ?? {};
115
- const codeKey = Object.keys(responses).sort((a5, b8) => Number(a5) - Number(b8)).find((codeKey2) => Number(codeKey2) >= 200 && Number(codeKey2) <= 299);
116
- if (!codeKey) {
117
- return {
118
- statusCode: void 0,
119
- mime: void 0
120
- };
121
- }
122
- statusCode = Number(codeKey);
81
+ function getSuccessfulResponseMeme(operation) {
82
+ const responses = operation.responses ?? {};
83
+ const codeKey = Object.keys(responses).sort((a5, b8) => Number(a5) - Number(b8)).find((codeKey2) => Number(codeKey2) >= 200 && Number(codeKey2) <= 299);
84
+ if (!codeKey) {
85
+ return {
86
+ statusCode: void 0,
87
+ mime: void 0
88
+ };
123
89
  }
90
+ const statusCode = Number(codeKey);
124
91
  const content = operation.responses?.[statusCode]?.content;
125
92
  const mime = content?.["application/json"] ? "application/json" : content?.["*/*"] ? "*/*" : void 0;
126
93
  return {
@@ -102130,7 +102097,7 @@ __export(generate_exports, {
102130
102097
  transformPayloads: () => transformPayloads
102131
102098
  });
102132
102099
  function transformPayloads(pathItems, options8) {
102133
- const { transformer, path: path13, base, statusCodeStrategy, statusCodes } = options8;
102100
+ const { transformer, path: path13, base } = options8;
102134
102101
  return Object.entries(pathItems).filter(([key2]) => SUPPORTED_HTTP_METHODS.includes(key2)).reduce((payloads, [method, operation]) => {
102135
102102
  const url2 = transformer.url({ path: path13, base });
102136
102103
  const args = { path: path13, base, url: url2, method, operation };
@@ -102149,11 +102116,7 @@ function transformPayloads(pathItems, options8) {
102149
102116
  entity,
102150
102117
  required: isRequiredRequestBody(operation.requestBody)
102151
102118
  }) : "undefined";
102152
- const { mime, statusCode } = doStatusCodeStrategy(
102153
- operation,
102154
- statusCodes[method] ?? 200,
102155
- statusCodeStrategy
102156
- );
102119
+ const { mime, statusCode } = getSuccessfulResponseMeme(operation);
102157
102120
  const typeResponseBody = transformer.typeResponseBody({ ...args, type: type2, verb, entity });
102158
102121
  const typeResponseBodyValue = mime && statusCode ? transformer.typeResponseBodyValue({ ...args, type: type2, verb, entity, statusCode, mime }) : "undefined";
102159
102122
  payloads.push({
@@ -102191,7 +102154,7 @@ function partitionApiModules(schema2, options8) {
102191
102154
  return apiModules;
102192
102155
  }
102193
102156
  function renderApiModules(apiModules, options8) {
102194
- const { output, ts: ts9, overrides, preset } = options8;
102157
+ const { output, ts: ts9, typesOnly, overrides, preset } = options8;
102195
102158
  const templateFile = readTemplateFile(preset);
102196
102159
  const typesFilename = options8.typesFilename.replace(".ts", "");
102197
102160
  return Promise.all(
@@ -102200,7 +102163,8 @@ function renderApiModules(apiModules, options8) {
102200
102163
  const data = {
102201
102164
  apiModule,
102202
102165
  typesFilename,
102203
- ts: ts9
102166
+ ts: ts9,
102167
+ typesOnly
102204
102168
  };
102205
102169
  src_default.format(import_ejs.default.render(templateFile, data), {
102206
102170
  parser: "typescript",
@@ -102236,18 +102200,14 @@ async function generate(userOptions = {}) {
102236
102200
  const {
102237
102201
  base,
102238
102202
  ts: ts9 = true,
102203
+ typesOnly = false,
102239
102204
  overrides = true,
102240
102205
  preset = "axle",
102241
- statusCodeStrategy = "smart",
102242
102206
  input = "./schema.json",
102243
102207
  output = "./src/apis/generated",
102244
102208
  typesFilename = "_types.ts",
102245
102209
  transformer = {}
102246
102210
  } = options8;
102247
- const statusCodes = {
102248
- ...createStatusCodesByStrategy(statusCodeStrategy),
102249
- ...options8.statusCodes ?? {}
102250
- };
102251
102211
  const mergedTransformer = { ...createTransformer(), ...transformer };
102252
102212
  const schema2 = await readSchema(input);
102253
102213
  import_rslog.logger.info("Generating API modules...");
@@ -102255,12 +102215,10 @@ async function generate(userOptions = {}) {
102255
102215
  await generateTypes(schema2, output, typesFilename);
102256
102216
  }
102257
102217
  const apiModules = partitionApiModules(schema2, {
102258
- statusCodes,
102259
- statusCodeStrategy,
102260
102218
  base,
102261
102219
  transformer: mergedTransformer
102262
102220
  });
102263
- await renderApiModules(apiModules, { output, typesFilename, ts: ts9, overrides, preset });
102221
+ await renderApiModules(apiModules, { output, typesFilename, ts: ts9, typesOnly, overrides, preset });
102264
102222
  import_rslog.logger.success("Done");
102265
102223
  }
102266
102224
  var import_path14, import_ejs, import_fs_extra2, import_openapi_typescript, import_rattail2, import_rslog;
package/dist/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  getCliVersion
4
- } from "./chunk-LYOTF4II.js";
4
+ } from "./chunk-MLHGCJ6R.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-RJ23AKV7.js");
12
+ const { generate } = await import("./generate-7X5RRT3J.js");
13
13
  return generate();
14
14
  });
15
15
  program.parse();
@@ -4,8 +4,8 @@ import {
4
4
  partitionApiModules,
5
5
  renderApiModules,
6
6
  transformPayloads
7
- } from "./chunk-VAAFC73D.js";
8
- import "./chunk-LYOTF4II.js";
7
+ } from "./chunk-J2G2MBMU.js";
8
+ import "./chunk-MLHGCJ6R.js";
9
9
  import "./chunk-6OIOYGN7.js";
10
10
  export {
11
11
  generate,
package/dist/index.cjs CHANGED
@@ -79228,11 +79228,11 @@ __export(index_exports, {
79228
79228
  createStatusCodesByStrategy: () => createStatusCodesByStrategy,
79229
79229
  createTransformer: () => createTransformer,
79230
79230
  defineConfig: () => defineConfig,
79231
- doStatusCodeStrategy: () => doStatusCodeStrategy,
79232
79231
  generate: () => generate,
79233
79232
  generateTypes: () => generateTypes,
79234
79233
  getCliVersion: () => getCliVersion,
79235
79234
  getConfig: () => getConfig,
79235
+ getSuccessfulResponseMeme: () => getSuccessfulResponseMeme,
79236
79236
  hasQueryParameter: () => hasQueryParameter,
79237
79237
  isRequiredRequestBody: () => isRequiredRequestBody,
79238
79238
  partitionApiModules: () => partitionApiModules,
@@ -102146,18 +102146,16 @@ function getCliVersion() {
102146
102146
  function isRequiredRequestBody(value) {
102147
102147
  return "required" in value && value.required === true;
102148
102148
  }
102149
- function doStatusCodeStrategy(operation, statusCode, strategy) {
102150
- if (strategy === "smart") {
102151
- const responses = operation.responses ?? {};
102152
- const codeKey = Object.keys(responses).sort((a5, b8) => Number(a5) - Number(b8)).find((codeKey2) => Number(codeKey2) >= 200 && Number(codeKey2) <= 299);
102153
- if (!codeKey) {
102154
- return {
102155
- statusCode: void 0,
102156
- mime: void 0
102157
- };
102158
- }
102159
- statusCode = Number(codeKey);
102149
+ function getSuccessfulResponseMeme(operation) {
102150
+ const responses = operation.responses ?? {};
102151
+ const codeKey = Object.keys(responses).sort((a5, b8) => Number(a5) - Number(b8)).find((codeKey2) => Number(codeKey2) >= 200 && Number(codeKey2) <= 299);
102152
+ if (!codeKey) {
102153
+ return {
102154
+ statusCode: void 0,
102155
+ mime: void 0
102156
+ };
102160
102157
  }
102158
+ const statusCode = Number(codeKey);
102161
102159
  const content = operation.responses?.[statusCode]?.content;
102162
102160
  const mime = content?.["application/json"] ? "application/json" : content?.["*/*"] ? "*/*" : void 0;
102163
102161
  return {
@@ -102168,7 +102166,7 @@ function doStatusCodeStrategy(operation, statusCode, strategy) {
102168
102166
 
102169
102167
  // src/generate.ts
102170
102168
  function transformPayloads(pathItems, options8) {
102171
- const { transformer, path: path13, base, statusCodeStrategy, statusCodes } = options8;
102169
+ const { transformer, path: path13, base } = options8;
102172
102170
  return Object.entries(pathItems).filter(([key2]) => SUPPORTED_HTTP_METHODS.includes(key2)).reduce((payloads, [method, operation]) => {
102173
102171
  const url2 = transformer.url({ path: path13, base });
102174
102172
  const args = { path: path13, base, url: url2, method, operation };
@@ -102187,11 +102185,7 @@ function transformPayloads(pathItems, options8) {
102187
102185
  entity,
102188
102186
  required: isRequiredRequestBody(operation.requestBody)
102189
102187
  }) : "undefined";
102190
- const { mime, statusCode } = doStatusCodeStrategy(
102191
- operation,
102192
- statusCodes[method] ?? 200,
102193
- statusCodeStrategy
102194
- );
102188
+ const { mime, statusCode } = getSuccessfulResponseMeme(operation);
102195
102189
  const typeResponseBody = transformer.typeResponseBody({ ...args, type: type2, verb, entity });
102196
102190
  const typeResponseBodyValue = mime && statusCode ? transformer.typeResponseBodyValue({ ...args, type: type2, verb, entity, statusCode, mime }) : "undefined";
102197
102191
  payloads.push({
@@ -102229,7 +102223,7 @@ function partitionApiModules(schema2, options8) {
102229
102223
  return apiModules;
102230
102224
  }
102231
102225
  function renderApiModules(apiModules, options8) {
102232
- const { output, ts: ts9, overrides, preset } = options8;
102226
+ const { output, ts: ts9, typesOnly, overrides, preset } = options8;
102233
102227
  const templateFile = readTemplateFile(preset);
102234
102228
  const typesFilename = options8.typesFilename.replace(".ts", "");
102235
102229
  return Promise.all(
@@ -102238,7 +102232,8 @@ function renderApiModules(apiModules, options8) {
102238
102232
  const data = {
102239
102233
  apiModule,
102240
102234
  typesFilename,
102241
- ts: ts9
102235
+ ts: ts9,
102236
+ typesOnly
102242
102237
  };
102243
102238
  src_default.format(import_ejs.default.render(templateFile, data), {
102244
102239
  parser: "typescript",
@@ -102274,18 +102269,14 @@ async function generate(userOptions = {}) {
102274
102269
  const {
102275
102270
  base,
102276
102271
  ts: ts9 = true,
102272
+ typesOnly = false,
102277
102273
  overrides = true,
102278
102274
  preset = "axle",
102279
- statusCodeStrategy = "smart",
102280
102275
  input = "./schema.json",
102281
102276
  output = "./src/apis/generated",
102282
102277
  typesFilename = "_types.ts",
102283
102278
  transformer = {}
102284
102279
  } = options8;
102285
- const statusCodes = {
102286
- ...createStatusCodesByStrategy(statusCodeStrategy),
102287
- ...options8.statusCodes ?? {}
102288
- };
102289
102280
  const mergedTransformer = { ...createTransformer(), ...transformer };
102290
102281
  const schema2 = await readSchema(input);
102291
102282
  import_rslog.logger.info("Generating API modules...");
@@ -102293,12 +102284,10 @@ async function generate(userOptions = {}) {
102293
102284
  await generateTypes(schema2, output, typesFilename);
102294
102285
  }
102295
102286
  const apiModules = partitionApiModules(schema2, {
102296
- statusCodes,
102297
- statusCodeStrategy,
102298
102287
  base,
102299
102288
  transformer: mergedTransformer
102300
102289
  });
102301
- await renderApiModules(apiModules, { output, typesFilename, ts: ts9, overrides, preset });
102290
+ await renderApiModules(apiModules, { output, typesFilename, ts: ts9, typesOnly, overrides, preset });
102302
102291
  import_rslog.logger.success("Done");
102303
102292
  }
102304
102293
 
@@ -102309,11 +102298,11 @@ var import_pluralize2 = __toESM(require("pluralize"), 1);
102309
102298
  createStatusCodesByStrategy,
102310
102299
  createTransformer,
102311
102300
  defineConfig,
102312
- doStatusCodeStrategy,
102313
102301
  generate,
102314
102302
  generateTypes,
102315
102303
  getCliVersion,
102316
102304
  getConfig,
102305
+ getSuccessfulResponseMeme,
102317
102306
  hasQueryParameter,
102318
102307
  isRequiredRequestBody,
102319
102308
  partitionApiModules,
package/dist/index.d.cts CHANGED
@@ -122,7 +122,7 @@ declare function readTemplateFile(preset?: Preset): string;
122
122
  declare function hasQueryParameter(operation: OperationObject): boolean;
123
123
  declare function getCliVersion(): any;
124
124
  declare function isRequiredRequestBody(value: RequestBodyObject | ReferenceObject): boolean;
125
- declare function doStatusCodeStrategy(operation: OperationObject, statusCode: number, strategy: StatusCodeStrategy): {
125
+ declare function getSuccessfulResponseMeme(operation: OperationObject): {
126
126
  statusCode: undefined;
127
127
  mime: undefined;
128
128
  } | {
@@ -143,6 +143,10 @@ interface ApiModuleTemplateData {
143
143
  * Whether to generate ts code
144
144
  */
145
145
  ts: boolean;
146
+ /**
147
+ * Whether to generate only types
148
+ */
149
+ typesOnly: boolean;
146
150
  }
147
151
  interface ApiModule {
148
152
  /**
@@ -233,6 +237,10 @@ interface GenerateOptions {
233
237
  * Whether to generate TypeScript code.
234
238
  */
235
239
  ts?: boolean;
240
+ /**
241
+ * Whether to generate only types.
242
+ */
243
+ typesOnly?: boolean;
236
244
  /**
237
245
  * Whether to override the existing files, or an array of filenames to override.
238
246
  */
@@ -241,43 +249,21 @@ interface GenerateOptions {
241
249
  * The preset ejs template to use.
242
250
  */
243
251
  preset?: Preset;
244
- /**
245
- * The status code strategy to use.
246
- * loose: all success status codes are 200,
247
- * strict: use the openapi recommended success status codes.
248
- * smart: find a valid status code between [200, 299] that is closest to 200
249
- */
250
- statusCodeStrategy?: StatusCodeStrategy;
251
- /**
252
- * The status codes to override the default status codes.
253
- */
254
- statusCodes?: {
255
- get?: number;
256
- post?: number;
257
- put?: number;
258
- delete?: number;
259
- patch?: number;
260
- options?: number;
261
- head?: number;
262
- };
263
252
  }
264
253
  declare function transformPayloads(pathItems: Record<string, OperationObject>, options: {
265
254
  path: string;
266
- statusCodeStrategy: StatusCodeStrategy;
267
- statusCodes: StatusCodes;
268
255
  transformer: Transformer;
269
256
  base: string | undefined;
270
257
  }): ApiModulePayload[];
271
258
  declare function partitionApiModules(schema: OpenAPI3, options: {
272
259
  transformer: Transformer;
273
- statusCodeStrategy: StatusCodeStrategy;
274
- statusCodes: StatusCodes;
275
260
  base: string | undefined;
276
261
  }): ApiModule[];
277
262
  declare function renderApiModules(apiModules: ApiModule[], options: {
278
263
  output: string;
279
264
  typesFilename: string;
280
265
  ts: boolean;
266
+ typesOnly: boolean;
281
267
  overrides: boolean | string[];
282
268
  preset: Preset;
283
269
  }): Promise<unknown[]>;
@@ -288,4 +274,4 @@ type Config = GenerateOptions;
288
274
  declare function defineConfig(config: Config): GenerateOptions;
289
275
  declare function getConfig(): Promise<Config>;
290
276
 
291
- export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type StatusCodeStrategy, type StatusCodes, type Transformer, type TransformerBaseArgs, createStatusCodesByStrategy, createTransformer, defineConfig, doStatusCodeStrategy, generate, generateTypes, getCliVersion, getConfig, hasQueryParameter, isRequiredRequestBody, partitionApiModules, readSchema, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
277
+ export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type StatusCodeStrategy, type StatusCodes, type Transformer, type TransformerBaseArgs, createStatusCodesByStrategy, createTransformer, defineConfig, generate, generateTypes, getCliVersion, getConfig, getSuccessfulResponseMeme, hasQueryParameter, isRequiredRequestBody, partitionApiModules, readSchema, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
package/dist/index.d.ts CHANGED
@@ -122,7 +122,7 @@ declare function readTemplateFile(preset?: Preset): string;
122
122
  declare function hasQueryParameter(operation: OperationObject): boolean;
123
123
  declare function getCliVersion(): any;
124
124
  declare function isRequiredRequestBody(value: RequestBodyObject | ReferenceObject): boolean;
125
- declare function doStatusCodeStrategy(operation: OperationObject, statusCode: number, strategy: StatusCodeStrategy): {
125
+ declare function getSuccessfulResponseMeme(operation: OperationObject): {
126
126
  statusCode: undefined;
127
127
  mime: undefined;
128
128
  } | {
@@ -143,6 +143,10 @@ interface ApiModuleTemplateData {
143
143
  * Whether to generate ts code
144
144
  */
145
145
  ts: boolean;
146
+ /**
147
+ * Whether to generate only types
148
+ */
149
+ typesOnly: boolean;
146
150
  }
147
151
  interface ApiModule {
148
152
  /**
@@ -233,6 +237,10 @@ interface GenerateOptions {
233
237
  * Whether to generate TypeScript code.
234
238
  */
235
239
  ts?: boolean;
240
+ /**
241
+ * Whether to generate only types.
242
+ */
243
+ typesOnly?: boolean;
236
244
  /**
237
245
  * Whether to override the existing files, or an array of filenames to override.
238
246
  */
@@ -241,43 +249,21 @@ interface GenerateOptions {
241
249
  * The preset ejs template to use.
242
250
  */
243
251
  preset?: Preset;
244
- /**
245
- * The status code strategy to use.
246
- * loose: all success status codes are 200,
247
- * strict: use the openapi recommended success status codes.
248
- * smart: find a valid status code between [200, 299] that is closest to 200
249
- */
250
- statusCodeStrategy?: StatusCodeStrategy;
251
- /**
252
- * The status codes to override the default status codes.
253
- */
254
- statusCodes?: {
255
- get?: number;
256
- post?: number;
257
- put?: number;
258
- delete?: number;
259
- patch?: number;
260
- options?: number;
261
- head?: number;
262
- };
263
252
  }
264
253
  declare function transformPayloads(pathItems: Record<string, OperationObject>, options: {
265
254
  path: string;
266
- statusCodeStrategy: StatusCodeStrategy;
267
- statusCodes: StatusCodes;
268
255
  transformer: Transformer;
269
256
  base: string | undefined;
270
257
  }): ApiModulePayload[];
271
258
  declare function partitionApiModules(schema: OpenAPI3, options: {
272
259
  transformer: Transformer;
273
- statusCodeStrategy: StatusCodeStrategy;
274
- statusCodes: StatusCodes;
275
260
  base: string | undefined;
276
261
  }): ApiModule[];
277
262
  declare function renderApiModules(apiModules: ApiModule[], options: {
278
263
  output: string;
279
264
  typesFilename: string;
280
265
  ts: boolean;
266
+ typesOnly: boolean;
281
267
  overrides: boolean | string[];
282
268
  preset: Preset;
283
269
  }): Promise<unknown[]>;
@@ -288,4 +274,4 @@ type Config = GenerateOptions;
288
274
  declare function defineConfig(config: Config): GenerateOptions;
289
275
  declare function getConfig(): Promise<Config>;
290
276
 
291
- export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type StatusCodeStrategy, type StatusCodes, type Transformer, type TransformerBaseArgs, createStatusCodesByStrategy, createTransformer, defineConfig, doStatusCodeStrategy, generate, generateTypes, getCliVersion, getConfig, hasQueryParameter, isRequiredRequestBody, partitionApiModules, readSchema, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
277
+ export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type StatusCodeStrategy, type StatusCodes, type Transformer, type TransformerBaseArgs, createStatusCodesByStrategy, createTransformer, defineConfig, generate, generateTypes, getCliVersion, getConfig, getSuccessfulResponseMeme, hasQueryParameter, isRequiredRequestBody, partitionApiModules, readSchema, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
package/dist/index.js CHANGED
@@ -20,16 +20,16 @@ import {
20
20
  transformTypeValue,
21
21
  transformUrl,
22
22
  transformVerb
23
- } from "./chunk-VAAFC73D.js";
23
+ } from "./chunk-J2G2MBMU.js";
24
24
  import {
25
25
  createStatusCodesByStrategy,
26
- doStatusCodeStrategy,
27
26
  getCliVersion,
27
+ getSuccessfulResponseMeme,
28
28
  hasQueryParameter,
29
29
  isRequiredRequestBody,
30
30
  readSchema,
31
31
  readTemplateFile
32
- } from "./chunk-LYOTF4II.js";
32
+ } from "./chunk-MLHGCJ6R.js";
33
33
  import "./chunk-6OIOYGN7.js";
34
34
 
35
35
  // src/index.ts
@@ -38,11 +38,11 @@ export {
38
38
  createStatusCodesByStrategy,
39
39
  createTransformer,
40
40
  defineConfig,
41
- doStatusCodeStrategy,
42
41
  generate,
43
42
  generateTypes,
44
43
  getCliVersion,
45
44
  getConfig,
45
+ getSuccessfulResponseMeme,
46
46
  hasQueryParameter,
47
47
  isRequiredRequestBody,
48
48
  partitionApiModules,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-farmer",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "description": "API module generation tool based on Openapi3/Swagger2.",
5
5
  "keywords": [
6
6
  "cli",
@@ -1,16 +1,18 @@
1
- <% if (ts) { %> import { type AxiosRequestConfig } from 'axios' <% } %>
2
- import { request } from '@/request'
1
+ <% if (ts && !typesOnly) { %> import { type AxiosRequestConfig } from 'axios' <% } %>
2
+ <% if (!typesOnly) { %> import { request } from '@/request' <% } %>
3
3
  <% if (ts) { %> import { type paths } from './<%- typesFilename %>' <% } %>
4
4
 
5
+ <% if (!typesOnly) { %>
5
6
  <% apiModule.payloads.forEach(payload => { %> -%>
6
7
  export const <%- payload.fn %> = (config<% if (ts) { %>: AxiosRequestConfig<<%- payload.typeRequestBody %>> <% } %>)
7
- => request<% if (ts) { %><any, Res<<%- payload.typeResponseBody %>>><% } %>({
8
+ => request<% if (ts) { %><any, <%- payload.typeResponseBody %>><% } %>({
8
9
  url: '<%- payload.url %>',
9
10
  method: '<%- payload.method %>',
10
11
  ...config
11
12
  })
12
13
 
13
14
  <% }) %>
15
+ <% } %>
14
16
 
15
17
  <% if (ts) { %>
16
18
  <% apiModule.payloads.forEach(payload => { %> -%>
@@ -1,10 +1,11 @@
1
- import { api } from '@/request'
1
+ <% if (!typesOnly) { %> import { api } from '@/request' <% } %>
2
2
  <% if (ts) { %> import { type paths } from './<%- typesFilename %>' <% } %>
3
3
 
4
+ <% if (!typesOnly) { %>
4
5
  <% apiModule.payloads.forEach(payload => { %> -%>
5
6
  export const <%- payload.fn %> = api
6
7
  <% if (ts) { %>
7
- <Res<<%- payload.typeResponseBody %>>,
8
+ <<%- payload.typeResponseBody %>,
8
9
  <% if (['get', 'delete', 'head', 'options'].includes(payload.method)) { %>
9
10
  <%- payload.typeQuery %>
10
11
  <% } else { %>
@@ -14,6 +15,7 @@ export const <%- payload.fn %> = api
14
15
  ('<%- payload.url %>', '<%- payload.method %>')
15
16
 
16
17
  <% }) %>
18
+ <% } %>
17
19
 
18
20
  <% if (ts) { %>
19
21
  <% apiModule.payloads.forEach(payload => { %> -%>