Skip to content

Commit

Permalink
feat: add default IdGeneraor
Browse files Browse the repository at this point in the history
  • Loading branch information
Papooch committed Sep 28, 2021
1 parent ea05e03 commit e4214a3
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
node_modules
tmp
tmp
test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ interface ClsMiddlewareOptions {
# Request ID
Because of a shared storage, CLS is an ideal tool for tracking request (correlation) ids for the purpose of logging. This package provides an option to automatically generate request ids in the middleware and also provides a way to provide a custom ID generator function.
Because of a shared storage, CLS is an ideal tool for tracking request (correlation) ids for the purpose of logging. This package provides an option to automatically generate request ids in the middleware, if you pass `{ generateId: true }` to the middleware options. By default, the generated is a string based on `Math.random()`, but you can provide a custom function in the `idGenerator` option.
This function receives the `Request` as the first parameter, which can be used in the generation process.
Expand Down
3 changes: 2 additions & 1 deletion src/lib/cls.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export class ClsMiddlewareOptions {
/**
* the function to generate request ids inside the middleware
*/
idGenerator?: (req: Request) => string | Promise<string>;
idGenerator?: (req: Request) => string | Promise<string> = () =>
Math.random().toString(36).slice(-10);

/**
* Whether to store the Request object to the cls
Expand Down

0 comments on commit e4214a3

Please sign in to comment.