import { ApiOutputTypes } from './types/core.types.js'; import mongoose from 'mongoose'; import { Request, Response } from 'express'; import { DbsWithoutCtx } from './db.js'; import { ThrowErrorTypeSafe } from './error.js'; import { type ModelTypes } from './cache/dbs/index.generated.js'; /** `ctx` stores contextual informations about a request like user permissions, paginationData...etc * * ctx is scoped to a request and is carried along during all the request lifetime in the backend * * That's why `ctx` is used everywhere as the first parameter of 99.9% backend functions * */ export declare class CtxClass { /** default database. @example ```ctx.db.user.getById()``` */ db: { [x: string]: import("./index.js").DaoMethodsBaseWithoutCtxMongo; } & import("./databases/mongo/initMongoDb.js").ModelAdditionalFields; /** Databases if you have multiple databases @example ```ctx.dbs.myDb.user.getById()``` */ dbs: DbsWithoutCtx; /** TODO not actually working Number; 1 or 2 => verbosity */ debugMode: boolean; /** dev, prod, preprod... */ env: string; /** used to cimple check if it's a ctx for sure and not another object */ isCtx: true; /** Public Ctx means user is not logged */ isPublic: boolean; /** SystemCtx is used by developper 🚸 to bypass all security 🚸 when making a request */ isSystem: boolean; /** Used when no db calls needs to be made but all the process is to be ran */ simulateRequest: boolean; /** Used to store */ transactionSession?: mongoose.mongo.ClientSession; /** actual userId or a public | system generic id */ _id: string; /** The actual user role as given by the JWT token */ role: GD['role'] | TechnicalRoles; /** All valid authentication methods used by the user */ authenticationMethod: Array; /** Used to define through witch platform the ctx is connected, to be overrided by app */ platform: string; /** user stored in the cache */ _user?: ModelTypes['user']; /** The actual user permissions fields as given by the JWT token */ permissions: UserPermissionFields; /** This is to store the type that will be used in all the for clauses in the app, since in a for you have to provide role. This is not the ideal place to put it, toDo */ permissionsWithoutRolePermissions: UserPermissionsWithoutRolePerms; /** Api request informations */ api: { params: Record; body: Record; originalUrl: string; query: Record; ipAdress?: string; req: Request; res: Response; /** This is to configure the output type of the api request. Default: json */ outputType?: ((req: Record, ctx: Ctx) => ApiOutputTypes) | ApiOutputTypes; }; GM: typeof this; /** */ isFromGeneratedDbApi: boolean; /** NOTE: req object is modified by the constructor */ constructor(ctx: Ctx); constructor(ctxUser: CtxUser, req?: Request, res?: Response, previousCtx?: Ctx); /** Cleanly throw an error, associating all ctx infos to it (user._id, aplication, service name, route...) */ error: ThrowErrorTypeSafe; /** Use that to change the role used by the actual Ctx, other user permission related fields may be changed at the same time, that's why there is the param fieldsToMergeWithCtxUser */ useRole(role: Ctx['role'], permissionsOrUser?: Partial, /** default: true; If false, will return the created ctx without modifying the actual one */ modifyActualCtx?: boolean): this; addWarning(): Promise<{ nbWarnings: number; nbWarningLeftBeforeBan: number; }>; banUser(): Promise; system(): this; /** Check if user has this role * system will always return true or false depending on * systemAlwaysReturnTrue value (default, false) */ hasRole(role: Ctx['role'], systemAlwaysReturnTrue?: boolean): boolean; toString(): string; /** Returns the ctx user like it is in database with the permission 'system' (⚠️ with all fields including password and sensitive fields ⚠️) */ getUser({ refreshCache, errorIfNotSet }?: { refreshCache?: boolean; errorIfNotSet?: boolean; }): Promise; getUserMinimal(): { _id: string; role: "public" | "system"; premissions: UserPermissionFields; }; clearUserCache(): void; /** This is to check if the user Id a real logged userId or a generated one corresponding to public or system */ isAnonymousUser(): boolean; clone>(override?: T): Ctx & T; /** Build and return or modify Ctx from user fields and role */ fromUser(role: Ctx['role'], user: Partial, modifyActualCtx?: boolean): this; } declare global { interface Ctx extends CtxClass { } interface CtxUser { _id: Ctx['_id']; role: Ctx['role']; permissions: Partial; platform?: Ctx['platform']; user?: Ctx['_user']; authenticationMethod?: Ctx['authenticationMethod']; } interface SystemCtx extends Ctx { isSystem: true; isPublic: false; } interface PublicCtx extends Ctx { isSystem: false; isPublic: true; } } export declare const systemRole = "system"; export type SystemRole = typeof systemRole; export declare const publicRole = "public"; export type PublicRole = typeof publicRole; export declare const technicalRoles: readonly ["system", "public"]; export type TechnicalRoles = typeof technicalRoles[number]; export declare const systemUserId = "777fffffffffffffffffffff"; export declare const publicUserId = "000fffffffffffffffffffff"; export declare const authenticationMethod: readonly ["biometricAuthToken", "pincode", "2FA"]; export type AuthenticationMethod = typeof authenticationMethod[number]; /** This is to check if the user Id a real logged userId or a generated one corresponding to public or system */ export declare const isAnonymousUser: (id: any) => boolean; /** * Use it when you don't already have access to a ctx from which to do ctx.GM */ export declare function newSystemCtx(): SystemCtx; /** * Use it when you want anonymous user to have a ctx */ export declare function newPublicCtx(): PublicCtx; export declare const ctx: Ctx;