amqp-extension 0.0.4 → 1.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.
Files changed (54) hide show
  1. package/README.MD +117 -19
  2. package/dist/config/index.d.ts +2 -0
  3. package/dist/config/index.js +15 -0
  4. package/dist/config/index.js.map +1 -0
  5. package/dist/config/type.d.ts +15 -0
  6. package/dist/{connection → config}/type.js +0 -0
  7. package/dist/{connection → config}/type.js.map +1 -1
  8. package/dist/config/utils.d.ts +4 -0
  9. package/dist/config/utils.js +39 -0
  10. package/dist/config/utils.js.map +1 -0
  11. package/dist/connection/index.d.ts +0 -1
  12. package/dist/connection/index.js +0 -1
  13. package/dist/connection/index.js.map +1 -1
  14. package/dist/connection/utils.d.ts +5 -5
  15. package/dist/connection/utils.js +11 -9
  16. package/dist/connection/utils.js.map +1 -1
  17. package/dist/consume/index.d.ts +3 -0
  18. package/dist/consume/index.js +16 -0
  19. package/dist/consume/index.js.map +1 -0
  20. package/dist/consume/static.d.ts +1 -0
  21. package/dist/consume/static.js +5 -0
  22. package/dist/consume/static.js.map +1 -0
  23. package/dist/consume/type.d.ts +33 -0
  24. package/dist/consume/type.js +3 -0
  25. package/dist/consume/type.js.map +1 -0
  26. package/dist/consume/utils.d.ts +2 -0
  27. package/dist/consume/utils.js +130 -0
  28. package/dist/consume/utils.js.map +1 -0
  29. package/dist/index.d.ts +4 -0
  30. package/dist/index.js +4 -0
  31. package/dist/index.js.map +1 -1
  32. package/dist/message/build.d.ts +2 -0
  33. package/dist/message/build.js +16 -0
  34. package/dist/message/build.js.map +1 -0
  35. package/dist/message/index.d.ts +2 -0
  36. package/dist/message/index.js +15 -0
  37. package/dist/message/index.js.map +1 -0
  38. package/dist/message/type.d.ts +46 -0
  39. package/dist/message/type.js +3 -0
  40. package/dist/message/type.js.map +1 -0
  41. package/dist/publish/index.d.ts +2 -0
  42. package/dist/publish/index.js +15 -0
  43. package/dist/publish/index.js.map +1 -0
  44. package/dist/publish/type.d.ts +12 -0
  45. package/dist/publish/type.js +3 -0
  46. package/dist/publish/type.js.map +1 -0
  47. package/dist/publish/utils.d.ts +3 -0
  48. package/dist/publish/utils.js +87 -0
  49. package/dist/publish/utils.js.map +1 -0
  50. package/dist/utils.d.ts +6 -0
  51. package/dist/utils.js +81 -0
  52. package/dist/utils.js.map +1 -0
  53. package/package.json +3 -2
  54. package/dist/connection/type.d.ts +0 -1
package/README.MD CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  # AMQP Extension 🏰
6
6
 
7
- This is a library on top of the famous [amqplib](https://www.npmjs.com/package/amqplib) library and defines a [message format](#message-types) for queue message through a message broker across multiple standalone services.
7
+ This is a library on top of the famous [amqplib](https://www.npmjs.com/package/amqplib) library and defines a [message format](#message-types) for queue messages through a message broker across multiple standalone services.
8
8
  All utility functions support the usage of multiple registered connections.
9
9
 
10
10
  **Table of Contents**
@@ -14,6 +14,7 @@ All utility functions support the usage of multiple registered connections.
14
14
  - [Publish](#publish)
15
15
  - [Consume](#consume)
16
16
  - [Functions](#functions)
17
+ - [setConfig](#setconfig)
17
18
  - [useConnection](#useconnection)
18
19
  - [publishMessage](#publishmessage)
19
20
  - [consumeQueue](#consumequeue)
@@ -44,11 +45,11 @@ import {
44
45
  Message,
45
46
  useConnection,
46
47
  publishMessage,
47
- useConnection
48
+ setConfig
48
49
  } from "amqp-extension";
49
50
 
50
51
  // This will set the default connection :)
51
- useConnection({
52
+ setConfig({
52
53
  connection: 'amqp://<user>:<password>@<host>',
53
54
  exchange: {
54
55
  name: '<name>',
@@ -82,12 +83,12 @@ import {
82
83
  ConsumeOptions,
83
84
  Message,
84
85
  MessageContext,
85
- useConnection,
86
- publishMessage
86
+ publishMessage,
87
+ setConfig
87
88
  } from "amqp-extension";
88
89
 
89
90
  // This will set the default connection :)
90
- useConnection({
91
+ setConfig({
91
92
  connection: 'amqp://<user>:<password>@<host>',
92
93
  exchange: {
93
94
  name: '<name>',
@@ -121,13 +122,13 @@ import {
121
122
  ConsumeOptions,
122
123
  Message,
123
124
  MessageContext,
124
- useConnection,
125
- publishMessage
125
+ publishMessage,
126
+ PublishOptions,
127
+ setConfig
126
128
  } from "amqp-extension";
127
- import {PublishOptions} from "./type";
128
129
 
129
130
  // This will set the default connection :)
130
- useConnection({
131
+ setConfig({
131
132
  connection: 'amqp://<user>:<password>@<host>',
132
133
  exchange: {
133
134
  name: '<name>',
@@ -135,7 +136,7 @@ useConnection({
135
136
  }
136
137
  });
137
138
 
138
- useConnection({
139
+ setConfig({
139
140
  alias: 'foo',
140
141
  connection: 'amqp://<user>:<password>@<host>',
141
142
  exchange: {
@@ -162,29 +163,126 @@ const publishOptions: PublishOptions = {
162
163
 
163
164
  ## Functions
164
165
 
166
+ ### setConfig
167
+
168
+ ▸ `function` **setConfig**(`key?: string | Config`, `value?: Config`): `Config`
169
+
170
+ Register a connection as `default` alias or specify an `<alias>` as config property.
171
+
172
+ #### Example
173
+ **`Simple`**
174
+
175
+ Define the default connection config.
176
+
177
+ ```typescript
178
+ import {setConfig, useConnection} from "amqp-extension";
179
+
180
+ (async () => {
181
+ setConfig({
182
+ connection: 'amqp://<user>:<password>@<host>',
183
+ exchange: {
184
+ name: '<name>',
185
+ type: 'topic'
186
+ }
187
+ });
188
+
189
+ const connection = await useConnection();
190
+ })();
191
+
192
+ ```
193
+
194
+ Define a non default connection.
195
+
196
+ ```typescript
197
+ import {setConfig, useConnection} from "amqp-extension";
198
+
199
+ (async () => {
200
+ setConfig({
201
+ alias: '<alias>',
202
+ connection: 'amqp://<user>:<password>@<host>',
203
+ exchange: {
204
+ name: '<name>',
205
+ type: 'topic'
206
+ }
207
+ });
208
+
209
+ setConfig('foo', {
210
+ connection: 'amqp://<user>:<password>@<host>',
211
+ exchange: {
212
+ name: '<name>',
213
+ type: 'topic'
214
+ }
215
+ });
216
+
217
+ const connection = await useConnection('<alias>');
218
+ const fooConnection = await useConnection('foo');
219
+ })();
220
+
221
+ ```
222
+
223
+
224
+ #### Type parameters
225
+
226
+ | Name | Description |
227
+ | :------ | :------ |
228
+
229
+
230
+
231
+ #### Parameters
232
+
233
+ | Name | Type | Description |
234
+ | :------ | :------ | :------ |
235
+ | `key` | `string` or `Config` | Config object or alias of config. [more](#config-types) |
236
+ | `value` | `Config` | Config object. [more](#config-types) |
237
+
238
+ #### Returns
239
+
240
+ `Config`
241
+
242
+ The function returns the Config object.
243
+
165
244
  ### useConnection
166
245
 
167
246
  ▸ `function` **useConnection**(`key?: string | Config`): `Promise<Connection>`
168
247
 
169
248
  Either register a connection as `default` alias or specify an `alias` as config property.
170
249
  If you have registered a connection you can receive the connection by specifying no arguments or provide an alias name if specified.
250
+
251
+
171
252
  #### Example
172
253
  **`Simple`**
173
254
 
255
+ Receive underlying driver connection.
256
+
174
257
  ```typescript
175
258
  import {useConnection} from "amqp-extension";
176
259
 
177
260
  (async () => {
261
+ const connection = await useConnection();
262
+ })();
263
+
264
+ ```
265
+
266
+ **`Advanced`**
267
+
268
+ Use a none `default` connection.
269
+
270
+ ```typescript
271
+ import {setConfig, useConnection} from "amqp-extension";
272
+
273
+ (async () => {
274
+ setConfig({
275
+ alias: '<alias>',
276
+ connection: 'amqp://<user>:<password>@<host>',
277
+ exchange: {
278
+ name: '<name>',
279
+ type: 'topic'
280
+ }
281
+ });
178
282
 
283
+ const connection = await useConnection('<alias>');
179
284
  })();
180
- const connection = useConnection({
181
- alias: '<alias>',
182
- connection: 'amqp://<user>:<password>@<host>',
183
- exchange: {
184
- name: '<name>',
185
- type: 'topic'
186
- }
187
- });
285
+
188
286
  ```
189
287
 
190
288
  #### Type parameters
@@ -0,0 +1,2 @@
1
+ export * from './type';
2
+ export * from './utils';
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./type"), exports);
14
+ __exportStar(require("./utils"), exports);
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yCAAuB;AACvB,0CAAwB"}
@@ -0,0 +1,15 @@
1
+ import { Options } from "amqplib";
2
+ import { ConsumeOptions } from "../consume";
3
+ import { PublishOptions } from "../publish";
4
+ export declare type ExchangeType = 'fanout' | 'direct' | 'topic' | 'match' | string;
5
+ export declare type Config = {
6
+ alias?: string;
7
+ connection: Options.Connect | string;
8
+ exchange: {
9
+ name: string;
10
+ type: ExchangeType;
11
+ options?: Options.AssertExchange;
12
+ };
13
+ publish?: PublishOptions;
14
+ consume?: ConsumeOptions;
15
+ };
File without changes
@@ -1 +1 @@
1
- {"version":3,"file":"type.js","sourceRoot":"","sources":["../../src/connection/type.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../src/config/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ import { Config } from "./type";
2
+ export declare const DEFAULT_KEY: string;
3
+ export declare function setConfig(key: string | Config, value?: Config): Config;
4
+ export declare function getConfig(key?: string | Config): Config;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getConfig = exports.setConfig = exports.DEFAULT_KEY = void 0;
4
+ exports.DEFAULT_KEY = 'default';
5
+ var configMap = new Map();
6
+ function setConfig(key, value) {
7
+ var _a;
8
+ if (typeof key === 'string') {
9
+ if (typeof value === 'undefined') {
10
+ throw new Error("A config must be defined for the alias: " + key);
11
+ }
12
+ value.alias = key;
13
+ configMap.set(key, value);
14
+ return value;
15
+ }
16
+ else {
17
+ (_a = key.alias) !== null && _a !== void 0 ? _a : (key.alias = exports.DEFAULT_KEY);
18
+ configMap.set(key.alias, key);
19
+ return key;
20
+ }
21
+ }
22
+ exports.setConfig = setConfig;
23
+ function getConfig(key) {
24
+ var _a;
25
+ key !== null && key !== void 0 ? key : (key = exports.DEFAULT_KEY);
26
+ if (typeof key === 'string') {
27
+ var data = configMap.get(key);
28
+ if (typeof data === 'undefined') {
29
+ throw new Error("A config must be defined for the alias: " + key);
30
+ }
31
+ return data;
32
+ }
33
+ var config = key;
34
+ (_a = config.alias) !== null && _a !== void 0 ? _a : (config.alias = exports.DEFAULT_KEY);
35
+ setConfig(config);
36
+ return config;
37
+ }
38
+ exports.getConfig = getConfig;
39
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/config/utils.ts"],"names":[],"mappings":";;;AAEa,QAAA,WAAW,GAAY,SAAS,CAAC;AAE9C,IAAM,SAAS,GAAwB,IAAI,GAAG,EAAkB,CAAC;AAEjE,SAAgB,SAAS,CAAC,GAAoB,EAAE,KAAc;;IAC1D,IAAG,OAAO,GAAG,KAAK,QAAQ,EAAE;QACxB,IAAG,OAAO,KAAK,KAAK,WAAW,EAAE;YAC7B,MAAM,IAAI,KAAK,CAAC,6CAA2C,GAAK,CAAC,CAAC;SACrE;QAED,KAAK,CAAC,KAAK,GAAG,GAAG,CAAC;QAElB,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAE1B,OAAO,KAAK,CAAC;KAChB;SAAM;QACH,MAAA,GAAG,CAAC,KAAK,oCAAT,GAAG,CAAC,KAAK,GAAK,mBAAW,EAAC;QAC1B,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAE9B,OAAO,GAAG,CAAC;KACd;AACL,CAAC;AAjBD,8BAiBC;AAED,SAAgB,SAAS,CAAC,GAAqB;;IAC3C,GAAG,aAAH,GAAG,cAAH,GAAG,IAAH,GAAG,GAAK,mBAAW,EAAC;IAEpB,IAAG,OAAO,GAAG,KAAK,QAAQ,EAAE;QACxB,IAAM,IAAI,GAAwB,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACrD,IAAG,OAAO,IAAI,KAAK,WAAW,EAAE;YAC5B,MAAM,IAAI,KAAK,CAAC,6CAA2C,GAAK,CAAC,CAAC;SACrE;QAED,OAAO,IAAI,CAAC;KACf;IAED,IAAM,MAAM,GAAY,GAAG,CAAC;IAC5B,MAAA,MAAM,CAAC,KAAK,oCAAZ,MAAM,CAAC,KAAK,GAAK,mBAAW,EAAC;IAE7B,SAAS,CAAC,MAAM,CAAC,CAAC;IAElB,OAAO,MAAM,CAAC;AAClB,CAAC;AAlBD,8BAkBC"}
@@ -1,2 +1 @@
1
- export * from './type';
2
1
  export * from './utils';
@@ -10,6 +10,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./type"), exports);
14
13
  __exportStar(require("./utils"), exports);
15
14
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/connection/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yCAAuB;AACvB,0CAAwB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/connection/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,0CAAwB"}
@@ -1,6 +1,6 @@
1
- import RabbitMQ, { Connection } from "amqplib";
2
- import { ConnectionAlias } from "./type";
1
+ import RabbitMQ, { Connection, Options } from "amqplib";
2
+ import { Config } from "../config";
3
3
  export declare function clearConnections(): void;
4
- export declare function getConnections(): Map<ConnectionAlias, Connection>;
5
- export declare function useConnection(alias: ConnectionAlias, config: string): Promise<Connection>;
6
- export declare function createConnection(config: string): Promise<RabbitMQ.Connection>;
4
+ export declare function getConnections(): Map<string, Connection>;
5
+ export declare function useConnection(key: string | Config): Promise<Connection>;
6
+ export declare function createConnection(config: Options.Connect | string): Promise<RabbitMQ.Connection>;
@@ -41,29 +41,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
42
  exports.createConnection = exports.useConnection = exports.getConnections = exports.clearConnections = void 0;
43
43
  var amqplib_1 = __importDefault(require("amqplib"));
44
- var connections = new Map();
44
+ var config_1 = require("../config");
45
+ var connectionMap = new Map();
45
46
  function clearConnections() {
46
- connections.clear();
47
+ connectionMap.clear();
47
48
  }
48
49
  exports.clearConnections = clearConnections;
49
50
  function getConnections() {
50
- return connections;
51
+ return connectionMap;
51
52
  }
52
53
  exports.getConnections = getConnections;
53
54
  /* istanbul ignore next */
54
- function useConnection(alias, config) {
55
+ function useConnection(key) {
55
56
  return __awaiter(this, void 0, void 0, function () {
56
- var connection;
57
+ var config, connection;
57
58
  return __generator(this, function (_a) {
58
59
  switch (_a.label) {
59
60
  case 0:
60
- if (connections.has(alias)) {
61
- return [2 /*return*/, connections.get(alias)];
61
+ config = (0, config_1.getConfig)(key);
62
+ if (connectionMap.has(config.alias)) {
63
+ return [2 /*return*/, connectionMap.get(config.alias)];
62
64
  }
63
- return [4 /*yield*/, createConnection(config)];
65
+ return [4 /*yield*/, createConnection(config.connection)];
64
66
  case 1:
65
67
  connection = _a.sent();
66
- connections.set(alias, connection);
68
+ connectionMap.set(config.alias, connection);
67
69
  return [2 /*return*/, connection];
68
70
  }
69
71
  });
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/connection/utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAA6C;AAG7C,IAAI,WAAW,GAAqC,IAAI,GAAG,EAAsB,CAAC;AAElF,SAAgB,gBAAgB;IAC5B,WAAW,CAAC,KAAK,EAAE,CAAC;AACxB,CAAC;AAFD,4CAEC;AAED,SAAgB,cAAc;IAC1B,OAAO,WAAW,CAAC;AACvB,CAAC;AAFD,wCAEC;AAED,0BAA0B;AAC1B,SAAsB,aAAa,CAAC,KAAsB,EAAE,MAAc;;;;;;oBACtE,IAAG,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;wBACvB,sBAAO,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,EAAC;qBACjC;oBAEkB,qBAAM,gBAAgB,CAAC,MAAM,CAAC,EAAA;;oBAA3C,UAAU,GAAG,SAA8B;oBAEjD,WAAW,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;oBAEnC,sBAAO,UAAU,EAAC;;;;CACrB;AAVD,sCAUC;AAED,0BAA0B;AAC1B,SAAsB,gBAAgB,CAAC,MAAc;;;;wBAC1C,qBAAM,iBAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,EAAA;wBAArC,sBAAO,SAA8B,EAAC;;;;CACzC;AAFD,4CAEC"}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/connection/utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAsD;AACtD,oCAA4C;AAE5C,IAAM,aAAa,GAA4B,IAAI,GAAG,EAAsB,CAAC;AAE7E,SAAgB,gBAAgB;IAC5B,aAAa,CAAC,KAAK,EAAE,CAAC;AAC1B,CAAC;AAFD,4CAEC;AAED,SAAgB,cAAc;IAC1B,OAAO,aAAa,CAAC;AACzB,CAAC;AAFD,wCAEC;AAED,0BAA0B;AAC1B,SAAsB,aAAa,CAC/B,GAAoB;;;;;;oBAEd,MAAM,GAAY,IAAA,kBAAS,EAAC,GAAG,CAAC,CAAC;oBAEvC,IAAG,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;wBAChC,sBAAO,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,EAAC;qBAC1C;oBAEkB,qBAAM,gBAAgB,CAAC,MAAM,CAAC,UAAU,CAAC,EAAA;;oBAAtD,UAAU,GAAG,SAAyC;oBAE5D,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;oBAE5C,sBAAO,UAAU,EAAC;;;;CACrB;AAdD,sCAcC;AAED,0BAA0B;AAC1B,SAAsB,gBAAgB,CAAC,MAAgC;;;;wBAC5D,qBAAM,iBAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,EAAA;wBAArC,sBAAO,SAA8B,EAAC;;;;CACzC;AAFD,4CAEC"}
@@ -0,0 +1,3 @@
1
+ export * from './static';
2
+ export * from './utils';
3
+ export * from './type';
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./static"), exports);
14
+ __exportStar(require("./utils"), exports);
15
+ __exportStar(require("./type"), exports);
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/consume/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAyB;AACzB,0CAAwB;AACxB,yCAAuB"}
@@ -0,0 +1 @@
1
+ export declare const ConsumeHandlerAnyKey = "$any";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConsumeHandlerAnyKey = void 0;
4
+ exports.ConsumeHandlerAnyKey = '$any';
5
+ //# sourceMappingURL=static.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"static.js","sourceRoot":"","sources":["../../src/consume/static.ts"],"names":[],"mappings":";;;AAAa,QAAA,oBAAoB,GAAG,MAAM,CAAC"}
@@ -0,0 +1,33 @@
1
+ import { Options } from "amqplib";
2
+ import { Config } from "../config";
3
+ import { Message, MessageContext } from "../message";
4
+ import { ConsumeHandlerAnyKey } from "./static";
5
+ export declare type ConsumeHandler = (message: Message, context: MessageContext) => Promise<void>;
6
+ export declare type ConsumeHandlerAnyKeyType = typeof ConsumeHandlerAnyKey;
7
+ export declare type ConsumeHandlers = Record<ConsumeHandlerAnyKeyType | string, ConsumeHandler>;
8
+ export declare type ConsumeOptions = {
9
+ /**
10
+ * Queue routing key(s).
11
+ */
12
+ routingKey?: string | string[];
13
+ /**
14
+ * Config key or object.
15
+ */
16
+ alias?: string | Config;
17
+ /**
18
+ * Queue name.
19
+ *
20
+ * Default: ''
21
+ */
22
+ name?: string;
23
+ /**
24
+ * Amqplib consume options.
25
+ *
26
+ * Default: { }
27
+ */
28
+ options?: Options.Consume;
29
+ /**
30
+ * Default: false
31
+ */
32
+ requeueOnFailure?: boolean;
33
+ };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../src/consume/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ import { ConsumeHandlers, ConsumeOptions } from "./type";
2
+ export declare function consumeQueue(options: ConsumeOptions, handlers: ConsumeHandlers): Promise<void>;
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.consumeQueue = void 0;
51
+ var config_1 = require("../config");
52
+ var utils_1 = require("../utils");
53
+ var static_1 = require("./static");
54
+ /* istanbul ignore next */
55
+ function consumeQueue(options, handlers) {
56
+ var _a, _b, _c, _d;
57
+ return __awaiter(this, void 0, void 0, function () {
58
+ var config, _e, channel, connection, queueName, assertionQueue, routingKeys, promises, consumeOptions;
59
+ var _this = this;
60
+ return __generator(this, function (_f) {
61
+ switch (_f.label) {
62
+ case 0:
63
+ config = (0, config_1.getConfig)(options.alias);
64
+ return [4 /*yield*/, (0, utils_1.createChannel)(config)];
65
+ case 1:
66
+ _e = _f.sent(), channel = _e.channel, connection = _e.connection;
67
+ queueName = (_a = options.name) !== null && _a !== void 0 ? _a : '';
68
+ return [4 /*yield*/, channel.assertQueue(queueName, {
69
+ durable: false,
70
+ autoDelete: true
71
+ })];
72
+ case 2:
73
+ assertionQueue = _f.sent();
74
+ if (!(typeof options.routingKey !== 'undefined')) return [3 /*break*/, 4];
75
+ routingKeys = Array.isArray(options.routingKey) ? options.routingKey : [options.routingKey];
76
+ promises = routingKeys.map(function (routKey) {
77
+ return channel.bindQueue(assertionQueue.queue, config.exchange.name, routKey);
78
+ });
79
+ return [4 /*yield*/, Promise.all(promises)];
80
+ case 3:
81
+ _f.sent();
82
+ _f.label = 4;
83
+ case 4:
84
+ consumeOptions = __assign(__assign({}, ((_c = (_b = config.consume) === null || _b === void 0 ? void 0 : _b.options) !== null && _c !== void 0 ? _c : {})), ((_d = options.options) !== null && _d !== void 0 ? _d : {}));
85
+ return [4 /*yield*/, channel.consume(assertionQueue.queue, ((function (message) { return __awaiter(_this, void 0, void 0, function () {
86
+ var content, handler, context, requeueOnFailure, e_1;
87
+ var _a, _b, _c;
88
+ return __generator(this, function (_d) {
89
+ switch (_d.label) {
90
+ case 0:
91
+ if (!message) {
92
+ return [2 /*return*/];
93
+ }
94
+ content = JSON.parse(message.content.toString('utf-8'));
95
+ handler = (_a = handlers[content.type]) !== null && _a !== void 0 ? _a : handlers[static_1.ConsumeHandlerAnyKey];
96
+ context = {
97
+ channel: channel,
98
+ connection: connection,
99
+ messageFields: message.fields,
100
+ messageProperties: message.properties
101
+ };
102
+ requeueOnFailure = (_c = (_b = config.consume) === null || _b === void 0 ? void 0 : _b.requeueOnFailure) !== null && _c !== void 0 ? _c : false;
103
+ if (typeof handler === 'undefined') {
104
+ return [2 /*return*/, channel.reject(message, requeueOnFailure)];
105
+ }
106
+ _d.label = 1;
107
+ case 1:
108
+ _d.trys.push([1, 3, , 4]);
109
+ return [4 /*yield*/, handler(content, context)];
110
+ case 2:
111
+ _d.sent();
112
+ channel.ack(message);
113
+ return [3 /*break*/, 4];
114
+ case 3:
115
+ e_1 = _d.sent();
116
+ channel.reject(message, requeueOnFailure);
117
+ return [3 /*break*/, 4];
118
+ case 4: return [2 /*return*/];
119
+ }
120
+ });
121
+ }); })), consumeOptions)];
122
+ case 5:
123
+ _f.sent();
124
+ return [2 /*return*/];
125
+ }
126
+ });
127
+ });
128
+ }
129
+ exports.consumeQueue = consumeQueue;
130
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/consume/utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,oCAA4C;AAE5C,kCAAuC;AAEvC,mCAA8C;AAE9C,0BAA0B;AAC1B,SAAsB,YAAY,CAC9B,OAAuB,EACvB,QAAyB;;;;;;;;oBAEnB,MAAM,GAAY,IAAA,kBAAS,EAAC,OAAO,CAAC,KAAK,CAAC,CAAC;oBACnB,qBAAM,IAAA,qBAAa,EAAC,MAAM,CAAC,EAAA;;oBAAnD,KAAwB,SAA2B,EAAlD,OAAO,aAAA,EAAE,UAAU,gBAAA;oBAEpB,SAAS,GAAY,MAAA,OAAO,CAAC,IAAI,mCAAI,EAAE,CAAC;oBAEvB,qBAAM,OAAO,CAAC,WAAW,CAAC,SAAS,EAAE;4BACxD,OAAO,EAAE,KAAK;4BACd,UAAU,EAAE,IAAI;yBACnB,CAAC,EAAA;;oBAHI,cAAc,GAAG,SAGrB;yBAEC,CAAA,OAAO,OAAO,CAAC,UAAU,KAAK,WAAW,CAAA,EAAzC,wBAAyC;oBAClC,WAAW,GAAa,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;oBAEtG,QAAQ,GAA6B,WAAW,CAAC,GAAG,CAAC,UAAA,OAAO;wBAC9D,OAAO,OAAO,CAAC,SAAS,CAAC,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAsC,CAAC;oBACvH,CAAC,CAAC,CAAC;oBAEH,qBAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAA;;oBAA3B,SAA2B,CAAC;;;oBAG1B,cAAc,yBACb,CAAC,MAAA,MAAA,MAAM,CAAC,OAAO,0CAAE,OAAO,mCAAI,EAAE,CAAC,GAC/B,CAAC,MAAA,OAAO,CAAC,OAAO,mCAAI,EAAE,CAAC,CAC7B,CAAC;oBAEF,qBAAM,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,KAAK,EAAE,CAAC,CAAC,UAAO,OAAO;;;;;;wCACxD,IAAG,CAAC,OAAO,EAAE;4CACT,sBAAO;yCACV;wCAEK,OAAO,GAAa,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;wCAClE,OAAO,GAAG,MAAA,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,mCAAI,QAAQ,CAAC,6BAAoB,CAAC,CAAC;wCAEnE,OAAO,GAAoB;4CAC7B,OAAO,EAAE,OAAO;4CAChB,UAAU,EAAE,UAAU;4CACtB,aAAa,EAAE,OAAO,CAAC,MAAM;4CAC7B,iBAAiB,EAAE,OAAO,CAAC,UAAU;yCACxC,CAAA;wCAEK,gBAAgB,GAAa,MAAA,MAAA,MAAM,CAAC,OAAO,0CAAE,gBAAgB,mCAAI,KAAK,CAAC;wCAE7E,IAAG,OAAO,OAAO,KAAK,WAAW,EAAE;4CAC/B,sBAAO,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,gBAAgB,CAAC,EAAC;yCACpD;;;;wCAGG,qBAAM,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,EAAA;;wCAA/B,SAA+B,CAAC;wCAChC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;;;;wCAErB,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC;;;;;6BAEjD,CAAC,CAAC,EAAE,cAAc,CAAC,EAAA;;oBA3BpB,SA2BoB,CAAC;;;;;CACxB;AAzDD,oCAyDC"}
package/dist/index.d.ts CHANGED
@@ -1 +1,5 @@
1
+ export * from './config';
2
+ export * from './consume';
1
3
  export * from './connection';
4
+ export * from './message';
5
+ export * from './publish';
package/dist/index.js CHANGED
@@ -10,5 +10,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./config"), exports);
14
+ __exportStar(require("./consume"), exports);
13
15
  __exportStar(require("./connection"), exports);
16
+ __exportStar(require("./message"), exports);
17
+ __exportStar(require("./publish"), exports);
14
18
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,+CAA6B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAyB;AACzB,4CAA0B;AAC1B,+CAA6B;AAC7B,4CAA0B;AAC1B,4CAA0B"}
@@ -0,0 +1,2 @@
1
+ import { BuildMessageContext, Message } from "./type";
2
+ export declare function buildMessage(context: BuildMessageContext): Message;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildMessage = void 0;
4
+ var uuid_1 = require("uuid");
5
+ function buildMessage(context) {
6
+ var _a, _b, _c, _d;
7
+ return {
8
+ id: (_a = context.id) !== null && _a !== void 0 ? _a : (0, uuid_1.v4)(),
9
+ type: context.type,
10
+ options: (_b = context.options) !== null && _b !== void 0 ? _b : {},
11
+ data: (_c = context.data) !== null && _c !== void 0 ? _c : {},
12
+ metadata: (_d = context.metadata) !== null && _d !== void 0 ? _d : {}
13
+ };
14
+ }
15
+ exports.buildMessage = buildMessage;
16
+ //# sourceMappingURL=build.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"build.js","sourceRoot":"","sources":["../../src/message/build.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAGxB,SAAgB,YAAY,CACxB,OAA4B;;IAE5B,OAAO;QACH,EAAE,EAAE,MAAA,OAAO,CAAC,EAAE,mCAAI,IAAA,SAAE,GAAE;QACtB,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,OAAO,EAAE,MAAA,OAAO,CAAC,OAAO,mCAAI,EAAE;QAC9B,IAAI,EAAE,MAAA,OAAO,CAAC,IAAI,mCAAI,EAAE;QACxB,QAAQ,EAAE,MAAA,OAAO,CAAC,QAAQ,mCAAI,EAAE;KACnC,CAAC;AACN,CAAC;AAVD,oCAUC"}
@@ -0,0 +1,2 @@
1
+ export * from './build';
2
+ export * from './type';
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./build"), exports);
14
+ __exportStar(require("./type"), exports);
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/message/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,0CAAwB;AACxB,yCAAuB"}
@@ -0,0 +1,46 @@
1
+ import { Channel, Connection, MessageFields, MessageProperties, Options } from "amqplib";
2
+ export interface MessageOptions {
3
+ /**
4
+ * Routing key for message broker.
5
+ */
6
+ routingKey?: string;
7
+ /**
8
+ * Override default publish options.
9
+ */
10
+ publish?: Options.Publish;
11
+ }
12
+ export declare type Message = {
13
+ /**
14
+ * Routing information for amqp library.
15
+ * This property will be removed, before it is passed to the message queue.
16
+ */
17
+ options?: MessageOptions;
18
+ /**
19
+ *
20
+ * Default: <generated uuid>
21
+ */
22
+ id: string;
23
+ /**
24
+ * Event- or Command-name.
25
+ */
26
+ type: string;
27
+ /**
28
+ * Metadata object to provide details for the message broker.
29
+ *
30
+ * Default: {}
31
+ */
32
+ metadata: Record<string, any>;
33
+ /**
34
+ * The message data.
35
+ *
36
+ * Default: {}
37
+ */
38
+ data: Record<string, any>;
39
+ };
40
+ export declare type BuildMessageContext = Pick<Message, 'type'> & Partial<Pick<Message, 'id' | 'options' | 'data' | 'metadata'>>;
41
+ export declare type MessageContext = {
42
+ channel: Channel;
43
+ connection: Connection;
44
+ messageFields?: MessageFields;
45
+ messageProperties?: MessageProperties;
46
+ };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../src/message/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export * from './utils';
2
+ export * from './type';
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./utils"), exports);
14
+ __exportStar(require("./type"), exports);
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/publish/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,0CAAwB;AACxB,yCAAuB"}
@@ -0,0 +1,12 @@
1
+ import { Options } from "amqplib";
2
+ import { Config } from "../config";
3
+ export declare type PublishOptions = {
4
+ /**
5
+ * Config key or object.
6
+ */
7
+ alias?: string | Config;
8
+ /**
9
+ * Amqplib publish options.
10
+ */
11
+ options?: Options.Publish;
12
+ };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../src/publish/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,3 @@
1
+ import { Message } from "../message";
2
+ import { PublishOptions } from "./type";
3
+ export declare function publishMessage(message: Message, options?: PublishOptions): Promise<void>;
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __rest = (this && this.__rest) || function (s, e) {
50
+ var t = {};
51
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
52
+ t[p] = s[p];
53
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
54
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
55
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
56
+ t[p[i]] = s[p[i]];
57
+ }
58
+ return t;
59
+ };
60
+ Object.defineProperty(exports, "__esModule", { value: true });
61
+ exports.publishMessage = void 0;
62
+ var config_1 = require("../config");
63
+ var utils_1 = require("../utils");
64
+ /* istanbul ignore next */
65
+ function publishMessage(message, options) {
66
+ var _a, _b, _c, _d;
67
+ return __awaiter(this, void 0, void 0, function () {
68
+ var messageOptions, messagePayload, buffer, config, channel, publishOptions;
69
+ return __generator(this, function (_e) {
70
+ switch (_e.label) {
71
+ case 0:
72
+ messageOptions = message.options, messagePayload = __rest(message, ["options"]);
73
+ buffer = Buffer.from(JSON.stringify(messagePayload));
74
+ options !== null && options !== void 0 ? options : (options = {});
75
+ config = (0, config_1.getConfig)(options.alias);
76
+ return [4 /*yield*/, (0, utils_1.createChannel)(config)];
77
+ case 1:
78
+ channel = (_e.sent()).channel;
79
+ publishOptions = __assign(__assign(__assign({}, ((_b = (_a = config.publish) === null || _a === void 0 ? void 0 : _a.options) !== null && _b !== void 0 ? _b : {})), ((_c = messageOptions.publish) !== null && _c !== void 0 ? _c : {})), ((_d = options.options) !== null && _d !== void 0 ? _d : {}));
80
+ channel.publish(config.exchange.name, messageOptions.routingKey, buffer, publishOptions);
81
+ return [2 /*return*/];
82
+ }
83
+ });
84
+ });
85
+ }
86
+ exports.publishMessage = publishMessage;
87
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/publish/utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,oCAA4C;AAE5C,kCAAuC;AAGvC,0BAA0B;AAC1B,SAAsB,cAAc,CAChC,OAAgB,EAChB,OAAwB;;;;;;;oBAEV,cAAc,GAAuB,OAAO,QAA9B,EAAK,cAAc,UAAI,OAAO,EAAtD,WAA4C,CAAD,CAAY;oBAErD,MAAM,GAAW,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,CAAC;oBAEnE,OAAO,aAAP,OAAO,cAAP,OAAO,IAAP,OAAO,GAAK,EAAE,EAAC;oBACT,MAAM,GAAW,IAAA,kBAAS,EAAC,OAAO,CAAC,KAAK,CAAC,CAAC;oBAC9B,qBAAM,IAAA,qBAAa,EAAC,MAAM,CAAC,EAAA;;oBAAtC,OAAO,GAAI,CAAA,SAA2B,CAAA,QAA/B;oBAER,cAAc,kCACb,CAAC,MAAA,MAAA,MAAM,CAAC,OAAO,0CAAE,OAAO,mCAAI,EAAE,CAAC,GAC/B,CAAC,MAAA,cAAc,CAAC,OAAO,mCAAI,EAAE,CAAC,GAC9B,CAAC,MAAA,OAAO,CAAC,OAAO,mCAAI,EAAE,CAAC,CAC7B,CAAA;oBAED,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,cAAc,CAAC,CAAC;;;;;CAC5F;AAnBD,wCAmBC"}
@@ -0,0 +1,6 @@
1
+ import { Channel, Connection } from "amqplib";
2
+ import { Config } from "./config";
3
+ export declare function createChannel(key: string | Config): Promise<{
4
+ channel: Channel;
5
+ connection: Connection;
6
+ }>;
package/dist/utils.js ADDED
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.createChannel = void 0;
51
+ var config_1 = require("./config");
52
+ var connection_1 = require("./connection");
53
+ /* istanbul ignore next */
54
+ function createChannel(key) {
55
+ var _a, _b;
56
+ return __awaiter(this, void 0, void 0, function () {
57
+ var config, connection, channel, exchangeOptions;
58
+ return __generator(this, function (_c) {
59
+ switch (_c.label) {
60
+ case 0:
61
+ config = (0, config_1.getConfig)(key);
62
+ return [4 /*yield*/, (0, connection_1.useConnection)(config.alias)];
63
+ case 1:
64
+ connection = _c.sent();
65
+ return [4 /*yield*/, connection.createChannel()];
66
+ case 2:
67
+ channel = _c.sent();
68
+ exchangeOptions = __assign({ durable: true }, ((_b = (_a = config.exchange) === null || _a === void 0 ? void 0 : _a.options) !== null && _b !== void 0 ? _b : {}));
69
+ return [4 /*yield*/, channel.assertExchange(config.exchange.name, config.exchange.type, exchangeOptions)];
70
+ case 3:
71
+ _c.sent();
72
+ return [2 /*return*/, {
73
+ channel: channel,
74
+ connection: connection
75
+ }];
76
+ }
77
+ });
78
+ });
79
+ }
80
+ exports.createChannel = createChannel;
81
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,mCAA2C;AAC3C,2CAA2C;AAE3C,0BAA0B;AAC1B,SAAsB,aAAa,CAAC,GAAoB;;;;;;;oBAI9C,MAAM,GAAY,IAAA,kBAAS,EAAC,GAAG,CAAC,CAAC;oBAEP,qBAAM,IAAA,0BAAa,EAAC,MAAM,CAAC,KAAK,CAAC,EAAA;;oBAA3D,UAAU,GAAgB,SAAiC;oBACvC,qBAAM,UAAU,CAAC,aAAa,EAAE,EAAA;;oBAApD,OAAO,GAAa,SAAgC;oBAEpD,eAAe,cACjB,OAAO,EAAE,IAAI,IACV,CAAC,MAAA,MAAA,MAAM,CAAC,QAAQ,0CAAE,OAAO,mCAAI,EAAE,CAAC,CACtC,CAAC;oBAEF,qBAAM,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAC,EAAA;;oBAAzF,SAAyF,CAAC;oBAE1F,sBAAO;4BACH,OAAO,EAAE,OAAO;4BAChB,UAAU,EAAE,UAAU;yBACzB,EAAC;;;;CACL;AApBD,sCAoBC"}
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "amqp-extension",
3
- "version": "0.0.4",
3
+ "version": "1.0.0",
4
4
  "description": "An amqp extension with functions and utility functions to consume and publish queue messages.",
5
5
  "main": "./dist/index.js",
6
+ "typings": "dist/index.d.ts",
6
7
  "scripts": {
7
8
  "build": "rm -rf ./dist && tsc",
8
9
  "test": "cross-env NODE_ENV=test jest --config ./test/jest.config.js",
@@ -39,8 +40,8 @@
39
40
  "coveralls": "^3.1.0",
40
41
  "cross-env": "^7.0.3",
41
42
  "jest": "^27.0.5",
43
+ "np": "^7.5.0",
42
44
  "ts-jest": "^27.0.3",
43
- "ts-node": "^10.0.0",
44
45
  "tslint": "^6.1.3",
45
46
  "tslint-config-prettier": "^1.18.0",
46
47
  "typescript": "^4.4.3"
@@ -1 +0,0 @@
1
- export declare type ConnectionAlias = 'default' | string;