Skip to content

Commit 01efb4b

Browse files
Merge pull request #58 from kitbagjs/useQuery-always-getter
QueryComposition arguments should ALWAYS be a getter
2 parents c589f46 + 5a7dfda commit 01efb4b

5 files changed

Lines changed: 27 additions & 28 deletions

File tree

src/createQueryClient.spec-d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ describe('useQuery', () => {
5353
const placeholder = 'placeholder' as const
5454
const action = () => response
5555

56-
const queryA = useQuery(action, [])
56+
const queryA = useQuery(action, () => [])
5757
expectTypeOf(queryA.data).toEqualTypeOf<typeof response | undefined>()
5858

59-
const queryB = useQuery(action, [], { placeholder })
59+
const queryB = useQuery(action, () => [], { placeholder })
6060
expectTypeOf(queryB.data).toEqualTypeOf<typeof response | typeof placeholder>()
6161

62-
const queryC = await useQuery(action, [])
62+
const queryC = await useQuery(action, () => [])
6363
expectTypeOf(queryC.data).toEqualTypeOf<typeof response>()
6464

65-
const queryD = await useQuery(action, [], { placeholder })
65+
const queryD = await useQuery(action, () => [], { placeholder })
6666
expectTypeOf(queryD.data).toEqualTypeOf<typeof response>()
6767
})
6868
})

src/createQueryClient.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ describe('useQuery', () => {
269269
const action = vi.fn(() => response)
270270
const { useQuery } = createQueryClient()
271271

272-
const query = await useQuery(action, [])
272+
const query = await useQuery(action, () => [])
273273

274274
expect(query.data).toBe(response)
275275
})
@@ -280,7 +280,7 @@ describe('useQuery', () => {
280280
throw new Error('test')
281281
})
282282
const { useQuery } = createQueryClient()
283-
const value = useQuery(action, [])
283+
const value = useQuery(action, () => [])
284284

285285
await expect(value).rejects.toThrow('test')
286286
})
@@ -339,8 +339,8 @@ describe('useQuery', () => {
339339
const action2 = vi.fn(() => false)
340340
const { useQuery } = createQueryClient()
341341

342-
const query1 = useQuery(action1, [])
343-
const query2 = useQuery(action2, [])
342+
const query1 = useQuery(action1, () => [])
343+
const query2 = useQuery(action2, () => [])
344344

345345
await vi.runOnlyPendingTimersAsync()
346346

@@ -353,7 +353,7 @@ describe('useQuery', () => {
353353
const response = Symbol('response')
354354
const { useQuery } = createQueryClient()
355355

356-
const value = useQuery(() => response, [], { placeholder })
356+
const value = useQuery(() => response, () => [], { placeholder })
357357

358358
expect(value.data).toBe(placeholder)
359359

@@ -368,7 +368,7 @@ describe('useQuery', () => {
368368
const action = vi.fn(() => response)
369369
const { useQuery } = createQueryClient()
370370

371-
const query = useQuery(action, [], { immediate: true })
371+
const query = useQuery(action, () => [], { immediate: true })
372372

373373
await vi.runOnlyPendingTimersAsync()
374374

@@ -381,7 +381,7 @@ describe('useQuery', () => {
381381
const action = vi.fn(() => response)
382382
const { useQuery } = createQueryClient()
383383

384-
const query = useQuery(action, [], { immediate: false })
384+
const query = useQuery(action, () => [], { immediate: false })
385385

386386
await vi.runOnlyPendingTimersAsync()
387387

@@ -395,7 +395,7 @@ describe('useQuery', () => {
395395
const action = vi.fn(() => response)
396396
const { useQuery } = createQueryClient()
397397

398-
const query = useQuery(action, [], { immediate: false, placeholder })
398+
const query = useQuery(action, () => [], { immediate: false, placeholder })
399399

400400
await vi.runOnlyPendingTimersAsync()
401401

@@ -408,7 +408,7 @@ describe('useQuery', () => {
408408
const action = vi.fn(() => response)
409409
const { useQuery } = createQueryClient()
410410

411-
const query = useQuery(action, [], { immediate: false })
411+
const query = useQuery(action, () => [], { immediate: false })
412412

413413
await vi.runOnlyPendingTimersAsync()
414414

@@ -426,7 +426,7 @@ describe('useQuery', () => {
426426
const action = vi.fn(() => response)
427427
const { useQuery } = createQueryClient()
428428

429-
const query = useQuery(action, [], { immediate: false })
429+
const query = useQuery(action, () => [], { immediate: false })
430430

431431
await vi.runOnlyPendingTimersAsync()
432432

@@ -448,7 +448,7 @@ describe('useQuery', () => {
448448
})
449449
const { useQuery } = createQueryClient()
450450

451-
const query = useQuery(action, [], { immediate: false })
451+
const query = useQuery(action, () => [], { immediate: false })
452452

453453
await vi.runOnlyPendingTimersAsync()
454454

@@ -489,7 +489,7 @@ describe('defineQuery', () => {
489489

490490
const { useQuery } = defineQuery(action)
491491

492-
const value = useQuery([])
492+
const value = useQuery(() => [])
493493

494494
await vi.runOnlyPendingTimersAsync()
495495

src/tag.spec-d.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,33 @@ import { tag } from './tag'
66
test('tag function returns a tag when no callback is provided', () => {
77
const value = tag()
88

9-
expectTypeOf(value).toMatchTypeOf<QueryTag>()
9+
expectTypeOf(value).toExtend<QueryTag>()
1010
})
1111

1212
test('tag function returns a tag factory when a callback is provided', () => {
1313
const factory = tag((string: string) => string)
1414

15-
expectTypeOf(factory).toMatchTypeOf<QueryTagFactory<unknown, string>>()
15+
expectTypeOf(factory).toExtend<QueryTagFactory<unknown, string>>()
1616

1717
const value = factory('foo')
1818

19-
expectTypeOf(value).toMatchTypeOf<QueryTag<Unset>>()
19+
expectTypeOf(value).toExtend<QueryTag<Unset>>()
2020
})
2121

2222
test('tag function returns a typed tag when data generic is provided', () => {
2323
const value = tag<string>()
2424

25-
expectTypeOf(value).toMatchTypeOf<QueryTag<string>>()
25+
expectTypeOf(value).toExtend<QueryTag<string>>()
2626
})
2727

2828
test('tag factory returns a typed tag when data generic is provided', () => {
2929
const factory = tag<string, string>((value: string) => value)
3030

31-
expectTypeOf(factory).toMatchTypeOf<QueryTagFactory<string, string>>()
31+
expectTypeOf(factory).toExtend<QueryTagFactory<string, string>>()
3232

3333
const value = factory('foo')
3434

35-
expectTypeOf(value).toMatchTypeOf<QueryTag<string>>()
35+
expectTypeOf(value).toExtend<QueryTag<string>>()
3636
})
3737

3838
test('query from query function with tags callback is called with the query data', () => {
@@ -41,7 +41,7 @@ test('query from query function with tags callback is called with the query data
4141

4242
query(action, [], {
4343
tags: (data) => {
44-
expectTypeOf(data).toMatchTypeOf<string>()
44+
expectTypeOf(data).toExtend<string>()
4545

4646
return []
4747
},
@@ -52,9 +52,9 @@ test('query from query composition with tags callback is called with the query d
5252
const { useQuery } = createQueryClient()
5353
const action = vi.fn(() => 'foo')
5454

55-
useQuery(action, [], {
55+
useQuery(action, () => [], {
5656
tags: (data) => {
57-
expectTypeOf(data).toMatchTypeOf<string>()
57+
expectTypeOf(data).toExtend<string>()
5858

5959
return []
6060
},

src/types/getters.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export type MaybeGetter<T> = T | Getter<T>
21
export type Getter<T> = () => T

src/types/query.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RetryOptions } from '@/utilities/retry'
2-
import { Getter, MaybeGetter } from './getters'
2+
import { Getter } from './getters'
33
import { QueryTag, Unset } from '@/types/tags'
44
import { DefaultValue } from './utilities'
55

@@ -15,7 +15,7 @@ export type QueryData<
1515

1616
export type QueryActionArgs<
1717
TAction extends QueryAction
18-
> = MaybeGetter<Parameters<TAction>> | Getter<Parameters<TAction> | null> | Getter<null>
18+
> = Getter<Parameters<TAction> | null> | Getter<null>
1919

2020
export type QueryTags<
2121
TAction extends QueryAction = QueryAction

0 commit comments

Comments
 (0)