Skip to content

Commit 9109fdf

Browse files
author
devgioele
committed
fix type of default query
1 parent b3e1cbf commit 9109fdf

7 files changed

Lines changed: 81 additions & 20 deletions

File tree

src/engine/pagination.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const useAbstractQueryAndPagination = <
5252
>(
5353
router: Router,
5454
parseQuery: ParseQuery<TQuery>,
55-
defaultQuery: TDefaultQuery,
55+
defaultQuery: TDefaultQuery = {} as TDefaultQuery,
5656
defaultPagination?: Partial<PaginationQuery>,
5757
options?: Partial<AbstractQueryOptions>,
5858
) => {

src/engine/query.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export type RouterSSR = Router & {
3838
isReady: boolean
3939
}
4040

41-
export const useQuery = <T extends Query>(router: Router, defaultQuery: T) => {
41+
export const useQuery = (router: Router, defaultQuery: Query) => {
4242
const resetQuery = useCallback(() => {
4343
router.setQuery(defaultQuery, defaultQuery)
4444
}, [router, defaultQuery])
@@ -47,7 +47,7 @@ export const useQuery = <T extends Query>(router: Router, defaultQuery: T) => {
4747

4848
return {
4949
query,
50-
setQuery: (query: Partial<T>) => {
50+
setQuery: (query: Query) => {
5151
router.setQuery(query, defaultQuery)
5252
},
5353
resetQuery,
@@ -81,7 +81,7 @@ export const useAbstractQuery = <
8181
>(
8282
router: Router,
8383
parseQuery: ParseQuery<TQuery>,
84-
defaultQuery: TDefaultQuery,
84+
defaultQuery: TDefaultQuery = {} as TDefaultQuery,
8585
options?: Partial<AbstractQueryOptions>,
8686
) => {
8787
const mergedOptions = useMemo(

src/routers/__test__/nextRouter.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { act, renderHook } from '@testing-library/react'
22
import router from 'next/router'
33
import { z } from 'zod'
4-
import { vi } from 'vitest'
4+
import { expectTypeOf, vi } from 'vitest'
55
import { useQuery, useQueryAndPagination } from '../../zod/routers/nextRouter'
66
import { usePagination } from '../nextRouter'
77

@@ -82,11 +82,43 @@ describe('NextRouter', () => {
8282
})
8383

8484
expect(result.current.query.search).toBe(search)
85+
expectTypeOf(result.current.query.search).toEqualTypeOf<string>()
8586
expect(router.query.search).toBe(search)
8687
expect(result.current.page).toBe(0)
8788
expect(router.query.page).toBeUndefined()
8889
})
8990

91+
test('passing no default query should return query values that are possibly undefined', () => {
92+
const { result: resultQueryAndPagination } = renderHook(() =>
93+
useQueryAndPagination(searchSchema),
94+
)
95+
expectTypeOf(resultQueryAndPagination.current.query.search).toEqualTypeOf<
96+
string | undefined
97+
>()
98+
99+
const { result: resultQuery } = renderHook(() => useQuery(searchSchema))
100+
expectTypeOf(resultQuery.current.query.search).toEqualTypeOf<
101+
string | undefined
102+
>()
103+
})
104+
105+
test('passing a default query should return query values that are not undefined', () => {
106+
const { result: resultQueryAndPagination } = renderHook(() =>
107+
useQueryAndPagination(searchSchema, { search: '' }),
108+
)
109+
expectTypeOf(
110+
resultQueryAndPagination.current.query.search,
111+
).toEqualTypeOf<string>()
112+
113+
const { result: resultQuery } = renderHook(() =>
114+
useQuery(searchSchema, { search: '' }),
115+
)
116+
expectTypeOf(resultQuery.current.query.search).toEqualTypeOf<string>()
117+
118+
const { result: resultPagination } = renderHook(() => usePagination())
119+
expectTypeOf(resultPagination.current.page).toEqualTypeOf<number>()
120+
})
121+
90122
test('change default parameters', () => {
91123
const page = 1
92124
const size = 10
@@ -261,6 +293,7 @@ describe('NextRouter', () => {
261293
})
262294

263295
expect(result.current.query.search).toBe(search)
296+
expectTypeOf(result.current.query.search).toEqualTypeOf<string>()
264297
expect(router.query.search).toBe(search)
265298
})
266299

@@ -270,6 +303,9 @@ describe('NextRouter', () => {
270303
)
271304

272305
expect(result.current.query.department).toBeUndefined()
306+
expectTypeOf(result.current.query.department).toEqualTypeOf<
307+
string | undefined
308+
>()
273309
})
274310

275311
test('the query should be a merge of the default query and the current query', () => {

src/routers/inMemory.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,24 @@ const useInMemoryRouter = (): Router => {
2929
}
3030
}
3131

32-
export const useQuery = <TQuery extends AbstractQuery>(
32+
export const useQuery = <
33+
TQuery extends AbstractQuery,
34+
TDefaultQuery extends Partial<TQuery>,
35+
>(
3336
parseQuery: ParseQuery<TQuery>,
34-
defaultQuery: Partial<TQuery> = {},
37+
defaultQuery?: TDefaultQuery,
3538
options?: Partial<AbstractQueryOptions>,
3639
) => {
3740
const router = useInMemoryRouter()
3841
return useAbstractQuery(router, parseQuery, defaultQuery, options)
3942
}
4043

41-
export const useQueryAndPagination = <TQuery extends AbstractQuery>(
44+
export const useQueryAndPagination = <
45+
TQuery extends AbstractQuery,
46+
TDefaultQuery extends Partial<TQuery>,
47+
>(
4248
parseQuery: ParseQuery<TQuery>,
43-
defaultQuery: Partial<TQuery> = {},
49+
defaultQuery?: TDefaultQuery,
4450
defaultPagination?: Partial<PaginationQuery>,
4551
options?: Partial<AbstractQueryOptions>,
4652
) => {

src/routers/nextRouter.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,25 @@ const useNextRouter = (
5959
}
6060
}
6161

62-
export const useQuery = <TQuery extends AbstractQuery>(
62+
export const useQuery = <
63+
TQuery extends AbstractQuery,
64+
TDefaultQuery extends Partial<TQuery>,
65+
>(
6366
parseQuery: ParseQuery<TQuery>,
64-
defaultQuery: Partial<TQuery> = {},
67+
defaultQuery?: TDefaultQuery,
6568
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
6669
) => {
6770
const router = useNextRouter(options)
6871
const result = useAbstractQuery(router, parseQuery, defaultQuery, options)
6972
return { ...result, queryIsReady: router.isReady }
7073
}
7174

72-
export const useQueryAndPagination = <TQuery extends AbstractQuery>(
75+
export const useQueryAndPagination = <
76+
TQuery extends AbstractQuery,
77+
TDefaultQuery extends Partial<TQuery>,
78+
>(
7379
parseQuery: ParseQuery<TQuery>,
74-
defaultQuery: Partial<TQuery> = {},
80+
defaultQuery?: TDefaultQuery,
7581
defaultPagination?: Partial<PaginationQuery>,
7682
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
7783
) => {

src/routers/reactRouter.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,24 @@ const useReactRouter = (
6767
}
6868
}
6969

70-
export const useQuery = <TQuery extends AbstractQuery>(
70+
export const useQuery = <
71+
TQuery extends AbstractQuery,
72+
TDefaultQuery extends Partial<TQuery>,
73+
>(
7174
parseQuery: ParseQuery<TQuery>,
72-
defaultQuery: Partial<TQuery> = {},
75+
defaultQuery?: TDefaultQuery,
7376
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
7477
) => {
7578
const router = useReactRouter(options)
7679
return useAbstractQuery(router, parseQuery, defaultQuery, options)
7780
}
7881

79-
export const useQueryAndPagination = <TQuery extends AbstractQuery>(
82+
export const useQueryAndPagination = <
83+
TQuery extends AbstractQuery,
84+
TDefaultQuery extends Partial<TQuery>,
85+
>(
8086
parseQuery: ParseQuery<TQuery>,
81-
defaultQuery: Partial<TQuery> = {},
87+
defaultQuery?: TDefaultQuery,
8288
defaultPagination?: Partial<PaginationQuery>,
8389
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
8490
) => {

src/zod/routers/nextRouter.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,25 @@ import { RouterWithHistoryOptions } from '../../routers/shared'
1010
export const useQuery = <
1111
TSchema extends z.ZodTypeAny,
1212
TQuery extends z.output<TSchema>,
13+
TDefaultQuery extends Partial<TQuery>,
1314
>(
1415
schemaQuery: TSchema,
15-
defaultQuery: Partial<TQuery> = {},
16+
defaultQuery?: TDefaultQuery,
1617
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
17-
) => useQueryVanilla(zodParser(schemaQuery), defaultQuery, options)
18+
) =>
19+
useQueryVanilla<TQuery, TDefaultQuery>(
20+
zodParser(schemaQuery),
21+
defaultQuery,
22+
options,
23+
)
1824

1925
export const useQueryAndPagination = <
2026
TSchema extends z.ZodTypeAny,
2127
TQuery extends z.output<TSchema>,
28+
TDefaultQuery extends Partial<TQuery>,
2229
>(
2330
schemaQuery: TSchema,
24-
defaultQuery: Partial<TQuery> = {},
31+
defaultQuery?: TDefaultQuery,
2532
defaultPagination?: Partial<PaginationQuery>,
2633
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
2734
) =>

0 commit comments

Comments
 (0)