@flusys/nestjs-shared 6.0.2 → 6.1.1
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/cjs/classes/api-controller.class.spec.js +684 -0
- package/cjs/classes/api-service.class.spec.js +809 -0
- package/cjs/classes/handle-custome-db.spec.js +82 -0
- package/cjs/classes/hybrid-cache.class.spec.js +168 -0
- package/cjs/classes/winston.logger.class.spec.js +330 -0
- package/cjs/decorators/api-response.decorator.spec.js +126 -0
- package/cjs/decorators/current-user.decorator.spec.js +74 -0
- package/cjs/decorators/log-action.decorator.spec.js +353 -0
- package/cjs/decorators/public.decorator.spec.js +50 -0
- package/cjs/decorators/require-permission.decorator.spec.js +147 -0
- package/cjs/exceptions/base-app.exception.spec.js +157 -0
- package/cjs/exceptions/permission.exception.spec.js +82 -0
- package/cjs/filters/global-exception.filter.spec.js +440 -0
- package/cjs/guards/jwt-auth.guard.spec.js +75 -0
- package/cjs/guards/optional-jwt-auth.guard.spec.js +55 -0
- package/cjs/guards/permission.guard.spec.js +358 -0
- package/cjs/interceptors/delete-empty-id-from-body.interceptor.spec.js +107 -0
- package/cjs/interceptors/idempotency.interceptor.spec.js +116 -0
- package/cjs/interceptors/response-meta.interceptor.spec.js +89 -0
- package/cjs/interceptors/set-user-field-on-body.interceptor.spec.js +128 -0
- package/cjs/interceptors/slug.interceptor.spec.js +110 -0
- package/cjs/middlewares/logger.middleware.spec.js +462 -0
- package/cjs/modules/cache/cache.module.spec.js +48 -0
- package/cjs/modules/datasource/multi-tenant-datasource.service.spec.js +381 -0
- package/cjs/modules/utils/utils.service.spec.js +169 -0
- package/cjs/utils/date-time.util.spec.js +84 -0
- package/cjs/utils/html-sanitizer.util.spec.js +76 -0
- package/cjs/utils/permission-cache-key.util.spec.js +88 -0
- package/cjs/utils/query-helpers.util.spec.js +209 -0
- package/cjs/utils/request.util.spec.js +249 -0
- package/cjs/utils/string.util.spec.js +63 -0
- package/fesm/classes/api-controller.class.spec.js +680 -0
- package/fesm/classes/api-service.class.spec.js +805 -0
- package/fesm/classes/handle-custome-db.spec.js +78 -0
- package/fesm/classes/hybrid-cache.class.spec.js +164 -0
- package/fesm/classes/winston.logger.class.spec.js +327 -0
- package/fesm/decorators/api-response.decorator.spec.js +122 -0
- package/fesm/decorators/current-user.decorator.spec.js +73 -0
- package/fesm/decorators/log-action.decorator.spec.js +349 -0
- package/fesm/decorators/public.decorator.spec.js +46 -0
- package/fesm/decorators/require-permission.decorator.spec.js +143 -0
- package/fesm/exceptions/base-app.exception.spec.js +153 -0
- package/fesm/exceptions/permission.exception.spec.js +78 -0
- package/fesm/filters/global-exception.filter.spec.js +436 -0
- package/fesm/guards/jwt-auth.guard.spec.js +71 -0
- package/fesm/guards/optional-jwt-auth.guard.spec.js +51 -0
- package/fesm/guards/permission.guard.spec.js +354 -0
- package/fesm/interceptors/delete-empty-id-from-body.interceptor.spec.js +103 -0
- package/fesm/interceptors/idempotency.interceptor.spec.js +112 -0
- package/fesm/interceptors/response-meta.interceptor.spec.js +85 -0
- package/fesm/interceptors/set-user-field-on-body.interceptor.spec.js +124 -0
- package/fesm/interceptors/slug.interceptor.spec.js +106 -0
- package/fesm/middlewares/logger.middleware.spec.js +459 -0
- package/fesm/modules/cache/cache.module.spec.js +44 -0
- package/fesm/modules/datasource/multi-tenant-datasource.service.spec.js +377 -0
- package/fesm/modules/utils/utils.service.spec.js +165 -0
- package/fesm/utils/date-time.util.spec.js +80 -0
- package/fesm/utils/html-sanitizer.util.spec.js +72 -0
- package/fesm/utils/permission-cache-key.util.spec.js +84 -0
- package/fesm/utils/query-helpers.util.spec.js +205 -0
- package/fesm/utils/request.util.spec.js +245 -0
- package/fesm/utils/string.util.spec.js +59 -0
- package/package.json +2 -2
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
require("reflect-metadata");
|
|
6
|
+
const _dtos = require("../dtos");
|
|
7
|
+
const _apiresponsedecorator = require("./api-response.decorator");
|
|
8
|
+
function _define_property(obj, key, value) {
|
|
9
|
+
if (key in obj) {
|
|
10
|
+
Object.defineProperty(obj, key, {
|
|
11
|
+
value: value,
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true
|
|
15
|
+
});
|
|
16
|
+
} else {
|
|
17
|
+
obj[key] = value;
|
|
18
|
+
}
|
|
19
|
+
return obj;
|
|
20
|
+
}
|
|
21
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
22
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
23
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
24
|
+
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;
|
|
25
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
26
|
+
}
|
|
27
|
+
function _ts_metadata(k, v) {
|
|
28
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
29
|
+
}
|
|
30
|
+
// @nestjs/swagger doesn't expose ./dist/constants via its package "exports" map,
|
|
31
|
+
// so the decorator metadata keys are mirrored here (see node_modules/@nestjs/swagger/dist/constants.js).
|
|
32
|
+
const SWAGGER_API_RESPONSE = 'swagger/apiResponse';
|
|
33
|
+
const SWAGGER_API_EXTRA_MODELS = 'swagger/apiExtraModels';
|
|
34
|
+
let SampleDto = class SampleDto {
|
|
35
|
+
constructor(){
|
|
36
|
+
_define_property(this, "id", void 0);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
function getApiResponseSchema(target, methodName) {
|
|
40
|
+
const responses = Reflect.getMetadata(SWAGGER_API_RESPONSE, target[methodName]);
|
|
41
|
+
// ApiOkResponse stores under status code 200
|
|
42
|
+
return responses?.[200]?.schema ?? responses?.['200']?.schema;
|
|
43
|
+
}
|
|
44
|
+
describe('ApiResponseDto', ()=>{
|
|
45
|
+
it('should wrap a single response in SingleResponseDto by default', ()=>{
|
|
46
|
+
let Controller = class Controller {
|
|
47
|
+
handler() {}
|
|
48
|
+
};
|
|
49
|
+
_ts_decorate([
|
|
50
|
+
(0, _apiresponsedecorator.ApiResponseDto)(SampleDto),
|
|
51
|
+
_ts_metadata("design:type", Function),
|
|
52
|
+
_ts_metadata("design:paramtypes", []),
|
|
53
|
+
_ts_metadata("design:returntype", void 0)
|
|
54
|
+
], Controller.prototype, "handler", null);
|
|
55
|
+
const schema = getApiResponseSchema(Controller.prototype, 'handler');
|
|
56
|
+
expect(schema.allOf[0].$ref).toContain(_dtos.SingleResponseDto.name);
|
|
57
|
+
expect(schema.allOf[1].properties.data.$ref).toContain(SampleDto.name);
|
|
58
|
+
expect(schema.allOf[1].properties.meta).toBeUndefined();
|
|
59
|
+
});
|
|
60
|
+
it('should wrap a list response with pagination meta when isArray + list', ()=>{
|
|
61
|
+
let Controller = class Controller {
|
|
62
|
+
handler() {}
|
|
63
|
+
};
|
|
64
|
+
_ts_decorate([
|
|
65
|
+
(0, _apiresponsedecorator.ApiResponseDto)(SampleDto, true, 'list'),
|
|
66
|
+
_ts_metadata("design:type", Function),
|
|
67
|
+
_ts_metadata("design:paramtypes", []),
|
|
68
|
+
_ts_metadata("design:returntype", void 0)
|
|
69
|
+
], Controller.prototype, "handler", null);
|
|
70
|
+
const schema = getApiResponseSchema(Controller.prototype, 'handler');
|
|
71
|
+
expect(schema.allOf[0].$ref).toContain(_dtos.ListResponseDto.name);
|
|
72
|
+
expect(schema.allOf[1].properties.data.type).toBe('array');
|
|
73
|
+
expect(schema.allOf[1].properties.data.items.$ref).toContain(SampleDto.name);
|
|
74
|
+
expect(schema.allOf[1].properties.meta.$ref).toBeDefined();
|
|
75
|
+
});
|
|
76
|
+
it('should wrap a bulk response with bulk meta when isArray + bulk', ()=>{
|
|
77
|
+
let Controller = class Controller {
|
|
78
|
+
handler() {}
|
|
79
|
+
};
|
|
80
|
+
_ts_decorate([
|
|
81
|
+
(0, _apiresponsedecorator.ApiResponseDto)(SampleDto, true, 'bulk'),
|
|
82
|
+
_ts_metadata("design:type", Function),
|
|
83
|
+
_ts_metadata("design:paramtypes", []),
|
|
84
|
+
_ts_metadata("design:returntype", void 0)
|
|
85
|
+
], Controller.prototype, "handler", null);
|
|
86
|
+
const schema = getApiResponseSchema(Controller.prototype, 'handler');
|
|
87
|
+
expect(schema.allOf[0].$ref).toContain(_dtos.BulkResponseDto.name);
|
|
88
|
+
expect(schema.allOf[1].properties.data.type).toBe('array');
|
|
89
|
+
expect(schema.allOf[1].properties.meta.$ref).toBeDefined();
|
|
90
|
+
});
|
|
91
|
+
it('should use SingleResponseDto when isArray is true but arrayType is "single"', ()=>{
|
|
92
|
+
let Controller = class Controller {
|
|
93
|
+
handler() {}
|
|
94
|
+
};
|
|
95
|
+
_ts_decorate([
|
|
96
|
+
(0, _apiresponsedecorator.ApiResponseDto)(SampleDto, true, 'single'),
|
|
97
|
+
_ts_metadata("design:type", Function),
|
|
98
|
+
_ts_metadata("design:paramtypes", []),
|
|
99
|
+
_ts_metadata("design:returntype", void 0)
|
|
100
|
+
], Controller.prototype, "handler", null);
|
|
101
|
+
const schema = getApiResponseSchema(Controller.prototype, 'handler');
|
|
102
|
+
expect(schema.allOf[0].$ref).toContain(_dtos.SingleResponseDto.name);
|
|
103
|
+
// NOTE: data schema stays array-shaped here because `isArray` (not `arrayType`) drives it —
|
|
104
|
+
// arrayType only controls which wrapper/meta DTO is chosen.
|
|
105
|
+
expect(schema.allOf[1].properties.data.type).toBe('array');
|
|
106
|
+
expect(schema.allOf[1].properties.data.items.$ref).toContain(SampleDto.name);
|
|
107
|
+
});
|
|
108
|
+
it('should register extra models including the wrapper DTOs and the given DTO', ()=>{
|
|
109
|
+
let Controller = class Controller {
|
|
110
|
+
handler() {}
|
|
111
|
+
};
|
|
112
|
+
_ts_decorate([
|
|
113
|
+
(0, _apiresponsedecorator.ApiResponseDto)(SampleDto),
|
|
114
|
+
_ts_metadata("design:type", Function),
|
|
115
|
+
_ts_metadata("design:paramtypes", []),
|
|
116
|
+
_ts_metadata("design:returntype", void 0)
|
|
117
|
+
], Controller.prototype, "handler", null);
|
|
118
|
+
const extraModels = Reflect.getMetadata(SWAGGER_API_EXTRA_MODELS, Controller.prototype.handler);
|
|
119
|
+
expect(extraModels).toEqual(expect.arrayContaining([
|
|
120
|
+
_dtos.SingleResponseDto,
|
|
121
|
+
_dtos.ListResponseDto,
|
|
122
|
+
_dtos.BulkResponseDto,
|
|
123
|
+
SampleDto
|
|
124
|
+
]));
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _loggedusermock = require("@test-utils/mocks/logged-user.mock");
|
|
6
|
+
const _currentuserdecorator = require("./current-user.decorator");
|
|
7
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
8
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
10
|
+
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;
|
|
11
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
12
|
+
}
|
|
13
|
+
function _ts_metadata(k, v) {
|
|
14
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
15
|
+
}
|
|
16
|
+
function _ts_param(paramIndex, decorator) {
|
|
17
|
+
return function(target, key) {
|
|
18
|
+
decorator(target, key, paramIndex);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function getParamDecoratorFactory(decorator) {
|
|
22
|
+
let Test = class Test {
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
24
|
+
test(_value) {
|
|
25
|
+
/* noop */ }
|
|
26
|
+
};
|
|
27
|
+
_ts_decorate([
|
|
28
|
+
_ts_param(0, decorator()),
|
|
29
|
+
_ts_metadata("design:type", Function),
|
|
30
|
+
_ts_metadata("design:paramtypes", [
|
|
31
|
+
Object
|
|
32
|
+
]),
|
|
33
|
+
_ts_metadata("design:returntype", void 0)
|
|
34
|
+
], Test.prototype, "test", null);
|
|
35
|
+
const args = Reflect.getMetadata('__routeArguments__', Test, 'test');
|
|
36
|
+
const [firstParam] = Object.values(args);
|
|
37
|
+
return firstParam.factory;
|
|
38
|
+
}
|
|
39
|
+
function buildContext(request) {
|
|
40
|
+
return {
|
|
41
|
+
switchToHttp: ()=>({
|
|
42
|
+
getRequest: ()=>request
|
|
43
|
+
})
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
describe('CurrentUser', ()=>{
|
|
47
|
+
const factory = getParamDecoratorFactory(_currentuserdecorator.CurrentUser);
|
|
48
|
+
it('should return the entire user object when called without data', ()=>{
|
|
49
|
+
const user = (0, _loggedusermock.buildMockUser)();
|
|
50
|
+
const ctx = buildContext({
|
|
51
|
+
user
|
|
52
|
+
});
|
|
53
|
+
expect(factory(undefined, ctx)).toBe(user);
|
|
54
|
+
});
|
|
55
|
+
it('should return a specific property when data is provided', ()=>{
|
|
56
|
+
const user = (0, _loggedusermock.buildMockUser)({
|
|
57
|
+
email: 'specific@example.com'
|
|
58
|
+
});
|
|
59
|
+
const ctx = buildContext({
|
|
60
|
+
user
|
|
61
|
+
});
|
|
62
|
+
expect(factory('email', ctx)).toBe('specific@example.com');
|
|
63
|
+
});
|
|
64
|
+
it('should return undefined when requesting a property from a null user', ()=>{
|
|
65
|
+
const ctx = buildContext({
|
|
66
|
+
user: null
|
|
67
|
+
});
|
|
68
|
+
expect(factory('email', ctx)).toBeUndefined();
|
|
69
|
+
});
|
|
70
|
+
it('should return undefined for the whole user when request.user is undefined', ()=>{
|
|
71
|
+
const ctx = buildContext({});
|
|
72
|
+
expect(factory(undefined, ctx)).toBeUndefined();
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _common = require("@nestjs/common");
|
|
6
|
+
const _logactiondecorator = require("./log-action.decorator");
|
|
7
|
+
function _define_property(obj, key, value) {
|
|
8
|
+
if (key in obj) {
|
|
9
|
+
Object.defineProperty(obj, key, {
|
|
10
|
+
value: value,
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
writable: true
|
|
14
|
+
});
|
|
15
|
+
} else {
|
|
16
|
+
obj[key] = value;
|
|
17
|
+
}
|
|
18
|
+
return obj;
|
|
19
|
+
}
|
|
20
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
21
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
22
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
23
|
+
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;
|
|
24
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
25
|
+
}
|
|
26
|
+
function _ts_metadata(k, v) {
|
|
27
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
28
|
+
}
|
|
29
|
+
const mockWinstonLogger = {
|
|
30
|
+
log: jest.fn(),
|
|
31
|
+
error: jest.fn()
|
|
32
|
+
};
|
|
33
|
+
const createModuleLoggerMock = jest.fn((..._args)=>mockWinstonLogger);
|
|
34
|
+
jest.mock('../classes/winston.logger.class', ()=>({
|
|
35
|
+
createModuleLogger: (...args)=>createModuleLoggerMock(...args)
|
|
36
|
+
}));
|
|
37
|
+
const getRequestIdMock = jest.fn(()=>'req-1');
|
|
38
|
+
const getUserIdMock = jest.fn(()=>'user-1');
|
|
39
|
+
const getTenantIdMock = jest.fn(()=>'tenant-1');
|
|
40
|
+
const getCompanyIdMock = jest.fn(()=>'company-1');
|
|
41
|
+
jest.mock('../middlewares/logger.middleware', ()=>({
|
|
42
|
+
getRequestId: ()=>getRequestIdMock(),
|
|
43
|
+
getUserId: ()=>getUserIdMock(),
|
|
44
|
+
getTenantId: ()=>getTenantIdMock(),
|
|
45
|
+
getCompanyId: ()=>getCompanyIdMock()
|
|
46
|
+
}));
|
|
47
|
+
describe('LogAction', ()=>{
|
|
48
|
+
let nestLoggerWarnSpy;
|
|
49
|
+
let nestLoggerDebugSpy;
|
|
50
|
+
let nestLoggerErrorSpy;
|
|
51
|
+
beforeEach(()=>{
|
|
52
|
+
jest.clearAllMocks();
|
|
53
|
+
nestLoggerWarnSpy = jest.spyOn(_common.Logger.prototype, 'warn').mockImplementation(()=>undefined);
|
|
54
|
+
nestLoggerDebugSpy = jest.spyOn(_common.Logger.prototype, 'debug').mockImplementation(()=>undefined);
|
|
55
|
+
nestLoggerErrorSpy = jest.spyOn(_common.Logger.prototype, 'error').mockImplementation(()=>undefined);
|
|
56
|
+
});
|
|
57
|
+
afterEach(()=>{
|
|
58
|
+
jest.restoreAllMocks();
|
|
59
|
+
});
|
|
60
|
+
describe('using the Nest Logger (no moduleName)', ()=>{
|
|
61
|
+
let Service = class Service {
|
|
62
|
+
async doWork(value) {
|
|
63
|
+
return `done:${value}`;
|
|
64
|
+
}
|
|
65
|
+
async fail() {
|
|
66
|
+
throw new Error('boom');
|
|
67
|
+
}
|
|
68
|
+
constructor(){
|
|
69
|
+
_define_property(this, "entityName", 'product');
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
_ts_decorate([
|
|
73
|
+
(0, _logactiondecorator.LogAction)(),
|
|
74
|
+
_ts_metadata("design:type", Function),
|
|
75
|
+
_ts_metadata("design:paramtypes", [
|
|
76
|
+
String
|
|
77
|
+
]),
|
|
78
|
+
_ts_metadata("design:returntype", Promise)
|
|
79
|
+
], Service.prototype, "doWork", null);
|
|
80
|
+
_ts_decorate([
|
|
81
|
+
(0, _logactiondecorator.LogAction)(),
|
|
82
|
+
_ts_metadata("design:type", Function),
|
|
83
|
+
_ts_metadata("design:paramtypes", []),
|
|
84
|
+
_ts_metadata("design:returntype", Promise)
|
|
85
|
+
], Service.prototype, "fail", null);
|
|
86
|
+
it('should log a debug start/success message and return the original result', async ()=>{
|
|
87
|
+
const service = new Service();
|
|
88
|
+
const result = await service.doWork('x');
|
|
89
|
+
expect(result).toBe('done:x');
|
|
90
|
+
expect(nestLoggerDebugSpy).toHaveBeenCalledTimes(2);
|
|
91
|
+
const [startMessage] = nestLoggerDebugSpy.mock.calls[0];
|
|
92
|
+
expect(startMessage).toContain('Executing action method "product.doWork"');
|
|
93
|
+
const [successMessage] = nestLoggerDebugSpy.mock.calls[1];
|
|
94
|
+
expect(successMessage).toContain('Executed action method "product.doWork"');
|
|
95
|
+
});
|
|
96
|
+
it('should log and rethrow the original error on failure', async ()=>{
|
|
97
|
+
const service = new Service();
|
|
98
|
+
await expect(service.fail()).rejects.toThrow('boom');
|
|
99
|
+
expect(nestLoggerErrorSpy).toHaveBeenCalledTimes(1);
|
|
100
|
+
const [message, stack] = nestLoggerErrorSpy.mock.calls[0];
|
|
101
|
+
expect(message).toContain('Failed action method "product.fail"');
|
|
102
|
+
expect(message).toContain('boom');
|
|
103
|
+
expect(stack).toEqual(expect.any(String));
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
describe('using the Winston module logger (moduleName resolved from instance property)', ()=>{
|
|
107
|
+
let Service = class Service {
|
|
108
|
+
async doWork() {
|
|
109
|
+
return {
|
|
110
|
+
id: '1'
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
constructor(){
|
|
114
|
+
_define_property(this, "entityName", 'product');
|
|
115
|
+
_define_property(this, "moduleName", 'catalog');
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
_ts_decorate([
|
|
119
|
+
(0, _logactiondecorator.LogAction)(),
|
|
120
|
+
_ts_metadata("design:type", Function),
|
|
121
|
+
_ts_metadata("design:paramtypes", []),
|
|
122
|
+
_ts_metadata("design:returntype", Promise)
|
|
123
|
+
], Service.prototype, "doWork", null);
|
|
124
|
+
it('should resolve the module logger via createModuleLogger(moduleName)', async ()=>{
|
|
125
|
+
const service = new Service();
|
|
126
|
+
await service.doWork();
|
|
127
|
+
expect(createModuleLoggerMock).toHaveBeenCalledWith('catalog');
|
|
128
|
+
expect(mockWinstonLogger.log).toHaveBeenCalledTimes(2);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
describe('options', ()=>{
|
|
132
|
+
it('should use a custom action name when provided', async ()=>{
|
|
133
|
+
let Service = class Service {
|
|
134
|
+
async doWork() {
|
|
135
|
+
return 'ok';
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
_ts_decorate([
|
|
139
|
+
(0, _logactiondecorator.LogAction)({
|
|
140
|
+
action: 'custom.action'
|
|
141
|
+
}),
|
|
142
|
+
_ts_metadata("design:type", Function),
|
|
143
|
+
_ts_metadata("design:paramtypes", []),
|
|
144
|
+
_ts_metadata("design:returntype", Promise)
|
|
145
|
+
], Service.prototype, "doWork", null);
|
|
146
|
+
await new Service().doWork();
|
|
147
|
+
const [startMessage] = nestLoggerDebugSpy.mock.calls[0];
|
|
148
|
+
expect(startMessage).toContain('"custom.action"');
|
|
149
|
+
});
|
|
150
|
+
it('should prefer decorator module option over instance moduleName', async ()=>{
|
|
151
|
+
let Service = class Service {
|
|
152
|
+
async doWork() {
|
|
153
|
+
return 'ok';
|
|
154
|
+
}
|
|
155
|
+
constructor(){
|
|
156
|
+
_define_property(this, "moduleName", 'instance-module');
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
_ts_decorate([
|
|
160
|
+
(0, _logactiondecorator.LogAction)({
|
|
161
|
+
module: 'decorator-module'
|
|
162
|
+
}),
|
|
163
|
+
_ts_metadata("design:type", Function),
|
|
164
|
+
_ts_metadata("design:paramtypes", []),
|
|
165
|
+
_ts_metadata("design:returntype", Promise)
|
|
166
|
+
], Service.prototype, "doWork", null);
|
|
167
|
+
await new Service().doWork();
|
|
168
|
+
expect(createModuleLoggerMock).toHaveBeenCalledWith('decorator-module');
|
|
169
|
+
});
|
|
170
|
+
it('should use the custom logLevel', async ()=>{
|
|
171
|
+
let Service = class Service {
|
|
172
|
+
async doWork() {
|
|
173
|
+
return 'ok';
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
_ts_decorate([
|
|
177
|
+
(0, _logactiondecorator.LogAction)({
|
|
178
|
+
logLevel: 'warn'
|
|
179
|
+
}),
|
|
180
|
+
_ts_metadata("design:type", Function),
|
|
181
|
+
_ts_metadata("design:paramtypes", []),
|
|
182
|
+
_ts_metadata("design:returntype", Promise)
|
|
183
|
+
], Service.prototype, "doWork", null);
|
|
184
|
+
await new Service().doWork();
|
|
185
|
+
// logLevel routes to the matching Logger method (nestLogger.warn), not nestLogger.log
|
|
186
|
+
expect(nestLoggerWarnSpy).toHaveBeenCalledTimes(2);
|
|
187
|
+
expect(nestLoggerDebugSpy).not.toHaveBeenCalled();
|
|
188
|
+
});
|
|
189
|
+
it('should redact sensitive keys from params when includeParams is true', async ()=>{
|
|
190
|
+
let Service = class Service {
|
|
191
|
+
async doWork(_dto) {
|
|
192
|
+
return 'ok';
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
_ts_decorate([
|
|
196
|
+
(0, _logactiondecorator.LogAction)({
|
|
197
|
+
includeParams: true
|
|
198
|
+
}),
|
|
199
|
+
_ts_metadata("design:type", Function),
|
|
200
|
+
_ts_metadata("design:paramtypes", [
|
|
201
|
+
typeof Record === "undefined" ? Object : Record
|
|
202
|
+
]),
|
|
203
|
+
_ts_metadata("design:returntype", Promise)
|
|
204
|
+
], Service.prototype, "doWork", null);
|
|
205
|
+
await new Service().doWork({
|
|
206
|
+
email: 'a@b.com',
|
|
207
|
+
password: 'secret',
|
|
208
|
+
token: 'abc'
|
|
209
|
+
});
|
|
210
|
+
const [, startContext] = nestLoggerDebugSpy.mock.calls[0];
|
|
211
|
+
expect(startContext.params[0]).toEqual({
|
|
212
|
+
email: 'a@b.com',
|
|
213
|
+
password: '[REDACTED]',
|
|
214
|
+
token: '[REDACTED]'
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
it('should pass through null/undefined/primitive args unchanged when sanitizing params', async ()=>{
|
|
218
|
+
let Service = class Service {
|
|
219
|
+
async doWork(_a, _b, _c) {
|
|
220
|
+
return 'ok';
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
_ts_decorate([
|
|
224
|
+
(0, _logactiondecorator.LogAction)({
|
|
225
|
+
includeParams: true
|
|
226
|
+
}),
|
|
227
|
+
_ts_metadata("design:type", Function),
|
|
228
|
+
_ts_metadata("design:paramtypes", [
|
|
229
|
+
Object,
|
|
230
|
+
Object,
|
|
231
|
+
Object
|
|
232
|
+
]),
|
|
233
|
+
_ts_metadata("design:returntype", Promise)
|
|
234
|
+
], Service.prototype, "doWork", null);
|
|
235
|
+
await new Service().doWork(null, undefined, 'plain-string');
|
|
236
|
+
const [, startContext] = nestLoggerDebugSpy.mock.calls[0];
|
|
237
|
+
expect(startContext.params).toEqual([
|
|
238
|
+
null,
|
|
239
|
+
undefined,
|
|
240
|
+
'plain-string'
|
|
241
|
+
]);
|
|
242
|
+
});
|
|
243
|
+
it('should include a result preview when includeResult is true and result is an array', async ()=>{
|
|
244
|
+
let Service = class Service {
|
|
245
|
+
async doWork() {
|
|
246
|
+
return [
|
|
247
|
+
1,
|
|
248
|
+
2,
|
|
249
|
+
3
|
|
250
|
+
];
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
_ts_decorate([
|
|
254
|
+
(0, _logactiondecorator.LogAction)({
|
|
255
|
+
includeResult: true
|
|
256
|
+
}),
|
|
257
|
+
_ts_metadata("design:type", Function),
|
|
258
|
+
_ts_metadata("design:paramtypes", []),
|
|
259
|
+
_ts_metadata("design:returntype", Promise)
|
|
260
|
+
], Service.prototype, "doWork", null);
|
|
261
|
+
await new Service().doWork();
|
|
262
|
+
const [, successContext] = nestLoggerDebugSpy.mock.calls[1];
|
|
263
|
+
expect(successContext.resultType).toBe('Array<3 items>');
|
|
264
|
+
expect(successContext.resultPreview).toEqual({
|
|
265
|
+
count: 3,
|
|
266
|
+
sample: [
|
|
267
|
+
1,
|
|
268
|
+
2
|
|
269
|
+
]
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
it('should include a keys-preview when includeResult is true and result is an object', async ()=>{
|
|
273
|
+
let Service = class Service {
|
|
274
|
+
async doWork() {
|
|
275
|
+
return {
|
|
276
|
+
a: 1,
|
|
277
|
+
b: 2,
|
|
278
|
+
c: 3,
|
|
279
|
+
d: 4,
|
|
280
|
+
e: 5,
|
|
281
|
+
f: 6
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
_ts_decorate([
|
|
286
|
+
(0, _logactiondecorator.LogAction)({
|
|
287
|
+
includeResult: true
|
|
288
|
+
}),
|
|
289
|
+
_ts_metadata("design:type", Function),
|
|
290
|
+
_ts_metadata("design:paramtypes", []),
|
|
291
|
+
_ts_metadata("design:returntype", Promise)
|
|
292
|
+
], Service.prototype, "doWork", null);
|
|
293
|
+
await new Service().doWork();
|
|
294
|
+
const [, successContext] = nestLoggerDebugSpy.mock.calls[1];
|
|
295
|
+
expect(successContext.resultPreview).toEqual({
|
|
296
|
+
keys: [
|
|
297
|
+
'a',
|
|
298
|
+
'b',
|
|
299
|
+
'c',
|
|
300
|
+
'd',
|
|
301
|
+
'e',
|
|
302
|
+
'f'
|
|
303
|
+
].slice(0, 5),
|
|
304
|
+
hasMore: true
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
it('should not attach a result preview when includeResult is false', async ()=>{
|
|
308
|
+
let Service = class Service {
|
|
309
|
+
async doWork() {
|
|
310
|
+
return {
|
|
311
|
+
a: 1
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
_ts_decorate([
|
|
316
|
+
(0, _logactiondecorator.LogAction)(),
|
|
317
|
+
_ts_metadata("design:type", Function),
|
|
318
|
+
_ts_metadata("design:paramtypes", []),
|
|
319
|
+
_ts_metadata("design:returntype", Promise)
|
|
320
|
+
], Service.prototype, "doWork", null);
|
|
321
|
+
await new Service().doWork();
|
|
322
|
+
const [, successContext] = nestLoggerDebugSpy.mock.calls[1];
|
|
323
|
+
expect(successContext.resultPreview).toBeUndefined();
|
|
324
|
+
});
|
|
325
|
+
it('should report resultType "null" and "undefined" correctly', async ()=>{
|
|
326
|
+
let Service = class Service {
|
|
327
|
+
async returnsNull() {
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
async returnsUndefined() {
|
|
331
|
+
return undefined;
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
_ts_decorate([
|
|
335
|
+
(0, _logactiondecorator.LogAction)(),
|
|
336
|
+
_ts_metadata("design:type", Function),
|
|
337
|
+
_ts_metadata("design:paramtypes", []),
|
|
338
|
+
_ts_metadata("design:returntype", Promise)
|
|
339
|
+
], Service.prototype, "returnsNull", null);
|
|
340
|
+
_ts_decorate([
|
|
341
|
+
(0, _logactiondecorator.LogAction)(),
|
|
342
|
+
_ts_metadata("design:type", Function),
|
|
343
|
+
_ts_metadata("design:paramtypes", []),
|
|
344
|
+
_ts_metadata("design:returntype", Promise)
|
|
345
|
+
], Service.prototype, "returnsUndefined", null);
|
|
346
|
+
const service = new Service();
|
|
347
|
+
await service.returnsNull();
|
|
348
|
+
await service.returnsUndefined();
|
|
349
|
+
expect(nestLoggerDebugSpy.mock.calls[1][1].resultType).toBe('null');
|
|
350
|
+
expect(nestLoggerDebugSpy.mock.calls[3][1].resultType).toBe('undefined');
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _core = require("@nestjs/core");
|
|
6
|
+
const _constants = require("../constants");
|
|
7
|
+
const _publicdecorator = require("./public.decorator");
|
|
8
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
9
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
10
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
11
|
+
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;
|
|
12
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
13
|
+
}
|
|
14
|
+
function _ts_metadata(k, v) {
|
|
15
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
16
|
+
}
|
|
17
|
+
describe('Public', ()=>{
|
|
18
|
+
it('should set the IS_PUBLIC_KEY metadata to true on a method', ()=>{
|
|
19
|
+
let TestController = class TestController {
|
|
20
|
+
handler() {}
|
|
21
|
+
};
|
|
22
|
+
_ts_decorate([
|
|
23
|
+
(0, _publicdecorator.Public)(),
|
|
24
|
+
_ts_metadata("design:type", Function),
|
|
25
|
+
_ts_metadata("design:paramtypes", []),
|
|
26
|
+
_ts_metadata("design:returntype", void 0)
|
|
27
|
+
], TestController.prototype, "handler", null);
|
|
28
|
+
const reflector = new _core.Reflector();
|
|
29
|
+
const metadata = reflector.get(_constants.IS_PUBLIC_KEY, TestController.prototype.handler);
|
|
30
|
+
expect(metadata).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
it('should set the IS_PUBLIC_KEY metadata to true on a class', ()=>{
|
|
33
|
+
let TestController = class TestController {
|
|
34
|
+
};
|
|
35
|
+
TestController = _ts_decorate([
|
|
36
|
+
(0, _publicdecorator.Public)()
|
|
37
|
+
], TestController);
|
|
38
|
+
const reflector = new _core.Reflector();
|
|
39
|
+
const metadata = reflector.get(_constants.IS_PUBLIC_KEY, TestController);
|
|
40
|
+
expect(metadata).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
it('should not set metadata on a handler that does not use the decorator', ()=>{
|
|
43
|
+
let TestController = class TestController {
|
|
44
|
+
handler() {}
|
|
45
|
+
};
|
|
46
|
+
const reflector = new _core.Reflector();
|
|
47
|
+
const metadata = reflector.get(_constants.IS_PUBLIC_KEY, TestController.prototype.handler);
|
|
48
|
+
expect(metadata).toBeUndefined();
|
|
49
|
+
});
|
|
50
|
+
});
|