From 028187176da0f1ad3560024d8a28f6cdbea5a350 Mon Sep 17 00:00:00 2001 From: Kalarrs Topham Date: Wed, 25 Jul 2018 09:23:47 -0700 Subject: [PATCH] Updated aws provider to invoke .promise on methods that support it. Otherwise falls back to .send with a callback. --- lib/plugins/aws/provider/awsProvider.js | 6 ++-- lib/plugins/aws/provider/awsProvider.test.js | 33 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index 50f7a33c0..f2236a7b9 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -247,10 +247,10 @@ class AwsProvider { // TODO: Add listeners, put Debug statments here... // req.on('send', function (r) {console.log(r)}); - return BbPromise.fromCallback(cb => { + const promise = req.promise ? req.promise() : BbPromise.fromCallback(cb => { req.send(cb); - }) - .catch(err => { + }); + return promise.catch(err => { let message = err.message; if (err.message === 'Missing credentials in config') { const errorMessage = [ diff --git a/lib/plugins/aws/provider/awsProvider.test.js b/lib/plugins/aws/provider/awsProvider.test.js index b518cc28d..0f355eadf 100644 --- a/lib/plugins/aws/provider/awsProvider.test.js +++ b/lib/plugins/aws/provider/awsProvider.test.js @@ -231,6 +231,39 @@ describe('AwsProvider', () => { }); }); + it('should call correct aws method with a promise', () => { + // mocking S3 for testing + class FakeAPIGateway { + constructor(credentials) { + this.credentials = credentials; + } + + getRestApis() { + return { + promise: () => BbPromise.resolve({ called: true }), + }; + } + } + awsProvider.sdk = { + APIGateway: FakeAPIGateway, + }; + awsProvider.serverless.service.environment = { + vars: {}, + stages: { + dev: { + vars: { + profile: 'default', + }, + regions: {}, + }, + }, + }; + + return awsProvider.request('APIGateway', 'getRestApis', {}).then(data => { + expect(data.called).to.equal(true); + }); + }); + it('should retry if error code is 429', (done) => { const error = { statusCode: 429,