From 424c410110b0027b2fff2671e9b0aa08d3b358c2 Mon Sep 17 00:00:00 2001 From: egg Date: Wed, 2 Jun 2021 12:01:37 +0900 Subject: [PATCH] * add DHL * apply standard format. --- CHANGELOG.md | 3 + README.md | 1 + bin/index.js | 2 +- lib/courier/dhl.js | 145 ++++++++++++++++++++++----------------------- package.json | 5 +- 5 files changed, 80 insertions(+), 76 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59b9f20..14880cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.6.0 +* add DHL. + # 2.5.8 * add an exception logic for internal server error in JNT. diff --git a/README.md b/README.md index 06ac8dc..481b695 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ XIOExpress | @egg- | https://xioexpress.com/ eParcel | @egg- | https://eparcel.kr/ LBC | @egg- | https://www.lbcexpress.com/ J&T (PH) | @egg- | https://www.jtexpress.ph/ +DHL | @carstenschwede | https://www.dhl.com/ ## Installation diff --git a/bin/index.js b/bin/index.js index a28dda9..e907b00 100644 --- a/bin/index.js +++ b/bin/index.js @@ -5,7 +5,7 @@ var tracker = require('../') program .arguments('') - .option('-c, --courier ', 'Courier Namespace', /^(KOREAPOST|ECARGO|FEDEX|PANTOS|RINCOS|AUSPOST|ROYALMAIL|USPS|CJKOREAEXPRESS|POSLAJU|YELLOEXPRESS|EFS|AIRBRIDGE|UPS|TNT|CESCO|XPOST|KERRYTHAI|SICEPAT|XIOEXPRESS|EPARCEL|LBC|JNT)$/i) + .option('-c, --courier ', 'Courier Namespace', /^(KOREAPOST|ECARGO|FEDEX|PANTOS|RINCOS|AUSPOST|ROYALMAIL|USPS|CJKOREAEXPRESS|POSLAJU|YELLOEXPRESS|EFS|AIRBRIDGE|UPS|TNT|CESCO|XPOST|KERRYTHAI|SICEPAT|XIOEXPRESS|EPARCEL|LBC|JNT|DHL)$/i) .option('-k, --apikey ', 'API KEY') .action(function (tracecode) { if (!tracker.COURIER[program.courier]) { diff --git a/lib/courier/dhl.js b/lib/courier/dhl.js index 8ac8e79..740376f 100644 --- a/lib/courier/dhl.js +++ b/lib/courier/dhl.js @@ -5,91 +5,90 @@ var moment = require('moment') var tracker = require('../') -const loadHandler = function(opts) { - return { - trackingInfo: function (number) { - return { - method: 'GET', - url: 'https://api-eu.dhl.com/track/shipments?trackingNumber=' + number, - json: true, - headers: { - 'DHL-API-Key': opts.apikey - } - } - }, - parser: { - trace: function (data) { - var courier = { - code: tracker.COURIER.DHL.CODE, - name: tracker.COURIER.DHL.NAME - } +const loadHandler = function (opts) { + return { + trackingInfo: function (number) { + return { + method: 'GET', + url: 'https://api-eu.dhl.com/track/shipments?trackingNumber=' + number, + json: true, + headers: { + 'DHL-API-Key': opts.apikey + } + } + }, + parser: { + trace: function (data) { + var courier = { + code: tracker.COURIER.DHL.CODE, + name: tracker.COURIER.DHL.NAME + } - var result = { - courier: courier, - number: data.id, - status: tracker.STATUS.PENDING - } + var result = { + courier: courier, + number: data.id, + status: tracker.STATUS.PENDING + } - var checkpoints = [] - for (var i = 0; i < data.events.length; i++) { - var item = data.events[i] - var checkpoint = { - courier: courier, - message: item.description, - status: tracker.STATUS.IN_TRANSIT, - time: moment(item.timestamp).utc().format('YYYY-MM-DDTHH:mmZ') - } + var checkpoints = [] + for (var i = 0; i < data.events.length; i++) { + var item = data.events[i] + var checkpoint = { + courier: courier, + message: item.description, + status: tracker.STATUS.IN_TRANSIT, + time: moment(item.timestamp).utc().format('YYYY-MM-DDTHH:mmZ') + } - let location = item.location && item.location.address && item.location.address.addressLocality; + const location = item.location && item.location.address && item.location.address.addressLocality - if (location) { - checkpoint.location = location; - } + if (location) { + checkpoint.location = location + } - if (item.statusCode === 'delivered') { - checkpoint.status = tracker.STATUS.DELIVERED - } else if (item.statusCode === 'returned') { - checkpoint.status = tracker.STATUS.RETURNED - } - checkpoints.push(checkpoint) - } + if (item.statusCode === 'delivered') { + checkpoint.status = tracker.STATUS.DELIVERED + } else if (item.statusCode === 'returned') { + checkpoint.status = tracker.STATUS.RETURNED + } + checkpoints.push(checkpoint) + } - result.checkpoints = checkpoints - result.status = tracker.normalizeStatus(result.checkpoints) + result.checkpoints = checkpoints + result.status = tracker.normalizeStatus(result.checkpoints) - return result - } - } + return result + } } -}; - + } +} module.exports = function (opts = {}) { - let handler = loadHandler(opts); - return { - trackingInfo: handler.trackingInfo, - trace: function (number, cb) { - if (!opts.apikey) { - return cb(null,tracker.error(tracker.ERROR.REQUIRED_APIKEY)); - } + const handler = loadHandler(opts) + return { + trackingInfo: handler.trackingInfo, + trace: function (number, cb) { + if (!opts.apikey) { + return cb(null, tracker.error(tracker.ERROR.REQUIRED_APIKEY)) + } - var tracking = handler.trackingInfo(number, opts.consumerKey, cb) - request(tracking, function (err, res, body) { - if (err) { - return cb(err) - } + var tracking = handler.trackingInfo(number, opts.consumerKey, cb) + request(tracking, function (err, res, body) { + if (err) { + return cb(err) + } - try { - if (res.statusCode !== 200) { - return cb(tracker.error(res.statusMessage)) - } + try { + if (res.statusCode !== 200) { + return cb(tracker.error(res.statusMessage)) + } - var result = handler.parser.trace(body.shipments[0]) - cb(result ? null : tracker.error(tracker.ERROR.INVALID_NUMBER), result) - } catch (e) { - cb(tracker.error(e.message)) - } - }) + var result = handler.parser.trace(body.shipments[0]) + cb(result ? null : tracker.error(tracker.ERROR.INVALID_NUMBER), result) + } catch (e) { + cb(tracker.error(e.message)) } + }) } -} \ No newline at end of file + } +} diff --git a/package.json b/package.json index a6d0dc7..63d7567 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "delivery-tracker", - "version": "2.5.8", + "version": "2.6.0", "author": { "name": "egg", "email": "i@egg.pe.kr" @@ -51,7 +51,8 @@ "eparcel", "lbc", "j&t", - "jnt" + "jnt", + "dhl" ], "license": "MIT", "bugs": {