diff --git a/README.md b/README.md index 2763e74..7bd0307 100644 --- a/README.md +++ b/README.md @@ -245,10 +245,16 @@ var batch = { }] } -client.updateBatchSubscribers(batch, function (errors, responses, bodies) { - // Do stuff - } -) +client.updateBatchSubscribers(batch, (errors, responses, bodies) => { + // Do stuff... + if (errors) { + console.error('Some requests failed:', errors); + } else { + console.log('All requests succeeded'); + } + console.log('Responses:', responses); + console.log('Bodies:', bodies); + }); ``` ### Sending a batch of events diff --git a/lib/subscribers.js b/lib/subscribers.js index 2d842bd..1029a3c 100644 --- a/lib/subscribers.js +++ b/lib/subscribers.js @@ -84,26 +84,26 @@ module.exports = { * @param {callback} callback - Required. A callback */ - // TODO: Return an optional Promise - updateBatchSubscribers(payload, callback) { - const subscribers = payload && payload.batches && payload.batches[0].subscribers || []; + updateBatchSubscribers: async function (payload, callback) { + const subscribers = (payload && payload.batches && payload.batches[0].subscribers) || []; const batchSize = 1000; const batches = []; const errors = []; const responses = []; const bodies = []; const headers = this.requestHeaders(); - let done = 0; let hasError = false; // Break the payload into batch-sized chunks - for (let i = 0, j = subscribers.length; i < j; i += batchSize) { - batches.push(subscribers.slice(i, batchSize)); + for (let i = 0; i < subscribers.length; i += batchSize) { + batches.push(subscribers.slice(i, i + batchSize)); } - batches.forEach((batch, batchIndex) => { - request.post( - { + // Map each batch to an axios request and store the results + await Promise.all( + batches.map((batch, batchIndex) => + request({ + method: 'post', url: `${helpers.baseUrl}v2/${this.accountId}/subscribers/batches`, headers, responseType: 'json', @@ -112,24 +112,21 @@ module.exports = { subscribers: batch }] } - }, - (error, response, body) => { - errors[batchIndex] = error; + }) + .then((response) => { responses[batchIndex] = response; - bodies[batchIndex] = body; - hasError = hasError || error; - done += 1; + bodies[batchIndex] = response.data; + errors[batchIndex] = null; + }) + .catch((error) => { + errors[batchIndex] = error; + responses[batchIndex] = null; + bodies[batchIndex] = null; + hasError = true; + }) + ) + ); - if (done === batches.length) { - // All batches complete; call back - callback( - hasError ? errors : null, - responses, - bodies - ); - } - } - ); - }); + callback(hasError ? errors : null, responses, bodies); } }; diff --git a/spec/lib/subscribers_spec.js b/spec/lib/subscribers_spec.js index 55a9938..f6dc3a9 100644 --- a/spec/lib/subscribers_spec.js +++ b/spec/lib/subscribers_spec.js @@ -1,4 +1,4 @@ -const request = require('axios'); +const axios = require('axios'); const sinon = require('sinon'); const client = require('../../lib/index')({ token: 'abc123', accountId: 9999999 }); @@ -96,12 +96,11 @@ describe('Subscribers with callback', () => { }; beforeEach(() => { - sinon.stub(request, 'post') - .yields(null, { statusCode: 201 }, {}); + sinon.stub(axios, 'request').resolves({ status: 201, data: {} }); }); afterEach(() => { - request.post.restore(); + axios.request.restore(); }); it('should post batches of subscribers and call request with post', (done) => { @@ -113,7 +112,7 @@ describe('Subscribers with callback', () => { expect(responses[0].statusCode).toBe(201); expect(responses[1].statusCode).toBe(201); expect(bodies).toEqual([{}, {}]); - expect(request.post.callCount).toBe(2); + expect(axios.request.callCount).toBe(2); }); done(); }); @@ -127,13 +126,11 @@ describe('Subscribers with callback', () => { }; beforeEach(() => { - sinon.stub(request, 'post') - .yields(null, { statusCode: 201 }, {}); - spyOn(request, 'post').and.callThrough(); + sinon.stub(axios, 'request').resolves({ status: 201, data: {} }); }); afterEach(() => { - request.post.restore(); + axios.request.restore(); }); it('should set the correct request URL', (done) => { @@ -143,9 +140,9 @@ describe('Subscribers with callback', () => { expect(responses[0].statusCode).toBe(201); expect(bodies).toEqual([{}]); }); - done(); - - expect(request.post).toHaveBeenCalledWith({ + + expect(axios.request.calledWith({ + method: 'post', url: 'https://api.getdrip.com/v2/9999999/subscribers/batches', headers: client.requestHeaders(), responseType: 'json', @@ -154,7 +151,10 @@ describe('Subscribers with callback', () => { subscribers: [undefined] }] } - }, jasmine.any(Function)); + })); + + done(); + }); });