This example demonstrates all the ways to handle responses from an InvocationList returned by request.send().
Create a .env file in the repo root or export these variables in your shell:
JMAP_BEARER_TOKEN=your-token
JMAP_HOSTNAME=api.fastmail.com
JMAP_LOG_LEVEL=info
Clone the jmap-kit repository, then from the repo root:
yarn tsx examples/response-dispatch/response-dispatch.ts
Sends 3 Core/echo invocations with distinct symbol IDs and demonstrates every dispatch mode:
"Core/echo" responses with a single handler"Core" responseserror property on the handler map (fires for server-returned errors)dispatch() (catches unmatched invocations)for...of loop with the isErrorInvocation() type guardWhen dispatching, the library checks handlers in this order:
handlers[invocation.id]) — highest priorityhandlers["Core/echo"])handlers["Core"])dispatch())The first matching handler wins. If none match, the invocation is skipped (logged in dev mode).
Core/echo always succeeds, so the error handler won't fire in this example. It would fire for server-returned errors like "unknownMethod" or "invalidArguments".dispatch() can be called multiple times on the same response — it iterates the stored array without consuming it.isErrorInvocation() type guard narrows the type to ErrorInvocation, giving access to .type and .arguments.