Skip to content

Commit

Permalink
add support for cursor pagination to supported endpoints (#81)
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Vieira <[email protected]>
  • Loading branch information
Bruno Vieira authored Oct 24, 2023
1 parent e6be205 commit 153a885
Show file tree
Hide file tree
Showing 17 changed files with 723 additions and 36 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog],
and this project adheres to [Semantic Versioning].

[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html

## [2.1.2] - 2023-10-05

### Added
- Support for cursor based pagination to `.all()` endpoints.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ ChartMogul.DataSource.destroy(config, dataSourceUuid)

```js
ChartMogul.Customer.create(config, data)
ChartMogul.Customer.all(config, {
page: 2,
per_page: 20
})
ChartMogul.Customer.all(config, { per_page: 20 })
ChartMogul.Customer.destroy(config, customerUuid)

ChartMogul.Customer.contacts(config, customerUuid, { per_page: 10, cursor: 'aabbcc...' })
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chartmogul-node",
"version": "2.1.1",
"version": "2.1.2",
"description": "Official Chartmogul API Node.js Client",
"main": "lib/chartmogul.js",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions test/chartmogul/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('Contact', () => {
customer_external_id: 'external_001',
email: '[email protected]'
}],
cursor: 'MjAyMy0wMy0xM1QxMjowMTozMi44MDYxODYwMDArMDk6MDAmY29uXzAwMDAwMDAwLTAwMDAtMDAwMC0wMDAwLTAwMDAwMDAwMDAwMA==',
cursor: 'MjAyMy0wMy0xM1QxMjowMTozMi44MD==',
has_more: false
/* eslint-enable camelcase */
});
Expand All @@ -80,8 +80,8 @@ describe('Contact', () => {
.then(res => {
expect(res).to.have.property('entries');
expect(res.entries).to.be.instanceof(Array);
expect(res).to.have.property('cursor');
expect(res).to.have.property('has_more');
expect(res.cursor).to.eql('MjAyMy0wMy0xM1QxMjowMTozMi44MD==');
expect(res.has_more).to.eql(false);
});
});

Expand Down
103 changes: 95 additions & 8 deletions test/chartmogul/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,44 @@ describe('Customer', () => {
});
});

it('should get all customers', () => {
it('should get all customers with old pagination', () => {
nock(config.API_BASE)
.get('/v1/customers')
.reply(200, {
/* eslint-disable camelcase */
customers: [{
entries: [{
uuid: 'cus_7e4e5c3d-832c-4fa4-bf77-6fdc8c6e14bc',
external_id: 'cus_0001',
name: 'Adam Smith',
company: '',
email: '[email protected]',
city: 'New York',
state: '',
country: 'US',
zip: '',
data_source_uuid: 'ds_e243129a-12c0-4e29-8f54-07da7905fbd1'
}],
current_page: 1,
total_pages: 1,
page: 1
/* eslint-enable camelcase */
});

return Customer.all(config)
.then(res => {
expect(res).to.have.property('entries');
expect(res.entries).to.be.instanceof(Array);
expect(res.page).to.eql(1);
expect(res.total_pages).to.eql(1);
});
});

it('should get all customers with new pagination', () => {
nock(config.API_BASE)
.get('/v1/customers')
.reply(200, {
/* eslint-disable camelcase */
entries: [{
uuid: 'cus_7e4e5c3d-832c-4fa4-bf77-6fdc8c6e14bc',
external_id: 'cus_0001',
name: 'Adam Smith',
Expand All @@ -58,14 +90,18 @@ describe('Customer', () => {
country: 'US',
zip: '',
data_source_uuid: 'ds_e243129a-12c0-4e29-8f54-07da7905fbd1'
}]
}],
cursor: 'MjAyMy0wMy0xM1QxMjowMTozMi44MD==',
has_more: false
/* eslint-enable camelcase */
});

return Customer.all(config)
.then(res => {
expect(res).to.have.property('customers');
expect(res.customers).to.be.instanceof(Array);
expect(res).to.have.property('entries');
expect(res.entries).to.be.instanceof(Array);
expect(res.cursor).to.eql('MjAyMy0wMy0xM1QxMjowMTozMi44MD==');
expect(res.has_more).to.eql(false);
});
});

Expand Down Expand Up @@ -145,7 +181,7 @@ describe('Customer', () => {
customer_external_id: 'external_001',
email: '[email protected]'
}],
cursor: 'MjAyMy0wMy0xM1QxMjowMTozMi44MDYxODYwMDArMDk6MDAmY29uXzAwMDAwMDAwLTAwMDAtMDAwMC0wMDAwLTAwMDAwMDAwMDAwMA==',
cursor: 'MjAyMy0wMy0xM1QxMjowMTozMi44MD==',
has_more: false
/* eslint-enable camelcase */
});
Expand All @@ -154,6 +190,8 @@ describe('Customer', () => {
.then(res => {
expect(res).to.have.property('entries');
expect(res.entries).to.be.instanceof(Array);
expect(res.cursor).to.eql('MjAyMy0wMy0xM1QxMjowMTozMi44MD==');
expect(res.has_more).to.eql(false);
});
});
});
Expand Down Expand Up @@ -201,7 +239,7 @@ describe('Enrichment#Customer', () => {
});
});

it('should get all customers', () => {
it('should get all customers with old pagination', () => {
nock(config.API_BASE)
.get('/v1/customers')
.reply(200, {
Expand All @@ -217,10 +255,31 @@ describe('Enrichment#Customer', () => {
.then(res => {
expect(res).to.have.property('entries');
expect(res.entries).to.be.instanceof(Array);
expect(res.page).to.eql(1);
});
});

it('should search for a customer', () => {
it('should get all customers with new pagination', () => {
nock(config.API_BASE)
.get('/v1/customers')
.reply(200, {
/* eslint-disable camelcase */
entries: [],
has_more: false,
cursor: null
/* eslint-enable camelcase */
});

return Customer.all(config)
.then(res => {
expect(res).to.have.property('entries');
expect(res.entries).to.be.instanceof(Array);
expect(res.cursor).to.eql(null);
expect(res.has_more).to.eql(false);
});
});

it('should search for a customer with old pagination', () => {
const email = '[email protected]';

nock(config.API_BASE)
Expand All @@ -243,6 +302,34 @@ describe('Enrichment#Customer', () => {
.then(res => {
expect(res).to.have.property('entries');
expect(res.entries).to.be.instanceof(Array);
expect(res.page).to.eql(1);
});
});

it('should search for a customer with new pagination', () => {
const email = '[email protected]';

nock(config.API_BASE)
.get('/v1/customers/search')
.query({
email
})
.reply(200, {
/* eslint-disable camelcase */
entries: [],
has_more: false,
cursor: 'JjI3MjI4NTM=='
/* eslint-enable camelcase */
});

return Customer.search(config, {
email
})
.then(res => {
expect(res).to.have.property('entries');
expect(res.entries).to.be.instanceof(Array);
expect(res.cursor).to.eql('JjI3MjI4NTM==');
expect(res.has_more).to.eql(false);
});
});

Expand Down
53 changes: 51 additions & 2 deletions test/chartmogul/enrichment/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('DeprecatedEnrichment#Customer', () => {
});
});

it('should get all customers', () => {
it('should get all customers with old pagination', () => {
nock(config.API_BASE)
.get('/v1/customers')
.reply(200, {
Expand All @@ -64,10 +64,31 @@ describe('DeprecatedEnrichment#Customer', () => {
.then(res => {
expect(res).to.have.property('entries');
expect(res.entries).to.be.instanceof(Array);
expect(res.page).to.eql(1);
});
});

it('should search for a customer', () => {
it('should get all customers with new pagination', () => {
nock(config.API_BASE)
.get('/v1/customers')
.reply(200, {
/* eslint-disable camelcase */
entries: [],
has_more: false,
cursor: 'cursor=='
/* eslint-enable camelcase */
});

return Customer.all(config)
.then(res => {
expect(res).to.have.property('entries');
expect(res.entries).to.be.instanceof(Array);
expect(res.cursor).to.eql('cursor==');
expect(res.has_more).to.eql(false);
});
});

it('should search for a customer with old pagination', () => {
const email = '[email protected]';

nock(config.API_BASE)
Expand All @@ -90,6 +111,34 @@ describe('DeprecatedEnrichment#Customer', () => {
.then(res => {
expect(res).to.have.property('entries');
expect(res.entries).to.be.instanceof(Array);
expect(res.page).to.eql(1);
});
});

it('should search for a customer with new pagination', () => {
const email = '[email protected]';

nock(config.API_BASE)
.get('/v1/customers/search')
.query({
email
})
.reply(200, {
/* eslint-disable camelcase */
entries: [],
has_more: false,
cursor: 'cursor=='
/* eslint-enable camelcase */
});

return Customer.search(config, {
email
})
.then(res => {
expect(res).to.have.property('entries');
expect(res.entries).to.be.instanceof(Array);
expect(res.cursor).to.eql('cursor==');
expect(res.has_more).to.eql(false);
});
});

Expand Down
57 changes: 55 additions & 2 deletions test/chartmogul/import/invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('DeprecatedCustomerInvoice', () => {
});
});

it('should get all customer invoices', () => {
it('should get all customer invoices with old pagination', () => {
const customerUUID = 'cus_9bf6482d-01e5-4944-957d-5bc730d2cda3';

nock(config.API_BASE)
Expand All @@ -125,10 +125,35 @@ describe('DeprecatedCustomerInvoice', () => {
.then(res => {
expect(res).to.have.property('invoices');
expect(res.invoices).to.be.instanceof(Array);
expect(res.current_page).to.eql(1);
expect(res.total_pages).to.eql(1);
});
});

it('should get all customer invoices in a callback', done => {
it('should get all customer invoices with new pagination', () => {
const customerUUID = 'cus_9bf6482d-01e5-4944-957d-5bc730d2cda3';

nock(config.API_BASE)
.get('/v1/import/customers/' + customerUUID + '/invoices')
.reply(200, {
/* eslint-disable camelcase */
customer_uuid: 'cus_9bf6482d-01e5-4944-957d-5bc730d2cda3',
invoices: [],
cursor: 'cursor==',
has_more: false
/* eslint-enable camelcase */
});

return Invoice.all(config, customerUUID)
.then(res => {
expect(res).to.have.property('invoices');
expect(res.invoices).to.be.instanceof(Array);
expect(res.cursor).to.eql('cursor==');
expect(res.has_more).to.eql(false);
});
});

it('should get all customer invoices in callback (old pagination)', done => {
const customerUUID = 'cus_9bf6482d-01e5-4944-957d-5bc730d2cda3';

nock(config.API_BASE)
Expand All @@ -148,6 +173,34 @@ describe('DeprecatedCustomerInvoice', () => {
}
expect(res).to.have.property('invoices');
expect(res.invoices).to.be.instanceof(Array);
expect(res.current_page).to.eql(1);
expect(res.total_pages).to.eql(1);
done();
});
});

it('should get all customer invoices in callback (new pagination)', done => {
const customerUUID = 'cus_9bf6482d-01e5-4944-957d-5bc730d2cda3';

nock(config.API_BASE)
.get('/v1/import/customers/' + customerUUID + '/invoices')
.reply(200, {
/* eslint-disable camelcase */
customer_uuid: 'cus_9bf6482d-01e5-4944-957d-5bc730d2cda3',
invoices: [],
cursor: 'cursor==',
has_more: false
/* eslint-enable camelcase */
});

Invoice.all(config, customerUUID, (err, res) => {
if (err) {
return done(err);
}
expect(res).to.have.property('invoices');
expect(res.invoices).to.be.instanceof(Array);
expect(res.cursor).to.eql('cursor==');
expect(res.has_more).to.eql(false);
done();
});
});
Expand Down
Loading

0 comments on commit 153a885

Please sign in to comment.