Skip to content

Commit c98f5fd

Browse files
authoredApr 25, 2020
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
1 parent 807f719 commit c98f5fd

File tree

9 files changed

+120
-59
lines changed

9 files changed

+120
-59
lines changed
 

‎README.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ The intent is to simplify the request process by handling the tedious logic. It'
1414

1515
* [Installation](#installation)
1616
* [Usage](#usage)
17+
* [Examples](#examples)
1718
* [Starter Guide](#starter-guide)
1819
* [Test](#test)
1920
* [Issues](#issues)
2021
* [Contribution](#contribution)
2122
* [LICENSE](#license)
2223

2324

24-
2525
## 🚚 Installation
2626

2727
```shell
@@ -46,6 +46,42 @@ let ebay = new eBay({
4646

4747
Check out the [Starter Guide](https://pajaydev.github.io/ebay-node-api) documentation with examples to get started.
4848

49+
## Examples
50+
51+
```javascript
52+
// findItemsBykeyword
53+
ebay.findItemsByKeywords({
54+
keywords: 'Garmin nuvi 1300 Automotive GPS Receiver',
55+
sortOrder: 'PricePlusShippingLowest', //https://developer.ebay.com/devzone/finding/callref/extra/fndcmpltditms.rqst.srtordr.html
56+
pageNumber: 2,
57+
limit: 10
58+
}).then((data) => {
59+
console.log(data);
60+
}, (error) => {
61+
console.log(error);
62+
});
63+
64+
// Get Single item listing on eBay
65+
ebay.getSingleItem('153265274986').then((data) => {
66+
console.log(data);
67+
});
68+
69+
// Search Items by Keyword
70+
ebay.getAccessToken()
71+
.then((data) => {
72+
ebay.searchItems({
73+
keyword: 'drone',
74+
limit: '3'
75+
}).then((data) => {
76+
console.log(data);
77+
// Data is in format of JSON
78+
// 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)
79+
})
80+
});
81+
```
82+
83+
[More Examples](https://pajaydev.github.io/ebay-node-api)
84+
4985
## Test
5086
All test files are present inside test folder. You can run using
5187

‎demo/shopping.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ ebay.getAllCategories('1234').then((data) => {
1414
});
1515

1616

17-
// Get User Profile
18-
// https://developer.ebay.com/devzone/shopping/docs/callref/GetUserProfile.html
17+
// // Get User Profile
18+
// // https://developer.ebay.com/devzone/shopping/docs/callref/GetUserProfile.html
1919
ebay.getUserDetails({ userId: 'ajaykumapratha_0', details: true }).then((data) => {
2020
console.log(data);
2121
}, (error) => {
@@ -33,10 +33,19 @@ ebay.getItemStatus(['153265274986', '153265274986']).then((data) => {
3333

3434
// https://developer.ebay.com/devzone/shopping/docs/callref/GetShippingCosts.html
3535
ebay.getShippingCosts({
36-
itemId: '153265274986', destCountryCode: 'US',
37-
destPostalCode: '95128'
36+
itemId: '153265274986', destinationCountryCode: 'US',
37+
destinationPostalCode: '95128'
3838
}).then((data) => {
3939
console.log(data);
4040
}, (error) => {
4141
console.log(error);
42+
});
43+
44+
//https://developer.ebay.com/devzone/shopping/docs/callref/getsingleitem.html
45+
ebay.getSingleItem('153265274986').then((data) => {
46+
console.log(data);
47+
});
48+
49+
ebay.getMultipleItems({ itemId: ['153265274986', '153265274986'] }).then((data) => {
50+
console.log(data);
4251
});

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ebay-node-api",
3-
"version": "2.8.1",
3+
"version": "2.8.3",
44
"description": "Ebay node api client",
55
"main": "./src/index.js",
66
"homepage": "https://github.com/pajaydev/ebay-node-api",
@@ -32,7 +32,7 @@
3232
"extends": "ajay"
3333
},
3434
"dependencies": {
35-
"eslint-config-ajay": "^1.0.2",
35+
"eslint-config-ajay": "^1.0.6",
3636
"make-string": "^1.0.3",
3737
"oauth-ebay": "^1.0.0"
3838
},

‎src/buildURL.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@ const buildURL = {
3838
* @return {String} url
3939
* @private
4040
*/
41-
buildShoppingUrl(options) {
41+
buildShoppingUrl(options, operationName) {
4242
let baseUrl = `https://${options.baseUrl}/Shopping?`;
43-
baseUrl += 'appid=' + options.clientID;
44-
baseUrl += '&callname=' + options.operationName;
45-
baseUrl += '&version=967&siteid=0&responseencoding=JSON&';
46-
baseUrl += options.param + '=' + options.name;
43+
baseUrl += `appid=${options.clientID}`;
44+
baseUrl += `&callname=${operationName}`;
45+
baseUrl += `&version=967&siteid=${options.siteId || 0}&responseencoding=JSON`;
4746
baseUrl += options.includeSelector ? '&IncludeSelector=' + options.includeSelector : '';
48-
//base_url += '&GLOBAL-ID=' + oglobalID;
4947
return baseUrl;
5048
}
5149

‎src/common-utils/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ module.exports = {
2424
return resultJSON;
2525
});
2626
},
27+
setSiteId: function (siteId) {
28+
this.options.siteId = siteId;
29+
},
2730
setHeaders(self, headerObj) {
2831
self.headers = Object.assign({}, self.headers, headerObj);
2932
},

‎src/index.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
'use strict';
22
const ebayBuyApi = require('./buy-api');
3-
const { getAllCategories,
4-
getShippingCosts,
5-
getItemStatus,
6-
getUserDetails } = require('./shopping');
3+
const shoppingApi = require('./shopping');
74
const { getDefaultCategoryTreeId,
85
getCategoryTree,
96
getCategorySubtree,
@@ -47,6 +44,7 @@ function Ebay(options) {
4744
this.options = options;
4845
setHeaders(this, options.headers);
4946
this.options.globalID = options.countryCode || 'EBAY-US';
47+
this.options.siteId = options.siteId || '0';
5048
}
5149

5250
Ebay.prototype = {
@@ -61,10 +59,7 @@ Ebay.prototype = {
6159
getItemAspectsForCategory,
6260
getMostWatchedItems,
6361
getSimilarItems,
64-
getAllCategories,
65-
getShippingCosts,
66-
getItemStatus,
67-
getUserDetails,
62+
...shoppingApi,
6863
...ebayBuyApi,
6964
...ebayFindingApi
7065
};

‎src/shopping.js

+49-28
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,83 @@ const urlObject = require('./buildURL');
55
const makeString = require('make-string');
66

77
const getAllCategories = function (categoryID) {
8-
this.options.name = categoryID ? categoryID : -1;
9-
this.options.operationName = 'GetCategoryInfo';
10-
this.options.param = 'CategoryID';
11-
const url = urlObject.buildShoppingUrl(this.options);
12-
return getRequest(url).then((data) => {
8+
const requestURL = `${urlObject.buildShoppingUrl(this.options, 'GetCategoryInfo')}&${stringifyUrl({ 'CategoryID': categoryID || -1 })}`;
9+
return getRequest(requestURL).then((data) => {
1310
return JSON.parse(data);
1411
}, console.error // eslint-disable-line no-console
1512
);
1613
};
1714

1815
const getUserDetails = function (input) {
1916
if (!input || typeof input !== 'object') throw new Error('Invalid input');
20-
if (!input.userId) throw new Error('Invalid Input, UserId is required');
21-
this.options.operationName = 'GetUserProfile';
22-
this.options.param = 'UserID';
23-
this.options.name = input.userId;
24-
this.options.includeSelector = input.details ? 'Details' : null;
25-
const url = urlObject.buildShoppingUrl(this.options);
26-
return getRequest(url).then((data) => {
17+
if (!input.userId) throw new Error('invalid_request_error -> userId is null or invalid');
18+
const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetUserProfile')}&${stringifyUrl(input)}`;
19+
return getRequest(requestUrl).then((data) => {
2720
return JSON.parse(data);
2821
}, console.error // eslint-disable-line no-console
2922
);
3023
};
3124

3225
const getItemStatus = function (itemIds) {
33-
if (!itemIds) throw new Error('User ID is null or invalid');
34-
this.options.operationName = 'GetItemStatus';
35-
this.options.param = 'ItemID';
36-
this.options.name = makeString(itemIds, { braces: 'false', quotes: 'no' });
37-
const url = urlObject.buildShoppingUrl(this.options);
38-
return getRequest(url).then((data) => {
26+
if (!itemIds) throw new Error('invalid_request_error -> itemIds is null or invalid');
27+
const paramsObj = {
28+
'ItemID': makeString(itemIds, { braces: 'false', quotes: 'no' })
29+
};
30+
const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetItemStatus')}&${stringifyUrl(paramsObj)}`;
31+
return getRequest(requestUrl).then((data) => {
3932
return JSON.parse(data);
4033
}, console.error // eslint-disable-line no-console
4134
);
4235
};
4336

4437
const getShippingCosts = function (input) {
4538
if (!input || typeof input !== 'object') throw new Error('Invalid input');
46-
if (!input.itemId) throw new Error('Item ID is null or invalid');
47-
this.options.operationName = 'GetShippingCosts';
48-
this.options.param = 'ItemID';
49-
this.options.name = input.itemId;
50-
const countryCodeParam = input.destCountryCode ? '&DestinationCountryCode=' + input.destCountryCode : '';
51-
const postalCodeParam = input.destPostalCode ? '&DestinationPostalCode=' + input.destPostalCode : '';
52-
const params = countryCodeParam + postalCodeParam;
53-
let url = urlObject.buildShoppingUrl(this.options);
54-
url = url + params;
39+
if (!input.itemId) throw new Error('invalid_request_error -> Item id is null or invalid');
40+
const url = `${urlObject.buildShoppingUrl(this.options, 'GetShippingCosts')}&${stringifyUrl(input)} `;
5541
return getRequest(url).then((data) => {
5642
return JSON.parse(data);
5743
}, console.error // eslint-disable-line no-console
5844
);
5945
};
6046

47+
/**
48+
* @method getMultipleItems {Function}
49+
* Retrieves publicly visible details about for one or more listings on eBay.
50+
* @param {Object} options (required)
51+
*/
52+
const getMultipleItems = function (options) {
53+
if (!options || !options.itemId) throw new Error('invalid_request_error -> Item ID is null or invalid');
54+
const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetMultipleItems')}&${stringifyUrl({ 'itemId': makeString(options.itemId, { braces: 'false', quotes: 'no' }) })}`;
55+
return getRequest(requestUrl).then((data) => {
56+
return JSON.parse(data);
57+
}, console.error // eslint-disable-line no-console
58+
);
59+
};
60+
61+
62+
/**
63+
* @method getSingleItem {Function}
64+
* Retrieves publicly visible details about one listing on eBay.
65+
* @param {String} itemId (required)
66+
*/
67+
const getSingleItem = function (itemId) {
68+
if (!itemId) throw new Error('invalid_request_error -> Item ID is null or invalid');
69+
const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetSingleItem')}&${stringifyUrl({ 'ItemID': itemId })} `;
70+
return getRequest(requestUrl).then((data) => {
71+
return JSON.parse(data);
72+
}, console.error // eslint-disable-line no-console
73+
);
74+
};
75+
76+
const stringifyUrl = (obj) => {
77+
return makeString(obj, { braces: 'false', assignment: '=', quotes: 'no', seperator: '&' });
78+
};
79+
6180
module.exports = {
6281
getAllCategories,
6382
getUserDetails,
6483
getItemStatus,
65-
getShippingCosts
84+
getShippingCosts,
85+
getSingleItem,
86+
getMultipleItems
6687
};

‎test/buildURL.test.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,29 @@ describe('test building url methods', () => {
1717
globalID: 'EBAY-US',
1818
baseSvcUrl: 'svcs.ebay.com'
1919
};
20-
expect(buildURL.buildSearchUrl(options)).to.be.equal(expected_search_url);
20+
expect(buildURL.buildSearchUrl(options, 'findItemsByKeywords')).to.be.equal(expected_search_url);
2121
});
2222

2323
it('test Shopping url without selector', () => {
24-
let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON&keywords=iphone';
24+
let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON';
2525
let options = {
2626
name: 'iphone',
27-
operationName: 'demoShoppingName',
2827
param: 'keywords',
2928
clientID: 'testID',
3029
baseUrl: 'open.api.ebay.com'
3130
};
32-
expect(buildURL.buildShoppingUrl(options)).to.be.equal(expected_search_url);
31+
expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expected_search_url);
3332
});
3433

3534
it('test Shopping url including selector', () => {
36-
let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON&keywords=iphone&IncludeSelector=true';
35+
let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON&IncludeSelector=true';
3736
let options = {
3837
name: 'iphone',
39-
operationName: 'demoShoppingName',
4038
param: 'keywords',
4139
clientID: 'testID',
4240
includeSelector: true,
4341
baseUrl: 'open.api.ebay.com'
4442
};
45-
expect(buildURL.buildShoppingUrl(options)).to.be.equal(expected_search_url);
43+
expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expected_search_url);
4644
});
4745
});

‎yarn.lock

+4-3
Original file line numberDiff line numberDiff line change
@@ -1862,9 +1862,10 @@ escape-string-regexp@1.0.5, escape-string-regexp@^1.0.2, escape-string-regexp@^1
18621862
version "1.0.5"
18631863
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
18641864

1865-
eslint-config-ajay@^1.0.2:
1866-
version "1.0.3"
1867-
resolved "https://registry.yarnpkg.com/eslint-config-ajay/-/eslint-config-ajay-1.0.3.tgz#3d5a2016469ae17eab7e4ce951f520faaeb75075"
1865+
eslint-config-ajay@^1.0.6:
1866+
version "1.0.6"
1867+
resolved "https://registry.yarnpkg.com/eslint-config-ajay/-/eslint-config-ajay-1.0.6.tgz#c741e63ca92b004006e7cdbae9d93185fdf102ba"
1868+
integrity sha512-guK8sw0qFnuSbAwFy6SL407gzmAAhKGtwizJ4tsXmE9nVK7NXkeJSQsW8CVBM3RBrM91tgWlUZ3hNU9OHkbmPA==
18681869
dependencies:
18691870
eslint "^5.16.0"
18701871

0 commit comments

Comments
 (0)