entity-routes logo
Docs

Request Context#

Request Context#

Every request passing through the RequestContextMiddleware will have a custom RequestContext attached to the current request context state.

#
typescript
1type RequestContext<Entity extends GenericEntity = GenericEntity, QP = QueryParams, State = Record<string, any>> = {
2 /** Current request id */
3 requestId?: string;
4 /** Request context adapter */
5 ctx?: ContextAdapter<QP, State>;
6 /** Current route entity id */
7 entityId?: string | number;
8 /** Parent subresource relations, used to auto-join on this entity's relation inverse side */
9 subresourceRelations?: SubresourceRelation[];
10 /** Is update or create operation ? To check if there is a body sent */
11 isUpdateOrCreate?: boolean;
12 /** Request body values sent */
13 values?: DeepPartial<Entity>;
14 /** Request query params */
15 queryParams?: QP;
16 /** Custom operation for a custom action */
17 operation?: RouteOperation;
18};

Store#

Requests passing through the RequestContextMiddleware will also be stored in the requestStore until the request is over, when it will be removed from the store in the EndResponseMiddleware.

You can retrieve a request context using the getRequestContext method with its key. Each context key is made by generating a uuid (v4), it is then attach on the ctx.state.requestId.

#
typescript
1const requestStore = new Map<string, ContextWithState>();
2const getRequestContext = (key: string) => requestStore.get(key);
4type RequestState<Entity extends GenericEntity = GenericEntity> = {
5 requestId: string;
6 requestContext: RequestContext<Entity>;
7 queryRunner: QueryRunner;
8};
9type ContextWithState = Context<any, EntityRouteState>;
Prev
Subresources Playground
Next
Middlewares