-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): Add @context decorator
- Add documentation for @Locals, @context, @Session - Update LogIncomingRequestMiddleware. - RequestContext now create RequestLogger with the right options.
- Loading branch information
Showing
25 changed files
with
468 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {ParamTypes} from "../../models/ParamTypes"; | ||
import {UseFilter} from "./useFilter"; | ||
import {mapParamsOptions} from "./utils/mapParamsOptions"; | ||
|
||
/** | ||
* Context decorator return the @@RequestContext@@ 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 @@RequestContext@@ inherit from Map class. So you can store any information with. | ||
* ::: | ||
* | ||
* #### Example | ||
* | ||
* ```typescript | ||
* @Middleware() | ||
* class AuthTokenMiddleware { | ||
* use(@Req() request: Express.Request, @Context() context: RequestContext) { | ||
* if (!context.has('auth')){ | ||
* context.set('auth', new AuthToken(req)) | ||
* } | ||
* | ||
* try { | ||
* context.get('auth').claims() // check token | ||
* } catch(er){ | ||
* throw 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 | ||
* @returns {Function} | ||
*/ | ||
export function Context(expression: string): ParameterDecorator; | ||
export function Context(): ParameterDecorator; | ||
export function Context(...args: any[]): ParameterDecorator { | ||
const {expression, useType, useConverter = false, useValidation = false} = mapParamsOptions(args); | ||
|
||
return UseFilter(ParamTypes.CONTEXT, { | ||
expression, | ||
useType, | ||
useConverter, | ||
useValidation | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import {LocalsContainer} from "@tsed/di"; | ||
import {EndpointMetadata} from "./EndpointMetadata"; | ||
import {RequestLogger} from "./RequestLogger"; | ||
|
||
export interface IRequestContextOptions { | ||
id: string; | ||
logger: any; | ||
url: string; | ||
ignoreUrlPatterns?: any[]; | ||
} | ||
|
||
export class RequestContext extends Map<any, any> { | ||
/** | ||
* Request id generated by @@createMiddleware@@. | ||
* | ||
* ::: tip | ||
* By default Ts.ED generate uuid like that `uuidv4().replace(/-/gi, ""))`. | ||
* Dash are removed to simplify tracking logs in Kibana | ||
* ::: | ||
* | ||
* ::: tip | ||
* Request id can by customized by changing the server configuration. | ||
* | ||
* ```typescript | ||
* @ServerSettings({ | ||
* logger: { | ||
* reqIdBuilder: createUniqId // give your own id generator function | ||
* } | ||
* }) | ||
* class Server { | ||
* | ||
* } | ||
* ``` | ||
* ::: | ||
* | ||
*/ | ||
readonly id: string; | ||
/** | ||
* Date when request have been handled by the server. @@RequestLogger@@ use this date to log request duration. | ||
*/ | ||
readonly dateStart: Date = new Date(); | ||
/** | ||
* The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)` | ||
*/ | ||
readonly container = new LocalsContainer<any>(); | ||
/** | ||
* The current @@EndpointMetadata@@ resolved by Ts.ED during the request. | ||
*/ | ||
public endpoint: EndpointMetadata; | ||
/** | ||
* The data return by the previous endpoint if you use multiple handler on the same route. By default data is empty. | ||
*/ | ||
public data: any; | ||
/** | ||
* Logger attached to the context request. | ||
*/ | ||
readonly logger: RequestLogger; | ||
|
||
constructor({id, logger, url, ignoreUrlPatterns = []}: IRequestContextOptions) { | ||
super(); | ||
this.id = id; | ||
this.logger = new RequestLogger(logger, { | ||
id, | ||
startDate: this.dateStart, | ||
url, | ||
ignoreUrlPatterns | ||
}); | ||
} | ||
|
||
async destroy() { | ||
await this.container.destroy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.