core-services-sdk 1.3.1 → 1.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "core-services-sdk",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "main": "src/index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -47,25 +47,30 @@ const parseMessage = (msgInfo) => {
47
47
 
48
48
  /**
49
49
  * Subscribes to a queue to receive messages.
50
- * @param {{
51
- * channel: amqp.Channel,
52
- * queue: string,
53
- * onReceive: (data: any) => Promise<void>,
54
- * log: Log,
55
- * nackOnError?: boolean
56
- * }} options
50
+ *
51
+ * @param {Object} options
52
+ * @param {import('amqplib').Channel} options.channel - RabbitMQ channel
53
+ * @param {string} options.queue - Queue name to subscribe to
54
+ * @param {(data: any) => Promise<void>} options.onReceive - Async handler for incoming message
55
+ * @param {Log} options.log - Logging utility
56
+ * @param {boolean} [options.nackOnError=false] - Whether to nack the message on error (default: false)
57
+ * @param {number} [options.prefetch=1] - Max unacked messages per consumer (default: 1)
58
+ *
57
59
  * @returns {Promise<void>}
58
60
  */
59
61
  export const subscribeToQueue = async ({
60
62
  log,
61
63
  queue,
62
64
  channel,
65
+ prefetch = 1,
63
66
  onReceive,
64
67
  nackOnError = false,
65
68
  }) => {
66
69
  try {
67
70
  await channel.assertQueue(queue, { durable: true })
68
71
 
72
+ !!prefetch && (await channel.prefetch(prefetch))
73
+
69
74
  channel.consume(queue, async (msgInfo) => {
70
75
  if (!msgInfo) return
71
76
 
@@ -4,29 +4,36 @@ import { describe, it, expect, vi } from 'vitest'
4
4
  import sgTransport from 'nodemailer-sendgrid-transport'
5
5
 
6
6
  vi.mock('nodemailer', () => {
7
+ const createTransport = vi.fn((options) => ({
8
+ type: 'mock-transport',
9
+ options,
10
+ }))
7
11
  return {
12
+ __esModule: true,
13
+ default: { createTransport },
14
+ createTransport,
15
+ }
16
+ })
17
+
18
+ vi.mock('aws-sdk', () => {
19
+ return {
20
+ __esModule: true,
8
21
  default: {
9
- createTransport: vi.fn(() => ({ type: 'mock-transport' })),
22
+ SES: vi.fn(() => 'mocked-ses'),
10
23
  },
11
24
  }
12
25
  })
13
26
 
14
- vi.mock('aws-sdk', async () => {
15
- const actual = await vi.importActual('aws-sdk')
27
+ vi.mock('nodemailer-sendgrid-transport', () => {
16
28
  return {
17
- ...actual,
18
- SES: vi.fn().mockImplementation(() => 'mocked-ses'),
29
+ __esModule: true,
30
+ default: vi.fn((opts) => ({
31
+ sendgrid: true,
32
+ options: opts,
33
+ })),
19
34
  }
20
35
  })
21
36
 
22
- vi.mock('nodemailer-sendgrid-transport', () => ({
23
- __esModule: true,
24
- default: vi.fn().mockImplementation((opts) => {
25
- const transport = { sendgrid: true, options: opts }
26
- return transport
27
- }),
28
- }))
29
-
30
37
  import { TransportFactory } from '../src/mailer/transport.factory.js'
31
38
  describe('TransportFactory', () => {
32
39
  it('should create smtp transport', () => {
@@ -69,8 +76,27 @@ describe('TransportFactory', () => {
69
76
  }
70
77
 
71
78
  const transport = TransportFactory.create(config)
72
- expect(transport.sendgrid).toBe(true)
73
- expect(transport.options.auth.api_key).toBe(config.apiKey)
79
+
80
+ expect(nodemailer.createTransport).toHaveBeenCalled()
81
+
82
+ const args = nodemailer.createTransport.mock.calls[0][0]
83
+
84
+ expect(args).toEqual({
85
+ sendgrid: true,
86
+ options: {
87
+ auth: { api_key: config.apiKey },
88
+ },
89
+ })
90
+
91
+ expect(transport).toEqual({
92
+ type: 'mock-transport',
93
+ options: {
94
+ sendgrid: true,
95
+ options: {
96
+ auth: { api_key: config.apiKey },
97
+ },
98
+ },
99
+ })
74
100
  })
75
101
 
76
102
  it('should create ses transport', () => {
@@ -81,12 +107,17 @@ describe('TransportFactory', () => {
81
107
  region: 'us-west-2',
82
108
  }
83
109
 
84
- const transport = TransportFactory.create(config)
110
+ TransportFactory.create(config)
111
+
85
112
  expect(nodemailer.createTransport).toHaveBeenCalled()
113
+ const args = nodemailer.createTransport.mock.calls[0][0]
86
114
 
87
- expect(nodemailer.createTransport.mock.calls[0][0]).toHaveProperty(
88
- 'SES.ses',
89
- )
115
+ expect(args).toEqual({
116
+ SES: {
117
+ ses: 'mocked-ses',
118
+ aws: expect.anything(),
119
+ },
120
+ })
90
121
  })
91
122
 
92
123
  it('should throw error for unsupported type', () => {