-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (73 loc) · 2.58 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
const axios = require("axios");
const auth = require("./auth");
class EnvioPack {
constructor(apiKey, secretKey, logger = console, _axios = axios) {
this.apiKey = apiKey;
this.secretKey = secretKey;
this.logger = logger;
this.access_token = null;
this.baseUrl = "https://api.enviopack.com/";
this.transport = _axios.create({
baseURL: this.baseUrl,
headers: {"Accept": "application/json"}
});
this.initializeTransport();
this.client = this.wrapClient(this.transport)
this.cotizar = require("./cotizar")(this.client, this.apiKey, this.secretKey);
this.sucursales = require("./sucursales")(this.client, this.apiKey, this.secretKey);
this.localidades = require("./localidades")(this.client, this.apiKey, this.secretKey);
this.pedidos = require("./pedidos")(this.client, this.apiKey, this.secretKey);
this.envios = require("./envios")(this.client, this.apiKey, this.secretKey);
this.correos = require("./correos")(this.client, this.apiKey, this.secretKey);
this.provincias = require("./provincias")(this.client, this.apiKey, this.secretKey);
}
initializeTransport() {
this.transport.interceptors.request.use((config) => {
if (this.access_token) {
if (!config.params) {
config.params = {};
}
config.params["access_token"] = this.access_token;
}
return config;
});
}
updateAccessToken(access_token) {
this.access_token = access_token;
}
wrapClient(axiosClient) {
const self = this;
async function handleError(error, method, ...args) {
if (error.response && error.response.data && error.response.data.code === 401) {
self.logger.log("Authentication error, we should regenerate the access_token");
const access_token = await auth.create(axiosClient, self.apiKey, self.secretKey);
self.updateAccessToken(access_token);
if (args[1].params) {
args[1].params["access_token"] = access_token;
} else {
args[2].params["access_token"] = access_token;
}
return await method(...args);
}
self.logger.log("Error not handled");
self.logger.log(error);
throw error;
}
function wrapMethod(methodName) {
return async (...args) => {
const method = axiosClient[methodName];
try {
return await method(...args);
} catch (error) {
return await handleError(error, method, ...args);
}
}
}
return {
...axiosClient,
get: wrapMethod("get"),
post: wrapMethod("post"),
}
}
}
module.exports = EnvioPack;