-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
114 lines (95 loc) · 4 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
import soap from 'soap';
import format from 'xml-formatter';
import countryData from 'country-data';
async function wsdlRequest(wsdlUrl, method, auth, req) {
return new Promise((resolve, reject) => {
const res = {};
soap.createClient(wsdlUrl, function(err, client) {
if (auth.username === undefined) {
reject('No username specified');
}
if (auth.password === undefined) {
reject('No password specified');
}
const wsSecurity = new soap.WSSecurity(auth.username, auth.password)
client.setSecurity(wsSecurity);
client.on('response', responseXml => {
res.responseXml = responseXml;
});
let clientMethod = client[method];
if (method === 'PickUpRequest') {
clientMethod = clientMethod.euExpressRateBook_providerServices_PickUpRequest_Port.PickUpRequest;
}
clientMethod(req, function(err, response) {
const requestXml = format(client.lastRequest).replace(auth.password, '**********');
if (err) {
err.requestXml = requestXml;
reject(err);
} else {
res.requestXml = requestXml;
res.response = response;
resolve(res);
}
});
});
});
}
const liveUrlPrefix = 'https://wsbexpress.dhl.com/gbl';
const testUrlPrefix = 'https://wsbexpress.dhl.com/sndpt';
const liveExpressRateBookUrl = `${liveUrlPrefix}/expressRateBook?WSDL`;
const testExpressRateBookUrl = `${testUrlPrefix}/expressRateBook?WSDL`;
export async function rateRequest(auth, req) {
const res = await wsdlRequest(liveExpressRateBookUrl, 'getRateRequest', auth, req);
return res;
};
export async function requestPickup(auth, req) {
return await wsdlRequest(`${liveUrlPrefix}/requestPickup?WSDL`, 'PickUpRequest', auth, req);
};
export async function shipmentRequest(auth, req) {
return await wsdlRequest(liveExpressRateBookUrl, 'createShipmentRequest', auth, req);
};
export async function trackingRequest(auth, req) {
return await wsdlRequest(`${liveUrlPrefix}/glDHLExpressTrack?WSDL`, 'trackShipmentRequest', auth, req);
};
export async function testRateRequest(auth, req) {
return await wsdlRequest(testExpressRateBookUrl, 'getRateRequest', auth, req);
};
export async function testRequestPickup(auth, req) {
return await wsdlRequest(`${testUrlPrefix}/requestPickup?WSDL`, 'PickUpRequest', auth, req);
};
export async function testShipmentRequest(auth, req) {
return await wsdlRequest(testExpressRateBookUrl, 'createShipmentRequest', auth, req);
};
export async function testTrackingRequest(auth, req) {
return await wsdlRequest(`${testUrlPrefix}/glDHLExpressTrack?WSDL`, 'trackShipmentRequest', auth, req);
};
export function getIsoDateTime() {
return (new Date).toISOString();
};
export function getIsoDateTimeGmt(dateParam) {
const date = dateParam || new Date();
const offset = date.getTimezoneOffset();
const offsetAbs = Math.abs(offset);
const offsetSign = offset / offsetAbs;
const offsetSignChar = offsetSign < 0 ? '-' : '+';
const offsetHoursAbs = Math.floor(offsetAbs / 60);
const offsetMinutesAbs = offsetAbs % 60;
return `${date.getUTCFullYear()}-\
${(date.getUTCMonth()+1).toString().padStart(2, 0)}-\
${date.getUTCDate().toString().padStart(2,0)}T\
${date.getUTCHours().toString().padStart(2,0)}:\
${date.getUTCMinutes().toString().padStart(2,0)}:\
${date.getUTCSeconds().toString().padStart(2,0)}GMT\
${offsetSignChar}\
${offsetHoursAbs.toString().padStart(2,0)}:\
${offsetMinutesAbs.toString().padStart(2,0)}`;
};
export function getMessageReference() {
return Array(32).fill(0).map(x => Math.random().toString(36).charAt(2)).join('');
};
export function countryToCode(country) {
if (country === 'Vietnam') {
country = 'Viet Nam';
}
return countryData.lookup.countries({name: country})[0].alpha2;
};