dispatch-node-sdk
Advanced tools
Comparing version 3.6.0 to 3.7.0
@@ -11,8 +11,2 @@ 'use strict'; | ||
var _underscore = require('underscore'); | ||
var _underscore2 = _interopRequireDefault(_underscore); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -40,3 +34,3 @@ | ||
return this.client.getCollection(this.endpoint, query, opts); | ||
return this.client.getCollection(this.endpoint, _extends({}, query, { no_total: true }), opts); | ||
} | ||
@@ -48,3 +42,3 @@ }, { | ||
return this.client.getCollectionWithMeta(this.endpoint, query); | ||
return this.client.getCollectionWithMeta(this.endpoint, _extends({}, query, { no_total: true })); | ||
} | ||
@@ -128,19 +122,18 @@ }, { | ||
key: 'getAllRecords', | ||
value: function getAllRecords(query) { | ||
value: function getAllRecords(query, records) { | ||
var _this = this; | ||
var API_PAGE_LIMIT = 200; // enforced by rails | ||
query.limit = query.limit || API_PAGE_LIMIT; | ||
query.offset = query.offset || 0; | ||
return this.getTotalRecords(query).then(function (totalRecords) { | ||
var queryOffsets = getQueryOffsets(totalRecords, query.limit); | ||
return this.get(query).then(function (results) { | ||
var newRecords = (records || []).concat(results || []); | ||
// make a records request for every existing page | ||
var requests = queryOffsets.map(function (queryOffset) { | ||
return _this.get(_extends({}, query, { offset: queryOffset })); | ||
}); | ||
if ((results || []).length === query.limit) { | ||
return _this.getAllRecords(_extends({}, query, { offset: query.offset + query.limit }), newRecords); | ||
} | ||
return Promise.all(requests).then(function (recordPages) { | ||
return _underscore2.default.flatten(recordPages); | ||
}); | ||
return Promise.resolve(newRecords); | ||
}); | ||
@@ -153,21 +146,2 @@ } | ||
/** | ||
* Returns an array of offsets. The offset is the index of the record in the db. | ||
* Conceptually, you can think of it as the start of a page. | ||
* We use these offsets to make a request for every available page. | ||
* | ||
* @param {number} totalRecords - The total number of records available for | ||
* that collection | ||
* @param {number} pageSize - The number of records returned in each request | ||
* | ||
*/ | ||
exports.default = Collection; | ||
function getQueryOffsets(totalRecords, pageSize) { | ||
var offsets = []; | ||
for (var offset = 0; offset < totalRecords; offset += pageSize) { | ||
offsets.push(offset); | ||
} | ||
return offsets; | ||
} | ||
exports.default = Collection; |
@@ -1,2 +0,1 @@ | ||
import _ from 'underscore'; | ||
/** | ||
@@ -15,7 +14,7 @@ * Helper class so we can do things like client.entities.jobs.get(query) | ||
get(query = {}, opts = {}) { | ||
return this.client.getCollection(this.endpoint, query, opts); | ||
return this.client.getCollection(this.endpoint, { ...query, no_total: true }, opts); | ||
} | ||
getWithMeta(query = {}) { | ||
return this.client.getCollectionWithMeta(this.endpoint, query); | ||
return this.client.getCollectionWithMeta(this.endpoint, { ...query, no_total: true }); | ||
} | ||
@@ -81,39 +80,19 @@ | ||
*/ | ||
getAllRecords(query) { | ||
getAllRecords(query, records) { | ||
const API_PAGE_LIMIT = 200; // enforced by rails | ||
query.limit = query.limit || API_PAGE_LIMIT; | ||
query.offset = query.offset || 0; | ||
return this.getTotalRecords(query) | ||
.then((totalRecords) => { | ||
const queryOffsets = getQueryOffsets(totalRecords, query.limit); | ||
return this.get(query) | ||
.then((results) => { | ||
const newRecords = (records || []).concat(results || []); | ||
// make a records request for every existing page | ||
const requests = queryOffsets.map((queryOffset) => { | ||
return this.get({ ...query, offset: queryOffset }); | ||
}); | ||
if ((results || []).length === query.limit) { | ||
return this.getAllRecords({ ...query, offset: query.offset + query.limit }, newRecords); | ||
} | ||
return Promise.all(requests) | ||
.then((recordPages) => { | ||
return _.flatten(recordPages); | ||
}); | ||
return Promise.resolve(newRecords); | ||
}); | ||
} | ||
} | ||
/** | ||
* Returns an array of offsets. The offset is the index of the record in the db. | ||
* Conceptually, you can think of it as the start of a page. | ||
* We use these offsets to make a request for every available page. | ||
* | ||
* @param {number} totalRecords - The total number of records available for | ||
* that collection | ||
* @param {number} pageSize - The number of records returned in each request | ||
* | ||
*/ | ||
function getQueryOffsets(totalRecords, pageSize) { | ||
const offsets = []; | ||
for (let offset = 0; offset < totalRecords; offset += pageSize) { | ||
offsets.push(offset); | ||
} | ||
return offsets; | ||
} |
@@ -19,3 +19,3 @@ import expect from 'expect'; | ||
const scope = nock('https://api.dispatch.me') | ||
.get(`${endpoints.ATTACHMENTS}?filter[file_token_null]=1&filter[entity_type]=Job&filter[entity_id]=123`) | ||
.get(`${endpoints.ATTACHMENTS}?filter[file_token_null]=1&filter[entity_type]=Job&filter[entity_id]=123&no_total=true`) | ||
.reply(200); | ||
@@ -39,3 +39,3 @@ const col = new Collection(client, endpoints.ATTACHMENTS); | ||
const scope = nock('https://api.dispatch.me') | ||
.get(`${endpoints.CUSTOMERS}?limit=1&offset=0`) | ||
.get(`${endpoints.CUSTOMERS}?limit=1&offset=0&no_total=true`) | ||
.reply(200); | ||
@@ -210,15 +210,4 @@ const col = new Collection(client, endpoints.CUSTOMERS); | ||
const mockTotalRecordsRequest = nock('https://api.dispatch.me') | ||
.get(`${endpoints.CUSTOMERS}?limit=0`) | ||
.reply(200, { | ||
customers: [], | ||
meta: { | ||
total: 2, | ||
limit: 0, | ||
offset: 0, | ||
}, | ||
}); | ||
const mockRequestPage1 = nock('https://api.dispatch.me') | ||
.get(`${endpoints.CUSTOMERS}?limit=1&offset=0`) | ||
.get(`${endpoints.CUSTOMERS}?limit=1&offset=0&no_total=true`) | ||
.reply(200, { | ||
@@ -230,3 +219,2 @@ customers: [{ | ||
meta: { | ||
total: 2, | ||
limit: 1, | ||
@@ -238,3 +226,3 @@ offset: 0, | ||
const mockRequestPage2 = nock('https://api.dispatch.me') | ||
.get(`${endpoints.CUSTOMERS}?limit=1&offset=1`) | ||
.get(`${endpoints.CUSTOMERS}?limit=1&offset=1&no_total=true`) | ||
.reply(200, { | ||
@@ -246,3 +234,2 @@ customers: [{ | ||
meta: { | ||
total: 2, | ||
limit: 1, | ||
@@ -253,8 +240,18 @@ offset: 1, | ||
const mockRequestPage3 = nock('https://api.dispatch.me') | ||
.get(`${endpoints.CUSTOMERS}?limit=1&offset=2&no_total=true`) | ||
.reply(200, { | ||
customers: [], | ||
meta: { | ||
limit: 1, | ||
offset: 2, | ||
}, | ||
}); | ||
const col = new Collection(client, endpoints.CUSTOMERS); | ||
col.getAllRecords({ limit: 1 }) | ||
.then((customers) => { | ||
expect(mockTotalRecordsRequest.isDone()).toEqual(true); | ||
expect(mockRequestPage1.isDone()).toEqual(true); | ||
expect(mockRequestPage2.isDone()).toEqual(true); | ||
expect(mockRequestPage3.isDone()).toEqual(true); | ||
expect(customers).toEqual([ | ||
@@ -294,18 +291,7 @@ { | ||
nock('https://api.dispatch.me') | ||
.get(`${endpoints.CUSTOMERS}?limit=0`) | ||
.reply(200, { | ||
customers: [], | ||
meta: { | ||
total: 2, | ||
limit: 0, | ||
offset: 0, | ||
}, | ||
}); | ||
nock('https://api.dispatch.me') | ||
.get(`${endpoints.CUSTOMERS}?limit=1&offset=0`) | ||
.get(`${endpoints.CUSTOMERS}?limit=1&offset=0&no_total=true`) | ||
.reply(500); | ||
nock('https://api.dispatch.me') | ||
.get(`${endpoints.CUSTOMERS}?limit=1&offset=1`) | ||
.get(`${endpoints.CUSTOMERS}?limit=1&offset=1&no_total=true`) | ||
.reply(200, { | ||
@@ -336,3 +322,3 @@ customers: [{ | ||
nock('https://api.dispatch.me') | ||
.get(`${endpoints.CUSTOMERS}?limit=0`) | ||
.get(`${endpoints.CUSTOMERS}?limit=1&offset=0&no_total=true`) | ||
.reply(500); | ||
@@ -355,3 +341,3 @@ | ||
nock('https://api.dispatch.me') | ||
.get(`${endpoints.CUSTOMERS}?limit=0`) | ||
.get(`${endpoints.CUSTOMERS}?limit=0&no_total=true`) | ||
.reply(200, { | ||
@@ -377,3 +363,3 @@ data: {}, | ||
nock('https://api.dispatch.me') | ||
.get(`${endpoints.CUSTOMERS}?limit=0`) | ||
.get(`${endpoints.CUSTOMERS}?limit=0&no_total=true`) | ||
.reply(500); | ||
@@ -380,0 +366,0 @@ |
{ | ||
"name": "dispatch-node-sdk", | ||
"version": "3.6.0", | ||
"version": "3.7.0", | ||
"description": "High- and low-level libraries for interacting with the Dispatch API", | ||
@@ -5,0 +5,0 @@ "main": "dist/lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
413741
10424