@mbc-cqrs-serverless/core 1.2.7-beta.0 → 1.3.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/README.md +37 -0
- package/dist/app.module.js +2 -0
- package/dist/app.module.js.map +1 -1
- package/dist/auth/auth.module.d.ts +2 -0
- package/dist/auth/auth.module.js +30 -0
- package/dist/auth/auth.module.js.map +1 -0
- package/dist/auth/authz-bootstrap.service.d.ts +11 -0
- package/dist/auth/authz-bootstrap.service.js +47 -0
- package/dist/auth/authz-bootstrap.service.js.map +1 -0
- package/dist/auth/group-role-resolver.interface.d.ts +13 -0
- package/dist/auth/group-role-resolver.interface.js +3 -0
- package/dist/auth/group-role-resolver.interface.js.map +1 -0
- package/dist/auth/group-role-resolver.registry.d.ts +6 -0
- package/dist/auth/group-role-resolver.registry.js +26 -0
- package/dist/auth/group-role-resolver.registry.js.map +1 -0
- package/dist/auth/index.d.ts +4 -0
- package/dist/auth/index.js +21 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/context/invoke.d.ts +1 -0
- package/dist/context/invoke.js.map +1 -1
- package/dist/context/user.d.ts +4 -0
- package/dist/context/user.js +50 -0
- package/dist/context/user.js.map +1 -1
- package/dist/decorators/constants.d.ts +2 -0
- package/dist/decorators/constants.js +3 -1
- package/dist/decorators/constants.js.map +1 -1
- package/dist/decorators/group-role-resolver.decorator.d.ts +9 -0
- package/dist/decorators/group-role-resolver.decorator.js +17 -0
- package/dist/decorators/group-role-resolver.decorator.js.map +1 -0
- package/dist/decorators/index.d.ts +2 -0
- package/dist/decorators/index.js +2 -0
- package/dist/decorators/index.js.map +1 -1
- package/dist/decorators/notification-transport.decorator.d.ts +6 -0
- package/dist/decorators/notification-transport.decorator.js +14 -0
- package/dist/decorators/notification-transport.decorator.js.map +1 -0
- package/dist/env.validation.d.ts +19 -0
- package/dist/env.validation.js +22 -1
- package/dist/env.validation.js.map +1 -1
- package/dist/guard/roles.guard.d.ts +11 -1
- package/dist/guard/roles.guard.js +44 -8
- package/dist/guard/roles.guard.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/notification-env.validation.d.ts +8 -0
- package/dist/notification-env.validation.js +36 -0
- package/dist/notification-env.validation.js.map +1 -0
- package/dist/notifications/appsync-events.service.d.ts +44 -0
- package/dist/notifications/appsync-events.service.js +147 -0
- package/dist/notifications/appsync-events.service.js.map +1 -0
- package/dist/notifications/appsync.service.d.ts +3 -2
- package/dist/notifications/appsync.service.js +20 -12
- package/dist/notifications/appsync.service.js.map +1 -1
- package/dist/notifications/enums/index.d.ts +1 -0
- package/dist/notifications/enums/index.js +18 -0
- package/dist/notifications/enums/index.js.map +1 -0
- package/dist/notifications/enums/notification-transport.enum.d.ts +4 -0
- package/dist/notifications/enums/notification-transport.enum.js +9 -0
- package/dist/notifications/enums/notification-transport.enum.js.map +1 -0
- package/dist/notifications/event/notification.event.handler.d.ts +13 -5
- package/dist/notifications/event/notification.event.handler.js +31 -7
- package/dist/notifications/event/notification.event.handler.js.map +1 -1
- package/dist/notifications/index.d.ts +3 -0
- package/dist/notifications/index.js +3 -0
- package/dist/notifications/index.js.map +1 -1
- package/dist/notifications/interfaces/index.d.ts +1 -0
- package/dist/notifications/interfaces/index.js +18 -0
- package/dist/notifications/interfaces/index.js.map +1 -0
- package/dist/notifications/interfaces/notification-transport.interface.d.ts +7 -0
- package/dist/notifications/interfaces/notification-transport.interface.js +5 -0
- package/dist/notifications/interfaces/notification-transport.interface.js.map +1 -0
- package/dist/notifications/notification.module.js +12 -2
- package/dist/notifications/notification.module.js.map +1 -1
- package/dist/services/explorer.service.d.ts +5 -0
- package/dist/services/explorer.service.js +9 -0
- package/dist/services/explorer.service.js.map +1 -1
- package/dist/services/mocks/group-role-resolver.mock.d.ts +4 -0
- package/dist/services/mocks/group-role-resolver.mock.js +20 -0
- package/dist/services/mocks/group-role-resolver.mock.js.map +1 -0
- package/package.json +2 -2
|
@@ -8,17 +8,22 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
8
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
11
14
|
var RolesGuard_1;
|
|
12
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
16
|
exports.RolesGuard = void 0;
|
|
14
17
|
const common_1 = require("@nestjs/common");
|
|
15
18
|
const core_1 = require("@nestjs/core");
|
|
19
|
+
const group_role_resolver_registry_1 = require("../auth/group-role-resolver.registry");
|
|
16
20
|
const constants_1 = require("../constants");
|
|
17
21
|
const context_1 = require("../context");
|
|
18
22
|
const decorators_1 = require("../decorators");
|
|
19
23
|
let RolesGuard = RolesGuard_1 = class RolesGuard {
|
|
20
|
-
constructor(reflector) {
|
|
24
|
+
constructor(reflector, registry) {
|
|
21
25
|
this.reflector = reflector;
|
|
26
|
+
this.registry = registry;
|
|
22
27
|
this.logger = new common_1.Logger(RolesGuard_1.name);
|
|
23
28
|
}
|
|
24
29
|
async canActivate(context) {
|
|
@@ -113,22 +118,51 @@ let RolesGuard = RolesGuard_1 = class RolesGuard {
|
|
|
113
118
|
const invokeContext = (0, context_1.extractInvokeContext)(context);
|
|
114
119
|
return (0, context_1.getAuthorizerClaims)(invokeContext);
|
|
115
120
|
}
|
|
121
|
+
hasAnyRole(requiredRoles, userRoles) {
|
|
122
|
+
return requiredRoles.some((role) => userRoles.includes(role));
|
|
123
|
+
}
|
|
124
|
+
async resolveGroupRoles(context, groupIds) {
|
|
125
|
+
const resolver = this.registry?.get();
|
|
126
|
+
if (!resolver || !groupIds.length) {
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
const userContext = (0, context_1.getUserContext)(context);
|
|
130
|
+
const claims = this.getAuthorizerClaims(context);
|
|
131
|
+
try {
|
|
132
|
+
return await resolver.resolveRoles({
|
|
133
|
+
tenantCode: userContext.tenantCode,
|
|
134
|
+
groupIds,
|
|
135
|
+
claims,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
this.logger.error('GroupRoleResolver.resolveRoles failed', err);
|
|
140
|
+
throw err;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
116
143
|
async verifyRole(context) {
|
|
117
144
|
const requiredRoles = this.reflector.getAllAndOverride(decorators_1.ROLE_METADATA, [context.getHandler(), context.getClass()]);
|
|
118
145
|
if (!requiredRoles || !requiredRoles.length) {
|
|
119
146
|
// all user can access
|
|
120
147
|
return true;
|
|
121
148
|
}
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
149
|
+
const userContext = (0, context_1.getUserContext)(context);
|
|
150
|
+
const { tenantRoles, tenantGroupIds } = userContext;
|
|
151
|
+
if (tenantRoles.includes(constants_1.ROLE_SYSTEM_ADMIN)) {
|
|
152
|
+
return true;
|
|
125
153
|
}
|
|
126
|
-
if (
|
|
154
|
+
if (this.hasAnyRole(requiredRoles, tenantRoles)) {
|
|
127
155
|
return true;
|
|
128
156
|
}
|
|
129
|
-
|
|
157
|
+
const groupRoles = await this.resolveGroupRoles(context, tenantGroupIds);
|
|
158
|
+
return this.hasAnyRole(requiredRoles, groupRoles);
|
|
130
159
|
}
|
|
131
|
-
|
|
160
|
+
/**
|
|
161
|
+
* Returns the primary direct role string from JWT context.
|
|
162
|
+
*
|
|
163
|
+
* @deprecated Not invoked by `verifyRole`. For custom authorization, override
|
|
164
|
+
* `verifyRole`, `resolveGroupRoles`, or `canOverrideTenant` instead.
|
|
165
|
+
*/
|
|
132
166
|
async getUserRole(context) {
|
|
133
167
|
const userContext = (0, context_1.getUserContext)(context);
|
|
134
168
|
return userContext.tenantRole;
|
|
@@ -137,6 +171,8 @@ let RolesGuard = RolesGuard_1 = class RolesGuard {
|
|
|
137
171
|
exports.RolesGuard = RolesGuard;
|
|
138
172
|
exports.RolesGuard = RolesGuard = RolesGuard_1 = __decorate([
|
|
139
173
|
(0, common_1.Injectable)(),
|
|
140
|
-
|
|
174
|
+
__param(1, (0, common_1.Optional)()),
|
|
175
|
+
__metadata("design:paramtypes", [core_1.Reflector,
|
|
176
|
+
group_role_resolver_registry_1.GroupRoleResolverRegistry])
|
|
141
177
|
], RolesGuard);
|
|
142
178
|
//# sourceMappingURL=roles.guard.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"roles.guard.js","sourceRoot":"","sources":["../../src/guard/roles.guard.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"roles.guard.js","sourceRoot":"","sources":["../../src/guard/roles.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAMuB;AACvB,uCAAwC;AAExC,uFAAgF;AAChF,4CAIqB;AACrB,wCAKmB;AACnB,8CAA6C;AAGtC,IAAM,UAAU,kBAAhB,MAAM,UAAU;IAGrB,YACY,SAAoB,EAClB,QAA8C;QADhD,cAAS,GAAT,SAAS,CAAW;QACR,aAAQ,GAAR,QAAQ,CAA4B;QAJzC,WAAM,GAAG,IAAI,eAAM,CAAC,YAAU,CAAC,IAAI,CAAC,CAAA;IAKpD,CAAC;IAEJ,KAAK,CAAC,WAAW,CAAC,OAAyB;QACzC,eAAe;QACf,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,CAAA;QACtD,IAAI,CAAC,aAAa,EAAE,CAAC;YACnB,OAAO,KAAK,CAAA;QACd,CAAC;QAED,yBAAyB;QACzB,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAA;QAClD,OAAO,WAAW,CAAA;IACpB,CAAC;IAED;;;;OAIG;IACO,KAAK,CAAC,YAAY,CAAC,OAAyB;QACpD,MAAM,WAAW,GAAG,IAAA,wB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IAAI,WAAW,CAAC,QAAQ,CAAC,6BAAiB,CAAC,EAAE,CAAC;YAC5C,OAAO,IAAI,CAAA;QACb,CAAC;QACD,IAAI,IAAI,CAAC,UAAU,CAAC,aAAa,EAAE,WAAW,CAAC,EAAE,CAAC;YAChD,OAAO,IAAI,CAAA;QACb,CAAC;QAED,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,iBAAiB,CAAC,OAAO,EAAE,cAAc,CAAC,CAAA;QACxE,OAAO,IAAI,CAAC,UAAU,CAAC,aAAa,EAAE,UAAU,CAAC,CAAA;IACnD,CAAC;IAED;;;;;OAKG;IACO,KAAK,CAAC,WAAW,CAAC,OAAyB;QACnD,MAAM,WAAW,GAAG,IAAA,wBAAc,EAAC,OAAO,CAAC,CAAA;QAE3C,OAAO,WAAW,CAAC,UAAU,CAAA;IAC/B,CAAC;CACF,CAAA;AArLY,gCAAU;qBAAV,UAAU;IADtB,IAAA,mBAAU,GAAE;IAMR,WAAA,IAAA,iBAAQ,GAAE,CAAA;qCADU,gBAAS;QACG,wDAAyB;GALjD,UAAU,CAqLtB"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./app.module"), exports);
|
|
18
|
+
__exportStar(require("./auth"), exports);
|
|
18
19
|
__exportStar(require("./bootstrap"), exports);
|
|
19
20
|
__exportStar(require("./command-events"), exports);
|
|
20
21
|
__exportStar(require("./commands"), exports);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA4B;AAC5B,8CAA2B;AAC3B,mDAAgC;AAChC,6CAA0B;AAC1B,8CAA2B;AAC3B,4CAAyB;AACzB,+CAA4B;AAC5B,+CAA4B;AAC5B,mDAAgC;AAChC,2CAAwB;AACxB,+CAA4B;AAC5B,4CAAyB;AACzB,0CAAuB;AACvB,4CAAyB;AACzB,+CAA4B;AAC5B,kDAA+B;AAC/B,yCAAsB;AACtB,0CAAuB;AACvB,6CAA0B;AAC1B,8CAA2B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA4B;AAC5B,yCAAsB;AACtB,8CAA2B;AAC3B,mDAAgC;AAChC,6CAA0B;AAC1B,8CAA2B;AAC3B,4CAAyB;AACzB,+CAA4B;AAC5B,+CAA4B;AAC5B,mDAAgC;AAChC,2CAAwB;AACxB,+CAA4B;AAC5B,4CAAyB;AACzB,0CAAuB;AACvB,4CAAyB;AACzB,+CAA4B;AAC5B,kDAA+B;AAC/B,yCAAsB;AACtB,0CAAuB;AACvB,6CAA0B;AAC1B,8CAA2B"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Built-in transports and their required endpoint environment variables. */
|
|
2
|
+
export declare const BUILTIN_NOTIFICATION_TRANSPORT_ENV: Record<string, string>;
|
|
3
|
+
/**
|
|
4
|
+
* Parses NOTIFICATION_TRANSPORTS the same way as NotificationEventHandler.
|
|
5
|
+
* Defaults to appsync-graphql when unset or empty.
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseNotificationTransports(raw: string | undefined): string[];
|
|
8
|
+
export declare function validateBuiltinNotificationTransportEnv(config: Record<string, unknown>): void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BUILTIN_NOTIFICATION_TRANSPORT_ENV = void 0;
|
|
4
|
+
exports.parseNotificationTransports = parseNotificationTransports;
|
|
5
|
+
exports.validateBuiltinNotificationTransportEnv = validateBuiltinNotificationTransportEnv;
|
|
6
|
+
const enums_1 = require("./notifications/enums");
|
|
7
|
+
/** Built-in transports and their required endpoint environment variables. */
|
|
8
|
+
exports.BUILTIN_NOTIFICATION_TRANSPORT_ENV = {
|
|
9
|
+
[enums_1.NotificationTransports.APPSYNC_GRAPHQL]: 'APPSYNC_ENDPOINT',
|
|
10
|
+
[enums_1.NotificationTransports.APPSYNC_EVENT]: 'APPSYNC_EVENTS_ENDPOINT',
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Parses NOTIFICATION_TRANSPORTS the same way as NotificationEventHandler.
|
|
14
|
+
* Defaults to appsync-graphql when unset or empty.
|
|
15
|
+
*/
|
|
16
|
+
function parseNotificationTransports(raw) {
|
|
17
|
+
const names = (raw ?? '')
|
|
18
|
+
.split(',')
|
|
19
|
+
.map((s) => s.trim())
|
|
20
|
+
.filter(Boolean);
|
|
21
|
+
return names.length > 0 ? names : [enums_1.NotificationTransports.APPSYNC_GRAPHQL];
|
|
22
|
+
}
|
|
23
|
+
function validateBuiltinNotificationTransportEnv(config) {
|
|
24
|
+
const active = parseNotificationTransports(config.NOTIFICATION_TRANSPORTS);
|
|
25
|
+
for (const name of active) {
|
|
26
|
+
const envKey = exports.BUILTIN_NOTIFICATION_TRANSPORT_ENV[name];
|
|
27
|
+
if (!envKey) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const value = config[envKey];
|
|
31
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
32
|
+
throw new Error(`Notification transport "${name}" is enabled (NOTIFICATION_TRANSPORTS) but ${envKey} is not set.`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=notification-env.validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notification-env.validation.js","sourceRoot":"","sources":["../src/notification-env.validation.ts"],"names":[],"mappings":";;;AAYA,kEAMC;AAED,0FAoBC;AAxCD,iDAA8D;AAE9D,6EAA6E;AAChE,QAAA,kCAAkC,GAA2B;IACxE,CAAC,8BAAsB,CAAC,eAAe,CAAC,EAAE,kBAAkB;IAC5D,CAAC,8BAAsB,CAAC,aAAa,CAAC,EAAE,yBAAyB;CAClE,CAAA;AAED;;;GAGG;AACH,SAAgB,2BAA2B,CAAC,GAAuB;IACjE,MAAM,KAAK,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC;SACtB,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;SACpB,MAAM,CAAC,OAAO,CAAC,CAAA;IAClB,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,8BAAsB,CAAC,eAAe,CAAC,CAAA;AAC5E,CAAC;AAED,SAAgB,uCAAuC,CACrD,MAA+B;IAE/B,MAAM,MAAM,GAAG,2BAA2B,CACxC,MAAM,CAAC,uBAA6C,CACrD,CAAA;IAED,KAAK,MAAM,IAAI,IAAI,MAAM,EAAE,CAAC;QAC1B,MAAM,MAAM,GAAG,0CAAkC,CAAC,IAAI,CAAC,CAAA;QACvD,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,SAAQ;QACV,CAAC;QAED,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,CAAA;QAC5B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;YAC/C,MAAM,IAAI,KAAK,CACb,2BAA2B,IAAI,8CAA8C,MAAM,cAAc,CAClG,CAAA;QACH,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ConfigService } from '@nestjs/config';
|
|
2
|
+
import { INotification } from '../interfaces';
|
|
3
|
+
import { INotificationTransport } from './interfaces';
|
|
4
|
+
export declare class AppSyncEventsService implements INotificationTransport {
|
|
5
|
+
private readonly config;
|
|
6
|
+
private readonly logger;
|
|
7
|
+
private readonly url;
|
|
8
|
+
private readonly namespace;
|
|
9
|
+
private readonly signer;
|
|
10
|
+
constructor(config: ConfigService);
|
|
11
|
+
/**
|
|
12
|
+
* Publish INotification to an AppSync Events channel via IAM SigV4.
|
|
13
|
+
*
|
|
14
|
+
* Channel structure (max 5 segments, seg 1 = namespace):
|
|
15
|
+
* /{namespace}/{tenantCode}/{action}/{sanitizedId}
|
|
16
|
+
*
|
|
17
|
+
* Client subscription options (wildcard /* catches all sub-channels):
|
|
18
|
+
* /{namespace}/{tenantCode}/* — all events for tenant
|
|
19
|
+
* /{namespace}/{tenantCode}/{action}/* — filtered by action
|
|
20
|
+
* /{namespace}/{tenantCode}/{action}/{id} — specific command
|
|
21
|
+
*
|
|
22
|
+
* Requires: Lambda execution role must have appsync:EventPublish permission.
|
|
23
|
+
*/
|
|
24
|
+
sendMessage(notification: INotification): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Resolves the most specific channel path for a notification.
|
|
27
|
+
* Non-alphanumeric characters (e.g. #, @) are sanitized to dashes
|
|
28
|
+
* since AppSync Events channel segments only allow [a-zA-Z0-9-].
|
|
29
|
+
*/
|
|
30
|
+
resolveChannel(notification: INotification): string;
|
|
31
|
+
/**
|
|
32
|
+
* Formats a string to comply with AppSync Events API channel segment rules:
|
|
33
|
+
* 1. Only alphanumeric characters and dashes.
|
|
34
|
+
* 2. Cannot start or end with a dash.
|
|
35
|
+
* 3. Maximum length of 50 characters.
|
|
36
|
+
*/
|
|
37
|
+
private sanitizeSegment;
|
|
38
|
+
private postToChannel;
|
|
39
|
+
/**
|
|
40
|
+
* Builds and signs an HttpRequest for the given channel and notification.
|
|
41
|
+
* Separating signing from the fetch call makes each step independently testable.
|
|
42
|
+
*/
|
|
43
|
+
private signRequest;
|
|
44
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
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;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
var AppSyncEventsService_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.AppSyncEventsService = void 0;
|
|
17
|
+
const node_crypto_1 = require("node:crypto");
|
|
18
|
+
const sha256_js_1 = require("@aws-crypto/sha256-js");
|
|
19
|
+
const credential_provider_node_1 = require("@aws-sdk/credential-provider-node");
|
|
20
|
+
const common_1 = require("@nestjs/common");
|
|
21
|
+
const config_1 = require("@nestjs/config");
|
|
22
|
+
const protocol_http_1 = require("@smithy/protocol-http");
|
|
23
|
+
const signature_v4_1 = require("@smithy/signature-v4");
|
|
24
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
25
|
+
const decorators_1 = require("../decorators");
|
|
26
|
+
const enums_1 = require("./enums");
|
|
27
|
+
/** Default headers required for AppSync Events API requests */
|
|
28
|
+
const DEFAULT_HEADERS = {
|
|
29
|
+
accept: 'application/json, text/javascript',
|
|
30
|
+
'content-encoding': 'amz-1.0',
|
|
31
|
+
'content-type': 'application/json; charset=UTF-8',
|
|
32
|
+
};
|
|
33
|
+
let AppSyncEventsService = AppSyncEventsService_1 = class AppSyncEventsService {
|
|
34
|
+
constructor(config) {
|
|
35
|
+
this.config = config;
|
|
36
|
+
this.logger = new common_1.Logger(AppSyncEventsService_1.name);
|
|
37
|
+
const endpoint = config.get('APPSYNC_EVENTS_ENDPOINT');
|
|
38
|
+
this.namespace = config.get('APPSYNC_EVENTS_NAMESPACE') ?? 'default';
|
|
39
|
+
if (endpoint) {
|
|
40
|
+
this.url = new URL(endpoint);
|
|
41
|
+
// Region is parsed from the hostname automatically:
|
|
42
|
+
// <id>.appsync-api.<region>.amazonaws.com → region
|
|
43
|
+
// Falls back to AWS_REGION env var (always set in Lambda runtime).
|
|
44
|
+
const match = this.url.hostname.match(/\w+\.appsync-api\.([\w-]+)\.amazonaws\.com/);
|
|
45
|
+
const region = match?.[1] ?? process.env.AWS_REGION ?? 'ap-northeast-1';
|
|
46
|
+
this.signer = new signature_v4_1.SignatureV4({
|
|
47
|
+
credentials: (0, credential_provider_node_1.defaultProvider)(),
|
|
48
|
+
service: 'appsync',
|
|
49
|
+
region,
|
|
50
|
+
sha256: sha256_js_1.Sha256,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Publish INotification to an AppSync Events channel via IAM SigV4.
|
|
56
|
+
*
|
|
57
|
+
* Channel structure (max 5 segments, seg 1 = namespace):
|
|
58
|
+
* /{namespace}/{tenantCode}/{action}/{sanitizedId}
|
|
59
|
+
*
|
|
60
|
+
* Client subscription options (wildcard /* catches all sub-channels):
|
|
61
|
+
* /{namespace}/{tenantCode}/* — all events for tenant
|
|
62
|
+
* /{namespace}/{tenantCode}/{action}/* — filtered by action
|
|
63
|
+
* /{namespace}/{tenantCode}/{action}/{id} — specific command
|
|
64
|
+
*
|
|
65
|
+
* Requires: Lambda execution role must have appsync:EventPublish permission.
|
|
66
|
+
*/
|
|
67
|
+
async sendMessage(notification) {
|
|
68
|
+
if (!this.url || !this.signer) {
|
|
69
|
+
this.logger.warn('APPSYNC_EVENTS_ENDPOINT not set, skipping');
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const channel = this.resolveChannel(notification);
|
|
73
|
+
this.logger.debug(`sendMessage:: channel=${channel}`);
|
|
74
|
+
await this.postToChannel(channel, notification);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Resolves the most specific channel path for a notification.
|
|
78
|
+
* Non-alphanumeric characters (e.g. #, @) are sanitized to dashes
|
|
79
|
+
* since AppSync Events channel segments only allow [a-zA-Z0-9-].
|
|
80
|
+
*/
|
|
81
|
+
resolveChannel(notification) {
|
|
82
|
+
const namespace = this.sanitizeSegment(this.namespace);
|
|
83
|
+
const tenantCode = this.sanitizeSegment(notification.tenantCode);
|
|
84
|
+
const action = this.sanitizeSegment(notification.action);
|
|
85
|
+
const id = this.sanitizeSegment(notification.id);
|
|
86
|
+
return `/${namespace}/${tenantCode}/${action}/${id}`;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Formats a string to comply with AppSync Events API channel segment rules:
|
|
90
|
+
* 1. Only alphanumeric characters and dashes.
|
|
91
|
+
* 2. Cannot start or end with a dash.
|
|
92
|
+
* 3. Maximum length of 50 characters.
|
|
93
|
+
*/
|
|
94
|
+
sanitizeSegment(segment) {
|
|
95
|
+
if (!segment)
|
|
96
|
+
return 'none';
|
|
97
|
+
// Replace any non-alphanumeric character (like _, %, =) with a dash
|
|
98
|
+
let s = segment.replace(/[^a-zA-Z0-9]/g, '-');
|
|
99
|
+
// Truncate to AWS's strict 50 character limit
|
|
100
|
+
s = s.substring(0, 50);
|
|
101
|
+
// Strip out any dashes from the very beginning or end
|
|
102
|
+
s = s.replace(/^-+|-+$/g, '');
|
|
103
|
+
// Fallback if the string became empty after trimming
|
|
104
|
+
return s || 'none';
|
|
105
|
+
}
|
|
106
|
+
async postToChannel(channel, notification) {
|
|
107
|
+
const signedReq = await this.signRequest(channel, notification);
|
|
108
|
+
const res = await (0, node_fetch_1.default)(this.url.toString(), {
|
|
109
|
+
method: signedReq.method,
|
|
110
|
+
headers: signedReq.headers,
|
|
111
|
+
body: signedReq.body,
|
|
112
|
+
});
|
|
113
|
+
if (!res.ok) {
|
|
114
|
+
const text = await res.text();
|
|
115
|
+
throw new Error(`AppSync Events publish failed [${res.status}] on channel ${channel}: ${text}`);
|
|
116
|
+
}
|
|
117
|
+
this.logger.debug(`sendMessage:: published successfully to ${channel}`);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Builds and signs an HttpRequest for the given channel and notification.
|
|
121
|
+
* Separating signing from the fetch call makes each step independently testable.
|
|
122
|
+
*/
|
|
123
|
+
async signRequest(channel, notification) {
|
|
124
|
+
const body = JSON.stringify({
|
|
125
|
+
id: (0, node_crypto_1.randomUUID)(),
|
|
126
|
+
channel,
|
|
127
|
+
events: [JSON.stringify(notification)],
|
|
128
|
+
});
|
|
129
|
+
const httpRequest = new protocol_http_1.HttpRequest({
|
|
130
|
+
method: 'POST',
|
|
131
|
+
headers: {
|
|
132
|
+
...DEFAULT_HEADERS,
|
|
133
|
+
host: this.url.hostname,
|
|
134
|
+
},
|
|
135
|
+
body,
|
|
136
|
+
hostname: this.url.hostname,
|
|
137
|
+
path: this.url.pathname,
|
|
138
|
+
});
|
|
139
|
+
return this.signer.sign(httpRequest);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
exports.AppSyncEventsService = AppSyncEventsService;
|
|
143
|
+
exports.AppSyncEventsService = AppSyncEventsService = AppSyncEventsService_1 = __decorate([
|
|
144
|
+
(0, decorators_1.NotificationTransport)(enums_1.NotificationTransports.APPSYNC_EVENT),
|
|
145
|
+
__metadata("design:paramtypes", [config_1.ConfigService])
|
|
146
|
+
], AppSyncEventsService);
|
|
147
|
+
//# sourceMappingURL=appsync-events.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"appsync-events.service.js","sourceRoot":"","sources":["../../src/notifications/appsync-events.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAAwC;AA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IAC/D,CAAC;CACF,CAAA;AA5IY,oDAAoB;+BAApB,oBAAoB;IADhC,IAAA,kCAAqB,EAAC,8BAAsB,CAAC,aAAa,CAAC;qCAQrB,sBAAa;GAPvC,oBAAoB,CA4IhC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ConfigService } from '@nestjs/config';
|
|
2
2
|
import { INotification } from '../interfaces';
|
|
3
|
-
|
|
3
|
+
import { INotificationTransport } from './interfaces';
|
|
4
|
+
export declare class AppSyncService implements INotificationTransport {
|
|
4
5
|
private readonly config;
|
|
5
6
|
private readonly logger;
|
|
6
7
|
private readonly endpoint;
|
|
@@ -9,5 +10,5 @@ export declare class AppSyncService {
|
|
|
9
10
|
private readonly region;
|
|
10
11
|
private readonly signer;
|
|
11
12
|
constructor(config: ConfigService);
|
|
12
|
-
sendMessage(
|
|
13
|
+
sendMessage(notification: INotification): Promise<void>;
|
|
13
14
|
}
|
|
@@ -20,6 +20,8 @@ const signature_v4_1 = require("@aws-sdk/signature-v4");
|
|
|
20
20
|
const common_1 = require("@nestjs/common");
|
|
21
21
|
const config_1 = require("@nestjs/config");
|
|
22
22
|
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
23
|
+
const decorators_1 = require("../decorators");
|
|
24
|
+
const enums_1 = require("./enums");
|
|
23
25
|
const query = /* GraphQL */ `
|
|
24
26
|
mutation SEND_MESSAGE($message: AWSJSON!) {
|
|
25
27
|
sendMessage(message: $message) {
|
|
@@ -37,18 +39,24 @@ let AppSyncService = AppSyncService_1 = class AppSyncService {
|
|
|
37
39
|
constructor(config) {
|
|
38
40
|
this.config = config;
|
|
39
41
|
this.logger = new common_1.Logger(AppSyncService_1.name);
|
|
40
|
-
this.endpoint = config.get('APPSYNC_ENDPOINT');
|
|
41
|
-
this.apiKey = config.get('APPSYNC_API_KEY');
|
|
42
|
+
this.endpoint = config.get('APPSYNC_ENDPOINT') ?? '';
|
|
43
|
+
this.apiKey = config.get('APPSYNC_API_KEY') ?? '';
|
|
42
44
|
this.region = 'ap-northeast-1';
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
if (this.endpoint) {
|
|
46
|
+
this.hostname = new URL(this.endpoint).hostname;
|
|
47
|
+
this.signer = new signature_v4_1.SignatureV4({
|
|
48
|
+
credentials: (0, credential_provider_node_1.defaultProvider)(),
|
|
49
|
+
region: this.region,
|
|
50
|
+
service: 'appsync',
|
|
51
|
+
sha256: sha256_js_1.Sha256,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
50
54
|
}
|
|
51
|
-
async sendMessage(
|
|
55
|
+
async sendMessage(notification) {
|
|
56
|
+
if (!this.endpoint?.trim() || !this.hostname) {
|
|
57
|
+
this.logger.warn('APPSYNC_ENDPOINT is not set, skipping.');
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
52
60
|
const headers = {
|
|
53
61
|
'Content-Type': 'application/json',
|
|
54
62
|
host: this.hostname,
|
|
@@ -56,7 +64,7 @@ let AppSyncService = AppSyncService_1 = class AppSyncService {
|
|
|
56
64
|
const body = JSON.stringify({
|
|
57
65
|
query,
|
|
58
66
|
variables: {
|
|
59
|
-
message: JSON.stringify(
|
|
67
|
+
message: JSON.stringify(notification),
|
|
60
68
|
},
|
|
61
69
|
});
|
|
62
70
|
const method = 'POST';
|
|
@@ -96,7 +104,7 @@ let AppSyncService = AppSyncService_1 = class AppSyncService {
|
|
|
96
104
|
};
|
|
97
105
|
exports.AppSyncService = AppSyncService;
|
|
98
106
|
exports.AppSyncService = AppSyncService = AppSyncService_1 = __decorate([
|
|
99
|
-
(0,
|
|
107
|
+
(0, decorators_1.NotificationTransport)(enums_1.NotificationTransports.APPSYNC_GRAPHQL),
|
|
100
108
|
__metadata("design:paramtypes", [config_1.ConfigService])
|
|
101
109
|
], AppSyncService);
|
|
102
110
|
//# sourceMappingURL=appsync.service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"appsync.service.js","sourceRoot":"","sources":["../../src/notifications/appsync.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAA8C;AAC9C,gFAAmE;AACnE,wDAAmD;AACnD,
|
|
1
|
+
{"version":3,"file":"appsync.service.js","sourceRoot":"","sources":["../../src/notifications/appsync.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAA8C;AAC9C,gFAAmE;AACnE,wDAAmD;AACnD,2CAAuC;AACvC,2CAA8C;AAC9C,4DAA4C;AAE5C,8CAAqD;AAErD,mCAAgD;AAEhD,MAAM,KAAK,GAAG,aAAa,CAAC;;;;;;;;;;;;CAY3B,CAAA;AAGM,IAAM,cAAc,sBAApB,MAAM,cAAc;IASzB,YAA6B,MAAqB;QAArB,WAAM,GAAN,MAAM,CAAe;QARjC,WAAM,GAAG,IAAI,eAAM,CAAC,gBAAc,CAAC,IAAI,CAAC,CAAA;QASvD,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAS,kBAAkB,CAAC,IAAI,EAAE,CAAA;QAC5D,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,GAAG,CAAS,iBAAiB,CAAC,IAAI,EAAE,CAAA;QACzD,IAAI,CAAC,MAAM,GAAG,gBAAgB,CAAA;QAE9B,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClB,IAAI,CAAC,QAAQ,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAA;YAC/C,IAAI,CAAC,MAAM,GAAG,IAAI,0BAAW,CAAC;gBAC5B,WAAW,EAAE,IAAA,0CAAe,GAAE;gBAC9B,MAAM,EAAE,IAAI,CAAC,MAAM;gBACnB,OAAO,EAAE,SAAS;gBAClB,MAAM,EAAE,kBAAM;aACf,CAAC,CAAA;QACJ,CAAC;IACH,CAAC;IAED,KAAK,CAAC,WAAW,CAAC,YAA2B;QAC3C,IAAI,CAAC,IAAI,CAAC,QAAQ,E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aAAa,CAAC,IAAI;aACzB,CAAC,CAAA;QACJ,CAAC;QACD,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAA;QAC7B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,sCAAsC,EAAE,IAAI,CAAC,CAAA;QAE/D,OAAO,IAAI,CAAA;IACb,CAAC;CACF,CAAA;AA3EY,wCAAc;yBAAd,cAAc;IAD1B,IAAA,kCAAqB,EAAC,8BAAsB,CAAC,eAAe,CAAC;qCAUvB,sBAAa;GATvC,cAAc,CA2E1B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './notification-transport.enum';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./notification-transport.enum"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/notifications/enums/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gEAA6C"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotificationTransports = void 0;
|
|
4
|
+
var NotificationTransports;
|
|
5
|
+
(function (NotificationTransports) {
|
|
6
|
+
NotificationTransports["APPSYNC_GRAPHQL"] = "appsync-graphql";
|
|
7
|
+
NotificationTransports["APPSYNC_EVENT"] = "appsync-event";
|
|
8
|
+
})(NotificationTransports || (exports.NotificationTransports = NotificationTransports = {}));
|
|
9
|
+
//# sourceMappingURL=notification-transport.enum.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notification-transport.enum.js","sourceRoot":"","sources":["../../../src/notifications/enums/notification-transport.enum.ts"],"names":[],"mappings":";;;AAAA,IAAY,sBAGX;AAHD,WAAY,sBAAsB;IAChC,6DAAmC,CAAA;IACnC,yDAA+B,CAAA;AACjC,CAAC,EAHW,sBAAsB,sCAAtB,sBAAsB,QAGjC"}
|
|
@@ -1,9 +1,17 @@
|
|
|
1
|
+
import { OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { ConfigService } from '@nestjs/config';
|
|
3
|
+
import { ModuleRef } from '@nestjs/core';
|
|
1
4
|
import { IEventHandler } from '../../interfaces';
|
|
2
|
-
import {
|
|
5
|
+
import { ExplorerService } from '../../services';
|
|
3
6
|
import { NotificationEvent } from './notification.event';
|
|
4
|
-
export declare class NotificationEventHandler implements IEventHandler<NotificationEvent
|
|
5
|
-
private readonly
|
|
7
|
+
export declare class NotificationEventHandler implements IEventHandler<NotificationEvent>, OnModuleInit {
|
|
8
|
+
private readonly moduleRef;
|
|
9
|
+
private readonly explorerService;
|
|
10
|
+
private readonly config;
|
|
6
11
|
private readonly logger;
|
|
7
|
-
|
|
8
|
-
|
|
12
|
+
private readonly transports;
|
|
13
|
+
private readonly activeTransportNames;
|
|
14
|
+
constructor(moduleRef: ModuleRef, explorerService: ExplorerService, config: ConfigService);
|
|
15
|
+
onModuleInit(): void;
|
|
16
|
+
execute(event: NotificationEvent): Promise<void>;
|
|
9
17
|
}
|
|
@@ -12,24 +12,48 @@ var NotificationEventHandler_1;
|
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
exports.NotificationEventHandler = void 0;
|
|
14
14
|
const common_1 = require("@nestjs/common");
|
|
15
|
+
const config_1 = require("@nestjs/config");
|
|
16
|
+
const core_1 = require("@nestjs/core");
|
|
15
17
|
const decorators_1 = require("../../decorators");
|
|
16
|
-
const
|
|
18
|
+
const notification_env_validation_1 = require("../../notification-env.validation");
|
|
19
|
+
const services_1 = require("../../services");
|
|
17
20
|
const notification_event_1 = require("./notification.event");
|
|
18
21
|
let NotificationEventHandler = NotificationEventHandler_1 = class NotificationEventHandler {
|
|
19
|
-
constructor(
|
|
20
|
-
this.
|
|
22
|
+
constructor(moduleRef, explorerService, config) {
|
|
23
|
+
this.moduleRef = moduleRef;
|
|
24
|
+
this.explorerService = explorerService;
|
|
25
|
+
this.config = config;
|
|
21
26
|
this.logger = new common_1.Logger(NotificationEventHandler_1.name);
|
|
27
|
+
this.transports = new Map();
|
|
28
|
+
this.activeTransportNames = (0, notification_env_validation_1.parseNotificationTransports)(this.config.get('NOTIFICATION_TRANSPORTS'));
|
|
29
|
+
}
|
|
30
|
+
onModuleInit() {
|
|
31
|
+
this.logger.debug('Finding notification transports from decorators');
|
|
32
|
+
const { notificationTransports } = this.explorerService.exploreNotificationTransports();
|
|
33
|
+
for (const TransportClass of notificationTransports) {
|
|
34
|
+
const name = Reflect.getMetadata(decorators_1.NOTIFICATION_TRANSPORT_METADATA, TransportClass);
|
|
35
|
+
if (this.activeTransportNames.includes(name)) {
|
|
36
|
+
const instance = this.moduleRef.get(TransportClass, { strict: false });
|
|
37
|
+
// Give the instance its decorator name automatically if it isn't set
|
|
38
|
+
if (!instance.name) {
|
|
39
|
+
instance.name = name;
|
|
40
|
+
}
|
|
41
|
+
this.transports.set(name, instance);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
this.logger.debug(`Initialized active transports: [${[...this.transports.keys()].join(', ')}]`);
|
|
22
45
|
}
|
|
23
46
|
async execute(event) {
|
|
24
|
-
this.logger.debug('notification event executing:: ', event);
|
|
25
|
-
//send to appsync
|
|
26
47
|
const body = JSON.parse(event.body);
|
|
27
|
-
|
|
48
|
+
this.logger.debug(`execute:: broadcasting via ${this.transports.size} transport(s)`);
|
|
49
|
+
await Promise.all([...this.transports.values()].map((t) => t.sendMessage(body)));
|
|
28
50
|
}
|
|
29
51
|
};
|
|
30
52
|
exports.NotificationEventHandler = NotificationEventHandler;
|
|
31
53
|
exports.NotificationEventHandler = NotificationEventHandler = NotificationEventHandler_1 = __decorate([
|
|
32
54
|
(0, decorators_1.EventHandler)(notification_event_1.NotificationEvent),
|
|
33
|
-
__metadata("design:paramtypes", [
|
|
55
|
+
__metadata("design:paramtypes", [core_1.ModuleRef,
|
|
56
|
+
services_1.ExplorerService,
|
|
57
|
+
config_1.ConfigService])
|
|
34
58
|
], NotificationEventHandler);
|
|
35
59
|
//# sourceMappingURL=notification.event.handler.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"notification.event.handler.js","sourceRoot":"","sources":["../../../src/notifications/event/notification.event.handler.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"notification.event.handler.js","sourceRoot":"","sources":["../../../src/notifications/event/notification.event.handler.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAAqD;AACrD,2CAA8C;AAC9C,uCAAwC;AAExC,iDAAgF;AAEhF,mFAA+E;AAC/E,6CAAgD;AAEhD,6DAAwD;AAGjD,IAAM,wBAAwB,gCAA9B,MAAM,wBAAwB;IAOnC,YACmB,SAAoB,EACpB,eAAgC,EAChC,MAAqB;QAFrB,cAAS,GAAT,SAAS,CAAW;QACpB,oBAAe,GAAf,eAAe,CAAiB;QAChC,WAAM,GAAN,MAAM,CAAe;QAPvB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