unjs / unjs/hookable

Support chainable hooks

Open
#111 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

discussion enhancement
Dominant language
TypeScript
Stars
960
Forks
43
Avg merge
19m
Merged PRs (30d)
1

Description

Describe the feature

Hookable hooks will be called sequentially by default with original arguments. But result of each step is not preserved.

We could export a built-in chainable hook runner.

Example from https://github.com/unjs/community/discussions/15 (@ChrisGV04)

  const chainableCaller = async (hooks, args) => {
      let hookResult: Order = args[0];

      for (const hook of hooks) {
        hookResult = await hook(hookResult);
      }

      return hookResult;
    }
  /** Before create hook */
  const orderData = await hooks.callHookWith(chainableCaller, 'orders:before-create', originalOrder);

Some ideas:

  • We could export a built-in chainableCaller
  • We could support hookable.callHookChained(name, <initial>) that chains first arg
  • Hooks can be registred as chainable <-- I prefer this most / to define strategy on register instead of use but needs few initial refactors

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing hook-calling API around the proposed callHookWith and callHookChained forms, then compare them with the chainableCaller example in the issue. Resolve whether chaining is configured when hooks are registered or selected at call time, and define tests that verify sequential execution, argument flow, and the final result.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.