api-farmer 0.0.22 → 0.0.23

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.
@@ -7,7 +7,7 @@ import {
7
7
  isRequiredRequestBody,
8
8
  readSchema,
9
9
  readTemplateFile
10
- } from "./chunk-PTGO2Y5X.js";
10
+ } from "./chunk-KEUG7T6E.js";
11
11
 
12
12
  // src/generate.ts
13
13
  import { resolve } from "path";
@@ -90,26 +90,26 @@ function getCliVersion() {
90
90
  function isRequiredRequestBody(value) {
91
91
  return "required" in value && value.required === true;
92
92
  }
93
+ function findObjectKey(object, targetKeys) {
94
+ return Object.keys(object).find((key) => targetKeys.includes(key));
95
+ }
93
96
  function getRequestBodyContentType(value) {
94
97
  if (!("content" in value)) {
95
98
  return "";
96
99
  }
97
- return value.content["application/json"] ? "application/json" : value.content["application/x-www-form-urlencoded"] ? "application/x-www-form-urlencoded" : void 0;
100
+ return findObjectKey(value.content, ["application/json", "application/x-www-form-urlencoded", "multipart/form-data"]);
98
101
  }
99
102
  function getResponseMetadataItems(operation, validateStatus) {
100
103
  const responses = operation.responses ?? {};
101
104
  const validStatusResults = Object.keys(responses).sort((a, b) => Number(a) - Number(b)).filter((key) => validateStatus(Number(key))).map(Number);
102
105
  const metadataItems = validStatusResults.map((status) => {
103
106
  const content = operation.responses?.[status]?.content ?? {};
104
- const responseContentType = findResponseContentType(content);
107
+ const responseContentType = findObjectKey(content, ["application/json", "*/*"]);
105
108
  return {
106
109
  status,
107
110
  responseContentType
108
111
  };
109
112
  }).filter((result) => result.responseContentType);
110
- function findResponseContentType(content) {
111
- return content["application/json"] ? "application/json" : content["*/*"] ? "*/*" : void 0;
112
- }
113
113
  return metadataItems;
114
114
  }
115
115
 
@@ -124,6 +124,7 @@ export {
124
124
  hasQueryParameter,
125
125
  getCliVersion,
126
126
  isRequiredRequestBody,
127
+ findObjectKey,
127
128
  getRequestBodyContentType,
128
129
  getResponseMetadataItems
129
130
  };
package/dist/cli.cjs CHANGED
@@ -91,26 +91,26 @@ function getCliVersion() {
91
91
  function isRequiredRequestBody(value) {
92
92
  return "required" in value && value.required === true;
93
93
  }
94
+ function findObjectKey(object, targetKeys) {
95
+ return Object.keys(object).find((key) => targetKeys.includes(key));
96
+ }
94
97
  function getRequestBodyContentType(value) {
95
98
  if (!("content" in value)) {
96
99
  return "";
97
100
  }
98
- return value.content["application/json"] ? "application/json" : value.content["application/x-www-form-urlencoded"] ? "application/x-www-form-urlencoded" : void 0;
101
+ return findObjectKey(value.content, ["application/json", "application/x-www-form-urlencoded", "multipart/form-data"]);
99
102
  }
100
103
  function getResponseMetadataItems(operation, validateStatus) {
101
104
  const responses = operation.responses ?? {};
102
105
  const validStatusResults = Object.keys(responses).sort((a, b) => Number(a) - Number(b)).filter((key) => validateStatus(Number(key))).map(Number);
103
106
  const metadataItems = validStatusResults.map((status) => {
104
107
  const content = operation.responses?.[status]?.content ?? {};
105
- const responseContentType = findResponseContentType(content);
108
+ const responseContentType = findObjectKey(content, ["application/json", "*/*"]);
106
109
  return {
107
110
  status,
108
111
  responseContentType
109
112
  };
110
113
  }).filter((result) => result.responseContentType);
111
- function findResponseContentType(content) {
112
- return content["application/json"] ? "application/json" : content["*/*"] ? "*/*" : void 0;
113
- }
114
114
  return metadataItems;
115
115
  }
116
116
  var import_path2, import_axle, import_fs_extra, import_rattail, import_rslog, import_swagger2openapi, import_yaml;
package/dist/cli.js CHANGED
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  getCliVersion
4
- } from "./chunk-PTGO2Y5X.js";
4
+ } from "./chunk-KEUG7T6E.js";
5
5
 
6
6
  // src/cli.ts
7
7
  import { Command } from "commander";
8
8
  var program = new Command();
9
9
  program.version(getCliVersion());
10
10
  program.action(async () => {
11
- const { generate } = await import("./generate-HKA2M6ER.js");
11
+ const { generate } = await import("./generate-5XSJIKDA.js");
12
12
  return generate();
13
13
  });
14
14
  program.parse();
@@ -4,8 +4,8 @@ import {
4
4
  partitionApiModules,
5
5
  renderApiModules,
6
6
  transformPayloads
7
- } from "./chunk-GMB5TIVL.js";
8
- import "./chunk-PTGO2Y5X.js";
7
+ } from "./chunk-DJ4JEQRW.js";
8
+ import "./chunk-KEUG7T6E.js";
9
9
  export {
10
10
  generate,
11
11
  generateTypes,
package/dist/index.cjs CHANGED
@@ -33,6 +33,7 @@ __export(index_exports, {
33
33
  createStatusCodesByStrategy: () => createStatusCodesByStrategy,
34
34
  createTransformer: () => createTransformer,
35
35
  defineConfig: () => defineConfig,
36
+ findObjectKey: () => findObjectKey,
36
37
  generate: () => generate,
37
38
  generateTypes: () => generateTypes,
38
39
  getCliVersion: () => getCliVersion,
@@ -267,26 +268,26 @@ function getCliVersion() {
267
268
  function isRequiredRequestBody(value) {
268
269
  return "required" in value && value.required === true;
269
270
  }
271
+ function findObjectKey(object, targetKeys) {
272
+ return Object.keys(object).find((key) => targetKeys.includes(key));
273
+ }
270
274
  function getRequestBodyContentType(value) {
271
275
  if (!("content" in value)) {
272
276
  return "";
273
277
  }
274
- return value.content["application/json"] ? "application/json" : value.content["application/x-www-form-urlencoded"] ? "application/x-www-form-urlencoded" : void 0;
278
+ return findObjectKey(value.content, ["application/json", "application/x-www-form-urlencoded", "multipart/form-data"]);
275
279
  }
276
280
  function getResponseMetadataItems(operation, validateStatus) {
277
281
  const responses = operation.responses ?? {};
278
282
  const validStatusResults = Object.keys(responses).sort((a, b) => Number(a) - Number(b)).filter((key) => validateStatus(Number(key))).map(Number);
279
283
  const metadataItems = validStatusResults.map((status) => {
280
284
  const content = operation.responses?.[status]?.content ?? {};
281
- const responseContentType = findResponseContentType(content);
285
+ const responseContentType = findObjectKey(content, ["application/json", "*/*"]);
282
286
  return {
283
287
  status,
284
288
  responseContentType
285
289
  };
286
290
  }).filter((result) => result.responseContentType);
287
- function findResponseContentType(content) {
288
- return content["application/json"] ? "application/json" : content["*/*"] ? "*/*" : void 0;
289
- }
290
291
  return metadataItems;
291
292
  }
292
293
 
@@ -431,6 +432,7 @@ var import_pluralize2 = __toESM(require("pluralize"), 1);
431
432
  createStatusCodesByStrategy,
432
433
  createTransformer,
433
434
  defineConfig,
435
+ findObjectKey,
434
436
  generate,
435
437
  generateTypes,
436
438
  getCliVersion,
package/dist/index.d.cts CHANGED
@@ -44,7 +44,8 @@ declare function readTemplateFile(preset?: Preset): string;
44
44
  declare function hasQueryParameter(operation: OperationObject): boolean;
45
45
  declare function getCliVersion(): any;
46
46
  declare function isRequiredRequestBody(value: RequestBodyObject | ReferenceObject): boolean;
47
- declare function getRequestBodyContentType(value: RequestBodyObject | ReferenceObject): "" | "application/json" | "application/x-www-form-urlencoded" | undefined;
47
+ declare function findObjectKey(object: Record<string, any>, targetKeys: string[]): string | undefined;
48
+ declare function getRequestBodyContentType(value: RequestBodyObject | ReferenceObject): string | undefined;
48
49
  type ResponseMetadataItem = {
49
50
  status: number;
50
51
  responseContentType: string;
@@ -293,4 +294,4 @@ type Config = GenerateOptions;
293
294
  declare function defineConfig(config: Config): GenerateOptions;
294
295
  declare function getConfig(): Promise<Config>;
295
296
 
296
- export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type ResponseMetadataItem, type StatusCodeStrategy, type StatusCodes, type Transformer, type TransformerBaseArgs, createStatusCodesByStrategy, createTransformer, defineConfig, generate, generateTypes, getCliVersion, getConfig, getRequestBodyContentType, getResponseMetadataItems, hasQueryParameter, isRemoteSchema, isRequiredRequestBody, partitionApiModules, readSchema, readSchemaContent, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
297
+ export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type ResponseMetadataItem, type StatusCodeStrategy, type StatusCodes, type Transformer, type TransformerBaseArgs, createStatusCodesByStrategy, createTransformer, defineConfig, findObjectKey, generate, generateTypes, getCliVersion, getConfig, getRequestBodyContentType, getResponseMetadataItems, hasQueryParameter, isRemoteSchema, isRequiredRequestBody, partitionApiModules, readSchema, readSchemaContent, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
package/dist/index.d.ts CHANGED
@@ -44,7 +44,8 @@ declare function readTemplateFile(preset?: Preset): string;
44
44
  declare function hasQueryParameter(operation: OperationObject): boolean;
45
45
  declare function getCliVersion(): any;
46
46
  declare function isRequiredRequestBody(value: RequestBodyObject | ReferenceObject): boolean;
47
- declare function getRequestBodyContentType(value: RequestBodyObject | ReferenceObject): "" | "application/json" | "application/x-www-form-urlencoded" | undefined;
47
+ declare function findObjectKey(object: Record<string, any>, targetKeys: string[]): string | undefined;
48
+ declare function getRequestBodyContentType(value: RequestBodyObject | ReferenceObject): string | undefined;
48
49
  type ResponseMetadataItem = {
49
50
  status: number;
50
51
  responseContentType: string;
@@ -293,4 +294,4 @@ type Config = GenerateOptions;
293
294
  declare function defineConfig(config: Config): GenerateOptions;
294
295
  declare function getConfig(): Promise<Config>;
295
296
 
296
- export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type ResponseMetadataItem, type StatusCodeStrategy, type StatusCodes, type Transformer, type TransformerBaseArgs, createStatusCodesByStrategy, createTransformer, defineConfig, generate, generateTypes, getCliVersion, getConfig, getRequestBodyContentType, getResponseMetadataItems, hasQueryParameter, isRemoteSchema, isRequiredRequestBody, partitionApiModules, readSchema, readSchemaContent, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
297
+ export { type ApiModule, type ApiModulePayload, type ApiModuleTemplateData, type Config, type GenerateOptions, type Preset, type ResponseMetadataItem, type StatusCodeStrategy, type StatusCodes, type Transformer, type TransformerBaseArgs, createStatusCodesByStrategy, createTransformer, defineConfig, findObjectKey, generate, generateTypes, getCliVersion, getConfig, getRequestBodyContentType, getResponseMetadataItems, hasQueryParameter, isRemoteSchema, isRequiredRequestBody, partitionApiModules, readSchema, readSchemaContent, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb };
package/dist/index.js CHANGED
@@ -20,9 +20,10 @@ import {
20
20
  transformTypeValue,
21
21
  transformUrl,
22
22
  transformVerb
23
- } from "./chunk-GMB5TIVL.js";
23
+ } from "./chunk-DJ4JEQRW.js";
24
24
  import {
25
25
  createStatusCodesByStrategy,
26
+ findObjectKey,
26
27
  getCliVersion,
27
28
  getRequestBodyContentType,
28
29
  getResponseMetadataItems,
@@ -32,7 +33,7 @@ import {
32
33
  readSchema,
33
34
  readSchemaContent,
34
35
  readTemplateFile
35
- } from "./chunk-PTGO2Y5X.js";
36
+ } from "./chunk-KEUG7T6E.js";
36
37
 
37
38
  // src/index.ts
38
39
  import { default as default2 } from "pluralize";
@@ -40,6 +41,7 @@ export {
40
41
  createStatusCodesByStrategy,
41
42
  createTransformer,
42
43
  defineConfig,
44
+ findObjectKey,
43
45
  generate,
44
46
  generateTypes,
45
47
  getCliVersion,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-farmer",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "description": "API module generation tool based on Openapi3/Swagger2.",
5
5
  "keywords": [
6
6
  "cli",
@@ -2,48 +2,49 @@
2
2
  <% if (!typesOnly) { %> import { request } from '@/request' <% } %>
3
3
  <% if (ts) { %> import { type paths } from './<%- typesFilename %>' <% } %>
4
4
 
5
- <% if (!typesOnly) { %>
6
- <% apiModule.payloads.forEach(payload => { %> -%>
7
- export const <%- payload.fn %> = (config<% if (ts) { %>: RequestConfig<<%- payload.typeQuery %>, <%- payload.typeRequestBody %>> <% } %> = {})
8
- => request<% if (ts) { %><<%- payload.typeResponseBody %>><% } %>({
9
- url: '<%- payload.url %>',
10
- method: '<%- payload.method %>',
11
- ...config,
12
- <% if (payload.requestContentType === 'application/x-www-form-urlencoded') { %>headers: {
13
- 'Content-Type': 'application/x-www-form-urlencoded',
14
- ...config.headers,
15
- },
16
- <% } %>
17
- })
18
-
19
- <% }) %>
20
- <% } %>
5
+ <%_ if (!typesOnly) { _%>
6
+ <% apiModule.payloads.forEach(payload => { %> %>
7
+ export const <%- payload.fn %> =
8
+ (config<% if (ts) { %>: RequestConfig<<%- payload.typeQuery %>, <%- payload.typeRequestBody %>> <% } %> = {}) => request
9
+ <% if (ts) { %><<%- payload.typeResponseBody %>><% } %>({
10
+ url: '<%- payload.url %>',
11
+ method: '<%- payload.method %>',
12
+ ...config,
13
+ <%_ if (payload.requestContentType && payload.requestContentType !== 'application/json') { _%>
14
+ headers: {
15
+ 'Content-Type': '<%- payload.requestContentType %>',
16
+ ...config.headers,
17
+ }
18
+ <% } %>
19
+ })
20
+ <% }) %>
21
+ <%_ } _%>
21
22
 
22
23
  <% if (ts) { %>
23
- <% apiModule.payloads.forEach(payload => { %> -%>
24
- export type <%- payload.type %> = <%- payload.typeValue %>
25
-
26
- <% }) %>
27
-
28
- <% apiModule.payloads.forEach(payload => { %> -%>
29
- <% if (payload.typeQueryValue) { %>
30
- export type <%- payload.typeQuery %> = <%- payload.typeQueryValue %>
31
-
32
- <% } %>
33
- <% }) %>
34
-
35
- <% apiModule.payloads.forEach(payload => { %> -%>
36
- <% if (payload.typeRequestBodyValue) { %>
37
- export type <%- payload.typeRequestBody %> = <%- payload.typeRequestBodyValue %>
38
-
39
- <% } %>
40
- <% }) %>
41
-
42
- <% apiModule.payloads.forEach(payload => { %> -%>
43
- <% if (payload.typeResponseBodyValue) { %>
44
- export type <%- payload.typeResponseBody %> = <%- payload.typeResponseBodyValue %>
45
-
46
- <% } %>
47
- <% }) %>
24
+ <% apiModule.payloads.forEach(payload => { %> %>
25
+ export type <%- payload.type %> = <%- payload.typeValue %>
26
+
27
+ <% }) %>
28
+
29
+ <% apiModule.payloads.forEach(payload => { %> %>
30
+ <% if (payload.typeQueryValue) { %>
31
+ export type <%- payload.typeQuery %> = <%- payload.typeQueryValue %>
32
+
33
+ <% } %>
34
+ <% }) %>
35
+
36
+ <% apiModule.payloads.forEach(payload => { %> %>
37
+ <% if (payload.typeRequestBodyValue) { %>
38
+ export type <%- payload.typeRequestBody %> = <%- payload.typeRequestBodyValue %>
39
+
40
+ <% } %>
41
+ <% }) %>
42
+
43
+ <% apiModule.payloads.forEach(payload => { %> %>
44
+ <% if (payload.typeResponseBodyValue) { %>
45
+ export type <%- payload.typeResponseBody %> = <%- payload.typeResponseBodyValue %>
46
+
47
+ <% } %>
48
+ <% }) %>
48
49
  <% } %>
49
50
 
@@ -1,49 +1,59 @@
1
1
  <% if (!typesOnly) { %> import { api } from '@/request' <% } %>
2
- <% if (ts) { %> import { type paths } from './<%- typesFilename %>' <% } %>
2
+ <% if (ts) { %> import { type paths } from './<%- typesFilename %>' <% } %>
3
3
 
4
4
  <% if (!typesOnly) { %>
5
- <% apiModule.payloads.forEach(payload => { %> -%>
6
- export const <%- payload.fn %> = api
7
- <% if (ts) { %>
8
- <<%- payload.typeResponseBody %>,
9
- <% if (['get', 'delete', 'head', 'options'].includes(payload.method)) { %>
10
- <%- payload.typeQuery %>
11
- <% } else { %>
12
- <%- payload.typeRequestBody %>
13
- <% } %>,
14
- <%- payload.typeRequestBody %>>
15
- <% } %>
16
- ('<%- payload.url %>',
17
- <% if (payload.requestContentType === 'application/x-www-form-urlencoded') { %> '<%- payload.method %>UrlEncode' <% } else { %> '<%- payload.method %>' <% } %>)
18
-
19
- <% }) %>
5
+ <% apiModule.payloads.forEach(payload => { %> %>
6
+ export const <%- payload.fn %> = api
7
+
8
+ <% if (ts) { %>
9
+ <
10
+ <%- payload.typeResponseBody %>,
11
+ <% if (['get', 'delete', 'head', 'options'].includes(payload.method)) { %>
12
+ <%- payload.typeQuery %>
13
+ <% } else { %>
14
+ <%- payload.typeRequestBody %>
15
+ <% } %>,
16
+ <%- payload.typeRequestBody %>
17
+ >
18
+ <% } %>
19
+
20
+ ('<%- payload.url %>',
21
+ <%_ if (payload.requestContentType === 'application/x-www-form-urlencoded') { _%>
22
+ '<%- payload.method %>UrlEncode'
23
+ <%_ } else if (payload.requestContentType === 'multipart/form-data') { _%>
24
+ '<%- payload.method %>Multipart'
25
+ <%_ } else { _%>
26
+ '<%- payload.method %>'
27
+ <%_ } _%>)
28
+
29
+ <% }) %>
20
30
  <% } %>
21
31
 
22
32
  <% if (ts) { %>
23
- <% apiModule.payloads.forEach(payload => { %> -%>
24
- export type <%- payload.type %> = <%- payload.typeValue %>
25
-
26
- <% }) %>
27
-
28
- <% apiModule.payloads.forEach(payload => { %> -%>
29
- <% if (payload.typeQueryValue) { %>
30
- export type <%- payload.typeQuery %> = <%- payload.typeQueryValue %>
31
-
32
- <% } %>
33
- <% }) %>
34
-
35
- <% apiModule.payloads.forEach(payload => { %> -%>
36
- <% if (payload.typeRequestBodyValue) { %>
37
- export type <%- payload.typeRequestBody %> = <%- payload.typeRequestBodyValue %>
38
-
39
- <% } %>
40
- <% }) %>
41
-
42
- <% apiModule.payloads.forEach(payload => { %> -%>
43
- <% if (payload.typeResponseBodyValue) { %>
44
- export type <%- payload.typeResponseBody %> = <%- payload.typeResponseBodyValue %>
45
-
46
- <% } %>
47
- <% }) %>
33
+ <% apiModule.payloads.forEach(payload => { %> %>
34
+ export type <%- payload.type %> = <%- payload.typeValue %>
35
+
36
+ <% }) %>
37
+
38
+ <% apiModule.payloads.forEach(payload => { %> %>
39
+ <% if (payload.typeQueryValue) { %>
40
+ export type <%- payload.typeQuery %> = <%- payload.typeQueryValue %>
41
+
42
+ <% } %>
43
+ <% }) %>
44
+
45
+ <% apiModule.payloads.forEach(payload => { %> %>
46
+ <% if (payload.typeRequestBodyValue) { %>
47
+ export type <%- payload.typeRequestBody %> = <%- payload.typeRequestBodyValue %>
48
+
49
+ <% } %>
50
+ <% }) %>
51
+
52
+ <% apiModule.payloads.forEach(payload => { %> %>
53
+ <% if (payload.typeResponseBodyValue) { %>
54
+ export type <%- payload.typeResponseBody %> = <%- payload.typeResponseBodyValue %>
55
+
56
+ <% } %>
57
+ <% }) %>
48
58
  <% } %>
49
59