Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
test.js
28 changes: 12 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const BlueBirdProm = require('bluebird')
const fetch = require('node-fetch')
const querystring = require('querystring')

Expand Down Expand Up @@ -51,19 +52,18 @@ module.exports = class {
* @param {eachPage} eachPage Callback for each page provided by endpoint
* @param {object} queries Object w/ keys & string values of each url query parameter (example: {sku:'10205'}). Page & limit can be passed to control start & page size.
*/
async paginate(endpoint, eachPage, queries={}){
const page = await this.get(endpoint, queries)
const current = this.meta.pagination.current_page
let total = this.meta.pagination.total_pages
await eachPage(page)
async paginate(endpoint, queries={}) {
const _paginate = (accum = [], current) => {
const total = this.meta.pagination.total_pages;
queries.page = current + 1;

if (this.debug) console.log('CURRENT PAGE:', current, 'TOTAL PAGES:', total)
if (current === total) return
for (let current_page = current + 1; current_page <= total; current_page++){
queries.page = current_page
await eachPage(await this.get(endpoint, queries))
if (this.debug) console.log('CURRENT PAGE:', current_page, 'TOTAL PAGES:', total)
if (this.debug) console.log('CURRENT PAGE:', current, 'TOTAL PAGES:', total);

if(current < total) {
return _paginate([...accum, this.get(endpoint, queries)], current + 1)
} else return accum;
}
return _paginate([await this.get(endpoint, queries)], 1);
}

/**
Expand All @@ -74,11 +74,7 @@ module.exports = class {
* @param {object} queries Object w/ keys & string values of each url query parameter (example: {sku:'10205'})
*/
async getAll(endpoint, queries={}){
let result = []
await this.paginate(endpoint, (items)=>{
result = result.concat(items)
}, queries)
return result
return BlueBirdProm.map(this.paginate(endpoint, queries), (prom) => prom, { concurrency: 3 })
}

/**
Expand Down
32 changes: 30 additions & 2 deletions package-lock.json

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

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
"name": "bigcommerce-client",
"version": "1.2.2",
"description": "Simple API client for the BigCommerce Management API",
"keywords":["BigCommerce", "API", "Client"],
"keywords": [
"BigCommerce",
"API",
"Client"
],
"main": "index.js",
"repository": {
"type" : "git",
"url" : "https://github.com/hunterashaw/bigcommerce-client.git"
"type": "git",
"url": "https://github.com/hunterashaw/bigcommerce-client.git"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Hunter Shaw",
"license": "ISC",
"dependencies": {
"bluebird": "^3.7.2",
"node-fetch": "^2.6.1"
}
}