Skip to content

Commit

Permalink
feat: runtime api return a proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Jan 22, 2025
1 parent dd8b67c commit 712e12e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/toolkit/plugin-v2/src/cli/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function initPluginAPI<Extends extends CLIPluginExtends>({
Object.keys(extendsHooks!).forEach(hookName => {
extendsPluginApi[hookName as AllKeysForCLIPluginExtendsAPI<Extends>] = (
extendsHooks as Record<string, PluginHook<(...args: any[]) => any>>
)[hookName].tap as AllValueForCLIPluginExtendsAPI<Extends>; // Type assertion
)[hookName].tap as AllValueForCLIPluginExtendsAPI<Extends>;
});
}

Expand Down
21 changes: 18 additions & 3 deletions packages/toolkit/plugin-v2/src/runtime/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function initPluginAPI<Extends extends RuntimePluginExtends>({
plugins.forEach(plugin => {
const { _registryApi } = plugin;
if (_registryApi) {
const apis = _registryApi(getRuntimeContext, updateRuntimeContext); // Explicitly define the type
const apis = _registryApi(getRuntimeContext, updateRuntimeContext);
Object.keys(apis).forEach(apiName => {
extendsPluginApi[apiName as keyof RuntimePluginExtendsAPI<Extends>] =
apis[
Expand All @@ -81,7 +81,7 @@ export function initPluginAPI<Extends extends RuntimePluginExtends>({
});
}

return {
const pluginAPI = {
updateRuntimeContext,
getHooks,
getRuntimeConfig,
Expand All @@ -90,5 +90,20 @@ export function initPluginAPI<Extends extends RuntimePluginExtends>({
wrapRoot: hooks.wrapRoot.tap,
pickContext: hooks.pickContext.tap,
...extendsPluginApi,
} as RuntimePluginAPI<Extends>;
};

return new Proxy(pluginAPI, {
get(target: Record<string, any>, prop: string) {
// hack then function to fix p-defer handle error
if (prop === 'then') {
return undefined;
}
if (prop in target) {
return target[prop];
}
return () => {
console.warn(`api.${prop.toString()} not exist`);
};
},
}) as RuntimePluginAPI<Extends>;
}

0 comments on commit 712e12e

Please sign in to comment.