Skip to content

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,
    queryOptions?: 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.

async $say_hello(dto: SayHelloDto, ctx: CrudContext, inheritance?: any) {
   const user = ctx.user;
}
And in hook functions.
async afterControllerHook(res: any, ctx: CrudContext) {
    return Promise.resolve();
}

Caching

You can store data in the context and it will be available globally through your request.

async defineCRUDAbility(can, cannot, ctx) {
   ctx.my_prop = { message: "hello world"};
}

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.

import { CookieToSet } from "@eicrud/core/crud";
ctx.setCookies = ctx.setCookies || {};
const newCookie: CookieToSet = {
    value: 'I love cookies',
    maxAge: 60*30, // 30min
    path: '/',
}
ctx.setCookies['my-cookie'] = newCookie;

Comments