Skip to content

Commit 65a64d5

Browse files
committed
chore: 🔥 fixed pagination transformer and base transformer
1 parent 185b337 commit 65a64d5

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-api-queries",
3-
"version": "0.0.16",
3+
"version": "0.1.0",
44
"description": "Elegant and simple way to build requests for REST API",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/__tests__/base-proxy.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ describe('BaseProxy', () => {
6666
expect(all.pagination.page).toEqual(1)
6767
})
6868

69+
it('should assign default object meta if it is null or undefined', async () => {
70+
const items = {
71+
data: [{ first_name: 'Chantouch', last_name: 'Sek' }],
72+
meta: undefined,
73+
}
74+
mockAdapter.onGet('/posts').reply(200, items)
75+
const { data, meta } = await proxy.removeParameters([]).all()
76+
const all = {
77+
items: BaseTransformer.fetchCollection(data),
78+
pagination: PaginationTransformer.fetch(meta),
79+
}
80+
expect(meta).toBeUndefined()
81+
expect(data.length).toEqual(1)
82+
expect(all.pagination.currentPage).toEqual(1)
83+
})
84+
6985
it('will fetch all records', async () => {
7086
const items = [{ first_name: 'Chantouch', last_name: 'Sek' }]
7187
mockAdapter.onGet('/posts').reply(200, { data: items })

src/core/BaseTransformer.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,21 @@
11
import snakeCaseKeys from 'snakecase-keys'
22
import camelcaseKeys from 'camelcase-keys'
33

4-
export type Attribute = {
5-
[key in string | number]: any
6-
}
7-
84
class BaseTransformer {
9-
static fetchCollection(
10-
items: Array<Attribute>,
11-
camelCaseKey?: boolean,
12-
): Array<Attribute> {
13-
return items.map((item: Attribute) => this.fetch(item, camelCaseKey))
5+
static fetchCollection(items: Array<any>, camelKey?: boolean): Array<any> {
6+
return items.map((item: any) => this.fetch(item, camelKey))
147
}
158

16-
static sendCollection(
17-
items: Array<Attribute>,
18-
snakeCaseKey?: boolean,
19-
): Array<Attribute> {
20-
return items.map((item: Attribute) => this.send(item, snakeCaseKey))
9+
static sendCollection(items: Array<any>, snakeKey?: boolean): Array<any> {
10+
return items.map((item: any) => this.send(item, snakeKey))
2111
}
2212

23-
static fetch(item: Attribute, camelCaseKey?: boolean): Attribute {
24-
if (camelCaseKey) {
25-
return camelcaseKeys(item, { deep: true })
26-
}
27-
return item
13+
static fetch(item: any, camelKey?: boolean) {
14+
return camelKey ? camelcaseKeys(item, { deep: true }) : item
2815
}
2916

30-
static send(item: Attribute, snakeCaseKey?: boolean): Attribute {
31-
if (snakeCaseKey) {
32-
return snakeCaseKeys(item)
33-
}
34-
return item
17+
static send(item: any, snakeKey?: boolean) {
18+
return snakeKey ? snakeCaseKeys(item) : item
3519
}
3620
}
3721

src/core/PaginationTransformer.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,21 @@ export interface MetaOptions {
2626

2727
class PaginationTransformer extends BaseTransformer {
2828
static fetch(meta: MetaOptions | any) {
29+
if (!meta) {
30+
meta = { pagination: {}, include: [] }
31+
}
2932
if (!Object.prototype.hasOwnProperty.call(meta, 'pagination')) {
30-
return snakecaseKeys(meta, { deep: true })
33+
return super.fetch(meta, true)
3134
}
3235
const { pagination = {}, include } = meta
33-
return {
36+
const payload = {
37+
...pagination,
3438
perPage: pagination.per_page,
3539
totalPages: pagination.total_pages,
36-
currentPage: pagination.current_page,
40+
currentPage: pagination.current_page || 1,
3741
total: pagination.total,
3842
links: pagination.links,
39-
count: pagination.count,
43+
count: pagination.count || 0,
4044
page: pagination.current_page,
4145
itemsPerPage: pagination.per_page,
4246
pageCount: pagination.total_pages,
@@ -45,6 +49,8 @@ class PaginationTransformer extends BaseTransformer {
4549
pageStop: pagination.count,
4650
include,
4751
}
52+
53+
return super.fetch(payload, true)
4854
}
4955
}
5056

0 commit comments

Comments
 (0)