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