Skip to content

Commit

Permalink
refactor!: remove firstPage & firstOffset options (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersDJohnson committed Oct 5, 2020
1 parent c671822 commit b9dc136
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 48 deletions.
20 changes: 2 additions & 18 deletions .README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,15 @@ Defaults to `"offset"`.

If using `params` with `page`, this indicates the page at which to start fetching.

Defaults to value of `firstPage`.
Defaults to `1`.

### `offset`

`number`

If using `params` with `offset` and `limit`, this indicates the offset at which to start fetching.

Defaults to value of `firstOffset`.
Defaults to `0`.

### `limit`

Expand All @@ -266,22 +266,6 @@ If using `params` with `offset` and `limit`, this indicates the size of each pag

Defaults to the size of the first page fetched.

### `firstPage`

`number`

The first page index.

Defaults to `1`.

### `firstOffset`

`number`

The first offset index.

Defaults to `0`.

### `fetchOptions`

`ResponseInit` (`Object`)
Expand Down
38 changes: 9 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export interface FetchPaginateNextOptions<Item> {
url: string;
response?: Response;
pageItems: Item[];
firstPage: number;
firstOffset: number;
page: number;
limit?: number;
offset: number;
Expand Down Expand Up @@ -91,8 +89,6 @@ export interface FetchPaginateOptions<$Body, Item> {
offset?: number;
params?: FetchPaginateParams;
page?: number;
firstOffset?: number;
firstPage?: number;
getFetch?: (args: FetchPaginateGetFetchArgs<$Body, Item>) => typeof fetch;
}

Expand Down Expand Up @@ -141,8 +137,6 @@ const getNextWithParams = <Item>({
params,
pageItems,
url,
firstPage,
firstOffset,
page,
limit,
offset,
Expand All @@ -151,8 +145,6 @@ const getNextWithParams = <Item>({
params?: FetchPaginateParams;
pageItems: Item[];
url: string;
firstPage: number;
firstOffset: number;
page: number;
limit?: number;
offset: number;
Expand All @@ -177,9 +169,7 @@ const getNextWithParams = <Item>({
const offsetParam =
(params.offset === true ? undefined : params.offset) || "offset";

if (nextOffset !== firstOffset) {
parsedUrl.searchParams.set(offsetParam, nextOffset.toString());
}
parsedUrl.searchParams.set(offsetParam, nextOffset.toString());

if (nextLimit) {
parsedUrl.searchParams.set(limitParam, nextLimit.toString());
Expand All @@ -193,15 +183,13 @@ const getNextWithParams = <Item>({
} else {
const nextPage = isFirst ? page : page + 1;

if (nextPage !== firstPage) {
const defaultPageValue = "page";
parsedUrl.searchParams.set(
params === true || params.page === true
? defaultPageValue
: params.page || defaultPageValue,
nextPage.toString()
);
}
const defaultPageValue = "page";
parsedUrl.searchParams.set(
params === true || params.page === true
? defaultPageValue
: params.page || defaultPageValue,
nextPage.toString()
);

return {
url: parsedUrl.toString(),
Expand All @@ -214,8 +202,6 @@ const defaultNext = <Item>({
url,
response,
pageItems,
firstPage,
firstOffset,
page,
limit,
offset,
Expand All @@ -226,8 +212,6 @@ const defaultNext = <Item>({
url,
pageItems,
page,
firstPage,
firstOffset,
limit,
offset,
params,
Expand Down Expand Up @@ -256,8 +240,6 @@ const fetchPaginateIterator = <$Body, Item>(
parse = defaultParse,
next = defaultNext,
until,
firstOffset = 0,
firstPage = 1,
fetchOptions,
getFetch = () => fetch,
} = options;
Expand All @@ -280,7 +262,7 @@ const fetchPaginateIterator = <$Body, Item>(

const url = typeof $url === "string" ? $url : $url.toString();

let { limit, offset = firstOffset, page = firstPage } = options;
let { limit, offset = 0, page = 1 } = options;

let pages: $Body[] = [];
let pageBody: $Body;
Expand Down Expand Up @@ -332,8 +314,6 @@ const fetchPaginateIterator = <$Body, Item>(
url: nextUrl,
response,
pageItems,
firstPage,
firstOffset,
limit,
offset,
page,
Expand Down
4 changes: 3 additions & 1 deletion src/test/nocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ nock(base)

nock(base)
.get("/paged")
.query({ page: "1" })
.times(Infinity)
.reply(200, '{ "list": ["one"] }')
.get("/paged")
Expand All @@ -46,6 +47,7 @@ nock(base)

nock(base)
.get("/paged-empty-end")
.query({ page: "1" })
.times(Infinity)
.reply(200, '{ "list": ["one"] }')
.get("/paged-empty-end")
Expand Down Expand Up @@ -81,7 +83,7 @@ nock(base)
nock(base)
.get("/offset-limit")
.times(Infinity)
.query({ limit: "1" })
.query({ offset: "0", limit: "1" })
.reply(200, '{ "list": ["one"] }')
.get("/offset-limit")
.times(Infinity)
Expand Down

0 comments on commit b9dc136

Please sign in to comment.