-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-source.js
47 lines (47 loc) · 1.6 KB
/
data-source.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const { RESTDataSource } = require("apollo-datasource-rest");
const countryList = require("./countries");
const categoryList = require("./categories");
var parser = require('xml2js').Parser({explicitArray:false});
class EbayAPI extends RESTDataSource{
constructor(){
super();
this.baseURL = "https://www.ebay.com/rps/feed/v1.1/"
}
rawDeals(data){
const deal = (data) => {
return{
itemId: data.itemId,
title: data.title,
url: data.url,
endsAt: data.endsAt,
currency: data.currency,
image225: data.image225,
price: data.price,
originalPrice: data.originalPrice,
discountPercentage: data.discountPercentage,
quantity: data.quantity,
shippingCost:data.shippingCost,
dealUrl: data.dealUrl
}
}
let cleaned = [];
parser.parseString(data, function (err, result) {
cleaned = Array.isArray(result.eBayDealsAndEventsItems.item) ? result.eBayDealsAndEventsItems.item.map((item) => deal(item)) : []
});
return cleaned
}
async deals(args){
const { country, category, limit, offset } = args
if(category){
return this.rawDeals( await this.get(`${country}?eBayCatId=${category}&limit=${limit}&offset=${offset}`))
}
return this.rawDeals( await this.get(`${country}?limit=${limit}&offset=${offset}`))
}
async countries(){
return countryList;
}
async categories(){
return categoryList;
}
}
module.exports = {EbayAPI}