@geekmidas/client 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/dist/auth-fetcher.cjs +0 -1
- package/dist/auth-fetcher.cjs.map +1 -1
- package/dist/auth-fetcher.d.cts +1 -1
- package/dist/auth-fetcher.d.cts.map +1 -1
- package/dist/auth-fetcher.d.mts +1 -1
- package/dist/auth-fetcher.d.mts.map +1 -1
- package/dist/auth-fetcher.mjs +0 -1
- package/dist/auth-fetcher.mjs.map +1 -1
- package/dist/endpoint-hooks.cjs +10 -5
- package/dist/endpoint-hooks.cjs.map +1 -1
- package/dist/endpoint-hooks.d.cts +2 -2
- package/dist/endpoint-hooks.d.cts.map +1 -1
- package/dist/endpoint-hooks.d.mts +2 -2
- package/dist/endpoint-hooks.d.mts.map +1 -1
- package/dist/endpoint-hooks.mjs +10 -5
- package/dist/endpoint-hooks.mjs.map +1 -1
- package/dist/fetcher-5fnBE7Dk.mjs.map +1 -1
- package/dist/fetcher-CgLEaIAC.cjs.map +1 -1
- package/dist/fetcher.d.cts +1 -1
- package/dist/fetcher.d.cts.map +1 -1
- package/dist/fetcher.d.mts +1 -1
- package/dist/fetcher.d.mts.map +1 -1
- package/dist/infer.d.cts.map +1 -1
- package/dist/infer.d.mts.map +1 -1
- package/dist/openapi-hooks.cjs.map +1 -1
- package/dist/openapi-hooks.d.cts +2 -2
- package/dist/openapi-hooks.d.cts.map +1 -1
- package/dist/openapi-hooks.d.mts +2 -2
- package/dist/openapi-hooks.d.mts.map +1 -1
- package/dist/openapi-hooks.mjs.map +1 -1
- package/dist/openapi-types.d.cts.map +1 -1
- package/dist/openapi-types.d.mts.map +1 -1
- package/dist/openapi.cjs.map +1 -1
- package/dist/openapi.mjs.map +1 -1
- package/dist/react-query.cjs.map +1 -1
- package/dist/react-query.d.cts +1 -1
- package/dist/react-query.d.cts.map +1 -1
- package/dist/react-query.d.mts +1 -1
- package/dist/react-query.d.mts.map +1 -1
- package/dist/react-query.mjs.map +1 -1
- package/dist/{types-FxummKaL.d.mts → types-4K4N-Fl1.d.cts} +2 -2
- package/dist/types-4K4N-Fl1.d.cts.map +1 -0
- package/dist/{types-vutATyWv.d.cts → types-hhkZWjQn.d.mts} +2 -2
- package/dist/types-hhkZWjQn.d.mts.map +1 -0
- package/dist/types.d.cts +1 -1
- package/dist/types.d.mts +1 -1
- package/package.json +3 -3
- package/src/__tests__/auth-fetcher.spec.ts +476 -476
- package/src/__tests__/endpoint-hooks.spec.tsx +348 -348
- package/src/__tests__/fetcher.spec.ts +403 -403
- package/src/__tests__/infer.integration.spec.ts +171 -171
- package/src/__tests__/infer.spec.ts +182 -182
- package/src/__tests__/method-restrictions.spec.tsx +165 -165
- package/src/__tests__/openapi-hooks.spec.tsx +536 -536
- package/src/__tests__/react-query-infinite.spec.tsx +989 -989
- package/src/__tests__/react-query-invalidation.spec.tsx +223 -222
- package/src/__tests__/react-query.spec.tsx +567 -567
- package/src/__tests__/setup.ts +260 -260
- package/src/__tests__/types.spec.ts +127 -127
- package/src/__tests__/url-parsing.spec.ts +191 -191
- package/src/auth-fetcher.ts +160 -162
- package/src/endpoint-hooks.ts +151 -156
- package/src/fetcher.ts +163 -163
- package/src/infer.ts +64 -63
- package/src/openapi-hooks.ts +146 -146
- package/src/openapi-types.d.ts +428 -428
- package/src/openapi.json +593 -593
- package/src/react-query.ts +281 -281
- package/src/types.ts +121 -124
- package/dist/types-FxummKaL.d.mts.map +0 -1
- package/dist/types-vutATyWv.d.cts.map +0 -1
package/src/endpoint-hooks.ts
CHANGED
|
@@ -1,110 +1,110 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
QueryClient,
|
|
3
|
+
UseMutationOptions,
|
|
4
|
+
UseQueryOptions,
|
|
5
5
|
} from '@tanstack/react-query';
|
|
6
6
|
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
7
7
|
import { useMemo } from 'react';
|
|
8
8
|
import type {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
ExtractEndpointResponse,
|
|
10
|
+
FilteredRequestConfig,
|
|
11
|
+
IsConfigRequired,
|
|
12
|
+
MutationEndpoint,
|
|
13
|
+
QueryEndpoint,
|
|
14
|
+
TypedApiFunction,
|
|
15
15
|
} from './types';
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Build query key from endpoint and config
|
|
19
19
|
*/
|
|
20
20
|
function buildQueryKey<Paths, T extends QueryEndpoint<Paths>>(
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
endpoint: T,
|
|
22
|
+
config?: FilteredRequestConfig<Paths, T>,
|
|
23
23
|
): unknown[] {
|
|
24
|
-
|
|
24
|
+
const key: unknown[] = [endpoint];
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
if (config && 'params' in config && config.params) {
|
|
27
|
+
key.push({ params: config.params });
|
|
28
|
+
}
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
if (config && 'query' in config && config.query) {
|
|
31
|
+
key.push({ query: config.query });
|
|
32
|
+
}
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
return key;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
38
|
* Options for creating endpoint-based hooks
|
|
39
39
|
*/
|
|
40
40
|
export interface CreateEndpointHooksOptions {
|
|
41
|
-
|
|
41
|
+
queryClient?: QueryClient;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* Hook options type that conditionally requires config
|
|
46
46
|
*/
|
|
47
47
|
type UseQueryArgs<Paths, T extends QueryEndpoint<Paths>> = IsConfigRequired<
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
Paths,
|
|
49
|
+
T
|
|
50
50
|
> extends true
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
51
|
+
? [
|
|
52
|
+
config: FilteredRequestConfig<Paths, T>,
|
|
53
|
+
options?: Omit<
|
|
54
|
+
UseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,
|
|
55
|
+
'queryKey' | 'queryFn'
|
|
56
|
+
>,
|
|
57
|
+
]
|
|
58
|
+
: [
|
|
59
|
+
config?: FilteredRequestConfig<Paths, T>,
|
|
60
|
+
options?: Omit<
|
|
61
|
+
UseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,
|
|
62
|
+
'queryKey' | 'queryFn'
|
|
63
|
+
>,
|
|
64
|
+
];
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* Endpoint-based React Query hooks
|
|
68
68
|
*/
|
|
69
69
|
export interface EndpointHooks<Paths> {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
70
|
+
/**
|
|
71
|
+
* Use query hook for GET endpoints.
|
|
72
|
+
* Config is required when endpoint has path params.
|
|
73
|
+
*/
|
|
74
|
+
useQuery: <T extends QueryEndpoint<Paths>>(
|
|
75
|
+
endpoint: T,
|
|
76
|
+
...args: UseQueryArgs<Paths, T>
|
|
77
|
+
) => ReturnType<typeof useQuery<ExtractEndpointResponse<Paths, T>, Error>>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Use mutation hook for POST, PUT, PATCH, DELETE endpoints.
|
|
81
|
+
* Config with params/body is passed to mutate().
|
|
82
|
+
*/
|
|
83
|
+
useMutation: <T extends MutationEndpoint<Paths>>(
|
|
84
|
+
endpoint: T,
|
|
85
|
+
options?: Omit<
|
|
86
|
+
UseMutationOptions<
|
|
87
|
+
ExtractEndpointResponse<Paths, T>,
|
|
88
|
+
Error,
|
|
89
|
+
FilteredRequestConfig<Paths, T>
|
|
90
|
+
>,
|
|
91
|
+
'mutationFn'
|
|
92
|
+
>,
|
|
93
|
+
) => ReturnType<
|
|
94
|
+
typeof useMutation<
|
|
95
|
+
ExtractEndpointResponse<Paths, T>,
|
|
96
|
+
Error,
|
|
97
|
+
FilteredRequestConfig<Paths, T>
|
|
98
|
+
>
|
|
99
|
+
>;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Build a query key for manual cache operations
|
|
103
|
+
*/
|
|
104
|
+
buildQueryKey: <T extends QueryEndpoint<Paths>>(
|
|
105
|
+
endpoint: T,
|
|
106
|
+
config?: FilteredRequestConfig<Paths, T>,
|
|
107
|
+
) => unknown[];
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
/**
|
|
@@ -123,87 +123,82 @@ export interface EndpointHooks<Paths> {
|
|
|
123
123
|
* ```
|
|
124
124
|
*/
|
|
125
125
|
export function createEndpointHooks<Paths>(
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
fetcher: TypedApiFunction<Paths>,
|
|
127
|
+
_options: CreateEndpointHooksOptions = {},
|
|
128
128
|
): EndpointHooks<Paths> {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
>(memoizedOptions);
|
|
205
|
-
},
|
|
206
|
-
|
|
207
|
-
buildQueryKey,
|
|
208
|
-
};
|
|
129
|
+
return {
|
|
130
|
+
useQuery: <T extends QueryEndpoint<Paths>>(
|
|
131
|
+
endpoint: T,
|
|
132
|
+
...args: UseQueryArgs<Paths, T>
|
|
133
|
+
) => {
|
|
134
|
+
// Parse args - config is first, options is second
|
|
135
|
+
const [config, queryOptions] = args as [
|
|
136
|
+
FilteredRequestConfig<Paths, T> | undefined,
|
|
137
|
+
(
|
|
138
|
+
| Omit<
|
|
139
|
+
UseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,
|
|
140
|
+
'queryKey' | 'queryFn'
|
|
141
|
+
>
|
|
142
|
+
| undefined
|
|
143
|
+
),
|
|
144
|
+
];
|
|
145
|
+
|
|
146
|
+
const queryKey = buildQueryKey(endpoint, config);
|
|
147
|
+
|
|
148
|
+
const memoizedOptions = useMemo(
|
|
149
|
+
() => ({
|
|
150
|
+
queryKey,
|
|
151
|
+
queryFn: () =>
|
|
152
|
+
// Type assertion needed due to complex conditional types
|
|
153
|
+
(
|
|
154
|
+
fetcher as (
|
|
155
|
+
endpoint: T,
|
|
156
|
+
config?: unknown,
|
|
157
|
+
) => Promise<ExtractEndpointResponse<Paths, T>>
|
|
158
|
+
)(endpoint, config),
|
|
159
|
+
...queryOptions,
|
|
160
|
+
}),
|
|
161
|
+
[endpoint, config, fetcher, queryKey, queryOptions],
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
return useQuery<ExtractEndpointResponse<Paths, T>, Error>(
|
|
165
|
+
memoizedOptions,
|
|
166
|
+
);
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
useMutation: <T extends MutationEndpoint<Paths>>(
|
|
170
|
+
endpoint: T,
|
|
171
|
+
mutationOptions?: Omit<
|
|
172
|
+
UseMutationOptions<
|
|
173
|
+
ExtractEndpointResponse<Paths, T>,
|
|
174
|
+
Error,
|
|
175
|
+
FilteredRequestConfig<Paths, T>
|
|
176
|
+
>,
|
|
177
|
+
'mutationFn'
|
|
178
|
+
>,
|
|
179
|
+
) => {
|
|
180
|
+
const memoizedOptions = useMemo(
|
|
181
|
+
() => ({
|
|
182
|
+
mutationFn: (config: FilteredRequestConfig<Paths, T>) =>
|
|
183
|
+
// Type assertion needed due to complex conditional types
|
|
184
|
+
(
|
|
185
|
+
fetcher as (
|
|
186
|
+
endpoint: T,
|
|
187
|
+
config?: unknown,
|
|
188
|
+
) => Promise<ExtractEndpointResponse<Paths, T>>
|
|
189
|
+
)(endpoint, config),
|
|
190
|
+
...mutationOptions,
|
|
191
|
+
}),
|
|
192
|
+
[endpoint, fetcher, mutationOptions],
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
return useMutation<
|
|
196
|
+
ExtractEndpointResponse<Paths, T>,
|
|
197
|
+
Error,
|
|
198
|
+
FilteredRequestConfig<Paths, T>
|
|
199
|
+
>(memoizedOptions);
|
|
200
|
+
},
|
|
201
|
+
|
|
202
|
+
buildQueryKey,
|
|
203
|
+
};
|
|
209
204
|
}
|