Skip to content

Commit

Permalink
validate that page parameters are deprecated
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Vieira <[email protected]>
  • Loading branch information
Bruno Vieira committed Sep 15, 2023
1 parent e82794a commit 2478378
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/chartmogul/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ const ForbiddenError = require('./forbidden-error');
const NotFoundError = require('./not-found-error');
const ResourceInvalidError = require('./resource-invalid-error');
const SchemaInvalidError = require('./schema-invalid-error');
const InvalidParameterError = require('./invalid-parameter-error');

module.exports = {
ChartMogulError,
ConfigurationError,
ForbiddenError,
NotFoundError,
ResourceInvalidError,
SchemaInvalidError
SchemaInvalidError,
InvalidParameterError
};
12 changes: 12 additions & 0 deletions lib/chartmogul/errors/invalid-parameter-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const ChartMogulError = require('./chartmogul-error');

class InvalidParameterError extends ChartMogulError {
constructor (message, httpStatus, response) {
super(message, httpStatus, response);
Error.captureStackTrace(this, this.constructor.name);
}
}

module.exports = InvalidParameterError;
7 changes: 7 additions & 0 deletions lib/chartmogul/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class Resource {
return reject(error);
}

if (method === 'GET' && data && data.page !== undefined) {
const error = new errors.InvalidParameterError(
"Argument 'per_page' is deprecated"
);
return reject(error);
}

const qs = method === 'GET' ? data : {};
const body = method === 'GET' ? {} : data;

Expand Down
14 changes: 14 additions & 0 deletions test/chartmogul/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Customer = require('../../lib/chartmogul/customer');
describe('Resource', () => {
const config = new ChartMogul.Config('token');
config.retries = 0; // no retry

it('should send basicAuth headers', done => {
nock(config.API_BASE)
.get('/')
Expand Down Expand Up @@ -107,6 +108,19 @@ describe('Resource', () => {
done();
});
});

it('should throw InvalidParameterError', done => {
nock(config.API_BASE)
.get('/customers?page=1')
.reply(200, '{}');

Customer.all(config, { page: 1 })
.then(res => done(new Error('Should throw error')))
.catch(e => {
expect(e).to.be.instanceOf(ChartMogul.ConfigurationError);
done();
});
});
});

describe('Resource Retry', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/chartmogul/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('Subscription', () => {
expect(res).to.have.property('subscriptions');
expect(res.subscriptions).to.be.instanceof(Array);
expect(res.cursor).to.eql('cursor==');
expect(res.has_more).to.eql(false)
expect(res.has_more).to.eql(false);
});
});
});

0 comments on commit 2478378

Please sign in to comment.