diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d20d5f18..0573c9836 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,13 +3,14 @@ ## Next Release - Adds support for `UspsShipAccount` +- Adds `Tracker.retrieveBatch` function ## v8.2.0 (2025-06-18) - Adds the following functions - - `shipment.createAndBuyLuma` - - `shipment.buyLuma` - - `luma.getPromise` + - `Shipment.createAndBuyLuma` + - `Shipment.buyLuma` + - `Luma.getPromise` ## v8.1.0 (2025-05-29) diff --git a/src/services/tracker_service.js b/src/services/tracker_service.js index 10db3c530..11ce7cbca 100644 --- a/src/services/tracker_service.js +++ b/src/services/tracker_service.js @@ -57,4 +57,21 @@ export default (easypostClient) => return this._retrieve(url); } + + /** + * Retrieve a batch of {@link Tracker trackers}. + * @param {Object} [params] - The parameters to filter the trackers by. + * @returns {Object} - An object containing the list of {@link Tracker trackers}. + */ + static async retrieveBatch(params = {}) { + const url = 'trackers/batch'; + + try { + const response = await easypostClient._post(url, params); + + return this._convertToEasyPostObject(response.body, params); + } catch (e) { + return Promise.reject(e); + } + } }; diff --git a/test/cassettes/Tracker-Service_4233073632/retrieves-a-batch-of-trackers_745559199/recording.har b/test/cassettes/Tracker-Service_4233073632/retrieves-a-batch-of-trackers_745559199/recording.har new file mode 100644 index 000000000..29cc047a8 --- /dev/null +++ b/test/cassettes/Tracker-Service_4233073632/retrieves-a-batch-of-trackers_745559199/recording.har @@ -0,0 +1,320 @@ +{ + "log": { + "_recordingName": "Tracker Service/retrieves a batch of trackers", + "creator": { + "comment": "persister:fs", + "name": "Polly.JS", + "version": "6.0.6" + }, + "entries": [ + { + "_id": "71df7d97a4b5e028a84d5b53b02adc06", + "_order": 0, + "cache": {}, + "request": { + "bodySize": 44, + "cookies": [], + "headers": [ + { + "name": "accept-encoding", + "value": "gzip, deflate" + }, + { + "name": "accept", + "value": "application/json" + }, + { + "name": "content-type", + "value": "application/json" + }, + { + "name": "content-length", + "value": 44 + }, + { + "name": "host", + "value": "api.easypost.com" + } + ], + "headersSize": 389, + "httpVersion": "HTTP/1.1", + "method": "POST", + "postData": { + "mimeType": "application/json", + "params": [], + "text": "{\"tracker\":{\"tracking_code\":\"EZ1000000001\"}}" + }, + "queryString": [], + "url": "https://api.easypost.com/v2/trackers" + }, + "response": { + "bodySize": 894, + "content": { + "encoding": "base64", + "mimeType": "application/json; charset=utf-8", + "size": 894, + "text": "{\"id\":\"trk_e2eecd8746c94c2d8e2f863c7b89f233\",\"object\":\"Tracker\",\"mode\":\"test\",\"tracking_code\":\"EZ1000000001\",\"status\":\"pre_transit\",\"status_detail\":\"status_update\",\"created_at\":\"2025-11-05T23:25:43Z\",\"updated_at\":\"2025-11-05T23:25:43Z\",\"signed_by\":null,\"weight\":null,\"est_delivery_date\":\"2025-11-05T23:25:43Z\",\"shipment_id\":null,\"carrier\":\"USPS\",\"tracking_details\":[{\"object\":\"TrackingDetail\",\"message\":\"Pre-Shipment Info Sent to USPS\",\"description\":\"\",\"status\":\"pre_transit\",\"status_detail\":\"status_update\",\"datetime\":\"2025-10-05T23:25:43Z\",\"source\":\"USPS\",\"carrier_code\":\"\",\"tracking_location\":{\"object\":\"TrackingLocation\",\"city\":null,\"state\":null,\"country\":null,\"zip\":null},\"est_delivery_date\":null},{\"object\":\"TrackingDetail\",\"message\":\"Shipping Label Created\",\"description\":\"\",\"status\":\"pre_transit\",\"status_detail\":\"status_update\",\"datetime\":\"2025-10-06T12:02:43Z\",\"source\":\"USPS\",\"carrier_code\":\"\",\"tracking_location\":{\"object\":\"TrackingLocation\",\"city\":\"HOUSTON\",\"state\":\"TX\",\"country\":null,\"zip\":\"77063\"},\"est_delivery_date\":null}],\"carrier_detail\":{\"object\":\"CarrierDetail\",\"service\":\"First-Class Package Service\",\"container_type\":null,\"est_delivery_date_local\":null,\"est_delivery_time_local\":null,\"origin_location\":\"HOUSTON TX, 77001\",\"origin_tracking_location\":{\"object\":\"TrackingLocation\",\"city\":\"HOUSTON\",\"state\":\"TX\",\"country\":null,\"zip\":\"77063\"},\"destination_location\":\"CHARLESTON SC, 29401\",\"destination_tracking_location\":null,\"guaranteed_delivery_date\":null,\"alternate_identifier\":null,\"initial_delivery_attempt\":null},\"delivery_evidence\":[],\"finalized\":true,\"is_return\":false,\"public_url\":\"https://track.easypost.com/djE6dHJrX2UyZWVjZDg3NDZjOTRjMmQ4ZTJmODYzYzdiODlmMjMz\"}" + }, + "cookies": [], + "headers": [ + { + "name": "x-frame-options", + "value": "SAMEORIGIN" + }, + { + "name": "x-xss-protection", + "value": "1; mode=block" + }, + { + "name": "x-content-type-options", + "value": "nosniff" + }, + { + "name": "x-download-options", + "value": "noopen" + }, + { + "name": "x-permitted-cross-domain-policies", + "value": "none" + }, + { + "name": "referrer-policy", + "value": "strict-origin-when-cross-origin" + }, + { + "name": "x-ep-request-uuid", + "value": "38fab070690bdd9fe7996ea602659c36" + }, + { + "name": "cache-control", + "value": "private, no-cache, no-store" + }, + { + "name": "pragma", + "value": "no-cache" + }, + { + "name": "expires", + "value": "0" + }, + { + "name": "location", + "value": "/api/v2/trackers/trk_e2eecd8746c94c2d8e2f863c7b89f233" + }, + { + "name": "content-type", + "value": "application/json; charset=utf-8" + }, + { + "name": "x-runtime", + "value": "0.061504" + }, + { + "name": "content-encoding", + "value": "gzip" + }, + { + "name": "transfer-encoding", + "value": "chunked" + }, + { + "name": "x-node", + "value": "bigweb41nuq" + }, + { + "name": "x-version-label", + "value": "easypost-202511052124-83d556159a-master" + }, + { + "name": "x-backend", + "value": "easypost" + }, + { + "name": "x-proxied", + "value": "intlb3nuq c0061e0a2e, extlb2nuq cbbd141214" + }, + { + "name": "strict-transport-security", + "value": "max-age=31536000; includeSubDomains; preload" + }, + { + "name": "connection", + "value": "close" + } + ], + "headersSize": 775, + "httpVersion": "HTTP/1.1", + "redirectURL": "/api/v2/trackers/trk_e2eecd8746c94c2d8e2f863c7b89f233", + "status": 200, + "statusText": "OK" + }, + "startedDateTime": "2025-11-05T23:28:31.504Z", + "time": 330, + "timings": { + "blocked": -1, + "connect": -1, + "dns": -1, + "receive": 0, + "send": 0, + "ssl": -1, + "wait": 330 + } + }, + { + "_id": "a65cc6df7238dae94eadc6563e288560", + "_order": 0, + "cache": {}, + "request": { + "bodySize": 35, + "cookies": [], + "headers": [ + { + "name": "accept-encoding", + "value": "gzip, deflate" + }, + { + "name": "accept", + "value": "application/json" + }, + { + "name": "content-type", + "value": "application/json" + }, + { + "name": "content-length", + "value": 35 + }, + { + "name": "host", + "value": "api.easypost.com" + } + ], + "headersSize": 395, + "httpVersion": "HTTP/1.1", + "method": "POST", + "postData": { + "mimeType": "application/json", + "params": [], + "text": "{\"tracking_codes\":[\"EZ1000000001\"]}" + }, + "queryString": [], + "url": "https://api.easypost.com/v2/trackers/batch" + }, + "response": { + "bodySize": 936, + "content": { + "encoding": "base64", + "mimeType": "application/json; charset=utf-8", + "size": 936, + "text": "{\"trackers\":[{\"id\":\"trk_e2eecd8746c94c2d8e2f863c7b89f233\",\"object\":\"Tracker\",\"mode\":\"test\",\"tracking_code\":\"EZ1000000001\",\"status\":\"pre_transit\",\"status_detail\":\"status_update\",\"created_at\":\"2025-11-05T23:25:43Z\",\"updated_at\":\"2025-11-05T23:25:43Z\",\"signed_by\":null,\"weight\":null,\"est_delivery_date\":\"2025-11-05T23:25:43Z\",\"shipment_id\":null,\"carrier\":\"USPS\",\"tracking_details\":[{\"object\":\"TrackingDetail\",\"message\":\"Pre-Shipment Info Sent to USPS\",\"description\":\"\",\"status\":\"pre_transit\",\"status_detail\":\"status_update\",\"datetime\":\"2025-10-05T23:25:43Z\",\"source\":\"USPS\",\"carrier_code\":\"\",\"tracking_location\":{\"object\":\"TrackingLocation\",\"city\":null,\"state\":null,\"country\":null,\"zip\":null},\"est_delivery_date\":null},{\"object\":\"TrackingDetail\",\"message\":\"Shipping Label Created\",\"description\":\"\",\"status\":\"pre_transit\",\"status_detail\":\"status_update\",\"datetime\":\"2025-10-06T12:02:43Z\",\"source\":\"USPS\",\"carrier_code\":\"\",\"tracking_location\":{\"object\":\"TrackingLocation\",\"city\":\"HOUSTON\",\"state\":\"TX\",\"country\":null,\"zip\":\"77063\"},\"est_delivery_date\":null}],\"fees\":[{\"object\":\"Fee\",\"type\":\"TrackerFee\",\"amount\":\"0.02000\",\"charged\":true,\"refunded\":false}],\"carrier_detail\":{\"object\":\"CarrierDetail\",\"service\":\"First-Class Package Service\",\"container_type\":null,\"est_delivery_date_local\":null,\"est_delivery_time_local\":null,\"origin_location\":\"HOUSTON TX, 77001\",\"origin_tracking_location\":{\"object\":\"TrackingLocation\",\"city\":\"HOUSTON\",\"state\":\"TX\",\"country\":null,\"zip\":\"77063\"},\"destination_location\":\"CHARLESTON SC, 29401\",\"destination_tracking_location\":null,\"guaranteed_delivery_date\":null,\"alternate_identifier\":null,\"initial_delivery_attempt\":null},\"delivery_evidence\":[],\"public_url\":\"https://track.easypost.com/djE6dHJrX2UyZWVjZDg3NDZjOTRjMmQ4ZTJmODYzYzdiODlmMjMz\"}],\"has_more\":false}" + }, + "cookies": [], + "headers": [ + { + "name": "x-frame-options", + "value": "SAMEORIGIN" + }, + { + "name": "x-xss-protection", + "value": "1; mode=block" + }, + { + "name": "x-content-type-options", + "value": "nosniff" + }, + { + "name": "x-download-options", + "value": "noopen" + }, + { + "name": "x-permitted-cross-domain-policies", + "value": "none" + }, + { + "name": "referrer-policy", + "value": "strict-origin-when-cross-origin" + }, + { + "name": "x-ep-request-uuid", + "value": "38fab077690bdda0e7996ea702659c6c" + }, + { + "name": "cache-control", + "value": "private, no-cache, no-store" + }, + { + "name": "pragma", + "value": "no-cache" + }, + { + "name": "expires", + "value": "0" + }, + { + "name": "content-type", + "value": "application/json; charset=utf-8" + }, + { + "name": "x-runtime", + "value": "0.055802" + }, + { + "name": "content-encoding", + "value": "gzip" + }, + { + "name": "transfer-encoding", + "value": "chunked" + }, + { + "name": "x-node", + "value": "bigweb32nuq" + }, + { + "name": "x-version-label", + "value": "easypost-202511052124-83d556159a-master" + }, + { + "name": "x-backend", + "value": "easypost" + }, + { + "name": "x-canary", + "value": "direct" + }, + { + "name": "x-proxied", + "value": "intlb6nuq c0061e0a2e, extlb2nuq cbbd141214" + }, + { + "name": "strict-transport-security", + "value": "max-age=31536000; includeSubDomains; preload" + }, + { + "name": "connection", + "value": "close" + } + ], + "headersSize": 728, + "httpVersion": "HTTP/1.1", + "redirectURL": "", + "status": 200, + "statusText": "OK" + }, + "startedDateTime": "2025-11-05T23:28:31.843Z", + "time": 307, + "timings": { + "blocked": -1, + "connect": -1, + "dns": -1, + "receive": 0, + "send": 0, + "ssl": -1, + "wait": 307 + } + } + ], + "pages": [], + "version": "1.2" + } +} diff --git a/test/services/tracker.test.js b/test/services/tracker.test.js index 9fcb21c36..ba4b82d31 100644 --- a/test/services/tracker.test.js +++ b/test/services/tracker.test.js @@ -70,4 +70,18 @@ describe('Tracker Service', function () { } } }); + + it('retrieves a batch of trackers', async function () { + const tracker = await client.Tracker.create({ + tracking_code: 'EZ1000000001', + }); + + const trackers = await client.Tracker.retrieveBatch({ + tracking_codes: [tracker.tracking_code], + }); + + trackers.trackers.forEach((tracker) => { + expect(tracker).to.be.an.instanceOf(Tracker); + }); + }); });