diff --git a/.jshintignore b/.jshintignore new file mode 100644 index 0000000..93a3c9f --- /dev/null +++ b/.jshintignore @@ -0,0 +1,3 @@ +node_modules +examples/typeScript +test/ \ No newline at end of file diff --git a/.jshintrc b/.jshintrc index 1afdb81..963c2d5 100755 --- a/.jshintrc +++ b/.jshintrc @@ -6,7 +6,15 @@ "exports": true, "module": true, "Buffer": true, - "JSON": true + "JSON": true, + "async": true, + /* MOCHA */ + "describe": false, + "it": false, + "before": false, + "beforeEach": false, + "after": false, + "afterEach": false }, "maxparams": 6, "maxdepth": 15, @@ -40,7 +48,7 @@ "smarttabs": true, "indent": 2, "latedef": "nofunc", - "unused": false, + "unused": true, "enforceall": true, "-W024": true, "-W089": false, diff --git a/.travis.yml b/.travis.yml index b0a0b8a..2a7b90d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: node_js node_js: - - "0.12" - "4" - "5" - "6" diff --git a/CHANGELOG.md b/CHANGELOG.md index fb61c77..58113d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +0.0.18 + - Add basic Test cases + - Add jshint lint checking + - Code Reformatting + - Add support for parsing special characters + - Add support for API which return data as binary file format + - Extend support for GetLowestPricedOffersForASIN, GetLowestPricedOffersForSKU and other product methods + - Added test cases for Feeds, Finances, Fulfillment Inventory, Fulfillment Outbound Shipment, Fulfillment Inbound Shipment, Products, Orders, Sellers + 0.0.17 - Extend support for TypeScript typed definition : feeds, finances, fulfillmentInboundShipment, fulfillmentInventory, fulfillmentOutboundShipment, merchantFulfillment, orders, products, sellers diff --git a/README.md b/README.md index 46135bb..a29fd4d 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,11 @@ You can find [examples here](https://github.com/bhushankumarl/amazon-mws/tree/ma $ npm install amazon-mws --save ``` +## Test Cases +```bash +$ npm run test.mocha +``` + ## Debugging Run the DEBUG: @@ -31,7 +36,7 @@ export AWS_ACCESS_KEY_ID=KEY export AWS_SECRET_ACCESS_KEY=SECRET ``` -## Configuration +## Configuration Using JavaScript Set your Access Key and Access Secret. @@ -39,6 +44,16 @@ Set your Access Key and Access Secret. var amazonMws = require('amazon-mws')('AWS_ACCESS_KEY_ID','AWS_SECRET_ACCESS_KEY'); ``` +## Configuration Using TypeScript + +``` +import * as MwsApi from 'amazon-mws'; + +const amazonMws = new MwsApi(); +amazonMws.setApiKey(accessKey, accessSecret); +``` + + ### Feeds #### Submit Feed @@ -346,6 +361,44 @@ var amazonMws = require('amazon-mws')('AWS_ACCESS_KEY_ID','AWS_SECRET_ACCESS_KEY ### Products +#### Get Lowest Priced Offers For ASIN +```js + amazonMws.products.searchFor({ + 'Version': '2011-10-01', + 'Action': 'GetLowestPricedOffersForASIN', + 'SellerId': 'SELLER_ID', + 'MWSAuthToken': 'MWS_AUTH_TOKEN', + 'MarketplaceId': 'MARKET_PLACE_ID', + 'ASIN': 'ASIN', + 'ItemCondition': 'New' + }, function (error, response) { + if (error) { + console.log('error products', error); + return; + } + console.log('response ', response); + }); +``` + +#### Get Lowest Priced Offers For SKU +```js + amazonMws.products.searchFor({ + 'Version': '2011-10-01', + 'Action': 'GetLowestPricedOffersForSKU', + 'SellerId': 'SELLER_ID', + 'MWSAuthToken': 'MWS_AUTH_TOKEN', + 'MarketplaceId': 'MARKET_PLACE_ID', + 'SellerSKU': 'SELLER_SKU', + 'ItemCondition': 'New' + }, function (error, response) { + if (error) { + console.log('error ', error); + return; + } + console.log('response ', response); + }); +``` + #### List Matching Products ```js amazonMws.products.search({ @@ -438,6 +491,7 @@ var amazonMws = require('amazon-mws')('AWS_ACCESS_KEY_ID','AWS_SECRET_ACCESS_KEY #### Get Report ###### This will provide you JSON report/data. +###### This will not provide you Throttling details in Header. ```js amazonMws.reports.search({ 'Version': '2009-01-01', @@ -479,6 +533,31 @@ var amazonMws = require('amazon-mws')('AWS_ACCESS_KEY_ID','AWS_SECRET_ACCESS_KEY }); ``` +#### Get Report +###### Using TypeScript. +``` + const accessKey = process.env.AWS_ACCESS_KEY_ID || 'YOUR_KEY'; + const accessSecret = process.env.AWS_SECRET_ACCESS_KEY || 'YOUR_SECRET'; + + import * as MwsApi from 'amazon-mws'; + + const amazonMws = new MwsApi(); + amazonMws.setApiKey(accessKey, accessSecret); + + try { + const response: any = await amazonMws.reports.search({ + 'Version': '2009-01-01', + 'Action': 'GetReport', + 'SellerId': 'SELLER_ID', + 'MWSAuthToken': 'MWS_AUTH_TOKEN', + 'ReportId': 'REPORT_ID' + }); + console.log('response', response); + } catch (error: any) { + console.log('error ', error); + } +``` + #### Additionally all api returns Throttling: Limits to how often you can submit requests Reference : http://docs.developer.amazonservices.com/en_CA/dev_guide/DG_Throttling.html ```json diff --git a/examples/javaScript/feeds/file.txt b/examples/javaScript/feeds/file.txt index ff50c6a..bf669e8 100644 --- a/examples/javaScript/feeds/file.txt +++ b/examples/javaScript/feeds/file.txt @@ -2,7 +2,7 @@
1.01 -AUV38W4NKU8JH +XXXXXXXXXXXXXXX
Inventory diff --git a/examples/javaScript/feeds/getFeedSubmissionResultRaw.js b/examples/javaScript/feeds/getFeedSubmissionResultRaw.js new file mode 100644 index 0000000..840b0d9 --- /dev/null +++ b/examples/javaScript/feeds/getFeedSubmissionResultRaw.js @@ -0,0 +1,33 @@ +'use strict'; +var fs = require('fs'); + +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 fse = require('fs-extra'); + +/** + * Use __RAW__ to get the raw response in response->data; + * This along with __CHARSET__ do not get written in the request. + * */ +function feedRequest() { + var FeedSubmissionId = '10101010XXX'; + amazonMws.feeds.search({ + 'Version': '2009-01-01', + 'Action': 'GetFeedSubmissionResult', + 'SellerId': 'SELLER_ID', + 'MWSAuthToken': 'MWS_AUTH_TOKEN', + 'FeedSubmissionId': FeedSubmissionId, + __RAW__: true + }, function (error, response) { + if (error) { + console.log('error ', error); + return; + } + fse.writeFileSync('response.txt', response.data); + console.log('Headers', response.Headers); + }); +} + +feedRequest(); \ No newline at end of file diff --git a/examples/javaScript/products/getLowestPricedOffersForASIN.js b/examples/javaScript/products/getLowestPricedOffersForASIN.js new file mode 100644 index 0000000..d85f9ca --- /dev/null +++ b/examples/javaScript/products/getLowestPricedOffersForASIN.js @@ -0,0 +1,26 @@ +'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 productRequest = function () { + amazonMws.products.searchFor({ + 'Version': '2011-10-01', + 'Action': 'GetLowestPricedOffersForASIN', + 'SellerId': 'SELLER_ID', + 'MWSAuthToken': 'MWS_AUTH_TOKEN', + 'MarketplaceId': 'MARKET_PLACE_ID', + 'ASIN': 'ASIN', + 'ItemCondition': 'New' + }, function (error, response) { + if (error) { + console.log('error products', error); + return; + } + console.log('response ', response); + }); +}; + +productRequest(); \ No newline at end of file diff --git a/examples/javaScript/products/getLowestPricedOffersForSKU.js b/examples/javaScript/products/getLowestPricedOffersForSKU.js new file mode 100644 index 0000000..007d355 --- /dev/null +++ b/examples/javaScript/products/getLowestPricedOffersForSKU.js @@ -0,0 +1,26 @@ +'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 productRequest = function () { + amazonMws.products.searchFor({ + 'Version': '2011-10-01', + 'Action': 'GetLowestPricedOffersForSKU', + 'SellerId': 'SELLER_ID', + 'MWSAuthToken': 'MWS_AUTH_TOKEN', + 'MarketplaceId': 'MARKET_PLACE_ID', + 'SellerSKU': 'SELLER_SKU', + 'ItemCondition': 'New' + }, function (error, response) { + if (error) { + console.log('error ', error); + return; + } + console.log('response ', response); + }); +}; + +productRequest(); \ No newline at end of file diff --git a/examples/javaScript/reports/getReport.js b/examples/javaScript/reports/getReport.js index 680861f..66d1c85 100644 --- a/examples/javaScript/reports/getReport.js +++ b/examples/javaScript/reports/getReport.js @@ -5,13 +5,18 @@ var accessSecret = process.env.AWS_SECRET_ACCESS_KEY || 'YOUR_SECRET'; var amazonMws = require('../../../lib/amazon-mws')(accessKey, accessSecret); +/** + * This will not provide you Throttling details in Header. + * Amazon MWS itself not providing Throttling detail in GetReport call. + */ + var reportRequest = function () { amazonMws.reports.search({ 'Version': '2009-01-01', 'Action': 'GetReport', 'SellerId': 'SELLER_ID', 'MWSAuthToken': 'MWS_AUTH_TOKEN', - 'ReportId':'REPORT_ID' + 'ReportId': 'REPORT_ID' }, function (error, response) { if (error) { console.log('error ', error); diff --git a/examples/javaScript/reports/getReportList.js b/examples/javaScript/reports/getReportList.js index 660fb7b..f6239a6 100644 --- a/examples/javaScript/reports/getReportList.js +++ b/examples/javaScript/reports/getReportList.js @@ -11,7 +11,7 @@ var reportRequest = function () { 'Version': '2009-01-01', 'Action': 'GetReportList', 'SellerId': 'SELLER_ID', - 'MWSAuthToken': 'MWS_AUTH_TOKEN', + 'MWSAuthToken': 'MWS_AUTH_TOKEN' //'ReportTypeList.Type.1': 'REPORT_TYPE_LIST' //optional }, function (error, response) { if (error) { diff --git a/examples/typeScript/package.json b/examples/typeScript/package.json index f2484ca..2b8df6b 100644 --- a/examples/typeScript/package.json +++ b/examples/typeScript/package.json @@ -1,15 +1,15 @@ { - "name": "trello-sample", - "version": "1.0.0", - "description": "trello", - "license": "MIT", - "dependencies": { - "@types/node": "^8.0.53", - "@types/es6-promise": "0.0.33", - "babel-eslint": "^8.0.1", - "babel-preset-node6": "^11.0.0", - "amazon-mws": "^0.0.16", - "node-expat": "^2.3.16" - }, - "devDependencies": {} + "name": "amazon-mws-typescript-sample", + "version": "1.0.0", + "description": "trello", + "license": "MIT", + "dependencies": { + "@types/node": "^8.0.53", + "@types/es6-promise": "0.0.33", + "babel-eslint": "^8.0.1", + "babel-preset-node6": "^11.0.0", + "amazon-mws": "*", + "node-expat": "^2.3.16" + }, + "devDependencies": {} } diff --git a/examples/typeScript/reports/getReport.ts b/examples/typeScript/reports/getReport.ts index b6ace4f..50dff09 100644 --- a/examples/typeScript/reports/getReport.ts +++ b/examples/typeScript/reports/getReport.ts @@ -6,6 +6,11 @@ import * as MwsApi from 'amazon-mws'; const amazonMws = new MwsApi(); amazonMws.setApiKey(accessKey, accessSecret); +/** + * This will not provide you Throttling details in Header. + * Amazon MWS itself not providing Throttling detail in GetReport call. + */ + const reportRequest = async () => { try { @@ -21,7 +26,6 @@ const reportRequest = async () => { console.log('error ', error); } - }; reportRequest(); \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 4c6035d..418f5e0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -42,6 +42,8 @@ declare class Orders extends BaseAmazonMWS { declare class Products extends BaseAmazonMWS { + searchFor(params: any): Promise; + } declare class Reports extends BaseAmazonMWS { diff --git a/lib/AmazonMwsMethod.js b/lib/AmazonMwsMethod.js index f0d36b6..3e8121f 100644 --- a/lib/AmazonMwsMethod.js +++ b/lib/AmazonMwsMethod.js @@ -1,6 +1,6 @@ 'use strict'; -var debug = require('debug')('MWS:AmazonMwsMethod'); +// var debug = require('debug')('MWS:AmazonMwsMethod'); var objectAssign = require('object-assign'); var Promise = require('bluebird'); var utils = require('./utils'); @@ -25,12 +25,11 @@ function amazonMwsMethod(spec) { var requestMethod = (spec.method || 'GET').toUpperCase(); var urlParams = spec.urlParams || []; var encode = spec.encode || function (data) { - return data; - }; + return data; + }; return function () { var self = this; var args = [].slice.call(arguments); - var callback = typeof args[args.length - 1] === 'function' && args.pop(); var urlData = this.createUrlData(); @@ -51,7 +50,6 @@ function amazonMwsMethod(spec) { continue; } - return reject(new Error( 'AmazonMws: Argument "' + urlParams[i] + '" required, but got: ' + arg + ' (on API request to ' + requestMethod + ' ' + commandPath + ')' @@ -85,6 +83,7 @@ function amazonMwsMethod(spec) { } var options = {headers: objectAssign(opts.headers, spec.headers)}; + options.useBody = spec.useBody || false; self._request(requestMethod, requestPath, data, opts.auth, options, requestCallback); }).bind(this)), callback); }; diff --git a/lib/AmazonMwsResource.js b/lib/AmazonMwsResource.js index fb8bf2e..e4194fa 100644 --- a/lib/AmazonMwsResource.js +++ b/lib/AmazonMwsResource.js @@ -10,13 +10,15 @@ var crypto = require('crypto'); var _ = require('lodash'); var qs = require('qs'); var csv = require('fast-csv'); +var Iconv = require('iconv').Iconv; var utils = require('./utils'); var Error = require('./Error'); + var hasOwn = {}.hasOwnProperty; -var RESPONSE_CONTENT_TYPE = ['text/xml', 'text/xml;charset=utf-8']; +var RESPONSE_CONTENT_TYPE = ['text/xml', 'text/xml;charset=utf-8', 'application/xml']; // Provide extension mechanism for AmazonMws Resource Sub-Classes AmazonMwsResource.extend = utils.protoExtend; @@ -119,7 +121,7 @@ AmazonMwsResource.prototype = { }; }, - _responseHandler: function (requestParamsJSONCopy, req, callback) { + _responseHandler: function (requestParamsJSONCopy, req, userOptions, callback) { var self = this; function processXml(o, action) { @@ -153,20 +155,14 @@ AmazonMwsResource.prototype = { function parseCSVFile(res, responseString, delimiter, callback) { var data = []; + csv.fromString(responseString, {headers: true, delimiter: delimiter, ignoreEmpty: true}) .on('data', function (value) { data.push(value); }) .on('end', function () { - debug('response after parsing file %o ', data); var items = {}; items.data = data; - items.Headers = { - 'x-mws-quota-max': res.headers['x-mws-quota-max'] || 'unknown', - 'x-mws-quota-remaining': res.headers['x-mws-quota-remaining'] || 'unknown', - 'x-mws-quota-resetson': res.headers['x-mws-quota-resetson'] || 'unknown', - 'x-mws-timestamp': res.headers['x-mws-timestamp'] - }; return callback(null, items); }) .on('error', function (error) { @@ -183,12 +179,6 @@ AmazonMwsResource.prototype = { var items = xml2json.toJson(responseString); //debug('res after parsing JSON %o ', res); items = JSON.parse(items); - items.Headers = { - 'x-mws-quota-max': res.headers['x-mws-quota-max'] || 'unknown', - 'x-mws-quota-remaining': res.headers['x-mws-quota-remaining'] || 'unknown', - 'x-mws-quota-resetson': res.headers['x-mws-quota-resetson'] || 'unknown', - 'x-mws-timestamp': res.headers['x-mws-timestamp'] - }; //debug('after adding header res', res); return callback(null, items); } else { @@ -207,28 +197,75 @@ AmazonMwsResource.prototype = { } return function (res) { - var responseString = ''; - res.setEncoding('utf8'); + var dbgResponseBuffer = []; + var headers = res.headers; + /* new stuff */ + + var charset = ''; + var content_type = ''; + if (headers['content-type'].indexOf('charset') > -1 && headers['content-type'].split(';')[0] && headers['content-type'].split(';')[1]) { + content_type = headers['content-type'].split(';')[0]; + if(headers['content-type'].split(';')[1].match(/^((\b[^\s=]+)=(([^=]|\\=)+))*$/)[3]){ + charset = headers['content-type'].split(';')[1].match(/^((\b[^\s=]+)=(([^=]|\\=)+))*$/)[3]; + } + } else { + content_type = headers['content-type']; + } + var ResponseHeaders = { + 'x-mws-quota-max': res.headers['x-mws-quota-max'] || 'unknown', + 'x-mws-quota-remaining': res.headers['x-mws-quota-remaining'] || 'unknown', + 'x-mws-quota-resetson': res.headers['x-mws-quota-resetson'] || 'unknown', + 'x-mws-timestamp': res.headers['x-mws-timestamp'], + 'content-type': content_type || 'unknown', + 'content-charset': charset || 'unknown', + 'content-length': res.headers['content-length'] || 'unknown', + 'content-md5': res.headers['content-md5'] || 'unknown', + 'date': res.headers['date'] || 'unknown', + }; + res.on('data', function (chunk) { - responseString += chunk; + dbgResponseBuffer.push(chunk); }); res.on('end', function () { + var bufferString = Buffer.concat(dbgResponseBuffer); + if (userOptions.userRaw === true) { + debug('Inside raw response'); + var response = { + data: bufferString, + Headers: ResponseHeaders + }; + return callback.call(self, null, response); + } + if (userOptions.userCharset) { + charset = userOptions.userCharset; + } + var responseString = ''; + if (charset) { + try { + var iconv = new Iconv(charset, 'UTF-8'); + responseString = iconv.convert(bufferString).toString(); + } catch (Exception) { + debug('Exception ', Exception); + return callback.call(self, new Error.AmazonMwsAPIError({ + message: 'Failed to parse response received from the AmazonMws API', + response: response, + exception: Exception + }), null); + } + } else { + responseString = bufferString.toString(); + } + debug('responseString ', responseString); try { - //debug('self.requestParamsJSON ', self.requestParamsJSON); - processResponseType(res, responseString, function (error, response) { - + response.Headers = ResponseHeaders; if (error) { debug('Error ', error); - return callback.call( - self, - new Error.AmazonMwsAPIError({ - message: 'Failed to parse response received from the AmazonMws API', - response: response, - exception: error - }), - null - ); + return callback.call(self, new Error.AmazonMwsAPIError({ + message: 'Failed to parse response received from the AmazonMws API', + response: response, + exception: error + }), null); } if (response.ErrorResponse) { @@ -242,10 +279,9 @@ AmazonMwsResource.prototype = { debug('res.headers[content-type]', res.headers['content-type']); if (RESPONSE_CONTENT_TYPE.indexOf(res.headers['content-type'].toLowerCase()) > -1) { /* - * It should execute for only XML response - */ - // debug('response.headers', response.headers); - debug('response Before Process %o ', JSON.stringify(response)); + It should execute for only XML response + */ + // debug('response Before Process %o ', JSON.stringify(response)); Headers = response.Headers; if (response[requestParamsJSONCopy.Action + 'Response']) { response = response[requestParamsJSONCopy.Action + 'Response']; @@ -264,7 +300,7 @@ AmazonMwsResource.prototype = { response.ResponseMetadata = ResponseMetadata; response.Headers = Headers; - debug('response before %o ', JSON.stringify(response)); + // debug('response before %o ', JSON.stringify(response)); processXml(response, 'update'); processXml(response, 'delete'); //debug('response after Process %o ', JSON.stringify(response)); @@ -280,16 +316,12 @@ AmazonMwsResource.prototype = { }); - } catch (e) { - return callback.call( - self, - new Error.AmazonMwsAPIError({ - message: 'Invalid JSON received from the AmazonMws API', - response: responseString, - exception: e - }), - null - ); + } catch (exception) { + return callback.call(self, new Error.AmazonMwsAPIError({ + message: 'Invalid JSON received from the AmazonMws API', + response: responseString, + exception: exception + }), null); } }); }; @@ -302,58 +334,54 @@ AmazonMwsResource.prototype = { // already handled return; } - callback.call( - self, - new Error.AmazonMwsConnectionError({ - message: 'An error occurred with our connection to AmazonMws', - detail: error - }), - null - ); + callback.call(self, new Error.AmazonMwsConnectionError({ + message: 'An error occurred with our connection to AmazonMws', + detail: error + }), null); }; }, _request: function (method, path, data, auth, options, callback) { var self = this; - //debug('path ', path); self.requestParams = data; self.body = ''; + + var userRaw = ''; + var userCharset = ''; + /**custom option passed by user, a better way to do this would be nice */ + if (data.__RAW__) { + userRaw = data.__RAW__; + delete data.__RAW__; + } + if (data.__CHARSET__) { + userCharset = data.__CHARSET__; + delete data.__CHARSET__; + } + if (!self.requestParams.Version) { - return callback.call( - self, - new Error.AmazonMwsAPIError({ - message: 'Please specify the Amazon MWS API Version', - detail: 'http://docs.developer.amazonservices.com', - type: 'GenericError', - code: '404', - param: self.requestParams - }), - null - ); + return callback.call(self, new Error.AmazonMwsAPIError({ + message: 'Please specify the Amazon MWS API Version', + detail: 'http://docs.developer.amazonservices.com', + type: 'GenericError', + code: '404', + param: self.requestParams + }), null); } else if (!self._AmazonMws.getApiField('key')) { - return callback.call( - self, - new Error.AmazonMwsAPIError({ - message: '**Please specify the AWS_ACCESS_KEY_ID', - detail: 'http://docs.developer.amazonservices.com', - type: 'GenericError', - code: '404', - param: self.requestParams - }), - null - ); + return callback.call(self, new Error.AmazonMwsAPIError({ + message: 'Please specify the AWS_ACCESS_KEY_ID', + detail: 'http://docs.developer.amazonservices.com', + type: 'GenericError', + code: '404', + param: self.requestParams + }), null); } else if (!self._AmazonMws.getApiField('secret')) { - return callback.call( - self, - new Error.AmazonMwsAPIError({ - message: 'Please specify the AWS_SECRET_ACCESS_KEY', - detail: 'http://docs.developer.amazonservices.com', - type: 'GenericError', - code: '404', - param: self.requestParams - }), - null - ); + return callback.call(self, new Error.AmazonMwsAPIError({ + message: 'Please specify the AWS_SECRET_ACCESS_KEY', + detail: 'http://docs.developer.amazonservices.com', + type: 'GenericError', + code: '404', + param: self.requestParams + }), null); } if (self.requestParams.APIVersion) { path = path + '/' + self.requestParams.APIVersion; @@ -387,14 +415,12 @@ AmazonMwsResource.prototype = { self.requestParams.Signature = crypto.createHmac('sha256', this._AmazonMws.getApiField('secret')).update(stringToSign, 'utf8').digest('base64'); self.requestParamsJSON = _.clone(self.requestParams); - self.requestParams = qs.stringify(self.requestParams); - /* + self.requestParams = '&' + qs.stringify(self.requestParams); + /** * Use Feed Content without modify it as querystring for the SubmitFeed API */ - //self.body = qs.stringify(self.body); var headers = { - 'Accept': 'text/xml', 'Content-Type': 'text/xml', 'Content-MD5': crypto.createHash('md5').update(self.requestParams).digest('base64') }; @@ -403,6 +429,11 @@ AmazonMwsResource.prototype = { headers['Content-Type'] = 'x-www-form-urlencoded'; headers['Content-MD5'] = self.requestParamsJSON.ContentMD5Value; } + if (options.useBody === true) { + headers['Content-Type'] = 'application/x-www-form-urlencoded'; + headers['Content-Length'] = Buffer.byteLength(self.requestParams); + self.body = self.requestParams; + } // Make a deep copy of the request params, assign to block scoped variable var requestParamsCopy = JSON.parse(JSON.stringify(self.requestParams)); @@ -427,20 +458,22 @@ AmazonMwsResource.prototype = { method: method, headers: headers }; - params.path = params.path + '?' + requestParamsCopy; + if (options.useBody === false) { + params.path = params.path + '?' + requestParamsCopy; + } + debug('params %o ', params); debug('body %o ', self.body); var req = (isInsecureConnection ? http : https).request(params); req.setTimeout(timeout, self._timeoutHandler(timeout, req, callback)); - req.on('response', self._responseHandler(requestParamsJSONCopy, req, callback)); + req.on('response', self._responseHandler(requestParamsJSONCopy, req, {userCharset, userRaw}, callback)); req.on('error', self._errorHandler(req, callback)); req.on('socket', function (socket) { socket.on((isInsecureConnection ? 'connect' : 'secureConnect'), function () { // Send payload; we're safe: - //debug('body %o ', self.body); /* * Use Feed Content without modify it as querystring for the SubmitFeed API */ diff --git a/lib/amazon-mws.js b/lib/amazon-mws.js index 480a225..acd4f38 100644 --- a/lib/amazon-mws.js +++ b/lib/amazon-mws.js @@ -1,6 +1,6 @@ 'use strict'; -var debug = require('debug')('MWS:amazon-mws'); +// var debug = require('debug')('MWS:amazon-mws'); AmazonMws.DEFAULT_HOST = 'mws.amazonservices.com'; AmazonMws.DEFAULT_PORT = '443'; AmazonMws.DEFAULT_BASE_PATH = '/'; diff --git a/lib/resources/Feeds.js b/lib/resources/Feeds.js index 5721ed3..182a5aa 100644 --- a/lib/resources/Feeds.js +++ b/lib/resources/Feeds.js @@ -1,6 +1,5 @@ 'use strict'; -var path = require('path'); var AmazonMwsResource = require('../AmazonMwsResource'); var amazonMwsMethod = AmazonMwsResource.method; diff --git a/lib/resources/Finances.js b/lib/resources/Finances.js index 8acf2c8..2050527 100644 --- a/lib/resources/Finances.js +++ b/lib/resources/Finances.js @@ -1,6 +1,5 @@ 'use strict'; -var path = require('path'); var AmazonMwsResource = require('../AmazonMwsResource'); var amazonMwsMethod = AmazonMwsResource.method; diff --git a/lib/resources/FulfillmentInboundShipment.js b/lib/resources/FulfillmentInboundShipment.js index 4e44a1f..6f3506d 100644 --- a/lib/resources/FulfillmentInboundShipment.js +++ b/lib/resources/FulfillmentInboundShipment.js @@ -1,6 +1,5 @@ 'use strict'; -var path = require('path'); var AmazonMwsResource = require('../AmazonMwsResource'); var amazonMwsMethod = AmazonMwsResource.method; diff --git a/lib/resources/FulfillmentInventory.js b/lib/resources/FulfillmentInventory.js index e1a3071..21ccf45 100644 --- a/lib/resources/FulfillmentInventory.js +++ b/lib/resources/FulfillmentInventory.js @@ -1,6 +1,5 @@ 'use strict'; -var path = require('path'); var AmazonMwsResource = require('../AmazonMwsResource'); var amazonMwsMethod = AmazonMwsResource.method; diff --git a/lib/resources/FulfillmentOutboundShipment.js b/lib/resources/FulfillmentOutboundShipment.js index f3737bd..04a7f4c 100644 --- a/lib/resources/FulfillmentOutboundShipment.js +++ b/lib/resources/FulfillmentOutboundShipment.js @@ -1,6 +1,5 @@ 'use strict'; -var path = require('path'); var AmazonMwsResource = require('../AmazonMwsResource'); var amazonMwsMethod = AmazonMwsResource.method; diff --git a/lib/resources/MerchantFulfillment.js b/lib/resources/MerchantFulfillment.js index d9148da..bde2d02 100644 --- a/lib/resources/MerchantFulfillment.js +++ b/lib/resources/MerchantFulfillment.js @@ -1,6 +1,5 @@ 'use strict'; -var path = require('path'); var AmazonMwsResource = require('../AmazonMwsResource'); var amazonMwsMethod = AmazonMwsResource.method; diff --git a/lib/resources/Orders.js b/lib/resources/Orders.js index 63c7b3e..5fa4471 100644 --- a/lib/resources/Orders.js +++ b/lib/resources/Orders.js @@ -1,6 +1,5 @@ 'use strict'; -var path = require('path'); var AmazonMwsResource = require('../AmazonMwsResource'); var amazonMwsMethod = AmazonMwsResource.method; diff --git a/lib/resources/Products.js b/lib/resources/Products.js index 899d452..d98a075 100644 --- a/lib/resources/Products.js +++ b/lib/resources/Products.js @@ -1,6 +1,5 @@ 'use strict'; -var path = require('path'); var AmazonMwsResource = require('../AmazonMwsResource'); var amazonMwsMethod = AmazonMwsResource.method; @@ -9,6 +8,10 @@ module.exports = AmazonMwsResource.extend({ path: 'Products', search: amazonMwsMethod({ method: 'GET' + }), + searchFor: amazonMwsMethod({ + useBody: true, + method: 'POST' }) }); \ No newline at end of file diff --git a/lib/resources/Reports.js b/lib/resources/Reports.js index dcef595..0d850e1 100644 --- a/lib/resources/Reports.js +++ b/lib/resources/Reports.js @@ -1,6 +1,5 @@ 'use strict'; -var path = require('path'); var AmazonMwsResource = require('../AmazonMwsResource'); var amazonMwsMethod = AmazonMwsResource.method; diff --git a/lib/resources/Sellers.js b/lib/resources/Sellers.js index ad0bc9f..f5ad738 100644 --- a/lib/resources/Sellers.js +++ b/lib/resources/Sellers.js @@ -1,6 +1,5 @@ 'use strict'; -var path = require('path'); var AmazonMwsResource = require('../AmazonMwsResource'); var amazonMwsMethod = AmazonMwsResource.method; diff --git a/lib/utils.js b/lib/utils.js index 6ca32f6..7441498 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -12,8 +12,8 @@ var utils = module.exports = { isOptionsHash: function (o) { return _.isPlainObject(o) && ['api_key'].some(function (key) { - return o.hasOwnProperty(key); - }); + return o.hasOwnProperty(key); + }); }, /** diff --git a/package.json b/package.json index d714e54..651a34a 100644 --- a/package.json +++ b/package.json @@ -1,61 +1,71 @@ { - "name": "amazon-mws", - "version": "0.0.17", - "description": "Amazon MWS API wrapper", - "keywords": [ - "Amazon MWS", - "Amazon MWS Feeds", - "Amazon MWS Finances", - "Amazon MWS Fulfillment Inbound Shipment", - "Amazon MWS Fulfillment Outbound Shipment", - "Amazon MWS Fulfillment Inventory", - "Amazon MWS Orders", - "Amazon MWS Products", - "Amazon MWS Sellers", - "node-mws", - "amazon-mws-node-api", - "Amazon Marketplace Web Service", - "mws", - "mws-products", - "mws-sellers", - "mws-orders", - "mws-finances", - "mws-inventory", - "mws-shipment", - "mws-api", - "mws-sdk", - "mws-client", - "mws-node-api", - "mws-node" - ], - "homepage": "https://github.com/bhushankumarl/amazon-mws", - "author": "Bhushankumar Lilapara", - "contributors": [ - "Bhushankumar Lilapara (bhushankumar.lilapara@gmail.com)" - ], - "repository": { - "type": "git", - "url": "git://github.com/bhushankumarl/amazon-mws.git" - }, - "bugs:": "https://github.com/bhushankumarl/amazon-mws/issues", - "engines": { - "node": ">= v0.10.0" - }, - "main": "lib/amazon-mws.js", - "devDependencies": {}, - "dependencies": { - "bluebird": "^3.5.1", - "debug": "^3.1.0", - "fast-csv": "^2.4.0", - "fs-extra": "^5.0.0", - "lodash": "^4.17.4", - "node-expat": "2.3.16", - "object-assign": "^4.1.0", - "qs": "^6.5.1", - "xml2json": "^0.11.0" - }, - "license": "MIT", - "scripts": { - "lint": "jshintrc" - } + "name": "amazon-mws", + "version": "0.0.18", + "description": "Amazon MWS API wrapper", + "keywords": [ + "Amazon MWS", + "Amazon MWS Feeds", + "Amazon MWS Finances", + "Amazon MWS Fulfillment Inbound Shipment", + "Amazon MWS Fulfillment Outbound Shipment", + "Amazon MWS Fulfillment Inventory", + "Amazon MWS Orders", + "Amazon MWS Products", + "Amazon MWS Sellers", + "node-mws", + "amazon-mws-node-api", + "amazon-mws-typescript", + "amazon-mws-await", + "amazon-mws-async", + "amazon-mws-promises", + "Amazon Marketplace Web Service", + "mws", + "mws-products", + "mws-sellers", + "mws-orders", + "mws-finances", + "mws-inventory", + "mws-shipment", + "mws-api", + "mws-sdk", + "mws-client", + "mws-node-api", + "mws-node" + ], + "homepage": "https://github.com/bhushankumarl/amazon-mws", + "author": "Bhushankumar Lilapara", + "contributors": [ + "Bhushankumar Lilapara (bhushankumar.lilapara@gmail.com)" + ], + "repository": { + "type": "git", + "url": "git://github.com/bhushankumarl/amazon-mws.git" + }, + "bugs:": "https://github.com/bhushankumarl/amazon-mws/issues", + "engines": { + "node": ">= v0.10.0" + }, + "main": "lib/amazon-mws.js", + "devDependencies": { + "jshint": "^2.9.5" + }, + "dependencies": { + "bluebird": "^3.5.1", + "chai": "^4.1.2", + "debug": "^3.1.0", + "fast-csv": "^2.4.0", + "fs-extra": "^5.0.0", + "iconv": "^2.3.0", + "lodash": "^4.17.4", + "mocha": "^5.2.0", + "node-expat": "2.3.16", + "object-assign": "^4.1.0", + "qs": "^6.5.1", + "xml2json": "^0.11.0" + }, + "license": "MIT", + "scripts": { + "lint": "jshint ./", + "test.mocha": "mocha" + } } diff --git a/test/intialize/config.js b/test/intialize/config.js new file mode 100644 index 0000000..149915c --- /dev/null +++ b/test/intialize/config.js @@ -0,0 +1,10 @@ +'use strict'; +module.exports = { + accessKey: process.env.AWS_ACCESS_KEY_ID, + accessSecret: process.env.AWS_SECRET_ACCESS_KEY, + SellerId: process.env.SELLER_ID, + MWSAuthToken: process.env.MWS_AUTH_TOKEN, + MarketplaceId: process.env.MARKETPLACE_ID, + ASIN: process.env.ASIN, + SKU: process.env.SKU +}; \ No newline at end of file diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..e1d51cf --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1,11 @@ +### +### mocha.opts +### + +--bail +--silly +--full-trace +--recursive +--timeout 10000 +--reporter spec +./test/specs/*.spec.js diff --git a/test/specs/feeds.spec.js b/test/specs/feeds.spec.js new file mode 100644 index 0000000..6c0378b --- /dev/null +++ b/test/specs/feeds.spec.js @@ -0,0 +1,41 @@ +'use strict'; +var config = require('../intialize/config'); +var accessKey = config.accessKey; +var accessSecret = config.accessSecret; + +var chai = require('chai'); +var expect = chai.expect; + +var amazonMws = require('../../lib/amazon-mws')(accessKey, accessSecret); + +describe('Feeds', function () { + + before(function () { + expect(accessKey).to.be.a('string'); + expect(accessSecret).to.be.a('string'); + }); + + it('It should get Feed Submission list using GetFeedSubmissionList Action', async function () { + var options = { + 'Version': '2009-01-01', + 'Action': 'GetFeedSubmissionList', + 'SellerId': config.SellerId, + 'MWSAuthToken': config.MWSAuthToken + }; + + expect(options.SellerId).to.be.a('string'); + expect(options.MWSAuthToken).to.be.a('string'); + + var response = await amazonMws.feeds.search(options); + + expect(response).to.be.a('object'); + expect(response).to.have.property('FeedSubmissionInfo').to.be.a('array'); + 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'); + }); +}); \ No newline at end of file diff --git a/test/specs/finances.spec.js b/test/specs/finances.spec.js new file mode 100644 index 0000000..37d77f3 --- /dev/null +++ b/test/specs/finances.spec.js @@ -0,0 +1,42 @@ +'use strict'; +var config = require('../intialize/config'); +var accessKey = config.accessKey; +var accessSecret = config.accessSecret; + +var chai = require('chai'); +var expect = chai.expect; + +var amazonMws = require('../../lib/amazon-mws')(accessKey, accessSecret); + +describe('Finances', function () { + + before(function () { + expect(accessKey).to.be.a('string'); + expect(accessSecret).to.be.a('string'); + }); + + it('It should get list of Financial Event Groups using ListFinancialEventGroups Action', async function () { + var options = { + 'Version': '2015-05-01', + 'Action': 'ListFinancialEventGroups', + 'SellerId': config.SellerId, + 'MWSAuthToken': config.MWSAuthToken, + 'FinancialEventGroupStartedAfter': new Date(13, 12, 2016) + }; + + expect(options.SellerId).to.be.a('string'); + expect(options.MWSAuthToken).to.be.a('string'); + + var response = await amazonMws.finances.search(options); + + expect(response).to.be.a('object'); + expect(response).to.have.property('FinancialEventGroupList').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'); + }); +}); \ No newline at end of file diff --git a/test/specs/fulfillmentInboundShipment.spec.js b/test/specs/fulfillmentInboundShipment.spec.js new file mode 100644 index 0000000..c497ca4 --- /dev/null +++ b/test/specs/fulfillmentInboundShipment.spec.js @@ -0,0 +1,46 @@ +'use strict'; +var config = require('../intialize/config'); +var accessKey = config.accessKey; +var accessSecret = config.accessSecret; + +var chai = require('chai'); +var expect = chai.expect; + +var amazonMws = require('../../lib/amazon-mws')(accessKey, accessSecret); + +describe('Fulfillment Inbound Shipment', function () { + + before(function () { + expect(accessKey).to.be.a('string'); + expect(accessSecret).to.be.a('string'); + }); + + it('It should get Inbound Guidance For SKU using GetInboundGuidanceForSKU Action', async function () { + var options = { + 'Version': '2010-10-01', + 'Action': 'GetInboundGuidanceForSKU', + 'SellerId': config.SellerId, + 'MWSAuthToken': config.MWSAuthToken, + 'MarketplaceId': config.MarketplaceId, + 'SellerSKUList.Id.1': config.SKU + }; + + console.log('options ', options); + expect(options.SellerId).to.be.a('string'); + expect(options.MWSAuthToken).to.be.a('string'); + expect(options.MarketplaceId).to.be.a('string'); + expect(options['SellerSKUList.Id.1']).to.be.a('string'); + + var response = await amazonMws.fulfillmentInboundShipment.search(options); + + expect(response).to.be.a('object'); + expect(response).to.have.property('SKUInboundGuidanceList').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'); + }); +}); \ No newline at end of file diff --git a/test/specs/fulfillmentInventory.spec.js b/test/specs/fulfillmentInventory.spec.js new file mode 100644 index 0000000..61cb6c7 --- /dev/null +++ b/test/specs/fulfillmentInventory.spec.js @@ -0,0 +1,43 @@ +'use strict'; +var config = require('../intialize/config'); +var accessKey = config.accessKey; +var accessSecret = config.accessSecret; + +var chai = require('chai'); +var expect = chai.expect; + +var amazonMws = require('../../lib/amazon-mws')(accessKey, accessSecret); + +describe('Fulfillment Inventory', function () { + + before(function () { + expect(accessKey).to.be.a('string'); + expect(accessSecret).to.be.a('string'); + }); + + it('It should get list of Inventory Supply using ListInventorySupply Action', async function () { + var options = { + 'Version': '2010-10-01', + 'Action': 'ListInventorySupply', + 'SellerId': config.SellerId, + 'MWSAuthToken': config.MWSAuthToken, + 'MarketplaceId': config.MarketplaceId, + 'QueryStartDateTime': new Date(13, 12, 2016) + }; + + expect(options.SellerId).to.be.a('string'); + expect(options.MWSAuthToken).to.be.a('string'); + + var response = await amazonMws.fulfillmentInventory.search(options); + + expect(response).to.be.a('object'); + expect(response).to.have.property('InventorySupplyList').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'); + }); +}); \ No newline at end of file diff --git a/test/specs/fulfillmentOutboundShipment.spec.js b/test/specs/fulfillmentOutboundShipment.spec.js new file mode 100644 index 0000000..cff5804 --- /dev/null +++ b/test/specs/fulfillmentOutboundShipment.spec.js @@ -0,0 +1,42 @@ +'use strict'; +var config = require('../intialize/config'); +var accessKey = config.accessKey; +var accessSecret = config.accessSecret; + +var chai = require('chai'); +var expect = chai.expect; + +var amazonMws = require('../../lib/amazon-mws')(accessKey, accessSecret); + +describe('Fulfillment Outbound Shipment', function () { + + before(function () { + expect(accessKey).to.be.a('string'); + expect(accessSecret).to.be.a('string'); + }); + + it('It should get list of Fulfillment Orders using ListAllFulfillmentOrders Action', async function () { + var options = { + 'Version': '2010-10-01', + 'Action': 'ListAllFulfillmentOrders', + 'SellerId': config.SellerId, + 'MWSAuthToken': config.MWSAuthToken, + 'QueryStartDateTime': new Date(13, 12, 2016) + }; + + expect(options.SellerId).to.be.a('string'); + expect(options.MWSAuthToken).to.be.a('string'); + + var response = await amazonMws.fulfillmentOutboundShipment.search(options); + + expect(response).to.be.a('object'); + expect(response).to.have.property('FulfillmentOrders').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'); + }); +}); \ No newline at end of file diff --git a/test/specs/orders.spec.js b/test/specs/orders.spec.js new file mode 100644 index 0000000..5f5710d --- /dev/null +++ b/test/specs/orders.spec.js @@ -0,0 +1,43 @@ +'use strict'; +var config = require('../intialize/config'); +var accessKey = config.accessKey; +var accessSecret = config.accessSecret; + +var chai = require('chai'); +var expect = chai.expect; + +var amazonMws = require('../../lib/amazon-mws')(accessKey, accessSecret); + +describe('Orders', function () { + + before(function () { + expect(accessKey).to.be.a('string'); + expect(accessSecret).to.be.a('string'); + }); + + it('It should get list of orders using ListOrders Action', async function () { + var options = { + 'Version': '2013-09-01', + 'Action': 'ListOrders', + 'SellerId': config.SellerId, + 'MWSAuthToken': config.MWSAuthToken, + 'MarketplaceId.Id.1': config.MarketplaceId, + 'LastUpdatedAfter': new Date(13, 12, 2016) + }; + expect(options.SellerId).to.be.a('string'); + expect(options.MWSAuthToken).to.be.a('string'); + expect(options['MarketplaceId.Id.1']).to.be.a('string'); + + var response = await amazonMws.orders.search(options); + + expect(response).to.be.a('object'); + expect(response).to.have.property('Orders').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'); + }); +}); \ No newline at end of file diff --git a/test/specs/products.spec.js b/test/specs/products.spec.js new file mode 100644 index 0000000..f68ba0e --- /dev/null +++ b/test/specs/products.spec.js @@ -0,0 +1,73 @@ +'use strict'; +var config = require('../intialize/config'); +var accessKey = config.accessKey; +var accessSecret = config.accessSecret; + +var chai = require('chai'); +var expect = chai.expect; + +var amazonMws = require('../../lib/amazon-mws')(accessKey, accessSecret); + +describe('Products', function () { + + before(function () { + expect(accessKey).to.be.a('string'); + expect(accessSecret).to.be.a('string'); + }); + + it('It should get offers using GetLowestPricedOffersForASIN Action', async function () { + var options = { + 'Version': '2011-10-01', + 'Action': 'ListMatchingProducts', + 'SellerId': config.SellerId, + 'MWSAuthToken': config.MWSAuthToken, + 'MarketplaceId': config.MarketplaceId, + 'Query': 'k' + }; + expect(options.SellerId).to.be.a('string'); + expect(options.MWSAuthToken).to.be.a('string'); + expect(options.MarketplaceId).to.be.a('string'); + + var response = await amazonMws.products.searchFor(options); + + expect(response).to.be.a('object'); + expect(response).to.have.property('Products').to.be.a('object'); + expect(response).to.have.property('Products').to.have.property('Product'); + 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'); + }); + + it('It should get offers using GetLowestPricedOffersForASIN Action', async function () { + var options = { + 'Version': '2011-10-01', + 'Action': 'GetLowestPricedOffersForASIN', + 'SellerId': config.SellerId, + 'MWSAuthToken': config.MWSAuthToken, + 'MarketplaceId': config.MarketplaceId, + 'ASIN': config.ASIN, + 'ItemCondition': 'New' + }; + expect(options.SellerId).to.be.a('string'); + expect(options.MWSAuthToken).to.be.a('string'); + expect(options.MarketplaceId).to.be.a('string'); + expect(options.ASIN).to.be.a('string'); + + var response = await amazonMws.products.searchFor(options); + + expect(response).to.be.a('object'); + expect(response).to.have.property('status').to.be.a('string'); + expect(response).to.have.property('Summary').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'); + }); +}); \ No newline at end of file diff --git a/test/specs/reports.spec.js b/test/specs/reports.spec.js new file mode 100644 index 0000000..edb018f --- /dev/null +++ b/test/specs/reports.spec.js @@ -0,0 +1,39 @@ +'use strict'; +var config = require('../intialize/config'); +var accessKey = config.accessKey; +var accessSecret = config.accessSecret; + +var chai = require('chai'); +var expect = chai.expect; + +var amazonMws = require('../../lib/amazon-mws')(accessKey, accessSecret); + +describe('Reports', function () { + + before(function () { + expect(accessKey).to.be.a('string'); + expect(accessSecret).to.be.a('string'); + }); + + it('It should get report list using GetReportList Action', async function () { + var options = { + 'Version': '2009-01-01', + 'Action': 'GetReportList', + 'SellerId': config.SellerId + }; + + expect(options.SellerId).to.be.a('string'); + + var response = await amazonMws.reports.search(options); + + expect(response).to.be.a('object'); + expect(response).to.have.property('ReportInfo').to.be.a('array'); + 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'); + }); +}); \ No newline at end of file diff --git a/test/specs/sellers.spec.js b/test/specs/sellers.spec.js new file mode 100644 index 0000000..6ac6e99 --- /dev/null +++ b/test/specs/sellers.spec.js @@ -0,0 +1,42 @@ +'use strict'; +var config = require('../intialize/config'); +var accessKey = config.accessKey; +var accessSecret = config.accessSecret; + +var chai = require('chai'); +var expect = chai.expect; + +var amazonMws = require('../../lib/amazon-mws')(accessKey, accessSecret); + +describe('Sellers', function () { + + before(function () { + expect(accessKey).to.be.a('string'); + expect(accessSecret).to.be.a('string'); + }); + + it('It should get List of Marketplace Participations using ListMarketplaceParticipations Action', async function () { + var options = { + 'Version': '2011-07-01', + 'Action': 'ListMarketplaceParticipations', + 'SellerId': config.SellerId, + 'MWSAuthToken': config.MWSAuthToken + }; + + expect(options.SellerId).to.be.a('string'); + expect(options.MWSAuthToken).to.be.a('string'); + + var response = await amazonMws.sellers.search(options); + + expect(response).to.be.a('object'); + expect(response).to.have.property('ListParticipations').to.be.a('object'); + expect(response).to.have.property('ListParticipations').to.have.property('Participation'); + 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'); + }); +}); \ No newline at end of file