-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (53 loc) · 1.73 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
'use strict';
const { mapResponse } = require('./lib')
const {
constants: { API_ENDPOINT },
} = require('./utils');
/**
* trackingAPI
* @param {Object.<apiKey, lang>} apiKey: your secret apiKey, lang: fr_FR, en_GB, de_DE, es_ES, it_IT, nl_NL
* @returns
*/
module.exports = ({apiKey, lang}) => {
return {
/**
* getTracking
* @see https://developer.laposte.fr/products/suivi/2/swagger
* @param {String} trackingId
* @returns {Array.<{
* trackingId: String,
* type: String,
* currentStatus: String,
* duration: Number,
* isComplete: Boolean,
* lastUpdated: Date,
* created: Date,
* history: Array
* }>}
*/
getTracking: async (trackingId) => {
let trackingResponse;
try {
const res = await fetch(`${API_ENDPOINT}/${trackingId}?lang=${lang || 'fr_FR'}`, {
headers: {
accept: 'application/json',
'accept-language': 'fr-FR,fr;q=0.6',
'X-Okapi-Key': apiKey,
},
body: null,
method: 'GET'
});
if (res.ok) {
trackingResponse = await res.json();
} else if (res.status === 404) {
console.error('Not found');
} else {
console.error('Bad response');
}
} catch (error) {
console.error('fetch', error);
}
return trackingResponse ? mapResponse(trackingResponse) : null;
}
};
};