From 0b15b20a5273fdc57d69ec1229216f4408e684df Mon Sep 17 00:00:00 2001
From: czhlin <2324133088@qq.com>
Date: Tue, 4 Nov 2025 13:58:56 +0800
Subject: [PATCH] fix(wormhole): fix alova init template
---
.changeset/thirty-falcons-lie.md | 6 ++
packages/wormhole/src/utils/template.ts | 5 +-
.../test/__snapshots__/generate.spec.ts.snap | 72 +++++++++++++++++++
packages/wormhole/test/generate.spec.ts | 19 +++--
packages/wormhole/typings/index.d.ts | 5 +-
packages/wormhole/typings/plugins.d.ts | 5 +-
6 files changed, 99 insertions(+), 13 deletions(-)
create mode 100644 .changeset/thirty-falcons-lie.md
diff --git a/.changeset/thirty-falcons-lie.md b/.changeset/thirty-falcons-lie.md
new file mode 100644
index 0000000..4e4b321
--- /dev/null
+++ b/.changeset/thirty-falcons-lie.md
@@ -0,0 +1,6 @@
+---
+"alova-vscode-extension": patch
+"@alova/wormhole": patch
+---
+
+fix alova init template
diff --git a/packages/wormhole/src/utils/template.ts b/packages/wormhole/src/utils/template.ts
index df9c7d6..eb2cd3a 100644
--- a/packages/wormhole/src/utils/template.ts
+++ b/packages/wormhole/src/utils/template.ts
@@ -31,9 +31,6 @@ async function registerPartials() {
}
}
-// Initialize partials registration
-registerPartials()
-
const getType = (obj: any) => Object.prototype.toString.call(obj).slice(8, -1).toLowerCase()
handlebars.registerHelper('isType', function (this: any, value, type: string, options: HelperOptions) {
@@ -88,6 +85,8 @@ handlebars.registerHelper(
* @returns Rendered content
*/
export async function readAndRenderTemplate(templatePath: string, view: any) {
+ // Initialize partials registration
+ await registerPartials()
let data = ''
try {
data = await fs.readFile(path.resolve(DEFAULT_CONFIG.templatePath, `${templatePath}.handlebars`), 'utf-8')
diff --git a/packages/wormhole/test/__snapshots__/generate.spec.ts.snap b/packages/wormhole/test/__snapshots__/generate.spec.ts.snap
index 5a7d430..a777945 100644
--- a/packages/wormhole/test/__snapshots__/generate.spec.ts.snap
+++ b/packages/wormhole/test/__snapshots__/generate.spec.ts.snap
@@ -1,5 +1,77 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
+exports[`createConfig API > should create alova.config 1`] = `
+"import { defineConfig } from '@alova/wormhole';
+
+// For more config detailed visit:
+// https://alova.js.org/tutorial/getting-started/extension-integration
+
+export default defineConfig({
+ generator: [
+ {
+ /**
+ * file input. support:
+ * 1. openapi json file url
+ * 2. local file
+ */
+ input: 'http://localhost:3000',
+
+ /**
+ * input file platform. Currently only swagger is supported.
+ * When this parameter is specified, the input field only needs to specify the document address without specifying the openapi file
+ */
+ platform: 'swagger',
+
+ /**
+ * output path of interface file and type file.
+ * Multiple generators cannot have the same address, otherwise the generated code will overwrite each other.
+ */
+ output: 'src/api'
+
+ /**
+ * the mediaType of the generated response data. default is \`application/json\`
+ */
+ // responseMediaType: 'application/json',
+
+ /**
+ * the bodyMediaType of the generated request body data. default is \`application/json\`
+ */
+ // bodyMediaType: 'application/json',
+
+ /**
+ * the generated api version. options are \`2\` or \`3\`, default is \`auto\`.
+ */
+ // version: 'auto',
+
+ /**
+ * type of generated code. The options are \`auto/ts/typescript/module/commonjs\`
+ */
+ // type: 'auto',
+
+ /**
+ * exported global api name, you can access the generated api globally through this name, default is \`Apis\`.
+ * it is required when multiple generators are configured, and it cannot be repeated
+ */
+ // global: 'Apis',
+
+ /**
+ * filter or convert the generated api information, return an apiDescriptor, if this function is not specified, the apiDescriptor object is not converted
+ */
+ // handleApi: apiDescriptor => {
+ // return apiDescriptor;
+ // }
+ }
+ ]
+
+ /**
+ * extension only
+ * whether to automatically update the interface, enabled by default, check every 5 minutes, closed when set to \`false\`
+ */
+ // autoUpdate: true
+});
+"
+`;
+
exports[`generate API > should generate api files according to the fileNameCase config 1`] = `
"///
/* tslint:disable */
diff --git a/packages/wormhole/test/generate.spec.ts b/packages/wormhole/test/generate.spec.ts
index 3b305a6..e5505e6 100644
--- a/packages/wormhole/test/generate.spec.ts
+++ b/packages/wormhole/test/generate.spec.ts
@@ -1,7 +1,7 @@
import type { Config, SchemaObject } from '@/type'
import fs from 'node:fs/promises'
import { resolve } from 'node:path'
-import { generate } from '@/index'
+import { createConfig, generate } from '@/index'
import { createStrReg } from './util'
vi.mock('node:fs')
@@ -133,7 +133,9 @@ describe('generate API', () => {
).rejects.toThrow('Cannot read file from http://localhost:3000/openapi.json')
})
- it('should generate code with a variant of openapi file formats', async () => {
+ it('should generate code with a variant of openapi file formats', {
+ timeout: 10 * 1000,
+ }, async () => {
const outputDir = resolve(__dirname, './mock_output/openapi_301')
const results = await generate({
generator: [
@@ -179,8 +181,6 @@ describe('generate API', () => {
expect(await fs.readFile(resolve(outputDir3, 'index.ts'), 'utf-8')).toMatchSnapshot()
expect(await fs.readFile(resolve(outputDir3, 'createApis.ts'), 'utf-8')).toMatchSnapshot()
expect(await fs.readFile(resolve(outputDir3, 'globals.d.ts'), 'utf-8')).toMatchSnapshot()
- }, {
- timeout: 10 * 1000,
})
it('shouldn\'t replace `index` file if it is generated', async () => {
@@ -1174,3 +1174,14 @@ describe('generate API', () => {
expect(await fs.readFile(resolve(outputDir, 'CreateApis.ts'), 'utf-8')).toMatchSnapshot()
})
})
+
+describe('createConfig API', () => {
+ it('should create alova.config', async () => {
+ const projectPath = resolve(__dirname, '..')
+ await createConfig({
+ projectPath,
+ type: 'typescript',
+ })
+ expect(await fs.readFile(resolve(projectPath, 'alova.config.ts'), 'utf-8')).toMatchSnapshot()
+ })
+})
diff --git a/packages/wormhole/typings/index.d.ts b/packages/wormhole/typings/index.d.ts
index f1ad43c..907e47a 100644
--- a/packages/wormhole/typings/index.d.ts
+++ b/packages/wormhole/typings/index.d.ts
@@ -54,10 +54,9 @@ export interface ApiPlugin {
*/
config?: (config: GeneratorConfig) => MaybePromise;
/**
- * Manipulate the input config before parsing the openapi file.
- * Returning null does NOT replacing anything.
+ * Called before parsing the OpenAPI file.
*/
- beforeOpenapiParse?: (inputConfig: Pick) => MaybePromise | undefined | null | void>;
+ beforeOpenapiParse?: (config: GeneratorConfig) => void;
/**
* Manipulate the openapi document after parsing.
* Returning null does NOT replacing anything.
diff --git a/packages/wormhole/typings/plugins.d.ts b/packages/wormhole/typings/plugins.d.ts
index ff43edf..f2c2b10 100644
--- a/packages/wormhole/typings/plugins.d.ts
+++ b/packages/wormhole/typings/plugins.d.ts
@@ -41,10 +41,9 @@ export interface ApiPlugin {
*/
config?: (config: GeneratorConfig) => MaybePromise;
/**
- * Manipulate the input config before parsing the openapi file.
- * Returning null does NOT replacing anything.
+ * Called before parsing the OpenAPI file.
*/
- beforeOpenapiParse?: (inputConfig: Pick) => MaybePromise | undefined | null | void>;
+ beforeOpenapiParse?: (config: GeneratorConfig) => void;
/**
* Manipulate the openapi document after parsing.
* Returning null does NOT replacing anything.