-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90f3d80
commit 02a5a7c
Showing
3 changed files
with
63 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import callApi from "./callApi"; | ||
|
||
function* callApiPaginated(request: Request): Generator<any, any, any> { | ||
let { response, bodyAsJson } = yield callApi(request); | ||
const allResults = [...bodyAsJson.results]; | ||
|
||
if (bodyAsJson.next) { | ||
let nextUrl = bodyAsJson.next; | ||
while (nextUrl !== null) { | ||
request = new Request(nextUrl, { | ||
method: 'GET' | ||
}); | ||
({ response, bodyAsJson } = yield callApi(request)); | ||
const status = response.status; | ||
|
||
switch (status) { | ||
case 200: | ||
allResults.push(...bodyAsJson.results); | ||
break; | ||
case 204: | ||
return { | ||
response | ||
}; | ||
break; | ||
|
||
default: | ||
return { | ||
response, | ||
bodyAsJson: { | ||
exception: response.status, | ||
message: response.statusText | ||
} | ||
}; | ||
break; | ||
} | ||
|
||
nextUrl = bodyAsJson.next || null; | ||
} | ||
} | ||
|
||
bodyAsJson.results = allResults; | ||
|
||
return { response, bodyAsJson }; | ||
} | ||
|
||
export default callApiPaginated; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters