@flusys/nestjs-shared 7.1.0 → 8.0.0
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/classes/api-controller.class.d.ts +4 -4
- package/classes/api-service.class.d.ts +7 -7
- package/classes/disabled-event-bus.class.d.ts +1 -1
- package/classes/domain-event.publisher.d.ts +1 -1
- package/classes/event-bus.registry.d.ts +1 -1
- package/classes/event-transports/event-transport.registry.d.ts +1 -1
- package/classes/event-transports/index.d.ts +2 -2
- package/classes/event-transports/kafka-transport.optional.d.ts +1 -1
- package/classes/event-transports/memory-transport.d.ts +1 -1
- package/classes/event-transports/rabbitmq-transport.optional.d.ts +1 -1
- package/classes/hybrid-event-bus.class.d.ts +1 -1
- package/classes/index.d.ts +11 -11
- package/constants/index.d.ts +2 -2
- package/decorators/api-response.decorator.d.ts +1 -1
- package/decorators/current-user.decorator.d.ts +1 -1
- package/decorators/index.d.ts +6 -6
- package/decorators/require-permission.decorator.d.ts +1 -1
- package/dtos/filter-and-pagination.dto.d.ts +1 -1
- package/dtos/index.d.ts +6 -6
- package/entities/index.d.ts +3 -3
- package/enums/index.d.ts +3 -3
- package/exceptions/index.d.ts +2 -2
- package/fesm/470.js +2713 -0
- package/fesm/509.js +793 -0
- package/fesm/667.js +606 -0
- package/fesm/67.js +808 -0
- package/fesm/720.js +914 -0
- package/{cjs/classes/event-transports/rabbitmq-transport.optional.js → fesm/chunks/123.js} +78 -55
- package/fesm/{classes/event-transports/kafka-transport.optional.js → chunks/37.js} +74 -41
- package/fesm/classes/index.js +62 -12
- package/fesm/constants/index.js +445 -17
- package/fesm/decorators/index.js +341 -6
- package/fesm/dtos/index.js +23 -6
- package/fesm/entities/index.js +336 -3
- package/fesm/enums/index.js +57 -3
- package/fesm/exceptions/index.js +280 -2
- package/fesm/guards/index.js +1003 -3
- package/fesm/index.js +1524 -14
- package/fesm/interceptors/index.js +924 -5
- package/fesm/interfaces/index.js +57 -11
- package/fesm/middlewares/index.js +33 -1
- package/fesm/modules/index.js +432 -9
- package/fesm/runtime.js +134 -0
- package/fesm/utils/index.js +1045 -8
- package/filters/global-exception.filter.d.ts +1 -1
- package/filters/index.d.ts +1 -1
- package/guards/index.d.ts +3 -3
- package/guards/jwt-auth.guard.d.ts +1 -1
- package/guards/optional-jwt-auth.guard.d.ts +1 -1
- package/guards/permission.guard.d.ts +2 -2
- package/index.d.ts +14 -14
- package/interceptors/delete-empty-id-from-body.interceptor.d.ts +1 -1
- package/interceptors/idempotency.interceptor.d.ts +2 -2
- package/interceptors/index.d.ts +5 -5
- package/interceptors/response-meta.interceptor.d.ts +1 -1
- package/interceptors/set-user-field-on-body.interceptor.d.ts +1 -1
- package/interceptors/slug.interceptor.d.ts +2 -2
- package/interfaces/ai-assistant-adapter.interface.d.ts +1 -1
- package/interfaces/api.interface.d.ts +2 -2
- package/interfaces/datasource.interface.d.ts +1 -1
- package/interfaces/event-manager-adapter.interface.d.ts +2 -2
- package/interfaces/index.d.ts +11 -11
- package/interfaces/module-config.interface.d.ts +1 -1
- package/interfaces/notification-adapter.interface.d.ts +1 -1
- package/middlewares/index.d.ts +1 -1
- package/middlewares/logger.middleware.d.ts +2 -2
- package/modules/cache/cache.module.d.ts +1 -1
- package/modules/datasource/index.d.ts +1 -1
- package/modules/datasource/multi-tenant-datasource.service.d.ts +4 -4
- package/modules/event-bus/event-bus.explorer.d.ts +2 -2
- package/modules/event-bus/event-bus.module.d.ts +2 -2
- package/modules/event-bus/module-events.provider.d.ts +1 -1
- package/modules/index.d.ts +9 -9
- package/modules/shared-permission-cache/shared-permission-cache.service.d.ts +3 -3
- package/modules/utils/utils.service.d.ts +1 -1
- package/package.json +30 -44
- package/utils/index.d.ts +8 -8
- package/utils/permission-cache-key.util.d.ts +1 -1
- package/utils/query-helpers.util.d.ts +2 -2
- package/utils/request.util.d.ts +1 -1
- package/cjs/classes/api-controller.class.js +0 -728
- package/cjs/classes/api-controller.class.spec.js +0 -684
- package/cjs/classes/api-service.class.js +0 -631
- package/cjs/classes/api-service.class.spec.js +0 -1137
- package/cjs/classes/disabled-event-bus.class.js +0 -15
- package/cjs/classes/disabled-event-bus.class.spec.js +0 -40
- package/cjs/classes/domain-event.publisher.js +0 -76
- package/cjs/classes/domain-event.publisher.spec.js +0 -234
- package/cjs/classes/event-bus.registry.js +0 -68
- package/cjs/classes/event-bus.registry.spec.js +0 -141
- package/cjs/classes/event-transports/event-transport.registry.js +0 -41
- package/cjs/classes/event-transports/event-transport.registry.spec.js +0 -31
- package/cjs/classes/event-transports/index.js +0 -23
- package/cjs/classes/event-transports/kafka-transport.optional.js +0 -151
- package/cjs/classes/event-transports/kafka-transport.optional.spec.js +0 -198
- package/cjs/classes/event-transports/memory-transport.js +0 -53
- package/cjs/classes/event-transports/memory-transport.spec.js +0 -57
- package/cjs/classes/event-transports/rabbitmq-transport.optional.spec.js +0 -255
- package/cjs/classes/handle-custome-db.js +0 -37
- package/cjs/classes/handle-custome-db.spec.js +0 -82
- package/cjs/classes/hybrid-cache.class.js +0 -96
- package/cjs/classes/hybrid-cache.class.spec.js +0 -168
- package/cjs/classes/hybrid-event-bus.class.js +0 -281
- package/cjs/classes/hybrid-event-bus.class.spec.js +0 -218
- package/cjs/classes/index.js +0 -28
- package/cjs/classes/winston.logger.class.js +0 -274
- package/cjs/classes/winston.logger.class.spec.js +0 -330
- package/cjs/constants/index.js +0 -88
- package/cjs/constants/message-keys.js +0 -68
- package/cjs/constants/permissions.js +0 -307
- package/cjs/decorators/api-response.decorator.js +0 -52
- package/cjs/decorators/api-response.decorator.spec.js +0 -126
- package/cjs/decorators/current-user.decorator.js +0 -16
- package/cjs/decorators/current-user.decorator.spec.js +0 -74
- package/cjs/decorators/index.js +0 -23
- package/cjs/decorators/log-action.decorator.js +0 -149
- package/cjs/decorators/log-action.decorator.spec.js +0 -353
- package/cjs/decorators/on-domain-event.decorator.js +0 -13
- package/cjs/decorators/on-domain-event.decorator.spec.js +0 -50
- package/cjs/decorators/public.decorator.js +0 -13
- package/cjs/decorators/public.decorator.spec.js +0 -50
- package/cjs/decorators/require-permission.decorator.js +0 -32
- package/cjs/decorators/require-permission.decorator.spec.js +0 -147
- package/cjs/dtos/delete.dto.js +0 -92
- package/cjs/dtos/filter-and-pagination.dto.js +0 -164
- package/cjs/dtos/get-by-ids.dto.js +0 -65
- package/cjs/dtos/identity-response.dto.js +0 -108
- package/cjs/dtos/index.js +0 -23
- package/cjs/dtos/pagination.dto.js +0 -67
- package/cjs/dtos/response-payload.dto.js +0 -369
- package/cjs/entities/identity.js +0 -100
- package/cjs/entities/index.js +0 -20
- package/cjs/entities/raw-type.js +0 -4
- package/cjs/entities/user-root.js +0 -189
- package/cjs/enums/index.js +0 -20
- package/cjs/enums/notification-type.enum.js +0 -17
- package/cjs/enums/participant-status.enum.js +0 -17
- package/cjs/enums/recurrence-type.enum.js +0 -18
- package/cjs/exceptions/base-app.exception.js +0 -151
- package/cjs/exceptions/base-app.exception.spec.js +0 -157
- package/cjs/exceptions/index.js +0 -19
- package/cjs/exceptions/permission.exception.js +0 -56
- package/cjs/exceptions/permission.exception.spec.js +0 -82
- package/cjs/filters/global-exception.filter.js +0 -279
- package/cjs/filters/global-exception.filter.spec.js +0 -440
- package/cjs/filters/index.js +0 -18
- package/cjs/guards/index.js +0 -20
- package/cjs/guards/jwt-auth.guard.js +0 -75
- package/cjs/guards/jwt-auth.guard.spec.js +0 -75
- package/cjs/guards/optional-jwt-auth.guard.js +0 -67
- package/cjs/guards/optional-jwt-auth.guard.spec.js +0 -55
- package/cjs/guards/permission.guard.js +0 -179
- package/cjs/guards/permission.guard.spec.js +0 -373
- package/cjs/index.js +0 -31
- package/cjs/interceptors/delete-empty-id-from-body.interceptor.js +0 -40
- package/cjs/interceptors/delete-empty-id-from-body.interceptor.spec.js +0 -107
- package/cjs/interceptors/idempotency.interceptor.js +0 -96
- package/cjs/interceptors/idempotency.interceptor.spec.js +0 -116
- package/cjs/interceptors/index.js +0 -22
- package/cjs/interceptors/response-meta.interceptor.js +0 -47
- package/cjs/interceptors/response-meta.interceptor.spec.js +0 -89
- package/cjs/interceptors/set-user-field-on-body.interceptor.js +0 -60
- package/cjs/interceptors/set-user-field-on-body.interceptor.spec.js +0 -128
- package/cjs/interceptors/slug.interceptor.js +0 -75
- package/cjs/interceptors/slug.interceptor.spec.js +0 -110
- package/cjs/interfaces/ai-assistant-adapter.interface.js +0 -24
- package/cjs/interfaces/api.interface.js +0 -4
- package/cjs/interfaces/datasource.interface.js +0 -4
- package/cjs/interfaces/event-bus.interface.js +0 -4
- package/cjs/interfaces/event-manager-adapter.interface.js +0 -11
- package/cjs/interfaces/identity.interface.js +0 -4
- package/cjs/interfaces/index.js +0 -28
- package/cjs/interfaces/logged-user-info.interface.js +0 -6
- package/cjs/interfaces/module-config.interface.js +0 -4
- package/cjs/interfaces/notification-adapter.interface.js +0 -11
- package/cjs/interfaces/permission-sync-adapter.interface.js +0 -11
- package/cjs/interfaces/permission.interface.js +0 -4
- package/cjs/middlewares/index.js +0 -18
- package/cjs/middlewares/logger.middleware.js +0 -264
- package/cjs/middlewares/logger.middleware.spec.js +0 -462
- package/cjs/modules/cache/cache.module.js +0 -39
- package/cjs/modules/cache/cache.module.spec.js +0 -48
- package/cjs/modules/datasource/index.js +0 -18
- package/cjs/modules/datasource/multi-tenant-datasource.service.js +0 -222
- package/cjs/modules/datasource/multi-tenant-datasource.service.spec.js +0 -381
- package/cjs/modules/event-bus/event-bus.explorer.js +0 -124
- package/cjs/modules/event-bus/event-bus.explorer.spec.js +0 -189
- package/cjs/modules/event-bus/event-bus.integration.spec.js +0 -253
- package/cjs/modules/event-bus/event-bus.module.js +0 -94
- package/cjs/modules/event-bus/event-bus.module.spec.js +0 -149
- package/cjs/modules/event-bus/module-events.provider.js +0 -39
- package/cjs/modules/event-bus/module-events.provider.spec.js +0 -56
- package/cjs/modules/index.js +0 -26
- package/cjs/modules/shared-permission-cache/shared-permission-cache.module.js +0 -32
- package/cjs/modules/shared-permission-cache/shared-permission-cache.service.js +0 -101
- package/cjs/modules/shared-permission-cache/shared-permission-cache.service.spec.js +0 -165
- package/cjs/modules/utils/utils.module.js +0 -32
- package/cjs/modules/utils/utils.service.js +0 -76
- package/cjs/modules/utils/utils.service.spec.js +0 -169
- package/cjs/utils/date-time.util.js +0 -94
- package/cjs/utils/date-time.util.spec.js +0 -84
- package/cjs/utils/event-pattern.util.js +0 -103
- package/cjs/utils/event-pattern.util.spec.js +0 -251
- package/cjs/utils/html-sanitizer.util.js +0 -64
- package/cjs/utils/html-sanitizer.util.spec.js +0 -76
- package/cjs/utils/index.js +0 -25
- package/cjs/utils/optional-module.util.js +0 -35
- package/cjs/utils/optional-module.util.spec.js +0 -14
- package/cjs/utils/permission-cache-key.util.js +0 -31
- package/cjs/utils/permission-cache-key.util.spec.js +0 -88
- package/cjs/utils/query-helpers.util.js +0 -60
- package/cjs/utils/query-helpers.util.spec.js +0 -209
- package/cjs/utils/request.util.js +0 -96
- package/cjs/utils/request.util.spec.js +0 -249
- package/cjs/utils/string.util.js +0 -63
- package/cjs/utils/string.util.spec.js +0 -63
- package/fesm/classes/api-controller.class.js +0 -718
- package/fesm/classes/api-controller.class.spec.js +0 -680
- package/fesm/classes/api-service.class.js +0 -621
- package/fesm/classes/api-service.class.spec.js +0 -1133
- package/fesm/classes/disabled-event-bus.class.js +0 -11
- package/fesm/classes/disabled-event-bus.class.spec.js +0 -36
- package/fesm/classes/domain-event.publisher.js +0 -63
- package/fesm/classes/domain-event.publisher.spec.js +0 -230
- package/fesm/classes/event-bus.registry.js +0 -64
- package/fesm/classes/event-bus.registry.spec.js +0 -137
- package/fesm/classes/event-transports/event-transport.registry.js +0 -35
- package/fesm/classes/event-transports/event-transport.registry.spec.js +0 -27
- package/fesm/classes/event-transports/index.js +0 -6
- package/fesm/classes/event-transports/kafka-transport.optional.spec.js +0 -194
- package/fesm/classes/event-transports/memory-transport.js +0 -49
- package/fesm/classes/event-transports/memory-transport.spec.js +0 -53
- package/fesm/classes/event-transports/rabbitmq-transport.optional.js +0 -174
- package/fesm/classes/event-transports/rabbitmq-transport.optional.spec.js +0 -251
- package/fesm/classes/handle-custome-db.js +0 -19
- package/fesm/classes/handle-custome-db.spec.js +0 -78
- package/fesm/classes/hybrid-cache.class.js +0 -81
- package/fesm/classes/hybrid-cache.class.spec.js +0 -164
- package/fesm/classes/hybrid-event-bus.class.js +0 -237
- package/fesm/classes/hybrid-event-bus.class.spec.js +0 -214
- package/fesm/classes/winston.logger.class.js +0 -228
- package/fesm/classes/winston.logger.class.spec.js +0 -327
- package/fesm/constants/message-keys.js +0 -49
- package/fesm/constants/permissions.js +0 -215
- package/fesm/decorators/api-response.decorator.js +0 -42
- package/fesm/decorators/api-response.decorator.spec.js +0 -122
- package/fesm/decorators/current-user.decorator.js +0 -18
- package/fesm/decorators/current-user.decorator.spec.js +0 -73
- package/fesm/decorators/log-action.decorator.js +0 -139
- package/fesm/decorators/log-action.decorator.spec.js +0 -349
- package/fesm/decorators/on-domain-event.decorator.js +0 -10
- package/fesm/decorators/on-domain-event.decorator.spec.js +0 -46
- package/fesm/decorators/public.decorator.js +0 -20
- package/fesm/decorators/public.decorator.spec.js +0 -46
- package/fesm/decorators/require-permission.decorator.js +0 -27
- package/fesm/decorators/require-permission.decorator.spec.js +0 -143
- package/fesm/dtos/delete.dto.js +0 -82
- package/fesm/dtos/filter-and-pagination.dto.js +0 -148
- package/fesm/dtos/get-by-ids.dto.js +0 -55
- package/fesm/dtos/identity-response.dto.js +0 -98
- package/fesm/dtos/pagination.dto.js +0 -57
- package/fesm/dtos/response-payload.dto.js +0 -336
- package/fesm/entities/identity.js +0 -90
- package/fesm/entities/raw-type.js +0 -1
- package/fesm/entities/user-root.js +0 -179
- package/fesm/enums/notification-type.enum.js +0 -7
- package/fesm/enums/participant-status.enum.js +0 -7
- package/fesm/enums/recurrence-type.enum.js +0 -8
- package/fesm/exceptions/base-app.exception.js +0 -115
- package/fesm/exceptions/base-app.exception.spec.js +0 -153
- package/fesm/exceptions/permission.exception.js +0 -35
- package/fesm/exceptions/permission.exception.spec.js +0 -78
- package/fesm/filters/global-exception.filter.js +0 -269
- package/fesm/filters/global-exception.filter.spec.js +0 -436
- package/fesm/filters/index.js +0 -1
- package/fesm/guards/jwt-auth.guard.js +0 -65
- package/fesm/guards/jwt-auth.guard.spec.js +0 -71
- package/fesm/guards/optional-jwt-auth.guard.js +0 -57
- package/fesm/guards/optional-jwt-auth.guard.spec.js +0 -51
- package/fesm/guards/permission.guard.js +0 -169
- package/fesm/guards/permission.guard.spec.js +0 -369
- package/fesm/interceptors/delete-empty-id-from-body.interceptor.js +0 -30
- package/fesm/interceptors/delete-empty-id-from-body.interceptor.spec.js +0 -103
- package/fesm/interceptors/idempotency.interceptor.js +0 -86
- package/fesm/interceptors/idempotency.interceptor.spec.js +0 -112
- package/fesm/interceptors/response-meta.interceptor.js +0 -37
- package/fesm/interceptors/response-meta.interceptor.spec.js +0 -85
- package/fesm/interceptors/set-user-field-on-body.interceptor.js +0 -39
- package/fesm/interceptors/set-user-field-on-body.interceptor.spec.js +0 -124
- package/fesm/interceptors/slug.interceptor.js +0 -65
- package/fesm/interceptors/slug.interceptor.spec.js +0 -106
- package/fesm/interfaces/ai-assistant-adapter.interface.js +0 -3
- package/fesm/interfaces/api.interface.js +0 -1
- package/fesm/interfaces/datasource.interface.js +0 -1
- package/fesm/interfaces/event-bus.interface.js +0 -1
- package/fesm/interfaces/event-manager-adapter.interface.js +0 -1
- package/fesm/interfaces/identity.interface.js +0 -1
- package/fesm/interfaces/logged-user-info.interface.js +0 -3
- package/fesm/interfaces/module-config.interface.js +0 -5
- package/fesm/interfaces/notification-adapter.interface.js +0 -1
- package/fesm/interfaces/permission-sync-adapter.interface.js +0 -1
- package/fesm/interfaces/permission.interface.js +0 -1
- package/fesm/middlewares/logger.middleware.js +0 -235
- package/fesm/middlewares/logger.middleware.spec.js +0 -459
- package/fesm/modules/cache/cache.module.js +0 -29
- package/fesm/modules/cache/cache.module.spec.js +0 -44
- package/fesm/modules/datasource/index.js +0 -1
- package/fesm/modules/datasource/multi-tenant-datasource.service.js +0 -212
- package/fesm/modules/datasource/multi-tenant-datasource.service.spec.js +0 -377
- package/fesm/modules/event-bus/event-bus.explorer.js +0 -114
- package/fesm/modules/event-bus/event-bus.explorer.spec.js +0 -185
- package/fesm/modules/event-bus/event-bus.integration.spec.js +0 -249
- package/fesm/modules/event-bus/event-bus.module.js +0 -84
- package/fesm/modules/event-bus/event-bus.module.spec.js +0 -145
- package/fesm/modules/event-bus/module-events.provider.js +0 -26
- package/fesm/modules/event-bus/module-events.provider.spec.js +0 -52
- package/fesm/modules/shared-permission-cache/shared-permission-cache.module.js +0 -22
- package/fesm/modules/shared-permission-cache/shared-permission-cache.service.js +0 -91
- package/fesm/modules/shared-permission-cache/shared-permission-cache.service.spec.js +0 -161
- package/fesm/modules/utils/utils.module.js +0 -22
- package/fesm/modules/utils/utils.service.js +0 -66
- package/fesm/modules/utils/utils.service.spec.js +0 -165
- package/fesm/utils/date-time.util.js +0 -70
- package/fesm/utils/date-time.util.spec.js +0 -80
- package/fesm/utils/event-pattern.util.js +0 -91
- package/fesm/utils/event-pattern.util.spec.js +0 -247
- package/fesm/utils/html-sanitizer.util.js +0 -69
- package/fesm/utils/html-sanitizer.util.spec.js +0 -72
- package/fesm/utils/optional-module.util.js +0 -25
- package/fesm/utils/optional-module.util.spec.js +0 -10
- package/fesm/utils/permission-cache-key.util.js +0 -10
- package/fesm/utils/permission-cache-key.util.spec.js +0 -84
- package/fesm/utils/query-helpers.util.js +0 -85
- package/fesm/utils/query-helpers.util.spec.js +0 -205
- package/fesm/utils/request.util.js +0 -81
- package/fesm/utils/request.util.spec.js +0 -245
- package/fesm/utils/string.util.js +0 -71
- package/fesm/utils/string.util.spec.js +0 -59
|
@@ -1,436 +0,0 @@
|
|
|
1
|
-
import { HttpException, HttpStatus, Logger } from '@nestjs/common';
|
|
2
|
-
import { QueryFailedError } from 'typeorm';
|
|
3
|
-
const mockGetRequestId = jest.fn();
|
|
4
|
-
const mockGetUserId = jest.fn();
|
|
5
|
-
const mockGetTenantId = jest.fn();
|
|
6
|
-
const mockGetCompanyId = jest.fn();
|
|
7
|
-
jest.mock('../middlewares/logger.middleware', ()=>({
|
|
8
|
-
getRequestId: (...args)=>mockGetRequestId(...args),
|
|
9
|
-
getUserId: (...args)=>mockGetUserId(...args),
|
|
10
|
-
getTenantId: (...args)=>mockGetTenantId(...args),
|
|
11
|
-
getCompanyId: (...args)=>mockGetCompanyId(...args)
|
|
12
|
-
}));
|
|
13
|
-
import { GlobalExceptionFilter } from './global-exception.filter';
|
|
14
|
-
import { BaseAppException, ConflictException } from '../exceptions/base-app.exception';
|
|
15
|
-
import { ERROR_MESSAGES } from '../constants/message-keys';
|
|
16
|
-
function makeQueryFailedError(driverError, message = 'db error') {
|
|
17
|
-
const err = new QueryFailedError('SELECT 1', [], new Error(message));
|
|
18
|
-
Object.assign(err, driverError);
|
|
19
|
-
return err;
|
|
20
|
-
}
|
|
21
|
-
describe('GlobalExceptionFilter', ()=>{
|
|
22
|
-
let filter;
|
|
23
|
-
let errorSpy;
|
|
24
|
-
let warnSpy;
|
|
25
|
-
let request;
|
|
26
|
-
let response;
|
|
27
|
-
let host;
|
|
28
|
-
beforeEach(()=>{
|
|
29
|
-
jest.clearAllMocks();
|
|
30
|
-
filter = new GlobalExceptionFilter();
|
|
31
|
-
errorSpy = jest.spyOn(Logger.prototype, 'error').mockImplementation(()=>undefined);
|
|
32
|
-
warnSpy = jest.spyOn(Logger.prototype, 'warn').mockImplementation(()=>undefined);
|
|
33
|
-
jest.spyOn(Logger.prototype, 'log').mockImplementation(()=>undefined);
|
|
34
|
-
mockGetRequestId.mockReturnValue(undefined);
|
|
35
|
-
mockGetUserId.mockReturnValue(undefined);
|
|
36
|
-
mockGetTenantId.mockReturnValue(undefined);
|
|
37
|
-
mockGetCompanyId.mockReturnValue(undefined);
|
|
38
|
-
request = {
|
|
39
|
-
headers: {},
|
|
40
|
-
method: 'GET',
|
|
41
|
-
originalUrl: '/api/users',
|
|
42
|
-
path: '/api/users'
|
|
43
|
-
};
|
|
44
|
-
response = {
|
|
45
|
-
status: jest.fn().mockReturnThis(),
|
|
46
|
-
json: jest.fn()
|
|
47
|
-
};
|
|
48
|
-
host = {
|
|
49
|
-
switchToHttp: ()=>({
|
|
50
|
-
getRequest: ()=>request,
|
|
51
|
-
getResponse: ()=>response
|
|
52
|
-
})
|
|
53
|
-
};
|
|
54
|
-
});
|
|
55
|
-
afterEach(()=>{
|
|
56
|
-
errorSpy.mockRestore();
|
|
57
|
-
warnSpy.mockRestore();
|
|
58
|
-
});
|
|
59
|
-
describe('request id resolution', ()=>{
|
|
60
|
-
it('should prefer the AsyncLocalStorage request id', ()=>{
|
|
61
|
-
mockGetRequestId.mockReturnValue('ctx-request-id');
|
|
62
|
-
request.headers['x-request-id'] = 'header-request-id';
|
|
63
|
-
filter.catch(new Error('boom'), host);
|
|
64
|
-
const payload = response.json.mock.calls[0][0];
|
|
65
|
-
expect(payload._meta.requestId).toBe('ctx-request-id');
|
|
66
|
-
});
|
|
67
|
-
it('should fall back to the x-request-id header', ()=>{
|
|
68
|
-
request.headers['x-request-id'] = 'header-request-id';
|
|
69
|
-
filter.catch(new Error('boom'), host);
|
|
70
|
-
expect(response.json.mock.calls[0][0]._meta.requestId).toBe('header-request-id');
|
|
71
|
-
});
|
|
72
|
-
it('should default to "no-request-id" when nothing is available', ()=>{
|
|
73
|
-
filter.catch(new Error('boom'), host);
|
|
74
|
-
expect(response.json.mock.calls[0][0]._meta.requestId).toBe('no-request-id');
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
describe('BaseAppException handling', ()=>{
|
|
78
|
-
it('should use the exception status, message, messageKey, and variables', ()=>{
|
|
79
|
-
const exception = new ConflictException('User', 'email');
|
|
80
|
-
filter.catch(exception, host);
|
|
81
|
-
expect(response.status).toHaveBeenCalledWith(HttpStatus.CONFLICT);
|
|
82
|
-
const payload = response.json.mock.calls[0][0];
|
|
83
|
-
expect(payload).toMatchObject({
|
|
84
|
-
success: false,
|
|
85
|
-
message: 'User with this email already exists',
|
|
86
|
-
messageKey: ERROR_MESSAGES.CONFLICT,
|
|
87
|
-
messageVariables: {
|
|
88
|
-
entity: 'User',
|
|
89
|
-
field: 'email'
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
it('should include validation errors when present', ()=>{
|
|
94
|
-
const exception = new BaseAppException({
|
|
95
|
-
message: 'Validation failed',
|
|
96
|
-
messageKey: ERROR_MESSAGES.VALIDATION,
|
|
97
|
-
status: HttpStatus.BAD_REQUEST,
|
|
98
|
-
errors: [
|
|
99
|
-
{
|
|
100
|
-
field: 'email',
|
|
101
|
-
message: 'must be an email'
|
|
102
|
-
}
|
|
103
|
-
]
|
|
104
|
-
});
|
|
105
|
-
filter.catch(exception, host);
|
|
106
|
-
const payload = response.json.mock.calls[0][0];
|
|
107
|
-
expect(payload.errors).toEqual([
|
|
108
|
-
{
|
|
109
|
-
field: 'email',
|
|
110
|
-
message: 'must be an email'
|
|
111
|
-
}
|
|
112
|
-
]);
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
describe('HttpException handling', ()=>{
|
|
116
|
-
it('should use the string response body as the message', ()=>{
|
|
117
|
-
const exception = new HttpException('Not allowed', HttpStatus.FORBIDDEN);
|
|
118
|
-
filter.catch(exception, host);
|
|
119
|
-
expect(response.status).toHaveBeenCalledWith(HttpStatus.FORBIDDEN);
|
|
120
|
-
const payload = response.json.mock.calls[0][0];
|
|
121
|
-
expect(payload.message).toBe('Not allowed');
|
|
122
|
-
expect(payload.messageKey).toBe(ERROR_MESSAGES.HTTP);
|
|
123
|
-
});
|
|
124
|
-
it('should convert an array of validation messages into structured errors', ()=>{
|
|
125
|
-
const exception = new HttpException({
|
|
126
|
-
message: [
|
|
127
|
-
'email must be a valid email',
|
|
128
|
-
'age must not be less than 0'
|
|
129
|
-
]
|
|
130
|
-
}, HttpStatus.BAD_REQUEST);
|
|
131
|
-
filter.catch(exception, host);
|
|
132
|
-
const payload = response.json.mock.calls[0][0];
|
|
133
|
-
expect(payload.message).toBe('Validation failed');
|
|
134
|
-
expect(payload.messageKey).toBe(ERROR_MESSAGES.VALIDATION);
|
|
135
|
-
expect(payload.errors).toEqual([
|
|
136
|
-
{
|
|
137
|
-
field: 'email',
|
|
138
|
-
message: 'must be a valid email'
|
|
139
|
-
},
|
|
140
|
-
{
|
|
141
|
-
field: 'age',
|
|
142
|
-
message: 'must not be less than 0'
|
|
143
|
-
}
|
|
144
|
-
]);
|
|
145
|
-
});
|
|
146
|
-
it('should fall back the message when a validation message has no spaces', ()=>{
|
|
147
|
-
const exception = new HttpException({
|
|
148
|
-
message: [
|
|
149
|
-
'required'
|
|
150
|
-
]
|
|
151
|
-
}, HttpStatus.BAD_REQUEST);
|
|
152
|
-
filter.catch(exception, host);
|
|
153
|
-
const payload = response.json.mock.calls[0][0];
|
|
154
|
-
expect(payload.errors).toEqual([
|
|
155
|
-
{
|
|
156
|
-
field: 'required',
|
|
157
|
-
message: 'required'
|
|
158
|
-
}
|
|
159
|
-
]);
|
|
160
|
-
});
|
|
161
|
-
it('should use the object messageKey/messageVariables/errors when present', ()=>{
|
|
162
|
-
const exception = new HttpException({
|
|
163
|
-
message: 'Custom message',
|
|
164
|
-
messageKey: 'custom.key',
|
|
165
|
-
messageVariables: {
|
|
166
|
-
foo: 'bar'
|
|
167
|
-
},
|
|
168
|
-
errors: [
|
|
169
|
-
{
|
|
170
|
-
field: 'x',
|
|
171
|
-
message: 'y'
|
|
172
|
-
}
|
|
173
|
-
]
|
|
174
|
-
}, HttpStatus.BAD_REQUEST);
|
|
175
|
-
filter.catch(exception, host);
|
|
176
|
-
const payload = response.json.mock.calls[0][0];
|
|
177
|
-
expect(payload).toMatchObject({
|
|
178
|
-
message: 'Custom message',
|
|
179
|
-
messageKey: 'custom.key',
|
|
180
|
-
messageVariables: {
|
|
181
|
-
foo: 'bar'
|
|
182
|
-
},
|
|
183
|
-
errors: [
|
|
184
|
-
{
|
|
185
|
-
field: 'x',
|
|
186
|
-
message: 'y'
|
|
187
|
-
}
|
|
188
|
-
]
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
it('should default to Unknown error/HTTP messageKey when the object has no message', ()=>{
|
|
192
|
-
const exception = new HttpException({}, HttpStatus.BAD_REQUEST);
|
|
193
|
-
filter.catch(exception, host);
|
|
194
|
-
const payload = response.json.mock.calls[0][0];
|
|
195
|
-
expect(payload.message).toBe('Unknown error');
|
|
196
|
-
expect(payload.messageKey).toBe(ERROR_MESSAGES.HTTP);
|
|
197
|
-
});
|
|
198
|
-
it('should handle a non-string/non-object response body', ()=>{
|
|
199
|
-
const exception = new HttpException(42, HttpStatus.BAD_REQUEST);
|
|
200
|
-
filter.catch(exception, host);
|
|
201
|
-
const payload = response.json.mock.calls[0][0];
|
|
202
|
-
expect(payload.message).toBe('Unknown error');
|
|
203
|
-
expect(payload.messageKey).toBe(ERROR_MESSAGES.UNKNOWN);
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
describe('QueryFailedError handling', ()=>{
|
|
207
|
-
it('should map a postgres unique violation (23505) to a conflict', ()=>{
|
|
208
|
-
const exception = makeQueryFailedError({
|
|
209
|
-
driverError: {
|
|
210
|
-
code: '23505',
|
|
211
|
-
detail: 'Key (email)=(a@b.com) already exists.',
|
|
212
|
-
table: 'users'
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
filter.catch(exception, host);
|
|
216
|
-
expect(response.status).toHaveBeenCalledWith(HttpStatus.CONFLICT);
|
|
217
|
-
const payload = response.json.mock.calls[0][0];
|
|
218
|
-
expect(payload.messageKey).toBe(ERROR_MESSAGES.CONFLICT);
|
|
219
|
-
expect(payload.message).toContain('Email');
|
|
220
|
-
});
|
|
221
|
-
it('should map a mysql duplicate entry (ER_DUP_ENTRY) to a conflict using the sqlMessage key name', ()=>{
|
|
222
|
-
const exception = makeQueryFailedError({
|
|
223
|
-
driverError: {
|
|
224
|
-
code: 'ER_DUP_ENTRY',
|
|
225
|
-
sqlMessage: "Duplicate entry 'a@b.com' for key 'users.email_UNIQUE'",
|
|
226
|
-
table: 'users'
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
filter.catch(exception, host);
|
|
230
|
-
const payload = response.json.mock.calls[0][0];
|
|
231
|
-
expect(payload.messageKey).toBe(ERROR_MESSAGES.CONFLICT);
|
|
232
|
-
});
|
|
233
|
-
it('should map an mssql duplicate constraint (2627) to a conflict', ()=>{
|
|
234
|
-
const exception = makeQueryFailedError({
|
|
235
|
-
driverError: {
|
|
236
|
-
number: 2627,
|
|
237
|
-
table: 'users'
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
filter.catch(exception, host);
|
|
241
|
-
expect(response.json.mock.calls[0][0].messageKey).toBe(ERROR_MESSAGES.CONFLICT);
|
|
242
|
-
});
|
|
243
|
-
it('should fall back to "record" when no table name is available for a duplicate', ()=>{
|
|
244
|
-
const exception = makeQueryFailedError({
|
|
245
|
-
driverError: {
|
|
246
|
-
code: '23505'
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
filter.catch(exception, host);
|
|
250
|
-
expect(response.json.mock.calls[0][0].message).toContain('Record');
|
|
251
|
-
});
|
|
252
|
-
it('should map a foreign key violation (23503) to a bad request', ()=>{
|
|
253
|
-
const exception = makeQueryFailedError({
|
|
254
|
-
driverError: {
|
|
255
|
-
code: '23503',
|
|
256
|
-
detail: 'Key (company_id)=(x) is not present in table "companies".'
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
filter.catch(exception, host);
|
|
260
|
-
expect(response.status).toHaveBeenCalledWith(HttpStatus.BAD_REQUEST);
|
|
261
|
-
const payload = response.json.mock.calls[0][0];
|
|
262
|
-
expect(payload.messageKey).toBe(ERROR_MESSAGES.DB_FOREIGN_KEY);
|
|
263
|
-
expect(payload.message).toContain('Company ID');
|
|
264
|
-
});
|
|
265
|
-
it('should map a mysql foreign key violation via errno 1452', ()=>{
|
|
266
|
-
const exception = makeQueryFailedError({
|
|
267
|
-
driverError: {
|
|
268
|
-
errno: 1452,
|
|
269
|
-
sqlMessage: 'Cannot add or update a child row: a foreign key constraint fails (FOREIGN KEY (`company_id`) ...)'
|
|
270
|
-
}
|
|
271
|
-
});
|
|
272
|
-
filter.catch(exception, host);
|
|
273
|
-
expect(response.json.mock.calls[0][0].messageKey).toBe(ERROR_MESSAGES.DB_FOREIGN_KEY);
|
|
274
|
-
});
|
|
275
|
-
it('should map a not-null violation (23502) with the human-readable column name', ()=>{
|
|
276
|
-
const exception = makeQueryFailedError({
|
|
277
|
-
driverError: {
|
|
278
|
-
code: '23502',
|
|
279
|
-
column: 'user_name'
|
|
280
|
-
}
|
|
281
|
-
});
|
|
282
|
-
filter.catch(exception, host);
|
|
283
|
-
const payload = response.json.mock.calls[0][0];
|
|
284
|
-
expect(payload.messageKey).toBe(ERROR_MESSAGES.DB_NOT_NULL);
|
|
285
|
-
expect(payload.message).toContain('User Name');
|
|
286
|
-
});
|
|
287
|
-
it('should map a mysql not-null violation using the sqlMessage column name', ()=>{
|
|
288
|
-
const exception = makeQueryFailedError({
|
|
289
|
-
driverError: {
|
|
290
|
-
code: 'ER_BAD_NULL_ERROR',
|
|
291
|
-
sqlMessage: "Column 'user_name' cannot be null"
|
|
292
|
-
}
|
|
293
|
-
});
|
|
294
|
-
filter.catch(exception, host);
|
|
295
|
-
expect(response.json.mock.calls[0][0].messageKey).toBe(ERROR_MESSAGES.DB_NOT_NULL);
|
|
296
|
-
});
|
|
297
|
-
it('should map a postgres invalid input syntax error (22P02)', ()=>{
|
|
298
|
-
const exception = makeQueryFailedError({
|
|
299
|
-
driverError: {
|
|
300
|
-
code: '22P02',
|
|
301
|
-
column: 'age'
|
|
302
|
-
}
|
|
303
|
-
}, 'invalid input syntax for type integer: "abc"');
|
|
304
|
-
filter.catch(exception, host);
|
|
305
|
-
const payload = response.json.mock.calls[0][0];
|
|
306
|
-
expect(payload.messageKey).toBe(ERROR_MESSAGES.DB_INVALID_INPUT);
|
|
307
|
-
expect(payload.message).toContain('integer');
|
|
308
|
-
expect(payload.message).toContain('Age');
|
|
309
|
-
});
|
|
310
|
-
it('should map a value-too-long error (22001)', ()=>{
|
|
311
|
-
const exception = makeQueryFailedError({
|
|
312
|
-
driverError: {
|
|
313
|
-
code: '22001',
|
|
314
|
-
column: 'name'
|
|
315
|
-
}
|
|
316
|
-
});
|
|
317
|
-
filter.catch(exception, host);
|
|
318
|
-
expect(response.json.mock.calls[0][0].messageKey).toBe(ERROR_MESSAGES.DB_VALUE_TOO_LONG);
|
|
319
|
-
});
|
|
320
|
-
it('should map a mysql data-too-long error using the sqlMessage column name', ()=>{
|
|
321
|
-
const exception = makeQueryFailedError({
|
|
322
|
-
driverError: {
|
|
323
|
-
code: 'ER_DATA_TOO_LONG',
|
|
324
|
-
sqlMessage: "Data too long for column 'name' at row 1"
|
|
325
|
-
}
|
|
326
|
-
});
|
|
327
|
-
filter.catch(exception, host);
|
|
328
|
-
const payload = response.json.mock.calls[0][0];
|
|
329
|
-
expect(payload.messageKey).toBe(ERROR_MESSAGES.DB_VALUE_TOO_LONG);
|
|
330
|
-
expect(payload.message).toContain('Name');
|
|
331
|
-
});
|
|
332
|
-
it('should map a check constraint violation (23514)', ()=>{
|
|
333
|
-
const exception = makeQueryFailedError({
|
|
334
|
-
driverError: {
|
|
335
|
-
code: '23514',
|
|
336
|
-
table: 'orders',
|
|
337
|
-
constraint: 'orders_total_check'
|
|
338
|
-
}
|
|
339
|
-
});
|
|
340
|
-
filter.catch(exception, host);
|
|
341
|
-
const payload = response.json.mock.calls[0][0];
|
|
342
|
-
expect(payload.messageKey).toBe(ERROR_MESSAGES.DB_CHECK_CONSTRAINT);
|
|
343
|
-
expect(payload.messageVariables).toMatchObject({
|
|
344
|
-
constraint: 'orders_total_check'
|
|
345
|
-
});
|
|
346
|
-
});
|
|
347
|
-
it('should map a deadlock error (40P01) to service unavailable', ()=>{
|
|
348
|
-
const exception = makeQueryFailedError({
|
|
349
|
-
driverError: {
|
|
350
|
-
code: '40P01'
|
|
351
|
-
}
|
|
352
|
-
});
|
|
353
|
-
filter.catch(exception, host);
|
|
354
|
-
expect(response.status).toHaveBeenCalledWith(HttpStatus.SERVICE_UNAVAILABLE);
|
|
355
|
-
expect(response.json.mock.calls[0][0].messageKey).toBe(ERROR_MESSAGES.DB_DEADLOCK);
|
|
356
|
-
});
|
|
357
|
-
it('should map a mysql deadlock via errno 1213', ()=>{
|
|
358
|
-
const exception = makeQueryFailedError({
|
|
359
|
-
driverError: {
|
|
360
|
-
errno: 1213
|
|
361
|
-
}
|
|
362
|
-
});
|
|
363
|
-
filter.catch(exception, host);
|
|
364
|
-
expect(response.json.mock.calls[0][0].messageKey).toBe(ERROR_MESSAGES.DB_DEADLOCK);
|
|
365
|
-
});
|
|
366
|
-
it('should fall back to a generic 500 for an unrecognized db error code', ()=>{
|
|
367
|
-
const exception = makeQueryFailedError({
|
|
368
|
-
driverError: {
|
|
369
|
-
code: '99999'
|
|
370
|
-
}
|
|
371
|
-
});
|
|
372
|
-
filter.catch(exception, host);
|
|
373
|
-
expect(response.status).toHaveBeenCalledWith(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
374
|
-
expect(response.json.mock.calls[0][0].messageKey).toBe(ERROR_MESSAGES.INTERNAL);
|
|
375
|
-
});
|
|
376
|
-
it('should read driver fields directly when driverError is absent', ()=>{
|
|
377
|
-
const exception = makeQueryFailedError({
|
|
378
|
-
code: '23505',
|
|
379
|
-
table: 'users'
|
|
380
|
-
});
|
|
381
|
-
filter.catch(exception, host);
|
|
382
|
-
expect(response.json.mock.calls[0][0].messageKey).toBe(ERROR_MESSAGES.CONFLICT);
|
|
383
|
-
});
|
|
384
|
-
});
|
|
385
|
-
describe('unrecognized exceptions', ()=>{
|
|
386
|
-
it('should return a generic 500 for a plain Error', ()=>{
|
|
387
|
-
filter.catch(new Error('unexpected'), host);
|
|
388
|
-
expect(response.status).toHaveBeenCalledWith(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
389
|
-
const payload = response.json.mock.calls[0][0];
|
|
390
|
-
expect(payload.message).toBe('Internal server error');
|
|
391
|
-
expect(payload.messageKey).toBe(ERROR_MESSAGES.INTERNAL);
|
|
392
|
-
});
|
|
393
|
-
it('should return a generic 500 for a thrown non-Error value', ()=>{
|
|
394
|
-
filter.catch('just a string', host);
|
|
395
|
-
expect(response.status).toHaveBeenCalledWith(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
396
|
-
});
|
|
397
|
-
it('should return a generic 500 for undefined/null', ()=>{
|
|
398
|
-
filter.catch(undefined, host);
|
|
399
|
-
expect(response.status).toHaveBeenCalledWith(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
400
|
-
});
|
|
401
|
-
});
|
|
402
|
-
describe('logging behavior', ()=>{
|
|
403
|
-
it('should log at error level with the stack trace for 5xx responses', ()=>{
|
|
404
|
-
const exception = new Error('unexpected');
|
|
405
|
-
filter.catch(exception, host);
|
|
406
|
-
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('500'), exception.stack, expect.objectContaining({
|
|
407
|
-
statusCode: HttpStatus.INTERNAL_SERVER_ERROR
|
|
408
|
-
}));
|
|
409
|
-
expect(warnSpy).not.toHaveBeenCalled();
|
|
410
|
-
});
|
|
411
|
-
it('should log at warn level without a stack trace for 4xx responses', ()=>{
|
|
412
|
-
const exception = new HttpException('Not allowed', HttpStatus.FORBIDDEN);
|
|
413
|
-
filter.catch(exception, host);
|
|
414
|
-
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('403'), expect.objectContaining({
|
|
415
|
-
statusCode: HttpStatus.FORBIDDEN
|
|
416
|
-
}));
|
|
417
|
-
expect(errorSpy).not.toHaveBeenCalled();
|
|
418
|
-
});
|
|
419
|
-
it('should include correlation context (userId/tenantId/companyId) in the log payload', ()=>{
|
|
420
|
-
mockGetUserId.mockReturnValue('user-1');
|
|
421
|
-
mockGetTenantId.mockReturnValue('tenant-1');
|
|
422
|
-
mockGetCompanyId.mockReturnValue('company-1');
|
|
423
|
-
filter.catch(new Error('unexpected'), host);
|
|
424
|
-
expect(errorSpy).toHaveBeenCalledWith(expect.any(String), expect.any(String), expect.objectContaining({
|
|
425
|
-
userId: 'user-1',
|
|
426
|
-
tenantId: 'tenant-1',
|
|
427
|
-
companyId: 'company-1'
|
|
428
|
-
}));
|
|
429
|
-
});
|
|
430
|
-
});
|
|
431
|
-
it('should always set the response status and JSON body once', ()=>{
|
|
432
|
-
filter.catch(new Error('boom'), host);
|
|
433
|
-
expect(response.status).toHaveBeenCalledTimes(1);
|
|
434
|
-
expect(response.json).toHaveBeenCalledTimes(1);
|
|
435
|
-
});
|
|
436
|
-
});
|
package/fesm/filters/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './global-exception.filter';
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
function _define_property(obj, key, value) {
|
|
2
|
-
if (key in obj) {
|
|
3
|
-
Object.defineProperty(obj, key, {
|
|
4
|
-
value: value,
|
|
5
|
-
enumerable: true,
|
|
6
|
-
configurable: true,
|
|
7
|
-
writable: true
|
|
8
|
-
});
|
|
9
|
-
} else {
|
|
10
|
-
obj[key] = value;
|
|
11
|
-
}
|
|
12
|
-
return obj;
|
|
13
|
-
}
|
|
14
|
-
function _ts_decorate(decorators, target, key, desc) {
|
|
15
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
16
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
17
|
-
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;
|
|
18
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
19
|
-
}
|
|
20
|
-
function _ts_metadata(k, v) {
|
|
21
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
22
|
-
}
|
|
23
|
-
function _ts_param(paramIndex, decorator) {
|
|
24
|
-
return function(target, key) {
|
|
25
|
-
decorator(target, key, paramIndex);
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
import { AUTH_MESSAGES, IS_PUBLIC_KEY } from '../constants';
|
|
29
|
-
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
|
|
30
|
-
import { Reflector } from '@nestjs/core';
|
|
31
|
-
import { AuthGuard } from '@nestjs/passport';
|
|
32
|
-
export class JwtAuthGuard extends AuthGuard('jwt') {
|
|
33
|
-
canActivate(context) {
|
|
34
|
-
// Check if route is marked as public
|
|
35
|
-
const isPublic = this.reflector.getAllAndOverride(IS_PUBLIC_KEY, [
|
|
36
|
-
context.getHandler(),
|
|
37
|
-
context.getClass()
|
|
38
|
-
]);
|
|
39
|
-
if (isPublic) {
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
|
-
return super.canActivate(context);
|
|
43
|
-
}
|
|
44
|
-
handleRequest(err, user) {
|
|
45
|
-
if (err || !user) {
|
|
46
|
-
throw err || new UnauthorizedException({
|
|
47
|
-
message: 'Invalid or expired token',
|
|
48
|
-
messageKey: AUTH_MESSAGES.TOKEN_INVALID
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
return user;
|
|
52
|
-
}
|
|
53
|
-
// NOTE: @Inject(Reflector) required for bundled code - external classes need explicit injection
|
|
54
|
-
constructor(reflector){
|
|
55
|
-
super(), _define_property(this, "reflector", void 0), this.reflector = reflector;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
JwtAuthGuard = _ts_decorate([
|
|
59
|
-
Injectable(),
|
|
60
|
-
_ts_param(0, Inject(Reflector)),
|
|
61
|
-
_ts_metadata("design:type", Function),
|
|
62
|
-
_ts_metadata("design:paramtypes", [
|
|
63
|
-
typeof Reflector === "undefined" ? Object : Reflector
|
|
64
|
-
])
|
|
65
|
-
], JwtAuthGuard);
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { UnauthorizedException } from '@nestjs/common';
|
|
2
|
-
import { Reflector } from '@nestjs/core';
|
|
3
|
-
import { AuthGuard } from '@nestjs/passport';
|
|
4
|
-
import { AUTH_MESSAGES, IS_PUBLIC_KEY } from '../constants';
|
|
5
|
-
import { JwtAuthGuard } from './jwt-auth.guard';
|
|
6
|
-
function buildContext() {
|
|
7
|
-
return {
|
|
8
|
-
getHandler: ()=>({}),
|
|
9
|
-
getClass: ()=>({})
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
describe('JwtAuthGuard', ()=>{
|
|
13
|
-
let reflector;
|
|
14
|
-
let guard;
|
|
15
|
-
let superCanActivateSpy;
|
|
16
|
-
beforeEach(()=>{
|
|
17
|
-
reflector = new Reflector();
|
|
18
|
-
guard = new JwtAuthGuard(reflector);
|
|
19
|
-
superCanActivateSpy = jest.spyOn(AuthGuard('jwt').prototype, 'canActivate').mockReturnValue(true);
|
|
20
|
-
});
|
|
21
|
-
afterEach(()=>{
|
|
22
|
-
jest.restoreAllMocks();
|
|
23
|
-
});
|
|
24
|
-
describe('canActivate', ()=>{
|
|
25
|
-
it('should allow access without invoking the parent guard when the route is public', ()=>{
|
|
26
|
-
jest.spyOn(reflector, 'getAllAndOverride').mockReturnValue(true);
|
|
27
|
-
const result = guard.canActivate(buildContext());
|
|
28
|
-
expect(result).toBe(true);
|
|
29
|
-
expect(superCanActivateSpy).not.toHaveBeenCalled();
|
|
30
|
-
});
|
|
31
|
-
it('should check IS_PUBLIC_KEY metadata on both handler and class', ()=>{
|
|
32
|
-
const getAllAndOverrideSpy = jest.spyOn(reflector, 'getAllAndOverride').mockReturnValue(false);
|
|
33
|
-
const ctx = buildContext();
|
|
34
|
-
guard.canActivate(ctx);
|
|
35
|
-
expect(getAllAndOverrideSpy).toHaveBeenCalledWith(IS_PUBLIC_KEY, [
|
|
36
|
-
ctx.getHandler(),
|
|
37
|
-
ctx.getClass()
|
|
38
|
-
]);
|
|
39
|
-
});
|
|
40
|
-
it('should delegate to the parent AuthGuard when the route is not public', ()=>{
|
|
41
|
-
jest.spyOn(reflector, 'getAllAndOverride').mockReturnValue(false);
|
|
42
|
-
const result = guard.canActivate(buildContext());
|
|
43
|
-
expect(result).toBe(true);
|
|
44
|
-
expect(superCanActivateSpy).toHaveBeenCalled();
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
describe('handleRequest', ()=>{
|
|
48
|
-
it('should return the user when authentication succeeds', ()=>{
|
|
49
|
-
const user = {
|
|
50
|
-
id: 'user-1'
|
|
51
|
-
};
|
|
52
|
-
expect(guard.handleRequest(null, user)).toBe(user);
|
|
53
|
-
});
|
|
54
|
-
it('should throw the original error when one is provided', ()=>{
|
|
55
|
-
const err = new Error('strategy failure');
|
|
56
|
-
expect(()=>guard.handleRequest(err, null)).toThrow(err);
|
|
57
|
-
});
|
|
58
|
-
it('should throw UnauthorizedException with AUTH_MESSAGES.TOKEN_INVALID when no error but no user', ()=>{
|
|
59
|
-
try {
|
|
60
|
-
guard.handleRequest(null, null);
|
|
61
|
-
fail('expected to throw');
|
|
62
|
-
} catch (error) {
|
|
63
|
-
expect(error).toBeInstanceOf(UnauthorizedException);
|
|
64
|
-
expect(error.getResponse()).toMatchObject({
|
|
65
|
-
message: 'Invalid or expired token',
|
|
66
|
-
messageKey: AUTH_MESSAGES.TOKEN_INVALID
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
});
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
function _define_property(obj, key, value) {
|
|
2
|
-
if (key in obj) {
|
|
3
|
-
Object.defineProperty(obj, key, {
|
|
4
|
-
value: value,
|
|
5
|
-
enumerable: true,
|
|
6
|
-
configurable: true,
|
|
7
|
-
writable: true
|
|
8
|
-
});
|
|
9
|
-
} else {
|
|
10
|
-
obj[key] = value;
|
|
11
|
-
}
|
|
12
|
-
return obj;
|
|
13
|
-
}
|
|
14
|
-
function _ts_decorate(decorators, target, key, desc) {
|
|
15
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
16
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
17
|
-
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;
|
|
18
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
19
|
-
}
|
|
20
|
-
function _ts_metadata(k, v) {
|
|
21
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
22
|
-
}
|
|
23
|
-
function _ts_param(paramIndex, decorator) {
|
|
24
|
-
return function(target, key) {
|
|
25
|
-
decorator(target, key, paramIndex);
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
import { Inject, Injectable } from '@nestjs/common';
|
|
29
|
-
import { Reflector } from '@nestjs/core';
|
|
30
|
-
import { AuthGuard } from '@nestjs/passport';
|
|
31
|
-
import { IS_PUBLIC_KEY } from '../constants';
|
|
32
|
-
export class OptionalJwtGuard extends AuthGuard('jwt') {
|
|
33
|
-
canActivate(context) {
|
|
34
|
-
const isPublic = this.reflector.getAllAndOverride(IS_PUBLIC_KEY, [
|
|
35
|
-
context.getHandler(),
|
|
36
|
-
context.getClass()
|
|
37
|
-
]);
|
|
38
|
-
if (isPublic) {
|
|
39
|
-
return true;
|
|
40
|
-
}
|
|
41
|
-
return super.canActivate(context);
|
|
42
|
-
}
|
|
43
|
-
handleRequest(_err, user) {
|
|
44
|
-
return user || undefined;
|
|
45
|
-
}
|
|
46
|
-
constructor(reflector){
|
|
47
|
-
super(), _define_property(this, "reflector", void 0), this.reflector = reflector;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
OptionalJwtGuard = _ts_decorate([
|
|
51
|
-
Injectable(),
|
|
52
|
-
_ts_param(0, Inject(Reflector)),
|
|
53
|
-
_ts_metadata("design:type", Function),
|
|
54
|
-
_ts_metadata("design:paramtypes", [
|
|
55
|
-
typeof Reflector === "undefined" ? Object : Reflector
|
|
56
|
-
])
|
|
57
|
-
], OptionalJwtGuard);
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { Reflector } from '@nestjs/core';
|
|
2
|
-
import { AuthGuard } from '@nestjs/passport';
|
|
3
|
-
import { OptionalJwtGuard } from './optional-jwt-auth.guard';
|
|
4
|
-
function buildContext() {
|
|
5
|
-
return {
|
|
6
|
-
getHandler: ()=>({}),
|
|
7
|
-
getClass: ()=>({})
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
describe('OptionalJwtGuard', ()=>{
|
|
11
|
-
let reflector;
|
|
12
|
-
let guard;
|
|
13
|
-
let superCanActivateSpy;
|
|
14
|
-
beforeEach(()=>{
|
|
15
|
-
reflector = new Reflector();
|
|
16
|
-
guard = new OptionalJwtGuard(reflector);
|
|
17
|
-
superCanActivateSpy = jest.spyOn(AuthGuard('jwt').prototype, 'canActivate').mockReturnValue(true);
|
|
18
|
-
});
|
|
19
|
-
afterEach(()=>{
|
|
20
|
-
jest.restoreAllMocks();
|
|
21
|
-
});
|
|
22
|
-
describe('canActivate', ()=>{
|
|
23
|
-
it('should allow access without invoking the parent guard when the route is public', ()=>{
|
|
24
|
-
jest.spyOn(reflector, 'getAllAndOverride').mockReturnValue(true);
|
|
25
|
-
expect(guard.canActivate(buildContext())).toBe(true);
|
|
26
|
-
expect(superCanActivateSpy).not.toHaveBeenCalled();
|
|
27
|
-
});
|
|
28
|
-
it('should delegate to the parent AuthGuard when the route is not public', ()=>{
|
|
29
|
-
jest.spyOn(reflector, 'getAllAndOverride').mockReturnValue(false);
|
|
30
|
-
expect(guard.canActivate(buildContext())).toBe(true);
|
|
31
|
-
expect(superCanActivateSpy).toHaveBeenCalled();
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
describe('handleRequest', ()=>{
|
|
35
|
-
it('should return the user when present', ()=>{
|
|
36
|
-
const user = {
|
|
37
|
-
id: 'user-1'
|
|
38
|
-
};
|
|
39
|
-
expect(guard.handleRequest(null, user)).toBe(user);
|
|
40
|
-
});
|
|
41
|
-
it('should return undefined (not throw) when there is an error', ()=>{
|
|
42
|
-
expect(guard.handleRequest(new Error('token expired'), null)).toBeUndefined();
|
|
43
|
-
});
|
|
44
|
-
it('should return undefined when there is no user and no error', ()=>{
|
|
45
|
-
expect(guard.handleRequest(null, null)).toBeUndefined();
|
|
46
|
-
});
|
|
47
|
-
it('should return undefined for a falsy user value', ()=>{
|
|
48
|
-
expect(guard.handleRequest(null, false)).toBeUndefined();
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
});
|