Skip to main content

setDelegate

Store a value on the request object that can be read by subsequent middleware. Each delegate key can only be set once per request — attempting to set the same key again throws an error.

Import

import { setDelegate } from '@evershop/evershop/lib/middleware/delegate';

Syntax

setDelegate<T>(id: string, value: T, request: EvershopRequest): void

Parameters

id

Type: string

A unique identifier for this delegate value.

value

Type: T

The value to store. Can be any serializable type.

request

Type: EvershopRequest

The Express request object.

Return Value

Returns void.

Examples

Share Data Between Middleware

middleware/loadProduct.ts
import { setDelegate } from '@evershop/evershop/lib/middleware/delegate';
import { select } from '@evershop/postgres-query-builder';
import { pool } from '@evershop/evershop/lib/postgres';

export default async (request, response, next) => {
const product = await select()
.from('product')
.where('product_id', '=', request.params.id)
.load(pool);

if (product) {
setDelegate('product', product, request);
next();
} else {
response.status(404);
next();
}
};

Notes

  • Write-once: setting the same key twice throws an error
  • Values are cloned when read via getDelegate, so the original cannot be modified
  • Initialize request locals before using (EverShop handles this automatically for route middleware)

See Also