diff --git a/CHANGELOG.md b/CHANGELOG.md index 048bfe24..dc6fd3e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Adds missing properties to `Rate` model - `delivery_date` - `est_delivery_days` +- Fixes `getNextPage` function in User Service ## v7.5.0 (2024-07-24) diff --git a/src/services/user_service.js b/src/services/user_service.js index ffa103d4..40c0b5d0 100644 --- a/src/services/user_service.js +++ b/src/services/user_service.js @@ -1,4 +1,5 @@ import baseService from './base_service'; +import EndOfPaginationError from '../errors/general/end_of_pagination_error'; export default (easypostClient) => /** @@ -145,4 +146,27 @@ export default (easypostClient) => const url = 'users/children'; return this._getNextPage(url, 'children', children, pageSize); } + + static async _getNextPage(url, key, collection, pageSize = null, optionalParams = {}) { + const collectionArray = collection[key]; + if (collectionArray == undefined || collectionArray.length == 0 || !collection.has_more) { + throw new EndOfPaginationError(); + } + + const defaultParams = collection._params ?? collectionArray[0]._params ?? {}; + + const params = { + ...defaultParams, + page_size: defaultParams.page_size ?? pageSize, + after_id: collectionArray[collectionArray.length - 1].id, + ...optionalParams, + }; + + const response = await this._all(url, params); + if (response == undefined || response[key].length == 0) { + throw new EndOfPaginationError(); + } + + return response; + } }; diff --git a/test/cassettes/User-Service_2658581389/retrieves-next-page-of-children_3033480518/recording.har b/test/cassettes/User-Service_2658581389/retrieves-next-page-of-children_3033480518/recording.har index fabed8cb..8b27c435 100644 --- a/test/cassettes/User-Service_2658581389/retrieves-next-page-of-children_3033480518/recording.har +++ b/test/cassettes/User-Service_2658581389/retrieves-next-page-of-children_3033480518/recording.har @@ -32,7 +32,7 @@ "value": "api.easypost.com" } ], - "headersSize": 386, + "headersSize": 385, "httpVersion": "HTTP/1.1", "method": "GET", "queryString": [ @@ -44,12 +44,12 @@ "url": "https://api.easypost.com/v2/users/children?page_size=5" }, "response": { - "bodySize": 84, + "bodySize": 464, "content": { "encoding": "base64", "mimeType": "application/json; charset=utf-8", - "size": 84, - "text": "{\"children\":[],\"has_more\":false}" + "size": 464, + "text": "{\"children\":[{\"id\":\"user_484dd58db70a4f31b4bb862998cf0e04\",\"object\":\"User\",\"parent_id\":\"user_0f6b83e3530b401cb1e8aeaa6a250d4d\",\"name\":\"Test User\",\"phone_number\":\"\",\"verified\":true,\"created_at\":\"2023-05-16T22:01:20Z\"},{\"id\":\"user_14e894c0d541459395f4456e7cf4f175\",\"object\":\"User\",\"parent_id\":\"user_0f6b83e3530b401cb1e8aeaa6a250d4d\",\"name\":\"Test User\",\"phone_number\":\"\",\"verified\":true,\"created_at\":\"2023-09-27T22:05:26Z\"},{\"id\":\"user_f04df3dad13848339a7975d295d6629f\",\"object\":\"User\",\"parent_id\":\"user_0f6b83e3530b401cb1e8aeaa6a250d4d\",\"name\":\"Test User\",\"phone_number\":\"\",\"verified\":true,\"created_at\":\"2023-11-30T19:23:22Z\"},{\"id\":\"user_44d216445b2f4595a123a18084000d94\",\"object\":\"User\",\"parent_id\":\"user_0f6b83e3530b401cb1e8aeaa6a250d4d\",\"name\":\"Test User\",\"phone_number\":\"\",\"verified\":true,\"created_at\":\"2024-07-26T18:36:54Z\"},{\"id\":\"user_f3b7021a2d924bedb080d7caa00d753e\",\"object\":\"User\",\"parent_id\":\"user_0f6b83e3530b401cb1e8aeaa6a250d4d\",\"name\":\"Test User\",\"phone_number\":\"\",\"verified\":true,\"created_at\":\"2024-07-26T18:41:48Z\"}],\"has_more\":true}" }, "cookies": [], "headers": [ @@ -79,7 +79,7 @@ }, { "name": "x-ep-request-uuid", - "value": "6107a17366a910f3e2b87c7e00357c7a" + "value": "ea26a1c566b51e77e786ab3b00130928" }, { "name": "cache-control", @@ -99,7 +99,7 @@ }, { "name": "x-runtime", - "value": "0.026398" + "value": "0.031050" }, { "name": "content-encoding", @@ -111,11 +111,11 @@ }, { "name": "x-node", - "value": "bigweb39nuq" + "value": "bigweb42nuq" }, { "name": "x-version-label", - "value": "easypost-202407300015-6e288fe720-master" + "value": "easypost-202408081823-7bbdcabd1f-master" }, { "name": "x-backend", @@ -140,8 +140,8 @@ "status": 200, "statusText": "OK" }, - "startedDateTime": "2024-07-30T16:12:34.986Z", - "time": 136, + "startedDateTime": "2024-08-08T19:37:27.408Z", + "time": 483, "timings": { "blocked": -1, "connect": -1, @@ -149,7 +149,160 @@ "receive": 0, "send": 0, "ssl": -1, - "wait": 136 + "wait": 483 + } + }, + { + "_id": "255ed9bfd949337ffa214f53bca78117", + "_order": 0, + "cache": {}, + "request": { + "bodySize": 0, + "cookies": [], + "headers": [ + { + "name": "accept-encoding", + "value": "gzip, deflate" + }, + { + "name": "accept", + "value": "application/json" + }, + { + "name": "content-type", + "value": "application/json" + }, + { + "name": "host", + "value": "api.easypost.com" + } + ], + "headersSize": 432, + "httpVersion": "HTTP/1.1", + "method": "GET", + "queryString": [ + { + "name": "page_size", + "value": "5" + }, + { + "name": "after_id", + "value": "user_f3b7021a2d924bedb080d7caa00d753e" + } + ], + "url": "https://api.easypost.com/v2/users/children?page_size=5&after_id=user_f3b7021a2d924bedb080d7caa00d753e" + }, + "response": { + "bodySize": 444, + "content": { + "encoding": "base64", + "mimeType": "application/json; charset=utf-8", + "size": 444, + "text": "{\"children\":[{\"id\":\"user_327a26c77be24e24b165450e7c0de3b9\",\"object\":\"User\",\"parent_id\":\"user_0f6b83e3530b401cb1e8aeaa6a250d4d\",\"name\":\"Test User\",\"phone_number\":\"\",\"verified\":true,\"created_at\":\"2024-07-26T19:08:21Z\"},{\"id\":\"user_acfa6f7268d14e449edd1fe6f4ab64cc\",\"object\":\"User\",\"parent_id\":\"user_0f6b83e3530b401cb1e8aeaa6a250d4d\",\"name\":\"Test User\",\"phone_number\":\"\",\"verified\":true,\"created_at\":\"2024-07-29T17:41:32Z\"},{\"id\":\"user_4b066506d76940aeb28943506eb76941\",\"object\":\"User\",\"parent_id\":\"user_0f6b83e3530b401cb1e8aeaa6a250d4d\",\"name\":\"Test User\",\"phone_number\":\"\",\"verified\":true,\"created_at\":\"2024-07-29T17:46:31Z\"},{\"id\":\"user_48b7c8e5981647a8aee52aa1cb910280\",\"object\":\"User\",\"parent_id\":\"user_0f6b83e3530b401cb1e8aeaa6a250d4d\",\"name\":\"Test User\",\"phone_number\":\"\",\"verified\":true,\"created_at\":\"2024-07-29T18:07:19Z\"},{\"id\":\"user_dc18fb938f184bfd8f345d7fb1469130\",\"object\":\"User\",\"parent_id\":\"user_0f6b83e3530b401cb1e8aeaa6a250d4d\",\"name\":\"Test User\",\"phone_number\":\"\",\"verified\":true,\"created_at\":\"2024-07-29T21:45:16Z\"}],\"has_more\":true}" + }, + "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": "ea26a1cb66b51e78e786ab3c00130985" + }, + { + "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.032038" + }, + { + "name": "content-encoding", + "value": "gzip" + }, + { + "name": "transfer-encoding", + "value": "chunked" + }, + { + "name": "x-node", + "value": "bigweb43nuq" + }, + { + "name": "x-version-label", + "value": "easypost-202408081823-7bbdcabd1f-master" + }, + { + "name": "x-backend", + "value": "easypost" + }, + { + "name": "x-canary", + "value": "direct" + }, + { + "name": "x-proxied", + "value": "intlb3nuq c0f5e722d1, extlb2nuq fa152d4755" + }, + { + "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": "2024-08-08T19:37:27.897Z", + "time": 427, + "timings": { + "blocked": -1, + "connect": -1, + "dns": -1, + "receive": 0, + "send": 0, + "ssl": -1, + "wait": 427 } } ],