@navios/react-query 0.4.0 → 0.5.0
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/CHANGELOG.md +74 -0
- package/dist/src/client/declare-client.d.mts +70 -0
- package/dist/src/client/declare-client.d.mts.map +1 -0
- package/dist/src/client/index.d.mts +3 -0
- package/dist/src/client/index.d.mts.map +1 -0
- package/dist/src/client/types.d.mts +287 -0
- package/dist/src/client/types.d.mts.map +1 -0
- package/dist/src/common/index.d.mts +2 -0
- package/dist/src/common/index.d.mts.map +1 -0
- package/dist/src/common/types.d.mts +21 -0
- package/dist/src/common/types.d.mts.map +1 -0
- package/dist/src/index.d.mts +13 -8
- package/dist/src/index.d.mts.map +1 -1
- package/dist/src/mutation/index.d.mts +4 -0
- package/dist/src/mutation/index.d.mts.map +1 -0
- package/dist/src/mutation/key-creator.d.mts +36 -0
- package/dist/src/mutation/key-creator.d.mts.map +1 -0
- package/dist/src/mutation/make-hook.d.mts +25 -0
- package/dist/src/mutation/make-hook.d.mts.map +1 -0
- package/dist/src/mutation/types.d.mts +50 -0
- package/dist/src/mutation/types.d.mts.map +1 -0
- package/dist/src/query/index.d.mts +5 -0
- package/dist/src/query/index.d.mts.map +1 -0
- package/dist/src/query/key-creator.d.mts +17 -0
- package/dist/src/query/key-creator.d.mts.map +1 -0
- package/dist/src/query/make-infinite-options.d.mts +23 -0
- package/dist/src/query/make-infinite-options.d.mts.map +1 -0
- package/dist/src/query/make-options.d.mts +24 -0
- package/dist/src/query/make-options.d.mts.map +1 -0
- package/dist/src/query/types.d.mts +83 -0
- package/dist/src/query/types.d.mts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/lib/_tsup-dts-rollup.d.mts +500 -250
- package/lib/_tsup-dts-rollup.d.ts +500 -250
- package/lib/index.d.mts +26 -19
- package/lib/index.d.ts +26 -19
- package/lib/index.js +92 -66
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +92 -68
- package/lib/index.mjs.map +1 -1
- package/package.json +7 -7
- package/project.json +9 -1
- package/src/__tests__/client-type-check.spec.mts +1 -1
- package/src/__tests__/declare-client.spec.mts +1 -1
- package/src/__tests__/make-mutation.spec.mts +1 -1
- package/src/__tests__/makeDataTag.spec.mts +1 -1
- package/src/__tests__/makeInfiniteQueryOptions.spec.mts +1 -2
- package/src/__tests__/makeQueryOptions.spec.mts +1 -1
- package/src/{declare-client.mts → client/declare-client.mts} +93 -33
- package/src/client/index.mts +2 -0
- package/src/{types/client-instance.mts → client/types.mts} +429 -145
- package/src/common/index.mts +1 -0
- package/src/common/types.mts +31 -0
- package/src/index.mts +28 -8
- package/src/mutation/index.mts +3 -0
- package/src/mutation/key-creator.mts +64 -0
- package/src/{make-mutation.mts → mutation/make-hook.mts} +19 -8
- package/src/mutation/types.mts +106 -0
- package/src/query/index.mts +4 -0
- package/src/{utils/query-key-creator.mts → query/key-creator.mts} +22 -48
- package/src/{make-infinite-query-options.mts → query/make-infinite-options.mts} +28 -14
- package/src/{make-query-options.mts → query/make-options.mts} +26 -25
- package/src/query/types.mts +163 -0
- package/dist/tsdown.config.d.mts +0 -3
- package/dist/tsdown.config.d.mts.map +0 -1
- package/dist/tsup.config.d.mts +0 -3
- package/dist/tsup.config.d.mts.map +0 -1
- package/dist/vitest.config.d.mts +0 -3
- package/dist/vitest.config.d.mts.map +0 -1
- package/src/types/client-endpoint-helper.mts +0 -29
- package/src/types/index.mts +0 -7
- package/src/types/mutation-args.mts +0 -10
- package/src/types/mutation-helpers.mts +0 -13
- package/src/types/query-args.mts +0 -8
- package/src/types/query-helpers.mts +0 -34
- package/src/types/query-url-params-args.mts +0 -6
- package/src/types.mts +0 -118
- package/src/utils/mutation-key.creator.mts +0 -65
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './types.mjs'
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { BuilderInstance } from '@navios/builder'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Splits a string by a delimiter into a tuple type.
|
|
5
|
+
* Used for parsing URL paths into segments for query keys.
|
|
6
|
+
*/
|
|
7
|
+
export type Split<S extends string, D extends string> = string extends S
|
|
8
|
+
? string[]
|
|
9
|
+
: S extends ''
|
|
10
|
+
? []
|
|
11
|
+
: S extends `${infer T}${D}${infer U}`
|
|
12
|
+
? [T, ...Split<U, D>]
|
|
13
|
+
: [S]
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Function type for processing API responses before returning to the caller.
|
|
17
|
+
*/
|
|
18
|
+
export type ProcessResponseFunction<TData = unknown, TVariables = unknown> = (
|
|
19
|
+
variables: TVariables,
|
|
20
|
+
) => Promise<TData> | TData
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Options for creating a client instance.
|
|
24
|
+
*/
|
|
25
|
+
export type ClientOptions = {
|
|
26
|
+
api: BuilderInstance
|
|
27
|
+
defaults?: {
|
|
28
|
+
keyPrefix?: string[]
|
|
29
|
+
keySuffix?: string[]
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/index.mts
CHANGED
|
@@ -1,8 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
// Main module exports - organized by domain
|
|
2
|
+
export * from './common/index.mjs'
|
|
3
|
+
export * from './query/index.mjs'
|
|
4
|
+
export * from './mutation/index.mjs'
|
|
5
|
+
export * from './client/index.mjs'
|
|
6
|
+
|
|
7
|
+
// ============================================================================
|
|
8
|
+
// BACKWARDS COMPATIBILITY ALIASES
|
|
9
|
+
// ============================================================================
|
|
10
|
+
// These exports maintain compatibility with the previous flat structure.
|
|
11
|
+
// They are deprecated and will be removed in a future major version.
|
|
12
|
+
|
|
13
|
+
// Re-export old function names
|
|
14
|
+
export { makeQueryOptions } from './query/make-options.mjs'
|
|
15
|
+
export { makeInfiniteQueryOptions } from './query/make-infinite-options.mjs'
|
|
16
|
+
export { makeMutation } from './mutation/make-hook.mjs'
|
|
17
|
+
|
|
18
|
+
// Re-export legacy key creator names
|
|
19
|
+
export {
|
|
20
|
+
createQueryKey,
|
|
21
|
+
/** @deprecated Use createQueryKey instead */
|
|
22
|
+
queryKeyCreator,
|
|
23
|
+
} from './query/key-creator.mjs'
|
|
24
|
+
export {
|
|
25
|
+
createMutationKey,
|
|
26
|
+
/** @deprecated Use createMutationKey instead */
|
|
27
|
+
mutationKeyCreator,
|
|
28
|
+
} from './mutation/key-creator.mjs'
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AnyEndpointConfig,
|
|
3
|
+
UrlHasParams,
|
|
4
|
+
UrlParams,
|
|
5
|
+
} from '@navios/builder'
|
|
6
|
+
import type { DataTag } from '@tanstack/react-query'
|
|
7
|
+
|
|
8
|
+
import type { QueryParams } from '../query/types.mjs'
|
|
9
|
+
|
|
10
|
+
import { createQueryKey } from '../query/key-creator.mjs'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Creates a mutation key generator for a given endpoint configuration.
|
|
14
|
+
*
|
|
15
|
+
* @param config - The endpoint configuration
|
|
16
|
+
* @param options - Optional query parameters with a default `processResponse` function
|
|
17
|
+
* @returns A function that generates mutation keys
|
|
18
|
+
*
|
|
19
|
+
* @example Basic usage:
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const createMutationKey = createMutationKey(endpoint.config);
|
|
22
|
+
* const mutationKey = createMutationKey({ urlParams: { id: 123 } });
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example Advanced usage with processResponse:
|
|
26
|
+
* ```ts
|
|
27
|
+
* const createMutationKey = createMutationKey(endpoint.config, {
|
|
28
|
+
* processResponse: (data) => {
|
|
29
|
+
* if (!data.success) {
|
|
30
|
+
* throw new Error(data.message);
|
|
31
|
+
* }
|
|
32
|
+
* return data.data;
|
|
33
|
+
* },
|
|
34
|
+
* });
|
|
35
|
+
* // We create a mutation that will be shared across the project for all passed userId
|
|
36
|
+
* const mutationKey = createMutationKey({ urlParams: { projectId: 123, userId: 'wildcard' } });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function createMutationKey<
|
|
40
|
+
Config extends AnyEndpointConfig,
|
|
41
|
+
Options extends QueryParams<Config>,
|
|
42
|
+
Url extends Config['url'] = Config['url'],
|
|
43
|
+
HasParams extends UrlHasParams<Url> = UrlHasParams<Url>,
|
|
44
|
+
>(
|
|
45
|
+
config: Config,
|
|
46
|
+
options: Options = {
|
|
47
|
+
processResponse: (data) => data,
|
|
48
|
+
} as Options,
|
|
49
|
+
): (
|
|
50
|
+
params: HasParams extends true ? { urlParams: UrlParams<Url> } : {},
|
|
51
|
+
) => Options['processResponse'] extends (...args: unknown[]) => infer Result
|
|
52
|
+
? DataTag<[Config['url']], Result, Error>
|
|
53
|
+
: never {
|
|
54
|
+
const queryKey = createQueryKey(config, options, false)
|
|
55
|
+
|
|
56
|
+
// @ts-expect-error We have correct types in return type
|
|
57
|
+
return (params) => {
|
|
58
|
+
return queryKey.filterKey(params)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Legacy export for backwards compatibility
|
|
63
|
+
/** @deprecated Use createMutationKey instead */
|
|
64
|
+
export const mutationKeyCreator = createMutationKey
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AbstractEndpoint,
|
|
3
3
|
AnyEndpointConfig,
|
|
4
|
+
NaviosZodRequest,
|
|
4
5
|
UrlHasParams,
|
|
5
6
|
UrlParams,
|
|
6
7
|
} from '@navios/builder'
|
|
@@ -13,20 +14,30 @@ import {
|
|
|
13
14
|
useQueryClient,
|
|
14
15
|
} from '@tanstack/react-query'
|
|
15
16
|
|
|
16
|
-
import type {
|
|
17
|
+
import type { MutationParams } from './types.mjs'
|
|
17
18
|
|
|
18
|
-
import {
|
|
19
|
+
import { createMutationKey } from './key-creator.mjs'
|
|
19
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Creates a mutation hook for a given endpoint.
|
|
23
|
+
*
|
|
24
|
+
* Returns a function that when called returns a TanStack Query mutation result.
|
|
25
|
+
* The returned function also has helper methods attached (mutationKey, useIsMutating).
|
|
26
|
+
*
|
|
27
|
+
* @param endpoint - The navios endpoint to create a mutation hook for
|
|
28
|
+
* @param options - Mutation configuration including processResponse and callbacks
|
|
29
|
+
* @returns A hook function that returns mutation result with attached helpers
|
|
30
|
+
*/
|
|
20
31
|
export function makeMutation<
|
|
21
32
|
Config extends AnyEndpointConfig,
|
|
22
33
|
TData = unknown,
|
|
23
|
-
TVariables extends
|
|
34
|
+
TVariables extends NaviosZodRequest<Config> = NaviosZodRequest<Config>,
|
|
24
35
|
TResponse = z.output<Config['responseSchema']>,
|
|
25
36
|
TContext = unknown,
|
|
26
37
|
UseKey extends boolean = false,
|
|
27
38
|
>(
|
|
28
39
|
endpoint: AbstractEndpoint<Config>,
|
|
29
|
-
options:
|
|
40
|
+
options: MutationParams<
|
|
30
41
|
Config,
|
|
31
42
|
TData,
|
|
32
43
|
TVariables,
|
|
@@ -37,22 +48,22 @@ export function makeMutation<
|
|
|
37
48
|
) {
|
|
38
49
|
const config = endpoint.config
|
|
39
50
|
|
|
40
|
-
const mutationKey =
|
|
51
|
+
const mutationKey = createMutationKey(config, options)
|
|
41
52
|
const result = (
|
|
42
53
|
keyParams: UseKey extends true
|
|
43
54
|
? UrlHasParams<Config['url']> extends true
|
|
44
55
|
? UrlParams<Config['url']>
|
|
45
56
|
: never
|
|
46
57
|
: never,
|
|
47
|
-
): UseMutationResult<TData, Error,
|
|
58
|
+
): UseMutationResult<TData, Error, NaviosZodRequest<Config>> => {
|
|
48
59
|
const queryClient = useQueryClient()
|
|
49
60
|
const {
|
|
50
61
|
useKey,
|
|
51
62
|
useContext,
|
|
52
63
|
onError,
|
|
53
64
|
onSuccess,
|
|
54
|
-
keyPrefix,
|
|
55
|
-
keySuffix,
|
|
65
|
+
keyPrefix: _keyPrefix,
|
|
66
|
+
keySuffix: _keySuffix,
|
|
56
67
|
processResponse,
|
|
57
68
|
...rest
|
|
58
69
|
} = options
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AnyEndpointConfig,
|
|
3
|
+
NaviosZodRequest,
|
|
4
|
+
UrlHasParams,
|
|
5
|
+
UrlParams,
|
|
6
|
+
} from '@navios/builder'
|
|
7
|
+
import type { DataTag, QueryClient, UseMutationOptions } from '@tanstack/react-query'
|
|
8
|
+
import type { z, ZodObject } from 'zod/v4'
|
|
9
|
+
|
|
10
|
+
import type { ProcessResponseFunction } from '../common/types.mjs'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Arguments for mutation functions based on URL params, request schema, and query schema.
|
|
14
|
+
*/
|
|
15
|
+
export type MutationArgs<
|
|
16
|
+
Url extends string = string,
|
|
17
|
+
RequestSchema = unknown,
|
|
18
|
+
QuerySchema = unknown,
|
|
19
|
+
> = (UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {}) &
|
|
20
|
+
(RequestSchema extends ZodObject ? { data: z.input<RequestSchema> } : {}) &
|
|
21
|
+
(QuerySchema extends ZodObject ? { params: z.input<QuerySchema> } : {})
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Helper methods attached to mutation hooks.
|
|
25
|
+
*/
|
|
26
|
+
export type MutationHelpers<Url extends string, Result = unknown> =
|
|
27
|
+
UrlHasParams<Url> extends true
|
|
28
|
+
? {
|
|
29
|
+
mutationKey: (params: UrlParams<Url>) => DataTag<[Url], Result, Error>
|
|
30
|
+
useIsMutating: (keyParams: UrlParams<Url>) => boolean
|
|
31
|
+
}
|
|
32
|
+
: {
|
|
33
|
+
mutationKey: () => DataTag<[Url], Result, Error>
|
|
34
|
+
useIsMutating: () => boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Base parameters for mutation configuration.
|
|
39
|
+
*/
|
|
40
|
+
export interface MutationParams<
|
|
41
|
+
Config extends AnyEndpointConfig,
|
|
42
|
+
TData = unknown,
|
|
43
|
+
TVariables = NaviosZodRequest<Config>,
|
|
44
|
+
TResponse = z.output<Config['responseSchema']>,
|
|
45
|
+
TContext = unknown,
|
|
46
|
+
UseKey extends boolean = false,
|
|
47
|
+
> extends Omit<
|
|
48
|
+
UseMutationOptions<TData, Error, TVariables>,
|
|
49
|
+
'mutationKey' | 'mutationFn' | 'onSuccess' | 'onError' | 'scope'
|
|
50
|
+
> {
|
|
51
|
+
processResponse: ProcessResponseFunction<TData, TResponse>
|
|
52
|
+
/**
|
|
53
|
+
* React hooks that will prepare the context for the mutation onSuccess and onError
|
|
54
|
+
* callbacks. This is useful for when you want to use the context in the callbacks
|
|
55
|
+
*/
|
|
56
|
+
useContext?: () => TContext
|
|
57
|
+
onSuccess?: (
|
|
58
|
+
queryClient: QueryClient,
|
|
59
|
+
data: TData,
|
|
60
|
+
variables: TVariables,
|
|
61
|
+
context: TContext,
|
|
62
|
+
) => void | Promise<void>
|
|
63
|
+
onError?: (
|
|
64
|
+
queryClient: QueryClient,
|
|
65
|
+
err: unknown,
|
|
66
|
+
variables: TVariables,
|
|
67
|
+
context: TContext,
|
|
68
|
+
) => void | Promise<void>
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* If true, we will create a mutation key that can be shared across the project.
|
|
72
|
+
*/
|
|
73
|
+
useKey?: UseKey
|
|
74
|
+
keyPrefix?: UseKey extends true
|
|
75
|
+
? UrlHasParams<Config['url']> extends true
|
|
76
|
+
? string[]
|
|
77
|
+
: never
|
|
78
|
+
: never
|
|
79
|
+
keySuffix?: UseKey extends true
|
|
80
|
+
? UrlHasParams<Config['url']> extends true
|
|
81
|
+
? string[]
|
|
82
|
+
: never
|
|
83
|
+
: never
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Legacy type aliases for backwards compatibility
|
|
87
|
+
/** @deprecated Use MutationArgs instead */
|
|
88
|
+
export type ClientMutationArgs<
|
|
89
|
+
Url extends string = string,
|
|
90
|
+
RequestSchema = unknown,
|
|
91
|
+
QuerySchema = unknown,
|
|
92
|
+
> = MutationArgs<Url, RequestSchema, QuerySchema>
|
|
93
|
+
|
|
94
|
+
/** @deprecated Use MutationParams instead */
|
|
95
|
+
export type BaseMutationParams<
|
|
96
|
+
Config extends AnyEndpointConfig,
|
|
97
|
+
TData = unknown,
|
|
98
|
+
TVariables = NaviosZodRequest<Config>,
|
|
99
|
+
TResponse = z.output<Config['responseSchema']>,
|
|
100
|
+
TContext = unknown,
|
|
101
|
+
UseKey extends boolean = false,
|
|
102
|
+
> = MutationParams<Config, TData, TVariables, TResponse, TContext, UseKey>
|
|
103
|
+
|
|
104
|
+
/** @deprecated Use NaviosZodRequest from @navios/builder instead */
|
|
105
|
+
export type BaseMutationArgs<Config extends AnyEndpointConfig> =
|
|
106
|
+
NaviosZodRequest<Config>
|
|
@@ -1,62 +1,32 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AnyEndpointConfig,
|
|
3
|
-
UrlHasParams,
|
|
4
|
-
UrlParams,
|
|
5
|
-
} from '@navios/builder'
|
|
1
|
+
import type { AnyEndpointConfig, UrlHasParams } from '@navios/builder'
|
|
6
2
|
import type { DataTag, InfiniteData } from '@tanstack/react-query'
|
|
7
|
-
import type { z, ZodObject } from 'zod/v4'
|
|
8
3
|
|
|
9
4
|
import { bindUrlParams } from '@navios/builder'
|
|
10
5
|
|
|
11
|
-
import type {
|
|
6
|
+
import type { Split } from '../common/types.mjs'
|
|
7
|
+
import type { QueryKeyCreatorResult, QueryParams } from './types.mjs'
|
|
12
8
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
IsInfinite extends boolean = false,
|
|
26
|
-
HasParams extends UrlHasParams<Url> = UrlHasParams<Url>,
|
|
27
|
-
> = {
|
|
28
|
-
template: Split<Url, '/'>
|
|
29
|
-
dataTag: (
|
|
30
|
-
params: (HasParams extends true ? { urlParams: UrlParams<Url> } : {}) &
|
|
31
|
-
(QuerySchema extends ZodObject ? { params: z.input<QuerySchema> } : {}),
|
|
32
|
-
) => DataTag<
|
|
33
|
-
Split<Url, '/'>,
|
|
34
|
-
IsInfinite extends true ? InfiniteData<Result> : Result,
|
|
35
|
-
Error
|
|
36
|
-
>
|
|
37
|
-
filterKey: (
|
|
38
|
-
params: HasParams extends true ? { urlParams: UrlParams<Url> } : {},
|
|
39
|
-
) => DataTag<
|
|
40
|
-
Split<Url, '/'>,
|
|
41
|
-
IsInfinite extends true ? InfiniteData<Result> : Result,
|
|
42
|
-
Error
|
|
43
|
-
>
|
|
44
|
-
bindToUrl: (
|
|
45
|
-
params: (HasParams extends true ? { urlParams: UrlParams<Url> } : {}) &
|
|
46
|
-
(QuerySchema extends ZodObject ? { params: z.infer<QuerySchema> } : {}),
|
|
47
|
-
) => string
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function queryKeyCreator<
|
|
9
|
+
/**
|
|
10
|
+
* Creates a query key generator for a given endpoint configuration.
|
|
11
|
+
*
|
|
12
|
+
* The returned object provides methods to generate query keys that can be used
|
|
13
|
+
* with TanStack Query for caching, invalidation, and data tagging.
|
|
14
|
+
*
|
|
15
|
+
* @param config - The endpoint configuration
|
|
16
|
+
* @param options - Query parameters including processResponse and key prefix/suffix
|
|
17
|
+
* @param isInfinite - Whether this is for an infinite query
|
|
18
|
+
* @returns An object with methods to generate query keys
|
|
19
|
+
*/
|
|
20
|
+
export function createQueryKey<
|
|
51
21
|
Config extends AnyEndpointConfig,
|
|
52
|
-
Options extends
|
|
22
|
+
Options extends QueryParams<Config>,
|
|
53
23
|
IsInfinite extends boolean,
|
|
54
24
|
Url extends Config['url'] = Config['url'],
|
|
55
25
|
HasParams extends UrlHasParams<Url> = UrlHasParams<Url>,
|
|
56
26
|
>(
|
|
57
27
|
config: Config,
|
|
58
28
|
options: Options,
|
|
59
|
-
|
|
29
|
+
_isInfinite: IsInfinite,
|
|
60
30
|
): QueryKeyCreatorResult<
|
|
61
31
|
Config['querySchema'],
|
|
62
32
|
Url,
|
|
@@ -119,7 +89,11 @@ export function queryKeyCreator<
|
|
|
119
89
|
},
|
|
120
90
|
|
|
121
91
|
bindToUrl: (params) => {
|
|
122
|
-
return bindUrlParams<Url>(url, params ?? ({} as
|
|
92
|
+
return bindUrlParams<Url>(url, params ?? ({} as never))
|
|
123
93
|
},
|
|
124
94
|
}
|
|
125
95
|
}
|
|
96
|
+
|
|
97
|
+
// Legacy export for backwards compatibility
|
|
98
|
+
/** @deprecated Use createQueryKey instead */
|
|
99
|
+
export const queryKeyCreator = createQueryKey
|
|
@@ -17,11 +17,21 @@ import {
|
|
|
17
17
|
useSuspenseInfiniteQuery,
|
|
18
18
|
} from '@tanstack/react-query'
|
|
19
19
|
|
|
20
|
-
import type { InfiniteQueryOptions } from './types.mjs'
|
|
21
|
-
import type { ClientQueryArgs } from './types/index.mjs'
|
|
20
|
+
import type { InfiniteQueryOptions, QueryArgs } from './types.mjs'
|
|
22
21
|
|
|
23
|
-
import {
|
|
22
|
+
import { createQueryKey } from './key-creator.mjs'
|
|
24
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Creates infinite query options for a given endpoint.
|
|
26
|
+
*
|
|
27
|
+
* Returns a function that generates TanStack Query infinite options when called with params.
|
|
28
|
+
* The returned function also has helper methods attached (use, useSuspense, invalidate, etc.)
|
|
29
|
+
*
|
|
30
|
+
* @param endpoint - The navios endpoint to create infinite query options for
|
|
31
|
+
* @param options - Infinite query configuration including processResponse and pagination params
|
|
32
|
+
* @param baseQuery - Optional base query options to merge
|
|
33
|
+
* @returns A function that generates infinite query options with attached helpers
|
|
34
|
+
*/
|
|
25
35
|
export function makeInfiniteQueryOptions<
|
|
26
36
|
Config extends AnyEndpointConfig,
|
|
27
37
|
Options extends InfiniteQueryOptions<Config>,
|
|
@@ -40,11 +50,11 @@ export function makeInfiniteQueryOptions<
|
|
|
40
50
|
baseQuery: BaseQuery = {} as BaseQuery,
|
|
41
51
|
) {
|
|
42
52
|
const config = endpoint.config
|
|
43
|
-
const queryKey =
|
|
53
|
+
const queryKey = createQueryKey(config, options, true)
|
|
44
54
|
|
|
45
55
|
const processResponse = options.processResponse
|
|
46
56
|
const res = (
|
|
47
|
-
params:
|
|
57
|
+
params: QueryArgs<Config['url'], Config['querySchema']>,
|
|
48
58
|
): Options['processResponse'] extends (...args: any[]) => infer Result
|
|
49
59
|
? UseSuspenseInfiniteQueryOptions<
|
|
50
60
|
Result,
|
|
@@ -56,9 +66,8 @@ export function makeInfiniteQueryOptions<
|
|
|
56
66
|
: never => {
|
|
57
67
|
// @ts-expect-error TS2322 We know that the processResponse is defined
|
|
58
68
|
return infiniteQueryOptions({
|
|
59
|
-
// @ts-expect-error TS2322 We know the type
|
|
60
69
|
queryKey: queryKey.dataTag(params),
|
|
61
|
-
queryFn: async ({ signal, pageParam }) => {
|
|
70
|
+
queryFn: async ({ signal, pageParam }): Promise<ReturnType<Options['processResponse']>> => {
|
|
62
71
|
let result
|
|
63
72
|
try {
|
|
64
73
|
result = await endpoint({
|
|
@@ -77,9 +86,10 @@ export function makeInfiniteQueryOptions<
|
|
|
77
86
|
throw err
|
|
78
87
|
}
|
|
79
88
|
|
|
80
|
-
return processResponse(result)
|
|
89
|
+
return processResponse(result) as ReturnType<Options['processResponse']>
|
|
81
90
|
},
|
|
82
91
|
getNextPageParam: options.getNextPageParam,
|
|
92
|
+
getPreviousPageParam: options.getPreviousPageParam,
|
|
83
93
|
initialPageParam:
|
|
84
94
|
options.initialPageParam ??
|
|
85
95
|
config.querySchema.parse('params' in params ? params.params : {}),
|
|
@@ -88,24 +98,28 @@ export function makeInfiniteQueryOptions<
|
|
|
88
98
|
}
|
|
89
99
|
res.queryKey = queryKey
|
|
90
100
|
|
|
91
|
-
res.use = (params:
|
|
101
|
+
res.use = (params: QueryArgs<Config['url'], Config['querySchema']>) => {
|
|
92
102
|
return useInfiniteQuery(res(params))
|
|
93
103
|
}
|
|
94
104
|
|
|
95
|
-
res.useSuspense = (params:
|
|
105
|
+
res.useSuspense = (params: QueryArgs<Config['url'], Config['querySchema']>) => {
|
|
96
106
|
return useSuspenseInfiniteQuery(res(params))
|
|
97
107
|
}
|
|
98
108
|
|
|
99
|
-
res.invalidate = (
|
|
109
|
+
res.invalidate = (
|
|
110
|
+
queryClient: QueryClient,
|
|
111
|
+
params: QueryArgs<Config['url'], Config['querySchema']>,
|
|
112
|
+
) => {
|
|
100
113
|
return queryClient.invalidateQueries({
|
|
101
|
-
// @ts-expect-error We add additional function to the result
|
|
102
114
|
queryKey: res.queryKey.dataTag(params),
|
|
103
115
|
})
|
|
104
116
|
}
|
|
105
117
|
|
|
106
|
-
res.invalidateAll = (
|
|
118
|
+
res.invalidateAll = (
|
|
119
|
+
queryClient: QueryClient,
|
|
120
|
+
params: QueryArgs<Config['url'], Config['querySchema']>,
|
|
121
|
+
) => {
|
|
107
122
|
return queryClient.invalidateQueries({
|
|
108
|
-
// @ts-expect-error We add additional function to the result
|
|
109
123
|
queryKey: res.queryKey.filterKey(params),
|
|
110
124
|
exact: false,
|
|
111
125
|
})
|
|
@@ -8,22 +8,25 @@ import type {
|
|
|
8
8
|
|
|
9
9
|
import { queryOptions, useQuery, useSuspenseQuery } from '@tanstack/react-query'
|
|
10
10
|
|
|
11
|
-
import type {
|
|
12
|
-
import type {
|
|
11
|
+
import type { Split } from '../common/types.mjs'
|
|
12
|
+
import type { QueryArgs, QueryParams } from './types.mjs'
|
|
13
13
|
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
type Split<S extends string, D extends string> = string extends S
|
|
17
|
-
? string[]
|
|
18
|
-
: S extends ''
|
|
19
|
-
? []
|
|
20
|
-
: S extends `${infer T}${D}${infer U}`
|
|
21
|
-
? [T, ...Split<U, D>]
|
|
22
|
-
: [S]
|
|
14
|
+
import { createQueryKey } from './key-creator.mjs'
|
|
23
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Creates query options for a given endpoint.
|
|
18
|
+
*
|
|
19
|
+
* Returns a function that generates TanStack Query options when called with params.
|
|
20
|
+
* The returned function also has helper methods attached (use, useSuspense, invalidate, etc.)
|
|
21
|
+
*
|
|
22
|
+
* @param endpoint - The navios endpoint to create query options for
|
|
23
|
+
* @param options - Query configuration including processResponse
|
|
24
|
+
* @param baseQuery - Optional base query options to merge
|
|
25
|
+
* @returns A function that generates query options with attached helpers
|
|
26
|
+
*/
|
|
24
27
|
export function makeQueryOptions<
|
|
25
28
|
Config extends AnyEndpointConfig,
|
|
26
|
-
Options extends
|
|
29
|
+
Options extends QueryParams<Config>,
|
|
27
30
|
BaseQuery extends Omit<
|
|
28
31
|
UseQueryOptions<ReturnType<Options['processResponse']>, Error, any>,
|
|
29
32
|
| 'queryKey'
|
|
@@ -40,12 +43,11 @@ export function makeQueryOptions<
|
|
|
40
43
|
baseQuery: BaseQuery = {} as BaseQuery,
|
|
41
44
|
) {
|
|
42
45
|
const config = endpoint.config
|
|
43
|
-
|
|
44
|
-
const queryKey = queryKeyCreator(config, options, false)
|
|
46
|
+
const queryKey = createQueryKey(config, options, false)
|
|
45
47
|
const processResponse = options.processResponse
|
|
46
48
|
|
|
47
49
|
const result = (
|
|
48
|
-
params:
|
|
50
|
+
params: QueryArgs<Config['url'], Config['querySchema']>,
|
|
49
51
|
): Options['processResponse'] extends (...args: any[]) => infer Result
|
|
50
52
|
? UseSuspenseQueryOptions<
|
|
51
53
|
Result,
|
|
@@ -57,7 +59,7 @@ export function makeQueryOptions<
|
|
|
57
59
|
// @ts-expect-error TS2322 We know that the processResponse is defined
|
|
58
60
|
return queryOptions({
|
|
59
61
|
queryKey: queryKey.dataTag(params),
|
|
60
|
-
queryFn: async ({ signal }) => {
|
|
62
|
+
queryFn: async ({ signal }): Promise<ReturnType<Options['processResponse']>> => {
|
|
61
63
|
let result
|
|
62
64
|
try {
|
|
63
65
|
result = await endpoint({
|
|
@@ -71,35 +73,34 @@ export function makeQueryOptions<
|
|
|
71
73
|
throw err
|
|
72
74
|
}
|
|
73
75
|
|
|
74
|
-
return processResponse(result)
|
|
76
|
+
return processResponse(result) as ReturnType<Options['processResponse']>
|
|
75
77
|
},
|
|
76
78
|
...baseQuery,
|
|
77
79
|
})
|
|
78
80
|
}
|
|
79
81
|
result.queryKey = queryKey
|
|
80
|
-
result.use = (params:
|
|
81
|
-
// @ts-expect-error We add additional function to the result
|
|
82
|
+
result.use = (params: QueryArgs<Config['url'], Config['querySchema']>) => {
|
|
82
83
|
return useQuery(result(params))
|
|
83
84
|
}
|
|
84
85
|
|
|
85
|
-
result.useSuspense = (params:
|
|
86
|
-
// @ts-expect-error We add additional function to the result
|
|
86
|
+
result.useSuspense = (params: QueryArgs<Config['url'], Config['querySchema']>) => {
|
|
87
87
|
return useSuspenseQuery(result(params))
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
result.invalidate = (
|
|
90
|
+
result.invalidate = (
|
|
91
|
+
queryClient: QueryClient,
|
|
92
|
+
params: QueryArgs<Config['url'], Config['querySchema']>,
|
|
93
|
+
) => {
|
|
91
94
|
return queryClient.invalidateQueries({
|
|
92
|
-
// @ts-expect-error We add additional function to the result
|
|
93
95
|
queryKey: result.queryKey.dataTag(params),
|
|
94
96
|
})
|
|
95
97
|
}
|
|
96
98
|
|
|
97
99
|
result.invalidateAll = (
|
|
98
100
|
queryClient: QueryClient,
|
|
99
|
-
params:
|
|
101
|
+
params: QueryArgs<Config['url'], Config['querySchema']>,
|
|
100
102
|
) => {
|
|
101
103
|
return queryClient.invalidateQueries({
|
|
102
|
-
// @ts-expect-error We add additional function to the result
|
|
103
104
|
queryKey: result.queryKey.filterKey(params),
|
|
104
105
|
exact: false,
|
|
105
106
|
})
|