restatedev / restatedev/sdk-typescript

Hooks interface - add call/send interceptor

Open
#694 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
122
Forks
27
Avg merge
9h 17m
Merged PRs (30d)
8

Description

Extend hook interface to allow for customisation of RPC opts for call/send requests to other restate handlers

export interface Interceptor {
  /** Wraps the entire handler invocation. Fires on every attempt. */
  handler?: (next: () => Promise<void>) => Promise<void>;
  /**
   * Wraps each `ctx.run()` call. Only fires for runs that actually execute —
   * replayed runs (already in the journal) are skipped.
   * `name` is the run's label.
   */
  run?: (name: string, next: () => Promise<void>) => Promise<void>;
  /**
   * Wraps each RPC call. Only fires for calls that actually execute —
   * replayed calls (already in the journal) are skipped.
   * `call` is the argument provided to `ctx.genericCall`, which is also called from `ctx.serviceClient` etc.
   * Optionally modify the GenericCall instance you provide to the `next` callback.
   */
  call?: (call: GenericCall<unknown, unknown>, next: (nextCall: GenericCall<unknown, unknown>) => Promise<void>) => Promise<void>;
  /**
   * Wraps each RPC send. Only fires for sends that actually execute —
   * replayed sends (already in the journal) are skipped.
   * `send` is the argument provided to `ctx.genericSend`, which is also called from `ctx.serviceSendClient` etc.
   * Optionally modify the GenericSend instance you provide to the `next` callback.
   */
  send?: (send: GenericSend<unknown>, next: (nextSend: GenericSend<unknown>) => Promise<void>) => Promise<void>;
}

My use-case is that I want to use the existing handler interceptor to read some specific incoming HTTP headers into AsyncLocalStorage (ALS), which I then want to inject into outbound RPC HTTP headers automatically (to other restate handlers).
This interface would enable me to read from ALS and append these into the call.headers property, for example.

The JSdoc about "Only fires when not a replay" is optional for my use-case, just copied the JSdoc from the ctx.run example, but would likely be preferential for any observer-only behaviours

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 locating the Interceptor interface and the genericCall and genericSend entry points, then trace how the existing handler and run hooks are invoked. Add call and send interception with support for modified GenericCall and GenericSend values, and verify that outbound RPC options can be customized without changing replay behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend-api-design
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.