Skip to content

Commit 37f7641

Browse files
authored
fix: Incorrect type for SQS messageGroupId (#1205)
The current type is `null | undefined`, which is unusable! I've changed this to `string | null | undefined` as a non-breaking step torwards `string | undefined`, with plans to remove the `null` type in v3. Also improves associated JSDoc.
1 parent 42281b1 commit 37f7641

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

docs/migration/v2.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,11 @@ In v1, the default behaviour of `publish` was to catch any error thrown while se
172172
// v1
173173
sqs.publish(queue, message);
174174
// v2 equivalent
175-
sqs.publish(queue, message, null, 'catch');
175+
sqs.publish(queue, message, undefined, 'catch');
176176
```
177177

178+
We encourage use of `undefined` instead of `null` if you are not using the `messageGroupId` parameter. This keeps the type simple. `null` will continue to be accepted but is deprecated as of v2, and it will be dropped from the type in v3.
179+
178180
## Models
179181

180182
The `Model` base class has been removed. It's hard to make it type-safe (it tries to dynamically call setter methods) and we do modelling and validation differently now, using our [data-models](https://github.com/comicrelief/data-models) repo which is based around [Yup](https://github.com/jquense/yup).

src/services/SQSService.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,15 +430,22 @@ export default class SQSService<
430430
* local Lambda or SQS service instead of to AWS, depending on the offline
431431
* mode specified by `process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE`.
432432
*
433-
* @param queue string
434-
* @param messageObject object
435-
* @param messageGroupId string
433+
* @param queue Which queue to send to. This should be one of the queue names
434+
* configured in your Lambda Wrapper config.
435+
* @param messageObject Message body to put in the queue.
436+
* @param messageGroupId For FIFO queues, the message group ID. If omitted or
437+
* undefined, a random message group ID will be used.
438+
*
439+
* In v1 we encouraged use of `null` if this field was unused. This is now
440+
* deprecated in favour of `undefined`. Types will still permit `null`,
441+
* however they will change in v3 to require `string | undefined`.
442+
*
436443
* @param failureMode Choose how failures are handled:
437444
* - `throw`: errors will be thrown, causing promise to reject. (default)
438445
* - `catch`: errors will be caught and logged. Useful for non-critical
439446
* messages.
440447
*/
441-
async publish(queue: QueueName<TConfig>, messageObject: object, messageGroupId = null, failureMode: 'catch' | 'throw' = SQS_PUBLISH_FAILURE_MODES.THROW) {
448+
async publish(queue: QueueName<TConfig>, messageObject: object, messageGroupId?: string | null, failureMode: 'catch' | 'throw' = SQS_PUBLISH_FAILURE_MODES.THROW) {
442449
if (!Object.values(SQS_PUBLISH_FAILURE_MODES).includes(failureMode)) {
443450
throw new Error(`Invalid value for 'failureMode': ${failureMode}`);
444451
}
@@ -456,7 +463,7 @@ export default class SQSService<
456463

457464
if (queueUrl.includes('.fifo')) {
458465
messageParameters.MessageDeduplicationId = uuid();
459-
messageParameters.MessageGroupId = messageGroupId !== null ? messageGroupId : uuid();
466+
messageParameters.MessageGroupId = messageGroupId ?? uuid();
460467
}
461468

462469
try {

tests/unit/services/SQSService.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ describe('unit.services.SQSService', () => {
344344
sendMessage: new Error('SQS is down!'),
345345
}, false);
346346

347-
const promise = service.publish(TEST_QUEUE, { test: 1 }, null);
347+
const promise = service.publish(TEST_QUEUE, { test: 1 }, undefined);
348348

349349
await expect(promise).rejects.toThrowError('SQS is down!');
350350
});
@@ -354,7 +354,7 @@ describe('unit.services.SQSService', () => {
354354
sendMessage: new Error('SQS is down!'),
355355
}, false);
356356

357-
const promise = service.publish(TEST_QUEUE, { test: 1 }, null, SQS_PUBLISH_FAILURE_MODES.CATCH);
357+
const promise = service.publish(TEST_QUEUE, { test: 1 }, undefined, SQS_PUBLISH_FAILURE_MODES.CATCH);
358358

359359
await expect(promise).resolves.toEqual(null);
360360
});
@@ -364,7 +364,7 @@ describe('unit.services.SQSService', () => {
364364
sendMessage: new Error('SQS is down!'),
365365
}, false);
366366

367-
const promise = service.publish(TEST_QUEUE, { test: 1 }, null, SQS_PUBLISH_FAILURE_MODES.THROW);
367+
const promise = service.publish(TEST_QUEUE, { test: 1 }, undefined, SQS_PUBLISH_FAILURE_MODES.THROW);
368368

369369
await expect(promise).rejects.toThrowError('SQS is down!');
370370
});
@@ -377,7 +377,7 @@ describe('unit.services.SQSService', () => {
377377
it(`throws an error with the invalid value: ${invalidValue}`, async () => {
378378
const service = getService();
379379

380-
const promise = service.publish(TEST_QUEUE, { test: 1 }, null, invalidValue);
380+
const promise = service.publish(TEST_QUEUE, { test: 1 }, undefined, invalidValue);
381381

382382
await expect(promise).rejects.toThrowErrorMatchingSnapshot();
383383
});

0 commit comments

Comments
 (0)