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

206 lines 9.66 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UpsClient = void 0; /* eslint-disable constructor-super, no-cond-assign, no-constant-condition, no-eval, 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 xml2js_1 = require("xml2js"); 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 UpsClient extends shipper_1.ShipperClient { /** * Instantiates a Ups Client * @param options licenseNumber, userId, password are required */ constructor(options) { super(); this.STATUS_MAP = new Map([ ['D', shipper_1.STATUS_TYPES.DELIVERED], ['P', shipper_1.STATUS_TYPES.EN_ROUTE], ['M', shipper_1.STATUS_TYPES.SHIPPING] ]); this.options = options; this.parser = new xml2js_1.Parser(); this.builder = new xml2js_1.Builder({ renderOpts: { pretty: false } }); } get licenseNumber() { return this.options.licenseNumber; } ; get userId() { return this.options.userId; } ; get password() { return this.options.password; } ; generateRequest(trk, reference) { if (reference == null) { reference = 'n/a'; } const accessRequest = this.builder.buildObject({ AccessRequest: { AccessLicenseNumber: this.licenseNumber, UserId: this.userId, Password: this.password } }); const trackRequest = this.builder.buildObject({ TrackRequest: { Request: { TransactionReference: { CustomerContext: reference }, RequestAction: 'track', RequestOption: 3 }, TrackingNumber: trk } }); return `${accessRequest}${trackRequest}`; } validateResponse(response, cb) { function handleResponse(xmlErr, trackResult) { let errorMsg, shipment; if ((xmlErr != null) || (trackResult == null)) { return cb(xmlErr); } const responseStatus = __guard__(__guard__(__guard__(trackResult.TrackResponse != null ? trackResult.TrackResponse.Response : undefined, x2 => x2[0]), x1 => x1.ResponseStatusDescription), x => x[0]); if (responseStatus !== 'Success') { const error = __guard__(__guard__(__guard__(__guard__(__guard__(trackResult.TrackResponse != null ? trackResult.TrackResponse.Response : undefined, x7 => x7[0]), x6 => x6.Error), x5 => x5[0]), x4 => x4.ErrorDescription), x3 => x3[0]); errorMsg = error || 'unknown error'; shipment = null; } else { shipment = trackResult.TrackResponse.Shipment != null ? trackResult.TrackResponse.Shipment[0] : undefined; if (shipment == null) { errorMsg = 'missing shipment data'; } } if (errorMsg != null) { return cb(errorMsg); } return cb(null, shipment); } this.parser.reset(); return this.parser.parseString(response, handleResponse); } getEta(shipment) { return this.presentTimestamp(__guard__(__guard__(shipment.Package != null ? shipment.Package[0] : undefined, x1 => x1.RescheduledDeliveryDate), x => x[0]) || (shipment.ScheduledDeliveryDate != null ? shipment.ScheduledDeliveryDate[0] : undefined)); } getService(shipment) { let service; if (service = __guard__(__guard__(shipment.Service != null ? shipment.Service[0] : undefined, x1 => x1.Description), x => x[0])) { return change_case_1.titleCase(service); } } getWeight(shipment) { let weightData; let weight = null; if (weightData = __guard__(__guard__(shipment.Package != null ? shipment.Package[0] : undefined, x1 => x1.PackageWeight), x => x[0])) { let units; weight = weightData.Weight != null ? weightData.Weight[0] : undefined; if ((weight != null) && (units = __guard__(__guard__(weightData.UnitOfMeasurement != null ? weightData.UnitOfMeasurement[0] : undefined, x3 => x3.Code), x2 => x2[0]))) { weight = `${weight} ${units}`; } } return weight; } presentTimestamp(dateString, timeString) { if (dateString == null) { return; } if (timeString == null) { timeString = '00:00:00'; } const formatSpec = 'YYYYMMDD HHmmss ZZ'; return moment_timezone_1.default(`${dateString} ${timeString} +0000`, formatSpec).toDate(); } presentAddress(rawAddress) { if (!rawAddress) { return; } const city = rawAddress.City != null ? rawAddress.City[0] : undefined; const stateCode = rawAddress.StateProvinceCode != null ? rawAddress.StateProvinceCode[0] : undefined; const countryCode = rawAddress.CountryCode != null ? rawAddress.CountryCode[0] : undefined; const postalCode = rawAddress.PostalCode != null ? rawAddress.PostalCode[0] : undefined; return this.presentLocation({ city, stateCode, countryCode, postalCode }); } presentStatus(status) { if (status == null) { return shipper_1.STATUS_TYPES.UNKNOWN; } const statusType = __guard__(__guard__(status.StatusType != null ? status.StatusType[0] : undefined, x1 => x1.Code), x => x[0]); const statusCode = __guard__(__guard__(status.StatusCode != null ? status.StatusCode[0] : undefined, x3 => x3.Code), x2 => x2[0]); if (this.STATUS_MAP.has(statusType)) { return this.STATUS_MAP.get(statusType); } switch (statusType) { case 'I': switch (statusCode) { case 'OF': return shipper_1.STATUS_TYPES.OUT_FOR_DELIVERY; default: return shipper_1.STATUS_TYPES.EN_ROUTE; } case 'X': switch (statusCode) { case 'U2': return shipper_1.STATUS_TYPES.EN_ROUTE; default: return shipper_1.STATUS_TYPES.DELAYED; } default: return shipper_1.STATUS_TYPES.UNKNOWN; } } getDestination(shipment) { return this.presentAddress(__guard__(__guard__(shipment.ShipTo != null ? shipment.ShipTo[0] : undefined, x1 => x1.Address), x => x[0])); } getActivitiesAndStatus(shipment) { const activities = []; let status = null; const rawActivities = __guard__(__guard__(shipment != null ? shipment.Package : undefined, x1 => x1[0]), x => x.Activity); for (const rawActivity of Array.from(rawActivities || [])) { const location = this.presentAddress(__guard__(__guard__(rawActivity.ActivityLocation != null ? rawActivity.ActivityLocation[0] : undefined, x3 => x3.Address), x2 => x2[0])); const timestamp = this.presentTimestamp(rawActivity.Date != null ? rawActivity.Date[0] : undefined, rawActivity.Time != null ? rawActivity.Time[0] : undefined); const lastStatus = rawActivity.Status != null ? rawActivity.Status[0] : undefined; let details = __guard__(__guard__(__guard__(lastStatus != null ? lastStatus.StatusType : undefined, x6 => x6[0]), x5 => x5.Description), x4 => x4[0]); if ((details != null) && (timestamp != null)) { const statusObj = rawActivity.Status; details = change_case_1.upperCaseFirst(change_case_1.lowerCase(details)); const activity = { timestamp, location, details }; if (statusObj != null ? rawActivity.Status[0] : undefined) { activity.statusType = __guard__(__guard__(statusObj.StatusType != null ? statusObj.StatusType[0] : undefined, x8 => x8.Code), x7 => x7[0]); activity.statusCode = __guard__(__guard__(statusObj.StatusCode != null ? statusObj.StatusCode[0] : undefined, x10 => x10.Code), x9 => x9[0]); } activities.push(activity); } if (!status) { status = this.presentStatus(rawActivity.Status != null ? rawActivity.Status[0] : undefined); } } return { activities, status }; } requestOptions({ trackingNumber, reference, test }) { const hostname = test ? 'wwwcie.ups.com' : 'onlinetools.ups.com'; return { method: 'POST', uri: `https://${hostname}/ups.app/xml/Track`, body: this.generateRequest(trackingNumber, reference) }; } } exports.UpsClient = UpsClient; //# sourceMappingURL=ups.js.map