cqrs-boilerplate-code 1.0.6 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/common-infra/index.d.ts +0 -2
  2. package/dist/common-infra/index.js +0 -18
  3. package/dist/common-infra/index.js.map +1 -1
  4. package/dist/core-common/index.d.ts +0 -14
  5. package/dist/core-common/index.js +0 -30
  6. package/dist/core-common/index.js.map +1 -1
  7. package/dist/core-common/logger/index.d.ts +0 -3
  8. package/dist/core-common/logger/index.js +0 -19
  9. package/dist/core-common/logger/index.js.map +1 -1
  10. package/dist/index.d.ts +18 -2
  11. package/dist/index.js +18 -2
  12. package/dist/index.js.map +1 -1
  13. package/dist/tsconfig.build.tsbuildinfo +1 -1
  14. package/package.json +18 -2
  15. package/src/common-infra/common-infra.module.ts +0 -17
  16. package/src/common-infra/database/database.module.ts +0 -23
  17. package/src/common-infra/database/typeorm.config.ts +0 -18
  18. package/src/common-infra/index.ts +0 -2
  19. package/src/core-common/constant/app.constant.ts +0 -2
  20. package/src/core-common/core-common.module.ts +0 -19
  21. package/src/core-common/error/custom-error/already-exists.error.ts +0 -17
  22. package/src/core-common/error/custom-error/bad-request.error.ts +0 -8
  23. package/src/core-common/error/custom-error/conflict.error.ts +0 -17
  24. package/src/core-common/error/custom-error/custom-validation-error.ts +0 -9
  25. package/src/core-common/error/custom-error/forbidden.error.ts +0 -8
  26. package/src/core-common/error/custom-error/internal-server.error.ts +0 -11
  27. package/src/core-common/error/custom-error/not-found.error.ts +0 -8
  28. package/src/core-common/error/custom-error/service-unavailable.error.ts +0 -8
  29. package/src/core-common/error/custom-error/unauthorized.error.ts +0 -8
  30. package/src/core-common/error/custom-error/unprocess-entity.error.ts +0 -8
  31. package/src/core-common/error/custom-error/validation.error.ts +0 -8
  32. package/src/core-common/error/generic.error.ts +0 -31
  33. package/src/core-common/error/index.ts +0 -14
  34. package/src/core-common/index.ts +0 -17
  35. package/src/core-common/logger/index.ts +0 -3
  36. package/src/core-common/logger/logger.interface.ts +0 -8
  37. package/src/core-common/logger/logger.module.ts +0 -23
  38. package/src/core-common/logger/logger.service.ts +0 -175
  39. package/src/core-common/response-model/generic-error-response.model.ts +0 -44
  40. package/src/core-common/response-model/generic-success-response.model.ts +0 -25
  41. package/src/core-common/result-model/result.ts +0 -38
  42. package/src/index.ts +0 -2
package/package.json CHANGED
@@ -1,10 +1,19 @@
1
1
  {
2
2
  "name": "cqrs-boilerplate-code",
3
- "version": "1.0.6",
3
+ "version": "1.0.9",
4
4
  "description": "AI Platform Service API Gateway",
5
5
  "private": false,
6
6
  "license": "UNLICENSED",
7
7
  "author": "ankitdetroja",
8
+ "main": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "files": [
11
+ "dist",
12
+ "README.md",
13
+ "package.json",
14
+ "tsconfig.json",
15
+ "tsconfig.build.json"
16
+ ],
8
17
  "scripts": {
9
18
  "clean": "rimraf dist coverage",
10
19
  "build": "nest build",
@@ -15,6 +24,13 @@
15
24
  "format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\"",
16
25
  "lint": "eslint \"{src,tests}/**/*.ts\" --fix"
17
26
  },
27
+ "exports": {
28
+ ".": {
29
+ "require": "./dist/index.js",
30
+ "import": "./dist/index.js",
31
+ "types": "./dist/index.d.ts"
32
+ }
33
+ },
18
34
  "dependencies": {
19
35
  "@nestjs/common": "^11.1.3",
20
36
  "@nestjs/config": "^4.0.2",
@@ -64,4 +80,4 @@
64
80
  "tsc-files": "^1.1.4",
65
81
  "typescript": "^5.4.5"
66
82
  }
67
- }
83
+ }
@@ -1,17 +0,0 @@
1
- import { DynamicModule, Module } from "@nestjs/common";
2
- import { ConfigModule } from "@nestjs/config";
3
- import { DBModule } from "./database/database.module";
4
-
5
- @Module({})
6
- export class CommonInfraModule {
7
- static forRoot(): DynamicModule {
8
- return {
9
- module: CommonInfraModule,
10
- imports: [
11
- ConfigModule, // assumes consumer app config
12
- DBModule,
13
- ],
14
- exports: [DBModule],
15
- };
16
- }
17
- }
@@ -1,23 +0,0 @@
1
- // src/database/database.module.ts
2
- import { DynamicModule, Module } from "@nestjs/common";
3
- import { ConfigModule, ConfigService } from "@nestjs/config";
4
- import { TypeOrmModule } from "@nestjs/typeorm";
5
- import { typeOrmConfig } from "./typeorm.config";
6
-
7
- @Module({})
8
- export class DBModule {
9
- static forRoot(): DynamicModule {
10
- return {
11
- module: DBModule,
12
- imports: [
13
- TypeOrmModule.forRootAsync({
14
- imports: [ConfigModule],
15
- inject: [ConfigService],
16
- useFactory: (configService: ConfigService) =>
17
- typeOrmConfig(configService),
18
- }),
19
- ],
20
- exports: [TypeOrmModule],
21
- };
22
- }
23
- }
@@ -1,18 +0,0 @@
1
- import { TypeOrmModuleOptions } from "@nestjs/typeorm";
2
- import { ConfigService } from "@nestjs/config";
3
-
4
- export const typeOrmConfig = (
5
- configService: ConfigService,
6
- ): TypeOrmModuleOptions => ({
7
- type: "postgres",
8
- host: configService.get<string>("DB_HOST"),
9
- port: configService.get<number>("DB_PORT"),
10
- username: configService.get<string>("DB_USERNAME"),
11
- password: configService.get<string>("DB_PASSWORD"),
12
- database: configService.get<string>("DB_NAME"),
13
- autoLoadEntities: true,
14
- synchronize: configService.get<string>("NODE_ENV") !== "production",
15
- migrations: ["dist/migrations/*.js"],
16
- migrationsRun: true,
17
- logging: configService.get<string>("NODE_ENV") !== "production",
18
- });
@@ -1,2 +0,0 @@
1
- export * from "./common-infra.module";
2
- export * from "./database/typeorm.config";
@@ -1,2 +0,0 @@
1
- export const PORT = 3001;
2
- export const BASE_URL = '/api/retail'
@@ -1,19 +0,0 @@
1
- // src/core-common.module.ts
2
- import { DynamicModule, Global, Module } from "@nestjs/common";
3
- import { LoggerModule } from "./logger/logger.module";
4
-
5
- @Global()
6
- @Module({})
7
- export class CoreCommonModule {
8
- static forRoot(): DynamicModule {
9
- return {
10
- module: CoreCommonModule,
11
- imports: [
12
- LoggerModule.forRoot(),
13
- ],
14
- exports: [
15
- LoggerModule,
16
- ],
17
- };
18
- }
19
- }
@@ -1,17 +0,0 @@
1
- import { HttpStatus } from "@nestjs/common";
2
- import { GenericError } from "../generic.error";
3
-
4
- /**
5
- * Custom error class for representing the scenario where an entity already exists/Conflict with existing.
6
- * Extends the GenericError class.
7
- */
8
- export class AlreadyExistsError extends GenericError {
9
- /**
10
- * Constructor for the AlreadyExistsError class.
11
- * @param code - Short code associated with the error.
12
- * @param message - Detailed error information.
13
- */
14
- constructor(code: string, message: string) {
15
- super(code, message, HttpStatus.CONFLICT);
16
- }
17
- }
@@ -1,8 +0,0 @@
1
- import { HttpStatus } from "@nestjs/common";
2
- import { GenericError } from "../generic.error";
3
-
4
- export class BadRequestError extends GenericError {
5
- constructor(code: string, message: string) {
6
- super(code, message, HttpStatus.BAD_REQUEST);
7
- }
8
- }
@@ -1,17 +0,0 @@
1
- import { HttpStatus } from "@nestjs/common";
2
- import { GenericError } from "../generic.error";
3
-
4
- /**
5
- * Custom error class for representing conflict errors.
6
- * Extends the GenericError class.
7
- */
8
- export class ConflictError extends GenericError {
9
- /**
10
- * Constructor for the ConflictError class.
11
- * @param code - Short code associated with the error.
12
- * @param message - Detailed error information.
13
- */
14
- constructor(code: string, message: string) {
15
- super(code, message, HttpStatus.CONFLICT);
16
- }
17
- }
@@ -1,9 +0,0 @@
1
- import { BadRequestError } from "./bad-request.error";
2
-
3
- export class CustomValidationError extends BadRequestError {
4
- constructor(
5
- public validationErrors: any
6
- ) {
7
- super("ValidationError", "There are issues found in the input provided.");
8
- }
9
- }
@@ -1,8 +0,0 @@
1
- import { HttpStatus } from "@nestjs/common";
2
- import { GenericError } from "../generic.error";
3
-
4
- export class ForbiddenError extends GenericError {
5
- constructor(code: string, message: string) {
6
- super(code, message, HttpStatus.FORBIDDEN);
7
- }
8
- }
@@ -1,11 +0,0 @@
1
- import { HttpStatus } from "@nestjs/common";
2
- import { GenericError } from "../generic.error";
3
-
4
- export class InternalServerError extends GenericError {
5
- constructor(
6
- code = "INTERNAL_SERVER_ERROR",
7
- message = "An unexpected error occurred",
8
- ) {
9
- super(code, message, HttpStatus.INTERNAL_SERVER_ERROR);
10
- }
11
- }
@@ -1,8 +0,0 @@
1
- import { HttpStatus } from "@nestjs/common";
2
- import { GenericError } from "../generic.error";
3
-
4
- export class NotFoundError extends GenericError {
5
- constructor(code: string, message: string) {
6
- super(code, message, HttpStatus.NOT_FOUND);
7
- }
8
- }
@@ -1,8 +0,0 @@
1
- import { HttpStatus } from "@nestjs/common";
2
- import { GenericError } from "../generic.error";
3
-
4
- export class ServiceUnavailableError extends GenericError {
5
- constructor(code: string, message: string) {
6
- super(code, message, HttpStatus.SERVICE_UNAVAILABLE);
7
- }
8
- }
@@ -1,8 +0,0 @@
1
- import { HttpStatus } from "@nestjs/common";
2
- import { GenericError } from "../generic.error";
3
-
4
- export class UnauthorizedError extends GenericError {
5
- constructor(code: string, message: string) {
6
- super(code, message, HttpStatus.UNAUTHORIZED);
7
- }
8
- }
@@ -1,8 +0,0 @@
1
- import { HttpStatus } from "@nestjs/common";
2
- import { GenericError } from "../generic.error";
3
-
4
- export class UnprocessableEntityError extends GenericError {
5
- constructor(code: string, message: string) {
6
- super(code, message, HttpStatus.UNPROCESSABLE_ENTITY);
7
- }
8
- }
@@ -1,8 +0,0 @@
1
- import { HttpStatus } from "@nestjs/common";
2
- import { GenericError } from "../generic.error";
3
-
4
- export class ValidationError extends GenericError {
5
- constructor(code: string, message: string) {
6
- super(code, message, HttpStatus.UNPROCESSABLE_ENTITY);
7
- }
8
- }
@@ -1,31 +0,0 @@
1
- import { HttpStatus } from "@nestjs/common";
2
- import { ApiProperty } from "@nestjs/swagger";
3
-
4
- /**
5
- * Custom error class which needs to be returned from the lower layers to the higher calling layers
6
- */
7
- export class GenericError {
8
- @ApiProperty({ description: "Short error code" })
9
- public code: string;
10
-
11
- @ApiProperty({ description: "Detailed error description" })
12
- public message: string;
13
-
14
- @ApiProperty({ description: "Detailed error description" })
15
- public statusCode: HttpStatus;
16
-
17
- /**
18
- * Default constructor
19
- * @param code - Short error code e.g. "InvaliOperation", "ItemNotFound" etc
20
- * @param message - Detailed error message
21
- */
22
- constructor(
23
- code: string,
24
- message: string,
25
- statusCode: HttpStatus = HttpStatus.INTERNAL_SERVER_ERROR,
26
- ) {
27
- this.code = code;
28
- this.message = message;
29
- this.statusCode = statusCode;
30
- }
31
- }
@@ -1,14 +0,0 @@
1
- export * from "./generic.error";
2
- export * from "./custom-error/bad-request.error";
3
- export * from "./custom-error/unauthorized.error";
4
- export * from "./custom-error/forbidden.error";
5
- export * from "./custom-error/not-found.error";
6
- export * from "./custom-error/conflict.error";
7
- export * from "./custom-error/already-exists.error";
8
- export * from "./custom-error/validation.error";
9
- export * from "./custom-error/unprocess-entity.error";
10
- export * from "./custom-error/service-unavailable.error";
11
-
12
- export * from "./custom-error/internal-server.error";
13
- export * from "./custom-error/validation.error";
14
- export * from "./custom-error/custom-validation-error"
@@ -1,17 +0,0 @@
1
- export * from './constant/app.constant'
2
- export * from './error/custom-error/already-exists.error'
3
- export * from './error/custom-error/not-found.error'
4
- export * from './error/custom-error/validation.error'
5
- export * from './error/custom-error/internal-server.error'
6
- export * from './error/custom-error/unauthorized.error'
7
- export * from './error/custom-error/service-unavailable.error'
8
- export * from './error/generic.error'
9
- export * from './result-model/result'
10
-
11
- export * from './logger/index'
12
-
13
- export * from './response-model/generic-error-response.model'
14
- export * from './response-model/generic-success-response.model'
15
-
16
- export * from './core-common.module'
17
- export * from './constant/app.constant'
@@ -1,3 +0,0 @@
1
- export * from './logger.interface'
2
- export * from './logger.module'
3
- export * from './logger.service'
@@ -1,8 +0,0 @@
1
- // src/logger/logger.options.ts
2
- export interface LoggerModuleOptions {
3
- level?: string;
4
- deploymentEnv?: string;
5
- serviceVersion?: string;
6
- otelAgentHost?: string;
7
- overrideConsole?: boolean;
8
- }
@@ -1,23 +0,0 @@
1
- // src/logger/logger.module.ts
2
- import { DynamicModule, Module } from "@nestjs/common";
3
- import { LoggerService } from "./logger.service";
4
- import { LoggerModuleOptions } from "./logger.interface";
5
-
6
- export const LOGGER_OPTIONS = Symbol("LOGGER_OPTIONS");
7
-
8
- @Module({})
9
- export class LoggerModule {
10
- static forRoot(options: LoggerModuleOptions = {}): DynamicModule {
11
- return {
12
- module: LoggerModule,
13
- providers: [
14
- LoggerService,
15
- {
16
- provide: LOGGER_OPTIONS,
17
- useValue: options,
18
- },
19
- ],
20
- exports: [LoggerService],
21
- };
22
- }
23
- }
@@ -1,175 +0,0 @@
1
- import winston, { Logger } from "winston";
2
-
3
- /* ---------------------------------- Types --------------------------------- */
4
-
5
- enum SeverityText {
6
- INFO = "Information",
7
- DEBUG = "Debug",
8
- WARNING = "Warning",
9
- ERROR = "Error",
10
- }
11
-
12
- type LogMetadata = {
13
- ClassName?: string;
14
- MethodName?: string;
15
- };
16
-
17
- export type OrgContext = {
18
- orgId?: string;
19
- orgFid?: string;
20
- userId?: string;
21
- };
22
-
23
- /* --------------------------------- Service -------------------------------- */
24
-
25
- export class LoggerService {
26
- private readonly logger: Logger;
27
- private readonly deploymentEnv: string;
28
- private readonly hostImageVersion: string;
29
- private readonly otelAgentHost: string;
30
-
31
- constructor(logLevel: string = "debug") {
32
- this.deploymentEnv = "rls-dev";
33
- this.hostImageVersion = process.env.SERVICE_VERSION ?? "202501.1";
34
- this.otelAgentHost = process.env.OTEL_AGENT_HOST ?? "10.0.0.1";
35
- this.logger = this.createLogger(logLevel);
36
- this.overrideConsole();
37
- }
38
-
39
- /* ------------------------------- Public API ------------------------------- */
40
-
41
- log(message: string, metadata?: LogMetadata) {
42
- this.write("info", SeverityText.INFO, message, metadata);
43
- }
44
-
45
- debug(message: string, metadata?: LogMetadata) {
46
- this.write("debug", SeverityText.DEBUG, message, metadata);
47
- }
48
-
49
- warn(message: string, metadata?: LogMetadata) {
50
- this.write("warn", SeverityText.WARNING, message, metadata);
51
- }
52
-
53
- error(message: string, metadata?: LogMetadata, error?: Error) {
54
- this.write("error", SeverityText.ERROR, message, metadata, error);
55
- }
56
-
57
- /* ------------------------------ Logger Core ------------------------------ */
58
-
59
- private write(
60
- level: keyof Logger,
61
- severity: SeverityText,
62
- message: string,
63
- metadata: LogMetadata = {},
64
- error?: Error,
65
- ) {
66
- const body = {
67
- SeverityText: severity,
68
- ...metadata,
69
- ...(error && {
70
- ErrorMessage: error.message,
71
- StackTrace: error.stack,
72
- }),
73
- };
74
-
75
- this.logger[level](this.redactSecrets(message), body);
76
- }
77
-
78
- private createLogger(level: string): Logger {
79
- return winston.createLogger({
80
- level,
81
- levels: winston.config.npm.levels,
82
- format: winston.format.combine(
83
- winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
84
- winston.format.printf(({ level, message, timestamp, ...meta }) =>
85
- JSON.stringify({
86
- message: this.safeStringify(message),
87
- attributes: this.buildAttributes(level),
88
- timestamp: timestamp,
89
- ...this.cleanMeta(meta),
90
- }),
91
- ),
92
- ),
93
- transports: [new winston.transports.Console()],
94
- });
95
- }
96
-
97
- /* ------------------------------- Context -------------------------------- */
98
-
99
- private buildAttributes(level: string) {
100
- return {
101
- "service.image.version": this.hostImageVersion,
102
- "deployment.environment": this.deploymentEnv,
103
- "otel.agent.host": this.otelAgentHost,
104
- "service.log.level": level,
105
- };
106
- }
107
-
108
- /* ------------------------------- Helpers -------------------------------- */
109
-
110
- private cleanMeta(meta: Record<string, unknown>) {
111
- return Object.fromEntries(
112
- Object.entries(meta).filter(
113
- ([_, value]) => value !== undefined && value !== "None",
114
- ),
115
- );
116
- }
117
-
118
- private safeStringify(input: unknown): string {
119
- try {
120
- return typeof input === "string"
121
- ? input
122
- : JSON.stringify(input, this.circularReplacer());
123
- } catch {
124
- return "[Unserializable Object]";
125
- }
126
- }
127
-
128
- private circularReplacer() {
129
- const seen = new WeakSet();
130
- return (_: string, value: any) => {
131
- if (typeof value === "object" && value !== null) {
132
- if (seen.has(value)) return "[Circular]";
133
- seen.add(value);
134
- }
135
- return value;
136
- };
137
- }
138
-
139
- /* ----------------------------- Redaction -------------------------------- */
140
-
141
- private redactSecrets(message: string): string {
142
- const patterns = [
143
- {
144
- name: "JWT",
145
- regex: /eyJ[a-zA-Z0-9-_]+\.[a-zA-Z0-9-_]+\.[a-zA-Z0-9-_]+/g,
146
- visible: 10,
147
- },
148
- {
149
- name: "BEARER",
150
- regex: /Bearer\s+eyJ[A-Za-z0-9\-._~+/]+=*/gi,
151
- visible: 15,
152
- },
153
- { name: "API_KEY", regex: /\b[A-Za-z0-9]{32,}\b/g, visible: 8 },
154
- { name: "AWS_SECRET", regex: /AKIA[0-9A-Z]{16}/g, visible: 8 },
155
- ];
156
-
157
- return patterns.reduce((msg, { regex, visible, name }) => {
158
- return msg.replace(
159
- regex,
160
- (m) => `${m.slice(0, visible)}...[REDACTED_${name}]`,
161
- );
162
- }, message);
163
- }
164
-
165
- /* --------------------------- Console Override ---------------------------- */
166
-
167
- private overrideConsole() {
168
- const blocked = ["NodeSDK", "AuthToken"];
169
- console.log = (...args: unknown[]) => {
170
- const msg = args.map(String).join(" ");
171
- if (blocked.some((k) => msg.includes(k))) return;
172
- this.log(msg);
173
- };
174
- }
175
- }
@@ -1,44 +0,0 @@
1
- import { Result } from "../result-model/result";
2
-
3
- /**
4
- * Standard error response to be returned to the caller of the API.
5
- * This class represents the structure of an error response returned by the API
6
- * in case of failures.
7
- */
8
-
9
- export class ErrorDisplay {
10
- code: string;
11
- message: string;
12
- validationError?: any;
13
- }
14
- export class GenericErrorResponse {
15
- statusCode: number;
16
- success: boolean = false;
17
- error: ErrorDisplay;
18
- timestamp: string;
19
- data: any;
20
-
21
- constructor() {
22
- this.statusCode = 500; // Default value
23
- this.success = false;
24
- this.timestamp = new Date().toISOString(); // Default value
25
- }
26
- /**
27
- * Sets the properties from the Result object.
28
- * @param result - Result object instance returned from the controller method.
29
- * @param statusCode - HTTP status code.
30
- */
31
- initialize(
32
- result: Result<any>,
33
- statusCode: number,
34
- ) {
35
- this.statusCode = statusCode;
36
- this.error = {
37
- code: result.error?.code || "ERROR",
38
- message: result.error?.message || "An error occurred.",
39
- };
40
- this.data = result.data
41
- this.success = result.success || false;
42
- this.timestamp = result.timestamp || new Date().toISOString();
43
- }
44
- }
@@ -1,25 +0,0 @@
1
- import { Result } from "../result-model/result";
2
-
3
- /**
4
- * Standard response object to be returned to the caller of the API.
5
- * This class represents the structure of a success response returned by the API.
6
- * @typeparam T - The type of data included in the response.
7
- */
8
- export class GenericSuccessResponse<T> {
9
- statusCode: number;
10
- success: boolean;
11
- data: T;
12
- timestamp: string;
13
-
14
- /**
15
- * Sets the properties from the Result object.
16
- * @param result - Result object instance returned from the controller method.
17
- * @param statusCode - HTTP status code.
18
- */
19
- initialize(result: Result<T>, statusCode: number) {
20
- this.statusCode = statusCode;
21
- this.success = result.success;
22
- this.data = result.data;
23
- this.timestamp = new Date().toISOString();
24
- }
25
- }
@@ -1,38 +0,0 @@
1
- import { GenericError } from "../error/generic.error";
2
-
3
- export class Result<T> {
4
- success: boolean;
5
- data: T;
6
- error: GenericError;
7
- timestamp: string;
8
-
9
- public static success<T>(data: T): Result<T> {
10
- const result = new Result<T>();
11
- result.success = true;
12
- result.data = data;
13
- result.timestamp = new Date().toISOString();
14
- return result;
15
- }
16
-
17
- public static failed<T>(error: GenericError, data?: T): Result<T> {
18
- const result = new Result<T>();
19
- result.success = false;
20
- result.timestamp = new Date().toISOString();
21
- result.error = error;
22
- if (data) {
23
- result.data = data;
24
- }
25
- return result;
26
- }
27
-
28
- public static throwError<T>(error: GenericError, data?: T): Result<T> {
29
- const result = new Result<T>();
30
- result.success = false;
31
- result.timestamp = new Date().toISOString();
32
- result.error = error;
33
- if (data) {
34
- result.data = data;
35
- }
36
- throw result;
37
- }
38
- }
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './core-common/index'
2
- export * from './common-infra/index'