{"_id":"@alexy4744/nestjs-request-context","_rev":"6-9426e5fc523c7e6ff9e230a5c4155045","name":"@alexy4744/nestjs-request-context","dist-tags":{"latest":"1.1.0"},"versions":{"1.0.0":{"name":"@alexy4744/nestjs-request-context","version":"1.0.0","repository":{"type":"git","url":"git+https://github.com/alexy4744/packages.git"},"license":"MIT","bugs":{"url":"https://github.com/alexy4744/packages/issues"},"homepage":"https://github.com/alexy4744/packages#readme","engines":{"node":">=16.0.0"},"peerDependencies":{"@nestjs/common":"^7.0.0","@nestjs/core":"^7.0.0","reflect-metadata":"^0.1.12","rxjs":"^6.0.0"},"main":"./src/index.js","typings":"./src/index.d.ts","description":"Keep track of request-level data in NestJS using AsyncLocalStorage","_id":"@alexy4744/nestjs-request-context@1.0.0","_nodeVersion":"16.0.0","_npmVersion":"7.10.0","dist":{"integrity":"sha512-sXr70w37W7lBmOV+xy3QnlhszFuVyuCXNZbEA+Ids6WTYJ/XeGyZONIyDmODUro6Hg/UsgNf0NbQ3UCpmJ1udg==","shasum":"e6b3d0c42b2b76c4073cfbd6f367dccc1d5abdef","tarball":"https://registry.npmjs.org/@alexy4744/nestjs-request-context/-/nestjs-request-context-1.0.0.tgz","fileCount":18,"unpackedSize":10054,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg5iCUCRA9TVsSAnZWagAAmQMP/Rzph6f5iGQzZBMvotJ6\n8EuEaip/hwzVYkidpcrwCPa38EDxin9aTHDdSFT26ksGWKIrjSe033kyK1fR\nb9UtD8ntQAHEdtlimRNcgNJie6G7XNfo7xZvM1u2GUZUm8ZX4jx/8dbjd5rP\nva8ORSx5kS+9tH/OE0kiRNXMznrmSebz9QOavj+mL3Ng+G9iyGRVVu9mFaRa\neH15kEBVGCVAm9md7wOkr0mAjfzH7QpVs9H/vPra5BkR/WkIT2iPxq+TJkBu\nj87sNpdNT0v8Upiu9xO9er7g1RWSEcceymYjQxGnRMu496h6e61IMjuzDEdc\nQnc3m5VpPspCYvvuzKr/YrKARFIJFiSEu/bqUgmdEDY3iTjvYTc0kqTf3MUT\nUvoQQlc+cSnUqN0s1KE5bCBi8lfP3PeeQhzbU+24sgCB/T6GH8WX+QbjfU4g\n81DpfgiN4JLSwoE+xmdg2zKQZKvWJprueLWrPZqHXngJ1IYLO+KW3bG4ssSS\nBINjzjFxamhQanTLLlG2Y9Ls224jl5554FoKdDN7RsIyUNKNP/PpWLV/j/ub\nlh2HxBLmr8Yn/YnUACagocu67HfIMQ3QJdLU4pzH0NpcI1kPMylKi32DFgwb\nERQ2WT1I896jHgj3vFgv5KnTSvrrRpdrgSLrTQtdsJaJ/3UN2WK9utCGBTid\nyow6\r\n=mEzW\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIF0Ty+kKolaObbVX4tZRTPmXzC3ve+lsddMKod/xjqUtAiEA7RAYNjrX0GPyDggsr9RSH57G1KIU8xVMSh5UCK4F5Eg="}]},"_npmUser":{"name":"~alexy4744","email":"alexy4744@gmail.com"},"directories":{},"maintainers":[{"name":"~alexy4744","email":"alexy4744@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-request-context_1.0.0_1625694356112_0.6929088753868942"},"_hasShrinkwrap":false},"1.0.1":{"name":"@alexy4744/nestjs-request-context","version":"1.0.1","description":"Keep track of request-level data in NestJS using AsyncLocalStorage","keywords":["nestjs","middleware","async-local-storage","request","context"],"license":"MIT","repository":{"type":"git","url":"git+https://github.com/alexy4744/packages.git"},"bugs":{"url":"https://github.com/alexy4744/packages/issues"},"homepage":"https://github.com/alexy4744/packages#readme","engines":{"node":">=16.0.0"},"peerDependencies":{"@nestjs/common":"^7.0.0","@nestjs/core":"^7.0.0","reflect-metadata":"^0.1.12","rxjs":"^6.0.0"},"main":"./src/index.js","typings":"./src/index.d.ts","_id":"@alexy4744/nestjs-request-context@1.0.1","_nodeVersion":"16.0.0","_npmVersion":"7.10.0","dist":{"integrity":"sha512-RS01lDn8ylBhgeK9fE2izoLu4h3RffCtUHW8H2bd1+dcw8h2fbxvjZGE84ydFS7Ljz1hSuHN/IKN1e1PUMOc2w==","shasum":"aab030a25ba5b410376a07f19248a621f7afc644","tarball":"https://registry.npmjs.org/@alexy4744/nestjs-request-context/-/nestjs-request-context-1.0.1.tgz","fileCount":18,"unpackedSize":10384,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg5z5QCRA9TVsSAnZWagAA5SwP/RCLPTl1bdhyuU7KX9FQ\nkZqWeK78HJrZw6UI9zLOa8jkkXOO3yK+41D8+NkQ5WKNzhB16W86yNsN95e+\nFyz8rxeSTsTOdJ87R3SFZC3/HG5SwKQGTu0EUkfATj/xlZRQZLOyRXMo5qXf\nZjiAXDMiEJarAFJqg/ijOxugUfuafVz1P8KPk8rwCKyG/JSI+Fg5mt1ztMGQ\n+gcNbsF5ng9890M9UyqAzIqtdYStQJQhBQTmvxfNLJmsbGpksUOvREBh/AgT\nDIwrEHEoHNdqarPqBuDsz3yX1jgl13eIIMIYfoCFm3hU5/q0yf4wCrOS7vCS\nvLEbjHreZBBOkXjE9b+QbrwqKSXnXgzPhWQkaLLvoGp47MC3afiwVKE3slrN\ngBZC0pdRH3nbcB35vuyapS9G2ANtbRGv8PUKhnRsgcrYHledZmGf44newitu\n6c3T9nfhJkm85HwnKwjd8IgpInKP/VyJmP4UnAoCz40xTKLWoXnX1skXA9zC\nVkdv8mip6y9yHgLCbxk8M4bfjGHv0p9cU5If8tR/JtQk4tcw9bzKuZzl0Zev\n0Mitfa9P2413YcD/f4EruIZOHZEpIwnaqAyN5yHb8V6hla6TGOqS8i9TITkD\n2Xe+otM+KbuEVMBzQ0Fgb1gCCn3rIvlMoBgVAfeWt2NSGd9DgCKoDsL+Lj/N\nkpnP\r\n=3kNu\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCICXWW3l0Cn91y+lWxICGjeQfna01SjecCgGrH5w5gSdIAiEAgPvPuZDgXFuSYb0ltv0umumKXrG9j50mL7wG9IlUoDA="}]},"_npmUser":{"name":"~alexy4744","email":"alexy4744@gmail.com"},"directories":{},"maintainers":[{"name":"~alexy4744","email":"alexy4744@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-request-context_1.0.1_1625767503964_0.7626588433369226"},"_hasShrinkwrap":false},"1.0.2":{"name":"@alexy4744/nestjs-request-context","version":"1.0.2","description":"Keep track of request-level data in NestJS using AsyncLocalStorage","keywords":["nestjs","middleware","async-local-storage","request","context"],"license":"MIT","repository":{"type":"git","url":"git+https://github.com/alexy4744/packages.git"},"bugs":{"url":"https://github.com/alexy4744/packages/issues"},"homepage":"https://github.com/alexy4744/packages#readme","engines":{"node":">=16.0.0"},"peerDependencies":{"@nestjs/common":"^7.0.0","@nestjs/core":"^7.0.0","reflect-metadata":"^0.1.12","rxjs":"^6.0.0"},"main":"./src/index.js","typings":"./src/index.d.ts","_id":"@alexy4744/nestjs-request-context@1.0.2","_nodeVersion":"16.0.0","_npmVersion":"7.10.0","dist":{"integrity":"sha512-bqdbebDQTji2/cfz762fekMdx/7bjJKulG6p6gDE0idxjkMpm3py4JE3R3F/RFg8UaqeeJYTEuvCx/ebWZj0qQ==","shasum":"7cf3bb399ef2a3664844f126abf9e47caa645bbc","tarball":"https://registry.npmjs.org/@alexy4744/nestjs-request-context/-/nestjs-request-context-1.0.2.tgz","fileCount":18,"unpackedSize":10500,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg50BhCRA9TVsSAnZWagAANOwP/jZ2gqDkRvtS3qk/j9sL\nQtv6EFcROXVFcfL7bX5qsTHEqCFU3r7mz2XRyV3evVFf/krbvGF/PsOmSE56\n5fx30u3BwK2O+YwnXVZNl++Z+WCanqsX6BY3W9kC+VA71NJb0tMbtBwvL3uU\nXOr9zUZKn5VST9B9tPOEVuJEKJCbDhVcj/J8tds/xqEHeI5Uy1h12SluI1DY\n82DIvw26SnkEzmc2EsctmE6T614UrVpGJTcKwdTeCfCyvUAg7wZC/MlU946N\n7A2WKVsYL+iUB+igXTNOhUoCCmo3voxqA0k6slPPKCbUon2GcbHu0y6+3pT5\n/iFIx1BClIzJWZBAvcnXDdCwcW/JBeUD+LBUHPiL0dkYISVInMch965JR4/2\nZeze7MJmC2FVs+o8PL6W+FCdbT1oW3s2sIfCyL0L2Yai6V5AWimWGEJCze25\ngCli4/dkKR8Bo6um7H4dIMWO/+xClp61CwYfaEaFsAXX9qBZyxOflHsjxoHN\nH6b50N8snnbB4QNAC/C/tT8Ix0fWhi4Po8Mqrv4E89eZB6qjuT8ezQ7DqODX\n3Pcpx3TBrFAc1/ZAfhx51J/3R3VIjPZYGAOc8XPob0H8ZmVU3KazbfiHJgF5\nd+Xs2+KK4+T52mZsi1lby4LkYVaEr01iwEsle5A28Y+zrfs9GQnQHQoDj1cH\nFMPm\r\n=gcBn\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC3mOcv7LXsWcIVUnx4sGqOEVNfV9vHDjm5EsRT/sIjOQIgR4d+DMdG5IZ24Yv4+5KrebIQNwJzKHMRlNf7eCryJ7E="}]},"_npmUser":{"name":"~alexy4744","email":"alexy4744@gmail.com"},"directories":{},"maintainers":[{"name":"~alexy4744","email":"alexy4744@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-request-context_1.0.2_1625768033516_0.99170391956518"},"_hasShrinkwrap":false},"1.0.3":{"name":"@alexy4744/nestjs-request-context","version":"1.0.3","description":"Keep track of request-level data in NestJS using AsyncLocalStorage","keywords":["nestjs","middleware","async-local-storage","request","context"],"license":"MIT","repository":{"type":"git","url":"git+https://github.com/alexy4744/packages.git"},"bugs":{"url":"https://github.com/alexy4744/packages/issues"},"homepage":"https://github.com/alexy4744/packages#readme","engines":{"node":">=16.0.0"},"peerDependencies":{"@nestjs/common":"^7.0.0","@nestjs/core":"^7.0.0","reflect-metadata":"^0.1.12","rxjs":"^6.0.0"},"main":"./src/index.js","typings":"./src/index.d.ts","_id":"@alexy4744/nestjs-request-context@1.0.3","_nodeVersion":"16.0.0","_npmVersion":"7.10.0","dist":{"integrity":"sha512-CfpWSJwBM6TQJ0xltfjAaYD5SAasQXrv80OY12mFJMFVCdp3M+VYZ+KPLDXVc4qKALdDHTqANASnZHGNj8lUeQ==","shasum":"0617f5ae14f78d49404e0a24cbcad0d8db44eee6","tarball":"https://registry.npmjs.org/@alexy4744/nestjs-request-context/-/nestjs-request-context-1.0.3.tgz","fileCount":18,"unpackedSize":12173,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCwjn3WEquvmk5SJNz70PuvFrqF9480DZGkPPYrnivs/AIgesJxVK/cEoavOMXxRxd37/Q6hK6zOnu1VjcxLkOXINE="}]},"_npmUser":{"name":"~alexy4744","email":"alexy4744@gmail.com"},"directories":{},"maintainers":[{"name":"~alexy4744","email":"alexy4744@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-request-context_1.0.3_1635102336632_0.05072810359352231"},"_hasShrinkwrap":false},"1.1.0":{"name":"@alexy4744/nestjs-request-context","version":"1.1.0","description":"Keep track of request-level data in NestJS using AsyncLocalStorage","keywords":["nestjs","middleware","async-local-storage","request","context"],"license":"MIT","repository":{"type":"git","url":"git+https://github.com/alexy4744/packages.git"},"bugs":{"url":"https://github.com/alexy4744/packages/issues"},"homepage":"https://github.com/alexy4744/packages#readme","engines":{"node":">=16.0.0"},"peerDependencies":{"@nestjs/common":"^7.0.0","@nestjs/core":"^7.0.0","reflect-metadata":"^0.1.12","rxjs":"^6.0.0"},"main":"./src/index.js","typings":"./src/index.d.ts","_id":"@alexy4744/nestjs-request-context@1.1.0","_nodeVersion":"16.0.0","_npmVersion":"7.10.0","dist":{"integrity":"sha512-bOGfAyFLK+5z61Wv+aifvUw2UrpfLYlXD5de8ttgShF4S5SlMOSw9h6vy93LiRlsH2NE6kA9pAm1k4YuGBAPuw==","shasum":"ec078d49bbf244c0feceb951fdc92b8ee690891d","tarball":"https://registry.npmjs.org/@alexy4744/nestjs-request-context/-/nestjs-request-context-1.1.0.tgz","fileCount":21,"unpackedSize":14871,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhxJBECRA9TVsSAnZWagAA9OwP/jngpHBCrC1f2ieGEMT8\nPLuvyPZnFv9rHfECpTVfAHf6YCLZY3ZmxMGEBd03SQxE3X8Q1pvnjGNG8lTw\nTRwWZSZJIvRZXPn5TJ6crnijpv06XVN4wXfKcW1pAkpcTmIlQ6iqc5fMdCk5\nTvgLzgvDpWQThEwemjV16guynu9UW2yBYeLb+Ujj4Ip2sBN9GUtsKPl9ZP1Y\nRpqYjmN2opBKcNsjLK2C664xE7dc0va6RZgvFGFbvJc0Uvoq7NeLlZRnpz29\nJXHqmktpcHWTKYK3D8MJR4HMAMqPFvT1CgjraiPlMMXZffZr458w5/oCqbSY\nBnQJL5DAXTiO9xYa+3yAyhPDOZiqWUBriNUqRVkD+8ZZwfM6hJoAwOCyrZ+Z\nE9Jch7tnhgY42n0K2XqCWOrIPNB+SoGll9CmwW3mfFmHUGHEe8TPL4iYH1gb\n7vu5H76Hme84Kobt1iLCV23eNQ/sGR1CM2sFupO7W02hy1FPFMdtk32GQx3O\nf6DUBtiTKN6isIMW8t20DANAd1f0cLeXRhuNIHQAtlHNDogeWMzQwIzbELII\n7SKhPCp/2CqJdzKlISp1lp6oept+9OwxgyfL87D+g4/rK1dnSqsynEKwMygw\nQHRKOEceYNDirYxQTs2yOViIRlPAzvOvHP+kD1t+glJFEX8uVpgtBhF9bwb1\n1Aw1\r\n=BY5U\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC8FM6g9X4lOCh+t5fdPTB0stUIPCNkueAh0E/zzNdDQAIgBHXCkzv6mF8dvo5mtuneLF9s+7GGK739kmGTf3/jJUM="}]},"_npmUser":{"name":"~alexy4744","email":"alexy4744@gmail.com"},"directories":{},"maintainers":[{"name":"~alexy4744","email":"alexy4744@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-request-context_1.1.0_1635260326577_0.623217827604259"},"_hasShrinkwrap":false}},"time":{"created":"2021-07-07T21:45:55.759Z","1.0.0":"2021-07-07T21:45:56.226Z","modified":"2022-04-04T12:42:01.637Z","1.0.1":"2021-07-08T18:05:04.346Z","1.0.2":"2021-07-08T18:13:53.640Z","1.0.3":"2021-10-24T19:05:36.777Z","1.1.0":"2021-10-26T14:58:46.662Z"},"maintainers":[{"name":"~alexy4744","email":"alexy4744@gmail.com"}],"description":"Keep track of request-level data in NestJS using AsyncLocalStorage","homepage":"https://github.com/alexy4744/packages#readme","repository":{"type":"git","url":"git+https://github.com/alexy4744/packages.git"},"bugs":{"url":"https://github.com/alexy4744/packages/issues"},"license":"MIT","readme":"# NestJS Request Context\n\nKeep track of request-level data in NestJS using AsyncLocalStorage\n\n## Prerequisites\n - [Node.js](https://nodejs.org/en/) >= 16.0.0\n\n## Installation\n\n```bash\n$ npm install @alexy4744/nestjs-request-context\n```\n\n## Usage\n\n### RequestContext\n\nFirst, create a class that extends `RequestContext`. This class will hold your request-level data.\n\n```ts\nimport { BaseRequestContext } from \"@alexy4744/nestjs-request-context\";\n\nexport class RequestContext extends BaseRequestContext<RequestContext>() {\n  data?: string;\n}\n```\n\n### RequestContextMiddleware\n\nNext, apply `RequestContextMiddleware` as a global middleware and pass in our request context as a parameter:\n\n```ts\nimport { MiddlewareConsumer, Module, NestModule, RequestMethod } from \"@nestjs/common\";\n\nimport { RequestContextMiddleware } from \"@alexy4744/nestjs-request-context\";\n\nimport { RequestContext } from \"./request.context\";\n\n@Module()\nexport class AppModule implements NestModule {\n  configure(consumer: MiddlewareConsumer): void {\n    consumer.apply(RequestContextMiddleware(RequestContext)).forRoutes({\n      method: RequestMethod.ALL,\n      path: \"*\"\n    });\n  }\n}\n```\n\nYou can now access the request context by importing it anywhere in your application.\n\n```ts\nimport { Controller, Get } from \"@nestjs/common\";\n\nimport { RequestContext } from \"./request.context\";\n\n@Controller()\nexport class AppController {\n  @Get()\n  get(): RequestContext {\n    const store = RequestContext.getStore();\n\n    store.data = \"test\";\n\n    const data = RequestContext.getItem(\"data\");\n    \n    console.log(item); // \"test\"\n\n    return store;\n  }\n}\n```\n\nFor use cases where a middleware is not appropriate (i.e. NestJS microservices), you can use the `RequestContextGuard` or `RequestContextInterceptor`. Refer to the request lifecycle [here](https://docs.nestjs.com/faq/request-lifecycle#summary) to decide which one is the better option.\n\n### RequestContextGuard\n\nThe `RequestContextGuard` is ran after all middlewares, but before interceptors. This is the next best option for use cases where a middleware is not possible. `RequestContextGuard` uses the `.enterWith()` method to transition into the request context, making it a less-safer method. It is best to apply this guard before any other guards to minimize the risk of re-using the same context. The guard can be applied as any other global Nest guard.\n\n```ts\nimport { APP_GUARD, Module, Post } from \"@nestjs/common\";\n\nimport { RequestContextGuard } from \"@alexy4744/nestjs-request-context\";\n\nimport { RequestContext } from \"./request.context\";\n\n@Module({\n  ...\n  providers: [\n    {\n      provide: APP_GUARD,\n      useClass: RequestContextGuard(RequestContext),\n    },\n    ...\n  ]\n})\nexport class AppModule {}\n```\n\n### RequestContextInterceptor\n\nInterceptors always run after all middleware and guards. This may not be useful because the request context will be unaccessible in middleware and guards as it is too late in the request lifecycle. However, `RequestContextInterceptor` uses the `.run()` method to transition into the context, making it a safer option rather than using `RequestContextGuard`. The `RequestContextInterceptor` can be applied as any other Nest interceptor.\n\n```ts\nimport { Controller, Post, Query, UseInterceptors } from \"@nestjs/common\";\n\nimport { RequestContextInterceptor } from \"@alexy4744/nestjs-request-context\";\n\nimport { RequestContext } from \"./request.context\";\n\n@Controller()\nexport class AppController {\n  @Get()\n  @UseInterceptors(RequestContextInterceptor(RequestContext))\n  get(): RequestContext | undefined {\n    const store = RequestContext.getStore();\n\n    store.data = \"test\";\n\n    const data = RequestContext.getItem(\"data\");\n    \n    console.log(item); // \"test\"\n\n    return store;\n  }\n}\n```\n\n## Development\n\n```bash\n# Run e2e test\n$ nx e2e nestjs-request-context-e2e\n```\n\n```bash\n# Update version\n$ nx version nestjs-request-context\n```\n\n```bash\n# Build the project\n$ nx build nestjs-request-context\n```\n\n```bash\n# Publish new version on GitHub\n$ git push --follow-tags origin master\n```\n\n```bash\n# Publish new version on NPM\n$ npm publish ./dist/packages/nestjs-request-context --access=public\n```\n","readmeFilename":"README.md","keywords":["nestjs","middleware","async-local-storage","request","context"]}