wormajs 0.0.2-beta.3 → 0.2.0

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.
Files changed (37) hide show
  1. package/dist/constant.js +7 -10
  2. package/dist/core/parser/openApiParser/helper.js +91 -69
  3. package/dist/helper/config/GeneratorHelper.js +20 -18
  4. package/dist/helper/config/zType.js +2 -2
  5. package/dist/helper/progress.js +2 -2
  6. package/dist/plugins/index.js +6 -2
  7. package/dist/plugins/presets/payloadModifier/hepler.js +29 -33
  8. package/dist/plugins/presets/payloadModifier/index.js +8 -8
  9. package/dist/plugins/presets/platform/fastapi.js +22 -0
  10. package/dist/plugins/presets/platform/index.js +15 -0
  11. package/dist/plugins/presets/platform/knife4j.js +24 -0
  12. package/dist/plugins/presets/platform/shared.js +45 -0
  13. package/dist/plugins/presets/platform/swagger.js +32 -0
  14. package/dist/plugins/presets/platform/yapi.js +75 -0
  15. package/dist/plugins/presets/rename.js +154 -21
  16. package/dist/plugins/presets/tagModifier.js +6 -2
  17. package/dist/plugins/presets/utils.js +11 -10
  18. package/dist/template/index.js +0 -34
  19. package/dist/template/presets/alova/partials/dts-extra-config.handlebars +3 -3
  20. package/dist/template/presets/alova/partials/dts-fn-declare.handlebars +1 -1
  21. package/dist/template/presets/alova/typescript/services/{tag}.ts.handlebars +5 -5
  22. package/dist/template/presets/axios/partials/dts-types.handlebars +4 -4
  23. package/dist/template/presets/axios/typescript/services/{tag}.ts.handlebars +4 -4
  24. package/dist/template/presets/config/common/worma.config.js.handlebars +1 -1
  25. package/dist/template/presets/config/module/worma.config.js.handlebars +1 -1
  26. package/dist/template/presets/config/partials/generator-content.handlebars +45 -45
  27. package/dist/template/presets/config/typescript/worma.config.ts.handlebars +1 -1
  28. package/dist/template/presets/fetch/partials/dts-types.handlebars +4 -4
  29. package/dist/template/presets/fetch/typescript/services/{tag}.ts.handlebars +4 -4
  30. package/dist/template/presets/ky/partials/dts-types.handlebars +4 -4
  31. package/dist/template/presets/ky/typescript/services/{tag}.ts.handlebars +4 -4
  32. package/dist/tsconfig.build.tsbuildinfo +1 -1
  33. package/dist/utils/template.js +23 -0
  34. package/package.json +1 -1
  35. package/typings/index.d.ts +9 -5
  36. package/typings/plugins.d.ts +134 -48
  37. package/dist/plugins/presets/platform.js +0 -73
@@ -1,5 +1,5 @@
1
1
  import { defineConfig } from 'wormajs';
2
- import { platform, aiDoc{{#if templateImport}}, {{{templateImport}}}{{/if}} } from 'wormajs/plugin';
2
+ import { swagger, aiDoc{{#if templateImport}}, {{{templateImport}}}{{/if}} } from 'wormajs/plugin';
3
3
 
4
4
  // For more config detailed visit:
5
5
  // https://github.com/alovajs/devtools
@@ -1,45 +1,45 @@
1
- generator: [
2
- {
3
- /**
4
- * file input. support:
5
- * 1. openapi json file url
6
- * 2. local file
7
- * 3. string array — each URL will be tried in order, first success wins
8
- */
9
- input: 'http://localhost:3000',
10
-
11
- /**
12
- * output path of interface file and type file.
13
- * Multiple generators cannot have the same address, otherwise the generated code will overwrite each other.
14
- */
15
- output: 'src/api',
16
-
17
- /**
18
- * platform plugin: auto-resolves OpenAPI file URLs from platform type.
19
- * Uses config.input as base URL to construct candidate OpenAPI endpoints.
20
- */
21
- plugins: [platform('swagger'), aiDoc({ installSkill: true }), {{{templateCall}}}],
22
-
23
- /**
24
- * the mediaType of the generated response data. default is `application/json`
25
- */
26
- // responseMediaType: 'application/json',
27
-
28
- /**
29
- * the bodyMediaType of the generated request body data. default is `application/json`
30
- */
31
- // bodyMediaType: 'application/json',
32
-
33
- /**
34
- * type of generated code. The options are `auto/ts/typescript/module/commonjs`
35
- */
36
- // type: 'auto',
37
-
38
- /**
39
- * filter or convert the generated api information, return an apiDescriptor, if this function is not specified, the apiDescriptor object is not converted
40
- */
41
- // handleApi: apiDescriptor => {
42
- // return apiDescriptor;
43
- // }
44
- }
45
- ]
1
+ generator: [
2
+ {
3
+ /**
4
+ * file input. support:
5
+ * 1. openapi json file url
6
+ * 2. local file
7
+ * 3. string array — each URL will be tried in order, first success wins
8
+ */
9
+ // input: 'http://localhost:3000',
10
+
11
+ /**
12
+ * output path of interface file and type file.
13
+ * Multiple generators cannot have the same address, otherwise the generated code will overwrite each other.
14
+ */
15
+ output: 'src/api',
16
+
17
+ /**
18
+ * swagger platform plugin: auto-resolves OpenAPI file URLs from the base URL.
19
+ * The base URL is passed as the plugin argument (not config.input).
20
+ */
21
+ plugins: [swagger('http://localhost:3000'), aiDoc({ installSkill: true }), {{{templateCall}}}],
22
+
23
+ /**
24
+ * the mediaType of the generated response data. default is `application/json`
25
+ */
26
+ // responseMediaType: 'application/json',
27
+
28
+ /**
29
+ * the bodyMediaType of the generated request body data. default is `application/json`
30
+ */
31
+ // bodyMediaType: 'application/json',
32
+
33
+ /**
34
+ * type of generated code. The options are `auto/ts/typescript/module/commonjs`
35
+ */
36
+ // type: 'auto',
37
+
38
+ /**
39
+ * filter or convert the generated api information, return an apiDescriptor, if this function is not specified, the apiDescriptor object is not converted
40
+ */
41
+ // handleApi: apiDescriptor => {
42
+ // return apiDescriptor;
43
+ // }
44
+ }
45
+ ]
@@ -1,5 +1,5 @@
1
1
  import { defineConfig } from 'wormajs';
2
- import { platform, aiDoc{{#if templateImport}}, {{{templateImport}}}{{/if}} } from 'wormajs/plugin';
2
+ import { swagger, aiDoc{{#if templateImport}}, {{{templateImport}}}{{/if}} } from 'wormajs/plugin';
3
3
 
4
4
  // For more config detailed visit:
5
5
  // https://github.com/alovajs/devtools
@@ -1,7 +1,7 @@
1
- type {{{name}}}Response = {{processType response @root.componentNames}};
1
+ type {{{name}}}Response = {{addNamespace response @root.componentNames}};
2
2
  type {{{name}}}ExtraConfig = Omit<RequestInit, 'body'> & {
3
- {{#if pathParameters}}pathParams: {{processType pathParameters @root.componentNames}};
4
- {{/if}}{{#if queryParameters}}params?: {{processType queryParameters @root.componentNames}};
5
- {{/if}}{{#if requestBody}}body?: {{processType requestBody @root.componentNames}} | BodyInit;
3
+ {{#if pathParameters}}pathParams: {{addNamespace pathParameters @root.componentNames}};
4
+ {{/if}}{{#if queryParameters}}params?: {{addNamespace queryParameters @root.componentNames}};
5
+ {{/if}}{{#if requestBody}}body?: {{addNamespace requestBody @root.componentNames}} | BodyInit;
6
6
  {{/if}}
7
7
  };
@@ -8,11 +8,11 @@ import { {{{tagName}}}DefaultConfig } from '.';
8
8
 
9
9
  {{#each tagedApis}}
10
10
  {{#each apis}}
11
- type {{{name}}}Response = {{processType response @root.componentNames}};
11
+ type {{{name}}}Response = {{addNamespace response @root.componentNames}};
12
12
  type {{{name}}}ExtraConfig = Omit<RequestInit, 'body'> & {
13
- {{#if pathParameters}}pathParams: {{processType pathParameters @root.componentNames}};
14
- {{/if}}{{#if queryParameters}}params?: {{processType queryParameters @root.componentNames}};
15
- {{/if}}{{#if requestBody}}body?: {{processType requestBody @root.componentNames}};
13
+ {{#if pathParameters}}pathParams: {{addNamespace pathParameters @root.componentNames}};
14
+ {{/if}}{{#if queryParameters}}params?: {{addNamespace queryParameters @root.componentNames}};
15
+ {{/if}}{{#if requestBody}}body?: {{addNamespace requestBody @root.componentNames}};
16
16
  {{/if}}
17
17
  };
18
18
  {{> api-jsdoc}}
@@ -1,9 +1,9 @@
1
- type {{{name}}}Response = {{processType response @root.componentNames}};
1
+ type {{{name}}}Response = {{addNamespace response @root.componentNames}};
2
2
  {{#or pathParameters queryParameters requestBody}}
3
3
  type {{{name}}}ExtraConfig = Options & {
4
- {{#if pathParameters}}pathParams: {{processType pathParameters @root.componentNames}};
5
- {{/if}}{{#if queryParameters}}searchParams: {{processType queryParameters @root.componentNames}};
6
- {{/if}}{{#if requestBody}}json: {{processType requestBody @root.componentNames}};
4
+ {{#if pathParameters}}pathParams: {{addNamespace pathParameters @root.componentNames}};
5
+ {{/if}}{{#if queryParameters}}searchParams: {{addNamespace queryParameters @root.componentNames}};
6
+ {{/if}}{{#if requestBody}}json: {{addNamespace requestBody @root.componentNames}};
7
7
  {{/if}}
8
8
  };
9
9
  {{/or}}
@@ -9,11 +9,11 @@ import { {{{tagName}}}DefaultConfig } from '.';
9
9
 
10
10
  {{#each tagedApis}}
11
11
  {{#each apis}}
12
- type {{{name}}}Response = {{processType response @root.componentNames}};
12
+ type {{{name}}}Response = {{addNamespace response @root.componentNames}};
13
13
  type {{{name}}}ExtraConfig = Options & {
14
- {{#if pathParameters}}pathParams: {{processType pathParameters @root.componentNames}};
15
- {{/if}}{{#if queryParameters}}searchParams: {{processType queryParameters @root.componentNames}};
16
- {{/if}}{{#if requestBody}}json: {{processType requestBody @root.componentNames}};
14
+ {{#if pathParameters}}pathParams: {{addNamespace pathParameters @root.componentNames}};
15
+ {{/if}}{{#if queryParameters}}searchParams: {{addNamespace queryParameters @root.componentNames}};
16
+ {{/if}}{{#if requestBody}}json: {{addNamespace requestBody @root.componentNames}};
17
17
  {{/if}}
18
18
  };
19
19
  {{> api-jsdoc}}
@@ -1 +1 @@
1
- {"root":["../src/config.ts","../src/constant.ts","../src/createConfig.ts","../src/defineConfig.ts","../src/generate.ts","../src/index.ts","../src/readConfig.ts","../src/resolveWorkspaces.ts","../src/bin/actions.ts","../src/bin/cli.ts","../src/bin/progressRenderer.ts","../src/bin/renderer.ts","../src/bin/theme.ts","../src/core/WorkerPool.ts","../src/core/loader/index.ts","../src/core/loader/astLoader/helper.ts","../src/core/loader/astLoader/index.ts","../src/core/loader/astLoader/generates/array.ts","../src/core/loader/astLoader/generates/enum.ts","../src/core/loader/astLoader/generates/group.ts","../src/core/loader/astLoader/generates/index.ts","../src/core/loader/astLoader/generates/interface.ts","../src/core/loader/astLoader/generates/simple.ts","../src/core/loader/astLoader/generates/tuple.ts","../src/core/loader/astLoader/generates/type.ts","../src/core/loader/astLoader/generates/utils.ts","../src/core/loader/astLoader/normalize/index.ts","../src/core/loader/astLoader/normalize/normalizer.ts","../src/core/loader/astLoader/normalize/rules/convertTypeArray.ts","../src/core/loader/astLoader/normalize/rules/handleEmptyType.ts","../src/core/loader/astLoader/normalize/rules/inferType.ts","../src/core/loader/astLoader/normalize/rules/mergeAnyOf.ts","../src/core/loader/astLoader/normalize/rules/normalizeCombiningKeywords.ts","../src/core/loader/astLoader/normalize/rules/normalizeEnum.ts","../src/core/loader/astLoader/normalize/rules/normalizeNullType.ts","../src/core/loader/astLoader/normalize/rules/removeRedundantKeywords.ts","../src/core/loader/astLoader/normalize/rules/simplifySingleType.ts","../src/core/loader/astLoader/normalize/rules/validateSchema.ts","../src/core/loader/astLoader/parsers/array.ts","../src/core/loader/astLoader/parsers/const.ts","../src/core/loader/astLoader/parsers/enum.ts","../src/core/loader/astLoader/parsers/group.ts","../src/core/loader/astLoader/parsers/index.ts","../src/core/loader/astLoader/parsers/object.ts","../src/core/loader/astLoader/parsers/reference.ts","../src/core/loader/astLoader/parsers/simple.ts","../src/core/loader/astLoader/parsers/tuple.ts","../src/core/loader/astLoader/parsers/type.ts","../src/core/loader/astLoader/parsers/utils.ts","../src/core/loader/astLoader/parsers/forward/array.ts","../src/core/loader/astLoader/parsers/forward/const.ts","../src/core/loader/astLoader/parsers/forward/enum.ts","../src/core/loader/astLoader/parsers/forward/group.ts","../src/core/loader/astLoader/parsers/forward/index.ts","../src/core/loader/astLoader/parsers/forward/object.ts","../src/core/loader/astLoader/parsers/forward/tuple.ts","../src/core/loader/astLoader/parsers/forward/type.ts","../src/core/loader/callingCodeLoader/helper.ts","../src/core/loader/callingCodeLoader/index.ts","../src/core/loader/schemaLoader/index.ts","../src/core/loader/standardLoader/helper.ts","../src/core/loader/standardLoader/index.ts","../src/core/loader/standardLoader/standards.ts","../src/core/parser/index.ts","../src/core/parser/openApiParser/helper.ts","../src/core/parser/openApiParser/index.ts","../src/core/parser/templateParser/helper.ts","../src/core/parser/templateParser/index.ts","../src/core/workerPool/index.ts","../src/core/workerPool/poolManager.ts","../src/core/workerPool/swagger2Worker.ts","../src/core/workerPool/worker.ts","../src/functions/getAutoTemplateType.ts","../src/functions/getFrameworkTag.ts","../src/functions/prepareConfig.ts","../src/functions/readWormaRc.ts","../src/functions/wormaJson.ts","../src/helper/PluginDriver.ts","../src/helper/comment.ts","../src/helper/index.ts","../src/helper/progress.ts","../src/helper/config/ConfigHelper.ts","../src/helper/config/ConfigManager.ts","../src/helper/config/GeneratorHelper.ts","../src/helper/config/index.ts","../src/helper/config/type.ts","../src/helper/config/zType.ts","../src/helper/document/index.ts","../src/helper/logger/index.ts","../src/helper/template/index.ts","../src/plugins/createPlugin.ts","../src/plugins/index.ts","../src/plugins/presets/aiDoc.ts","../src/plugins/presets/apifox.ts","../src/plugins/presets/filterApi.ts","../src/plugins/presets/importType.ts","../src/plugins/presets/platform.ts","../src/plugins/presets/rename.ts","../src/plugins/presets/tagModifier.ts","../src/plugins/presets/utils.ts","../src/plugins/presets/payloadModifier/hepler.ts","../src/plugins/presets/payloadModifier/index.ts","../src/plugins/presets/payloadModifier/type.ts","../src/template/index.ts","../src/type/api.ts","../src/type/ast.ts","../src/type/base.ts","../src/type/index.ts","../src/type/lib.ts","../src/type/openapi.ts","../src/utils/base.ts","../src/utils/format.ts","../src/utils/index.ts","../src/utils/openapi.ts","../src/utils/readPackageJson.ts","../src/utils/template.ts"],"version":"5.9.3"}
1
+ {"root":["../src/config.ts","../src/constant.ts","../src/createConfig.ts","../src/defineConfig.ts","../src/generate.ts","../src/index.ts","../src/readConfig.ts","../src/resolveWorkspaces.ts","../src/bin/actions.ts","../src/bin/cli.ts","../src/bin/progressRenderer.ts","../src/bin/renderer.ts","../src/bin/theme.ts","../src/core/WorkerPool.ts","../src/core/loader/index.ts","../src/core/loader/astLoader/helper.ts","../src/core/loader/astLoader/index.ts","../src/core/loader/astLoader/generates/array.ts","../src/core/loader/astLoader/generates/enum.ts","../src/core/loader/astLoader/generates/group.ts","../src/core/loader/astLoader/generates/index.ts","../src/core/loader/astLoader/generates/interface.ts","../src/core/loader/astLoader/generates/simple.ts","../src/core/loader/astLoader/generates/tuple.ts","../src/core/loader/astLoader/generates/type.ts","../src/core/loader/astLoader/generates/utils.ts","../src/core/loader/astLoader/normalize/index.ts","../src/core/loader/astLoader/normalize/normalizer.ts","../src/core/loader/astLoader/normalize/rules/convertTypeArray.ts","../src/core/loader/astLoader/normalize/rules/handleEmptyType.ts","../src/core/loader/astLoader/normalize/rules/inferType.ts","../src/core/loader/astLoader/normalize/rules/mergeAnyOf.ts","../src/core/loader/astLoader/normalize/rules/normalizeCombiningKeywords.ts","../src/core/loader/astLoader/normalize/rules/normalizeEnum.ts","../src/core/loader/astLoader/normalize/rules/normalizeNullType.ts","../src/core/loader/astLoader/normalize/rules/removeRedundantKeywords.ts","../src/core/loader/astLoader/normalize/rules/simplifySingleType.ts","../src/core/loader/astLoader/normalize/rules/validateSchema.ts","../src/core/loader/astLoader/parsers/array.ts","../src/core/loader/astLoader/parsers/const.ts","../src/core/loader/astLoader/parsers/enum.ts","../src/core/loader/astLoader/parsers/group.ts","../src/core/loader/astLoader/parsers/index.ts","../src/core/loader/astLoader/parsers/object.ts","../src/core/loader/astLoader/parsers/reference.ts","../src/core/loader/astLoader/parsers/simple.ts","../src/core/loader/astLoader/parsers/tuple.ts","../src/core/loader/astLoader/parsers/type.ts","../src/core/loader/astLoader/parsers/utils.ts","../src/core/loader/astLoader/parsers/forward/array.ts","../src/core/loader/astLoader/parsers/forward/const.ts","../src/core/loader/astLoader/parsers/forward/enum.ts","../src/core/loader/astLoader/parsers/forward/group.ts","../src/core/loader/astLoader/parsers/forward/index.ts","../src/core/loader/astLoader/parsers/forward/object.ts","../src/core/loader/astLoader/parsers/forward/tuple.ts","../src/core/loader/astLoader/parsers/forward/type.ts","../src/core/loader/callingCodeLoader/helper.ts","../src/core/loader/callingCodeLoader/index.ts","../src/core/loader/schemaLoader/index.ts","../src/core/loader/standardLoader/helper.ts","../src/core/loader/standardLoader/index.ts","../src/core/loader/standardLoader/standards.ts","../src/core/parser/index.ts","../src/core/parser/openApiParser/helper.ts","../src/core/parser/openApiParser/index.ts","../src/core/parser/templateParser/helper.ts","../src/core/parser/templateParser/index.ts","../src/core/workerPool/index.ts","../src/core/workerPool/poolManager.ts","../src/core/workerPool/swagger2Worker.ts","../src/core/workerPool/worker.ts","../src/functions/getAutoTemplateType.ts","../src/functions/getFrameworkTag.ts","../src/functions/prepareConfig.ts","../src/functions/readWormaRc.ts","../src/functions/wormaJson.ts","../src/helper/PluginDriver.ts","../src/helper/comment.ts","../src/helper/index.ts","../src/helper/progress.ts","../src/helper/config/ConfigHelper.ts","../src/helper/config/ConfigManager.ts","../src/helper/config/GeneratorHelper.ts","../src/helper/config/index.ts","../src/helper/config/type.ts","../src/helper/config/zType.ts","../src/helper/document/index.ts","../src/helper/logger/index.ts","../src/helper/template/index.ts","../src/plugins/createPlugin.ts","../src/plugins/index.ts","../src/plugins/presets/aiDoc.ts","../src/plugins/presets/apifox.ts","../src/plugins/presets/filterApi.ts","../src/plugins/presets/importType.ts","../src/plugins/presets/rename.ts","../src/plugins/presets/tagModifier.ts","../src/plugins/presets/utils.ts","../src/plugins/presets/payloadModifier/hepler.ts","../src/plugins/presets/payloadModifier/index.ts","../src/plugins/presets/payloadModifier/type.ts","../src/plugins/presets/platform/fastapi.ts","../src/plugins/presets/platform/index.ts","../src/plugins/presets/platform/knife4j.ts","../src/plugins/presets/platform/shared.ts","../src/plugins/presets/platform/swagger.ts","../src/plugins/presets/platform/yapi.ts","../src/template/index.ts","../src/type/api.ts","../src/type/ast.ts","../src/type/base.ts","../src/type/index.ts","../src/type/lib.ts","../src/type/openapi.ts","../src/utils/base.ts","../src/utils/format.ts","../src/utils/index.ts","../src/utils/openapi.ts","../src/utils/readPackageJson.ts","../src/utils/template.ts"],"version":"5.9.3"}
@@ -76,6 +76,29 @@ function registerCommonHelpers(hbs) {
76
76
  return text;
77
77
  return text.split('\n').map(line => line.replace(/^\* ?/, '')).join('\n');
78
78
  });
79
+ /**
80
+ * Scan the type string for every PascalCase identifier that is in componentNames
81
+ * (and not already prefixed with `${importName}.`) and prefix it.
82
+ * Works uniformly for top-level names, generics, object literals, and arrays.
83
+ *
84
+ * @param typeStr The type expression to process.
85
+ * @param componentNames The list of component schema names to prefix.
86
+ * @param importName The imported namespace name to prefix with. Defaults to "ComponentTypes".
87
+ */
88
+ hbs.registerHelper('addNamespace', (typeStr, componentNames, importName) => {
89
+ const type = typeStr;
90
+ const names = componentNames;
91
+ const importPrefix = (typeof importName === 'string' && importName.length > 0) ? importName : 'ComponentTypes';
92
+ if (!type || !Array.isArray(names) || names.length === 0) {
93
+ return new hbs.SafeString(type || 'unknown');
94
+ }
95
+ const nameSet = new Set(names);
96
+ const escapedPrefix = importPrefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
97
+ const result = type.replace(new RegExp(`(?<!${escapedPrefix}\\.)(\\b[A-Z]\\w*\\b)`, 'g'), (match) => {
98
+ return nameSet.has(match) ? `${importPrefix}.${match}` : match;
99
+ });
100
+ return new hbs.SafeString(result);
101
+ });
79
102
  }
80
103
  /**
81
104
  * Pass in text content and generate a custom file in the specified directory
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wormajs",
3
- "version": "0.0.2-beta.3",
3
+ "version": "0.2.0",
4
4
  "description": "A modern OpenAPI code generator - Generate type-safe API clients from OpenAPI specs",
5
5
  "author": "worma",
6
6
  "license": "MIT",
@@ -54,12 +54,14 @@ export interface ConfigHookParams {
54
54
  projectPath: string;
55
55
  reportProgress: ReportProgress;
56
56
  }
57
- export interface BeforeOpenapiParseHookParams {
57
+ export interface BeforeSpecParseHookParams {
58
58
  config: Readonly<GeneratorConfig>;
59
+ /** The raw OpenAPI specification text (JSON or YAML), before it is parsed. */
60
+ spec: string;
59
61
  projectPath: string;
60
62
  reportProgress: ReportProgress;
61
63
  }
62
- export interface OpenapiParsedHookParams {
64
+ export interface SpecParsedHookParams {
63
65
  config: Readonly<GeneratorConfig>;
64
66
  document: OpenAPIDocument;
65
67
  projectPath: string;
@@ -140,14 +142,16 @@ export interface ApiPlugin {
140
142
  */
141
143
  config?: (params: ConfigHookParams) => MaybePromise<GeneratorConfig | undefined | null | void>;
142
144
  /**
143
- * Called before parsing the OpenAPI file.
145
+ * Called after the raw OpenAPI spec text is fetched but before it is parsed.
146
+ * Return a (possibly modified) string to replace the spec text that will be
147
+ * parsed. Returning nothing keeps the original spec text.
144
148
  */
145
- beforeOpenapiParse?: (params: BeforeOpenapiParseHookParams) => void;
149
+ beforeSpecParse?: (params: BeforeSpecParseHookParams) => MaybePromise<string | undefined | null | void>;
146
150
  /**
147
151
  * Manipulate the openapi document after parsing.
148
152
  * Returning null does NOT replacing anything.
149
153
  */
150
- openapiParsed?: (params: OpenapiParsedHookParams) => MaybePromise<OpenAPIDocument | undefined | null | void>;
154
+ specParsed?: (params: SpecParsedHookParams) => MaybePromise<OpenAPIDocument | undefined | null | void>;
151
155
  /**
152
156
  * Called before code generation. Mutate `params.data` directly to inject
153
157
  * configuration data (no longer returns a value).
@@ -42,12 +42,14 @@ export interface ConfigHookParams {
42
42
  projectPath: string;
43
43
  reportProgress: ReportProgress;
44
44
  }
45
- export interface BeforeOpenapiParseHookParams {
45
+ export interface BeforeSpecParseHookParams {
46
46
  config: Readonly<GeneratorConfig>;
47
+ /** The raw OpenAPI specification text (JSON or YAML), before it is parsed. */
48
+ spec: string;
47
49
  projectPath: string;
48
50
  reportProgress: ReportProgress;
49
51
  }
50
- export interface OpenapiParsedHookParams {
52
+ export interface SpecParsedHookParams {
51
53
  config: Readonly<GeneratorConfig>;
52
54
  document: OpenAPIDocument;
53
55
  projectPath: string;
@@ -128,14 +130,16 @@ export interface ApiPlugin {
128
130
  */
129
131
  config?: (params: ConfigHookParams) => MaybePromise<GeneratorConfig | undefined | null | void>;
130
132
  /**
131
- * Called before parsing the OpenAPI file.
133
+ * Called after the raw OpenAPI spec text is fetched but before it is parsed.
134
+ * Return a (possibly modified) string to replace the spec text that will be
135
+ * parsed. Returning nothing keeps the original spec text.
132
136
  */
133
- beforeOpenapiParse?: (params: BeforeOpenapiParseHookParams) => void;
137
+ beforeSpecParse?: (params: BeforeSpecParseHookParams) => MaybePromise<string | undefined | null | void>;
134
138
  /**
135
139
  * Manipulate the openapi document after parsing.
136
140
  * Returning null does NOT replacing anything.
137
141
  */
138
- openapiParsed?: (params: OpenapiParsedHookParams) => MaybePromise<OpenAPIDocument | undefined | null | void>;
142
+ specParsed?: (params: SpecParsedHookParams) => MaybePromise<OpenAPIDocument | undefined | null | void>;
139
143
  /**
140
144
  * Called before code generation. Mutate `params.data` directly to inject
141
145
  * configuration data (no longer returns a value).
@@ -529,36 +533,29 @@ export interface ImportTypeOptions {
529
533
  export declare function importType(imports: Record<string, string[]>, options?: {
530
534
  files?: string[];
531
535
  }): ApiPlugin;
532
- declare enum ModifierScope {
533
- PARAMS = "params",
534
- PATH_PARAMS = "pathParams",
535
- DATA = "data",
536
- RESPONSE = "response"
537
- }
538
- export type SchemaPrimitive = "number" | "string" | "boolean" | "undefined" | "null" | "unknown" | "any" | "never" | ({} & string);
536
+ export type ModifierScope = "params" | "pathParams" | "data" | "response";
537
+ export type SchemaPrimitive = "number" | "string" | "boolean" | "undefined" | "null" | "unknown" | "any" | "never";
539
538
  /**
540
- * 表示数组类型
539
+ * Array type: a native JS array whose elements are Schemas.
540
+ * e.g. ['string'] means string[]; ['string', 'number'] means the tuple [string, number]
541
541
  */
542
- export interface SchemaArray {
543
- type: "array";
544
- items: Schema | Schema[];
545
- }
542
+ export type SchemaArray = Schema[];
546
543
  /**
547
- * 修改参数为引用类型
548
- * key末端添加上?表示为可选值
544
+ * Object/reference type.
545
+ * Append '?' to the end of a key to mark it optional.
549
546
  */
550
547
  export interface SchemaReference {
551
548
  [attr: string]: Schema;
552
549
  }
553
550
  /**
554
- * 枚举类型表示
551
+ * Enum type representation.
555
552
  */
556
553
  export interface SchemaEnum {
557
554
  enum: Array<string | number | boolean | null>;
558
555
  type?: SchemaPrimitive;
559
556
  }
560
557
  /**
561
- * 组合类型表示(与/或/交叉)
558
+ * Composite types (oneOf / anyOf / allOf).
562
559
  */
563
560
  export interface SchemaOneOf {
564
561
  oneOf: Schema[];
@@ -570,60 +567,149 @@ export interface SchemaAllOf {
570
567
  allOf: Schema[];
571
568
  }
572
569
  /**
573
- * 数据Schema
574
- * SchemaArray表示类型数组,而数组表示“或”的意思
570
+ * Standalone primitive type that is itself optional (driven by the `type` field).
571
+ * Used in handler input/output to mean "this field is optional / make it optional".
572
+ */
573
+ export interface SchemaOptional {
574
+ required: boolean;
575
+ type: Schema;
576
+ }
577
+ /**
578
+ * The data Schema.
579
+ * - SchemaArray is a native array (elements are Schemas)
580
+ * - composite types use { oneOf | anyOf | allOf: Schema[] }
581
+ * - optional object properties use a trailing '?' on the key;
582
+ * a standalone optional primitive uses the SchemaOptional wrapper
575
583
  */
576
- export type Schema = SchemaPrimitive | SchemaReference | SchemaArray | SchemaEnum | SchemaOneOf | SchemaAnyOf | SchemaAllOf | Array<SchemaPrimitive | SchemaReference | SchemaArray | SchemaEnum>;
577
- export interface ModifierConfig<T extends Schema> {
584
+ export type Schema = SchemaPrimitive | SchemaReference | SchemaArray | SchemaEnum | SchemaOneOf | SchemaAnyOf | SchemaAllOf | SchemaOptional;
585
+ export interface ModifierConfig {
578
586
  /**
579
- * 生效范围,表示处理哪个位置的参数
587
+ * The scope the modifier applies to (which parameter location to process).
580
588
  */
581
589
  scope: ModifierScope;
582
590
  /**
583
- * 匹配规则,只有匹配到的才会进行转换,不指定则转换全部
584
- * string:原参数名包含此string;RegExp:原参数名匹配此正则;函数时接收key并返回是否匹配的boolean值
591
+ * Match rule. Only matched fields are transformed; when omitted, all fields are transformed.
592
+ * - string: the original field name contains this string
593
+ * - RegExp: the original field name matches this pattern
594
+ * - function: receives the key and returns a boolean indicating a match
585
595
  */
586
596
  match?: string | RegExp | ((key: string) => boolean);
587
597
  /**
588
- * handler用于灵活修改参数类型值
589
- * @param schema Schema中的一种,由用户自行定义
590
- * @returns 返回多种参数,具体为:Schema表示修改的类型;{ required: boolean, value: Schema }表示可将当前值修改为是否必填;void | null | undefined表示移除当前字段
598
+ * handler flexibly modifies the parameter type value.
599
+ * @param schema the original field type, already converted to the user-facing Schema representation.
600
+ * When the field itself is optional and is a primitive, it is passed as { required: false, type: 'string' }.
601
+ * Narrow the type inside handler if needed (e.g. with a cast).
602
+ * @returns Schema to change the type; { required: boolean, type: Schema } to change requiredness (driven by `type`);
603
+ * void | null | undefined to remove the field.
591
604
  */
592
- handler: (schema: T) => Schema | {
605
+ handler: (schema: Schema) => Schema | {
593
606
  required: boolean;
594
- value: Schema;
607
+ type: Schema;
595
608
  } | void | null | undefined;
596
609
  }
597
- export type PayloadModifierConfig = ModifierConfig<Schema>;
610
+ export type PayloadModifierConfig = ModifierConfig;
598
611
  export declare function payloadModifier(configs: PayloadModifierConfig[]): ApiPlugin;
599
612
  /**
600
- * Supported platform types
613
+ * FastAPI platform plugin.
614
+ *
615
+ * Pass the base URL of your FastAPI app; the plugin will try `/openapi.json`
616
+ * first, then fall back to the bare base URL.
617
+ *
618
+ * @param input - base URL string, or an array of base URLs
619
+ *
620
+ * @example
621
+ * ```ts
622
+ * plugins: [fastapi('http://fastapi-example.dokkuapp.com'), alovaGlobals()]
623
+ * ```
624
+ */
625
+ export declare const fastapi: (input: string | string[]) => ApiPlugin;
626
+ /**
627
+ * Knife4j platform plugin.
628
+ *
629
+ * Pass the base URL of your Knife4j instance; the plugin will try the OAS3
630
+ * endpoint (springdoc) first, then the Swagger2 endpoint (springfox), then the
631
+ * bare base URL.
632
+ *
633
+ * @param input - base URL string, or an array of base URLs
634
+ *
635
+ * @example
636
+ * ```ts
637
+ * plugins: [knife4j('https://openapi3.demo.knife4jnext.com'), alovaGlobals()]
638
+ * ```
639
+ */
640
+ export declare const knife4j: (input: string | string[]) => ApiPlugin;
641
+ /**
642
+ * Swagger platform plugin.
643
+ *
644
+ * Pass the base URL of your Swagger UI / server; the plugin will try several
645
+ * common OpenAPI document endpoints (OAS3 first, then Swagger2, then the bare
646
+ * base URL) and let the framework pick the first one that responds.
647
+ *
648
+ * @param input - base URL string, or an array of base URLs
649
+ *
650
+ * @example
651
+ * ```ts
652
+ * import { swagger, alovaGlobals } from 'wormajs/plugin';
653
+ *
654
+ * defineConfig({
655
+ * generator: [{
656
+ * plugins: [swagger('https://petstore3.swagger.io'), alovaGlobals()],
657
+ * output: './src/api',
658
+ * }]
659
+ * });
660
+ * ```
601
661
  */
602
- export type PlatformType = "swagger" | "knife4j" | "fastapi" | "yapi";
662
+ export declare const swagger: (input: string | string[]) => ApiPlugin;
663
+ export interface YapiOptions {
664
+ /** YApi 服务基础地址,例如 `https://yapi.xxx.com` */
665
+ url: string;
666
+ /** 项目 ID,必填,用于拼装导出地址 */
667
+ pid: string | number;
668
+ /** OpenAPI 类型,默认 `OpenAPIV2` */
669
+ type?: string;
670
+ /** 接口状态,默认 `all` */
671
+ status?: string;
672
+ /** 是否包含 wiki,默认 `true` */
673
+ isWiki?: boolean;
674
+ /** 登录 cookie。也可通过 fetchOptions.headers.cookie 传入 */
675
+ cookie?: string;
676
+ /** 额外的 fetch 超时(毫秒) */
677
+ timeout?: number;
678
+ }
603
679
  /**
604
- * Platform plugin for auto-resolving OpenAPI file URLs.
680
+ * YApi platform plugin.
605
681
  *
606
- * Pass a platform type (e.g., `'swagger'`) and the plugin will use `config.input`
607
- * as the base URL to generate candidate OpenAPI file URLs. The framework will try
608
- * each URL in order and use the first successful response.
682
+ * YApi projects are private, so the OpenAPI document must be exported through
683
+ * YApi's own export endpoint, authenticated with your login cookie. The plugin
684
+ * builds the export URL from the server base URL (`url`) plus the required
685
+ * `pid` and the optional `type` / `status` / `isWiki` query params (which
686
+ * default to `OpenAPIV2`, `all`, and `true` respectively).
609
687
  *
610
- * @param platformType - The platform type: 'swagger' | 'knife4j' | 'fastapi' | 'yapi'
611
- * @returns ApiPlugin
688
+ * `url`, `pid` and `cookie` are required the plugin throws a clear error when
689
+ * any is missing.
690
+ *
691
+ * @param options - `{ url, pid, cookie?, type?, status?, isWiki?, timeout? }`
612
692
  *
613
693
  * @example
614
694
  * ```ts
615
- * import { platform, alovaGlobals } from 'wormajs/plugin';
695
+ * import { yapi, alovaGlobals } from 'wormajs/plugin';
616
696
  *
617
697
  * defineConfig({
618
698
  * generator: [{
619
- * input: 'https://petstore3.swagger.io',
620
- * plugins: [platform('swagger'), alovaGlobals()],
699
+ * plugins: [
700
+ * yapi({
701
+ * url: 'https://yapi.xxx.com',
702
+ * pid: 123,
703
+ * cookie: '_yapi_token=xxx; _yapi_uid=yyy',
704
+ * }),
705
+ * alovaGlobals(),
706
+ * ],
621
707
  * output: './src/api',
622
708
  * }]
623
709
  * });
624
710
  * ```
625
711
  */
626
- export declare function platform(platformType: PlatformType): ApiPlugin;
712
+ export declare function yapi(options: YapiOptions): ApiPlugin;
627
713
  /**
628
714
  * Rename style options
629
715
  */
@@ -635,7 +721,7 @@ export interface RenameConfig {
635
721
  /**
636
722
  * Target scope for renaming, defaults to 'url'
637
723
  */
638
- scope?: "url" | "params" | "pathParams" | "data" | "response" | "refName";
724
+ scope?: "url" | "params" | "pathParams" | "data" | "response" | "refName" | "name";
639
725
  /**
640
726
  * Matching rule for selective renaming:
641
727
  * - string: target contains this string
@@ -643,7 +729,7 @@ export interface RenameConfig {
643
729
  * - function: custom matching logic
644
730
  * If not specified, all targets will be processed
645
731
  */
646
- match?: string | RegExp | ((key: string) => boolean);
732
+ match?: string | RegExp | ((key: string, level?: number) => boolean);
647
733
  /**
648
734
  * Naming style to apply
649
735
  */
@@ -1,73 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.platform = platform;
4
- const constant_1 = require("../../constant");
5
- /**
6
- * Generate OpenAPI file URLs based on platform type.
7
- * For Swagger and Knife4j, generates multiple URLs covering both OAS3 and Swagger2 endpoints.
8
- */
9
- function resolvePlatformUrls(baseUrl, platformType) {
10
- // Normalize: remove trailing slash
11
- const base = baseUrl.replace(/\/+$/, '');
12
- switch (platformType) {
13
- case 'swagger':
14
- // Try OAS3 first, then Swagger2, then generic openapi.json
15
- return [
16
- `${base}/openapi.json`,
17
- `${base}/v2/swagger.json`,
18
- `${base}/api/v3/openapi.json`,
19
- baseUrl,
20
- ];
21
- case 'knife4j':
22
- // Try OAS3 first (springdoc), then Swagger2 (springfox)
23
- return [
24
- `${base}/v3/api-docs`,
25
- `${base}/v2/api-docs`,
26
- baseUrl,
27
- ];
28
- case 'fastapi':
29
- return [`${base}/openapi.json`, baseUrl];
30
- case 'yapi':
31
- // YApi requires pid and token in URL, use input as-is
32
- return [baseUrl];
33
- }
34
- }
35
- /**
36
- * Platform plugin for auto-resolving OpenAPI file URLs.
37
- *
38
- * Pass a platform type (e.g., `'swagger'`) and the plugin will use `config.input`
39
- * as the base URL to generate candidate OpenAPI file URLs. The framework will try
40
- * each URL in order and use the first successful response.
41
- *
42
- * @param platformType - The platform type: 'swagger' | 'knife4j' | 'fastapi' | 'yapi'
43
- * @returns ApiPlugin
44
- *
45
- * @example
46
- * ```ts
47
- * import { platform, alovaGlobals } from 'wormajs/plugin';
48
- *
49
- * defineConfig({
50
- * generator: [{
51
- * input: 'https://petstore3.swagger.io',
52
- * plugins: [platform('swagger'), alovaGlobals()],
53
- * output: './src/api',
54
- * }]
55
- * });
56
- * ```
57
- */
58
- function platform(platformType) {
59
- return {
60
- name: constant_1.PluginName.PLATFORM,
61
- config({ config }) {
62
- const raw = config.input;
63
- if (!raw)
64
- return config;
65
- // Normalize to an array of base URLs, deduplicate
66
- const inputs = Array.isArray(raw) ? [...new Set(raw)] : [raw];
67
- // For each input, generate platform URLs, then flatten
68
- config.input = inputs.flatMap(url => resolvePlatformUrls(url, platformType));
69
- return config;
70
- },
71
- };
72
- }
73
- exports.default = platform;