eden-tanstack-query 0.0.4 → 0.0.7
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/README.md +27 -9
- package/dist/index.d.ts +47 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -160,19 +160,37 @@ const eden = createEdenQuery<App>('http://localhost:8080', {
|
|
|
160
160
|
})
|
|
161
161
|
```
|
|
162
162
|
|
|
163
|
-
###
|
|
163
|
+
### Type-Safe Data Narrowing
|
|
164
164
|
|
|
165
|
-
When
|
|
165
|
+
When `throwOnError` is `true` (the default), errors are thrown before reaching callbacks like `select` and `onSuccess`. The library automatically narrows the data type to exclude error shapes:
|
|
166
166
|
|
|
167
|
-
|
|
167
|
+
```typescript
|
|
168
|
+
// Default: throwOnError = true
|
|
169
|
+
// Data type in select/onSuccess excludes { error: string } shapes
|
|
170
|
+
const eden = createEdenQuery<App>('http://localhost:8080')
|
|
171
|
+
|
|
172
|
+
useQuery(eden.users.get.queryOptions(undefined, {
|
|
173
|
+
select: (data) => data.users, // TypeScript knows data doesn't have error shape
|
|
174
|
+
onSuccess: (data) => {
|
|
175
|
+
console.log(data.users) // No need for type guards
|
|
176
|
+
}
|
|
177
|
+
}))
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
When `throwOnError` is `false` or a function, the full union type is preserved:
|
|
168
181
|
|
|
169
182
|
```typescript
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
183
|
+
// Explicit: throwOnError = false
|
|
184
|
+
// Data type includes all response shapes (success and error)
|
|
185
|
+
const eden = createEdenQuery<App>('http://localhost:8080', {
|
|
186
|
+
throwOnError: false
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
useQuery(eden.users.get.queryOptions(undefined, {
|
|
190
|
+
select: (data) => {
|
|
191
|
+
// Need to handle both success and error shapes
|
|
192
|
+
if ('error' in data) return null
|
|
193
|
+
return data.users
|
|
176
194
|
}
|
|
177
195
|
}))
|
|
178
196
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,8 @@ interface EdenThrowOnErrorContext {
|
|
|
14
14
|
input: unknown;
|
|
15
15
|
}
|
|
16
16
|
type EdenThrowOnError = ((queryKey: readonly unknown[], status: number) => boolean) | ((context: EdenThrowOnErrorContext) => boolean);
|
|
17
|
+
/** Union type for throwOnError config option */
|
|
18
|
+
type ThrowOnErrorOption = boolean | EdenThrowOnError;
|
|
17
19
|
interface EdenErrorContext {
|
|
18
20
|
error: _elysiajs_eden.EdenFetchError;
|
|
19
21
|
queryKey: readonly unknown[];
|
|
@@ -22,11 +24,23 @@ interface EdenErrorContext {
|
|
|
22
24
|
input: unknown;
|
|
23
25
|
type: 'query' | 'mutation';
|
|
24
26
|
}
|
|
25
|
-
interface EdenQueryConfig {
|
|
26
|
-
throwOnError?:
|
|
27
|
+
interface EdenQueryConfig<TThrowOnError extends ThrowOnErrorOption = true> {
|
|
28
|
+
throwOnError?: TThrowOnError;
|
|
27
29
|
queryKeyPrefix?: string | string[];
|
|
28
30
|
onError?: (context: EdenErrorContext) => void;
|
|
29
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Narrows data type by excluding error-shaped objects when throwOnError is true.
|
|
34
|
+
* When throwOnError is true, errors are thrown before reaching callbacks like
|
|
35
|
+
* `select` or `onSuccess`, so the data type should exclude error shapes.
|
|
36
|
+
*
|
|
37
|
+
* Uses conditional type to check if each union member has an `error: string` property,
|
|
38
|
+
* which handles cases like `{ error: string; message?: undefined }` that simple
|
|
39
|
+
* `Exclude<T, { error: string }>` misses due to structural typing quirks.
|
|
40
|
+
*/
|
|
41
|
+
type NarrowedData<TData, TThrowOnError> = TThrowOnError extends true ? TData extends {
|
|
42
|
+
error: string;
|
|
43
|
+
} ? never : TData : TData;
|
|
30
44
|
interface EdenOptions<TEden = unknown> {
|
|
31
45
|
eden?: TEden;
|
|
32
46
|
}
|
|
@@ -43,27 +57,46 @@ type EdenData<T> = Extract<AwaitedReturn<T>, {
|
|
|
43
57
|
}> extends {
|
|
44
58
|
data: infer D;
|
|
45
59
|
} ? D : never;
|
|
46
|
-
type EdenQueryMethod<TMethod extends Extract<HTTPMethod, 'get' | 'head'>, TCall extends (...args: any[]) => Promise<any
|
|
60
|
+
type EdenQueryMethod<TMethod extends Extract<HTTPMethod, 'get' | 'head'>, TCall extends (...args: any[]) => Promise<any>, TThrowOnError extends ThrowOnErrorOption = true> = TCall & {
|
|
47
61
|
queryKey: (input?: FirstArg<TCall>) => EdenQueryKey<FirstArg<TCall>, TMethod>;
|
|
48
62
|
queryFilter: (input?: FirstArg<TCall>) => QueryFilters<EdenQueryKey<FirstArg<TCall>, TMethod>>;
|
|
49
|
-
queryOptions: (input?: FirstArg<TCall>, options?: EdenQueryOptions<EdenData<TCall>, EdenFetchError | Error, EdenData<TCall>, EdenQueryKey<FirstArg<TCall>, TMethod>, EdenPartial<NonNullable<FirstArg<TCall>>>>) => EdenQueryOptions<EdenData<TCall>, EdenFetchError | Error, EdenData<TCall>, EdenQueryKey<FirstArg<TCall>, TMethod>, EdenPartial<NonNullable<FirstArg<TCall>>>> & {
|
|
63
|
+
queryOptions: (input?: FirstArg<TCall>, options?: EdenQueryOptions<NarrowedData<EdenData<TCall>, TThrowOnError>, EdenFetchError | Error, NarrowedData<EdenData<TCall>, TThrowOnError>, EdenQueryKey<FirstArg<TCall>, TMethod>, EdenPartial<NonNullable<FirstArg<TCall>>>>) => EdenQueryOptions<NarrowedData<EdenData<TCall>, TThrowOnError>, EdenFetchError | Error, NarrowedData<EdenData<TCall>, TThrowOnError>, EdenQueryKey<FirstArg<TCall>, TMethod>, EdenPartial<NonNullable<FirstArg<TCall>>>> & {
|
|
50
64
|
queryKey: EdenQueryKey<FirstArg<TCall>, TMethod>;
|
|
51
|
-
queryFn: (context: QueryFunctionContext<EdenQueryKey<FirstArg<TCall>, TMethod>>) => Promise<EdenData<TCall>>;
|
|
65
|
+
queryFn: (context: QueryFunctionContext<EdenQueryKey<FirstArg<TCall>, TMethod>>) => Promise<NarrowedData<EdenData<TCall>, TThrowOnError>>;
|
|
52
66
|
};
|
|
53
67
|
};
|
|
54
|
-
type EdenMutationMethod<TMethod extends Extract<HTTPMethod, 'post' | 'put' | 'patch' | 'delete'>, TCall extends (...args: any[]) => Promise<any
|
|
55
|
-
mutationOptions: (options?: EdenMutationOptions<EdenData<TCall>, EdenFetchError | Error, FirstArg<TCall>, unknown, EdenPartial<NonNullable<SecondArg<TCall>>>>) => EdenMutationOptions<EdenData<TCall>, EdenFetchError | Error, FirstArg<TCall>, unknown, EdenPartial<NonNullable<SecondArg<TCall>>>> & {
|
|
68
|
+
type EdenMutationMethod<TMethod extends Extract<HTTPMethod, 'post' | 'put' | 'patch' | 'delete'>, TCall extends (...args: any[]) => Promise<any>, TThrowOnError extends ThrowOnErrorOption = true> = TCall & {
|
|
69
|
+
mutationOptions: (options?: EdenMutationOptions<NarrowedData<EdenData<TCall>, TThrowOnError>, EdenFetchError | Error, FirstArg<TCall>, unknown, EdenPartial<NonNullable<SecondArg<TCall>>>>) => EdenMutationOptions<NarrowedData<EdenData<TCall>, TThrowOnError>, EdenFetchError | Error, FirstArg<TCall>, unknown, EdenPartial<NonNullable<SecondArg<TCall>>>> & {
|
|
56
70
|
mutationKey: EdenQueryKey<undefined, TMethod>;
|
|
57
|
-
mutationFn: (variables: FirstArg<TCall>) => Promise<EdenData<TCall>>;
|
|
71
|
+
mutationFn: (variables: FirstArg<TCall>) => Promise<NarrowedData<EdenData<TCall>, TThrowOnError>>;
|
|
58
72
|
};
|
|
59
73
|
};
|
|
60
|
-
type EdenQueryify<T> = (T extends (...args: infer A) => infer R ? (...args: A) => EdenQueryify<R> : unknown) & (T extends object ? {
|
|
61
|
-
[K in keyof T]: EdenQueryifyValue<K, T[K]>;
|
|
74
|
+
type EdenQueryify<T, TThrowOnError extends ThrowOnErrorOption = true> = (T extends (...args: infer A) => infer R ? (...args: A) => EdenQueryify<R, TThrowOnError> : unknown) & (T extends object ? {
|
|
75
|
+
[K in keyof T]: EdenQueryifyValue<K, T[K], TThrowOnError>;
|
|
62
76
|
} : unknown);
|
|
63
|
-
type EdenQueryifyValue<K, V> = K extends 'get' | 'head' ? V extends (...args: any[]) => Promise<any> ? EdenQueryMethod<Extract<K, 'get' | 'head'>, V> : V : K extends 'post' | 'put' | 'patch' | 'delete' ? V extends (...args: any[]) => Promise<any> ? EdenMutationMethod<Extract<K, 'post' | 'put' | 'patch' | 'delete'>, V> : V : EdenQueryify<V>;
|
|
77
|
+
type EdenQueryifyValue<K, V, TThrowOnError extends ThrowOnErrorOption = true> = K extends 'get' | 'head' ? V extends (...args: any[]) => Promise<any> ? EdenQueryMethod<Extract<K, 'get' | 'head'>, V, TThrowOnError> : V : K extends 'post' | 'put' | 'patch' | 'delete' ? V extends (...args: any[]) => Promise<any> ? EdenMutationMethod<Extract<K, 'post' | 'put' | 'patch' | 'delete'>, V, TThrowOnError> : V : EdenQueryify<V, TThrowOnError>;
|
|
64
78
|
|
|
65
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Creates an Eden Query client with TanStack Query integration.
|
|
81
|
+
*
|
|
82
|
+
* @typeParam App - Your Elysia application type
|
|
83
|
+
* @typeParam TThrowOnError - Whether to throw on errors (inferred from options, defaults to true)
|
|
84
|
+
*
|
|
85
|
+
* When `throwOnError` is `true` (default), error-shaped objects are excluded from
|
|
86
|
+
* the data type in callbacks like `select` and `onSuccess`, since errors are thrown
|
|
87
|
+
* before reaching those callbacks.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* // Default: throwOnError = true, data types exclude error shapes
|
|
92
|
+
* const eden = createEdenQuery<App>('http://localhost:8080')
|
|
93
|
+
*
|
|
94
|
+
* // Explicit: throwOnError = false, data types include full union
|
|
95
|
+
* const eden = createEdenQuery<App>('http://localhost:8080', { throwOnError: false })
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
declare function createEdenQuery<App extends Elysia<any, any, any, any, any, any, any>, const TThrowOnError extends ThrowOnErrorOption = true>(domain: string, options?: EdenQueryConfig<TThrowOnError> & {
|
|
66
99
|
treaty?: Treaty.Config;
|
|
67
|
-
}): EdenQueryify<Treaty.Create<App
|
|
100
|
+
}): EdenQueryify<Treaty.Create<App>, TThrowOnError>;
|
|
68
101
|
|
|
69
|
-
export { type EdenErrorContext, type EdenInfiniteQueryOptions, type EdenMutationMethod, type EdenMutationOptions, type EdenOptions, type EdenQueryConfig, type EdenQueryKey, type EdenQueryMethod, type EdenQueryOptions, type EdenQueryify, type EdenThrowOnError, type EdenThrowOnErrorContext, type HTTPMethod, createEdenQuery };
|
|
102
|
+
export { type EdenErrorContext, type EdenInfiniteQueryOptions, type EdenMutationMethod, type EdenMutationOptions, type EdenOptions, type EdenQueryConfig, type EdenQueryKey, type EdenQueryMethod, type EdenQueryOptions, type EdenQueryify, type EdenThrowOnError, type EdenThrowOnErrorContext, type HTTPMethod, type NarrowedData, type ThrowOnErrorOption, createEdenQuery };
|