From c98f5fdeace06fde7b4d10475529855433dfc972 Mon Sep 17 00:00:00 2001 From: Ajaykumar Date: Sat, 25 Apr 2020 13:44:18 -0700 Subject: [PATCH] Shopping Api : Added getSingleItems and getMultipleitems (#84) * v2.7.7 * add getSingleItem for shipping api * added getMultipleitems shopping api support * version bump and update read me * update version --- README.md | 38 ++++++++++++++++++- demo/shopping.js | 17 +++++++-- package.json | 4 +- src/buildURL.js | 10 ++--- src/common-utils/index.js | 3 ++ src/index.js | 11 ++---- src/shopping.js | 77 +++++++++++++++++++++++++-------------- test/buildURL.test.js | 12 +++--- yarn.lock | 7 ++-- 9 files changed, 120 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 1ff3677..e6f4bea 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ The intent is to simplify the request process by handling the tedious logic. It' * [Installation](#installation) * [Usage](#usage) +* [Examples](#examples) * [Starter Guide](#starter-guide) * [Test](#test) * [Issues](#issues) @@ -21,7 +22,6 @@ The intent is to simplify the request process by handling the tedious logic. It' * [LICENSE](#license) - ## 🚚 Installation ```shell @@ -46,6 +46,42 @@ let ebay = new eBay({ Check out the [Starter Guide](https://pajaydev.github.io/ebay-node-api) documentation with examples to get started. +## Examples + +```javascript +// findItemsBykeyword +ebay.findItemsByKeywords({ + keywords: 'Garmin nuvi 1300 Automotive GPS Receiver', + sortOrder: 'PricePlusShippingLowest', //https://developer.ebay.com/devzone/finding/callref/extra/fndcmpltditms.rqst.srtordr.html + pageNumber: 2, + limit: 10 +}).then((data) => { + console.log(data); +}, (error) => { + console.log(error); +}); + +// Get Single item listing on eBay +ebay.getSingleItem('153265274986').then((data) => { + console.log(data); +}); + +// Search Items by Keyword +ebay.getAccessToken() + .then((data) => { + ebay.searchItems({ + keyword: 'drone', + limit: '3' + }).then((data) => { + console.log(data); + // Data is in format of JSON + // To check the format of Data, Go to this url (https://developer.ebay.com/api- docs/buy/browse/resources/item_summary/methods/search#w4-w1-w4-SearchforItemsbyKeyword-0) + }) + }); +``` + +[More Examples](https://pajaydev.github.io/ebay-node-api) + ## Test All test files are present inside test folder. You can run using diff --git a/demo/shopping.js b/demo/shopping.js index e830ac5..5651bbf 100644 --- a/demo/shopping.js +++ b/demo/shopping.js @@ -14,8 +14,8 @@ ebay.getAllCategories('1234').then((data) => { }); -// Get User Profile -// https://developer.ebay.com/devzone/shopping/docs/callref/GetUserProfile.html +// // Get User Profile +// // https://developer.ebay.com/devzone/shopping/docs/callref/GetUserProfile.html ebay.getUserDetails({ userId: 'ajaykumapratha_0', details: true }).then((data) => { console.log(data); }, (error) => { @@ -33,10 +33,19 @@ ebay.getItemStatus(['153265274986', '153265274986']).then((data) => { // https://developer.ebay.com/devzone/shopping/docs/callref/GetShippingCosts.html ebay.getShippingCosts({ - itemId: '153265274986', destCountryCode: 'US', - destPostalCode: '95128' + itemId: '153265274986', destinationCountryCode: 'US', + destinationPostalCode: '95128' }).then((data) => { console.log(data); }, (error) => { console.log(error); +}); + +//https://developer.ebay.com/devzone/shopping/docs/callref/getsingleitem.html +ebay.getSingleItem('153265274986').then((data) => { + console.log(data); +}); + +ebay.getMultipleItems({ itemId: ['153265274986', '153265274986'] }).then((data) => { + console.log(data); }); \ No newline at end of file diff --git a/package.json b/package.json index fbb3915..c8fe134 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ebay-node-api", - "version": "2.8.1", + "version": "2.8.3", "description": "Ebay node api client", "main": "./src/index.js", "homepage": "https://github.com/pajaydev/ebay-node-api", @@ -32,7 +32,7 @@ "extends": "ajay" }, "dependencies": { - "eslint-config-ajay": "^1.0.2", + "eslint-config-ajay": "^1.0.6", "make-string": "^1.0.3", "oauth-ebay": "^1.0.0" }, diff --git a/src/buildURL.js b/src/buildURL.js index d283f41..7d424b5 100644 --- a/src/buildURL.js +++ b/src/buildURL.js @@ -38,14 +38,12 @@ const buildURL = { * @return {String} url * @private */ - buildShoppingUrl(options) { + buildShoppingUrl(options, operationName) { let baseUrl = `https://${options.baseUrl}/Shopping?`; - baseUrl += 'appid=' + options.clientID; - baseUrl += '&callname=' + options.operationName; - baseUrl += '&version=967&siteid=0&responseencoding=JSON&'; - baseUrl += options.param + '=' + options.name; + baseUrl += `appid=${options.clientID}`; + baseUrl += `&callname=${operationName}`; + baseUrl += `&version=967&siteid=${options.siteId || 0}&responseencoding=JSON`; baseUrl += options.includeSelector ? '&IncludeSelector=' + options.includeSelector : ''; - //base_url += '&GLOBAL-ID=' + oglobalID; return baseUrl; } diff --git a/src/common-utils/index.js b/src/common-utils/index.js index 448554f..27f3be7 100644 --- a/src/common-utils/index.js +++ b/src/common-utils/index.js @@ -24,6 +24,9 @@ module.exports = { return resultJSON; }); }, + setSiteId: function (siteId) { + this.options.siteId = siteId; + }, setHeaders(self, headerObj) { self.headers = Object.assign({}, self.headers, headerObj); }, diff --git a/src/index.js b/src/index.js index 26ab126..bab315e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,6 @@ 'use strict'; const ebayBuyApi = require('./buy-api'); -const { getAllCategories, - getShippingCosts, - getItemStatus, - getUserDetails } = require('./shopping'); +const shoppingApi = require('./shopping'); const { getDefaultCategoryTreeId, getCategoryTree, getCategorySubtree, @@ -47,6 +44,7 @@ function Ebay(options) { this.options = options; setHeaders(this, options.headers); this.options.globalID = options.countryCode || 'EBAY-US'; + this.options.siteId = options.siteId || '0'; } Ebay.prototype = { @@ -61,10 +59,7 @@ Ebay.prototype = { getItemAspectsForCategory, getMostWatchedItems, getSimilarItems, - getAllCategories, - getShippingCosts, - getItemStatus, - getUserDetails, + ...shoppingApi, ...ebayBuyApi, ...ebayFindingApi }; diff --git a/src/shopping.js b/src/shopping.js index 6053595..568b5fa 100644 --- a/src/shopping.js +++ b/src/shopping.js @@ -5,11 +5,8 @@ const urlObject = require('./buildURL'); const makeString = require('make-string'); const getAllCategories = function (categoryID) { - this.options.name = categoryID ? categoryID : -1; - this.options.operationName = 'GetCategoryInfo'; - this.options.param = 'CategoryID'; - const url = urlObject.buildShoppingUrl(this.options); - return getRequest(url).then((data) => { + const requestURL = `${urlObject.buildShoppingUrl(this.options, 'GetCategoryInfo')}&${stringifyUrl({ 'CategoryID': categoryID || -1 })}`; + return getRequest(requestURL).then((data) => { return JSON.parse(data); }, console.error // eslint-disable-line no-console ); @@ -17,25 +14,21 @@ const getAllCategories = function (categoryID) { const getUserDetails = function (input) { if (!input || typeof input !== 'object') throw new Error('Invalid input'); - if (!input.userId) throw new Error('Invalid Input, UserId is required'); - this.options.operationName = 'GetUserProfile'; - this.options.param = 'UserID'; - this.options.name = input.userId; - this.options.includeSelector = input.details ? 'Details' : null; - const url = urlObject.buildShoppingUrl(this.options); - return getRequest(url).then((data) => { + if (!input.userId) throw new Error('invalid_request_error -> userId is null or invalid'); + const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetUserProfile')}&${stringifyUrl(input)}`; + return getRequest(requestUrl).then((data) => { return JSON.parse(data); }, console.error // eslint-disable-line no-console ); }; const getItemStatus = function (itemIds) { - if (!itemIds) throw new Error('User ID is null or invalid'); - this.options.operationName = 'GetItemStatus'; - this.options.param = 'ItemID'; - this.options.name = makeString(itemIds, { braces: 'false', quotes: 'no' }); - const url = urlObject.buildShoppingUrl(this.options); - return getRequest(url).then((data) => { + if (!itemIds) throw new Error('invalid_request_error -> itemIds is null or invalid'); + const paramsObj = { + 'ItemID': makeString(itemIds, { braces: 'false', quotes: 'no' }) + }; + const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetItemStatus')}&${stringifyUrl(paramsObj)}`; + return getRequest(requestUrl).then((data) => { return JSON.parse(data); }, console.error // eslint-disable-line no-console ); @@ -43,24 +36,52 @@ const getItemStatus = function (itemIds) { const getShippingCosts = function (input) { if (!input || typeof input !== 'object') throw new Error('Invalid input'); - if (!input.itemId) throw new Error('Item ID is null or invalid'); - this.options.operationName = 'GetShippingCosts'; - this.options.param = 'ItemID'; - this.options.name = input.itemId; - const countryCodeParam = input.destCountryCode ? '&DestinationCountryCode=' + input.destCountryCode : ''; - const postalCodeParam = input.destPostalCode ? '&DestinationPostalCode=' + input.destPostalCode : ''; - const params = countryCodeParam + postalCodeParam; - let url = urlObject.buildShoppingUrl(this.options); - url = url + params; + if (!input.itemId) throw new Error('invalid_request_error -> Item id is null or invalid'); + const url = `${urlObject.buildShoppingUrl(this.options, 'GetShippingCosts')}&${stringifyUrl(input)} `; return getRequest(url).then((data) => { return JSON.parse(data); }, console.error // eslint-disable-line no-console ); }; +/** + * @method getMultipleItems {Function} + * Retrieves publicly visible details about for one or more listings on eBay. + * @param {Object} options (required) + */ +const getMultipleItems = function (options) { + if (!options || !options.itemId) throw new Error('invalid_request_error -> Item ID is null or invalid'); + const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetMultipleItems')}&${stringifyUrl({ 'itemId': makeString(options.itemId, { braces: 'false', quotes: 'no' }) })}`; + return getRequest(requestUrl).then((data) => { + return JSON.parse(data); + }, console.error // eslint-disable-line no-console + ); +}; + + +/** + * @method getSingleItem {Function} + * Retrieves publicly visible details about one listing on eBay. + * @param {String} itemId (required) + */ +const getSingleItem = function (itemId) { + if (!itemId) throw new Error('invalid_request_error -> Item ID is null or invalid'); + const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetSingleItem')}&${stringifyUrl({ 'ItemID': itemId })} `; + return getRequest(requestUrl).then((data) => { + return JSON.parse(data); + }, console.error // eslint-disable-line no-console + ); +}; + +const stringifyUrl = (obj) => { + return makeString(obj, { braces: 'false', assignment: '=', quotes: 'no', seperator: '&' }); +}; + module.exports = { getAllCategories, getUserDetails, getItemStatus, - getShippingCosts + getShippingCosts, + getSingleItem, + getMultipleItems }; diff --git a/test/buildURL.test.js b/test/buildURL.test.js index eb8dd70..8cf2cdb 100644 --- a/test/buildURL.test.js +++ b/test/buildURL.test.js @@ -17,31 +17,29 @@ describe('test building url methods', () => { globalID: 'EBAY-US', baseSvcUrl: 'svcs.ebay.com' }; - expect(buildURL.buildSearchUrl(options)).to.be.equal(expected_search_url); + expect(buildURL.buildSearchUrl(options, 'findItemsByKeywords')).to.be.equal(expected_search_url); }); it('test Shopping url without selector', () => { - let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON&keywords=iphone'; + let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON'; let options = { name: 'iphone', - operationName: 'demoShoppingName', param: 'keywords', clientID: 'testID', baseUrl: 'open.api.ebay.com' }; - expect(buildURL.buildShoppingUrl(options)).to.be.equal(expected_search_url); + expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expected_search_url); }); it('test Shopping url including selector', () => { - let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON&keywords=iphone&IncludeSelector=true'; + let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON&IncludeSelector=true'; let options = { name: 'iphone', - operationName: 'demoShoppingName', param: 'keywords', clientID: 'testID', includeSelector: true, baseUrl: 'open.api.ebay.com' }; - expect(buildURL.buildShoppingUrl(options)).to.be.equal(expected_search_url); + expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expected_search_url); }); }); diff --git a/yarn.lock b/yarn.lock index ccbb502..e43aac0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1862,9 +1862,10 @@ escape-string-regexp@1.0.5, escape-string-regexp@^1.0.2, escape-string-regexp@^1 version "1.0.5" resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" -eslint-config-ajay@^1.0.2: - version "1.0.3" - resolved "https://registry.yarnpkg.com/eslint-config-ajay/-/eslint-config-ajay-1.0.3.tgz#3d5a2016469ae17eab7e4ce951f520faaeb75075" +eslint-config-ajay@^1.0.6: + version "1.0.6" + resolved "https://registry.yarnpkg.com/eslint-config-ajay/-/eslint-config-ajay-1.0.6.tgz#c741e63ca92b004006e7cdbae9d93185fdf102ba" + integrity sha512-guK8sw0qFnuSbAwFy6SL407gzmAAhKGtwizJ4tsXmE9nVK7NXkeJSQsW8CVBM3RBrM91tgWlUZ3hNU9OHkbmPA== dependencies: eslint "^5.16.0"