Scope
The scope identifies who runs a function and where it runs.
A scope is app-defined data such as the actor, account, and locale. Put booking IDs and form data in the input.
registry.as(scope)
Binds the scope once
ctx.scope
Available in each call
ctx.run
Inherits the current scope
Listener
Uses the emitter's scope
const const caller: Caller<Scope, AppError<"accounts.forbidden" | "accounts.getLocale.forbidden", undefined> | AppError<"accounts.getLocale.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"accounts.notFound", undefined>>
caller = const registry: Registry<Scope, {
readonly accounts: {
readonly getLocale: Fn<"query", undefined, string, false, {
scope: Scope;
}, undefined, undefined>;
};
}, Record<string, never>, {}, AppError<"accounts.forbidden" | "accounts.getLocale.forbidden", undefined> | AppError<"accounts.getLocale.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"accounts.notFound", undefined>>
registry.Registry<Scope, { readonly accounts: { readonly getLocale: Fn<"query", undefined, string, false, { scope: Scope; }, undefined, undefined>; }; }, Record<string, never>, {}, AppError<...> | ... 1 more ... | AppError<...>>.as(scope: Scope): Caller<Scope, AppError<"accounts.forbidden" | "accounts.getLocale.forbidden", undefined> | AppError<"accounts.getLocale.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"accounts.notFound", undefined>>
Bind who is calling, once per request, job or test: `const caller = registry.as(scope)`.as({ accountId: stringaccountId: "a1", actor: string | nullactor: null, locale: stringlocale: "es" });
await const caller: Caller<Scope, AppError<"accounts.forbidden" | "accounts.getLocale.forbidden", undefined> | AppError<"accounts.getLocale.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"accounts.notFound", undefined>>
caller.CallerLike<Scope>.run<Fn<"query", undefined, string, false, {
scope: Scope;
}, undefined, undefined>>(fn: Fn<"query", undefined, string, false, {
scope: Scope;
}, undefined, undefined>, input?: undefined, options?: RunOptions<Scope> | undefined): Promise<string>
Run any function as this caller: `await caller.run(bookings.cancel, input, { signal: request.signal })`.run(const getLocale: Fn<"query", undefined, string, false, {
scope: Scope;
}, undefined, undefined>
getLocale);
- Binding. Bind once per request, script, or test. Use
CallerOf<typeof registry>for helpers that take the caller. - Inheritance (1). A nested call gets the caller’s scope. To replace it, pass
{ scope }as the third argument toctx.run. The new scope is whole, not merged, and applies to that call and every call it runs. - Listeners (2). See Job scope for listener overrides and how
jobs.scope.loadrebuilds scope.
The system
Use distinct scope variants for signed-out actors, signed-in actors, and system work. A system scope can carry kind: "system", a job name, actor: null, and an account ID when needed.
Create system scopes in crons or trusted scripts. Grant system access explicitly in policies. Preserve the variant in job scope.