Skip to content

Commit a904d5c

Browse files
fix: handle zeros in paginated meta info correctly (#82)
Co-authored-by: Anbraten <anton@ju60.de>
1 parent c39becb commit a904d5c

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

src/utils.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,10 @@ export type ServiceModel<
3232

3333
export function isPaginated<T>(response: T | T[] | Paginated<T>): response is Paginated<T> {
3434
const { total, limit, skip, data } = response as Paginated<T>;
35-
return total !== undefined && limit !== undefined && skip !== undefined && data !== undefined && Array.isArray(data);
35+
return (
36+
typeof total === 'number' &&
37+
typeof limit === 'number' &&
38+
(typeof skip === 'number' || typeof skip === 'string') &&
39+
Array.isArray(data)
40+
);
3641
}

test/utils.test.ts

+91-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest';
22

3-
import { getId, PotentialIds } from '~/utils';
3+
import { getId, isPaginated, PotentialIds } from '~/utils';
44

55
describe('getId()', () => {
66
it('should return "id" property from item', () => {
@@ -27,3 +27,93 @@ describe('getId()', () => {
2727
expect(() => getId(item as PotentialIds)).toThrow('Unable to retrieve id from item');
2828
});
2929
});
30+
31+
describe('isPaginated()', () => {
32+
it('should return true when total, limit, skip and data array are present', () => {
33+
// given
34+
const response = {
35+
total: 100,
36+
limit: 10,
37+
skip: 20,
38+
data: [],
39+
};
40+
41+
// then
42+
expect(isPaginated<typeof response>(response)).toBe(true);
43+
});
44+
45+
it('should return true when total, limit and skip is "0"', () => {
46+
// given
47+
const response = {
48+
total: 0,
49+
limit: 0,
50+
skip: 0,
51+
data: [],
52+
};
53+
54+
// then
55+
expect(isPaginated<typeof response>(response)).toBe(true);
56+
});
57+
58+
it('should return true when skip is a string', () => {
59+
// given
60+
const response = {
61+
total: 100,
62+
limit: 10,
63+
skip: '20',
64+
data: [],
65+
};
66+
67+
// then
68+
expect(isPaginated<typeof response>(response)).toBe(true);
69+
});
70+
71+
it('should return false when total is missing', () => {
72+
// given
73+
const response = {
74+
limit: 10,
75+
skip: 20,
76+
data: [],
77+
};
78+
79+
// then
80+
expect(isPaginated<typeof response>(response)).toBe(false);
81+
});
82+
83+
it('should return false when limit is missing', () => {
84+
// given
85+
const response = {
86+
total: 100,
87+
skip: 20,
88+
data: [],
89+
};
90+
91+
// then
92+
expect(isPaginated<typeof response>(response)).toBe(false);
93+
});
94+
95+
it('should return false when skip is missing', () => {
96+
// given
97+
const response = {
98+
total: 100,
99+
limit: 10,
100+
data: [],
101+
};
102+
103+
// then
104+
expect(isPaginated<typeof response>(response)).toBe(false);
105+
});
106+
107+
it('should return false when data is no array', () => {
108+
// given
109+
const response = {
110+
total: 100,
111+
limit: 10,
112+
skip: 20,
113+
data: {},
114+
};
115+
116+
// then
117+
expect(isPaginated<typeof response>(response)).toBe(false);
118+
});
119+
});

0 commit comments

Comments
 (0)