Skip to content

Commit d3c2960

Browse files
author
devmandy
committed
Add pagination samples and clean up
1 parent 83790d5 commit d3c2960

5 files changed

Lines changed: 180 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Purpose #
2+
The purpose of this code example is to demonstrate how to use [Node.js](https://nodejs.org/en/)
3+
and the [axios](https://github.com/axios/axios) library to make HTTP requests
4+
to the ShipEngine API. This example demonstrate using pagination in a POST
5+
to the `/labels` endpoint.
6+
7+
# Pre-Requisites #
8+
This example assumes that you have already connected a UPS carrier and have a basic understanding of Javascript.
9+
10+
# Run It! #
11+
1. Install [Node.js](https://nodejs.org/en/)
12+
2. Install [NPM](https://nodejs.org/en/download/package-manager/).
13+
3. Install pre-requisites by running `npm install` from the top-level directory.
14+
3. Replace `'<your api-key>'` with a valid API key for your ShipEngine account.
15+
4. Run the script by running `npm run <sript>`. Example scripts included:
16+
* `loop` - This runs `loop_through_pages.js`. This script demonstrates looping through all the pages.
17+
* `link` - This runs `using_next_link.js`. This script demonstrates using the `next` link to loop through all pages.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const axios = require('axios');
2+
3+
// Set up headers to use with axios HTTP client
4+
const headers = {
5+
'api-key': '<YOUR_API_KEY>',
6+
'Content-Type': 'application/json',
7+
};
8+
9+
// Create axios config
10+
const config = {
11+
headers,
12+
baseURL: 'https://api.shipengine.com',
13+
validateStatus(){
14+
return true;
15+
}
16+
};
17+
18+
async function getLabels() {
19+
20+
// Make the initial request
21+
let getLabelsResponse = await axios.get('/v1/labels', config);
22+
const { pages } = getLabelsResponse.data;
23+
console.log(`There are ${pages} pages of data in this response.`);
24+
25+
26+
// getLabelsResponse will contain the first page of data in the response
27+
// Add code to use that data in your application here
28+
29+
console.log(`This is page ${getLabelsResponse.data.page}`)
30+
31+
// Loop through the paginated data using URL query parameter
32+
for(let i=2; i <= pages; i++) {
33+
34+
// Add url params to existing config object.
35+
// You can also add page_size if you wish to change the default page size, which is 25.
36+
config.params = {
37+
page: i,
38+
};
39+
40+
getLabelsResponse = await axios.get('/v1/labels', config);
41+
// Add code to use this page of data in your application
42+
console.log(`This is page ${getLabelsResponse.data.page}`);
43+
}
44+
}
45+
46+
getLabels().catch( (e) => {
47+
console.log(`There was an error: ${e.message}`);
48+
});

β€Žnode-axios-pagination-examples/package-lock.jsonβ€Ž

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "node-axios-manifest",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "using_next_link.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"loop": "node loop_through_pages.js",
9+
"link": "node using_next_link.js"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC",
14+
"dependencies": {
15+
"axios": "^0.21.0"
16+
}
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const axios = require('axios');
2+
3+
// Set up headers to use with axios HTTP client
4+
const headers = {
5+
'api-key': '<YOUR_API_KEY>',
6+
'Content-Type': 'application/json',
7+
};
8+
9+
// Create axios config
10+
const config = {
11+
headers,
12+
baseURL: 'https://api.shipengine.com',
13+
validateStatus(){
14+
return true;
15+
}
16+
};
17+
18+
async function getLabels() {
19+
20+
// Make the initial request
21+
let getLabelsResponse = await axios.get('/v1/labels', config);
22+
23+
// getLabelsResponse will contain the first page of data in the response
24+
// Add code to use that data in your application
25+
26+
console.log(`This is page ${getLabelsResponse.data.page}`)
27+
28+
// Loop through the paginated data using the next link to get the rest of the data.
29+
// As long as there is a next href, we have more data.
30+
while (getLabelsResponse.data.links.next.href) {
31+
getLabelsResponse = await axios.get(getLabelsResponse.data.links.next.href, config);
32+
// Add code to use this page of data in your application
33+
console.log(`This is page ${getLabelsResponse.data.page}`)
34+
}
35+
}
36+
37+
getLabels().catch( (e) => {
38+
console.log(`There was an error: ${e.message}`);
39+
});

0 commit comments

Comments
 (0)