Skip to content

Commit

Permalink
feat(api): add parsePaginationParams helper
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-h1 committed Aug 5, 2024
1 parent ff9d050 commit b99903b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"pg": "^8.12.0",
"pino": "^8.6.1",
"pino-pretty": "^9.1.0",
"qs": "^6.13.0",
"redis": "^4.7.0",
"reflect-metadata": "^0.2.2",
"zod": "^3.23.8"
Expand All @@ -67,6 +68,7 @@
"@types/jest": "^29.5.12",
"@types/lodash": "^4.17.6",
"@types/node": "^20.8.6",
"@types/qs": "^6.9.15",
"@types/superagent": "^8.1.8",
"@types/supertest": "^6.0.2",
"@types/uuid": "^10.0.0",
Expand Down
14 changes: 10 additions & 4 deletions pnpm-lock.yaml

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

41 changes: 41 additions & 0 deletions src/utils/__tests__/parsePaginationParams.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import parsePaginationParams from '../parsePaginationParams';

describe('parsePaginationParams', () => {
test('should parse valid page and pageSize as numbers', () => {
const query = {
page: '2',
pageSize: '10',
};

const result = parsePaginationParams(query);
expect(result).toEqual({
page: 2,
pageSize: 10,
});
});

test('should turn negative numbers into the default paging', () => {
const query = {
page: '-1',
pageSize: '-11111110',
};

const result = parsePaginationParams(query);
expect(result).toEqual({
page: 1,
pageSize: 10,
});
});

test('should return undefined for empty query', () => {
const query = {};
const result = parsePaginationParams(query);
expect(result).toEqual({ page: undefined, pageSize: undefined });
});

test('should return undefined for invalid page and pageSize', () => {
const query = { page: 'abc', pageSize: 'xyz' };
const result = parsePaginationParams(query);
expect(result).toEqual({ page: undefined, pageSize: undefined });
});
});
43 changes: 43 additions & 0 deletions src/utils/parsePaginationParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { ParsedQs } from 'qs';

interface PaginationParams {
page?: number;
pageSize?: number;
}

export default function parsePaginationParams(
query: ParsedQs,
): PaginationParams {
let page: number | undefined;
let pageSize: number | undefined;

if (
typeof query.page === 'number' ||
(typeof query.page === 'string' && isInt(query.page))
) {
page = Number(query.page);
if (page < 1) {
page = 1;
}
}

if (
typeof query.pageSize === 'number' ||
(typeof query.pageSize === 'string' && isInt(query.pageSize))
) {
pageSize = Number(query.pageSize);
if (pageSize < 1) {
pageSize = 10;
}
}

return {
page,
pageSize,
};
}

function isInt(value: string): boolean {
const number = Number(value);
return !Number.isNaN(number) && Number.isInteger(number);
}

0 comments on commit b99903b

Please sign in to comment.