UNPKG

@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

127 lines 4.77 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LasershipClient = void 0; /* eslint-disable constructor-super, no-constant-condition, no-eval, no-this-before-super, no-unused-vars, standard/no-callback-literal, */ // 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 moment_timezone_1 = __importDefault(require("moment-timezone")); const shipper_1 = require("./shipper"); function __guard__(value, transform) { return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined; } class LasershipClient extends shipper_1.ShipperClient { constructor(options) { super(); this.STATUS_MAP = new Map([ ['Released', shipper_1.STATUS_TYPES.DELIVERED], ['Delivered', shipper_1.STATUS_TYPES.DELIVERED], ['OutForDelivery', shipper_1.STATUS_TYPES.OUT_FOR_DELIVERY], ['Arrived', shipper_1.STATUS_TYPES.EN_ROUTE], ['Received', shipper_1.STATUS_TYPES.EN_ROUTE], ['OrderReceived', shipper_1.STATUS_TYPES.SHIPPING], ['OrderCreated', shipper_1.STATUS_TYPES.SHIPPING] ]); this.options = options; } validateResponse(response, cb) { try { response = JSON.parse(response); if (response.Events == null) { return cb({ error: 'missing events' }); } return cb(null, response); } catch (error) { return cb(error); } } presentAddress(address) { const city = address.City; const stateCode = address.State; const postalCode = address.PostalCode; const countryCode = address.Country; return this.presentLocation({ city, stateCode, countryCode, postalCode }); } presentStatus(eventType) { if (eventType != null) { return this.STATUS_MAP.get(eventType); } } getActivitiesAndStatus(shipment) { const activities = []; let status = null; let rawActivities = shipment != null ? shipment.Events : undefined; rawActivities = Array.from(rawActivities || []); for (const rawActivity of rawActivities) { let timestamp; const location = this.presentAddress(rawActivity); const dateTime = rawActivity != null ? rawActivity.DateTime : undefined; if (dateTime != null) { timestamp = moment_timezone_1.default(`${dateTime}Z`).toDate(); } const details = rawActivity != null ? rawActivity.EventShortText : undefined; if ((details != null) && (timestamp != null)) { const activity = { timestamp, location, details }; activities.push(activity); } if (!status) { status = this.presentStatus(rawActivity != null ? rawActivity.EventType : undefined); } } return { activities, status }; } getEta(shipment) { if ((shipment != null ? shipment.EstimatedDeliveryDate : undefined) == null) { return; } return moment_timezone_1.default(`${shipment.EstimatedDeliveryDate}T00:00:00Z`).toDate(); } getService() { } getWeight(shipment) { if (!__guard__(shipment != null ? shipment.Pieces : undefined, x => x.length)) { return; } const piece = shipment.Pieces[0]; let weight = `${piece.Weight}`; const units = piece.WeightUnit; if (units != null) { weight = `${weight} ${units}`; } return weight; } getDestination(shipment) { const destination = shipment != null ? shipment.Destination : undefined; if (destination == null) { return; } return this.presentAddress(destination); } requestOptions({ trackingNumber }) { return { method: 'GET', uri: `http://www.lasership.com/track/${trackingNumber}/json` }; } } exports.LasershipClient = LasershipClient; //# sourceMappingURL=lasership.js.map