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/perky-ties-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@alova/wormhole": patch
"alova-vscode-extension": patch
---

update plugin exports
13 changes: 11 additions & 2 deletions packages/wormhole/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@
"alova",
"wormhole"
],
"main": "./dist/index.js",
"types": "./typings/index.d.ts",
"exports": {
".": {
"types": "./typings/index.d.ts",
"default": "./dist/index.js"
},
"./plugin": {
"types": "./typings/plugins.d.ts",
"default": "./dist/plugins/index.js"
}
},
"bin": {
"alova": "dist/bin/cli.js"
},
Expand All @@ -40,6 +48,7 @@
"build": "run-s build:*",
"build:ts": "tsc --build tsconfig.build.json && tsc-alias -p tsconfig.build.json",
"build:dts": "dts-bundle-generator -o typings/index.d.ts src/index.ts --no-check --no-banner --project tsconfig.build.json",
"build:plugin-dts": "dts-bundle-generator -o typings/plugins.d.ts src/plugins/index.ts --no-check --no-banner --project tsconfig.build.json",
"build:copy": "ncp ./src/templates ./dist/templates"
},
"publishConfig": {
Expand Down
1 change: 0 additions & 1 deletion packages/wormhole/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ export { setGlobalConfig } from './config'
export { default as createConfig } from './createConfig'
export { defineConfig } from './defineConfig'
export { default as generate } from './generate'
export * from './plugins'
export * from './readConfig'
export { default as resolveWorkspaces } from './resolveWorkspaces'
1 change: 1 addition & 0 deletions packages/wormhole/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './createPlugin'
export * from './presets/apifox'
export * from './presets/filterApi'
export * from './presets/importType'
export * from './presets/payloadModifier'
export * from './presets/rename'
export * from './presets/tagModifier'
258 changes: 17 additions & 241 deletions packages/wormhole/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface FetchOptions {
timeout?: number;
method?: MethodType;
data?: RequestBody;
params?: Record<string, any>;
/** when true, do not throw on non-2xx but still return text; default false */
insecure?: boolean;
}
Expand Down Expand Up @@ -66,7 +67,10 @@ export interface ApiPlugin {
* Manipulate the template code before generating.
* Returning null does NOT replacing anything.
*/
beforeCodeGenerate?: (data: any, outputFile: string) => MaybePromise<string | undefined | null | void>;
beforeCodeGenerate?: (data: any, outputFile: string, ctx: {
renderTemplate: () => Promise<string>;
fileName: string;
}) => MaybePromise<string | undefined | null | void>;
/**
* Called when wormhold has finished code generating.
*/
Expand All @@ -87,6 +91,18 @@ export interface GeneratorConfig {
*/
input: string;
fetchOptions?: FetchOptions;
/**
* A list of type identifiers to exclude from generation.
* Matches against type names parsed from the OpenAPI schema; matched types
* are skipped and referenced directly by their identifier in generated code
* to avoid duplicate or conflicting declarations.
* Use this when you already have hand-written types or types provided by
* frameworks/libraries that should not be generated.
*
* @example
* externalTypes: ['File', 'Blob', 'FormData', 'Pagination']
*/
externalTypes?: string[];
/**
* Platforms that support openapi. Currently `swagger` are supported. The default is empty.
* When this parameter is specified, the input field only needs to specify the url of the document and doesn't need to be specified to the openapi file, reducing the usage threshold.
Expand Down Expand Up @@ -314,246 +330,6 @@ export declare function defineConfig(config: UserConfigExport): UserConfigExport
* @returns An array that contains the result of `generator` items in configuration whether generation is successful.
*/
export declare function generate(config: Config, rules?: GenerateApiOptions): Promise<boolean[]>;
/**
* Creates a plugin factory function with proper typing
*
* @param plugin - Function that creates a plugin instance
* @returns The original plugin function with proper typing
*
* @example
* // Create a custom plugin
* const myPlugin = createPlugin((options: MyOptions) => ({
* handleApi: (apiDescriptor) => {
* // Plugin implementation
* return apiDescriptor;
* }
* }));
*
* // Use the plugin
* generate({
* generator: [{
* // ...
* plugins: [myPlugin({ key: 'value' })]
* }]
* });
*/
export declare function createPlugin<T extends any[]>(plugin: (...args: T) => ApiPlugin): (...args: T) => ApiPlugin;
export interface APIFoxBody {
scope?: {
type?: "ALL" | "SELECTED_TAGS";
selectedTags?: string[];
excludedByTags?: string[];
};
options?: {
includeApifoxExtensionProperties?: boolean;
addFoldersToTags?: boolean;
};
oasVersion?: "2.0" | "3.0" | "3.1";
exportFormat?: "JSON" | "YAML";
environmentIds?: string[];
}
export interface ApifoxOptions extends Pick<APIFoxBody, "oasVersion" | "exportFormat">, Pick<NonNullable<APIFoxBody["options"]>, "includeApifoxExtensionProperties" | "addFoldersToTags"> {
projectId: string;
apifoxToken: string;
locale?: string;
apifoxVersion?: string;
selectedTags?: string[];
excludedByTags?: string[];
}
export declare function apifox({ projectId, locale, apifoxVersion, selectedTags, excludedByTags, apifoxToken, oasVersion, exportFormat, includeApifoxExtensionProperties, addFoldersToTags, }: ApifoxOptions): ApiPlugin;
/**
* Filter configuration interface
*/
export interface FilterApiConfig {
/**
* Target scope for filtering, defaults to 'url'
*/
scope?: "url" | "tag";
/**
* Include rule:
* - string: target contains this string
* - RegExp: target matches this pattern
* - function: custom matching logic
*/
include?: string | RegExp | ((key: string) => boolean);
/**
* Exclude rule:
* - string: target contains this string
* - RegExp: target matches this pattern
* - function: custom matching logic
*/
exclude?: string | RegExp | ((key: string) => boolean);
}
/**
* Main processing function for filtering API descriptors
* @param apiDescriptor API descriptor
* @param configs Configuration array
* @returns Filtered API descriptor, or null if filtered out
*/
export declare function filterApiDescriptor(apiDescriptor: ApiDescriptor, configs: FilterApiConfig[]): ApiDescriptor | null;
/**
* Creates a plugin for filtering APIs
*
* @param config Filter configuration, can be a single config or array of configs
* @returns API plugin instance
*
* @example
* ```ts
* // Only include URLs containing 'user'
* const userOnlyFilter = apiFilter({
* include: 'user'
* });
*
* // Exclude tags containing 'internal'
* const noInternalFilter = apiFilter({
* scope: 'tag',
* exclude: 'internal'
* });
*
* // Multi-condition filtering (union)
* const multiFilter = apiFilter([
* { include: 'user' },
* { include: 'admin' }
* ]);
* ```
*/
export declare function apiFilter(config: FilterApiConfig | FilterApiConfig[]): ApiPlugin;
export type ModifierScope = "params" | "pathParams" | "data" | "response";
export type SchemaPrimitive = "number" | "string" | "boolean" | "undefined" | "null" | "unknown" | "any" | "never";
/**
* 表示数组类型
*/
export interface SchemaArray {
type: "array";
items: Schema | Schema[];
}
/**
* 修改参数为引用类型
* 在key末端添加上?表示为可选值
*/
export interface SchemaReference {
[attr: string]: Schema;
}
/**
* 枚举类型表示
*/
export interface SchemaEnum {
enum: Array<string | number | boolean | null>;
type?: "string" | "number" | "integer" | "boolean" | "null";
}
/**
* 组合类型表示(与/或/交叉)
*/
export interface SchemaOneOf {
oneOf: Schema[];
}
export interface SchemaAnyOf {
anyOf: Schema[];
}
export interface SchemaAllOf {
allOf: Schema[];
}
/**
* 数据Schema
* SchemaArray表示类型数组,而数组表示“或”的意思
*/
export type Schema = SchemaPrimitive | SchemaReference | SchemaArray | SchemaEnum | SchemaOneOf | SchemaAnyOf | SchemaAllOf | Array<SchemaPrimitive | SchemaReference | SchemaArray | SchemaEnum>;
export interface ModifierConfig<T extends Schema> {
/**
* 生效范围,表示处理哪个位置的参数
*/
scope: ModifierScope;
/**
* 匹配规则,只有匹配到的才会进行转换,不指定则转换全部
* string:原参数名包含此string;RegExp:原参数名匹配此正则;函数时接收key并返回是否匹配的boolean值
*/
match?: string | RegExp | ((key: string) => boolean);
/**
* handler用于灵活修改参数类型值
* @param schema Schema中的一种,由用户自行定义
* @returns 返回多种参数,具体为:Schema表示修改的类型;{ required: boolean, value: Schema }表示可将当前值修改为是否必填;void | null | undefined表示移除当前字段
*/
handler: (schema: T) => Schema | {
required: boolean;
value: Schema;
} | void | null | undefined;
}
export type PayloadModifierConfig = ModifierConfig<Schema>;
export declare function payloadModifier(configs: PayloadModifierConfig[]): ApiPlugin;
/**
* Rename style options
*/
export type RenameStyle = "camelCase" | "kebabCase" | "snakeCase" | "pascalCase";
/**
* Rename plugin configuration
*/
export interface RenameConfig {
/**
* Target scope for renaming, defaults to 'url'
*/
scope?: "url" | "params" | "pathParams" | "data" | "response" | "refName";
/**
* Matching rule for selective renaming:
* - string: target contains this string
* - RegExp: target matches this pattern
* - function: custom matching logic
* If not specified, all targets will be processed
*/
match?: string | RegExp | ((key: string) => boolean);
/**
* Naming style to apply
*/
style?: RenameStyle;
/**
* Custom transformation function
* Will be applied before style transformation
*/
transform?: (apiDescriptor: ApiDescriptor) => string;
}
/**
* Creates a rename plugin that transforms API descriptors
* according to specified naming rules
*/
export declare function rename(config: RenameConfig | RenameConfig[]): ApiPlugin;
/**
* Tag modifier handler function type
* Receives a tag string and returns the modified tag string, or null/undefined/void to remove the tag
*/
export type ModifierHandler = (tag: string) => string | null | undefined | void;
/**
* Processes tags in the API descriptor
* @param apiDescriptor The API descriptor
* @param handler Tag modifier handler function
* @returns Modified API descriptor
*/
export declare function processApiTags(apiDescriptor: ApiDescriptor, handler: ModifierHandler): ApiDescriptor;
/**
* Creates a tag modifier plugin
*
* @param handler Tag modifier handler function that receives a tag string and returns modified tag or null/undefined/void to remove the tag
* @returns API plugin instance
*
* @example
* ```ts
* // Convert all tags to uppercase
* const upperCasePlugin = tagModifier(tag => tag.toUpperCase());
*
* // Add prefix to tags
* const prefixPlugin = tagModifier(tag => `api-${tag}`);
*
* // Remove specific tags
* const filterPlugin = tagModifier(tag => tag === 'internal' ? null : tag);
*
* // Use the plugin
* export default {
* generator: [{
* // ...other config
* plugins: [upperCasePlugin]
* }]
* };
* ```
*/
export declare function tagModifier(handler: ModifierHandler): ApiPlugin;
/**
* Read the alova.config configuration file and return the parsed configuration object.
* @param projectPath The project path where the configuration file is located. The default value is `process.cwd()`.
Expand Down
Loading