diff --git a/README.md b/README.md index bb098a8..2e1481c 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,22 @@ module.exports.handler = function(event, context, callback) { }; ``` +### Create instance using options object or arguments + +```js +const Q = new Lawos('https://sqs.eu-west-1.amazonaws.com …', SQS, Lambda, 10) + +// ... or ... + +const Q = new Lawos({ + queueUrl: 'https://sqs.eu-west-1.amazonaws.com …', + sqs: SQS, + lambda: Lambda, + messagesPerIteration: 10 +}) + +``` + ## License Feel free to use the code, it's released using the [MIT license](https://github.com/sbstjn/lawos/blob/master/LICENSE.md). diff --git a/package.json b/package.json index ff79658..991a18b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lawos", - "version": "1.1.5", + "version": "1.2.5", "description": "Wrapper for Amazon SQS Worker with AWS Lambda", "main": "src/main.js", "homepage": "https://github.com/sbstjn/lawos", diff --git a/src/main.js b/src/main.js index 46f004a..40ce2ee 100644 --- a/src/main.js +++ b/src/main.js @@ -1,9 +1,23 @@ 'use strict' class Lawos { - constructor (queueUrl, sqs, lambda) { + constructor (queueUrlOrOptions, sqs, lambda, messagesPerIteration) { + if (!queueUrlOrOptions) { + throw new Error('No SQS Queue URL or options object') + } + this.maxMessages = 10 - this.queueUrl = queueUrl + + if (typeof queueUrlOrOptions === 'object') { + this.messagesPerIteration(queueUrlOrOptions.messagesPerIteration) + this.queueUrl = queueUrlOrOptions.queueUrl + this.sqs = queueUrlOrOptions.sqs + this.lambda = queueUrlOrOptions.lambda + } else { + this.messagesPerIteration(messagesPerIteration) + this.queueUrl = queueUrlOrOptions + } + this.aws = { sqs: sqs, lambda: lambda @@ -24,6 +38,13 @@ class Lawos { } } + messagesPerIteration (numberOfMessages) { + if (numberOfMessages >= 1 && numberOfMessages <= 10) { + this.maxMessages = numberOfMessages + } + return this + } + invokeLambda (arn, data) { return new Promise(resolve => { this.aws.lambda.invoke( diff --git a/test/main.spec.js b/test/main.spec.js index 1730443..cf59f8f 100644 --- a/test/main.spec.js +++ b/test/main.spec.js @@ -8,8 +8,19 @@ it('is initialized with queue URL', () => { expect(Q.queueUrl).toBe('http://example.com') }) +it('is initialized with object', () => { + const Q = new Lawos({ + queueUrl: 'http://example.com' + }) + expect(Q.queueUrl).toBe('http://example.com') +}) + +it('fails without options', () => { + expect(() => new Lawos()).toThrow(/options/) +}) + it('fails without queue URL', () => { - expect(() => new Lawos()).toThrow('') + expect(() => new Lawos({ queueUrl: undefined })).toThrow(/missing/i) }) it('can has default handler', () => { @@ -21,6 +32,13 @@ it('can has default handler', () => { ]) }) +it('can set messages per iteration', () => { + const Q = new Lawos('http://example.com') + expect(Q.messagesPerIteration(0).maxMessages).toEqual(10) + expect(Q.messagesPerIteration(11).maxMessages).toEqual(10) + expect(Q.messagesPerIteration(5).maxMessages).toEqual(5) +}) + it('can set item handler', () => { const Q = new Lawos('http://example.com')