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

Framework callers

Bind a caller to a request in Hono, SvelteKit, or Next.js.

A framework caller runs a function as the scope you build for the request.

Build the scope

Resolve the actor and account from trusted request data. Use the app’s signed-out scope when the request permits guests.

Bind the caller and run the function

Bind registry.as(scope) once per request. Run only functions with an authorize policy.

const scope = await scopeFrom(request);
const caller = registry.as(scope);
const booking = await caller.run(bookings.confirm, { bookingId });

Connect your framework

Hono

app.use(async (c, next) => {
  c.set("caller", registry.as(await scopeFrom(c.req.raw)));
  await next();
});

app.post("/bookings/:id/confirm", async (c) =>
  c.json(await c.var.caller.run(bookings.confirm, { bookingId: c.req.param("id") })),
);

SvelteKit

// hooks.server.ts
export const handle = async ({ event, resolve }) => {
  event.locals.caller = registry.as(await scopeFrom(event.request));
  return resolve(event);
};

// +page.server.ts
export const actions = {
  confirm: ({ params, locals }) =>
    locals.caller.run(bookings.confirm, { bookingId: params.id }),
};

Next.js

"use server";

export async function confirmBooking(bookingId: string) {
  const caller = registry.as(await scopeFromCookies());
  return caller.run(bookings.confirm, { bookingId });
}

To deliver jobs after the response, set waitUntil: (p) => after(() => p) on the registry.

Each request gets the confirmed booking when its scope passes the policy.

See also

Was this page helpful?