Skip to content
This repository was archived by the owner on Jun 22, 2021. It is now read-only.

Commit a2e69dc

Browse files
committed
fix(getEntities): Fixes issue with invalid limit.
1 parent 3fd7f78 commit a2e69dc

14 files changed

+59
-15
lines changed

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export interface TodoEntity extends Entity {
2727
import factory from '@js-entity-repos/express/dist/factory';
2828

2929
const todosFacade = factory<TodoEntity>({
30+
// Optional property that determines the default pagination limit.
31+
defaultPaginationLimit: 10,
3032
// Optional property that catches errors from handlers.
3133
errorCatcher: (handler) => (req, res) => {
3234
handler(req, res).catch((err) => {

src/FacadeConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ import ErrorCatcher from './utils/ErrorCatcher';
55
export default interface FacadeConfig<E extends Entity> {
66
readonly service: Facade<E>;
77
readonly errorCatcher: ErrorCatcher;
8+
readonly defaultPaginationLimit: number;
89
}

src/FactoryConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ import ErrorCatcher from './utils/ErrorCatcher';
55
export default interface FactoryConfig<E extends Entity> {
66
readonly service: Facade<E>;
77
readonly errorCatcher?: ErrorCatcher;
8+
readonly defaultPaginationLimit?: number;
89
}

src/factory.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ facadeTest(axiosFactory<TestEntity>({
4343
}));
4444

4545
describe('facade', () => {
46-
it('should not throw JSON error', async () => {
46+
it('should not throw errors when not using any query params', async () => {
4747
const response = await axiosClient.get('/');
4848
assert.equal(response.status, OK);
4949
});
@@ -54,4 +54,11 @@ describe('facade', () => {
5454
assert.equal(err.response.status, BAD_REQUEST);
5555
});
5656
});
57+
it('should throw number error when using invalid limit', async () => {
58+
await axiosClient.get('/?limit=invalid_number').then((response) => {
59+
return { response };
60+
}).catch((err) => {
61+
assert.equal(err.response.status, BAD_REQUEST);
62+
});
63+
});
5764
});

src/factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import catchErrors from './utils/catchErrors';
1515

1616
export default <E extends Entity>(factoryConfig: FactoryConfig<E>): Router => {
1717
const facadeConfig: FacadeConfig<E> = {
18+
defaultPaginationLimit: 10,
1819
errorCatcher: catchErrors,
1920
...factoryConfig,
2021
};

src/functions/getEntities.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import { OK } from 'http-status-codes';
44
import FacadeConfig from '../FacadeConfig';
55
import catchErrors from '../utils/catchErrors';
66
import getJsonQueryParam from '../utils/getJsonQueryParam';
7+
import getNumberQueryParam from '../utils/getNumberQueryParam';
78

89
export default <E extends Entity>(config: FacadeConfig<E>) => {
910
return catchErrors(async (req: Request, res: Response) => {
11+
const limit = getNumberQueryParam(req.query, 'limit', config.defaultPaginationLimit);
1012
const result = await config.service.getEntities({
1113
filter: getJsonQueryParam(req.query, 'filter'),
1214
pagination: {
1315
cursor: req.query.cursor,
1416
forward: req.query.forward === 'true',
15-
limit: Number(req.query.limit),
17+
limit,
1618
},
1719
sort: getJsonQueryParam(req.query, 'sort'),
1820
});

src/utils/JsonError.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// tslint:disable:no-class
2+
import { BaseError } from 'make-error';
3+
4+
export default class JsonError extends BaseError {
5+
constructor(public data: any, public path: string[]) {
6+
super();
7+
}
8+
}

src/utils/JsonSyntaxError.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/utils/NumberError.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// tslint:disable:no-class
2+
import { BaseError } from 'make-error';
3+
4+
export default class NumberError extends BaseError {
5+
constructor(public data: any, public path: string[]) {
6+
super();
7+
}
8+
}

src/utils/Query.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default interface Query {
2+
readonly [k: string]: string | undefined;
3+
}

0 commit comments

Comments
 (0)