api-farmer 0.0.8 → 0.0.10

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
@@ -69,15 +69,17 @@ See the bottom of the document for template variable definitions.
69
69
 
70
70
  ### Status Code Strategy
71
71
 
72
- `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
+ `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>
73
75
 
74
76
  ```ts
75
77
  // api-farmer.config.ts
76
78
  import { defineConfig } from 'api-farmer'
77
79
 
78
80
  export default defineConfig({
79
- // 'strict' or 'loose', defaults 'strict'
80
- statusCodeStrategy: 'loose',
81
+ // 'strict' or 'loose' or 'smart', defaults 'smart'
82
+ statusCodeStrategy: 'strict',
81
83
  })
82
84
  ```
83
85
 
@@ -149,7 +151,9 @@ export interface Config {
149
151
  preset?: Preset
150
152
  /**
151
153
  * The status code strategy to use.
152
- * loose: all success status codes are 200, strict: use the openapi recommended success status codes.
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
153
157
  */
154
158
  statusCodeStrategy?: StatusCodeStrategy
155
159
  /**
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  CWD,
3
3
  createStatusCodesByStrategy,
4
- getResponseMime,
4
+ doStatusCodeStrategy,
5
5
  hasQueryParameter,
6
6
  isRequiredRequestBody,
7
7
  readSchema,
8
8
  readTemplateFile
9
- } from "./chunk-OBPECFFY.js";
9
+ } from "./chunk-THTMWQYI.js";
10
10
  import {
11
11
  __export
12
12
  } from "./chunk-6OIOYGN7.js";
@@ -22776,11 +22776,8 @@ function transformType({ verb, entity }) {
22776
22776
  function transformTypeValue({ path: path13, method }) {
22777
22777
  return `paths['${path13}']['${method}']`;
22778
22778
  }
22779
- function transformTypeQuery({
22780
- verb,
22781
- entity
22782
- }) {
22783
- return `Api${verb}${entity}Query`;
22779
+ function transformTypeQuery({ type: type2 }) {
22780
+ return `${type2}Query`;
22784
22781
  }
22785
22782
  function transformTypeQueryValue({
22786
22783
  type: type2
@@ -22788,10 +22785,9 @@ function transformTypeQueryValue({
22788
22785
  return `${type2}['parameters']['query']`;
22789
22786
  }
22790
22787
  function transformTypeRequestBody({
22791
- verb,
22792
- entity
22788
+ type: type2
22793
22789
  }) {
22794
- return `Api${verb}${entity}RequestBody`;
22790
+ return `${type2}RequestBody`;
22795
22791
  }
22796
22792
  function transformTypeRequestBodyValue({
22797
22793
  type: type2,
@@ -22800,10 +22796,9 @@ function transformTypeRequestBodyValue({
22800
22796
  return required ? `${type2}['requestBody']['content']['application/json']` : `NonNullable<${type2}['requestBody']>['content']['application/json'] | undefined`;
22801
22797
  }
22802
22798
  function transformTypeResponseBody({
22803
- verb,
22804
- entity
22799
+ type: type2
22805
22800
  }) {
22806
- return `Api${verb}${entity}ResponseBody`;
22801
+ return `${type2}ResponseBody`;
22807
22802
  }
22808
22803
  function transformTypeResponseBodyValue({
22809
22804
  type: type2,
@@ -22832,7 +22827,7 @@ function createTransformer() {
22832
22827
 
22833
22828
  // src/generate.ts
22834
22829
  function partitionApiModules(schema2, transformer, options8) {
22835
- const { statusCodes, base } = options8;
22830
+ const { statusCodes, statusCodeStrategy, base } = options8;
22836
22831
  const schemaPaths = schema2.paths ?? {};
22837
22832
  const schemaPathKeys = base ? Object.keys(schemaPaths).map((key2) => key2.replace(base, "")) : Object.keys(schemaPaths);
22838
22833
  const keyToPaths = groupBy(schemaPathKeys, (key2) => key2.split("/")[1]);
@@ -22858,10 +22853,13 @@ function partitionApiModules(schema2, transformer, options8) {
22858
22853
  entity,
22859
22854
  required: isRequiredRequestBody(operation.requestBody)
22860
22855
  }) : "never";
22861
- const statusCode = statusCodes[method] ?? 200;
22862
- const mime = getResponseMime(operation, statusCode);
22856
+ const { mime, statusCode } = doStatusCodeStrategy(
22857
+ operation,
22858
+ statusCodes[method] ?? 200,
22859
+ statusCodeStrategy
22860
+ );
22863
22861
  const typeResponseBody = transformer.typeResponseBody({ ...args, type: type2, verb, entity });
22864
- const typeResponseBodyValue = mime ? transformer.typeResponseBodyValue({ ...args, type: type2, verb, entity, statusCode, mime }) : "never";
22862
+ const typeResponseBodyValue = mime && statusCode ? transformer.typeResponseBodyValue({ ...args, type: type2, verb, entity, statusCode, mime }) : "never";
22865
22863
  payloads3.push({
22866
22864
  fn,
22867
22865
  url: url2,
@@ -22935,7 +22933,7 @@ async function generate(userOptions = {}) {
22935
22933
  ts = true,
22936
22934
  overrides = true,
22937
22935
  preset = "axle",
22938
- statusCodeStrategy = "strict",
22936
+ statusCodeStrategy = "smart",
22939
22937
  input = "./schema.json",
22940
22938
  output = "./src/apis",
22941
22939
  typesFilename = "types.generated.ts",
@@ -22951,7 +22949,7 @@ async function generate(userOptions = {}) {
22951
22949
  if (ts) {
22952
22950
  await generateTypes(schema2, output, typesFilename);
22953
22951
  }
22954
- const apiModules = partitionApiModules(schema2, mergedTransformer, { statusCodes, ts, base });
22952
+ const apiModules = partitionApiModules(schema2, mergedTransformer, { statusCodes, statusCodeStrategy, ts, base });
22955
22953
  await renderApiModules(apiModules, { output, typesFilename, ts, overrides, preset });
22956
22954
  logger.success("Done");
22957
22955
  }
@@ -32,6 +32,15 @@ function createStatusCodesByStrategy(strategy) {
32
32
  patch: 200,
33
33
  options: 200,
34
34
  head: 200
35
+ },
36
+ smart: {
37
+ get: 200,
38
+ post: 200,
39
+ put: 200,
40
+ delete: 200,
41
+ patch: 200,
42
+ options: 200,
43
+ head: 200
35
44
  }
36
45
  }[strategy];
37
46
  }
@@ -58,9 +67,24 @@ function getCliVersion() {
58
67
  function isRequiredRequestBody(value) {
59
68
  return "required" in value && value.required === true;
60
69
  }
61
- function getResponseMime(operation, statusCode) {
70
+ function doStatusCodeStrategy(operation, statusCode, strategy) {
71
+ if (strategy === "smart") {
72
+ const responses = operation.responses ?? {};
73
+ const codeKey = Object.keys(responses).sort((a, b) => Number(a) - Number(b)).find((codeKey2) => Number(codeKey2) >= statusCode && Number(codeKey2) <= 299);
74
+ if (!codeKey) {
75
+ return {
76
+ statusCode: void 0,
77
+ mime: void 0
78
+ };
79
+ }
80
+ statusCode = Number(codeKey);
81
+ }
62
82
  const content = operation.responses?.[statusCode]?.content;
63
- return content?.["application/json"] ? "application/json" : content?.["*/*"] ? "*/*" : void 0;
83
+ const mime = content?.["application/json"] ? "application/json" : content?.["*/*"] ? "*/*" : void 0;
84
+ return {
85
+ statusCode,
86
+ mime
87
+ };
64
88
  }
65
89
 
66
90
  export {
@@ -71,5 +95,5 @@ export {
71
95
  hasQueryParameter,
72
96
  getCliVersion,
73
97
  isRequiredRequestBody,
74
- getResponseMime
98
+ doStatusCodeStrategy
75
99
  };
package/dist/cli.cjs CHANGED
@@ -73,6 +73,15 @@ function createStatusCodesByStrategy(strategy) {
73
73
  patch: 200,
74
74
  options: 200,
75
75
  head: 200
76
+ },
77
+ smart: {
78
+ get: 200,
79
+ post: 200,
80
+ put: 200,
81
+ delete: 200,
82
+ patch: 200,
83
+ options: 200,
84
+ head: 200
76
85
  }
77
86
  }[strategy];
78
87
  }
@@ -99,9 +108,24 @@ function getCliVersion() {
99
108
  function isRequiredRequestBody(value) {
100
109
  return "required" in value && value.required === true;
101
110
  }
102
- function getResponseMime(operation, statusCode) {
111
+ function doStatusCodeStrategy(operation, statusCode, strategy) {
112
+ if (strategy === "smart") {
113
+ const responses = operation.responses ?? {};
114
+ const codeKey = Object.keys(responses).sort((a5, b8) => Number(a5) - Number(b8)).find((codeKey2) => Number(codeKey2) >= statusCode && Number(codeKey2) <= 299);
115
+ if (!codeKey) {
116
+ return {
117
+ statusCode: void 0,
118
+ mime: void 0
119
+ };
120
+ }
121
+ statusCode = Number(codeKey);
122
+ }
103
123
  const content = operation.responses?.[statusCode]?.content;
104
- return content?.["application/json"] ? "application/json" : content?.["*/*"] ? "*/*" : void 0;
124
+ const mime = content?.["application/json"] ? "application/json" : content?.["*/*"] ? "*/*" : void 0;
125
+ return {
126
+ statusCode,
127
+ mime
128
+ };
105
129
  }
106
130
  var import_path2, import_fs_extra, import_swagger2openapi, import_yaml;
107
131
  var init_utils = __esm({
@@ -102036,11 +102060,8 @@ function transformType({ verb, entity }) {
102036
102060
  function transformTypeValue({ path: path13, method }) {
102037
102061
  return `paths['${path13}']['${method}']`;
102038
102062
  }
102039
- function transformTypeQuery({
102040
- verb,
102041
- entity
102042
- }) {
102043
- return `Api${verb}${entity}Query`;
102063
+ function transformTypeQuery({ type: type2 }) {
102064
+ return `${type2}Query`;
102044
102065
  }
102045
102066
  function transformTypeQueryValue({
102046
102067
  type: type2
@@ -102048,10 +102069,9 @@ function transformTypeQueryValue({
102048
102069
  return `${type2}['parameters']['query']`;
102049
102070
  }
102050
102071
  function transformTypeRequestBody({
102051
- verb,
102052
- entity
102072
+ type: type2
102053
102073
  }) {
102054
- return `Api${verb}${entity}RequestBody`;
102074
+ return `${type2}RequestBody`;
102055
102075
  }
102056
102076
  function transformTypeRequestBodyValue({
102057
102077
  type: type2,
@@ -102060,10 +102080,9 @@ function transformTypeRequestBodyValue({
102060
102080
  return required ? `${type2}['requestBody']['content']['application/json']` : `NonNullable<${type2}['requestBody']>['content']['application/json'] | undefined`;
102061
102081
  }
102062
102082
  function transformTypeResponseBody({
102063
- verb,
102064
- entity
102083
+ type: type2
102065
102084
  }) {
102066
- return `Api${verb}${entity}ResponseBody`;
102085
+ return `${type2}ResponseBody`;
102067
102086
  }
102068
102087
  function transformTypeResponseBodyValue({
102069
102088
  type: type2,
@@ -102108,7 +102127,7 @@ __export(generate_exports, {
102108
102127
  renderApiModules: () => renderApiModules
102109
102128
  });
102110
102129
  function partitionApiModules(schema2, transformer, options8) {
102111
- const { statusCodes, base } = options8;
102130
+ const { statusCodes, statusCodeStrategy, base } = options8;
102112
102131
  const schemaPaths = schema2.paths ?? {};
102113
102132
  const schemaPathKeys = base ? Object.keys(schemaPaths).map((key2) => key2.replace(base, "")) : Object.keys(schemaPaths);
102114
102133
  const keyToPaths = (0, import_rattail2.groupBy)(schemaPathKeys, (key2) => key2.split("/")[1]);
@@ -102134,10 +102153,13 @@ function partitionApiModules(schema2, transformer, options8) {
102134
102153
  entity,
102135
102154
  required: isRequiredRequestBody(operation.requestBody)
102136
102155
  }) : "never";
102137
- const statusCode = statusCodes[method] ?? 200;
102138
- const mime = getResponseMime(operation, statusCode);
102156
+ const { mime, statusCode } = doStatusCodeStrategy(
102157
+ operation,
102158
+ statusCodes[method] ?? 200,
102159
+ statusCodeStrategy
102160
+ );
102139
102161
  const typeResponseBody = transformer.typeResponseBody({ ...args, type: type2, verb, entity });
102140
- const typeResponseBodyValue = mime ? transformer.typeResponseBodyValue({ ...args, type: type2, verb, entity, statusCode, mime }) : "never";
102162
+ const typeResponseBodyValue = mime && statusCode ? transformer.typeResponseBodyValue({ ...args, type: type2, verb, entity, statusCode, mime }) : "never";
102141
102163
  payloads3.push({
102142
102164
  fn: fn8,
102143
102165
  url: url2,
@@ -102211,7 +102233,7 @@ async function generate(userOptions = {}) {
102211
102233
  ts: ts9 = true,
102212
102234
  overrides = true,
102213
102235
  preset = "axle",
102214
- statusCodeStrategy = "strict",
102236
+ statusCodeStrategy = "smart",
102215
102237
  input = "./schema.json",
102216
102238
  output = "./src/apis",
102217
102239
  typesFilename = "types.generated.ts",
@@ -102227,7 +102249,7 @@ async function generate(userOptions = {}) {
102227
102249
  if (ts9) {
102228
102250
  await generateTypes(schema2, output, typesFilename);
102229
102251
  }
102230
- const apiModules = partitionApiModules(schema2, mergedTransformer, { statusCodes, ts: ts9, base });
102252
+ const apiModules = partitionApiModules(schema2, mergedTransformer, { statusCodes, statusCodeStrategy, ts: ts9, base });
102231
102253
  await renderApiModules(apiModules, { output, typesFilename, ts: ts9, overrides, preset });
102232
102254
  import_rslog.logger.success("Done");
102233
102255
  }
package/dist/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  getCliVersion
4
- } from "./chunk-OBPECFFY.js";
4
+ } from "./chunk-THTMWQYI.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-C3K2GRQJ.js");
12
+ const { generate } = await import("./generate-A4HBYHRZ.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-L33J46FJ.js";
7
- import "./chunk-OBPECFFY.js";
6
+ } from "./chunk-I24WWW5F.js";
7
+ import "./chunk-THTMWQYI.js";
8
8
  import "./chunk-6OIOYGN7.js";
9
9
  export {
10
10
  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,
79231
79232
  generate: () => generate,
79232
79233
  generateTypes: () => generateTypes,
79233
79234
  getCliVersion: () => getCliVersion,
79234
79235
  getConfig: () => getConfig,
79235
- getResponseMime: () => getResponseMime,
79236
79236
  hasQueryParameter: () => hasQueryParameter,
79237
79237
  isRequiredRequestBody: () => isRequiredRequestBody,
79238
79238
  partitionApiModules: () => partitionApiModules,
@@ -79300,11 +79300,8 @@ function transformType({ verb, entity }) {
79300
79300
  function transformTypeValue({ path: path13, method }) {
79301
79301
  return `paths['${path13}']['${method}']`;
79302
79302
  }
79303
- function transformTypeQuery({
79304
- verb,
79305
- entity
79306
- }) {
79307
- return `Api${verb}${entity}Query`;
79303
+ function transformTypeQuery({ type: type2 }) {
79304
+ return `${type2}Query`;
79308
79305
  }
79309
79306
  function transformTypeQueryValue({
79310
79307
  type: type2
@@ -79312,10 +79309,9 @@ function transformTypeQueryValue({
79312
79309
  return `${type2}['parameters']['query']`;
79313
79310
  }
79314
79311
  function transformTypeRequestBody({
79315
- verb,
79316
- entity
79312
+ type: type2
79317
79313
  }) {
79318
- return `Api${verb}${entity}RequestBody`;
79314
+ return `${type2}RequestBody`;
79319
79315
  }
79320
79316
  function transformTypeRequestBodyValue({
79321
79317
  type: type2,
@@ -79324,10 +79320,9 @@ function transformTypeRequestBodyValue({
79324
79320
  return required ? `${type2}['requestBody']['content']['application/json']` : `NonNullable<${type2}['requestBody']>['content']['application/json'] | undefined`;
79325
79321
  }
79326
79322
  function transformTypeResponseBody({
79327
- verb,
79328
- entity
79323
+ type: type2
79329
79324
  }) {
79330
- return `Api${verb}${entity}ResponseBody`;
79325
+ return `${type2}ResponseBody`;
79331
79326
  }
79332
79327
  function transformTypeResponseBodyValue({
79333
79328
  type: type2,
@@ -102113,6 +102108,15 @@ function createStatusCodesByStrategy(strategy) {
102113
102108
  patch: 200,
102114
102109
  options: 200,
102115
102110
  head: 200
102111
+ },
102112
+ smart: {
102113
+ get: 200,
102114
+ post: 200,
102115
+ put: 200,
102116
+ delete: 200,
102117
+ patch: 200,
102118
+ options: 200,
102119
+ head: 200
102116
102120
  }
102117
102121
  }[strategy];
102118
102122
  }
@@ -102139,14 +102143,29 @@ function getCliVersion() {
102139
102143
  function isRequiredRequestBody(value) {
102140
102144
  return "required" in value && value.required === true;
102141
102145
  }
102142
- function getResponseMime(operation, statusCode) {
102146
+ function doStatusCodeStrategy(operation, statusCode, strategy) {
102147
+ if (strategy === "smart") {
102148
+ const responses = operation.responses ?? {};
102149
+ const codeKey = Object.keys(responses).sort((a5, b8) => Number(a5) - Number(b8)).find((codeKey2) => Number(codeKey2) >= statusCode && Number(codeKey2) <= 299);
102150
+ if (!codeKey) {
102151
+ return {
102152
+ statusCode: void 0,
102153
+ mime: void 0
102154
+ };
102155
+ }
102156
+ statusCode = Number(codeKey);
102157
+ }
102143
102158
  const content = operation.responses?.[statusCode]?.content;
102144
- return content?.["application/json"] ? "application/json" : content?.["*/*"] ? "*/*" : void 0;
102159
+ const mime = content?.["application/json"] ? "application/json" : content?.["*/*"] ? "*/*" : void 0;
102160
+ return {
102161
+ statusCode,
102162
+ mime
102163
+ };
102145
102164
  }
102146
102165
 
102147
102166
  // src/generate.ts
102148
102167
  function partitionApiModules(schema2, transformer, options8) {
102149
- const { statusCodes, base } = options8;
102168
+ const { statusCodes, statusCodeStrategy, base } = options8;
102150
102169
  const schemaPaths = schema2.paths ?? {};
102151
102170
  const schemaPathKeys = base ? Object.keys(schemaPaths).map((key2) => key2.replace(base, "")) : Object.keys(schemaPaths);
102152
102171
  const keyToPaths = (0, import_rattail2.groupBy)(schemaPathKeys, (key2) => key2.split("/")[1]);
@@ -102172,10 +102191,13 @@ function partitionApiModules(schema2, transformer, options8) {
102172
102191
  entity,
102173
102192
  required: isRequiredRequestBody(operation.requestBody)
102174
102193
  }) : "never";
102175
- const statusCode = statusCodes[method] ?? 200;
102176
- const mime = getResponseMime(operation, statusCode);
102194
+ const { mime, statusCode } = doStatusCodeStrategy(
102195
+ operation,
102196
+ statusCodes[method] ?? 200,
102197
+ statusCodeStrategy
102198
+ );
102177
102199
  const typeResponseBody = transformer.typeResponseBody({ ...args, type: type2, verb, entity });
102178
- const typeResponseBodyValue = mime ? transformer.typeResponseBodyValue({ ...args, type: type2, verb, entity, statusCode, mime }) : "never";
102200
+ const typeResponseBodyValue = mime && statusCode ? transformer.typeResponseBodyValue({ ...args, type: type2, verb, entity, statusCode, mime }) : "never";
102179
102201
  payloads3.push({
102180
102202
  fn: fn8,
102181
102203
  url: url2,
@@ -102249,7 +102271,7 @@ async function generate(userOptions = {}) {
102249
102271
  ts: ts9 = true,
102250
102272
  overrides = true,
102251
102273
  preset = "axle",
102252
- statusCodeStrategy = "strict",
102274
+ statusCodeStrategy = "smart",
102253
102275
  input = "./schema.json",
102254
102276
  output = "./src/apis",
102255
102277
  typesFilename = "types.generated.ts",
@@ -102265,7 +102287,7 @@ async function generate(userOptions = {}) {
102265
102287
  if (ts9) {
102266
102288
  await generateTypes(schema2, output, typesFilename);
102267
102289
  }
102268
- const apiModules = partitionApiModules(schema2, mergedTransformer, { statusCodes, ts: ts9, base });
102290
+ const apiModules = partitionApiModules(schema2, mergedTransformer, { statusCodes, statusCodeStrategy, ts: ts9, base });
102269
102291
  await renderApiModules(apiModules, { output, typesFilename, ts: ts9, overrides, preset });
102270
102292
  import_rslog.logger.success("Done");
102271
102293
  }
@@ -102277,11 +102299,11 @@ var import_pluralize2 = __toESM(require("pluralize"), 1);
102277
102299
  createStatusCodesByStrategy,
102278
102300
  createTransformer,
102279
102301
  defineConfig,
102302
+ doStatusCodeStrategy,
102280
102303
  generate,
102281
102304
  generateTypes,
102282
102305
  getCliVersion,
102283
102306
  getConfig,
102284
- getResponseMime,
102285
102307
  hasQueryParameter,
102286
102308
  isRequiredRequestBody,
102287
102309
  partitionApiModules,
package/dist/index.d.cts CHANGED
@@ -31,7 +31,7 @@ declare function transformTypeValue({ path, method }: {
31
31
  verb: string;
32
32
  entity: string;
33
33
  } & TransformerBaseArgs): string;
34
- declare function transformTypeQuery({ verb, entity, }: {
34
+ declare function transformTypeQuery({ type }: {
35
35
  type: string;
36
36
  verb: string;
37
37
  entity: string;
@@ -41,7 +41,7 @@ declare function transformTypeQueryValue({ type, }: {
41
41
  verb: string;
42
42
  entity: string;
43
43
  } & TransformerBaseArgs): string;
44
- declare function transformTypeRequestBody({ verb, entity, }: {
44
+ declare function transformTypeRequestBody({ type, }: {
45
45
  type: string;
46
46
  verb: string;
47
47
  entity: string;
@@ -52,7 +52,7 @@ declare function transformTypeRequestBodyValue({ type, required, }: {
52
52
  entity: string;
53
53
  required: boolean;
54
54
  } & TransformerBaseArgs): string;
55
- declare function transformTypeResponseBody({ verb, entity, }: {
55
+ declare function transformTypeResponseBody({ type, }: {
56
56
  type: string;
57
57
  verb: string;
58
58
  entity: string;
@@ -82,7 +82,7 @@ interface Transformer {
82
82
  declare function createTransformer(): Transformer;
83
83
 
84
84
  type Preset = 'axle' | 'axios';
85
- type StatusCodeStrategy = 'strict' | 'loose';
85
+ type StatusCodeStrategy = 'strict' | 'loose' | 'smart';
86
86
  interface StatusCodes {
87
87
  get?: number;
88
88
  post?: number;
@@ -108,13 +108,27 @@ declare function createStatusCodesByStrategy(strategy: StatusCodeStrategy): {
108
108
  patch: number;
109
109
  options: number;
110
110
  head: number;
111
+ } | {
112
+ get: number;
113
+ post: number;
114
+ put: number;
115
+ delete: number;
116
+ patch: number;
117
+ options: number;
118
+ head: number;
111
119
  };
112
120
  declare function readSchema(input: string): Promise<OpenAPI3>;
113
121
  declare function readTemplateFile(preset?: Preset): string;
114
122
  declare function hasQueryParameter(operation: OperationObject): boolean;
115
123
  declare function getCliVersion(): any;
116
124
  declare function isRequiredRequestBody(value: RequestBodyObject | ReferenceObject): boolean;
117
- declare function getResponseMime(operation: OperationObject, statusCode: number): "application/json" | "*/*" | undefined;
125
+ declare function doStatusCodeStrategy(operation: OperationObject, statusCode: number, strategy: StatusCodeStrategy): {
126
+ statusCode: undefined;
127
+ mime: undefined;
128
+ } | {
129
+ statusCode: number;
130
+ mime: string | undefined;
131
+ };
118
132
 
119
133
  interface ApiModuleTemplateData {
120
134
  /**
@@ -228,7 +242,10 @@ interface GenerateOptions {
228
242
  */
229
243
  preset?: Preset;
230
244
  /**
231
- * The status code strategy to use. loose: all success status codes are 200, strict: use the openapi recommended success status codes.
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
232
249
  */
233
250
  statusCodeStrategy?: StatusCodeStrategy;
234
251
  /**
@@ -246,6 +263,7 @@ interface GenerateOptions {
246
263
  }
247
264
  declare function partitionApiModules(schema: OpenAPI3, transformer: Transformer, options: {
248
265
  ts: boolean;
266
+ statusCodeStrategy: StatusCodeStrategy;
249
267
  statusCodes: StatusCodes;
250
268
  base?: string;
251
269
  }): ApiModule[];
@@ -263,4 +281,4 @@ type Config = GenerateOptions;
263
281
  declare function defineConfig(config: Config): GenerateOptions;
264
282
  declare function getConfig(): Promise<Config>;
265
283
 
266
- 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, getResponseMime, hasQueryParameter, isRequiredRequestBody, partitionApiModules, readSchema, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
284
+ 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, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
package/dist/index.d.ts CHANGED
@@ -31,7 +31,7 @@ declare function transformTypeValue({ path, method }: {
31
31
  verb: string;
32
32
  entity: string;
33
33
  } & TransformerBaseArgs): string;
34
- declare function transformTypeQuery({ verb, entity, }: {
34
+ declare function transformTypeQuery({ type }: {
35
35
  type: string;
36
36
  verb: string;
37
37
  entity: string;
@@ -41,7 +41,7 @@ declare function transformTypeQueryValue({ type, }: {
41
41
  verb: string;
42
42
  entity: string;
43
43
  } & TransformerBaseArgs): string;
44
- declare function transformTypeRequestBody({ verb, entity, }: {
44
+ declare function transformTypeRequestBody({ type, }: {
45
45
  type: string;
46
46
  verb: string;
47
47
  entity: string;
@@ -52,7 +52,7 @@ declare function transformTypeRequestBodyValue({ type, required, }: {
52
52
  entity: string;
53
53
  required: boolean;
54
54
  } & TransformerBaseArgs): string;
55
- declare function transformTypeResponseBody({ verb, entity, }: {
55
+ declare function transformTypeResponseBody({ type, }: {
56
56
  type: string;
57
57
  verb: string;
58
58
  entity: string;
@@ -82,7 +82,7 @@ interface Transformer {
82
82
  declare function createTransformer(): Transformer;
83
83
 
84
84
  type Preset = 'axle' | 'axios';
85
- type StatusCodeStrategy = 'strict' | 'loose';
85
+ type StatusCodeStrategy = 'strict' | 'loose' | 'smart';
86
86
  interface StatusCodes {
87
87
  get?: number;
88
88
  post?: number;
@@ -108,13 +108,27 @@ declare function createStatusCodesByStrategy(strategy: StatusCodeStrategy): {
108
108
  patch: number;
109
109
  options: number;
110
110
  head: number;
111
+ } | {
112
+ get: number;
113
+ post: number;
114
+ put: number;
115
+ delete: number;
116
+ patch: number;
117
+ options: number;
118
+ head: number;
111
119
  };
112
120
  declare function readSchema(input: string): Promise<OpenAPI3>;
113
121
  declare function readTemplateFile(preset?: Preset): string;
114
122
  declare function hasQueryParameter(operation: OperationObject): boolean;
115
123
  declare function getCliVersion(): any;
116
124
  declare function isRequiredRequestBody(value: RequestBodyObject | ReferenceObject): boolean;
117
- declare function getResponseMime(operation: OperationObject, statusCode: number): "application/json" | "*/*" | undefined;
125
+ declare function doStatusCodeStrategy(operation: OperationObject, statusCode: number, strategy: StatusCodeStrategy): {
126
+ statusCode: undefined;
127
+ mime: undefined;
128
+ } | {
129
+ statusCode: number;
130
+ mime: string | undefined;
131
+ };
118
132
 
119
133
  interface ApiModuleTemplateData {
120
134
  /**
@@ -228,7 +242,10 @@ interface GenerateOptions {
228
242
  */
229
243
  preset?: Preset;
230
244
  /**
231
- * The status code strategy to use. loose: all success status codes are 200, strict: use the openapi recommended success status codes.
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
232
249
  */
233
250
  statusCodeStrategy?: StatusCodeStrategy;
234
251
  /**
@@ -246,6 +263,7 @@ interface GenerateOptions {
246
263
  }
247
264
  declare function partitionApiModules(schema: OpenAPI3, transformer: Transformer, options: {
248
265
  ts: boolean;
266
+ statusCodeStrategy: StatusCodeStrategy;
249
267
  statusCodes: StatusCodes;
250
268
  base?: string;
251
269
  }): ApiModule[];
@@ -263,4 +281,4 @@ type Config = GenerateOptions;
263
281
  declare function defineConfig(config: Config): GenerateOptions;
264
282
  declare function getConfig(): Promise<Config>;
265
283
 
266
- 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, getResponseMime, hasQueryParameter, isRequiredRequestBody, partitionApiModules, readSchema, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
284
+ 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, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
package/dist/index.js CHANGED
@@ -19,16 +19,16 @@ import {
19
19
  transformTypeValue,
20
20
  transformUrl,
21
21
  transformVerb
22
- } from "./chunk-L33J46FJ.js";
22
+ } from "./chunk-I24WWW5F.js";
23
23
  import {
24
24
  createStatusCodesByStrategy,
25
+ doStatusCodeStrategy,
25
26
  getCliVersion,
26
- getResponseMime,
27
27
  hasQueryParameter,
28
28
  isRequiredRequestBody,
29
29
  readSchema,
30
30
  readTemplateFile
31
- } from "./chunk-OBPECFFY.js";
31
+ } from "./chunk-THTMWQYI.js";
32
32
  import "./chunk-6OIOYGN7.js";
33
33
 
34
34
  // src/index.ts
@@ -37,11 +37,11 @@ export {
37
37
  createStatusCodesByStrategy,
38
38
  createTransformer,
39
39
  defineConfig,
40
+ doStatusCodeStrategy,
40
41
  generate,
41
42
  generateTypes,
42
43
  getCliVersion,
43
44
  getConfig,
44
- getResponseMime,
45
45
  hasQueryParameter,
46
46
  isRequiredRequestBody,
47
47
  partitionApiModules,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-farmer",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "API module generation tool based on Openapi3/Swagger2.",
5
5
  "keywords": [
6
6
  "cli",