@tsed/common
Version:
A TypeScript Framework on top of Express
54 lines (53 loc) • 1.73 kB
TypeScript
import { PlatformContext } from "../domain/PlatformContext";
/**
* Context decorator return the @@PlatformContext@@ created by Ts.ED when request is handled by the server.
*
* It contains some information as following:
*
* - The request id,
* - The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)`,
* - The current @@EndpointMetadata@@ resolved by Ts.ED during the request,
* - The data return by the previous endpoint if you use multiple handler on the same route. By default data is empty.
*
* ::: tip
* The @@PlatformContext@@ inherit from Map class. So you can store any information with.
* :::
*
* #### Example
*
* ```typescript
* @Middleware()
* class AuthTokenMiddleware {
* use(@Req() request: Req, @Context() context: PlatformContext) {
* if (!context.has('auth')){
* context.set('auth', new AuthToken(request))
* }
*
* try {
* context.get('auth').claims() // check token
* } catch(er){
* throw new Forbidden("Access forbidden - Bad token")
* }
* }
* }
*
* @Controller('/')
* @UseBefore(AuthTokenMiddleware) // protect all routes for this controller
* class MyCtrl {
* @Get('/')
* get(@Context('auth') auth: AuthToken) {
* console.log('auth', auth);
* console.log('auth.accessToken', auth.accessToken);
* console.log('auth.idToken', auth.idToken);
* }
* }
* ```
*
* @param expression The path of the property to get.
* @decorator
* @operation
* @input
*/
export declare function Context(expression: string): ParameterDecorator;
export declare function Context(): ParameterDecorator;
export declare type Context = PlatformContext;