Skip to content

Commit 2ed96c3

Browse files
authored
Added findCompleted items api support (#46)
* added findCompletedItems support * fix test and lint
1 parent ec50186 commit 2ed96c3

10 files changed

+202
-95
lines changed

demo/credentials.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
2-
clientId: "-- client id ----",
2+
clientId: "--- Client Id----",
33
clientSecret: "--- client secret---"
44
};

demo/fetchItemsByCategory.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

demo/fetchItemsByKeyword.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

demo/findingApi.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const Ebay = require('../src/index');
2+
const { clientId } = require('./credentials');
3+
4+
let ebay = new Ebay({
5+
clientID: clientId,
6+
});
7+
8+
ebay.findItemsByCategory(10181).then((data) => {
9+
console.log(data);
10+
}, (error) => {
11+
console.log(error);
12+
});
13+
14+
ebay.findItemsByKeywords('iphone').then((data) => {
15+
console.log(data);
16+
}, (error) => {
17+
console.log(error);
18+
});
19+
20+
//https://developer.ebay.com/devzone/finding/callref/findCompletedItems.html
21+
/* This call searches for items whose listings are completed and are no longer available for
22+
sale by category (using categoryId), by keywords (using keywords), or a combination of the two.
23+
Keyword queries search the title and subtitle of the item; they do not search descriptions. */
24+
25+
ebay.findCompletedItems({
26+
keywords: 'Garmin nuvi 1300 Automotive GPS Receiver',
27+
categoryId: '156955',
28+
sortOrder: 'PricePlusShippingLowest', //https://developer.ebay.com/devzone/finding/callref/extra/fndcmpltditms.rqst.srtordr.html
29+
Condition: 3000,
30+
SoldItemsOnly: true,
31+
entriesPerPage: 2
32+
}).then((data) => {
33+
console.log(data);
34+
}, (error) => {
35+
console.log(error);
36+
});
37+
38+
ebay.getVersion().then((data) => {
39+
console.log(data.version);
40+
}, (error) => {
41+
console.log(error);
42+
});

demo/getVersion.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/buildURL.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const buildURL = {
1919
baseUrl += '&OPERATION-NAME=' + options.operationName;
2020
baseUrl += '&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON';
2121
baseUrl += options.param ? '&' + options.param + '=' + options.name : '';
22+
baseUrl += options.additionalParam ? '&' + options.additionalParam : '';
2223
baseUrl += options.sortOrder ? '&sortOrder=' + options.sortOrder : '';
2324
baseUrl += '&outputSelector(0)=SellerInfo';
2425
baseUrl += options.limit ? '&paginationInput.entriesPerPage=' + options.limit : '';

src/findingApi.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict';
2+
3+
const urlObject = require('./buildURL');
4+
const { getRequest } = require('./request');
5+
6+
const findItemsByKeywords = function (keyword) {
7+
if (!keyword) throw new Error('Keyword is missing, Keyword is required');
8+
this.options.name = keyword;
9+
this.options.operationName = 'findItemsByKeywords';
10+
this.options.param = 'keywords';
11+
const url = urlObject.buildSearchUrl(this.options);
12+
return getRequest(url).then((data) => {
13+
return JSON.parse(data).findItemsByKeywordsResponse;
14+
15+
}, console.error
16+
);
17+
};
18+
19+
const findItemsByCategory = function (categoryID) {
20+
if (!categoryID) throw new Error('Category ID is null or invalid');
21+
this.options.name = categoryID;
22+
this.options.operationName = 'findItemsByCategory';
23+
this.options.param = 'categoryId';
24+
const url = urlObject.buildSearchUrl(this.options);
25+
return getRequest(url).then((data) => {
26+
return JSON.parse(data).findItemsByCategoryResponse;
27+
28+
}, console.error
29+
);
30+
};
31+
32+
const findCompletedItems = function (options) {
33+
if (!options) throw new Error('Keyword or category ID are required.');
34+
if (!options.keywords && !options.categoryId) throw new Error('Keyword or category ID are required.');
35+
if (options.keywords) {
36+
options.keywords = encodeURIComponent(options.keywords);
37+
}
38+
this.options.operationName = 'findCompletedItems';
39+
this.options.additionalParam = constructAdditionalParams(options);
40+
const url = urlObject.buildSearchUrl(this.options);
41+
console.log(url);
42+
return getRequest(url).then((data) => {
43+
return JSON.parse(data).findCompletedItemsResponse;
44+
45+
}, console.error
46+
);
47+
};
48+
49+
const getVersion = function () {
50+
this.options.operationName = 'getVersion';
51+
const url = urlObject.buildSearchUrl(this.options);
52+
return getRequest(url).then((data) => {
53+
return JSON.parse(data).getVersionResponse[0];
54+
}, console.error
55+
);
56+
};
57+
58+
const constructAdditionalParams = (options) => {
59+
let params = '';
60+
let count = 0;
61+
for (let key in options) {
62+
if (options.hasOwnProperty(key)) {
63+
if (key === 'entriesPerPage' || key === 'pageNumber') {
64+
params = `${params}paginationInput.${key}=${options[key]}&`;
65+
}
66+
else if (key === 'keywords' || key === 'categoryId' || key === 'sortOrder') {
67+
params = `${params}${key}=${options[key]}&`;
68+
}
69+
else {
70+
params = `${params}itemFilter(${count}).name=${key}&
71+
itemFilter(${count}).value=${options[key]}&`;
72+
count += 1;
73+
}
74+
}
75+
}
76+
// replace extra space
77+
params = params.replace(/\s/g, '');
78+
return params.substring(0, params.length - 1);
79+
};
80+
81+
module.exports = {
82+
findItemsByKeywords,
83+
findItemsByCategory,
84+
findCompletedItems,
85+
constructAdditionalParams,
86+
getVersion
87+
};

src/index.js

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const { getRequest, makeRequest, base64Encode } = require('./request');
2+
const { makeRequest, base64Encode } = require('./request');
33
const { getItem,
44
getItemByLegacyId,
55
getItemByItemGroup,
@@ -13,11 +13,15 @@ const { getDefaultCategoryTreeId,
1313
getCategorySubtree,
1414
getCategorySuggestions,
1515
getItemAspectsForCategory } = require('./taxonomy-api');
16+
const { findItemsByKeywords,
17+
findItemsByCategory,
18+
findCompletedItems,
19+
getVersion } = require('./findingApi');
1620
const { getSimilarItems, getMostWatchedItems } = require('./merchandising');
1721
const { PROD_BASE_URL, SANDBOX_BASE_URL, BASE_SANDBX_SVC_URL, BASE_SVC_URL } = require('./constants');
18-
const urlObject = require('./buildURL');
1922
const PROD_ENV = 'PROD';
2023
const SANDBOX_ENV = 'SANDBOX';
24+
2125
function Ebay(options) {
2226

2327
if (!options) throw new Error('Options is missing, please provide the input');
@@ -37,41 +41,6 @@ function Ebay(options) {
3741

3842
Ebay.prototype = {
3943

40-
findItemsByKeywords: function (keyword) {
41-
if (!keyword) throw new Error('Keyword is missing, Keyword is required');
42-
this.options.name = keyword;
43-
this.options.operationName = 'findItemsByKeywords';
44-
this.options.param = 'keywords';
45-
const url = urlObject.buildSearchUrl(this.options);
46-
return getRequest(url).then((data) => {
47-
return JSON.parse(data).findItemsByKeywordsResponse;
48-
49-
}, console.error
50-
);
51-
},
52-
53-
findItemsByCategory: function (categoryID) {
54-
if (!categoryID) throw new Error('Category ID is null or invalid');
55-
this.options.name = categoryID;
56-
this.options.operationName = 'findItemsByCategory';
57-
this.options.param = 'categoryId';
58-
const url = urlObject.buildSearchUrl(this.options);
59-
return getRequest(url).then((data) => {
60-
return JSON.parse(data).findItemsByCategoryResponse;
61-
62-
}, console.error
63-
);
64-
},
65-
66-
getVersion: function () {
67-
this.options.operationName = 'getVersion';
68-
const url = urlObject.buildSearchUrl(this.options);
69-
return getRequest(url).then((data) => {
70-
return JSON.parse(data).getVersionResponse[0];
71-
}, console.error
72-
);
73-
},
74-
7544
setAccessToken: function (token) {
7645
this.options.access_token = token;
7746
},
@@ -89,6 +58,10 @@ Ebay.prototype = {
8958
return resultJSON;
9059
});
9160
},
61+
findItemsByKeywords,
62+
findItemsByCategory,
63+
findCompletedItems,
64+
getVersion,
9265
getItem,
9366
getItemByLegacyId,
9467
getItemByItemGroup,

test/findItemsByKeyword.test.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,4 @@ describe('Test find items by keyword method', () => {
2727
})
2828
expect(() => { ebay.findItemsByKeywords() }).to.throw('Keyword is missing, Keyword is required');
2929
});
30-
31-
it('test get items from findItemsByKeyword method', () => {
32-
let ebay = new eBay({
33-
clientID: 'ClientId',
34-
})
35-
36-
return ebay.findItemsByKeywords('iphone').then((response) => {
37-
console.log('------' + response);
38-
//done();
39-
})
40-
})
41-
42-
})
30+
});

test/findingApi.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const expect = require('chai').expect;
2+
const should = require('chai').should();
3+
const eBay = require('../src/index');
4+
const { constructAdditionalParams } = require('../src/findingApi');
5+
6+
7+
describe('test ebay finding Api', () => {
8+
9+
describe('test findingApi methods with required params', () => {
10+
it('test findItemsByCategory with required params', () => {
11+
let ebay = new eBay({
12+
clientID: 'ClientId'
13+
});
14+
expect(() => { ebay.findItemsByCategory() }).to.throw('Category ID is null or invalid');
15+
});
16+
17+
it('test findCompletedItemswith required params', () => {
18+
let ebay = new eBay({
19+
clientID: 'ClientId'
20+
});
21+
expect(() => { ebay.findCompletedItems('') }).to.throw('Keyword or category ID are required.');
22+
});
23+
});
24+
25+
describe('test constructAdditionalParams', () => {
26+
it('test constructAdditionalParams with required params', () => {
27+
let expected_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest'
28+
const options = {
29+
keywords: 'iphone',
30+
categoryId: '111',
31+
sortOrder: 'PricePlusShippingLowest'
32+
};
33+
const emptyOptions = {};
34+
expect(constructAdditionalParams(options)).to.be.equal(expected_param);
35+
expect(constructAdditionalParams(emptyOptions)).to.be.equal('');
36+
});
37+
38+
it('test constructAdditionalParams with additional params', () => {
39+
let expected_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=SoldItemsOnly&itemFilter(1).value=true';
40+
let expected_pag_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=SoldItemsOnly&itemFilter(1).value=true&paginationInput.entriesPerPage=2';
41+
const options = {
42+
keywords: 'iphone',
43+
categoryId: '111',
44+
sortOrder: 'PricePlusShippingLowest',
45+
Condition: 3000,
46+
SoldItemsOnly: true
47+
};
48+
const optionsWithPagination = {
49+
keywords: 'iphone',
50+
categoryId: '111',
51+
sortOrder: 'PricePlusShippingLowest',
52+
Condition: 3000,
53+
SoldItemsOnly: true,
54+
entriesPerPage: 2
55+
};
56+
expect(constructAdditionalParams(options)).to.be.equal(expected_param);
57+
expect(constructAdditionalParams(optionsWithPagination)).to.be.equal(expected_pag_param);
58+
});
59+
});
60+
});

0 commit comments

Comments
 (0)