@yourrentals/cloudevent-receiver-nestjs 0.1.4 → 0.4.2

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 ADDED
@@ -0,0 +1,73 @@
1
+ # @yourrentals/cloudevent-receiver-nestjs
2
+
3
+ This library defines a plugin for NestJS to receive messages from the message bus.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ yarn add @yourrentals/cloudevent-receiver-nestjs @golevelup/nestjs-discovery
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import {CloudEventReceiverModule, CloudEventHandler} from '@yourrentals/cloudevent-receiver-nestjs'
15
+ import {RsaVerifier} from '@yourrentals/message-bus-signature-rsa'
16
+
17
+ @Injectable()
18
+ class MessagebusHandlerService {
19
+ @CloudEventHandler('my-queue')
20
+ async handleMyQueue(event: CloudEvent) {
21
+ // ...
22
+ }
23
+ }
24
+
25
+ @Module({
26
+ imports: [
27
+ CloudEventReceiverModule.forRoot({
28
+ pathPrefix: '/message-bus',
29
+ verifiers: [new RsaVerifier('signature', 'publicKey')],
30
+ }),
31
+ ],
32
+ providers: [MessagebusHandlerService],
33
+ })
34
+
35
+ ```
36
+
37
+ ```ts
38
+ import Axios from 'axios';
39
+
40
+ const main = async () => {
41
+ // Queue name defined in the receiver are automatically registered
42
+ await Axios.post('http://localhost:3000/message-bus/my-queue', {
43
+ headers: {
44
+ 'ce-specversion': '1.0',
45
+ 'ce-type': 'my-event',
46
+ 'ce-source': 'my-source',
47
+ 'ce-id': 'my-id',
48
+ 'ce-time': '2020-01-01T00:00:00Z',
49
+ 'ce-datacontenttype': 'application/json',
50
+ 'ce-dataschema': 'http://myschema.com',
51
+ 'ce-subject': 'my-subject',
52
+ 'ce-signature': 'my-signature',
53
+ },
54
+ body: {
55
+ foo: 'bar',
56
+ },
57
+ });
58
+ };
59
+ ```
60
+
61
+ ## Errors
62
+
63
+ The library defines the following errors:
64
+
65
+ | Error | Description | Expected HTTP status code | Retryable |
66
+ | ---------------------- | --------------------------------------- | ------------------------- | --------- |
67
+ | InvalidSignatureError | The signature of the message is invalid | 401 | No |
68
+ | UnparsableMessageError | The message is not a valid CloudEvent | 400 | No |
69
+ | NotFoundError | The queue name is not registered | 404 | Yes |
70
+ | ConflictError | The queue name is already registered | 409 | Yes |
71
+ | TooManyRequestsError | The queue is currently busy | 429 | Yes |
72
+ | ClientError | Any other client error | 400 | No |
73
+ | ServerError | Any other server error | 500 | Yes |
package/index.cjs.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.cjs.js ADDED
@@ -0,0 +1,163 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var common = require('@nestjs/common');
6
+ var utilReceiverError = require('@yourrentals/util-receiver-error');
7
+ var nestjsDiscovery = require('@golevelup/nestjs-discovery');
8
+ var cloudeventReceiverCore = require('@yourrentals/cloudevent-receiver-core');
9
+
10
+ /******************************************************************************
11
+ Copyright (c) Microsoft Corporation.
12
+
13
+ Permission to use, copy, modify, and/or distribute this software for any
14
+ purpose with or without fee is hereby granted.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
17
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
19
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
20
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22
+ PERFORMANCE OF THIS SOFTWARE.
23
+ ***************************************************************************** */
24
+
25
+ function __decorate(decorators, target, key, desc) {
26
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
27
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
28
+ 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;
29
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
30
+ }
31
+
32
+ function __param(paramIndex, decorator) {
33
+ return function (target, key) { decorator(target, key, paramIndex); }
34
+ }
35
+
36
+ function __metadata(metadataKey, metadataValue) {
37
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
38
+ }
39
+
40
+ function __awaiter(thisArg, _arguments, P, generator) {
41
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
42
+ return new (P || (P = Promise))(function (resolve, reject) {
43
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
44
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
45
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
46
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
47
+ });
48
+ }
49
+
50
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
51
+ var e = new Error(message);
52
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
53
+ };
54
+
55
+ const CLOUD_EVENT_HANDLER = Symbol.for('CLOUD_EVENT_HANDLER');
56
+ const CLOUD_EVENT_RECEIVER_OPTIONS = Symbol.for('CLOUD_EVENT_RECEIVER_OPTIONS');
57
+
58
+ let CloudEventReceiverService = class CloudEventReceiverService {
59
+ constructor(options, discovery) {
60
+ this.options = options;
61
+ this.discovery = discovery;
62
+ }
63
+ onModuleInit() {
64
+ return __awaiter(this, void 0, void 0, function* () {
65
+ const providers = yield this.discovery.providerMethodsWithMetaAtKey(CLOUD_EVENT_HANDLER);
66
+ this.receiver = new cloudeventReceiverCore.CloudEventReceiver({
67
+ queues: providers.map((provider) => ({
68
+ name: provider.meta.name,
69
+ handler: provider.discoveredMethod.handler.bind(provider.discoveredMethod.parentClass.instance),
70
+ })),
71
+ verifiers: this.options.verifiers || [],
72
+ });
73
+ });
74
+ }
75
+ handle(queueName, headers, body) {
76
+ return __awaiter(this, void 0, void 0, function* () {
77
+ return this.receiver.handle(queueName, headers, body);
78
+ });
79
+ }
80
+ };
81
+ CloudEventReceiverService = __decorate([
82
+ common.Injectable(),
83
+ __param(0, common.Inject(CLOUD_EVENT_RECEIVER_OPTIONS)),
84
+ __metadata("design:paramtypes", [Object, nestjsDiscovery.DiscoveryService])
85
+ ], CloudEventReceiverService);
86
+
87
+ const createController = (pathPrefix = '/message-bus') => {
88
+ let CloudEventReceiverController = class CloudEventReceiverController {
89
+ constructor(service) {
90
+ this.service = service;
91
+ }
92
+ receiveMessage(request) {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ try {
95
+ const queueName = request.params.queueName;
96
+ yield this.service.handle(queueName, request.headers, request.body);
97
+ }
98
+ catch (e) {
99
+ if (utilReceiverError.isReceiverError(e)) {
100
+ throw new common.HttpException(e.message, e.statusCode);
101
+ }
102
+ throw new common.InternalServerErrorException();
103
+ }
104
+ });
105
+ }
106
+ };
107
+ __decorate([
108
+ common.Post('/:queueName'),
109
+ common.HttpCode(200),
110
+ __param(0, common.Req()),
111
+ __metadata("design:type", Function),
112
+ __metadata("design:paramtypes", [Object]),
113
+ __metadata("design:returntype", Promise)
114
+ ], CloudEventReceiverController.prototype, "receiveMessage", null);
115
+ CloudEventReceiverController = __decorate([
116
+ common.Controller(pathPrefix),
117
+ __param(0, common.Inject(CloudEventReceiverService)),
118
+ __metadata("design:paramtypes", [CloudEventReceiverService])
119
+ ], CloudEventReceiverController);
120
+ return CloudEventReceiverController;
121
+ };
122
+
123
+ var CloudEventReceiverModule_1;
124
+ exports.CloudEventReceiverModule = CloudEventReceiverModule_1 = class CloudEventReceiverModule {
125
+ static forRoot(options) {
126
+ return {
127
+ module: CloudEventReceiverModule_1,
128
+ global: true,
129
+ imports: [nestjsDiscovery.DiscoveryModule],
130
+ providers: [
131
+ {
132
+ provide: CLOUD_EVENT_RECEIVER_OPTIONS,
133
+ useValue: options,
134
+ },
135
+ {
136
+ provide: CloudEventReceiverService,
137
+ useFactory: (options, discovery) => new CloudEventReceiverService(options, discovery),
138
+ inject: [CLOUD_EVENT_RECEIVER_OPTIONS, nestjsDiscovery.DiscoveryService],
139
+ },
140
+ ],
141
+ controllers: [createController(options.pathPrefix)],
142
+ };
143
+ }
144
+ };
145
+ exports.CloudEventReceiverModule = CloudEventReceiverModule_1 = __decorate([
146
+ common.Module({})
147
+ ], exports.CloudEventReceiverModule);
148
+
149
+ const CloudEventHandler = (queueName) => common.SetMetadata(CLOUD_EVENT_HANDLER, { name: queueName });
150
+
151
+ exports.CloudEventHandler = CloudEventHandler;
152
+ Object.keys(utilReceiverError).forEach(function (k) {
153
+ if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
154
+ enumerable: true,
155
+ get: function () { return utilReceiverError[k]; }
156
+ });
157
+ });
158
+ Object.keys(cloudeventReceiverCore).forEach(function (k) {
159
+ if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
160
+ enumerable: true,
161
+ get: function () { return cloudeventReceiverCore[k]; }
162
+ });
163
+ });
package/index.esm.js ADDED
@@ -0,0 +1,149 @@
1
+ import { Injectable, Inject, Post, HttpCode, Req, Controller, HttpException, InternalServerErrorException, Module, SetMetadata } from '@nestjs/common';
2
+ import { isReceiverError } from '@yourrentals/util-receiver-error';
3
+ export * from '@yourrentals/util-receiver-error';
4
+ import { DiscoveryService, DiscoveryModule } from '@golevelup/nestjs-discovery';
5
+ import { CloudEventReceiver } from '@yourrentals/cloudevent-receiver-core';
6
+ export * from '@yourrentals/cloudevent-receiver-core';
7
+
8
+ /******************************************************************************
9
+ Copyright (c) Microsoft Corporation.
10
+
11
+ Permission to use, copy, modify, and/or distribute this software for any
12
+ purpose with or without fee is hereby granted.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
15
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
17
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20
+ PERFORMANCE OF THIS SOFTWARE.
21
+ ***************************************************************************** */
22
+
23
+ function __decorate(decorators, target, key, desc) {
24
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
25
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
26
+ 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;
27
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
28
+ }
29
+
30
+ function __param(paramIndex, decorator) {
31
+ return function (target, key) { decorator(target, key, paramIndex); }
32
+ }
33
+
34
+ function __metadata(metadataKey, metadataValue) {
35
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
36
+ }
37
+
38
+ function __awaiter(thisArg, _arguments, P, generator) {
39
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
40
+ return new (P || (P = Promise))(function (resolve, reject) {
41
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
42
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
43
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
44
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
45
+ });
46
+ }
47
+
48
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
49
+ var e = new Error(message);
50
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
51
+ };
52
+
53
+ const CLOUD_EVENT_HANDLER = Symbol.for('CLOUD_EVENT_HANDLER');
54
+ const CLOUD_EVENT_RECEIVER_OPTIONS = Symbol.for('CLOUD_EVENT_RECEIVER_OPTIONS');
55
+
56
+ let CloudEventReceiverService = class CloudEventReceiverService {
57
+ constructor(options, discovery) {
58
+ this.options = options;
59
+ this.discovery = discovery;
60
+ }
61
+ onModuleInit() {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ const providers = yield this.discovery.providerMethodsWithMetaAtKey(CLOUD_EVENT_HANDLER);
64
+ this.receiver = new CloudEventReceiver({
65
+ queues: providers.map((provider) => ({
66
+ name: provider.meta.name,
67
+ handler: provider.discoveredMethod.handler.bind(provider.discoveredMethod.parentClass.instance),
68
+ })),
69
+ verifiers: this.options.verifiers || [],
70
+ });
71
+ });
72
+ }
73
+ handle(queueName, headers, body) {
74
+ return __awaiter(this, void 0, void 0, function* () {
75
+ return this.receiver.handle(queueName, headers, body);
76
+ });
77
+ }
78
+ };
79
+ CloudEventReceiverService = __decorate([
80
+ Injectable(),
81
+ __param(0, Inject(CLOUD_EVENT_RECEIVER_OPTIONS)),
82
+ __metadata("design:paramtypes", [Object, DiscoveryService])
83
+ ], CloudEventReceiverService);
84
+
85
+ const createController = (pathPrefix = '/message-bus') => {
86
+ let CloudEventReceiverController = class CloudEventReceiverController {
87
+ constructor(service) {
88
+ this.service = service;
89
+ }
90
+ receiveMessage(request) {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ try {
93
+ const queueName = request.params.queueName;
94
+ yield this.service.handle(queueName, request.headers, request.body);
95
+ }
96
+ catch (e) {
97
+ if (isReceiverError(e)) {
98
+ throw new HttpException(e.message, e.statusCode);
99
+ }
100
+ throw new InternalServerErrorException();
101
+ }
102
+ });
103
+ }
104
+ };
105
+ __decorate([
106
+ Post('/:queueName'),
107
+ HttpCode(200),
108
+ __param(0, Req()),
109
+ __metadata("design:type", Function),
110
+ __metadata("design:paramtypes", [Object]),
111
+ __metadata("design:returntype", Promise)
112
+ ], CloudEventReceiverController.prototype, "receiveMessage", null);
113
+ CloudEventReceiverController = __decorate([
114
+ Controller(pathPrefix),
115
+ __param(0, Inject(CloudEventReceiverService)),
116
+ __metadata("design:paramtypes", [CloudEventReceiverService])
117
+ ], CloudEventReceiverController);
118
+ return CloudEventReceiverController;
119
+ };
120
+
121
+ var CloudEventReceiverModule_1;
122
+ let CloudEventReceiverModule = CloudEventReceiverModule_1 = class CloudEventReceiverModule {
123
+ static forRoot(options) {
124
+ return {
125
+ module: CloudEventReceiverModule_1,
126
+ global: true,
127
+ imports: [DiscoveryModule],
128
+ providers: [
129
+ {
130
+ provide: CLOUD_EVENT_RECEIVER_OPTIONS,
131
+ useValue: options,
132
+ },
133
+ {
134
+ provide: CloudEventReceiverService,
135
+ useFactory: (options, discovery) => new CloudEventReceiverService(options, discovery),
136
+ inject: [CLOUD_EVENT_RECEIVER_OPTIONS, DiscoveryService],
137
+ },
138
+ ],
139
+ controllers: [createController(options.pathPrefix)],
140
+ };
141
+ }
142
+ };
143
+ CloudEventReceiverModule = CloudEventReceiverModule_1 = __decorate([
144
+ Module({})
145
+ ], CloudEventReceiverModule);
146
+
147
+ const CloudEventHandler = (queueName) => SetMetadata(CLOUD_EVENT_HANDLER, { name: queueName });
148
+
149
+ export { CloudEventHandler, CloudEventReceiverModule };
package/package.json CHANGED
@@ -1,10 +1,15 @@
1
1
  {
2
2
  "name": "@yourrentals/cloudevent-receiver-nestjs",
3
- "version": "0.1.4",
3
+ "version": "0.4.2",
4
4
  "dependencies": {
5
- "@golevelup/nestjs-discovery": "^4.0.0"
5
+ "@yourrentals/cloudevent-receiver-core": "0.4.2",
6
+ "@yourrentals/util-receiver-error": "0.4.2"
6
7
  },
7
8
  "peerDependencies": {
8
- "@nestjs/common": "^6.0.0 || ^7.0.0 || ^8.0.0 || ^9.0.0 || ^10.0.0"
9
- }
9
+ "@nestjs/common": "^6.0.0 || ^7.0.0 || ^8.0.0 || ^9.0.0 || ^10.0.0",
10
+ "@golevelup/nestjs-discovery": "^3.0.0 || ^4.0.0"
11
+ },
12
+ "type": "commonjs",
13
+ "main": "./index.cjs.js",
14
+ "module": "./index.esm.js"
10
15
  }
@@ -1,4 +1,4 @@
1
1
  export * from './lib/cloud-event-receiver.module';
2
2
  export * from './lib/cloud-event-receiver.decorators';
3
- export * from '@message-bus/cloudevent-receiver-core';
4
- export * from '@message-bus/util-receiver-error';
3
+ export * from '@yourrentals/cloudevent-receiver-core';
4
+ export * from '@yourrentals/util-receiver-error';
@@ -0,0 +1,7 @@
1
+ import { CloudEventReceiverService } from './cloud-event-receiver.service';
2
+ export declare const createController: (pathPrefix?: string) => {
3
+ new (service: CloudEventReceiverService): {
4
+ readonly service: CloudEventReceiverService;
5
+ receiveMessage(request: any): Promise<void>;
6
+ };
7
+ };
@@ -0,0 +1,12 @@
1
+ import { OnModuleInit } from '@nestjs/common';
2
+ import { DiscoveryService } from '@golevelup/nestjs-discovery';
3
+ import { CloudEventReceiver } from '@yourrentals/cloudevent-receiver-core';
4
+ import { CloudEventReceiverModuleOptions } from './cloud-event-receiver.types';
5
+ export declare class CloudEventReceiverService implements OnModuleInit {
6
+ readonly options: CloudEventReceiverModuleOptions;
7
+ readonly discovery: DiscoveryService;
8
+ receiver: CloudEventReceiver;
9
+ constructor(options: CloudEventReceiverModuleOptions, discovery: DiscoveryService);
10
+ onModuleInit(): Promise<void>;
11
+ handle(queueName: string, headers: any, body: any): Promise<void>;
12
+ }
@@ -0,0 +1,5 @@
1
+ import type { CloudEventReceiverOptions } from '@yourrentals/cloudevent-receiver-core';
2
+ export type CloudEventReceiverModuleOptions = {
3
+ pathPrefix?: string;
4
+ verifiers?: CloudEventReceiverOptions['verifiers'];
5
+ };