@tuyau/react-query 1.0.0 → 1.1.1
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/index.d.ts +61 -86
- package/build/index.js +16 -109
- package/package.json +16 -12
package/build/index.d.ts
CHANGED
|
@@ -1,62 +1,72 @@
|
|
|
1
|
-
import { SchemaEndpoint, RawRequestArgs,
|
|
1
|
+
import { SchemaEndpoint, ResponseOf, RawRequestArgs, ErrorOf, TuyauRegistry, InferTree, AdonisEndpoint, InferRoutes } from '@tuyau/core/types';
|
|
2
2
|
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
3
|
-
import { SkipToken, DataTag, QueryFilters,
|
|
3
|
+
import { SkipToken, DataTag, QueryFilters, UseMutationOptions, InfiniteData, UnusedSkipTokenInfiniteOptions, DefinedInitialDataInfiniteOptions, UndefinedInitialDataInfiniteOptions, DefinedInitialDataOptions, UnusedSkipTokenOptions, UndefinedInitialDataOptions } from '@tanstack/react-query';
|
|
4
|
+
import { TuyauQueryKey, WithRequired, DistributiveOmit, TuyauQueryBaseOptions, TuyauMutationKey, TuyauRequestOptions, DecorateRouterKeyable, CreateQueryFnOptions } from '@tuyau/query-core';
|
|
5
|
+
export { DecorateRouterKeyable, DistributiveOmit, QueryType, TuyauMutationKey, TuyauQueryBaseOptions, TuyauQueryKey, WithRequired } from '@tuyau/query-core';
|
|
4
6
|
import { Tuyau } from '@tuyau/core/client';
|
|
5
7
|
|
|
6
|
-
type Response$2<E extends SchemaEndpoint> = E['types']['response'];
|
|
7
8
|
/**
|
|
8
9
|
* Decorate query endpoints with Tanstack queries abilities
|
|
9
10
|
*/
|
|
10
11
|
interface DecorateQueryFn<EDef extends SchemaEndpoint> {
|
|
11
12
|
queryOptions: TuyauReactQueryOptions<EDef>;
|
|
12
|
-
queryKey: (args?: RawRequestArgs<EDef>) => DataTag<TuyauQueryKey,
|
|
13
|
-
queryFilter: (args?: RawRequestArgs<EDef>, filters?: QueryFilters<DataTag<TuyauQueryKey,
|
|
13
|
+
queryKey: (args?: RawRequestArgs<EDef>) => DataTag<TuyauQueryKey, ResponseOf<EDef>>;
|
|
14
|
+
queryFilter: (args?: RawRequestArgs<EDef>, filters?: QueryFilters<DataTag<TuyauQueryKey, ResponseOf<EDef>>>) => WithRequired<QueryFilters<DataTag<TuyauQueryKey, ResponseOf<EDef>>>, 'queryKey'>;
|
|
14
15
|
}
|
|
15
16
|
/**
|
|
16
17
|
* Type definition for query options with overloads for different scenarios
|
|
17
18
|
*/
|
|
18
19
|
interface TuyauReactQueryOptions<EDef extends SchemaEndpoint> {
|
|
19
|
-
<TData =
|
|
20
|
-
<TData =
|
|
21
|
-
(): UnusedSkipTokenTuyauQueryOptionsOut<
|
|
22
|
-
(input: SkipToken): UndefinedTuyauQueryOptionsOut<
|
|
23
|
-
<TData =
|
|
20
|
+
<TData = ResponseOf<EDef>>(input: RawRequestArgs<EDef> | SkipToken, opts: DefinedTuyauQueryOptionsIn<ResponseOf<EDef>, TData, ErrorOf<EDef>>): DefinedTuyauQueryOptionsOut<ResponseOf<EDef>, TData, ErrorOf<EDef>>;
|
|
21
|
+
<TData = ResponseOf<EDef>>(input: RawRequestArgs<EDef>, opts?: UnusedSkipTokenTuyauQueryOptionsIn<ResponseOf<EDef>, TData, ErrorOf<EDef>>): UnusedSkipTokenTuyauQueryOptionsOut<ResponseOf<EDef>, TData, ErrorOf<EDef>>;
|
|
22
|
+
(): UnusedSkipTokenTuyauQueryOptionsOut<ResponseOf<EDef>, ResponseOf<EDef>, ErrorOf<EDef>>;
|
|
23
|
+
(input: SkipToken): UndefinedTuyauQueryOptionsOut<ResponseOf<EDef>, ResponseOf<EDef>, ErrorOf<EDef>>;
|
|
24
|
+
<TData = ResponseOf<EDef>>(input: RawRequestArgs<EDef> | SkipToken, opts?: UndefinedTuyauQueryOptionsIn<ResponseOf<EDef>, TData, ErrorOf<EDef>>): UndefinedTuyauQueryOptionsOut<ResponseOf<EDef>, TData, ErrorOf<EDef>>;
|
|
24
25
|
}
|
|
25
26
|
|
|
27
|
+
type ReservedOptions = 'mutationKey' | 'mutationFn';
|
|
26
28
|
/**
|
|
27
|
-
*
|
|
28
|
-
|
|
29
|
-
type DistributiveOmit<TObj, TKey extends keyof any> = TObj extends any ? Omit<TObj, TKey> : never;
|
|
30
|
-
/**
|
|
31
|
-
* Make certain keys required in a type
|
|
29
|
+
* User-facing mutation options input. Extends TanStack's mutation observer
|
|
30
|
+
* options but omits `mutationKey` and `mutationFn` which are auto-generated
|
|
32
31
|
*/
|
|
33
|
-
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
34
|
-
|
|
35
|
-
type ReservedOptions = 'mutationKey' | 'mutationFn';
|
|
36
|
-
type Response$1<E extends SchemaEndpoint> = E['types']['response'];
|
|
37
32
|
interface TuyauMutationOptionsIn<TInput, TError, TOutput, TContext> extends DistributiveOmit<UseMutationOptions<TOutput, TError, TInput, TContext>, ReservedOptions>, TuyauQueryBaseOptions {
|
|
38
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Fully resolved mutation options ready to be passed to `useMutation`.
|
|
36
|
+
* Includes the auto-generated `mutationKey` and `mutationFn`
|
|
37
|
+
*/
|
|
39
38
|
interface TuyauMutationOptionsOut<TInput, TError, TOutput, TContext> extends UseMutationOptions<TOutput, TError, TInput, TContext> {
|
|
40
39
|
mutationKey: TuyauMutationKey;
|
|
41
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Callable interface exposed on mutation endpoints.
|
|
43
|
+
* Returns fully resolved mutation options from user-provided options
|
|
44
|
+
*/
|
|
42
45
|
interface TuyauReactMutationOptions<TDef extends SchemaEndpoint> {
|
|
43
|
-
<TContext = unknown>(opts?: TuyauMutationOptionsIn<RawRequestArgs<TDef>,
|
|
46
|
+
<TContext = unknown>(opts?: TuyauMutationOptionsIn<RawRequestArgs<TDef>, ErrorOf<TDef>, ResponseOf<TDef>, TContext>): TuyauMutationOptionsOut<RawRequestArgs<TDef>, ErrorOf<TDef>, ResponseOf<TDef>, TContext>;
|
|
44
47
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Decorates mutation endpoints with `mutationOptions` and `mutationKey` methods
|
|
50
|
+
*/
|
|
48
51
|
interface DecorateMutationFn<EDef extends SchemaEndpoint> {
|
|
49
52
|
mutationOptions: TuyauReactMutationOptions<EDef>;
|
|
50
53
|
mutationKey: () => TuyauMutationKey;
|
|
51
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Internal options for building a mutation options object
|
|
57
|
+
*/
|
|
52
58
|
interface TuyauMutationOptionsOptions {
|
|
53
59
|
opts?: TuyauMutationOptionsIn<any, any, any, any>;
|
|
54
60
|
routeName: string;
|
|
55
61
|
client: Tuyau<any>;
|
|
62
|
+
globalOptions?: TuyauRequestOptions;
|
|
56
63
|
}
|
|
57
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Builds a TanStack React Query `mutationOptions` object from Tuyau route information.
|
|
66
|
+
* Delegates mutationFn creation to the shared `createMutationFn` from `@tuyau/query-core`
|
|
67
|
+
*/
|
|
68
|
+
declare function tuyauMutationOptions(options: TuyauMutationOptionsOptions): _tanstack_react_query.WithRequired<UseMutationOptions<any, any, any, any>, "mutationKey">;
|
|
58
69
|
|
|
59
|
-
type Response<E extends SchemaEndpoint> = E['types']['response'];
|
|
60
70
|
type ReservedInfiniteQueryOptions = 'queryKey' | 'queryFn' | 'queryHashFn' | 'queryHash';
|
|
61
71
|
/**
|
|
62
72
|
* Base infinite query options with Tuyau-specific options
|
|
@@ -111,47 +121,29 @@ interface TuyauReactInfiniteQueryOptions<EDef extends SchemaEndpoint> {
|
|
|
111
121
|
/**
|
|
112
122
|
* Overload 1: When input is NOT SkipToken, return options compatible with useSuspenseInfiniteQuery
|
|
113
123
|
*/
|
|
114
|
-
<TData = InfiniteData<
|
|
124
|
+
<TData = InfiniteData<ResponseOf<EDef>>>(input: RawRequestArgs<EDef>, opts: UnusedSkipTokenTuyauInfiniteQueryOptionsIn<ResponseOf<EDef>, ErrorOf<EDef>, TData>): UnusedSkipTokenTuyauInfiniteQueryOptionsOut<ResponseOf<EDef>, ErrorOf<EDef>, TData>;
|
|
115
125
|
/**
|
|
116
126
|
* Overload 2: When initialData is defined
|
|
117
127
|
*/
|
|
118
|
-
<TData = InfiniteData<
|
|
128
|
+
<TData = InfiniteData<ResponseOf<EDef>>>(input: RawRequestArgs<EDef> | SkipToken, opts: DefinedTuyauInfiniteQueryOptionsIn<ResponseOf<EDef>, ErrorOf<EDef>, TData>): DefinedTuyauInfiniteQueryOptionsOut<ResponseOf<EDef>, ErrorOf<EDef>, TData>;
|
|
119
129
|
/**
|
|
120
130
|
* Overload 3: No arguments - no skipToken possible
|
|
121
131
|
*/
|
|
122
|
-
(): UnusedSkipTokenTuyauInfiniteQueryOptionsOut<
|
|
132
|
+
(): UnusedSkipTokenTuyauInfiniteQueryOptionsOut<ResponseOf<EDef>, ErrorOf<EDef>, InfiniteData<ResponseOf<EDef>>>;
|
|
123
133
|
/**
|
|
124
134
|
* Overload 4: With skipToken or conditional (request | skipToken) and options
|
|
125
135
|
*/
|
|
126
|
-
<TData = InfiniteData<
|
|
136
|
+
<TData = InfiniteData<ResponseOf<EDef>>>(input: RawRequestArgs<EDef> | SkipToken, opts: UndefinedTuyauInfiniteQueryOptionsIn<ResponseOf<EDef>, ErrorOf<EDef>, TData>): UndefinedTuyauInfiniteQueryOptionsOut<ResponseOf<EDef>, ErrorOf<EDef>, TData>;
|
|
127
137
|
}
|
|
128
138
|
/**
|
|
129
139
|
* Decorate query endpoints with infinite query capabilities
|
|
130
140
|
*/
|
|
131
141
|
interface DecorateInfiniteQueryFn<EDef extends SchemaEndpoint> {
|
|
132
142
|
infiniteQueryOptions: TuyauReactInfiniteQueryOptions<EDef>;
|
|
133
|
-
infiniteQueryKey: (args?: RawRequestArgs<EDef>) => DataTag<TuyauQueryKey, InfiniteData<
|
|
134
|
-
infiniteQueryFilter: (args?: RawRequestArgs<EDef>, filters?: QueryFilters<DataTag<TuyauQueryKey, InfiniteData<
|
|
143
|
+
infiniteQueryKey: (args?: RawRequestArgs<EDef>) => DataTag<TuyauQueryKey, InfiniteData<ResponseOf<EDef>>>;
|
|
144
|
+
infiniteQueryFilter: (args?: RawRequestArgs<EDef>, filters?: QueryFilters<DataTag<TuyauQueryKey, InfiniteData<ResponseOf<EDef>>>>) => WithRequired<QueryFilters<DataTag<TuyauQueryKey, InfiniteData<ResponseOf<EDef>>>>, 'queryKey'>;
|
|
135
145
|
}
|
|
136
146
|
|
|
137
|
-
/**
|
|
138
|
-
* Query type identifier
|
|
139
|
-
*/
|
|
140
|
-
type QueryType = 'any' | 'infinite' | 'query';
|
|
141
|
-
/**
|
|
142
|
-
* Tuyau-specific query key structure
|
|
143
|
-
*/
|
|
144
|
-
type TuyauQueryKey = [
|
|
145
|
-
readonly string[],
|
|
146
|
-
{
|
|
147
|
-
request?: RawRequestArgs<any>;
|
|
148
|
-
type?: Exclude<QueryType, 'any'>;
|
|
149
|
-
}?
|
|
150
|
-
];
|
|
151
|
-
/**
|
|
152
|
-
* Tuyau-specific mutation key structure
|
|
153
|
-
*/
|
|
154
|
-
type TuyauMutationKey = [readonly string[]];
|
|
155
147
|
/**
|
|
156
148
|
* Query options with defined initial data
|
|
157
149
|
*/
|
|
@@ -186,30 +178,9 @@ interface UnusedSkipTokenTuyauQueryOptionsOut<TQueryFnData, TData, TError> exten
|
|
|
186
178
|
queryKey: DataTag<TuyauQueryKey, TData, TError>;
|
|
187
179
|
}
|
|
188
180
|
/**
|
|
189
|
-
*
|
|
190
|
-
*/
|
|
191
|
-
interface TuyauReactRequestOptions extends BaseRequestOptions {
|
|
192
|
-
/**
|
|
193
|
-
* Opt out or into aborting request on unmount
|
|
194
|
-
*/
|
|
195
|
-
abortOnUnmount?: boolean;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Base options for Tuyau queries and mutations
|
|
199
|
-
*/
|
|
200
|
-
interface TuyauQueryBaseOptions {
|
|
201
|
-
/**
|
|
202
|
-
* Tuyau-specific options including Ky request options (timeout, retry, headers, etc.)
|
|
203
|
-
*/
|
|
204
|
-
tuyau?: TuyauReactRequestOptions;
|
|
205
|
-
}
|
|
206
|
-
interface DecorateRouterKeyable {
|
|
207
|
-
pathKey: () => TuyauQueryKey;
|
|
208
|
-
pathFilter: (filters?: QueryFilters<TuyauQueryKey>) => WithRequired<QueryFilters<TuyauQueryKey>, 'queryKey'>;
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Keys that identify an endpoint-like structure
|
|
181
|
+
* Alias for backward compatibility with existing consumers
|
|
212
182
|
*/
|
|
183
|
+
type TuyauReactRequestOptions = TuyauRequestOptions;
|
|
213
184
|
type EndpointKeys = 'methods' | 'pattern' | 'types';
|
|
214
185
|
/**
|
|
215
186
|
* Determines if endpoint is a query (GET/HEAD) or mutation
|
|
@@ -230,26 +201,30 @@ type TransformToReactQuery<T> = {
|
|
|
230
201
|
} & DecorateRouterKeyable;
|
|
231
202
|
type TuyauReactQuery<R extends Record<string, SchemaEndpoint>> = TransformToReactQuery<R>;
|
|
232
203
|
|
|
204
|
+
/**
|
|
205
|
+
* Creates a type-safe TanStack React Query client from a Tuyau client instance.
|
|
206
|
+
* Returns a Proxy-based object that mirrors the API route tree and exposes
|
|
207
|
+
* `queryOptions`, `mutationOptions`, `infiniteQueryOptions`, and key/filter
|
|
208
|
+
* helpers on each endpoint node
|
|
209
|
+
*/
|
|
233
210
|
declare function createTuyauReactQueryClient<Reg extends TuyauRegistry, Tree = InferTree<Reg>, Routes extends Record<string, AdonisEndpoint> = InferRoutes<Reg>>(options: {
|
|
234
211
|
client: Tuyau<Reg, Routes>;
|
|
235
212
|
globalOptions?: TuyauReactRequestOptions;
|
|
236
213
|
}): TransformToReactQuery<Tree>;
|
|
237
214
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
215
|
+
/**
|
|
216
|
+
* Internal options for building a TanStack Query options object
|
|
217
|
+
*/
|
|
218
|
+
type TuyauQueryOptionsOptions = CreateQueryFnOptions & {
|
|
241
219
|
queryKey: TuyauQueryKey;
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
220
|
+
};
|
|
221
|
+
/**
|
|
222
|
+
* Builds a TanStack React Query `queryOptions` object from Tuyau route information.
|
|
223
|
+
* Delegates queryFn creation to the shared `createQueryFn` from `@tuyau/query-core`
|
|
224
|
+
*/
|
|
246
225
|
declare function tuyauQueryOptions(options: TuyauQueryOptionsOptions): _tanstack_react_query.UseQueryOptions<any, Error, any, readonly unknown[]> & {
|
|
247
226
|
initialData?: any;
|
|
248
|
-
} &
|
|
249
|
-
queryKey: readonly unknown[] & {
|
|
250
|
-
[dataTagSymbol]: any;
|
|
251
|
-
[dataTagErrorSymbol]: Error;
|
|
252
|
-
};
|
|
253
|
-
};
|
|
227
|
+
} & _tanstack_react_query.QueryKeyWithDataTag<readonly unknown[], any, Error>;
|
|
254
228
|
|
|
255
|
-
export {
|
|
229
|
+
export { createTuyauReactQueryClient, tuyauMutationOptions, tuyauQueryOptions };
|
|
230
|
+
export type { DecorateMutationFn, DefinedTuyauQueryOptionsIn, DefinedTuyauQueryOptionsOut, EndpointNode, TransformToReactQuery, TuyauMutationOptionsIn, TuyauMutationOptionsOptions, TuyauMutationOptionsOut, TuyauQueryOptionsOptions, TuyauReactMutationOptions, TuyauReactQuery, TuyauReactRequestOptions, UndefinedTuyauQueryOptionsIn, UndefinedTuyauQueryOptionsOut, UnusedSkipTokenTuyauQueryOptionsIn, UnusedSkipTokenTuyauQueryOptionsOut };
|
package/build/index.js
CHANGED
|
@@ -1,101 +1,21 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
import {
|
|
3
|
-
function isObject(value) {
|
|
4
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
|
-
}
|
|
6
|
-
function invoke(fn) {
|
|
7
|
-
return fn?.();
|
|
8
|
-
}
|
|
9
|
-
function buildKey(opts) {
|
|
10
|
-
const { segments, request, type } = opts;
|
|
11
|
-
const splitPath = segments.flatMap((part) => part.split("."));
|
|
12
|
-
if (!request && type === "any") {
|
|
13
|
-
return splitPath.length ? [splitPath] : [];
|
|
14
|
-
}
|
|
15
|
-
if (type === "infinite" && isObject(request)) {
|
|
16
|
-
const query = request.query;
|
|
17
|
-
const body = request.body;
|
|
18
|
-
const hasCursorInQuery = isObject(query) && ("direction" in query || "cursor" in query);
|
|
19
|
-
const hasCursorInBody = isObject(body) && ("direction" in body || "cursor" in body);
|
|
20
|
-
if (hasCursorInQuery || hasCursorInBody) {
|
|
21
|
-
const cleanedRequest = { ...request };
|
|
22
|
-
if (hasCursorInQuery && isObject(query)) {
|
|
23
|
-
const { cursor: _, direction: __, ...cleanQuery } = query;
|
|
24
|
-
cleanedRequest.query = cleanQuery;
|
|
25
|
-
}
|
|
26
|
-
if (hasCursorInBody && isObject(body)) {
|
|
27
|
-
const { cursor: _, direction: __, ...cleanBody } = body;
|
|
28
|
-
cleanedRequest.body = cleanBody;
|
|
29
|
-
}
|
|
30
|
-
return [splitPath, { request: cleanedRequest, type: "infinite" }];
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return [
|
|
34
|
-
splitPath,
|
|
35
|
-
{
|
|
36
|
-
...typeof request !== "undefined" && request !== skipToken && { request },
|
|
37
|
-
...type && type !== "any" && { type }
|
|
38
|
-
}
|
|
39
|
-
];
|
|
40
|
-
}
|
|
41
|
-
function extractKyOptions(tuyauOpts) {
|
|
42
|
-
if (!tuyauOpts) return {};
|
|
43
|
-
const { abortOnUnmount, ...kyOptions } = tuyauOpts;
|
|
44
|
-
return kyOptions;
|
|
45
|
-
}
|
|
1
|
+
// src/main.ts
|
|
2
|
+
import { buildKey, segmentsToRouteName, getMutationKeyInternal as getMutationKeyInternal2 } from "@tuyau/query-core";
|
|
46
3
|
|
|
47
4
|
// src/query.ts
|
|
48
|
-
import { queryOptions
|
|
5
|
+
import { queryOptions } from "@tanstack/react-query";
|
|
6
|
+
import { createQueryFn } from "@tuyau/query-core";
|
|
49
7
|
function tuyauQueryOptions(options) {
|
|
50
|
-
const {
|
|
51
|
-
const queryFn =
|
|
52
|
-
|
|
53
|
-
return async (queryFnContext) => {
|
|
54
|
-
const effectiveAbortOnUnmount = opts?.tuyau?.abortOnUnmount ?? globalOptions?.abortOnUnmount ?? false;
|
|
55
|
-
const kyOptions = extractKyOptions(opts?.tuyau);
|
|
56
|
-
return await client.request(routeName, {
|
|
57
|
-
...request,
|
|
58
|
-
...kyOptions,
|
|
59
|
-
retry: 0,
|
|
60
|
-
...effectiveAbortOnUnmount ? { signal: queryFnContext.signal } : {}
|
|
61
|
-
});
|
|
62
|
-
};
|
|
63
|
-
});
|
|
64
|
-
return queryOptions({ ...opts, queryKey, queryFn });
|
|
8
|
+
const { queryKey, ...fnOptions } = options;
|
|
9
|
+
const queryFn = createQueryFn(fnOptions);
|
|
10
|
+
return queryOptions({ ...options.opts, queryKey, queryFn });
|
|
65
11
|
}
|
|
66
12
|
|
|
67
13
|
// src/infinite_query.ts
|
|
68
|
-
import { infiniteQueryOptions
|
|
14
|
+
import { infiniteQueryOptions } from "@tanstack/react-query";
|
|
15
|
+
import { createInfiniteQueryFn } from "@tuyau/query-core";
|
|
69
16
|
function tuyauInfiniteQueryOptions(options) {
|
|
70
17
|
const { request, routeName, opts, queryKey, client, globalOptions } = options;
|
|
71
|
-
const queryFn =
|
|
72
|
-
if (request === skipToken3) return skipToken3;
|
|
73
|
-
return async (queryFnContext) => {
|
|
74
|
-
const { pageParam } = queryFnContext;
|
|
75
|
-
const effectiveAbortOnUnmount = opts?.tuyau?.abortOnUnmount ?? globalOptions?.abortOnUnmount ?? false;
|
|
76
|
-
const kyOptions = extractKyOptions(opts?.tuyau);
|
|
77
|
-
const pageParamKey = opts?.pageParamKey || "page";
|
|
78
|
-
let requestArgs;
|
|
79
|
-
if (typeof request === "object" && request !== null) {
|
|
80
|
-
if (pageParam !== void 0) {
|
|
81
|
-
requestArgs = {
|
|
82
|
-
...request,
|
|
83
|
-
query: { ...request.query, [pageParamKey]: pageParam }
|
|
84
|
-
};
|
|
85
|
-
} else {
|
|
86
|
-
requestArgs = request;
|
|
87
|
-
}
|
|
88
|
-
} else {
|
|
89
|
-
requestArgs = { query: pageParam !== void 0 ? { [pageParamKey]: pageParam } : {} };
|
|
90
|
-
}
|
|
91
|
-
return await client.request(routeName, {
|
|
92
|
-
...requestArgs,
|
|
93
|
-
...kyOptions,
|
|
94
|
-
retry: 0,
|
|
95
|
-
...effectiveAbortOnUnmount ? { signal: queryFnContext.signal } : {}
|
|
96
|
-
});
|
|
97
|
-
};
|
|
98
|
-
});
|
|
18
|
+
const queryFn = createInfiniteQueryFn({ request, routeName, opts, client, globalOptions });
|
|
99
19
|
return infiniteQueryOptions({
|
|
100
20
|
...opts,
|
|
101
21
|
queryKey,
|
|
@@ -108,28 +28,15 @@ function tuyauInfiniteQueryOptions(options) {
|
|
|
108
28
|
|
|
109
29
|
// src/mutation.ts
|
|
110
30
|
import { mutationOptions } from "@tanstack/react-query";
|
|
111
|
-
|
|
112
|
-
const key = [options.segments.flatMap((part) => part.split("."))];
|
|
113
|
-
return key;
|
|
114
|
-
}
|
|
31
|
+
import { getMutationKeyInternal, createMutationFn } from "@tuyau/query-core";
|
|
115
32
|
function tuyauMutationOptions(options) {
|
|
116
|
-
const { opts, routeName, client } = options;
|
|
33
|
+
const { opts, routeName, client, globalOptions } = options;
|
|
117
34
|
const mutationKey = getMutationKeyInternal({ segments: routeName.split(".") });
|
|
118
|
-
const
|
|
119
|
-
const mutationFn = async (request) => {
|
|
120
|
-
const requestArgs = request || {};
|
|
121
|
-
return await client.request(routeName, { ...requestArgs, ...kyOptions, retry: 0 });
|
|
122
|
-
};
|
|
35
|
+
const mutationFn = createMutationFn({ opts, routeName, client, globalOptions });
|
|
123
36
|
return mutationOptions({ ...opts, mutationKey, mutationFn });
|
|
124
37
|
}
|
|
125
38
|
|
|
126
39
|
// src/main.ts
|
|
127
|
-
function toSnakeCase(str) {
|
|
128
|
-
return str.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
|
|
129
|
-
}
|
|
130
|
-
function segmentsToRouteName(segments) {
|
|
131
|
-
return segments.map(toSnakeCase).join(".");
|
|
132
|
-
}
|
|
133
40
|
function createTuyauReactQueryClient(options) {
|
|
134
41
|
const { client, globalOptions } = options;
|
|
135
42
|
function makeReactQueryNamed(segments) {
|
|
@@ -174,8 +81,8 @@ function createTuyauReactQueryClient(options) {
|
|
|
174
81
|
/**
|
|
175
82
|
* Mutations
|
|
176
83
|
*/
|
|
177
|
-
mutationOptions: (opts) => tuyauMutationOptions({ opts, client, routeName }),
|
|
178
|
-
mutationKey: () =>
|
|
84
|
+
mutationOptions: (opts) => tuyauMutationOptions({ opts, client, routeName, globalOptions }),
|
|
85
|
+
mutationKey: () => getMutationKeyInternal2({ segments }),
|
|
179
86
|
/**
|
|
180
87
|
* Paths
|
|
181
88
|
*/
|
|
@@ -187,6 +94,7 @@ function createTuyauReactQueryClient(options) {
|
|
|
187
94
|
};
|
|
188
95
|
return new Proxy(decoratedEndpoint, {
|
|
189
96
|
get: (target, prop) => {
|
|
97
|
+
if (typeof prop === "symbol") return void 0;
|
|
190
98
|
if (prop in target) return target[prop];
|
|
191
99
|
return makeReactQueryNamed([...segments, String(prop)]);
|
|
192
100
|
}
|
|
@@ -196,7 +104,6 @@ function createTuyauReactQueryClient(options) {
|
|
|
196
104
|
}
|
|
197
105
|
export {
|
|
198
106
|
createTuyauReactQueryClient,
|
|
199
|
-
getMutationKeyInternal,
|
|
200
107
|
tuyauMutationOptions,
|
|
201
108
|
tuyauQueryOptions
|
|
202
109
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tuyau/react-query",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.1.1",
|
|
5
5
|
"description": "Tanstack Query integration for Tuyau",
|
|
6
6
|
"author": "Julien Ripouteau <julien@ripouteau.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -32,20 +32,24 @@
|
|
|
32
32
|
"engines": {
|
|
33
33
|
"node": ">=24.0.0"
|
|
34
34
|
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@tuyau/query-core": "1.1.1"
|
|
37
|
+
},
|
|
35
38
|
"peerDependencies": {
|
|
36
39
|
"@tanstack/react-query": "^5.74.7",
|
|
37
40
|
"@tuyau/core": "^1.0.0-beta.9"
|
|
38
41
|
},
|
|
39
42
|
"devDependencies": {
|
|
40
|
-
"@adonisjs/core": "^7.
|
|
41
|
-
"@faker-js/faker": "^10.
|
|
42
|
-
"@happy-dom/global-registrator": "^
|
|
43
|
-
"@tanstack/react-query": "^5.
|
|
44
|
-
"@testing-library/react": "^16.3.
|
|
45
|
-
"@types/react": "^19.
|
|
46
|
-
"@vinejs/vine": "^4.
|
|
47
|
-
"
|
|
48
|
-
"
|
|
43
|
+
"@adonisjs/core": "^7.6.0",
|
|
44
|
+
"@faker-js/faker": "^10.6.0",
|
|
45
|
+
"@happy-dom/global-registrator": "^21.0.1",
|
|
46
|
+
"@tanstack/react-query": "^5.104.3",
|
|
47
|
+
"@testing-library/react": "^16.3.3",
|
|
48
|
+
"@types/react": "^19.3.0",
|
|
49
|
+
"@vinejs/vine": "^4.4.0",
|
|
50
|
+
"nock": "^15.0.1",
|
|
51
|
+
"react": "^19.3.1",
|
|
52
|
+
"@tuyau/core": "1.2.3"
|
|
49
53
|
},
|
|
50
54
|
"tsup": {
|
|
51
55
|
"entry": [
|
|
@@ -59,10 +63,10 @@
|
|
|
59
63
|
},
|
|
60
64
|
"publishConfig": {
|
|
61
65
|
"access": "public",
|
|
62
|
-
"tag": "
|
|
66
|
+
"tag": "latest"
|
|
63
67
|
},
|
|
64
68
|
"scripts": {
|
|
65
|
-
"lint": "
|
|
69
|
+
"lint": "oxlint .",
|
|
66
70
|
"typecheck": "tsc --noEmit",
|
|
67
71
|
"build": "tsup-node",
|
|
68
72
|
"test": "pnpm quick:test",
|