@heuristical/trackit
Version:
This module allows you to connect to many shipping carriers like UPS and FedEx and download tracking data for your packages in a common schema
180 lines • 7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DhlGmClient = void 0;
/* eslint-disable
constructor-super,
no-constant-condition,
no-eval,
no-return-assign,
no-this-before-super,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS001: Remove Babel/TypeScript constructor workaround
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS103: Rewrite code to no longer use __guard__
* DS206: Consider reworking classes to avoid initClass
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const cheerio_1 = require("cheerio");
const moment_timezone_1 = __importDefault(require("moment-timezone"));
const change_case_1 = require("change-case");
const shipper_1 = require("./shipper");
function __guard__(value, transform) {
return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined;
}
class DhlGmClient extends shipper_1.ShipperClient {
constructor(options) {
super();
this.STATUS_MAP = new Map([
['electronic notification received', shipper_1.STATUS_TYPES.SHIPPING],
['out for delivery', shipper_1.STATUS_TYPES.OUT_FOR_DELIVERY],
['departure origin', shipper_1.STATUS_TYPES.EN_ROUTE],
['transferred', shipper_1.STATUS_TYPES.EN_ROUTE],
['cleared', shipper_1.STATUS_TYPES.EN_ROUTE],
['received', shipper_1.STATUS_TYPES.EN_ROUTE],
['processed', shipper_1.STATUS_TYPES.EN_ROUTE],
['sorted', shipper_1.STATUS_TYPES.EN_ROUTE],
['sorting complete', shipper_1.STATUS_TYPES.EN_ROUTE],
['arrival', shipper_1.STATUS_TYPES.EN_ROUTE],
['tendered', shipper_1.STATUS_TYPES.EN_ROUTE],
['delivered', shipper_1.STATUS_TYPES.DELIVERED]
]);
this.options = options;
}
validateResponse(response, cb) {
try {
response = response.replace(/<br>/gi, ' ');
return cb(null, cheerio_1.load(response, { normalizeWhitespace: true }));
}
catch (error) {
return cb(error);
}
}
extractSummaryField(data, name) {
if (data == null) {
return;
}
const $ = data;
let value;
const regex = new RegExp(name);
$('.card-info > dl').children().each(function (findex, field) {
if (regex.test($(field).text())) {
value = __guard__(__guard__($(field).next(), x1 => x1.text()), x => x.trim());
}
if (value != null) {
return false;
}
});
return value;
}
extractHeaderField(data, name) {
if (data == null) {
return;
}
const $ = data;
let value;
const regex = new RegExp(name);
$('.card > .row').children().each(function (findex, field) {
$(field).children().each((cindex, col) => $(col).find('dt').each(function (dindex, element) {
if (regex.test($(element).text())) {
return value = __guard__(__guard__($(element).next(), x1 => x1.text()), x => x.trim());
}
}));
if (value != null) {
return false;
}
});
return value;
}
getEta(data) {
if (data == null) {
return;
}
const $ = data;
const eta = $('.status-info > .row .est-delivery > p').text();
if (!(eta != null ? eta.length : undefined)) {
return;
}
return moment_timezone_1.default(new Date(`${eta} 23:59:59 +00:00`)).toDate();
}
getService(data) {
return this.extractSummaryField(data, 'Service');
}
getWeight(data) {
return this.extractSummaryField(data, 'Weight');
}
findStatusFromMap(statusText) {
let status = shipper_1.STATUS_TYPES.UNKNOWN;
if (statusText && statusText.length > 0) {
for (const [key, value] of this.STATUS_MAP) {
if (statusText === null || statusText === void 0 ? void 0 : statusText.toLowerCase().includes(key === null || key === void 0 ? void 0 : key.toLowerCase())) {
status = value;
break;
}
}
}
return status;
}
presentStatus(details) {
return this.findStatusFromMap(details);
}
getActivitiesAndStatus(data) {
let status = null;
const activities = [];
if (data == null) {
return { activities, status };
}
const $ = data;
let currentDate = null;
for (const rowData of Array.from($('.timeline').children() || [])) {
const row = $(rowData);
if (row.hasClass('timeline-date')) {
currentDate = row.text();
}
if (row.hasClass('timeline-event')) {
let timestamp;
let currentTime = row.find('.timeline-time').text();
if (currentTime != null ? currentTime.length : undefined) {
if (currentTime != null ? currentTime.length : undefined) {
currentTime = __guard__(currentTime.trim().split(' '), x => x[0]);
}
currentTime = currentTime.replace('AM', ' AM').replace('PM', ' PM');
timestamp = moment_timezone_1.default(new Date(`${currentDate} ${currentTime}`)).toDate();
}
let location = row.find('.timeline-location-responsive').text();
location = location != null ? location.trim() : undefined;
if (location != null ? location.length : undefined) {
location = change_case_1.upperCaseFirst(location);
}
const details = __guard__(row.find('.timeline-description').text(), x1 => x1.trim());
if ((details != null) && (timestamp != null)) {
if (status == null) {
status = this.presentStatus(details);
}
activities.push({ details, location, timestamp });
}
}
}
return { activities, status };
}
getDestination(data) {
return this.extractHeaderField(data, 'To:');
}
requestOptions({ trackingNumber }) {
return {
method: 'GET',
uri: `http://webtrack.dhlglobalmail.com/?trackingnumber=${trackingNumber}`
};
}
}
exports.DhlGmClient = DhlGmClient;
//# sourceMappingURL=dhlgm.js.map