@tanstack/svelte-query 5.0.0-alpha.5 → 5.0.0-alpha.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/lib/HydrationBoundary.svelte.d.ts +4 -3
- package/build/lib/HydrationBoundary.svelte.d.ts.map +1 -0
- package/build/lib/QueryClientProvider.svelte.d.ts +4 -3
- package/build/lib/QueryClientProvider.svelte.d.ts.map +1 -0
- package/build/lib/__tests__/CreateMutation.svelte +10 -0
- package/build/lib/__tests__/CreateMutation.svelte.d.ts +18 -0
- package/build/lib/__tests__/CreateMutation.svelte.d.ts.map +1 -0
- package/build/lib/__tests__/CreateQueries.svelte +16 -0
- package/build/lib/__tests__/CreateQueries.svelte.d.ts +20 -0
- package/build/lib/__tests__/CreateQueries.svelte.d.ts.map +1 -0
- package/build/lib/__tests__/CreateQuery.svelte +22 -0
- package/build/lib/__tests__/CreateQuery.svelte.d.ts +18 -0
- package/build/lib/__tests__/CreateQuery.svelte.d.ts.map +1 -0
- package/build/lib/__tests__/createMutation.test.d.ts +2 -0
- package/build/lib/__tests__/createMutation.test.d.ts.map +1 -0
- package/build/lib/__tests__/createMutation.test.js +19 -0
- package/build/lib/__tests__/createQueries.test.d.ts +2 -0
- package/build/lib/__tests__/createQueries.test.d.ts.map +1 -0
- package/build/lib/__tests__/createQueries.test.js +38 -0
- package/build/lib/__tests__/createQuery.test.d.ts +2 -0
- package/build/lib/__tests__/createQuery.test.d.ts.map +1 -0
- package/build/lib/__tests__/createQuery.test.js +57 -0
- package/build/lib/__tests__/utils.d.ts +10 -0
- package/build/lib/__tests__/utils.d.ts.map +1 -0
- package/build/lib/__tests__/utils.js +33 -0
- package/build/lib/context.d.ts +1 -0
- package/build/lib/context.d.ts.map +1 -0
- package/build/lib/createBaseQuery.d.ts +3 -2
- package/build/lib/createBaseQuery.d.ts.map +1 -0
- package/build/lib/createBaseQuery.js +1 -11
- package/build/lib/createInfiniteQuery.d.ts +3 -2
- package/build/lib/createInfiniteQuery.d.ts.map +1 -0
- package/build/lib/createMutation.d.ts +3 -2
- package/build/lib/createMutation.d.ts.map +1 -0
- package/build/lib/createQueries.d.ts +17 -16
- package/build/lib/createQueries.d.ts.map +1 -0
- package/build/lib/createQueries.js +7 -6
- package/build/lib/createQuery.d.ts +6 -5
- package/build/lib/createQuery.d.ts.map +1 -0
- package/build/lib/index.d.ts +1 -0
- package/build/lib/index.d.ts.map +1 -0
- package/build/lib/types.d.ts +27 -29
- package/build/lib/types.d.ts.map +1 -0
- package/build/lib/useHydrate.d.ts +1 -0
- package/build/lib/useHydrate.d.ts.map +1 -0
- package/build/lib/useIsFetching.d.ts +1 -0
- package/build/lib/useIsFetching.d.ts.map +1 -0
- package/build/lib/useIsMutating.d.ts +1 -0
- package/build/lib/useIsMutating.d.ts.map +1 -0
- package/build/lib/useQueryClient.d.ts +1 -0
- package/build/lib/useQueryClient.d.ts.map +1 -0
- package/build/lib/utils.d.ts +1 -0
- package/build/lib/utils.d.ts.map +1 -0
- package/package.json +34 -29
- package/src/createBaseQuery.ts +9 -28
- package/src/createInfiniteQuery.ts +7 -10
- package/src/createMutation.ts +1 -4
- package/src/createQueries.ts +54 -32
- package/src/createQuery.ts +3 -10
- package/src/types.ts +39 -36
- package/build/lib/.gitignore +0 -9
- package/src/__tests__/CreateMutation.svelte +0 -17
- package/src/__tests__/CreateQueries.svelte +0 -20
- package/src/__tests__/CreateQuery.svelte +0 -30
- package/src/__tests__/createMutation.test.ts +0 -24
- package/src/__tests__/createQueries.test.ts +0 -41
- package/src/__tests__/createQuery.test.ts +0 -64
- package/src/__tests__/utils.ts +0 -40
package/src/types.ts
CHANGED
|
@@ -10,87 +10,91 @@ import type {
|
|
|
10
10
|
DefinedQueryObserverResult,
|
|
11
11
|
DefaultError,
|
|
12
12
|
} from '@tanstack/query-core'
|
|
13
|
-
import type { QueryClient } from '@tanstack/query-core'
|
|
14
13
|
import type { Readable, Writable } from 'svelte/store'
|
|
15
14
|
|
|
15
|
+
/** Allows a type to be either the base object or a store of that object */
|
|
16
16
|
export type WritableOrVal<T> = T | Writable<T>
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
* Use this to pass your Svelte Query context. Otherwise, `defaultContext` will be used.
|
|
21
|
-
*/
|
|
22
|
-
context?: QueryClient | undefined
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface CreateBaseQueryOptions<
|
|
18
|
+
/** Options for createBaseQuery */
|
|
19
|
+
export type CreateBaseQueryOptions<
|
|
26
20
|
TQueryFnData = unknown,
|
|
27
21
|
TError = DefaultError,
|
|
28
22
|
TData = TQueryFnData,
|
|
29
23
|
TQueryData = TQueryFnData,
|
|
30
24
|
TQueryKey extends QueryKey = QueryKey,
|
|
31
|
-
>
|
|
32
|
-
|
|
25
|
+
> = WritableOrVal<
|
|
26
|
+
QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
|
|
27
|
+
>
|
|
33
28
|
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
/** Result from createBaseQuery */
|
|
30
|
+
export type CreateBaseQueryResult<
|
|
31
|
+
TData = unknown,
|
|
32
|
+
TError = DefaultError,
|
|
33
|
+
> = Readable<QueryObserverResult<TData, TError>>
|
|
36
34
|
|
|
37
|
-
|
|
35
|
+
/** Options for createQuery */
|
|
36
|
+
export type CreateQueryOptions<
|
|
38
37
|
TQueryFnData = unknown,
|
|
39
38
|
TError = DefaultError,
|
|
40
39
|
TData = TQueryFnData,
|
|
41
40
|
TQueryKey extends QueryKey = QueryKey,
|
|
42
|
-
>
|
|
43
|
-
TQueryFnData,
|
|
44
|
-
TError,
|
|
45
|
-
TData,
|
|
46
|
-
TQueryFnData,
|
|
47
|
-
TQueryKey
|
|
48
|
-
> {}
|
|
41
|
+
> = CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>
|
|
49
42
|
|
|
50
|
-
|
|
51
|
-
|
|
43
|
+
/** Result from createQuery */
|
|
44
|
+
export type CreateQueryResult<
|
|
45
|
+
TData = unknown,
|
|
46
|
+
TError = DefaultError,
|
|
47
|
+
> = CreateBaseQueryResult<TData, TError>
|
|
52
48
|
|
|
53
|
-
|
|
49
|
+
/** Options for createInfiniteQuery */
|
|
50
|
+
export type CreateInfiniteQueryOptions<
|
|
54
51
|
TQueryFnData = unknown,
|
|
55
52
|
TError = DefaultError,
|
|
56
53
|
TData = TQueryFnData,
|
|
57
54
|
TQueryData = TQueryFnData,
|
|
58
55
|
TQueryKey extends QueryKey = QueryKey,
|
|
59
56
|
TPageParam = unknown,
|
|
60
|
-
>
|
|
57
|
+
> = WritableOrVal<
|
|
58
|
+
InfiniteQueryObserverOptions<
|
|
61
59
|
TQueryFnData,
|
|
62
60
|
TError,
|
|
63
61
|
TData,
|
|
64
62
|
TQueryData,
|
|
65
63
|
TQueryKey,
|
|
66
64
|
TPageParam
|
|
67
|
-
>
|
|
65
|
+
>
|
|
66
|
+
>
|
|
68
67
|
|
|
68
|
+
/** Result from createInfiniteQuery */
|
|
69
69
|
export type CreateInfiniteQueryResult<
|
|
70
70
|
TData = unknown,
|
|
71
71
|
TError = DefaultError,
|
|
72
72
|
> = Readable<InfiniteQueryObserverResult<TData, TError>>
|
|
73
73
|
|
|
74
|
+
/** Options for createBaseQuery with initialData */
|
|
74
75
|
export type DefinedCreateBaseQueryResult<
|
|
75
76
|
TData = unknown,
|
|
76
77
|
TError = DefaultError,
|
|
77
78
|
> = Readable<DefinedQueryObserverResult<TData, TError>>
|
|
78
79
|
|
|
80
|
+
/** Options for createQuery with initialData */
|
|
79
81
|
export type DefinedCreateQueryResult<
|
|
80
82
|
TData = unknown,
|
|
81
83
|
TError = DefaultError,
|
|
82
84
|
> = DefinedCreateBaseQueryResult<TData, TError>
|
|
83
85
|
|
|
84
|
-
|
|
86
|
+
/** Options for createMutation */
|
|
87
|
+
export type CreateMutationOptions<
|
|
85
88
|
TData = unknown,
|
|
86
89
|
TError = DefaultError,
|
|
87
90
|
TVariables = void,
|
|
88
91
|
TContext = unknown,
|
|
89
|
-
>
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
> = WritableOrVal<
|
|
93
|
+
Omit<
|
|
94
|
+
MutationObserverOptions<TData, TError, TVariables, TContext>,
|
|
95
|
+
'_defaulted' | 'variables'
|
|
96
|
+
>
|
|
97
|
+
>
|
|
94
98
|
|
|
95
99
|
export type CreateMutateFunction<
|
|
96
100
|
TData = unknown,
|
|
@@ -120,13 +124,12 @@ export type CreateBaseMutationResult<
|
|
|
120
124
|
mutateAsync: CreateMutateAsyncFunction<TData, TError, TVariables, TContext>
|
|
121
125
|
}
|
|
122
126
|
|
|
123
|
-
|
|
127
|
+
/** Result from createMutation */
|
|
128
|
+
export type CreateMutationResult<
|
|
124
129
|
TData = unknown,
|
|
125
130
|
TError = DefaultError,
|
|
126
131
|
TVariables = unknown,
|
|
127
132
|
TContext = unknown,
|
|
128
|
-
>
|
|
129
|
-
CreateBaseMutationResult<TData, TError, TVariables, TContext>
|
|
130
|
-
> {}
|
|
133
|
+
> = Readable<CreateBaseMutationResult<TData, TError, TVariables, TContext>>
|
|
131
134
|
|
|
132
135
|
type Override<A, B> = { [K in keyof A]: K extends keyof B ? B[K] : A[K] }
|
package/build/lib/.gitignore
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import {
|
|
3
|
-
createMutation,
|
|
4
|
-
QueryClient,
|
|
5
|
-
type CreateMutationOptions,
|
|
6
|
-
} from '../index'
|
|
7
|
-
import { setQueryClientContext } from '../context'
|
|
8
|
-
|
|
9
|
-
export let options: CreateMutationOptions
|
|
10
|
-
|
|
11
|
-
const queryClient = new QueryClient()
|
|
12
|
-
setQueryClientContext(queryClient)
|
|
13
|
-
|
|
14
|
-
const mutation = createMutation(options)
|
|
15
|
-
</script>
|
|
16
|
-
|
|
17
|
-
<button on:click={() => $mutation.mutate()}>Click</button>
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import { createQueries, QueryClient } from '../index'
|
|
3
|
-
import { setQueryClientContext } from '../context'
|
|
4
|
-
import type { QueriesOptions } from '../createQueries'
|
|
5
|
-
|
|
6
|
-
export let options: { queries: [...QueriesOptions<any>] }
|
|
7
|
-
|
|
8
|
-
const queryClient = new QueryClient()
|
|
9
|
-
setQueryClientContext(queryClient)
|
|
10
|
-
|
|
11
|
-
const queries = createQueries(options)
|
|
12
|
-
</script>
|
|
13
|
-
|
|
14
|
-
{#each $queries as query, index}
|
|
15
|
-
{#if query.isPending}
|
|
16
|
-
<p>Loading {index + 1}</p>
|
|
17
|
-
{:else if query.isSuccess}
|
|
18
|
-
<p>{query.data}</p>
|
|
19
|
-
{/if}
|
|
20
|
-
{/each}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import {
|
|
3
|
-
createQuery,
|
|
4
|
-
QueryClient,
|
|
5
|
-
type CreateQueryOptions,
|
|
6
|
-
type WritableOrVal,
|
|
7
|
-
} from '../index'
|
|
8
|
-
import { setQueryClientContext } from '../context'
|
|
9
|
-
|
|
10
|
-
export let options: WritableOrVal<CreateQueryOptions<any>>
|
|
11
|
-
|
|
12
|
-
const queryClient = new QueryClient()
|
|
13
|
-
setQueryClientContext(queryClient)
|
|
14
|
-
|
|
15
|
-
const query = createQuery(options)
|
|
16
|
-
</script>
|
|
17
|
-
|
|
18
|
-
{#if $query.isPending}
|
|
19
|
-
<p>Loading</p>
|
|
20
|
-
{:else if $query.isError}
|
|
21
|
-
<p>Error</p>
|
|
22
|
-
{:else if $query.isSuccess}
|
|
23
|
-
<p>Success</p>
|
|
24
|
-
{/if}
|
|
25
|
-
|
|
26
|
-
<ul>
|
|
27
|
-
{#each $query.data ?? [] as entry}
|
|
28
|
-
<li>id: {entry.id}</li>
|
|
29
|
-
{/each}
|
|
30
|
-
</ul>
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi } from 'vitest'
|
|
2
|
-
import { fireEvent, render, screen } from '@testing-library/svelte'
|
|
3
|
-
import CreateMutation from './CreateMutation.svelte'
|
|
4
|
-
import { sleep } from './utils'
|
|
5
|
-
|
|
6
|
-
describe('createMutation', () => {
|
|
7
|
-
it('Call mutate and check function runs', async () => {
|
|
8
|
-
const mutationFn = vi.fn()
|
|
9
|
-
|
|
10
|
-
render(CreateMutation, {
|
|
11
|
-
props: {
|
|
12
|
-
options: {
|
|
13
|
-
mutationFn,
|
|
14
|
-
},
|
|
15
|
-
},
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
fireEvent.click(screen.getByRole('button'))
|
|
19
|
-
|
|
20
|
-
await sleep(20)
|
|
21
|
-
|
|
22
|
-
expect(mutationFn).toHaveBeenCalledTimes(1)
|
|
23
|
-
})
|
|
24
|
-
})
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { render, waitFor } from '@testing-library/svelte'
|
|
3
|
-
import CreateQueries from './CreateQueries.svelte'
|
|
4
|
-
import { sleep } from './utils'
|
|
5
|
-
|
|
6
|
-
describe('createQueries', () => {
|
|
7
|
-
it('Render and wait for success', async () => {
|
|
8
|
-
const rendered = render(CreateQueries, {
|
|
9
|
-
props: {
|
|
10
|
-
options: {
|
|
11
|
-
queries: [
|
|
12
|
-
{
|
|
13
|
-
queryKey: ['key-1'],
|
|
14
|
-
queryFn: async () => {
|
|
15
|
-
await sleep(10)
|
|
16
|
-
return 'Success 1'
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
queryKey: ['key-2'],
|
|
21
|
-
queryFn: async () => {
|
|
22
|
-
await sleep(10)
|
|
23
|
-
return 'Success 2'
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
],
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
await waitFor(() => {
|
|
32
|
-
expect(rendered.getByText('Loading 1')).toBeInTheDocument()
|
|
33
|
-
expect(rendered.getByText('Loading 2')).toBeInTheDocument()
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
await waitFor(() => {
|
|
37
|
-
expect(rendered.getByText('Success 1')).toBeInTheDocument()
|
|
38
|
-
expect(rendered.getByText('Success 2')).toBeInTheDocument()
|
|
39
|
-
})
|
|
40
|
-
})
|
|
41
|
-
})
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { render, waitFor } from '@testing-library/svelte'
|
|
3
|
-
import { writable } from 'svelte/store'
|
|
4
|
-
import CreateQuery from './CreateQuery.svelte'
|
|
5
|
-
import { sleep } from './utils'
|
|
6
|
-
import type { CreateQueryOptions, WritableOrVal } from '../types'
|
|
7
|
-
|
|
8
|
-
describe('createQuery', () => {
|
|
9
|
-
it('Render and wait for success', async () => {
|
|
10
|
-
const rendered = render(CreateQuery, {
|
|
11
|
-
props: {
|
|
12
|
-
options: {
|
|
13
|
-
queryKey: ['test'],
|
|
14
|
-
queryFn: async () => {
|
|
15
|
-
await sleep(10)
|
|
16
|
-
return 'Success'
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
await waitFor(() => {
|
|
23
|
-
expect(rendered.getByText('Loading')).toBeInTheDocument()
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
await waitFor(() => {
|
|
27
|
-
expect(rendered.getByText('Success')).toBeInTheDocument()
|
|
28
|
-
})
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
it('should keep previous data when returned as placeholder data', async () => {
|
|
32
|
-
const options: WritableOrVal<CreateQueryOptions> = writable({
|
|
33
|
-
queryKey: ['test', [1]],
|
|
34
|
-
queryFn: async ({ queryKey }) => {
|
|
35
|
-
await sleep(50)
|
|
36
|
-
const ids = queryKey[1]
|
|
37
|
-
if (!ids || !Array.isArray(ids)) return []
|
|
38
|
-
return ids.map((id) => ({ id }))
|
|
39
|
-
},
|
|
40
|
-
placeholderData: (previousData: { id: number }[]) => previousData,
|
|
41
|
-
})
|
|
42
|
-
const rendered = render(CreateQuery, { props: { options } })
|
|
43
|
-
|
|
44
|
-
expect(rendered.queryByText('id: 1')).not.toBeInTheDocument()
|
|
45
|
-
expect(rendered.queryByText('id: 2')).not.toBeInTheDocument()
|
|
46
|
-
|
|
47
|
-
await sleep(100)
|
|
48
|
-
|
|
49
|
-
expect(rendered.queryByText('id: 1')).toBeInTheDocument()
|
|
50
|
-
expect(rendered.queryByText('id: 2')).not.toBeInTheDocument()
|
|
51
|
-
|
|
52
|
-
options.update((o) => ({ ...o, queryKey: ['test', [1, 2]] }))
|
|
53
|
-
|
|
54
|
-
await sleep(0)
|
|
55
|
-
|
|
56
|
-
expect(rendered.queryByText('id: 1')).toBeInTheDocument()
|
|
57
|
-
expect(rendered.queryByText('id: 2')).not.toBeInTheDocument()
|
|
58
|
-
|
|
59
|
-
await sleep(100)
|
|
60
|
-
|
|
61
|
-
expect(rendered.queryByText('id: 1')).toBeInTheDocument()
|
|
62
|
-
expect(rendered.queryByText('id: 2')).toBeInTheDocument()
|
|
63
|
-
})
|
|
64
|
-
})
|
package/src/__tests__/utils.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { vi } from 'vitest'
|
|
2
|
-
import { act } from '@testing-library/svelte'
|
|
3
|
-
import { QueryClient, type QueryClientConfig } from '../index'
|
|
4
|
-
|
|
5
|
-
export function createQueryClient(config?: QueryClientConfig): QueryClient {
|
|
6
|
-
return new QueryClient(config)
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function mockVisibilityState(value: DocumentVisibilityState) {
|
|
10
|
-
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function mockNavigatorOnLine(value: boolean) {
|
|
14
|
-
return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
let queryKeyCount = 0
|
|
18
|
-
export function queryKey(): Array<string> {
|
|
19
|
-
queryKeyCount++
|
|
20
|
-
return [`query_${queryKeyCount}`]
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function sleep(timeout: number): Promise<void> {
|
|
24
|
-
return new Promise((resolve, _reject) => {
|
|
25
|
-
setTimeout(resolve, timeout)
|
|
26
|
-
})
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export async function simplefetcher() {
|
|
30
|
-
await sleep(10)
|
|
31
|
-
return 'test'
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function setActTimeout(fn: () => void, ms?: number) {
|
|
35
|
-
return setTimeout(() => {
|
|
36
|
-
act(() => {
|
|
37
|
-
fn()
|
|
38
|
-
})
|
|
39
|
-
}, ms)
|
|
40
|
-
}
|