@flusys/nestjs-shared 3.0.1 → 4.0.0-rc
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.
- package/README.md +159 -79
- package/cjs/classes/api-controller.class.js +26 -8
- package/cjs/classes/api-service.class.js +100 -17
- package/cjs/classes/winston-logger-adapter.class.js +15 -20
- package/cjs/classes/winston.logger.class.js +103 -70
- package/cjs/constants/index.js +1 -0
- package/cjs/constants/message-keys.js +80 -0
- package/cjs/constants/permissions.js +32 -1
- package/cjs/decorators/index.js +1 -0
- package/cjs/decorators/log-action.decorator.js +149 -0
- package/cjs/dtos/response-payload.dto.js +72 -0
- package/cjs/exceptions/base-app.exception.js +145 -0
- package/cjs/exceptions/index.js +1 -0
- package/cjs/exceptions/permission.exception.js +12 -8
- package/cjs/filters/global-exception.filter.js +167 -0
- package/cjs/filters/index.js +18 -0
- package/cjs/guards/jwt-auth.guard.js +4 -1
- package/cjs/guards/permission.guard.js +6 -13
- package/cjs/index.js +1 -0
- package/cjs/interceptors/idempotency.interceptor.js +1 -1
- package/cjs/interceptors/index.js +0 -1
- package/cjs/interfaces/logger.interface.js +1 -4
- package/cjs/middlewares/logger.middleware.js +83 -26
- package/cjs/modules/datasource/multi-tenant-datasource.service.js +33 -11
- package/cjs/modules/utils/utils.service.js +4 -20
- package/cjs/utils/index.js +0 -1
- package/cjs/utils/query-helpers.util.js +8 -1
- package/classes/api-controller.class.d.ts +1 -0
- package/classes/api-service.class.d.ts +5 -10
- package/classes/winston-logger-adapter.class.d.ts +12 -11
- package/classes/winston.logger.class.d.ts +1 -0
- package/constants/index.d.ts +1 -0
- package/constants/message-keys.d.ts +81 -0
- package/constants/permissions.d.ts +36 -0
- package/decorators/index.d.ts +1 -0
- package/decorators/log-action.decorator.d.ts +8 -0
- package/dtos/response-payload.dto.d.ts +8 -0
- package/exceptions/base-app.exception.d.ts +41 -0
- package/exceptions/index.d.ts +1 -0
- package/exceptions/permission.exception.d.ts +1 -1
- package/fesm/classes/api-controller.class.js +26 -8
- package/fesm/classes/api-service.class.js +101 -18
- package/fesm/classes/winston-logger-adapter.class.js +18 -44
- package/fesm/classes/winston.logger.class.js +100 -68
- package/fesm/constants/index.js +2 -0
- package/fesm/constants/message-keys.js +59 -0
- package/fesm/constants/permissions.js +24 -1
- package/fesm/decorators/index.js +1 -0
- package/fesm/decorators/log-action.decorator.js +139 -0
- package/fesm/dtos/response-payload.dto.js +72 -0
- package/fesm/exceptions/base-app.exception.js +109 -0
- package/fesm/exceptions/index.js +1 -0
- package/fesm/exceptions/permission.exception.js +15 -17
- package/fesm/filters/global-exception.filter.js +157 -0
- package/fesm/filters/index.js +1 -0
- package/fesm/guards/jwt-auth.guard.js +5 -2
- package/fesm/guards/permission.guard.js +8 -15
- package/fesm/index.js +1 -0
- package/fesm/interceptors/idempotency.interceptor.js +2 -2
- package/fesm/interceptors/index.js +0 -1
- package/fesm/interfaces/logger.interface.js +1 -4
- package/fesm/middlewares/logger.middleware.js +83 -26
- package/fesm/modules/datasource/multi-tenant-datasource.service.js +34 -12
- package/fesm/modules/utils/utils.service.js +5 -21
- package/fesm/utils/index.js +0 -1
- package/fesm/utils/query-helpers.util.js +8 -1
- package/filters/global-exception.filter.d.ts +10 -0
- package/filters/index.d.ts +1 -0
- package/guards/permission.guard.d.ts +1 -3
- package/index.d.ts +1 -0
- package/interceptors/index.d.ts +0 -1
- package/interfaces/logger.interface.d.ts +5 -5
- package/modules/datasource/multi-tenant-datasource.service.d.ts +1 -2
- package/modules/utils/utils.service.d.ts +0 -1
- package/package.json +2 -2
- package/utils/index.d.ts +0 -1
- package/cjs/interceptors/query-performance.interceptor.js +0 -66
- package/cjs/utils/error-handler.util.js +0 -90
- package/fesm/interceptors/query-performance.interceptor.js +0 -56
- package/fesm/utils/error-handler.util.js +0 -82
- package/interceptors/query-performance.interceptor.d.ts +0 -8
- package/utils/error-handler.util.d.ts +0 -19
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
get BaseAppException () {
|
|
13
|
+
return BaseAppException;
|
|
14
|
+
},
|
|
15
|
+
get ConflictException () {
|
|
16
|
+
return ConflictException;
|
|
17
|
+
},
|
|
18
|
+
get ForbiddenException () {
|
|
19
|
+
return ForbiddenException;
|
|
20
|
+
},
|
|
21
|
+
get InternalServerException () {
|
|
22
|
+
return InternalServerException;
|
|
23
|
+
},
|
|
24
|
+
get NotFoundException () {
|
|
25
|
+
return NotFoundException;
|
|
26
|
+
},
|
|
27
|
+
get ServiceUnavailableException () {
|
|
28
|
+
return ServiceUnavailableException;
|
|
29
|
+
},
|
|
30
|
+
get UnauthorizedException () {
|
|
31
|
+
return UnauthorizedException;
|
|
32
|
+
},
|
|
33
|
+
get ValidationException () {
|
|
34
|
+
return ValidationException;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
const _common = require("@nestjs/common");
|
|
38
|
+
const _messagekeys = require("../constants/message-keys");
|
|
39
|
+
function _define_property(obj, key, value) {
|
|
40
|
+
if (key in obj) {
|
|
41
|
+
Object.defineProperty(obj, key, {
|
|
42
|
+
value: value,
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true
|
|
46
|
+
});
|
|
47
|
+
} else {
|
|
48
|
+
obj[key] = value;
|
|
49
|
+
}
|
|
50
|
+
return obj;
|
|
51
|
+
}
|
|
52
|
+
let BaseAppException = class BaseAppException extends _common.HttpException {
|
|
53
|
+
constructor(options){
|
|
54
|
+
const status = options.status || _common.HttpStatus.BAD_REQUEST;
|
|
55
|
+
super({
|
|
56
|
+
success: false,
|
|
57
|
+
message: options.message,
|
|
58
|
+
messageKey: options.messageKey || _messagekeys.ERROR_MESSAGES.GENERIC,
|
|
59
|
+
messageParams: options.messageParams,
|
|
60
|
+
errors: options.errors
|
|
61
|
+
}, status), _define_property(this, "messageKey", void 0), _define_property(this, "messageParams", void 0), _define_property(this, "errors", void 0), _define_property(this, "metadata", void 0);
|
|
62
|
+
this.messageKey = options.messageKey || _messagekeys.ERROR_MESSAGES.GENERIC;
|
|
63
|
+
this.messageParams = options.messageParams;
|
|
64
|
+
this.errors = options.errors;
|
|
65
|
+
this.metadata = options.metadata;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
let NotFoundException = class NotFoundException extends BaseAppException {
|
|
69
|
+
constructor(entity, id){
|
|
70
|
+
super({
|
|
71
|
+
message: id ? `${entity} with id "${id}" not found` : `${entity} not found`,
|
|
72
|
+
messageKey: _messagekeys.ERROR_MESSAGES.NOT_FOUND,
|
|
73
|
+
status: _common.HttpStatus.NOT_FOUND,
|
|
74
|
+
metadata: {
|
|
75
|
+
entity,
|
|
76
|
+
id
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
let ValidationException = class ValidationException extends BaseAppException {
|
|
82
|
+
constructor(errors){
|
|
83
|
+
super({
|
|
84
|
+
message: 'Validation failed',
|
|
85
|
+
messageKey: _messagekeys.ERROR_MESSAGES.VALIDATION,
|
|
86
|
+
status: _common.HttpStatus.BAD_REQUEST,
|
|
87
|
+
errors
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
let UnauthorizedException = class UnauthorizedException extends BaseAppException {
|
|
92
|
+
constructor(message = 'Unauthorized access'){
|
|
93
|
+
super({
|
|
94
|
+
message,
|
|
95
|
+
messageKey: _messagekeys.ERROR_MESSAGES.UNAUTHORIZED,
|
|
96
|
+
status: _common.HttpStatus.UNAUTHORIZED
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
let ForbiddenException = class ForbiddenException extends BaseAppException {
|
|
101
|
+
constructor(message = 'Access forbidden'){
|
|
102
|
+
super({
|
|
103
|
+
message,
|
|
104
|
+
messageKey: _messagekeys.ERROR_MESSAGES.FORBIDDEN,
|
|
105
|
+
status: _common.HttpStatus.FORBIDDEN
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
let ConflictException = class ConflictException extends BaseAppException {
|
|
110
|
+
constructor(entity, field){
|
|
111
|
+
super({
|
|
112
|
+
message: field ? `${entity} with this ${field} already exists` : `${entity} already exists`,
|
|
113
|
+
messageKey: _messagekeys.ERROR_MESSAGES.CONFLICT,
|
|
114
|
+
status: _common.HttpStatus.CONFLICT,
|
|
115
|
+
metadata: {
|
|
116
|
+
entity,
|
|
117
|
+
field
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
let InternalServerException = class InternalServerException extends BaseAppException {
|
|
123
|
+
constructor(message = 'Internal server error'){
|
|
124
|
+
super({
|
|
125
|
+
message,
|
|
126
|
+
messageKey: _messagekeys.ERROR_MESSAGES.INTERNAL,
|
|
127
|
+
status: _common.HttpStatus.INTERNAL_SERVER_ERROR
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
let ServiceUnavailableException = class ServiceUnavailableException extends BaseAppException {
|
|
132
|
+
constructor(service){
|
|
133
|
+
super({
|
|
134
|
+
message: `${service} is temporarily unavailable`,
|
|
135
|
+
messageKey: _messagekeys.ERROR_MESSAGES.SERVICE_UNAVAILABLE,
|
|
136
|
+
messageParams: {
|
|
137
|
+
service
|
|
138
|
+
},
|
|
139
|
+
status: _common.HttpStatus.SERVICE_UNAVAILABLE,
|
|
140
|
+
metadata: {
|
|
141
|
+
service
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
};
|
package/cjs/exceptions/index.js
CHANGED
|
@@ -20,22 +20,26 @@ _export(exports, {
|
|
|
20
20
|
}
|
|
21
21
|
});
|
|
22
22
|
const _common = require("@nestjs/common");
|
|
23
|
+
const _messagekeys = require("../constants/message-keys");
|
|
23
24
|
let PermissionSystemUnavailableException = class PermissionSystemUnavailableException extends _common.InternalServerErrorException {
|
|
24
|
-
constructor(
|
|
25
|
+
constructor(){
|
|
25
26
|
super({
|
|
26
27
|
success: false,
|
|
27
|
-
message,
|
|
28
|
-
|
|
28
|
+
message: 'Permission system temporarily unavailable',
|
|
29
|
+
messageKey: _messagekeys.ERROR_MESSAGES.PERMISSION_SYSTEM_UNAVAILABLE
|
|
29
30
|
});
|
|
30
31
|
}
|
|
31
32
|
};
|
|
32
33
|
let InsufficientPermissionsException = class InsufficientPermissionsException extends _common.ForbiddenException {
|
|
33
34
|
constructor(missingPermissions, operator = 'AND'){
|
|
34
|
-
const
|
|
35
|
+
const messageKey = operator === 'OR' ? _messagekeys.ERROR_MESSAGES.INSUFFICIENT_PERMISSIONS_OR : _messagekeys.ERROR_MESSAGES.INSUFFICIENT_PERMISSIONS;
|
|
35
36
|
super({
|
|
36
37
|
success: false,
|
|
37
|
-
message,
|
|
38
|
-
|
|
38
|
+
message: 'Insufficient permissions',
|
|
39
|
+
messageKey,
|
|
40
|
+
messageParams: {
|
|
41
|
+
permissions: missingPermissions.join(', ')
|
|
42
|
+
},
|
|
39
43
|
missingPermissions,
|
|
40
44
|
operator
|
|
41
45
|
});
|
|
@@ -45,8 +49,8 @@ let NoPermissionsFoundException = class NoPermissionsFoundException extends _com
|
|
|
45
49
|
constructor(){
|
|
46
50
|
super({
|
|
47
51
|
success: false,
|
|
48
|
-
message: 'No permissions found
|
|
49
|
-
|
|
52
|
+
message: 'No permissions found',
|
|
53
|
+
messageKey: _messagekeys.ERROR_MESSAGES.NO_PERMISSIONS_FOUND
|
|
50
54
|
});
|
|
51
55
|
}
|
|
52
56
|
};
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "GlobalExceptionFilter", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return GlobalExceptionFilter;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _common = require("@nestjs/common");
|
|
12
|
+
const _messagekeys = require("../constants/message-keys");
|
|
13
|
+
const _baseappexception = require("../exceptions/base-app.exception");
|
|
14
|
+
const _loggermiddleware = require("../middlewares/logger.middleware");
|
|
15
|
+
function _define_property(obj, key, value) {
|
|
16
|
+
if (key in obj) {
|
|
17
|
+
Object.defineProperty(obj, key, {
|
|
18
|
+
value: value,
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true
|
|
22
|
+
});
|
|
23
|
+
} else {
|
|
24
|
+
obj[key] = value;
|
|
25
|
+
}
|
|
26
|
+
return obj;
|
|
27
|
+
}
|
|
28
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
29
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
30
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
31
|
+
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
32
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
33
|
+
}
|
|
34
|
+
let GlobalExceptionFilter = class GlobalExceptionFilter {
|
|
35
|
+
catch(exception, host) {
|
|
36
|
+
const ctx = host.switchToHttp();
|
|
37
|
+
const request = ctx.getRequest();
|
|
38
|
+
const response = ctx.getResponse();
|
|
39
|
+
const startTime = request._startTime || Date.now();
|
|
40
|
+
const responseTime = Date.now() - startTime;
|
|
41
|
+
const requestId = (0, _loggermiddleware.getRequestId)() || request.headers['x-request-id'] || 'no-request-id';
|
|
42
|
+
const userId = (0, _loggermiddleware.getUserId)();
|
|
43
|
+
const tenantId = (0, _loggermiddleware.getTenantId)();
|
|
44
|
+
const companyId = (0, _loggermiddleware.getCompanyId)();
|
|
45
|
+
const errorResponse = this.buildErrorResponse(exception, requestId, responseTime);
|
|
46
|
+
const statusCode = this.getStatusCode(exception);
|
|
47
|
+
const logContext = {
|
|
48
|
+
requestId,
|
|
49
|
+
userId,
|
|
50
|
+
tenantId,
|
|
51
|
+
companyId,
|
|
52
|
+
method: request.method,
|
|
53
|
+
url: request.originalUrl,
|
|
54
|
+
path: request.path,
|
|
55
|
+
statusCode,
|
|
56
|
+
duration: `${responseTime}ms`,
|
|
57
|
+
messageKey: errorResponse.messageKey
|
|
58
|
+
};
|
|
59
|
+
const logMessage = this.buildLogMessage(request, statusCode, responseTime, errorResponse);
|
|
60
|
+
if (statusCode >= 500) {
|
|
61
|
+
this.logger.error(logMessage, this.getStack(exception), logContext);
|
|
62
|
+
} else if (statusCode >= 400) {
|
|
63
|
+
this.logger.warn(logMessage, logContext);
|
|
64
|
+
}
|
|
65
|
+
response.status(statusCode).json(errorResponse);
|
|
66
|
+
}
|
|
67
|
+
buildErrorResponse(exception, requestId, responseTime) {
|
|
68
|
+
const timestamp = new Date().toISOString();
|
|
69
|
+
if (exception instanceof _baseappexception.BaseAppException) {
|
|
70
|
+
return {
|
|
71
|
+
success: false,
|
|
72
|
+
message: exception.message,
|
|
73
|
+
messageKey: exception.messageKey,
|
|
74
|
+
messageParams: exception.messageParams,
|
|
75
|
+
errors: exception.errors,
|
|
76
|
+
_meta: {
|
|
77
|
+
requestId,
|
|
78
|
+
timestamp,
|
|
79
|
+
responseTime
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
if (exception instanceof _common.HttpException) {
|
|
84
|
+
const response = exception.getResponse();
|
|
85
|
+
const { message, messageKey, messageParams, errors } = this.extractHttpExceptionDetails(response);
|
|
86
|
+
return {
|
|
87
|
+
success: false,
|
|
88
|
+
message,
|
|
89
|
+
messageKey,
|
|
90
|
+
messageParams,
|
|
91
|
+
errors,
|
|
92
|
+
_meta: {
|
|
93
|
+
requestId,
|
|
94
|
+
timestamp,
|
|
95
|
+
responseTime
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
success: false,
|
|
101
|
+
message: 'Internal server error',
|
|
102
|
+
messageKey: _messagekeys.ERROR_MESSAGES.INTERNAL,
|
|
103
|
+
_meta: {
|
|
104
|
+
requestId,
|
|
105
|
+
timestamp,
|
|
106
|
+
responseTime
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
extractHttpExceptionDetails(response) {
|
|
111
|
+
if (typeof response === 'string') {
|
|
112
|
+
return {
|
|
113
|
+
message: response,
|
|
114
|
+
messageKey: _messagekeys.ERROR_MESSAGES.HTTP
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
if (typeof response === 'object' && response !== null) {
|
|
118
|
+
const obj = response;
|
|
119
|
+
// Handle class-validator validation errors
|
|
120
|
+
if (Array.isArray(obj.message)) {
|
|
121
|
+
const errors = obj.message.map((msg)=>{
|
|
122
|
+
const [field, ...rest] = msg.split(' ');
|
|
123
|
+
return {
|
|
124
|
+
field: field || 'unknown',
|
|
125
|
+
message: rest.join(' ') || msg
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
return {
|
|
129
|
+
message: 'Validation failed',
|
|
130
|
+
messageKey: _messagekeys.ERROR_MESSAGES.VALIDATION,
|
|
131
|
+
errors
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
message: String(obj.message || 'Unknown error'),
|
|
136
|
+
messageKey: String(obj.messageKey || _messagekeys.ERROR_MESSAGES.HTTP),
|
|
137
|
+
messageParams: obj.messageParams,
|
|
138
|
+
errors: obj.errors
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
message: 'Unknown error',
|
|
143
|
+
messageKey: _messagekeys.ERROR_MESSAGES.UNKNOWN
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
getStatusCode(exception) {
|
|
147
|
+
if (exception instanceof _common.HttpException) {
|
|
148
|
+
return exception.getStatus();
|
|
149
|
+
}
|
|
150
|
+
return _common.HttpStatus.INTERNAL_SERVER_ERROR;
|
|
151
|
+
}
|
|
152
|
+
getStack(exception) {
|
|
153
|
+
if (exception instanceof Error) {
|
|
154
|
+
return exception.stack;
|
|
155
|
+
}
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
buildLogMessage(request, statusCode, responseTime, errorResponse) {
|
|
159
|
+
return `Exception caught "HTTP/1.1" "${request.method}" "${request.originalUrl}" - ${statusCode} "${errorResponse.messageKey}" ${responseTime}ms | ${errorResponse.message}`;
|
|
160
|
+
}
|
|
161
|
+
constructor(){
|
|
162
|
+
_define_property(this, "logger", new _common.Logger('ExceptionFilter'));
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
GlobalExceptionFilter = _ts_decorate([
|
|
166
|
+
(0, _common.Catch)()
|
|
167
|
+
], GlobalExceptionFilter);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
_export_star(require("./global-exception.filter"), exports);
|
|
6
|
+
function _export_star(from, to) {
|
|
7
|
+
Object.keys(from).forEach(function(k) {
|
|
8
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
9
|
+
Object.defineProperty(to, k, {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function() {
|
|
12
|
+
return from[k];
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
return from;
|
|
18
|
+
}
|
|
@@ -53,7 +53,10 @@ let JwtAuthGuard = class JwtAuthGuard extends (0, _passport.AuthGuard)('jwt') {
|
|
|
53
53
|
}
|
|
54
54
|
handleRequest(err, user) {
|
|
55
55
|
if (err || !user) {
|
|
56
|
-
throw err || new _common.UnauthorizedException(
|
|
56
|
+
throw err || new _common.UnauthorizedException({
|
|
57
|
+
message: 'Invalid or expired token',
|
|
58
|
+
messageKey: _constants.AUTH_MESSAGES.TOKEN_INVALID
|
|
59
|
+
});
|
|
57
60
|
}
|
|
58
61
|
return user;
|
|
59
62
|
}
|
|
@@ -11,10 +11,8 @@ Object.defineProperty(exports, "PermissionGuard", {
|
|
|
11
11
|
const _common = require("@nestjs/common");
|
|
12
12
|
const _core = require("@nestjs/core");
|
|
13
13
|
const _hybridcacheclass = require("../classes/hybrid-cache.class");
|
|
14
|
-
const _winstonloggeradapterclass = require("../classes/winston-logger-adapter.class");
|
|
15
14
|
const _constants = require("../constants");
|
|
16
15
|
const _permissionexception = require("../exceptions/permission.exception");
|
|
17
|
-
const _loggerinterface = require("../interfaces/logger.interface");
|
|
18
16
|
const _permissioninterface = require("../interfaces/permission.interface");
|
|
19
17
|
function _define_property(obj, key, value) {
|
|
20
18
|
if (key in obj) {
|
|
@@ -57,21 +55,21 @@ let PermissionGuard = class PermissionGuard {
|
|
|
57
55
|
if (!permissionConfig) return true;
|
|
58
56
|
const request = context.switchToHttp().getRequest();
|
|
59
57
|
const user = request.user;
|
|
60
|
-
if (!user) throw new _common.UnauthorizedException(
|
|
58
|
+
if (!user) throw new _common.UnauthorizedException({
|
|
59
|
+
message: 'Authentication required',
|
|
60
|
+
messageKey: _constants.AUTH_MESSAGES.TOKEN_REQUIRED
|
|
61
|
+
});
|
|
61
62
|
if (!this.cache) {
|
|
62
|
-
this.logger.error(`Cache not available (userId: ${user.id})`, undefined, 'PermissionGuard');
|
|
63
63
|
throw new _permissionexception.PermissionSystemUnavailableException();
|
|
64
64
|
}
|
|
65
65
|
const userPermissions = await this.getUserPermissions(user);
|
|
66
66
|
if (!userPermissions || userPermissions.length === 0) {
|
|
67
|
-
this.logger.warn(`No permissions found (userId: ${user.id})`, 'PermissionGuard');
|
|
68
67
|
throw new _permissionexception.NoPermissionsFoundException();
|
|
69
68
|
}
|
|
70
69
|
const logicNode = this.normalizeToLogicNode(permissionConfig);
|
|
71
70
|
if (!logicNode) return true;
|
|
72
71
|
const result = this.evaluateLogicNode(logicNode, userPermissions);
|
|
73
72
|
if (!result.passed) {
|
|
74
|
-
this.logger.warn(`Permission denied (userId: ${user.id})`, 'PermissionGuard');
|
|
75
73
|
throw new _permissionexception.InsufficientPermissionsException(result.missingPermissions, result.operator);
|
|
76
74
|
}
|
|
77
75
|
return true;
|
|
@@ -178,11 +176,10 @@ let PermissionGuard = class PermissionGuard {
|
|
|
178
176
|
}
|
|
179
177
|
return false;
|
|
180
178
|
}
|
|
181
|
-
constructor(reflector, cache, config
|
|
179
|
+
constructor(reflector, cache, config){
|
|
182
180
|
_define_property(this, "reflector", void 0);
|
|
183
181
|
_define_property(this, "cache", void 0);
|
|
184
182
|
_define_property(this, "config", void 0);
|
|
185
|
-
_define_property(this, "logger", void 0);
|
|
186
183
|
this.reflector = reflector;
|
|
187
184
|
this.cache = cache;
|
|
188
185
|
this.config = {
|
|
@@ -191,7 +188,6 @@ let PermissionGuard = class PermissionGuard {
|
|
|
191
188
|
companyPermissionKeyFormat: `${_constants.PERMISSIONS_CACHE_PREFIX}:company:{companyId}:branch:{branchId}:user:{userId}`,
|
|
192
189
|
...config
|
|
193
190
|
};
|
|
194
|
-
this.logger = logger || new _winstonloggeradapterclass.NestLoggerAdapter(new _common.Logger(PermissionGuard.name));
|
|
195
191
|
}
|
|
196
192
|
};
|
|
197
193
|
PermissionGuard = _ts_decorate([
|
|
@@ -201,13 +197,10 @@ PermissionGuard = _ts_decorate([
|
|
|
201
197
|
_ts_param(1, (0, _common.Inject)(_constants.CACHE_INSTANCE)),
|
|
202
198
|
_ts_param(2, (0, _common.Optional)()),
|
|
203
199
|
_ts_param(2, (0, _common.Inject)(_constants.PERMISSION_GUARD_CONFIG)),
|
|
204
|
-
_ts_param(3, (0, _common.Optional)()),
|
|
205
|
-
_ts_param(3, (0, _common.Inject)(_constants.LOGGER_INSTANCE)),
|
|
206
200
|
_ts_metadata("design:type", Function),
|
|
207
201
|
_ts_metadata("design:paramtypes", [
|
|
208
202
|
typeof _core.Reflector === "undefined" ? Object : _core.Reflector,
|
|
209
203
|
typeof _hybridcacheclass.HybridCache === "undefined" ? Object : _hybridcacheclass.HybridCache,
|
|
210
|
-
typeof _permissioninterface.PermissionGuardConfig === "undefined" ? Object : _permissioninterface.PermissionGuardConfig
|
|
211
|
-
typeof _loggerinterface.ILogger === "undefined" ? Object : _loggerinterface.ILogger
|
|
204
|
+
typeof _permissioninterface.PermissionGuardConfig === "undefined" ? Object : _permissioninterface.PermissionGuardConfig
|
|
212
205
|
])
|
|
213
206
|
], PermissionGuard);
|
package/cjs/index.js
CHANGED
|
@@ -9,6 +9,7 @@ _export_star(require("./dtos"), exports);
|
|
|
9
9
|
_export_star(require("./entities"), exports);
|
|
10
10
|
_export_star(require("./enums"), exports);
|
|
11
11
|
_export_star(require("./exceptions"), exports);
|
|
12
|
+
_export_star(require("./filters"), exports);
|
|
12
13
|
_export_star(require("./guards"), exports);
|
|
13
14
|
_export_star(require("./interceptors"), exports);
|
|
14
15
|
_export_star(require("./interfaces"), exports);
|
|
@@ -61,7 +61,7 @@ let IdempotencyInterceptor = class IdempotencyInterceptor {
|
|
|
61
61
|
throw new _common.ConflictException({
|
|
62
62
|
success: false,
|
|
63
63
|
message: 'Request is already being processed',
|
|
64
|
-
|
|
64
|
+
messageKey: _constants.SYSTEM_MESSAGES.DUPLICATE_REQUEST
|
|
65
65
|
});
|
|
66
66
|
}
|
|
67
67
|
// Return cached response
|
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
});
|
|
5
5
|
_export_star(require("./delete-empty-id-from-body.interceptor"), exports);
|
|
6
6
|
_export_star(require("./idempotency.interceptor"), exports);
|
|
7
|
-
_export_star(require("./query-performance.interceptor"), exports);
|
|
8
7
|
_export_star(require("./response-meta.interceptor"), exports);
|
|
9
8
|
_export_star(require("./set-user-field-on-body.interceptor"), exports);
|
|
10
9
|
_export_star(require("./slug.interceptor"), exports);
|