{"_id":"@appstellar/nestjs-audit","_rev":"2-65d8ed47bfb130a902b0c7e7669510e2","name":"@appstellar/nestjs-audit","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@appstellar/nestjs-audit","version":"1.0.0","description":"A NestJS module that helps you keep audit data in different storages for your platform.","main":"dist/index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1","build":"rm -rf ./dist && tsc","prepublish":"npm run build"},"repository":{"type":"git","url":"git+https://github.com/appstellar-team/nestjs-audit.git"},"keywords":["NestJS","Audit","Logger","MongoDB","AWS"],"author":{"name":"Erudit Morina"},"license":"MIT","bugs":{"url":"https://github.com/appstellar-team/nestjs-audit/issues"},"homepage":"https://github.com/appstellar-team/nestjs-audit#readme","dependencies":{"@aws-sdk/client-sns":"^3.391.0","mongoose":"^7.4.3","rxjs":"^7.8.1"},"devDependencies":{"@nestjs/common":"^10.1.3","@nestjs/core":"^10.1.3","@types/node":"^20.5.0","@typescript-eslint/eslint-plugin":"^6.3.0","@typescript-eslint/parser":"^6.3.0","eslint":"^8.47.0","prettier":"^3.0.1","typescript":"^5.1.6"},"peerDependencies":{"@nestjs/common":"^10.0.0","@nestjs/core":"^10.0.0"},"types":"./dist/index.d.ts","gitHead":"682e8d15c719f24b95c83d658555ade268c29329","_id":"@appstellar/nestjs-audit@1.0.0","_nodeVersion":"20.3.1","_npmVersion":"9.6.7","dist":{"integrity":"sha512-Qg6uJop83iaIZOsEqhJLmiUGpMRhphTDstiRbYzWnGxVYtBGABIykJDV3p/JJyNnExWFsaaW5cxyJJjQFkR31A==","shasum":"d9d3b5434636fc012080f3dfd70538478d741ca3","tarball":"https://registry.npmjs.org/@appstellar/nestjs-audit/-/nestjs-audit-1.0.0.tgz","fileCount":86,"unpackedSize":70444,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCGHmnQuamJMLFQXMeN5rPUmawAg1ullj02oysi5pRfRQIgK3OS0YVU3rE6LrqgRzsbrDX6FwU2YUbHlKefZ1UhNLM="}]},"_npmUser":{"name":"erudit","email":"erudit.morina@gmail.com"},"directories":{},"maintainers":[{"name":"erudit","email":"erudit.morina@gmail.com"},{"name":"rllalloshi","email":"rozi@appstellar.io"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-audit_1.0.0_1692619701734_0.089111389400643"},"_hasShrinkwrap":false}},"time":{"created":"2023-08-21T12:08:21.605Z","1.0.0":"2023-08-21T12:08:21.961Z","modified":"2023-12-27T08:34:06.254Z"},"maintainers":[{"email":"tali@appstellar.io","name":"mehmetalishaqiri"},{"email":"rozi@appstellar.io","name":"rllalloshi"}],"description":"A NestJS module that helps you keep audit data in different storages for your platform.","homepage":"https://github.com/appstellar-team/nestjs-audit#readme","keywords":["NestJS","Audit","Logger","MongoDB","AWS"],"repository":{"type":"git","url":"git+https://github.com/appstellar-team/nestjs-audit.git"},"author":{"name":"Erudit Morina"},"bugs":{"url":"https://github.com/appstellar-team/nestjs-audit/issues"},"license":"MIT","readme":"# nestjs-audit\n\nAn audit module for Nest framework to keep audit data for web requests.\n\n## Installation\n\nNpm\n\n```bash\nnpm install @appstellar/nestjs-audit\n```\n\nYarn\n\n```bash\nyarn add @appstellar/nestjs-audit\n```\n\n## Getting Started\n\nFirst, let's register the nestjs-audit in `app.module.ts` by including it in imports.\n\n```ts\n// app.module.ts\nimport { Module } from '@nestjs/common';\nimport { AuditModule } from '@appstellar/nestjs-audit';\n\n@Module({\n  imports: [AuditModule],\n})\nexport class AppModule {}\n```\n\nThe following configurations can be set on the app module level:\n\n- set transports\n- enable audit data for failed requests\n  and\n- provide a callback that returns the user (actor) who is performing requests\n\nEach service, controller or route that is part of that module will then inherit the same configurations and this way we avoid duplicating code.\n\nTo set module level configurations, pass the needed options while importing the module:\n\n```ts\n// app.module.ts\nimport { Module } from '@nestjs/common';\nimport { AuditModule, TransportMethods } from '@appstellar/nestjs-audit';\n// only if sns transport is needed\nimport { SNSClient } from '@aws-sdk/client-sns';\n\n@Module({\n  imports: [\n    AuditModule.forRoot({\n      transports: [\n        // only one of three supported transport methods is required, but multiple transports can be used as well\n        { name: TransportMethods.CONSOLE },\n        {\n          name: TransportMethods.MONGOOSE,\n          options: { connectionString: '<your-database-connection-url>' },\n        },\n        {\n          name: TransportMethods.SNS,\n          options: { client: new SNSClient(), snsTopicArn: '<sns-topic-arn>' },\n        },\n      ],\n      // optional (defaults to false)\n      logErrors: true,\n      // optional\n      getUserId: (req) => '<returned-user-id-from-req>',\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\n---\n\nAfter properly importing the module, we can inject our `AuditService` anywhere that's needed.\nThis can be done to set the audit configurations in service level so the same configurations will be inherited by controllers that have the specific service injected.\n\n```ts\n// app.service.ts\nimport { AuditService, TransportMethods } from '@appstellar/nestjs-audit';\n\n@Injectable()\nclass SomeService {\n  constructor(private readonly audit: AuditService) {\n    // examples of setting audit options in service level\n    audit.addTransport(TransportMethods.CONSOLE);\n    audit.setUserIdCallback((req) => req.headers.user.id);\n    audit.logErrors = true;\n  }\n}\n```\n\n## Decorator Usage\n\nIn order to enable auditing for requests, `@Audit()` decorator has to be used in the controller level for routes that we need to keep audit data for.\n\n```ts\n// app.controller.ts\nimport { Audit } from '@appstellar/nestjs-audit';\n\n// can be passed to each route specifically if there are routes that we don't need to keep audit for\n@Audit()\n@Controller()\nexport class SomeController {\n  constructor(private readonly someService: SomeService) {}\n\n  @Get()\n  // @Audit()\n  getData() {\n    return this.someService.getData();\n  }\n}\n```\n\nIf audit options are not set in module or service level, it can be done for each route specifically by passing audit parameters in the decorator:\n\n```ts\n// app.controller.ts\nimport { Action, Audit } from '@appstellar/nestjs-audit';\n\n@Get()\n@Audit({\n  // all params are optional\n  // action defaults to `req.method` if not set\n  action: Action.READ,\n  getUserId: (req) => '<returned-user-id-from-req>',\n  getResponseObjectId: (req) => '<returned-object-id-from-req>',\n  entity: '<entity-name-which-request-is-performed-on>',\n})\ngetData() {\n  // some code\n}\n```\n\n## Contributing\n\nPull requests are welcomed. For major changes, please open an issue first to discuss what you would like to change.\n\n## License\n\nnestjs-audit is [MIT licensed](LICENSE).\n","readmeFilename":"README.md"}