From f729e9a9c07bc7dcc4bfaef69b25d5ae4e3ca4cc Mon Sep 17 00:00:00 2001 From: BL Date: Tue, 26 Feb 2019 13:02:30 +0530 Subject: [PATCH 1/7] correct example for GetMyFeesEstimate API call --- examples/javaScript/products/getMyFeesEstimate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/javaScript/products/getMyFeesEstimate.js b/examples/javaScript/products/getMyFeesEstimate.js index 1d05c8a..27f9ea1 100644 --- a/examples/javaScript/products/getMyFeesEstimate.js +++ b/examples/javaScript/products/getMyFeesEstimate.js @@ -6,7 +6,7 @@ var accessSecret = process.env.AWS_SECRET_ACCESS_KEY || 'YOUR_SECRET'; var amazonMws = require('../../../lib/amazon-mws')(accessKey, accessSecret); var productRequest = function () { - amazonMws.products.searchFor({ + amazonMws.products.search({ 'Version': '2011-10-01', 'Action': 'GetMyFeesEstimate', 'SellerId': 'SELLER_ID', From f38f7c4cf46ea70663d300ecb4c0a6c8b08f8cde Mon Sep 17 00:00:00 2001 From: BL Date: Fri, 29 Mar 2019 13:06:09 +0530 Subject: [PATCH 2/7] Add Subscription in Typescript definition --- CHANGELOG.md | 4 ++++ index.d.ts | 4 ++++ package.json | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4d7304..27c9db8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +0.0.24 + - Add Subscription in Typescript definition + - Correct Documentation + 0.0.23 - Bug Fix for Feed Result charset diff --git a/index.d.ts b/index.d.ts index 703769a..4580361 100644 --- a/index.d.ts +++ b/index.d.ts @@ -64,6 +64,8 @@ declare class Sellers extends BaseAmazonMWS { declare class Subscriptions extends BaseAmazonMWS { + create(params: any): Promise; + searchFor(params: any): Promise; remove(params: any): Promise; @@ -100,6 +102,8 @@ declare class AmazonMWS { sellers: Sellers; + subscriptions: Subscriptions; + } diff --git a/package.json b/package.json index 42e1aef..8f1c7ba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "amazon-mws", - "version": "0.0.23", + "version": "0.0.24", "description": "Amazon MWS API wrapper", "keywords": [ "Amazon MWS", From 0c22b3063db801ee3bc1053afe2ead195164bc35 Mon Sep 17 00:00:00 2001 From: BL Date: Wed, 17 Apr 2019 15:59:14 +0530 Subject: [PATCH 3/7] correct README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 11a5f8b..3cb0286 100644 --- a/README.md +++ b/README.md @@ -559,7 +559,7 @@ Originally by [Bhushankumar L](mailto:bhushankumar.lilapara@gmail.com). #### Get My Fees Estimate ``` - amazonMws.products.searchFor({ + amazonMws.products.search({ 'Version': '2011-10-01', 'Action': 'GetMyFeesEstimate', 'SellerId': 'SELLER_ID', From 38b79de424a993b3026506d55ecc479dfef337d7 Mon Sep 17 00:00:00 2001 From: BL Date: Wed, 17 Apr 2019 16:13:45 +0530 Subject: [PATCH 4/7] allow user to receive report into the XML format --- CHANGELOG.md | 1 + README.md | 21 +++++++++++++++- .../sample/requestReportXMLResponse.js | 25 +++++++++++++++++++ lib/AmazonMwsResource.js | 16 ++++++++++-- 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 examples/javaScript/sample/requestReportXMLResponse.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 27c9db8..3cc5127 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ 0.0.24 - Add Subscription in Typescript definition - Correct Documentation + - Allow response type into XML format 0.0.23 - Bug Fix for Feed Result charset diff --git a/README.md b/README.md index 3cb0286..fc86178 100644 --- a/README.md +++ b/README.md @@ -722,7 +722,8 @@ Originally by [Bhushankumar L](mailto:bhushankumar.lilapara@gmail.com). 'Action': 'RequestReport', 'SellerId': 'SELLER_ID', 'MWSAuthToken': 'MWS_AUTH_TOKEN', - 'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_' + 'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_', + '__RESPONSE_TYPE__': 'XML' }, function (error, response) { if (error) { console.log('error ', error); @@ -790,6 +791,24 @@ Originally by [Bhushankumar L](mailto:bhushankumar.lilapara@gmail.com). }); ``` +#### Request Report XML Response +``` + amazonMws.reports.submit({ + 'Version': '2009-01-01', + 'Action': 'RequestReport', + 'SellerId': 'SELLER_ID', + 'MWSAuthToken': 'MWS_AUTH_TOKEN', + 'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_', + '__RESPONSE_TYPE__': 'XML' + }, function (error, response) { + if (error) { + console.log('error ', error); + return; + } + console.log('response', response); + }); +``` + ### Sellers #### List Marketplace Participations ``` diff --git a/examples/javaScript/sample/requestReportXMLResponse.js b/examples/javaScript/sample/requestReportXMLResponse.js new file mode 100644 index 0000000..2772904 --- /dev/null +++ b/examples/javaScript/sample/requestReportXMLResponse.js @@ -0,0 +1,25 @@ +'use strict'; + +var accessKey = process.env.AWS_ACCESS_KEY_ID || 'YOUR_KEY'; +var accessSecret = process.env.AWS_SECRET_ACCESS_KEY || 'YOUR_SECRET'; + +var amazonMws = require('../../../lib/amazon-mws')(accessKey, accessSecret); + +var reportRequest = function () { + amazonMws.reports.submit({ + 'Version': '2009-01-01', + 'Action': 'RequestReport', + 'SellerId': 'SELLER_ID', + 'MWSAuthToken': 'MWS_AUTH_TOKEN', + 'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_', + '__RESPONSE_TYPE__': 'XML' + }, function (error, response) { + if (error) { + console.log('error ', error); + return; + } + console.log('response', response); + }); +}; + +reportRequest(); \ No newline at end of file diff --git a/lib/AmazonMwsResource.js b/lib/AmazonMwsResource.js index d1e9164..1d61e91 100644 --- a/lib/AmazonMwsResource.js +++ b/lib/AmazonMwsResource.js @@ -292,6 +292,10 @@ AmazonMwsResource.prototype = { debug('responseString ', responseString); debug('contentType ', contentType); debug('statusCode ', statusCode); + if (userOptions.responseType === 'XML') { + debug('Inside user response as XML option'); + return callback.call(self, null, responseString); + } try { var errorResponse = {}; @@ -390,7 +394,10 @@ AmazonMwsResource.prototype = { var userRaw = ''; var userCharset = ''; - /**custom option passed by user, a better way to do this would be nice */ + var responseType = ''; + /** + * Custom option passed by user, a better way to do this would be nice + */ if (data.__RAW__) { userRaw = data.__RAW__; delete data.__RAW__; @@ -399,6 +406,10 @@ AmazonMwsResource.prototype = { userCharset = data.__CHARSET__; delete data.__CHARSET__; } + if (data.__RESPONSE_TYPE__) { + responseType = data.__RESPONSE_TYPE__; + delete data.__RESPONSE_TYPE__; + } self.requestParams = data; if (!self.requestParams.Version) { @@ -506,7 +517,8 @@ AmazonMwsResource.prototype = { req.setTimeout(timeout, self._timeoutHandler(timeout, req, callback)); var userOptions = { userCharset: userCharset, - userRaw: userRaw + userRaw: userRaw, + responseType: responseType }; req.on('response', self._responseHandler(requestParamsJSONCopy, req, userOptions, callback)); req.on('error', self._errorHandler(req, callback)); From f96d8143293fafe73060c1c8dcc8bdf2e1ff6ade Mon Sep 17 00:00:00 2001 From: BL Date: Wed, 17 Apr 2019 16:23:50 +0530 Subject: [PATCH 5/7] correct test cases --- test/specs/products.spec.js | 42 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/test/specs/products.spec.js b/test/specs/products.spec.js index 459fcc6..df8d512 100644 --- a/test/specs/products.spec.js +++ b/test/specs/products.spec.js @@ -85,19 +85,23 @@ describe('Products', function () { expect(options.MarketplaceId).to.be.a('string'); expect(options['ASINList.ASIN.1']).to.be.a('string'); - var response = await amazonMws.products.searchFor(options); - - expect(response).to.be.a('object'); - expect(response).to.have.property('ASIN').to.be.a('string'); - expect(response).to.have.property('status').to.be.a('string'); - expect(response).to.have.property('Product').to.be.a('object'); - expect(response).to.have.property('ResponseMetadata').to.be.a('object'); - expect(response).to.have.property('ResponseMetadata').to.have.property('RequestId'); - expect(response).to.have.property('Headers').to.be.a('object'); - expect(response).to.have.property('Headers').to.have.property('x-mws-quota-max'); - expect(response).to.have.property('Headers').to.have.property('x-mws-quota-remaining'); - expect(response).to.have.property('Headers').to.have.property('x-mws-quota-resetson'); - expect(response).to.have.property('Headers').to.have.property('x-mws-timestamp'); + try { + var response = await amazonMws.products.searchFor(options); + expect(response).to.be.a('object'); + expect(response).to.have.property('ASIN').to.be.a('string'); + expect(response).to.have.property('status').to.be.a('string'); + expect(response).to.have.property('Product').to.be.a('object'); + expect(response).to.have.property('ResponseMetadata').to.be.a('object'); + expect(response).to.have.property('ResponseMetadata').to.have.property('RequestId'); + expect(response).to.have.property('Headers').to.be.a('object'); + expect(response).to.have.property('Headers').to.have.property('x-mws-quota-max'); + expect(response).to.have.property('Headers').to.have.property('x-mws-quota-remaining'); + expect(response).to.have.property('Headers').to.have.property('x-mws-quota-resetson'); + expect(response).to.have.property('Headers').to.have.property('x-mws-timestamp'); + } catch (error) { + console.log('error ', error); + expect(error).to.be.undefined; + } }); it('It should NOT get my price for INVALID ASIN using GetMyPriceForASIN Action', async function () { @@ -131,7 +135,7 @@ describe('Products', function () { expect(error).to.be.a('object'); expect(error).to.have.property('Type').to.be.a('string'); expect(error).to.have.property('Message').to.be.a('string'); - expect(error).to.have.property('Detail').to.be.a('object'); + // expect(error).to.have.property('Detail').to.be.a('object'); expect(error).to.have.property('StatusCode').to.be.a('number'); expect(error).to.have.property('RequestId').to.be.a('string'); expect(error).to.have.property('Headers').to.be.a('object'); @@ -165,10 +169,12 @@ describe('Products', function () { expect(response).to.have.property('status').to.be.a('string'); expect(response).to.have.property('Product').to.be.a('object'); expect(response).to.have.property('Product').to.have.property('CompetitivePricing'); - expect(response).to.have.property('Product').to.have.property('CompetitivePricing').to.have.property('NumberOfOfferListings'); - expect(response).to.have.property('Product').to.have.property('CompetitivePricing').to.have.property('NumberOfOfferListings').to.have.property('OfferListingCount').to.be.a('array'); - expect(response.Product.CompetitivePricing.NumberOfOfferListings.OfferListingCount[0]).to.have.property('condition'); - expect(response.Product.CompetitivePricing.NumberOfOfferListings.OfferListingCount[0]).to.have.property('Value'); + if (response.Product.CompetitivePricing.NumberOfOfferListings.OfferListingCount) { + expect(response).to.have.property('Product').to.have.property('CompetitivePricing').to.have.property('NumberOfOfferListings'); + expect(response).to.have.property('Product').to.have.property('CompetitivePricing').to.have.property('NumberOfOfferListings').to.have.property('OfferListingCount').to.be.a('array'); + expect(response.Product.CompetitivePricing.NumberOfOfferListings.OfferListingCount[0]).to.have.property('condition'); + expect(response.Product.CompetitivePricing.NumberOfOfferListings.OfferListingCount[0]).to.have.property('Value'); + } expect(response).to.have.property('ResponseMetadata').to.be.a('object'); expect(response).to.have.property('ResponseMetadata').to.have.property('RequestId'); expect(response).to.have.property('Headers').to.be.a('object'); From 3779f7f0153c518cb44ee3f0821171110024c859 Mon Sep 17 00:00:00 2001 From: BL Date: Wed, 17 Apr 2019 16:35:41 +0530 Subject: [PATCH 6/7] add types for overriding responseType --- README.md | 8 ++++---- examples/javaScript/sample/requestReportXMLResponse.js | 9 +++++++-- index.d.ts | 4 +++- lib/AmazonMwsResource.js | 5 ++--- lib/amazon-mws.js | 4 ++-- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index fc86178..289a384 100644 --- a/README.md +++ b/README.md @@ -722,8 +722,7 @@ Originally by [Bhushankumar L](mailto:bhushankumar.lilapara@gmail.com). 'Action': 'RequestReport', 'SellerId': 'SELLER_ID', 'MWSAuthToken': 'MWS_AUTH_TOKEN', - 'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_', - '__RESPONSE_TYPE__': 'XML' + 'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_' }, function (error, response) { if (error) { console.log('error ', error); @@ -793,13 +792,14 @@ Originally by [Bhushankumar L](mailto:bhushankumar.lilapara@gmail.com). #### Request Report XML Response ``` + amazonMws.setResponseFormat('XML'); + amazonMws.reports.submit({ 'Version': '2009-01-01', 'Action': 'RequestReport', 'SellerId': 'SELLER_ID', 'MWSAuthToken': 'MWS_AUTH_TOKEN', - 'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_', - '__RESPONSE_TYPE__': 'XML' + 'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_' }, function (error, response) { if (error) { console.log('error ', error); diff --git a/examples/javaScript/sample/requestReportXMLResponse.js b/examples/javaScript/sample/requestReportXMLResponse.js index 2772904..9695a96 100644 --- a/examples/javaScript/sample/requestReportXMLResponse.js +++ b/examples/javaScript/sample/requestReportXMLResponse.js @@ -5,14 +5,19 @@ var accessSecret = process.env.AWS_SECRET_ACCESS_KEY || 'YOUR_SECRET'; var amazonMws = require('../../../lib/amazon-mws')(accessKey, accessSecret); +/** + * This example has been written to override/set the default RESPONSE type to XML. + */ + var reportRequest = function () { + amazonMws.setResponseFormat('XML'); + amazonMws.reports.submit({ 'Version': '2009-01-01', 'Action': 'RequestReport', 'SellerId': 'SELLER_ID', 'MWSAuthToken': 'MWS_AUTH_TOKEN', - 'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_', - '__RESPONSE_TYPE__': 'XML' + 'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_' }, function (error, response) { if (error) { console.log('error ', error); diff --git a/index.d.ts b/index.d.ts index 4580361..4ffb74d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -65,7 +65,7 @@ declare class Sellers extends BaseAmazonMWS { declare class Subscriptions extends BaseAmazonMWS { create(params: any): Promise; - + searchFor(params: any): Promise; remove(params: any): Promise; @@ -82,6 +82,8 @@ declare class AmazonMWS { setHost(host?: string, port?: string, protocol?: string): void; + setResponseFormat(responseFormat: string): void; + feeds: Feeds; finances: Finances; diff --git a/lib/AmazonMwsResource.js b/lib/AmazonMwsResource.js index 1d61e91..32d2b78 100644 --- a/lib/AmazonMwsResource.js +++ b/lib/AmazonMwsResource.js @@ -406,9 +406,8 @@ AmazonMwsResource.prototype = { userCharset = data.__CHARSET__; delete data.__CHARSET__; } - if (data.__RESPONSE_TYPE__) { - responseType = data.__RESPONSE_TYPE__; - delete data.__RESPONSE_TYPE__; + if (self._AmazonMws.getApiField('format') !== 'JSON') { + responseType = self._AmazonMws.getApiField('format'); } self.requestParams = data; diff --git a/lib/amazon-mws.js b/lib/amazon-mws.js index 2112a95..bfd5334 100644 --- a/lib/amazon-mws.js +++ b/lib/amazon-mws.js @@ -4,7 +4,7 @@ AmazonMws.DEFAULT_HOST = 'mws.amazonservices.com'; AmazonMws.DEFAULT_PORT = '443'; AmazonMws.DEFAULT_BASE_PATH = '/'; -AmazonMws.DEFAULT_RESPONSE_FORMAT = '.json'; +AmazonMws.DEFAULT_RESPONSE_FORMAT = 'JSON'; AmazonMws.DEFAULT_API_VERSION = null; // Use node's default timeout: @@ -72,7 +72,7 @@ function AmazonMws(accessKey, accessSecret, version) { this._prepResources(); this.setApiKey(accessKey, accessSecret); this.setApiVersion(version); - this.setResponseFormat(AmazonMws.DEFAULT_RESPONSE_FORMAT); + // this.setResponseFormat(AmazonMws.DEFAULT_RESPONSE_FORMAT); } AmazonMws.prototype = { From b64094f19f7a992669dafcf1b196af09bfb20e6a Mon Sep 17 00:00:00 2001 From: BL Date: Wed, 17 Apr 2019 16:43:23 +0530 Subject: [PATCH 7/7] Allow user to change content type for request --- CHANGELOG.md | 3 +- README.md | 19 +++++++++++ .../sample/requestReportContentType.js | 30 +++++++++++++++++ index.d.ts | 32 +++++++------------ lib/AmazonMwsResource.js | 4 +++ lib/amazon-mws.js | 4 +++ 6 files changed, 71 insertions(+), 21 deletions(-) create mode 100644 examples/javaScript/sample/requestReportContentType.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cc5127..9a57ed5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ 0.0.24 - Add Subscription in Typescript definition - Correct Documentation - - Allow response type into XML format + - Allow user to change response type into XML format + - Allow user to change content type for request 0.0.23 - Bug Fix for Feed Result charset diff --git a/README.md b/README.md index 289a384..4c2982e 100644 --- a/README.md +++ b/README.md @@ -790,6 +790,25 @@ Originally by [Bhushankumar L](mailto:bhushankumar.lilapara@gmail.com). }); ``` +#### Request Report Content Type +``` + amazonMws.setContentType('application/json'); + + amazonMws.reports.submit({ + 'Version': '2009-01-01', + 'Action': 'RequestReport', + 'SellerId': 'SELLER_ID', + 'MWSAuthToken': 'MWS_AUTH_TOKEN', + 'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_' + }, function (error, response) { + if (error) { + console.log('error ', error); + return; + } + console.log('response', response); + }); +``` + #### Request Report XML Response ``` amazonMws.setResponseFormat('XML'); diff --git a/examples/javaScript/sample/requestReportContentType.js b/examples/javaScript/sample/requestReportContentType.js new file mode 100644 index 0000000..0372ce1 --- /dev/null +++ b/examples/javaScript/sample/requestReportContentType.js @@ -0,0 +1,30 @@ +'use strict'; + +var accessKey = process.env.AWS_ACCESS_KEY_ID || 'YOUR_KEY'; +var accessSecret = process.env.AWS_SECRET_ACCESS_KEY || 'YOUR_SECRET'; + +var amazonMws = require('../../../lib/amazon-mws')(accessKey, accessSecret); + +/** + * This example has been written to override/set the contentType of the request. + */ + +var reportRequest = function () { + amazonMws.setContentType('application/json'); + + amazonMws.reports.submit({ + 'Version': '2009-01-01', + 'Action': 'RequestReport', + 'SellerId': 'SELLER_ID', + 'MWSAuthToken': 'MWS_AUTH_TOKEN', + 'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_' + }, function (error, response) { + if (error) { + console.log('error ', error); + return; + } + console.log('response', response); + }); +}; + +reportRequest(); \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 4ffb74d..9c81b32 100644 --- a/index.d.ts +++ b/index.d.ts @@ -74,38 +74,30 @@ declare class Subscriptions extends BaseAmazonMWS { declare class AmazonMWS { - constructor() - - constructor(key: string, token: string); - - setApiKey(key: string, secret: string): void; - - setHost(host?: string, port?: string, protocol?: string): void; - - setResponseFormat(responseFormat: string): void; - feeds: Feeds; - finances: Finances; - fulfillmentInboundShipment: FulfillmentInboundShipment; - fulfillmentInventory: FulfillmentInventory; - fulfillmentOutboundShipment: FulfillmentOutboundShipment; - merchantFulfillment: MerchantFulfillment; - orders: Orders; - products: Products; - reports: Reports; - sellers: Sellers; - subscriptions: Subscriptions; + constructor() + + constructor(key: string, token: string); + + setApiKey(key: string, secret: string): void; + + setHost(host?: string, port?: string, protocol?: string): void; + + setResponseFormat(responseFormat: string): void; + + setContentType(setContentType: string): void; + } diff --git a/lib/AmazonMwsResource.js b/lib/AmazonMwsResource.js index 32d2b78..d4f6f6a 100644 --- a/lib/AmazonMwsResource.js +++ b/lib/AmazonMwsResource.js @@ -482,6 +482,10 @@ AmazonMwsResource.prototype = { self.body = self.requestParams; } + if (self._AmazonMws.getApiField('contentType')) { + headers['Content-Type'] = self._AmazonMws.getApiField('contentType'); + } + // Make a deep copy of the request params, assign to block scoped variable var requestParamsCopy = JSON.parse(JSON.stringify(self.requestParams)); var requestParamsJSONCopy = JSON.parse(JSON.stringify(self.requestParamsJSON)); diff --git a/lib/amazon-mws.js b/lib/amazon-mws.js index bfd5334..c20a17a 100644 --- a/lib/amazon-mws.js +++ b/lib/amazon-mws.js @@ -99,6 +99,10 @@ AmazonMws.prototype = { this._setApiField('format', format); }, + setContentType: function (contentType) { + this._setApiField('contentType', contentType); + }, + setApiVersion: function (version) { if (version) { this._setApiField('version', version);