Skip to content

Commit 246e491

Browse files
committed
added search items method and organized the ebay buy
1 parent 1b53c7e commit 246e491

File tree

2 files changed

+78
-48
lines changed

2 files changed

+78
-48
lines changed

src/buy-api.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const getItem = function (itemId) {
2+
// console.log(this.options);
3+
if (!itemId) throw new Error("Item Id is required");
4+
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
5+
const auth = "Bearer " + this.options.access_token;
6+
const id = encodeURIComponent(itemId);
7+
return makeRequest('api.ebay.com', `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => {
8+
console.log("Success");
9+
let resultJSON = JSON.parse(result);
10+
//this.setAccessToken(resultJSON);
11+
return resultJSON;
12+
});
13+
};
14+
15+
const getItemByLegacyId = function (legacyOptions) {
16+
console.log(legacyOptions);
17+
if (!legacyOptions) throw new Error("Error Required input to get Items By LegacyID");
18+
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
19+
if (!legacyOptions.legacyItemId) throw new Error("Error Legacy Item Id is required");
20+
const auth = "Bearer " + this.options.access_token;
21+
let param = "legacy_item_id=" + legacyOptions.legacyItemId;
22+
param += legacyOptions.legacyVariationSku ? "&legacy_variation_sku=" + legacyOptions.legacyVariationSku : '';
23+
24+
return new promise
25+
26+
makeRequest('api.ebay.com', `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', this.options.body, auth).then((result) => {
27+
let resultJSON = JSON.parse(result);
28+
//this.setAccessToken(resultJSON);
29+
return resultJSON;
30+
}).then((error) => {
31+
console.log(error.errors);
32+
console.log("Error Occurred ===> " + error.errors[0].message);
33+
});
34+
};
35+
36+
const getItemByItemGroup = function (itemGroupId) {
37+
if (typeof itemGroupId == "object") throw new Error("Expecting String or number (Item group id)");
38+
if (!itemGroupId) throw new Error("Error Item Group ID is required");
39+
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
40+
const auth = "Bearer " + this.options.access_token;
41+
return new Promise((resolve, reject) => {
42+
makeRequest('api.ebay.com', `/buy/browse/v1/item/get_items_by_item_group?item_group_id=${itemGroupId}`, 'GET', this.options.body, auth).then((result) => {
43+
resolve(JSON.parse(result));
44+
});
45+
})
46+
};
47+
48+
const searchItems = function (searchConfig) {
49+
if (!searchConfig) throw new Error("Missing or invalid input parameter to search");
50+
if (!searchConfig.keyword || !searchConfig.categoryId) throw new Error("Error Keyword or category id is required in query param");
51+
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
52+
const auth = "Bearer " + this.options.access_token;
53+
let queryParam = searchConfig.keyword ? "q=" + searchConfig.keyword + "&" : "";
54+
queryParam = queryParam + searchConfig.categoryId ? "category_ids=" + searchConfig.categoryId + "&" : '';
55+
queryParam = queryParam + searchConfig.limit ? "limit=" + searchConfig.limit + "&" : "";
56+
queryParam = queryParam + searchConfig.fieldgroups.length > 0 ? "fieldgroups=" + searchConfig.fieldgroups.toString() + "&" : "";
57+
return new Promise((resolve, reject) => {
58+
makeRequest('api.ebay.com', `buy/browse/v1/item_summary/search?${queryparam}`, 'GET', this.options.body, auth).then((result) => {
59+
resolve(JSON.parse(result));
60+
});
61+
})
62+
};
63+
64+
module.exports = {
65+
getItem,
66+
getItemByLegacyId,
67+
getItemByItemGroup,
68+
searchItems
69+
}

src/index.js

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
//let baseURL = "http://svcs.ebay.com/services/search/FindingService/v1";
22
let { getRequest, makeRequest, base64Encode } = require('./request');
3+
let { getItem,
4+
getItemByLegacyId,
5+
getItemByItemGroup,
6+
searchItems } = require('./buy-api');
37
let urlObject = require('./buildURL');
48

59
function Ebay(options) {
@@ -92,53 +96,6 @@ Ebay.prototype = {
9296
console.log(error);
9397
})
9498
},
95-
96-
getItem: function (itemId) {
97-
// console.log(this.options);
98-
if (!itemId) throw new Error("Item Id is required");
99-
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
100-
const auth = "Bearer " + this.options.access_token;
101-
const id = encodeURIComponent(itemId);
102-
return makeRequest('api.ebay.com', `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => {
103-
console.log("Success");
104-
let resultJSON = JSON.parse(result);
105-
//this.setAccessToken(resultJSON);
106-
return resultJSON;
107-
});
108-
},
109-
110-
getItemByLegacyId: function (legacyOptions) {
111-
console.log(legacyOptions);
112-
if (!legacyOptions) throw new Error("Error Required input to get Items By LegacyID");
113-
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
114-
if (!legacyOptions.legacyItemId) throw new Error("Error Legacy Item Id is required");
115-
const auth = "Bearer " + this.options.access_token;
116-
let param = "legacy_item_id=" + legacyOptions.legacyItemId;
117-
param += legacyOptions.legacyVariationSku ? "&legacy_variation_sku=" + legacyOptions.legacyVariationSku : '';
118-
return makeRequest('api.ebay.com', `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', this.options.body, auth).then((result) => {
119-
let resultJSON = JSON.parse(result);
120-
//this.setAccessToken(resultJSON);
121-
return resultJSON;
122-
}).then((error) => {
123-
console.log(error.errors);
124-
console.log("Error Occurred ===> " + error.errors[0].message);
125-
});
126-
},
127-
128-
getItemByItemGroup: function (itemGroupId) {
129-
if (typeof itemGroupId == "object") throw new Error("Expecting String or number (Item group id)");
130-
if (!itemGroupId) throw new Error("Error Item Group ID is required");
131-
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
132-
const auth = "Bearer " + this.options.access_token;
133-
return new Promise((resolve, reject) => {
134-
makeRequest('api.ebay.com', `/buy/browse/v1/item/get_items_by_item_group?item_group_id=${itemGroupId}`, 'GET', this.options.body, auth).then((result) => {
135-
resolve(JSON.parse(result));
136-
});
137-
})
138-
139-
140-
},
141-
14299
setAccessToken: function (token) {
143100

144101
this.options.access_token = token;
@@ -157,7 +114,11 @@ Ebay.prototype = {
157114
self.setAccessToken(resultJSON.access_token);
158115
return resultJSON;
159116
});
160-
}
117+
},
118+
getItem,
119+
getItemByLegacyId,
120+
getItemByItemGroup,
121+
searchItems
161122
};
162123

163124
module.exports = Ebay;

0 commit comments

Comments
 (0)