Skip to content
RelKit
Esc
↑↓navigate↵open⌘Jpreview
On this page

Write functions with Effect

Return an Effect from a handler and use the relkit/effect helpers when your application already uses Effect.

RelKit’s API is Promise-based, and a handler can be a plain async function. If your application already uses Effect, a handler can return an Effect instead. RelKit runs it with the same cancellation, timeout, tracing and error handling as an async handler.

Return an Effect from a handler

/** A function whose handler returns an Effect and fails with a declared error. */
import { Effect } from "effect";
import { defineError, defineFunction } from "relkit";
import { failEffect } from "relkit/effect";
import { s } from "relkit/schema";

export const orderNotFound = defineError({
  id: "orders.order-not-found",
  data: s.object({ orderId: s.string() }),
  message: ({ orderId }) => `Order ${orderId} was not found`,
  http: { status: 404 },
});

const orders = new Map([["order-1", { id: "order-1", total: 42 }]]);

export default defineFunction({
  id: "orders.get-order",
  input: s.object({ orderId: s.string() }),
  output: s.object({ id: s.string(), total: s.number() }),
  errors: [orderNotFound],
  handler: ({ orderId }) =>
    Effect.gen(function* () {
      const order = orders.get(orderId);
      if (order === undefined) return yield* failEffect(orderNotFound, { orderId });
      return order;
    }).pipe(Effect.withSpan("orders.lookup")),
});
  • defineFunction and defineError come from relkit, as they do for an async handler.
  • failEffect comes from relkit/effect. It fails the Effect with a declared error, which the caller receives exactly as if an async handler had returned fail(orderNotFound, data). You can also fail with an instance: Effect.fail(new orderNotFound({ orderId })).
  • The Effect must not need services that RelKit does not provide: its requirements type must be never. Type checking reports an Effect that still needs a service, fails with an undeclared error, or succeeds with a value that does not match the output schema.

How RelKit runs the Effect

  • Cancellation and timeouts. When the caller cancels the request or the function’s timeoutMs passes, RelKit interrupts the Effect, so its finalizers run.
  • Tracing. The Effect runs inside the function’s span. Spans it opens with Effect.withSpan appear under that span in the trace and in the Inspector.
  • Errors. A declared error becomes the same response as from an async handler. Any other failure or defect becomes an internal error, and its details stay out of the response.

What is in relkit/effect

relkit/effect holds the Effect form of each relkit function, such as defineFunctionEffect, defineRouteEffect, failEffect and streamOfEffect. Each returns an Effect that builds the same descriptor or failure as the Promise-based function. callToolEffect(tool, input) and callGraphNodeEffect(node, input) are the Effect forms of a tool’s or graph node’s invoke. The relkit entry point itself has no Effect types. See the Effect API reference for the full list.

Reference and source