Skip to content

Commit

Permalink
Fix getNextPage in User Service (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchen293 authored Aug 8, 2024
1 parent e417214 commit beee810
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions src/services/user_service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import baseService from './base_service';
import EndOfPaginationError from '../errors/general/end_of_pagination_error';

export default (easypostClient) =>
/**
Expand Down Expand Up @@ -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;
}
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit beee810

Please sign in to comment.