Skip to content

Commit 64d3475

Browse files
authored
create common utils across project (#52)
1 parent 6d0083f commit 64d3475

File tree

5 files changed

+79
-42
lines changed

5 files changed

+79
-42
lines changed

src/buy-api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const makeString = require('make-string');
33
const { makeRequest } = require('./request');
4-
const { encodeURLQuery } = require('./utils');
4+
const { encodeURLQuery } = require('./common-utils');
55

66
const getItem = function (itemId) {
77
if (!itemId) throw new Error('Item Id is required');

src/common-utils/index.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
const { makeRequest } = require('../request');
3+
4+
function base64Encode(encodeData) {
5+
const buff = Buffer.from(encodeData);
6+
return buff.toString('base64');
7+
}
8+
let headers = {};
9+
module.exports = {
10+
setAccessToken: function (token) {
11+
this.options.access_token = token;
12+
},
13+
getAccessToken: function () {
14+
if (!this.options.clientID) throw new Error('Missing Client ID');
15+
if (!this.options.clientSecret) throw new Error('Missing Client Secret or Cert Id');
16+
if (!this.options.body) throw new Error('Missing Body, required Grant type');
17+
const encodedStr = base64Encode(this.options.clientID + ':' + this.options.clientSecret);
18+
const self = this;
19+
const auth = 'Basic ' + encodedStr;
20+
return makeRequest(this.options.baseUrl, '/identity/v1/oauth2/token', 'POST', this.options.body, auth).then((result) => {
21+
const resultJSON = JSON.parse(result);
22+
self.setAccessToken(resultJSON.access_token);
23+
return resultJSON;
24+
});
25+
},
26+
setHeaders(headerObj) {
27+
headers = { ...headers, ...headerObj };
28+
},
29+
getHeaders() {
30+
return headers;
31+
},
32+
upperCase(data) {
33+
if (!isString(data)) data = data.toString();
34+
return data.toUpperCase();
35+
},
36+
37+
// Returns if a value is a string
38+
isString(value) {
39+
return typeof value === 'string' || value instanceof String;
40+
},
41+
42+
// Returns if object is empty or not
43+
isEmptyObj(obj) {
44+
for (let key in obj) {
45+
if (obj.hasOwnProperty(key)) {
46+
return false;
47+
}
48+
}
49+
return true;
50+
},
51+
52+
encodeURLQuery(url) {
53+
return encodeURIComponent(url).replace(/'/g, '%27').replace(/"/g, '%22');
54+
},
55+
56+
};

src/index.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
const { makeRequest, base64Encode } = require('./request');
32
const { getItem,
43
getItemByLegacyId,
54
getItemByItemGroup,
@@ -17,13 +16,29 @@ const { findItemsByKeywords,
1716
findItemsByCategory,
1817
findCompletedItems,
1918
getVersion } = require('./findingApi');
19+
const { setAccessToken,
20+
getAccessToken,
21+
setHeaders,
22+
getHeaders,
23+
isEmptyObj } = require('./common-utils');
2024
const { getSimilarItems, getMostWatchedItems } = require('./merchandising');
2125
const { PROD_BASE_URL, SANDBOX_BASE_URL, BASE_SANDBX_SVC_URL, BASE_SVC_URL } = require('./constants');
2226
const PROD_ENV = 'PROD';
2327
const SANDBOX_ENV = 'SANDBOX';
2428

25-
function Ebay(options) {
2629

30+
/**
31+
* Creates a eBay instance.
32+
*
33+
* @param {Object} options configuration options
34+
* @param {String} options.clientID Client Id/App id
35+
* @param {String} options.env Environment, defaults to PROD
36+
* @param {String} options.headers HTTP request headers
37+
* @constructor
38+
* @public
39+
*/
40+
41+
function Ebay(options) {
2742
if (!options) throw new Error('Options is missing, please provide the input');
2843
if (!options.clientID) throw Error('Client ID is Missing\ncheck documentation to get Client ID http://developer.ebay.com/DevZone/account/');
2944
if (!(this instanceof Ebay)) return new Ebay(options);
@@ -40,24 +55,10 @@ function Ebay(options) {
4055
}
4156

4257
Ebay.prototype = {
43-
44-
setAccessToken: function (token) {
45-
this.options.access_token = token;
46-
},
47-
48-
getAccessToken: function () {
49-
if (!this.options.clientID) throw new Error('Missing Client ID');
50-
if (!this.options.clientSecret) throw new Error('Missing Client Secret or Cert Id');
51-
if (!this.options.body) throw new Error('Missing Body, required Grant type');
52-
const encodedStr = base64Encode(this.options.clientID + ':' + this.options.clientSecret);
53-
const self = this;
54-
const auth = 'Basic ' + encodedStr;
55-
return makeRequest(this.options.baseUrl, '/identity/v1/oauth2/token', 'POST', this.options.body, auth).then((result) => {
56-
const resultJSON = JSON.parse(result);
57-
self.setAccessToken(resultJSON.access_token);
58-
return resultJSON;
59-
});
60-
},
58+
setAccessToken,
59+
getAccessToken,
60+
setHeaders,
61+
getHeaders,
6162
findItemsByKeywords,
6263
findItemsByCategory,
6364
findCompletedItems,

src/taxonomy-api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
const { makeRequest } = require('./request');
3-
const { upperCase } = require('./utils');
3+
const { upperCase } = require('./common-utils');
44

55
/**
66
* @method getDefaultCategoryTreeId {Function}

src/utils.js

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

0 commit comments

Comments
 (0)