defineLocals
Declares a typed group of locals with defaults.
Declares a typed group of locals with defaults. It does not change the global DuskLocals interface (merge into that for the app-wide types); it gives one module a typed view and a middleware that makes sure the keys exist.
Import#
ts
import { defineLocals } from "@shadow-js/dusk/server";
Signature#
ts
function defineLocals<T extends Record<string, unknown>>(defaults: T): DefinedLocals<T>
Parameters#
| Name | Description |
|---|---|
defaults |
The default value of each key. A key a middleware set earlier keeps that value. An array, plain object, Set, Map or Date is copied for each request (so its mutation stays in that request); it must be structured-cloneable. |
Returns#
The middleware and the typed view.
Throws#
- TypeError when a default cannot be copied for each request.
Examples#
ts
const session = defineLocals({ user: undefined as User | undefined, requestedAt: 0 });
export const middleware = [session.middleware, async (event, next) => {
session.of(event).user = await readUser(event);
return next();
}];