Js chain of responsibility pattern

From Intense-Visions/harness-engineering · View interactive version

Js chain of responsibility pattern is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Pass a request along a chain of handlers until one handles it

How to use this skill

Skill file

JS Chain of Responsibility Pattern

Pass a request along a chain of handlers until one handles it

When to Use

Instructions

  1. Define a handler interface with a handle(request) method and a setNext(handler) reference.
  2. Each handler decides to process the request or pass it to the next handler in the chain.
  3. Build the chain by linking handlers: h1.setNext(h2).setNext(h3).
  4. The client sends the request to the first handler — it does not know which handler will process it.
class Handler {
  setNext(handler) {
    this.next = handler;
    return handler;
  }
  handle(request) {
    if (this.next) return this.next.handle(request);
    return null;
  }
}

class AuthHandler extends Handler {
  handle(req) {
    if (!req.token) return { error: 'Unauthorized' };
    return super.handle(req);
  }
}

class RateLimitHandler extends Handler {
  handle(req) {
    if (req.rateLimited) return { error: 'Too many requests' };
    return super.handle(req);
  }
}
  1. Return a result from the handler that processes the request, or a default/null if no handler matches.

Details

The Chain of Responsibility pattern decouples senders from receivers by giving multiple objects a chance to handle a request. The request travels along the chain until a handler processes it or it reaches the end. Express.js middleware is a functional variant of this pattern.

Trade-offs:

When NOT to use:

Source

https://patterns.dev/javascript/chain-of-responsibility-pattern

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.