Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: inject plugins through HooksManager constructor #795

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

noy4
Copy link
Contributor

@noy4 noy4 commented Dec 8, 2024

Inject plugins through HooksManager constructor instead of directly define in it.

Motivation:
In our sveltekit project, we're using this repo as a submodule and import some part to create Hono instance and attach it to our backend. When we deploy it to Cloudflare Pages, it failed because the plugins in HooksManager depend on Node API (#601). So we copied middlewares/hooks/index.ts and commented out the plugins part, and use the hooks middleware in our Hono instance.

export class HookSpan { ... }

export class HooksManager {
  private spans: Record<string, HookSpan> = {}
  private plugins: any

  constructor() {
    // this.plugins = plugins // ←
  }
}

export const hooks = (c: Context, next: any) => {
  const hooksManager = new HooksManager();
  c.set('hooksManager', hooksManager);
  c.set('executeHooks', hooksManager.executeHooks.bind(hooksManager));
  return next();
};

if plugins are injectable through HooksManager constructor, we only need to copy the hooks middleware part.

export const hooks = (c: Context, next: any) => {
  const hooksManager = new HooksManager(plugins);
  // const hooksManager = new HooksManager(); // we will change here, pass nothing, or custom something
  c.set('hooksManager', hooksManager);
  c.set('executeHooks', hooksManager.executeHooks.bind(hooksManager));
  return next();
};

Related Issues:

@VisargD
Copy link
Collaborator

VisargD commented Dec 20, 2024

Hey, sorry for the delay. We are reviewing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants