Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/thirty-falcons-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"alova-vscode-extension": patch
"@alova/wormhole": patch
---

fix alova init template
5 changes: 2 additions & 3 deletions packages/wormhole/src/utils/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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')
Expand Down
72 changes: 72 additions & 0 deletions packages/wormhole/test/__snapshots__/generate.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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`] = `
"/// <reference types='./globals.d.ts' />
/* tslint:disable */
Expand Down
19 changes: 15 additions & 4 deletions packages/wormhole/test/generate.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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()
})
})
5 changes: 2 additions & 3 deletions packages/wormhole/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ export interface ApiPlugin {
*/
config?: (config: GeneratorConfig) => MaybePromise<GeneratorConfig | undefined | null | void>;
/**
* Manipulate the input config before parsing the openapi file.
* Returning null does NOT replacing anything.
* Called before parsing the OpenAPI file.
*/
beforeOpenapiParse?: (inputConfig: Pick<GeneratorConfig, "input" | "platform" | "plugins" | "fetchOptions">) => MaybePromise<Pick<GeneratorConfig, "input" | "platform" | "plugins" | "fetchOptions"> | undefined | null | void>;
beforeOpenapiParse?: (config: GeneratorConfig) => void;
/**
* Manipulate the openapi document after parsing.
* Returning null does NOT replacing anything.
Expand Down
5 changes: 2 additions & 3 deletions packages/wormhole/typings/plugins.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ export interface ApiPlugin {
*/
config?: (config: GeneratorConfig) => MaybePromise<GeneratorConfig | undefined | null | void>;
/**
* Manipulate the input config before parsing the openapi file.
* Returning null does NOT replacing anything.
* Called before parsing the OpenAPI file.
*/
beforeOpenapiParse?: (inputConfig: Pick<GeneratorConfig, "input" | "platform" | "plugins" | "fetchOptions">) => MaybePromise<Pick<GeneratorConfig, "input" | "platform" | "plugins" | "fetchOptions"> | undefined | null | void>;
beforeOpenapiParse?: (config: GeneratorConfig) => void;
/**
* Manipulate the openapi document after parsing.
* Returning null does NOT replacing anything.
Expand Down