@geekmidas/client 0.3.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 +2 -3
- package/dist/auth-fetcher.cjs.map +1 -1
- package/dist/auth-fetcher.d.cts +2 -1
- package/dist/auth-fetcher.d.cts.map +1 -0
- package/dist/auth-fetcher.d.mts +2 -1
- package/dist/auth-fetcher.d.mts.map +1 -0
- package/dist/auth-fetcher.mjs +2 -3
- 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 -0
- package/dist/endpoint-hooks.d.mts +2 -2
- package/dist/endpoint-hooks.d.mts.map +1 -0
- package/dist/endpoint-hooks.mjs +10 -5
- package/dist/endpoint-hooks.mjs.map +1 -1
- package/dist/{fetcher-DLDD_7Sa.mjs → fetcher-5fnBE7Dk.mjs} +2 -2
- package/dist/fetcher-5fnBE7Dk.mjs.map +1 -0
- package/dist/{fetcher-KdwHgdAl.cjs → fetcher-CgLEaIAC.cjs} +2 -2
- package/dist/fetcher-CgLEaIAC.cjs.map +1 -0
- package/dist/fetcher.cjs +1 -1
- package/dist/fetcher.d.cts +3 -1
- package/dist/fetcher.d.cts.map +1 -0
- package/dist/fetcher.d.mts +3 -1
- package/dist/fetcher.d.mts.map +1 -0
- package/dist/fetcher.mjs +1 -1
- package/dist/infer.d.cts.map +1 -0
- package/dist/infer.d.mts.map +1 -0
- package/dist/openapi-hooks.cjs +3 -5
- package/dist/openapi-hooks.cjs.map +1 -1
- package/dist/openapi-hooks.d.cts +3 -3
- package/dist/openapi-hooks.d.cts.map +1 -0
- package/dist/openapi-hooks.d.mts +6 -6
- package/dist/openapi-hooks.d.mts.map +1 -0
- package/dist/openapi-hooks.mjs +3 -5
- package/dist/openapi-hooks.mjs.map +1 -1
- package/dist/openapi-types.d.cts.map +1 -0
- package/dist/openapi-types.d.mts.map +1 -0
- package/dist/openapi.cjs.map +1 -1
- package/dist/openapi.mjs.map +1 -1
- package/dist/react-query.cjs +1 -1
- package/dist/react-query.cjs.map +1 -1
- package/dist/react-query.d.cts +3 -1
- package/dist/react-query.d.cts.map +1 -0
- package/dist/react-query.d.mts +10 -8
- package/dist/react-query.d.mts.map +1 -0
- package/dist/react-query.mjs +1 -1
- package/dist/react-query.mjs.map +1 -1
- package/dist/{types-D4OSWveN.d.cts → types-4K4N-Fl1.d.cts} +8 -6
- package/dist/types-4K4N-Fl1.d.cts.map +1 -0
- package/dist/{types-Cdv1XAWr.d.mts → types-hhkZWjQn.d.mts} +8 -6
- 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 +6 -5
- package/src/__tests__/auth-fetcher.spec.ts +505 -0
- 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 -537
- 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 -150
- 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 +124 -125
- package/tsconfig.json +12 -0
- package/dist/fetcher-DLDD_7Sa.mjs.map +0 -1
- package/dist/fetcher-KdwHgdAl.cjs.map +0 -1
package/src/openapi-hooks.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
UseMutationOptions,
|
|
3
|
+
UseQueryOptions,
|
|
4
4
|
} from '@tanstack/react-query';
|
|
5
5
|
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
6
6
|
import { createTypedFetcher } from './fetcher';
|
|
@@ -11,35 +11,35 @@ type HttpMethods = 'get' | 'post' | 'put' | 'patch' | 'delete';
|
|
|
11
11
|
|
|
12
12
|
// Extract all operations from paths
|
|
13
13
|
type AllOperations<Paths> = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
14
|
+
[Path in keyof Paths]: {
|
|
15
|
+
[Method in keyof Paths[Path] & HttpMethods]: Paths[Path][Method] extends {
|
|
16
|
+
operationId: infer OpId;
|
|
17
|
+
}
|
|
18
|
+
? OpId extends string
|
|
19
|
+
? {
|
|
20
|
+
operationId: OpId;
|
|
21
|
+
path: Path;
|
|
22
|
+
method: Method;
|
|
23
|
+
spec: Paths[Path][Method] & {
|
|
24
|
+
_pathParams?: Paths[Path] extends {
|
|
25
|
+
parameters: { path: infer P };
|
|
26
|
+
}
|
|
27
|
+
? P
|
|
28
|
+
: never;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
: never
|
|
32
|
+
: never;
|
|
33
|
+
}[keyof Paths[Path] & HttpMethods];
|
|
34
34
|
}[keyof Paths];
|
|
35
35
|
|
|
36
36
|
// Create operation map
|
|
37
37
|
type OperationMap<Paths> = {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
[Op in AllOperations<Paths> as Op extends {
|
|
39
|
+
operationId: infer Id extends string;
|
|
40
|
+
}
|
|
41
|
+
? Id
|
|
42
|
+
: never]: Op;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
45
|
// Get operation IDs
|
|
@@ -47,65 +47,65 @@ type OperationId<Paths> = keyof OperationMap<Paths>;
|
|
|
47
47
|
|
|
48
48
|
// Get operations by method
|
|
49
49
|
type OperationsByMethod<Paths, Method extends HttpMethods> = {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
[K in OperationId<Paths>]: OperationMap<Paths>[K] extends { method: Method }
|
|
51
|
+
? K
|
|
52
|
+
: never;
|
|
53
53
|
}[OperationId<Paths>];
|
|
54
54
|
|
|
55
55
|
// Extract parameter types
|
|
56
56
|
type OperationParams<
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
Paths,
|
|
58
|
+
OpId extends OperationId<Paths>,
|
|
59
59
|
> = OperationMap<Paths>[OpId] extends {
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
path: infer Path extends keyof Paths;
|
|
61
|
+
spec: infer Spec;
|
|
62
62
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
63
|
+
? {
|
|
64
|
+
params?: Paths[Path] extends { parameters: { path: infer P } }
|
|
65
|
+
? P
|
|
66
|
+
: Spec extends { parameters: { path: infer P } }
|
|
67
|
+
? P
|
|
68
|
+
: never;
|
|
69
|
+
query?: Spec extends { parameters?: { query?: infer Q } } ? Q : never;
|
|
70
|
+
body?: Spec extends {
|
|
71
|
+
requestBody: { content: { 'application/json': infer Body } };
|
|
72
|
+
}
|
|
73
|
+
? Body
|
|
74
|
+
: Spec extends {
|
|
75
|
+
requestBody: {
|
|
76
|
+
required: true;
|
|
77
|
+
content: { 'application/json': infer Body };
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
? Body
|
|
81
|
+
: never;
|
|
82
|
+
}
|
|
83
|
+
: never;
|
|
84
84
|
|
|
85
85
|
// Extract response type
|
|
86
86
|
type OperationResponse<
|
|
87
|
-
|
|
88
|
-
|
|
87
|
+
Paths,
|
|
88
|
+
OpId extends OperationId<Paths>,
|
|
89
89
|
> = OperationMap<Paths>[OpId] extends { spec: infer Spec }
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
90
|
+
? Spec extends {
|
|
91
|
+
responses: { 200: { content: { 'application/json': infer R } } };
|
|
92
|
+
}
|
|
93
|
+
? R
|
|
94
|
+
: Spec extends {
|
|
95
|
+
responses: { 201: { content: { 'application/json': infer R } } };
|
|
96
|
+
}
|
|
97
|
+
? R
|
|
98
|
+
: Spec extends { responses: { 204: any } }
|
|
99
|
+
? undefined
|
|
100
|
+
: unknown
|
|
101
|
+
: never;
|
|
102
102
|
|
|
103
103
|
// Remove never properties
|
|
104
104
|
type RemoveNever<T> = Pick<
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
105
|
+
T,
|
|
106
|
+
{
|
|
107
|
+
[K in keyof T]: T[K] extends never ? never : K;
|
|
108
|
+
}[keyof T]
|
|
109
109
|
>;
|
|
110
110
|
|
|
111
111
|
// Check if type is empty
|
|
@@ -113,81 +113,81 @@ type IsEmpty<T> = keyof T extends never ? true : false;
|
|
|
113
113
|
|
|
114
114
|
// Runtime operation registry (would be generated)
|
|
115
115
|
interface OperationRegistry {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
116
|
+
[operationId: string]: {
|
|
117
|
+
path: string;
|
|
118
|
+
method: string;
|
|
119
|
+
};
|
|
120
120
|
}
|
|
121
121
|
|
|
122
122
|
export function createOpenAPIHooks<Paths>(
|
|
123
|
-
|
|
123
|
+
options: FetcherOptions & { operations?: OperationRegistry } = {},
|
|
124
124
|
) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
|
|
125
|
+
const { operations, ...fetcherOptions } = options;
|
|
126
|
+
const fetcher = createTypedFetcher<Paths>(fetcherOptions);
|
|
127
|
+
|
|
128
|
+
function buildEndpoint<OpId extends OperationId<Paths>>(
|
|
129
|
+
operationId: OpId,
|
|
130
|
+
): string {
|
|
131
|
+
// Runtime lookup from registry
|
|
132
|
+
const op = operations?.[operationId as string];
|
|
133
|
+
if (op) {
|
|
134
|
+
return `${op.method.toUpperCase()} ${op.path}`;
|
|
135
|
+
}
|
|
136
|
+
// Fallback for compile-time only usage
|
|
137
|
+
return operationId as string;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
useQuery: <OpId extends OperationsByMethod<Paths, 'get'>>(
|
|
142
|
+
operationId: OpId,
|
|
143
|
+
config?: RemoveNever<OperationParams<Paths, OpId>>,
|
|
144
|
+
options?: Omit<
|
|
145
|
+
UseQueryOptions<OperationResponse<Paths, OpId>, Error>,
|
|
146
|
+
'queryKey' | 'queryFn'
|
|
147
|
+
>,
|
|
148
|
+
) => {
|
|
149
|
+
const endpoint = buildEndpoint(operationId);
|
|
150
|
+
const queryKey = [operationId, ...(config ? [config] : [])];
|
|
151
|
+
|
|
152
|
+
return useQuery<OperationResponse<Paths, OpId>, Error>({
|
|
153
|
+
queryKey,
|
|
154
|
+
queryFn: async () => {
|
|
155
|
+
const response = await fetcher(endpoint as any, config as any);
|
|
156
|
+
return response as OperationResponse<Paths, OpId>;
|
|
157
|
+
},
|
|
158
|
+
...options,
|
|
159
|
+
});
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
useMutation: <
|
|
163
|
+
OpId extends Exclude<
|
|
164
|
+
OperationId<Paths>,
|
|
165
|
+
OperationsByMethod<Paths, 'get'>
|
|
166
|
+
>,
|
|
167
|
+
>(
|
|
168
|
+
operationId: OpId,
|
|
169
|
+
options?: Omit<
|
|
170
|
+
UseMutationOptions<
|
|
171
|
+
OperationResponse<Paths, OpId>,
|
|
172
|
+
Error,
|
|
173
|
+
RemoveNever<OperationParams<Paths, OpId>>
|
|
174
|
+
>,
|
|
175
|
+
'mutationFn'
|
|
176
|
+
>,
|
|
177
|
+
) => {
|
|
178
|
+
const endpoint = buildEndpoint(operationId);
|
|
179
|
+
|
|
180
|
+
return useMutation<
|
|
181
|
+
OperationResponse<Paths, OpId>,
|
|
182
|
+
Error,
|
|
183
|
+
RemoveNever<OperationParams<Paths, OpId>>
|
|
184
|
+
>({
|
|
185
|
+
mutationFn: async (variables) => {
|
|
186
|
+
const response = await fetcher(endpoint as any, variables as any);
|
|
187
|
+
return response as OperationResponse<Paths, OpId>;
|
|
188
|
+
},
|
|
189
|
+
...options,
|
|
190
|
+
});
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
193
|
}
|