-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (119 loc) · 4.52 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const fetch = require('node-fetch');
const Commission = require('./methods/commission');
const Insights = require('./methods/insights');
const Inventory = require('./methods/inventory');
const Invoices = require('./methods/invoices');
const Offers = require('./methods/offers');
const Products = require('./methods/products');
const ProductContent = require('./methods/productContent');
const Orders = require('./methods/orders');
const Shipments = require('./methods/shipments');
const Retailer = require('./methods/retailer');
/**
* Class representing the Bol API V.10.
*/
class Bol {
/**
* Creates a new Bol instance.
* @param {string} APIKEY - The API key.
* @param {string} SECRET - The secret key.
*/
constructor(APIKEY, SECRET, bol_token, expires_in) {
this.API = APIKEY;
this.SECRET = SECRET;
this.bol_token = bol_token;
this.expires_in = expires_in;
}
/**
* Create a header for the Bol API.
* @param {number} [tries=3] - The number of attempts to create the header.
* @returns {Promise<Object>} - A promise that resolves with the header object.
*/
async bolHeader(tries = 3) {
return new Promise(async (resolve, reject) => {
try {
if (!this.bol_token || this.expires_in < new Date().getTime()) await this.bolAccess(tries);
return resolve({
Accept: 'application/vnd.retailer.v10+json',
'Content-Type': 'application/vnd.retailer.v10+json',
Authorization: 'Bearer ' + this.bol_token,
});
} catch (e) {
tries--;
if (tries <= 0) return reject(e);
return setTimeout(() => resolve(this.bolHeader(tries)), 2000);
}
});
}
/**
* Fetch access tokens from the Bol API.
* @param {number} [tries=3] - The number of attempts to fetch the access tokens.
* @returns {Promise<void>} - A promise that resolves when the operation is done.
*/
async bolAccess(tries = 3) {
return new Promise(async (resolve, reject) => {
try {
let resp = await fetch('https://login.bol.com/token?grant_type=client_credentials', {
method: 'POST',
body: {},
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic ' + Buffer.from(this.API + ':' + this.SECRET).toString('base64'),
},
});
resp = await resp.json();
this.bol_token = resp.access_token;
this.expires_in = new Date().getTime() + resp.expires_in * 1000;
return resolve();
} catch (e) {
tries--;
if (tries <= 0) return reject(e);
return setTimeout(() => resolve(this.bolAccess(tries)), 2000);
}
});
}
// Commission
commission = Commission.commission;
commissionList = Commission.commissionList;
// Insights
offerInsights = Insights.offerInsights;
performanceIndicator = Insights.performanceIndicator;
salesForecast = Insights.salesForecast;
searchTerms = Insights.searchTerms;
// Inventory
getLVBInventory = Inventory.getLVBInventory;
// Invoices
getInvoices = Invoices.getInvoices;
getInvoiceById = Invoices.getInvoiceById;
getInvoiceSpecificationById = Invoices.getInvoiceSpecificationById;
// Offers
createNewOffer = Offers.createNewOffer;
exportOffers = Offers.exportOffers;
retrieveUnpublishedOfferReportById = Offers.retrieveUnpublishedOfferReportById;
retrieveOfferByOfferId = Offers.retrieveOfferByOfferId;
updateOffer = Offers.updateOffer;
deleteOffer = Offers.deleteOffer;
updateOfferPrice = Offers.updateOfferPrice;
updateOfferStock = Offers.updateOfferStock;
retrieveOfferReportById = Offers.retrieveOfferReportById;
exportStatus = Offers.exportStatus;
// Orders
orders = Orders.orders;
getOrderByOrderId = Orders.getOrderByOrderId;
// Product Content
getCatalogProductDetailsByEAN = ProductContent.getCatalogProductDetailsByEAN;
getProductList = ProductContent.getProductList;
// Products
getProductIdsByEan = Products.getProductIdsByEan;
getProductAssets = Products.getProductAssets;
// shipments
getShipmentList = Shipments.getShipmentList;
createAShipment = Shipments.createAShipment;
getAListOfInvoiceRequests = Shipments.getAListOfInvoiceRequests;
uploadAnInvoiceForShipmentId = Shipments.uploadAnInvoiceForShipmentId;
getAShipmentByShipmentId = Shipments.getAShipmentByShipmentId;
// retailer
getRetailer = Retailer.getRetailer;
getCurrentRetailer = Retailer.getCurrentRetailer;
}
module.exports = Bol;