Context
Each request in Eicrud is assigned a CrudContext
. It contains various information about the request and authenticated user.
export interface CrudContext {
user?: CrudUser,
userId?: string,
query?: any,
data?: any,
options?: CrudOptions,
getCurrentService?: () => CrudService<any>;
getHttpRequest?: () => any;
getHttpResponse?: () => any;
// ...
}
Availability
The CrudContext
is available in defineAbility functions.
import { CrudContext } from "@eicrud/core/crud";
//...
async defineCRUDAbility(can, cannot, ctx: CrudContext) {
// Define abilities for user
const userId = ctx.userId;
}
In command implementations.
And in hook functions.Caching
You can store data in the context and it will be available globally through your request.
Warning
Storing circular references in the CrudContext
will break your microservice configuration since ctx
is often serialized in dollar functions. You can store such objects in the _temp
property which is always deleted before serialization (it will be limited to the current ms).
Cookies
You can set cookies using the CrudContext
->setCookies
parameter and it will work in microservices configurations.