api-farmer 0.0.25 → 0.0.26-alpha.1743347452766

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.
@@ -16,7 +16,7 @@ var SUPPORTED_HTTP_METHODS = ["get", "post", "put", "delete", "patch", "options"
16
16
  import { resolve as resolve2 } from "path";
17
17
  import { createAxle } from "@varlet/axle";
18
18
  import fse from "fs-extra";
19
- import { tryParseJSON } from "rattail";
19
+ import { isObject, isPlainObject, tryParseJSON } from "rattail";
20
20
  import { logger } from "rslog";
21
21
  import swagger from "swagger2openapi";
22
22
  import yaml from "yaml";
@@ -56,8 +56,31 @@ async function readSchema(input) {
56
56
  const jsonSchema = tryParseJSON(content);
57
57
  const swaggerOrOpenapiSchema = jsonSchema ? jsonSchema : yaml.parse(content);
58
58
  const schema = swaggerOrOpenapiSchema.swagger ? (await swagger.convert(swaggerOrOpenapiSchema, {})).openapi : swaggerOrOpenapiSchema;
59
+ unrefSchema(schema, schema);
59
60
  return schema;
60
61
  }
62
+ function unrefSchema(node, schema) {
63
+ if (!isObject(node)) {
64
+ return;
65
+ }
66
+ for (const [key, value] of Object.entries(node)) {
67
+ if (!isPlainObject(value) || !value.$ref || !value.$ref.startsWith("#/")) {
68
+ unrefSchema(value, schema);
69
+ continue;
70
+ }
71
+ const refNode = getSchemaNode(schema, value.$ref.slice(2));
72
+ if (refNode) {
73
+ node[key] = refNode;
74
+ unrefSchema(node, schema);
75
+ } else {
76
+ logger.warn(`Unresolved reference: ${value.$ref}`);
77
+ }
78
+ }
79
+ }
80
+ function getSchemaNode(schema, path2) {
81
+ const paths = path2.split("/");
82
+ return paths.reduce((node, path3) => node[path3], schema);
83
+ }
61
84
  async function readSchemaContent(input) {
62
85
  if (isRemoteSchema(input)) {
63
86
  try {
@@ -118,6 +141,8 @@ export {
118
141
  SUPPORTED_HTTP_METHODS,
119
142
  createStatusCodesByStrategy,
120
143
  readSchema,
144
+ unrefSchema,
145
+ getSchemaNode,
121
146
  readSchemaContent,
122
147
  isRemoteSchema,
123
148
  readTemplateFile,
@@ -7,7 +7,7 @@ import {
7
7
  isRequiredRequestBody,
8
8
  readSchema,
9
9
  readTemplateFile
10
- } from "./chunk-KEUG7T6E.js";
10
+ } from "./chunk-3VOSIUFI.js";
11
11
 
12
12
  // src/generate.ts
13
13
  import { resolve } from "path";
package/dist/cli.cjs CHANGED
@@ -57,8 +57,31 @@ async function readSchema(input) {
57
57
  const jsonSchema = (0, import_rattail.tryParseJSON)(content);
58
58
  const swaggerOrOpenapiSchema = jsonSchema ? jsonSchema : import_yaml.default.parse(content);
59
59
  const schema = swaggerOrOpenapiSchema.swagger ? (await import_swagger2openapi.default.convert(swaggerOrOpenapiSchema, {})).openapi : swaggerOrOpenapiSchema;
60
+ unrefSchema(schema, schema);
60
61
  return schema;
61
62
  }
63
+ function unrefSchema(node, schema) {
64
+ if (!(0, import_rattail.isObject)(node)) {
65
+ return;
66
+ }
67
+ for (const [key, value] of Object.entries(node)) {
68
+ if (!(0, import_rattail.isPlainObject)(value) || !value.$ref || !value.$ref.startsWith("#/")) {
69
+ unrefSchema(value, schema);
70
+ continue;
71
+ }
72
+ const refNode = getSchemaNode(schema, value.$ref.slice(2));
73
+ if (refNode) {
74
+ node[key] = refNode;
75
+ unrefSchema(node, schema);
76
+ } else {
77
+ import_rslog.logger.warn(`Unresolved reference: ${value.$ref}`);
78
+ }
79
+ }
80
+ }
81
+ function getSchemaNode(schema, path) {
82
+ const paths = path.split("/");
83
+ return paths.reduce((node, path2) => node[path2], schema);
84
+ }
62
85
  async function readSchemaContent(input) {
63
86
  if (isRemoteSchema(input)) {
64
87
  try {
package/dist/cli.js CHANGED
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  getCliVersion
4
- } from "./chunk-KEUG7T6E.js";
4
+ } from "./chunk-3VOSIUFI.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-UUTBYLAG.js");
11
+ const { generate } = await import("./generate-KVWODGEV.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-AHZLGQI5.js";
8
- import "./chunk-KEUG7T6E.js";
7
+ } from "./chunk-JQIHZEB4.js";
8
+ import "./chunk-3VOSIUFI.js";
9
9
  export {
10
10
  generate,
11
11
  generateTypes,
package/dist/index.cjs CHANGED
@@ -40,6 +40,7 @@ __export(index_exports, {
40
40
  getConfig: () => getConfig,
41
41
  getRequestBodyContentType: () => getRequestBodyContentType,
42
42
  getResponseMetadataItems: () => getResponseMetadataItems,
43
+ getSchemaNode: () => getSchemaNode,
43
44
  hasQueryParameter: () => hasQueryParameter,
44
45
  isRemoteSchema: () => isRemoteSchema,
45
46
  isRequiredRequestBody: () => isRequiredRequestBody,
@@ -62,7 +63,8 @@ __export(index_exports, {
62
63
  transformTypeResponseBodyValue: () => transformTypeResponseBodyValue,
63
64
  transformTypeValue: () => transformTypeValue,
64
65
  transformUrl: () => transformUrl,
65
- transformVerb: () => transformVerb
66
+ transformVerb: () => transformVerb,
67
+ unrefSchema: () => unrefSchema
66
68
  });
67
69
  module.exports = __toCommonJS(index_exports);
68
70
 
@@ -236,8 +238,31 @@ async function readSchema(input) {
236
238
  const jsonSchema = (0, import_rattail2.tryParseJSON)(content);
237
239
  const swaggerOrOpenapiSchema = jsonSchema ? jsonSchema : import_yaml.default.parse(content);
238
240
  const schema = swaggerOrOpenapiSchema.swagger ? (await import_swagger2openapi.default.convert(swaggerOrOpenapiSchema, {})).openapi : swaggerOrOpenapiSchema;
241
+ unrefSchema(schema, schema);
239
242
  return schema;
240
243
  }
244
+ function unrefSchema(node, schema) {
245
+ if (!(0, import_rattail2.isObject)(node)) {
246
+ return;
247
+ }
248
+ for (const [key, value] of Object.entries(node)) {
249
+ if (!(0, import_rattail2.isPlainObject)(value) || !value.$ref || !value.$ref.startsWith("#/")) {
250
+ unrefSchema(value, schema);
251
+ continue;
252
+ }
253
+ const refNode = getSchemaNode(schema, value.$ref.slice(2));
254
+ if (refNode) {
255
+ node[key] = refNode;
256
+ unrefSchema(node, schema);
257
+ } else {
258
+ import_rslog.logger.warn(`Unresolved reference: ${value.$ref}`);
259
+ }
260
+ }
261
+ }
262
+ function getSchemaNode(schema, path) {
263
+ const paths = path.split("/");
264
+ return paths.reduce((node, path2) => node[path2], schema);
265
+ }
241
266
  async function readSchemaContent(input) {
242
267
  if (isRemoteSchema(input)) {
243
268
  try {
@@ -449,6 +474,7 @@ var import_pluralize2 = __toESM(require("pluralize"), 1);
449
474
  getConfig,
450
475
  getRequestBodyContentType,
451
476
  getResponseMetadataItems,
477
+ getSchemaNode,
452
478
  hasQueryParameter,
453
479
  isRemoteSchema,
454
480
  isRequiredRequestBody,
@@ -471,5 +497,6 @@ var import_pluralize2 = __toESM(require("pluralize"), 1);
471
497
  transformTypeResponseBodyValue,
472
498
  transformTypeValue,
473
499
  transformUrl,
474
- transformVerb
500
+ transformVerb,
501
+ unrefSchema
475
502
  });
package/dist/index.d.cts CHANGED
@@ -38,6 +38,8 @@ declare function createStatusCodesByStrategy(strategy: StatusCodeStrategy): {
38
38
  head: number;
39
39
  };
40
40
  declare function readSchema(input: string): Promise<OpenAPI3>;
41
+ declare function unrefSchema(node: Record<string, any> | any[], schema: OpenAPI3): void;
42
+ declare function getSchemaNode(schema: OpenAPI3, path: string): any;
41
43
  declare function readSchemaContent(input: string): Promise<string>;
42
44
  declare function isRemoteSchema(path: string): boolean;
43
45
  declare function readTemplateFile(preset?: Preset): string;
@@ -301,4 +303,4 @@ type Config = GenerateOptions;
301
303
  declare function defineConfig(config: Config): GenerateOptions;
302
304
  declare function getConfig(): Promise<Config>;
303
305
 
304
- 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 };
306
+ 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, getSchemaNode, hasQueryParameter, isRemoteSchema, isRequiredRequestBody, partitionApiModules, readSchema, readSchemaContent, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb, unrefSchema };
package/dist/index.d.ts CHANGED
@@ -38,6 +38,8 @@ declare function createStatusCodesByStrategy(strategy: StatusCodeStrategy): {
38
38
  head: number;
39
39
  };
40
40
  declare function readSchema(input: string): Promise<OpenAPI3>;
41
+ declare function unrefSchema(node: Record<string, any> | any[], schema: OpenAPI3): void;
42
+ declare function getSchemaNode(schema: OpenAPI3, path: string): any;
41
43
  declare function readSchemaContent(input: string): Promise<string>;
42
44
  declare function isRemoteSchema(path: string): boolean;
43
45
  declare function readTemplateFile(preset?: Preset): string;
@@ -301,4 +303,4 @@ type Config = GenerateOptions;
301
303
  declare function defineConfig(config: Config): GenerateOptions;
302
304
  declare function getConfig(): Promise<Config>;
303
305
 
304
- 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 };
306
+ 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, getSchemaNode, hasQueryParameter, isRemoteSchema, isRequiredRequestBody, partitionApiModules, readSchema, readSchemaContent, readTemplateFile, renderApiModules, transformEntity, transformFn, transformModuleName, transformPayloads, transformType, transformTypeQuery, transformTypeQueryValue, transformTypeRequestBody, transformTypeRequestBodyValue, transformTypeResponseBody, transformTypeResponseBodyValue, transformTypeValue, transformUrl, transformVerb, unrefSchema };
package/dist/index.js CHANGED
@@ -20,20 +20,22 @@ import {
20
20
  transformTypeValue,
21
21
  transformUrl,
22
22
  transformVerb
23
- } from "./chunk-AHZLGQI5.js";
23
+ } from "./chunk-JQIHZEB4.js";
24
24
  import {
25
25
  createStatusCodesByStrategy,
26
26
  findObjectKey,
27
27
  getCliVersion,
28
28
  getRequestBodyContentType,
29
29
  getResponseMetadataItems,
30
+ getSchemaNode,
30
31
  hasQueryParameter,
31
32
  isRemoteSchema,
32
33
  isRequiredRequestBody,
33
34
  readSchema,
34
35
  readSchemaContent,
35
- readTemplateFile
36
- } from "./chunk-KEUG7T6E.js";
36
+ readTemplateFile,
37
+ unrefSchema
38
+ } from "./chunk-3VOSIUFI.js";
37
39
 
38
40
  // src/index.ts
39
41
  import { default as default2 } from "pluralize";
@@ -48,6 +50,7 @@ export {
48
50
  getConfig,
49
51
  getRequestBodyContentType,
50
52
  getResponseMetadataItems,
53
+ getSchemaNode,
51
54
  hasQueryParameter,
52
55
  isRemoteSchema,
53
56
  isRequiredRequestBody,
@@ -70,5 +73,6 @@ export {
70
73
  transformTypeResponseBodyValue,
71
74
  transformTypeValue,
72
75
  transformUrl,
73
- transformVerb
76
+ transformVerb,
77
+ unrefSchema
74
78
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-farmer",
3
- "version": "0.0.25",
3
+ "version": "0.0.26-alpha.1743347452766",
4
4
  "description": "API module generation tool based on Openapi3/Swagger2.",
5
5
  "keywords": [
6
6
  "cli",