From ebdcf801f6541776999b36a8592e36fa9e2de9fa Mon Sep 17 00:00:00 2001 From: Ernie Francis Date: Sat, 3 Apr 2021 21:12:50 -0400 Subject: [PATCH 1/3] Update index.js Quick update to fix the package crashing as mentioned in https://github.com/leadstoloyals/node-agcod/issues/3 --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index dab1860..cbb6436 100644 --- a/index.js +++ b/index.js @@ -133,7 +133,7 @@ module.exports = class { */ _getNewId() { let hrTime = process.hrtime() - let id = new BigNumber(hrTime[0]).times('1e9').plus(hrTime[1]).toString(36) + let id = new BigNumber.BigNumber(hrTime[0]).times('1e9').plus(hrTime[1]).toString(36) return id } } From 2bafeea7a056e0e8c177e64a15ac636b668291c0 Mon Sep 17 00:00:00 2001 From: Ernie Francis Date: Mon, 5 Apr 2021 10:36:22 -0400 Subject: [PATCH 2/3] Create LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..51efe74 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Leads to Loyals Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From de3a09b4e148233b4c06e1a989fc92678e66a948 Mon Sep 17 00:00:00 2001 From: Ernie Francis Date: Mon, 14 Jun 2021 09:22:08 -0400 Subject: [PATCH 3/3] Update index.js --- index.js | 154 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 99 insertions(+), 55 deletions(-) diff --git a/index.js b/index.js index cbb6436..ce5b1d4 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,9 @@ -const BigNumber = require('bignumber.js') -const request = require('request') -const aws4 = require('aws4') -const helpers = require('./lib/helpers') +const BigNumber = require('bignumber.js'); +const request = require('request'); +const aws4 = require('aws4'); +const helpers = require('./lib/helpers'); + +const axios = require('axios').default; module.exports = class { constructor(cfg = {}) { @@ -9,39 +11,59 @@ module.exports = class { } createGiftCard(region, amount, currencyCode, cb) { - this._checkRegion(region) - const sequentialId = this._getNewId() - const requestBody = this._getCreateGiftCardRequestBody(sequentialId, amount, currencyCode) - const signedRequest = this._getSignedRequest(region, 'CreateGiftCard', requestBody) - const req = this._doRequest(signedRequest, cb) + try { + this._checkRegion(region); + const sequentialId = this._getNewId(); + const requestBody = this._getCreateGiftCardRequestBody(sequentialId, amount, currencyCode); + const signedRequest = this._getSignedRequest(region, 'CreateGiftCard', requestBody); + const req = this._doRequest(signedRequest, cb); + + return { req, sequentialId, requestBody, signedRequest }; + } catch (error) { + console.log('Error: ', error); + throw new Error(error); + } + } - return {req, sequentialId, requestBody, signedRequest} + async createGiftCardAsync(region, amount, currencyCode) { + try { + this._checkRegion(region); + const sequentialId = this._getNewId(); + const requestBody = this._getCreateGiftCardRequestBody(sequentialId, amount, currencyCode); + const signedRequest = this._getSignedRequest(region, 'CreateGiftCard', requestBody); + const res = await this._doRequestAsync(signedRequest); + + return res; + } catch (error) { + console.log('Error: ', error); + throw new Error(error); + } } createGiftCardAgain(region, amount, currencyCode, sequentialId, cb) { - this._checkRegion(region) - const requestBody = this._getCreateGiftCardRequestBody(sequentialId, amount, currencyCode) - const signedRequest = this._getSignedRequest(region, 'CreateGiftCard', requestBody) - const req = this._doRequest(signedRequest, cb) + this._checkRegion(region); + const requestBody = this._getCreateGiftCardRequestBody(sequentialId, amount, currencyCode); + const signedRequest = this._getSignedRequest(region, 'CreateGiftCard', requestBody); + const req = this._doRequest(signedRequest, cb); - return {req, sequentialId, requestBody, signedRequest} + return { req, sequentialId, requestBody, signedRequest }; } cancelGiftCard(region, sequentialId, gcId, cb) { - this._checkRegion(region) - const requestBody = this._getCancelGiftCardRequestBody(sequentialId, gcId) - const signedRequest = this._getSignedRequest(region, 'CancelGiftCard', requestBody) - const req = this._doRequest(signedRequest, cb) + this._checkRegion(region); + const requestBody = this._getCancelGiftCardRequestBody(sequentialId, gcId); + const signedRequest = this._getSignedRequest(region, 'CancelGiftCard', requestBody); + const req = this._doRequest(signedRequest, cb); - return {req, requestBody, signedRequest} + return { req, requestBody, signedRequest }; } /** * Throws when region is not NA, EU or FE */ _checkRegion(region) { - if (['NA', 'EU', 'FE'].indexOf(region) === -1 ) { - throw new Error(`First argument must be string NA, EU or FE`) + if (['NA', 'EU', 'FE'].indexOf(region) === -1) { + throw new Error(`First argument must be string NA, EU or FE`); } } @@ -50,10 +72,7 @@ module.exports = class { * @returns {Object} */ _getCreateGiftCardRequestBody(sequentialId, amount, currencyCode) { - return helpers.CreateGiftCardRequest( - this.config.partnerId, - sequentialId, amount, currencyCode - ) + return helpers.CreateGiftCardRequest(this.config.partnerId, sequentialId, amount, currencyCode); } /** @@ -61,10 +80,7 @@ module.exports = class { * @returns {Object} */ _getCancelGiftCardRequestBody(sequentialId, gcId) { - return helpers.CancelGiftCardRequest( - this.config.partnerId, - sequentialId, gcId - ) + return helpers.CancelGiftCardRequest(this.config.partnerId, sequentialId, gcId); } /** @@ -77,7 +93,7 @@ module.exports = class { */ _getSignedRequest(region, action, requestBody) { const credentials = this.config.credentials; - const endpoint = this.config.endpoint[region] + const endpoint = this.config.endpoint[region]; const opts = { region: endpoint.region, host: endpoint.host, @@ -85,16 +101,19 @@ module.exports = class { body: JSON.stringify(requestBody), // defaults service: 'AGCODService', - headers: Object.assign({ - 'accept': `application/json`, - 'content-type': `application/json`, - 'x-amz-target': `com.amazonaws.agcod.AGCODService.${action}` - }, this.config.extraHeaders), + headers: Object.assign( + { + accept: `application/json`, + 'content-type': `application/json`, + 'x-amz-target': `com.amazonaws.agcod.AGCODService.${action}`, + }, + this.config.extraHeaders + ), method: 'POST', - securityOptions: 'SSL_OP_NO_SSLv3' - } + securityOptions: 'SSL_OP_NO_SSLv3', + }; - return aws4.sign(opts, credentials) + return aws4.sign(opts, credentials); } /** @@ -108,32 +127,57 @@ module.exports = class { method: 'POST', url: `https://${signedRequest.host}${signedRequest.path}`, headers: signedRequest.headers, - body: signedRequest.body - } + body: signedRequest.body, + }; return request(params, (error, response, result) => { - if (error) return cb(error) + if (error) return cb(error); if (response.statusCode !== 200) { - const err = Object.assign({ - request: params, - statusCode: response.statusCode - }, JSON.parse(result)) - - return cb(err) + const err = Object.assign( + { + request: params, + statusCode: response.statusCode, + }, + JSON.parse(result) + ); + + return cb(err); } - return cb(null, JSON.parse(result)) - }) + return cb(null, JSON.parse(result)); + }); + } + + async _doRequestAsync(signedRequest) { + const params = { + method: 'POST', + url: `https://${signedRequest.host}${signedRequest.path}`, + headers: signedRequest.headers, + body: signedRequest.body, + }; + + const res = await axios.post(`https://${signedRequest.host}${signedRequest.path}`, signedRequest.body, { + headers: signedRequest.headers, + }); + if (res.status !== 200) { + const err = { + request: params, + statusCode: res.status, + }; + + return err; + } + return res.data; } /** * Generates a unique sequential base-36 string based on processor time * @returns string with length of 10 */ - _getNewId() { - let hrTime = process.hrtime() - let id = new BigNumber.BigNumber(hrTime[0]).times('1e9').plus(hrTime[1]).toString(36) - return id + _getNewId() { + let hrTime = process.hrtime(); + let id = new BigNumber.BigNumber(hrTime[0]).times('1e9').plus(hrTime[1]).toString(36); + return id; } -} +};