Skip to content

Commit f0d7352

Browse files
committed
added search items - browse api
1 parent f41db9f commit f0d7352

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

.npmignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Don't add demo folder #
2+
######################
3+
demo
4+
5+
# OS Generate Files #
6+
######################
7+
.DS_Store
8+
.DS_Store?
9+
10+
/npm-debug.log
11+
/node_modules

demo/searchApi.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const Ebay = require('../src/index');
2+
let ebay = new Ebay({
3+
clientID: "-- Client ID -----",
4+
clientSecret: '-- Client Secret---',
5+
body: {
6+
grant_type: "client_credentials",
7+
scope: 'https://api.ebay.com/oauth/api_scope'
8+
9+
}
10+
});
11+
12+
13+
// Search for Items by Keyword.
14+
ebay.getAccessToken()
15+
.then((data) => {
16+
ebay.searchItems({
17+
keyword: "drone",
18+
limit: "3"
19+
}).then((data) => {
20+
console.log(data);
21+
// Data is in format of JSON
22+
// 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)
23+
})
24+
});
25+
26+
27+
// Search for Items by Category.
28+
ebay.getAccessToken()
29+
.then((data) => {
30+
ebay.searchItems({
31+
categoryId: 2080,
32+
limit: "3"
33+
}).then((data) => {
34+
console.log(data);
35+
// Data is in format of JSON
36+
// 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-SearchforItemsbyCategory-1.
37+
})
38+
});
39+
40+
41+
42+
// Retrieve the Item Aspects by Keyword Search.
43+
ebay.getAccessToken()
44+
.then((data) => {
45+
ebay.searchItems({
46+
keyword: "iphone",
47+
fieldgroups: "ASPECT_REFINEMENTS"
48+
}).then((data) => {
49+
console.log(data);
50+
// Data is in format of JSON
51+
// 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-RetrievetheItemAspectsbyKeywordSearch-3.
52+
})
53+
});
54+
55+
56+
// Return Items with Free Shipping.
57+
// Pass params inside filter object to filter items.
58+
ebay.getAccessToken()
59+
.then((data) => {
60+
ebay.searchItems({
61+
keyword: "drone",
62+
limit: 3,
63+
filter: { maxDeliveryCost: 0 }
64+
}).then((data) => {
65+
console.log(data);
66+
// Data is in format of JSON
67+
// 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-ReturnItemswithFreeShipping-6.
68+
})
69+
});
70+
71+
72+
// Return Items Based on Price and Condition.
73+
ebay.getAccessToken()
74+
.then((data) => {
75+
ebay.searchItems({
76+
keyword: "iphone",
77+
limit: 3,
78+
filter: { price: "[300..800]", priceCurrency: "USD", conditions: "NEW" }
79+
}).then((data) => {
80+
console.log(data);
81+
// Data is in format of JSON
82+
// 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-ReturnItemsBasedonPriceandCondition-7.
83+
})
84+
});

src/buy-api.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ const getItemByLegacyId = function (legacyOptions) {
2222
let param = "legacy_item_id=" + legacyOptions.legacyItemId;
2323
param += legacyOptions.legacyVariationSku ? "&legacy_variation_sku=" + legacyOptions.legacyVariationSku : '';
2424

25-
return new promise
26-
2725
makeRequest('api.ebay.com', `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', this.options.body, auth).then((result) => {
2826
let resultJSON = JSON.parse(result);
2927
//this.setAccessToken(resultJSON);
@@ -47,15 +45,17 @@ const getItemByItemGroup = function (itemGroupId) {
4745
};
4846

4947
const searchItems = function (searchConfig) {
48+
console.log(searchConfig.keyword);
5049
if (!searchConfig) throw new Error("Error --> Missing or invalid input parameter to search");
51-
if (!searchConfig.keyword || !searchConfig.categoryId) throw new Error("Error --> Keyword or category id is required in query param");
50+
if (!searchConfig.keyword && !searchConfig.categoryId) throw new Error("Error --> Keyword or category id is required in query param");
5251
if (!this.options.access_token) throw new Error("Error -->Missing Access token, Generate access token");
5352
if (searchConfig.fieldgroups.length > 0 && !Array.isArray(searchConfig.fieldgroups)) throw new Error("Error -->Field groups should be an array");
5453
const auth = "Bearer " + this.options.access_token;
5554
let queryParam = searchConfig.keyword ? "q=" + searchConfig.keyword + "&" : "";
5655
queryParam = queryParam + searchConfig.categoryId ? "category_ids=" + searchConfig.categoryId + "&" : '';
5756
queryParam = queryParam + searchConfig.limit ? "limit=" + searchConfig.limit + "&" : "";
5857
queryParam = queryParam + searchConfig.fieldgroups.length > 0 ? "fieldgroups=" + searchConfig.fieldgroups.toString() + "&" : "";
58+
queryParam = queryParam + searchConfig.filter != undefined ? "filter=" + JSON.stringify(searchConfig.filter).replace(/[{}]/g, "") : "";
5959
return new Promise((resolve, reject) => {
6060
makeRequest('api.ebay.com', `buy/browse/v1/item_summary/search?${queryparam}`, 'GET', this.options.body, auth).then((result) => {
6161
resolve(JSON.parse(result));

0 commit comments

Comments
 (0)