eden-tanstack-query 0.1.0-alpha.2 → 0.1.0-alpha.3
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 +92 -6
- package/dist/index.d.mts +131 -63
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -0
- package/package.json +8 -9
- package/src/index.ts +209 -10
- package/src/types.ts +192 -5
- package/dist/index.d.ts +0 -84
- package/dist/index.js +0 -1
package/README.md
CHANGED
|
@@ -34,13 +34,34 @@ const query = createQuery(() =>
|
|
|
34
34
|
// query.data is typed as your Elysia response type!
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
+
### Infinite Queries
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { createInfiniteQuery } from '@tanstack/svelte-query'
|
|
41
|
+
|
|
42
|
+
const infiniteQuery = createInfiniteQuery(() =>
|
|
43
|
+
eden.posts.get.infiniteQueryOptions(
|
|
44
|
+
{ query: { limit: '10' } },
|
|
45
|
+
{
|
|
46
|
+
initialPageParam: 0,
|
|
47
|
+
getNextPageParam: (lastPage) => lastPage.nextCursor,
|
|
48
|
+
// cursorKey: 'cursor' // optional, defaults to 'cursor'
|
|
49
|
+
}
|
|
50
|
+
)
|
|
51
|
+
)
|
|
52
|
+
```
|
|
53
|
+
|
|
37
54
|
### Mutations
|
|
38
55
|
|
|
39
56
|
```ts
|
|
40
57
|
import { createMutation } from '@tanstack/svelte-query'
|
|
41
58
|
|
|
42
59
|
const mutation = createMutation(() =>
|
|
43
|
-
eden.users.post.mutationOptions(
|
|
60
|
+
eden.users.post.mutationOptions({
|
|
61
|
+
onSuccess: (data) => {
|
|
62
|
+
console.log('Created user:', data.id)
|
|
63
|
+
}
|
|
64
|
+
})
|
|
44
65
|
)
|
|
45
66
|
|
|
46
67
|
// Type-safe variables
|
|
@@ -65,6 +86,27 @@ await eden.users({ id: '123' }).get.invalidate(queryClient, {
|
|
|
65
86
|
await eden.users({ id: '123' }).get.invalidate(queryClient)
|
|
66
87
|
```
|
|
67
88
|
|
|
89
|
+
### Utils (Bound QueryClient)
|
|
90
|
+
|
|
91
|
+
For tRPC-like ergonomics, use `createEdenTQUtils` to bind a QueryClient once:
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import { createEdenTQ, createEdenTQUtils } from 'eden-tanstack-query'
|
|
95
|
+
|
|
96
|
+
const eden = createEdenTQ<App>('http://localhost:3000')
|
|
97
|
+
const utils = createEdenTQUtils(eden, queryClient)
|
|
98
|
+
|
|
99
|
+
// No need to pass queryClient every time!
|
|
100
|
+
await utils.users({ id: '123' }).get.invalidate({ params: { id: '123' } })
|
|
101
|
+
await utils.posts.get.prefetch({ query: { limit: '10' } })
|
|
102
|
+
await utils.posts.get.cancel()
|
|
103
|
+
await utils.posts.get.refetch()
|
|
104
|
+
|
|
105
|
+
// Cache manipulation
|
|
106
|
+
utils.users({ id: '123' }).get.setData({ params: { id: '123' } }, { id: '123', name: 'Updated' })
|
|
107
|
+
const cached = utils.users({ id: '123' }).get.getData({ params: { id: '123' } })
|
|
108
|
+
```
|
|
109
|
+
|
|
68
110
|
## API
|
|
69
111
|
|
|
70
112
|
### `createEdenTQ<App>(domain, config?)`
|
|
@@ -74,15 +116,59 @@ Creates a type-safe Eden client with TanStack Query helpers.
|
|
|
74
116
|
- `domain`: Your API URL or Elysia app instance
|
|
75
117
|
- `config.queryKeyPrefix`: Custom prefix for query keys (default: `['eden']`)
|
|
76
118
|
|
|
119
|
+
### `createEdenTQUtils<App>(eden, queryClient)`
|
|
120
|
+
|
|
121
|
+
Creates a utils object with a bound QueryClient for tRPC-like ergonomics.
|
|
122
|
+
|
|
77
123
|
### Method Helpers
|
|
78
124
|
|
|
79
125
|
Each HTTP method (`get`, `post`, `put`, `delete`, `patch`) has:
|
|
80
126
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
127
|
+
| Method | Description |
|
|
128
|
+
|--------|-------------|
|
|
129
|
+
| `.queryOptions(input, overrides?)` | Returns `{ queryKey, queryFn, ...options }` for `createQuery` |
|
|
130
|
+
| `.infiniteQueryOptions(input, opts, overrides?)` | Returns options for `createInfiniteQuery` |
|
|
131
|
+
| `.mutationOptions(overrides?)` | Returns `{ mutationKey, mutationFn, ...options }` for `createMutation` |
|
|
132
|
+
| `.queryKey(input?)` | Returns the query key |
|
|
133
|
+
| `.mutationKey(input?)` | Returns the mutation key |
|
|
134
|
+
| `.invalidate(queryClient, input?, exact?)` | Invalidates matching queries |
|
|
135
|
+
| `.prefetch(queryClient, input)` | Prefetch a query |
|
|
136
|
+
| `.ensureData(queryClient, input)` | Ensure data exists or fetch it |
|
|
137
|
+
| `.setData(queryClient, input, updater)` | Manually set cache data |
|
|
138
|
+
| `.getData(queryClient, input)` | Read from cache |
|
|
139
|
+
|
|
140
|
+
### Query Options Overrides
|
|
141
|
+
|
|
142
|
+
You can pass standard TanStack Query options as overrides:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
eden.posts.get.queryOptions(
|
|
146
|
+
{ query: { limit: '10' } },
|
|
147
|
+
{
|
|
148
|
+
staleTime: 5000,
|
|
149
|
+
gcTime: 10000,
|
|
150
|
+
enabled: isReady,
|
|
151
|
+
refetchOnMount: false,
|
|
152
|
+
retry: 3
|
|
153
|
+
}
|
|
154
|
+
)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Mutation Options Overrides
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
eden.users.post.mutationOptions({
|
|
161
|
+
onMutate: (variables) => {
|
|
162
|
+
// Optimistic update
|
|
163
|
+
},
|
|
164
|
+
onSuccess: (data, variables) => {
|
|
165
|
+
// Invalidate related queries
|
|
166
|
+
},
|
|
167
|
+
onError: (error, variables, context) => {
|
|
168
|
+
// Rollback
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
```
|
|
86
172
|
|
|
87
173
|
## Before / After
|
|
88
174
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,84 +1,152 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { Treaty } from '@elysiajs/eden/treaty2';
|
|
1
|
+
import { Treaty } from "@elysiajs/eden/treaty2";
|
|
2
|
+
import { ELYSIA_FORM_DATA, Elysia } from "elysia";
|
|
3
|
+
import { GetNextPageParamFunction, GetPreviousPageParamFunction, InfiniteData, QueryClient, QueryClient as QueryClient$1, QueryClient as QueryClient$2, QueryFunctionContext, QueryKey, QueryKey as QueryKey$1 } from "@tanstack/query-core";
|
|
5
4
|
|
|
5
|
+
//#region src/types.d.ts
|
|
6
6
|
type IsNever<T> = [T] extends [never] ? true : false;
|
|
7
|
-
type Prettify<T> = {
|
|
8
|
-
[K in keyof T]: T[K];
|
|
9
|
-
} & {};
|
|
7
|
+
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
10
8
|
type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N ? Acc[number] : Enumerate<N, [...Acc, Acc['length']]>;
|
|
11
9
|
type IntegerRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>;
|
|
12
10
|
type SuccessCodeRange = IntegerRange<200, 300>;
|
|
13
11
|
type ExtractData<Res extends Record<number, unknown>> = Res[Extract<keyof Res, SuccessCodeRange>] extends {
|
|
14
|
-
|
|
12
|
+
[ELYSIA_FORM_DATA]: infer Data;
|
|
15
13
|
} ? Data : Res[Extract<keyof Res, SuccessCodeRange>];
|
|
16
14
|
type ExtractError<Res extends Record<number, unknown>> = Exclude<keyof Res, SuccessCodeRange> extends never ? {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
} : {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
};
|
|
26
|
-
}[Exclude<keyof Res, SuccessCodeRange>];
|
|
15
|
+
status: unknown;
|
|
16
|
+
value: unknown;
|
|
17
|
+
} : { [Status in keyof Res]: {
|
|
18
|
+
status: Status;
|
|
19
|
+
value: Res[Status] extends {
|
|
20
|
+
[ELYSIA_FORM_DATA]: infer Data;
|
|
21
|
+
} ? Data : Res[Status];
|
|
22
|
+
} }[Exclude<keyof Res, SuccessCodeRange>];
|
|
27
23
|
interface TQParamBase {
|
|
28
|
-
|
|
24
|
+
fetch?: RequestInit;
|
|
29
25
|
}
|
|
30
|
-
type SerializeQueryParams<T> = T extends Record<string, any> ? {
|
|
31
|
-
[K in keyof T]: T[K] extends Date ? string : T[K] extends Date | undefined ? string | undefined : T[K];
|
|
32
|
-
} : T;
|
|
26
|
+
type SerializeQueryParams<T> = T extends Record<string, any> ? { [K in keyof T]: T[K] extends Date ? string : T[K] extends Date | undefined ? string | undefined : T[K] } : T;
|
|
33
27
|
type IsEmptyObject<T> = T extends Record<string, never> ? [keyof T] extends [never] ? true : false : false;
|
|
34
|
-
type MaybeEmptyObject<T, K extends PropertyKey> = [T] extends [never] ? {} : [T] extends [undefined] ? {
|
|
35
|
-
[P in K]?: T;
|
|
36
|
-
} : IsEmptyObject<T> extends true ? {
|
|
37
|
-
[P in K]?: T;
|
|
38
|
-
} : undefined extends T ? {
|
|
39
|
-
[P in K]?: T;
|
|
40
|
-
} : {
|
|
41
|
-
[P in K]: T;
|
|
42
|
-
};
|
|
28
|
+
type MaybeEmptyObject<T, K extends PropertyKey> = [T] extends [never] ? {} : [T] extends [undefined] ? { [P in K]?: T } : IsEmptyObject<T> extends true ? { [P in K]?: T } : undefined extends T ? { [P in K]?: T } : { [P in K]: T };
|
|
43
29
|
type TQMethodParam<Body, Headers, Query, Params> = MaybeEmptyObject<Headers, 'headers'> & MaybeEmptyObject<SerializeQueryParams<Query>, 'query'> & MaybeEmptyObject<Params, 'params'> & MaybeEmptyObject<Body, 'body'> & TQParamBase;
|
|
44
30
|
interface EdenQueryOptions<TData = unknown, TError = unknown> {
|
|
45
|
-
|
|
46
|
-
|
|
31
|
+
queryKey: QueryKey$1;
|
|
32
|
+
queryFn: () => Promise<TData>;
|
|
33
|
+
enabled?: boolean;
|
|
34
|
+
staleTime?: number;
|
|
35
|
+
gcTime?: number;
|
|
36
|
+
refetchOnMount?: boolean | 'always';
|
|
37
|
+
refetchOnWindowFocus?: boolean | 'always';
|
|
38
|
+
refetchOnReconnect?: boolean | 'always';
|
|
39
|
+
refetchInterval?: number | false;
|
|
40
|
+
retry?: boolean | number | ((failureCount: number, error: TError) => boolean);
|
|
41
|
+
retryDelay?: number | ((failureCount: number, error: TError) => number);
|
|
42
|
+
placeholderData?: TData | (() => TData | undefined);
|
|
43
|
+
}
|
|
44
|
+
interface EdenInfiniteQueryOptions<TData = unknown, TError = unknown, TPageParam = unknown> {
|
|
45
|
+
queryKey: QueryKey$1;
|
|
46
|
+
queryFn: (context: QueryFunctionContext<QueryKey$1, TPageParam>) => Promise<TData>;
|
|
47
|
+
initialPageParam: TPageParam;
|
|
48
|
+
getNextPageParam: GetNextPageParamFunction<TPageParam, TData>;
|
|
49
|
+
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
50
|
+
enabled?: boolean;
|
|
51
|
+
staleTime?: number;
|
|
52
|
+
gcTime?: number;
|
|
53
|
+
refetchOnMount?: boolean | 'always';
|
|
54
|
+
refetchOnWindowFocus?: boolean | 'always';
|
|
55
|
+
refetchOnReconnect?: boolean | 'always';
|
|
56
|
+
maxPages?: number;
|
|
47
57
|
}
|
|
48
58
|
interface EdenMutationOptions<TData = unknown, TError = unknown, TVariables = unknown> {
|
|
49
|
-
|
|
50
|
-
|
|
59
|
+
mutationKey: QueryKey$1;
|
|
60
|
+
mutationFn: (variables: TVariables) => Promise<TData>;
|
|
61
|
+
onMutate?: (variables: TVariables) => Promise<unknown> | unknown;
|
|
62
|
+
onSuccess?: (data: TData, variables: TVariables, context: unknown) => Promise<unknown> | unknown;
|
|
63
|
+
onError?: (error: TError, variables: TVariables, context: unknown) => Promise<unknown> | unknown;
|
|
64
|
+
onSettled?: (data: TData | undefined, error: TError | null, variables: TVariables, context: unknown) => Promise<unknown> | unknown;
|
|
65
|
+
retry?: boolean | number | ((failureCount: number, error: TError) => boolean);
|
|
66
|
+
retryDelay?: number | ((failureCount: number, error: TError) => number);
|
|
67
|
+
}
|
|
68
|
+
type OmitQueryInput<T> = Omit<T, 'body' | 'headers' | 'fetch'>;
|
|
69
|
+
interface InfiniteQueryInput<TPageParam, Query, Params> {
|
|
70
|
+
params?: Params;
|
|
71
|
+
query?: Omit<Query, 'cursor'> & {
|
|
72
|
+
cursor?: TPageParam;
|
|
73
|
+
};
|
|
74
|
+
headers?: Record<string, string>;
|
|
75
|
+
fetch?: RequestInit;
|
|
76
|
+
cursorKey?: string;
|
|
51
77
|
}
|
|
52
78
|
interface EdenTQMethod<Body, Headers, Query, Params, Res extends Record<number, unknown>> {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
79
|
+
<TQueryFnData = ExtractData<Res>>(input: TQMethodParam<Body, Headers, Query, Params>, options?: RequestInit): Promise<Treaty.TreatyResponse<Res>>;
|
|
80
|
+
queryKey(input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>): QueryKey$1;
|
|
81
|
+
queryOptions<TData = ExtractData<Res>>(input: TQMethodParam<Body, Headers, Query, Params>, overrides?: Partial<EdenQueryOptions<TData, ExtractError<Res>>>): EdenQueryOptions<TData, ExtractError<Res>>;
|
|
82
|
+
infiniteQueryOptions<TData = ExtractData<Res>, TPageParam = unknown>(input: InfiniteQueryInput<TPageParam, Query, Params>, opts: {
|
|
83
|
+
initialPageParam: TPageParam;
|
|
84
|
+
getNextPageParam: GetNextPageParamFunction<TPageParam, TData>;
|
|
85
|
+
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
86
|
+
cursorKey?: string;
|
|
87
|
+
}, overrides?: Partial<Omit<EdenInfiniteQueryOptions<TData, ExtractError<Res>, TPageParam>, 'queryKey' | 'queryFn' | 'initialPageParam' | 'getNextPageParam' | 'getPreviousPageParam'>>): EdenInfiniteQueryOptions<TData, ExtractError<Res>, TPageParam>;
|
|
88
|
+
mutationKey(input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>): QueryKey$1;
|
|
89
|
+
mutationOptions<TData = ExtractData<Res>>(overrides?: Partial<EdenMutationOptions<TData, ExtractError<Res>, TQMethodParam<Body, Headers, Query, Params>>>): EdenMutationOptions<TData, ExtractError<Res>, TQMethodParam<Body, Headers, Query, Params>>;
|
|
90
|
+
invalidate(queryClient: QueryClient$2, input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>, exact?: boolean): Promise<void>;
|
|
91
|
+
prefetch(queryClient: QueryClient$2, input: TQMethodParam<Body, Headers, Query, Params>): Promise<void>;
|
|
92
|
+
ensureData<TData = ExtractData<Res>>(queryClient: QueryClient$2, input: TQMethodParam<Body, Headers, Query, Params>): Promise<TData>;
|
|
93
|
+
setData<TData = ExtractData<Res>>(queryClient: QueryClient$2, input: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>, updater: TData | ((old: TData | undefined) => TData | undefined)): TData | undefined;
|
|
94
|
+
getData<TData = ExtractData<Res>>(queryClient: QueryClient$2, input: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>): TData | undefined;
|
|
59
95
|
}
|
|
60
96
|
declare namespace EdenTQ {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
type CreateParams<Route extends Record<string, any>> = Extract<keyof Route, `:${string}`> extends infer Path extends string ? IsNever<Path> extends true ? Prettify<Sign<Route>> : (((params: {
|
|
77
|
-
[param in Path extends `:${infer Param}` ? Param extends `${infer P}?` ? P : Param : never]: string | number;
|
|
78
|
-
}) => Prettify<Sign<Route[Path]>> & CreateParams<Route[Path]>) & Prettify<Sign<Route>>) & (Path extends `:${string}?` ? CreateParams<Route[Path]> : {}) : never;
|
|
79
|
-
export { };
|
|
97
|
+
export type Config = Treaty.Config & {
|
|
98
|
+
queryKeyPrefix?: QueryKey$1;
|
|
99
|
+
};
|
|
100
|
+
export type Create<App extends Elysia<any, any, any, any, any, any, any>> = App extends {
|
|
101
|
+
'~Routes': infer Schema extends Record<any, any>;
|
|
102
|
+
} ? Prettify<Sign<Schema>> & CreateParams<Schema> : 'Please install Elysia before using Eden';
|
|
103
|
+
export type Sign<in out Route extends Record<any, any>> = { [K in keyof Route as K extends `:${string}` ? never : K]: Route[K] extends {
|
|
104
|
+
body: infer Body;
|
|
105
|
+
headers: infer Headers;
|
|
106
|
+
params: infer Params;
|
|
107
|
+
query: infer Query;
|
|
108
|
+
response: infer Res extends Record<number, unknown>;
|
|
109
|
+
} ? EdenTQMethod<Body, Headers, Query, Params, Res> : CreateParams<Route[K]> };
|
|
110
|
+
type CreateParams<Route extends Record<string, any>> = Extract<keyof Route, `:${string}`> extends infer Path extends string ? IsNever<Path> extends true ? Prettify<Sign<Route>> : (((params: { [param in Path extends `:${infer Param}` ? Param extends `${infer P}?` ? P : Param : never]: string | number }) => Prettify<Sign<Route[Path]>> & CreateParams<Route[Path]>) & Prettify<Sign<Route>>) & (Path extends `:${string}?` ? CreateParams<Route[Path]> : {}) : never;
|
|
111
|
+
export {};
|
|
80
112
|
}
|
|
81
|
-
|
|
113
|
+
interface EdenTQUtilsMethod<Body, Headers, Query, Params, Res extends Record<number, unknown>> {
|
|
114
|
+
queryKey(input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>): QueryKey$1;
|
|
115
|
+
queryOptions<TData = ExtractData<Res>>(input: TQMethodParam<Body, Headers, Query, Params>, overrides?: Partial<EdenQueryOptions<TData, ExtractError<Res>>>): EdenQueryOptions<TData, ExtractError<Res>>;
|
|
116
|
+
infiniteQueryOptions<TData = ExtractData<Res>, TPageParam = unknown>(input: InfiniteQueryInput<TPageParam, Query, Params>, opts: {
|
|
117
|
+
initialPageParam: TPageParam;
|
|
118
|
+
getNextPageParam: GetNextPageParamFunction<TPageParam, TData>;
|
|
119
|
+
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>;
|
|
120
|
+
cursorKey?: string;
|
|
121
|
+
}, overrides?: Partial<Omit<EdenInfiniteQueryOptions<TData, ExtractError<Res>, TPageParam>, 'queryKey' | 'queryFn' | 'initialPageParam' | 'getNextPageParam' | 'getPreviousPageParam'>>): EdenInfiniteQueryOptions<TData, ExtractError<Res>, TPageParam>;
|
|
122
|
+
mutationKey(input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>): QueryKey$1;
|
|
123
|
+
mutationOptions<TData = ExtractData<Res>>(overrides?: Partial<EdenMutationOptions<TData, ExtractError<Res>, TQMethodParam<Body, Headers, Query, Params>>>): EdenMutationOptions<TData, ExtractError<Res>, TQMethodParam<Body, Headers, Query, Params>>;
|
|
124
|
+
invalidate(input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>, exact?: boolean): Promise<void>;
|
|
125
|
+
prefetch(input: TQMethodParam<Body, Headers, Query, Params>): Promise<void>;
|
|
126
|
+
ensureData<TData = ExtractData<Res>>(input: TQMethodParam<Body, Headers, Query, Params>): Promise<TData>;
|
|
127
|
+
setData<TData = ExtractData<Res>>(input: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>, updater: TData | ((old: TData | undefined) => TData | undefined)): TData | undefined;
|
|
128
|
+
getData<TData = ExtractData<Res>>(input: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>): TData | undefined;
|
|
129
|
+
cancel(input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>): Promise<void>;
|
|
130
|
+
refetch(input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>): Promise<void>;
|
|
131
|
+
}
|
|
132
|
+
declare namespace EdenTQUtils {
|
|
133
|
+
export type Create<App extends Elysia<any, any, any, any, any, any, any>> = App extends {
|
|
134
|
+
'~Routes': infer Schema extends Record<any, any>;
|
|
135
|
+
} ? Prettify<Sign<Schema>> & CreateParams<Schema> : 'Please install Elysia before using Eden';
|
|
136
|
+
export type Sign<in out Route extends Record<any, any>> = { [K in keyof Route as K extends `:${string}` ? never : K]: Route[K] extends {
|
|
137
|
+
body: infer Body;
|
|
138
|
+
headers: infer Headers;
|
|
139
|
+
params: infer Params;
|
|
140
|
+
query: infer Query;
|
|
141
|
+
response: infer Res extends Record<number, unknown>;
|
|
142
|
+
} ? EdenTQUtilsMethod<Body, Headers, Query, Params, Res> : CreateParams<Route[K]> };
|
|
143
|
+
type CreateParams<Route extends Record<string, any>> = Extract<keyof Route, `:${string}`> extends infer Path extends string ? IsNever<Path> extends true ? Prettify<Sign<Route>> : (((params: { [param in Path extends `:${infer Param}` ? Param extends `${infer P}?` ? P : Param : never]: string | number }) => Prettify<Sign<Route[Path]>> & CreateParams<Route[Path]>) & Prettify<Sign<Route>>) & (Path extends `:${string}?` ? CreateParams<Route[Path]> : {}) : never;
|
|
144
|
+
export {};
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/index.d.ts
|
|
82
148
|
declare function createEdenTQ<const App extends Elysia<any, any, any, any, any, any, any>>(domain: string | App, config?: EdenTQ.Config): EdenTQ.Create<App>;
|
|
83
|
-
|
|
84
|
-
|
|
149
|
+
declare function createEdenTQUtils<const App extends Elysia<any, any, any, any, any, any, any>>(eden: EdenTQ.Create<App>, queryClient: QueryClient$1): EdenTQUtils.Create<App>;
|
|
150
|
+
//#endregion
|
|
151
|
+
export { type EdenInfiniteQueryOptions, type EdenMutationOptions, type EdenQueryOptions, type EdenTQ, type EdenTQUtils, type InfiniteData, type QueryClient, type QueryKey, createEdenTQ, createEdenTQUtils };
|
|
152
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"s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KAAA,iCAER,QAAA,CAAS,IAAA,CAAK,KAAA,CAAM,IAAA,MACtB,YAAA,CAAa,KAAA,CAAM,IAAA,MACnB,QAAA,CAAS,IAAA,CAAK,KAAA,OACb,IAAA,yBACK,YAAA,CAAa,KAAA,CAAM,IAAA;EAAA;AAAA;;;iBC7E/B,YAAA,mBACM,MAAA,oCAAA,CAElB,MAAA,WAAiB,GAAA,EACjB,MAAA,GAAQ,MAAA,CAAO,MAAA,GAChB,MAAA,CAAO,MAAA,CAAO,GAAA;AAAA,iBA2GD,iBAAA,mBACM,MAAA,oCAAA,CAElB,IAAA,EAAM,MAAA,CAAO,MAAA,CAAO,GAAA,GACpB,WAAA,EAAa,aAAA,GACd,WAAA,CAAY,MAAA,CAAO,GAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
import{treaty as
|
|
1
|
+
import{treaty as e}from"@elysiajs/eden/treaty2";const t=[`get`,`post`,`put`,`delete`,`patch`,`options`,`head`];function n(e){return t.includes(e)}function r(e,t){let n=[];for(let r of e){let e=/^:(.+?)(\?)?$/.exec(r);if(!e){n.push(r);continue}let i=e[1],a=!!e[2],o=t?.[i];if(o==null){if(a)continue;throw Error(`Missing required route parameter: "${i}"`)}n.push(String(o))}return n}function i(e,t,n,r){return[...e,t,n,r?.params??null,r?.query??null]}function a(e,t,n,r){let i=e;for(let e of t)i=i[e];let a={};return r?.query!==void 0&&(a.query=r.query),r?.headers!==void 0&&(a.headers=r.headers),r?.fetch!==void 0&&Object.assign(a,r.fetch),n===`get`||n===`head`?i[n](Object.keys(a).length>0?a:void 0):i[n](r?.body,Object.keys(a).length>0?a:void 0)}async function o(e){let t=await e;if(t.error)throw t.error;return t.data}function s(e,t,n){let s=[...t],c=t=>{let i=r(s,t?.params);return a(e.raw,i,n,t)};return c.queryKey=t=>i(e.prefix,n,s,t),c.queryOptions=(t,i)=>({queryKey:c.queryKey(t),queryFn:()=>{let i=r(s,t?.params);return o(a(e.raw,i,n,t))},...i}),c.infiniteQueryOptions=(t,i,l)=>{let u=i.cursorKey??t.cursorKey??`cursor`;return{queryKey:c.queryKey(t),queryFn:i=>{let c=r(s,t?.params),l={...t.query,[u]:i.pageParam};return o(a(e.raw,c,n,{...t,query:l}))},initialPageParam:i.initialPageParam,getNextPageParam:i.getNextPageParam,getPreviousPageParam:i.getPreviousPageParam,...l}},c.mutationKey=t=>i(e.prefix,n,s,t),c.mutationOptions=t=>({mutationKey:[...e.prefix,n,s],mutationFn:t=>{let i=t,c=r(s,i?.params);return o(a(e.raw,c,n,i))},...t}),c.invalidate=async(t,r,i=!1)=>{let a=r?c.queryKey(r):[...e.prefix,n,s];await t.invalidateQueries({queryKey:a,exact:i})},c.prefetch=async(e,t)=>{await e.prefetchQuery(c.queryOptions(t))},c.ensureData=async(e,t)=>e.ensureQueryData(c.queryOptions(t)),c.setData=(e,t,n)=>e.setQueryData(c.queryKey(t),n),c.getData=(e,t)=>e.getQueryData(c.queryKey(t)),c}const c=Symbol.for(`eden-tq-ctx`);function l(e,t=[]){return new Proxy(()=>{},{get(r,i){if(i===c)return e;if(typeof i!=`symbol`)return n(i)?s(e,t,i):l(e,i===`index`?t:[...t,i])},apply(n,r,[i]){if(typeof i==`object`&&i){let n=Object.values(i)[0];return l(e,[...t,n])}return l(e,t)}})}function u(t,n={}){let{queryKeyPrefix:r=[`eden`],...i}=n;return l({raw:e(t,i),prefix:r})}function d(e,t,n){let r=[...t],i=s(e,t,n);return{queryKey:i.queryKey,queryOptions:i.queryOptions,infiniteQueryOptions:i.infiniteQueryOptions,mutationKey:i.mutationKey,mutationOptions:i.mutationOptions,invalidate:async(t,n=!1)=>i.invalidate(e.queryClient,t,n),prefetch:async t=>i.prefetch(e.queryClient,t),ensureData:async t=>i.ensureData(e.queryClient,t),setData:(t,n)=>i.setData(e.queryClient,t,n),getData:t=>i.getData(e.queryClient,t),cancel:async t=>{let a=t?i.queryKey(t):[...e.prefix,n,r];await e.queryClient.cancelQueries({queryKey:a})},refetch:async t=>{let a=t?i.queryKey(t):[...e.prefix,n,r];await e.queryClient.refetchQueries({queryKey:a})}}}function f(e,t=[]){return new Proxy(()=>{},{get(r,i){return n(i)?d(e,t,i):f(e,i===`index`?t:[...t,i])},apply(n,r,[i]){if(typeof i==`object`&&i){let n=Object.values(i)[0];return f(e,[...t,n])}return f(e,t)}})}function p(e,t){let n=e[c];if(!n)throw Error(`Invalid eden instance. Make sure you pass the result of createEdenTQ.`);return f({...n,queryClient:t})}export{u as createEdenTQ,p as createEdenTQUtils};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Elysia } from 'elysia'\nimport type { QueryKey, QueryClient, QueryFunctionContext } from '@tanstack/query-core'\nimport { treaty, type Treaty } from '@elysiajs/eden/treaty2'\nimport type {\n EdenTQ,\n EdenTQUtils,\n EdenQueryOptions,\n EdenInfiniteQueryOptions,\n EdenMutationOptions\n} from './types'\n\nconst HTTP_METHODS = [\n 'get',\n 'post',\n 'put',\n 'delete',\n 'patch',\n 'options',\n 'head'\n] as const\n\ntype HttpMethod = (typeof HTTP_METHODS)[number]\n\nfunction isHttpMethod(value: string): value is HttpMethod {\n return HTTP_METHODS.includes(value as HttpMethod)\n}\n\nfunction materializePath(\n paths: string[],\n params?: Record<string, string | number>\n): string[] {\n const result: string[] = []\n\n for (const segment of paths) {\n const match = /^:(.+?)(\\?)?$/.exec(segment)\n if (!match) {\n result.push(segment)\n continue\n }\n\n const paramName = match[1]\n const isOptional = !!match[2]\n const value = params?.[paramName]\n\n if (value == null) {\n if (isOptional) continue\n throw new Error(`Missing required route parameter: \"${paramName}\"`)\n }\n\n result.push(String(value))\n }\n\n return result\n}\n\nfunction buildQueryKey(\n prefix: QueryKey,\n method: string,\n pathTemplate: string[],\n input?: { params?: unknown; query?: unknown }\n): QueryKey {\n return [\n ...prefix,\n method,\n pathTemplate,\n input?.params ?? null,\n input?.query ?? null\n ] as const\n}\n\nfunction callTreaty(\n raw: any,\n segments: string[],\n method: string,\n input?: { body?: unknown; query?: unknown; headers?: unknown; fetch?: RequestInit }\n): Promise<Treaty.TreatyResponse<any>> {\n let current = raw\n\n for (const segment of segments) {\n current = current[segment]\n }\n\n const options: Record<string, unknown> = {}\n if (input?.query !== undefined) options.query = input.query\n if (input?.headers !== undefined) options.headers = input.headers\n if (input?.fetch !== undefined) Object.assign(options, input.fetch)\n\n if (method === 'get' || method === 'head') {\n return current[method](Object.keys(options).length > 0 ? options : undefined)\n }\n\n return current[method](\n input?.body,\n Object.keys(options).length > 0 ? options : undefined\n )\n}\n\nasync function dataOrThrow<T>(\n promise: Promise<Treaty.TreatyResponse<any>>\n): Promise<T> {\n const result = await promise\n if (result.error) throw result.error\n return result.data as T\n}\n\ninterface ProxyContext {\n raw: any\n prefix: QueryKey\n}\n\ninterface MethodDecoratorInput {\n params?: Record<string, string | number>\n body?: unknown\n query?: unknown\n headers?: unknown\n fetch?: RequestInit\n}\n\ninterface InfiniteQueryOpts<TData, TPageParam> {\n initialPageParam: TPageParam\n getNextPageParam: (lastPage: TData, allPages: TData[], lastPageParam: TPageParam, allPageParams: TPageParam[]) => TPageParam | undefined | null\n getPreviousPageParam?: (firstPage: TData, allPages: TData[], firstPageParam: TPageParam, allPageParams: TPageParam[]) => TPageParam | undefined | null\n cursorKey?: string\n}\n\nfunction createMethodDecorator(\n ctx: ProxyContext,\n paths: string[],\n method: string\n) {\n const pathTemplate = [...paths]\n\n const fn = (input?: MethodDecoratorInput) => {\n const materializedPath = materializePath(pathTemplate, input?.params)\n return callTreaty(ctx.raw, materializedPath, method, input)\n }\n\n fn.queryKey = (\n input?: { params?: Record<string, string | number>; query?: unknown }\n ): QueryKey => {\n return buildQueryKey(ctx.prefix, method, pathTemplate, input)\n }\n\n fn.queryOptions = <TData = unknown>(\n input: MethodDecoratorInput,\n overrides?: Partial<EdenQueryOptions<TData>>\n ): EdenQueryOptions<TData> => {\n return {\n queryKey: fn.queryKey(input),\n queryFn: () => {\n const materializedPath = materializePath(pathTemplate, input?.params)\n return dataOrThrow(callTreaty(ctx.raw, materializedPath, method, input))\n },\n ...overrides\n }\n }\n\n fn.infiniteQueryOptions = <TData = unknown, TPageParam = unknown>(\n input: { params?: Record<string, string | number>; query?: unknown; headers?: unknown; fetch?: RequestInit; cursorKey?: string },\n opts: InfiniteQueryOpts<TData, TPageParam>,\n overrides?: Partial<EdenInfiniteQueryOptions<TData, unknown, TPageParam>>\n ): EdenInfiniteQueryOptions<TData, unknown, TPageParam> => {\n const cursorKey = opts.cursorKey ?? input.cursorKey ?? 'cursor'\n\n return {\n queryKey: fn.queryKey(input),\n queryFn: (context: QueryFunctionContext<QueryKey, TPageParam>) => {\n const materializedPath = materializePath(pathTemplate, input?.params)\n const queryWithCursor = {\n ...input.query as Record<string, unknown>,\n [cursorKey]: context.pageParam\n }\n return dataOrThrow<TData>(callTreaty(ctx.raw, materializedPath, method, {\n ...input,\n query: queryWithCursor\n }))\n },\n initialPageParam: opts.initialPageParam,\n getNextPageParam: opts.getNextPageParam,\n getPreviousPageParam: opts.getPreviousPageParam,\n ...overrides\n }\n }\n\n fn.mutationKey = (\n input?: { params?: Record<string, string | number>; query?: unknown }\n ): QueryKey => {\n return buildQueryKey(ctx.prefix, method, pathTemplate, input)\n }\n\n fn.mutationOptions = <TData = unknown, TVariables = unknown>(\n overrides?: Partial<EdenMutationOptions<TData, unknown, TVariables>>\n ): EdenMutationOptions<TData, unknown, TVariables> => {\n return {\n mutationKey: [...ctx.prefix, method, pathTemplate],\n mutationFn: (variables: TVariables) => {\n const vars = variables as MethodDecoratorInput\n const materializedPath = materializePath(pathTemplate, vars?.params)\n return dataOrThrow(callTreaty(ctx.raw, materializedPath, method, vars))\n },\n ...overrides\n }\n }\n\n fn.invalidate = async (\n queryClient: QueryClient,\n input?: { params?: Record<string, string | number>; query?: unknown },\n exact = false\n ): Promise<void> => {\n const queryKey = input\n ? fn.queryKey(input)\n : [...ctx.prefix, method, pathTemplate]\n await queryClient.invalidateQueries({ queryKey, exact })\n }\n\n fn.prefetch = async (\n queryClient: QueryClient,\n input: MethodDecoratorInput\n ): Promise<void> => {\n await queryClient.prefetchQuery(fn.queryOptions(input))\n }\n\n fn.ensureData = async <TData = unknown>(\n queryClient: QueryClient,\n input: MethodDecoratorInput\n ): Promise<TData> => {\n return queryClient.ensureQueryData(fn.queryOptions<TData>(input))\n }\n\n fn.setData = <TData = unknown>(\n queryClient: QueryClient,\n input: { params?: Record<string, string | number>; query?: unknown },\n updater: TData | ((old: TData | undefined) => TData | undefined)\n ): TData | undefined => {\n return queryClient.setQueryData<TData>(fn.queryKey(input), updater)\n }\n\n fn.getData = <TData = unknown>(\n queryClient: QueryClient,\n input: { params?: Record<string, string | number>; query?: unknown }\n ): TData | undefined => {\n return queryClient.getQueryData<TData>(fn.queryKey(input))\n }\n\n return fn\n}\n\nconst CTX_SYMBOL = Symbol.for('eden-tq-ctx')\n\nfunction createEdenTQProxy(\n ctx: ProxyContext,\n paths: string[] = []\n): any {\n return new Proxy(() => {}, {\n get(_, prop: string | symbol): any {\n if (prop === CTX_SYMBOL) {\n return ctx\n }\n\n if (typeof prop === 'symbol') {\n return undefined\n }\n\n if (isHttpMethod(prop)) {\n return createMethodDecorator(ctx, paths, prop)\n }\n\n return createEdenTQProxy(\n ctx,\n prop === 'index' ? paths : [...paths, prop]\n )\n },\n apply(_, __, [body]) {\n if (typeof body === 'object' && body !== null) {\n const paramValue = Object.values(body)[0] as string\n return createEdenTQProxy(ctx, [...paths, paramValue])\n }\n return createEdenTQProxy(ctx, paths)\n }\n })\n}\n\nexport function createEdenTQ<\n const App extends Elysia<any, any, any, any, any, any, any>\n>(\n domain: string | App,\n config: EdenTQ.Config = {}\n): EdenTQ.Create<App> {\n const { queryKeyPrefix = ['eden'], ...treatyConfig } = config\n\n const raw = treaty<App>(domain as any, treatyConfig)\n\n const ctx: ProxyContext = {\n raw,\n prefix: queryKeyPrefix\n }\n\n return createEdenTQProxy(ctx) as EdenTQ.Create<App>\n}\n\ninterface UtilsProxyContext extends ProxyContext {\n queryClient: QueryClient\n}\n\nfunction createUtilsMethodDecorator(\n ctx: UtilsProxyContext,\n paths: string[],\n method: string\n) {\n const pathTemplate = [...paths]\n const baseDecorator = createMethodDecorator(ctx, paths, method)\n\n const fn = {\n queryKey: baseDecorator.queryKey,\n queryOptions: baseDecorator.queryOptions,\n infiniteQueryOptions: baseDecorator.infiniteQueryOptions,\n mutationKey: baseDecorator.mutationKey,\n mutationOptions: baseDecorator.mutationOptions,\n\n invalidate: async (\n input?: { params?: Record<string, string | number>; query?: unknown },\n exact = false\n ): Promise<void> => {\n return baseDecorator.invalidate(ctx.queryClient, input, exact)\n },\n\n prefetch: async (input: MethodDecoratorInput): Promise<void> => {\n return baseDecorator.prefetch(ctx.queryClient, input)\n },\n\n ensureData: async <TData = unknown>(input: MethodDecoratorInput): Promise<TData> => {\n return baseDecorator.ensureData<TData>(ctx.queryClient, input)\n },\n\n setData: <TData = unknown>(\n input: { params?: Record<string, string | number>; query?: unknown },\n updater: TData | ((old: TData | undefined) => TData | undefined)\n ): TData | undefined => {\n return baseDecorator.setData<TData>(ctx.queryClient, input, updater)\n },\n\n getData: <TData = unknown>(\n input: { params?: Record<string, string | number>; query?: unknown }\n ): TData | undefined => {\n return baseDecorator.getData<TData>(ctx.queryClient, input)\n },\n\n cancel: async (\n input?: { params?: Record<string, string | number>; query?: unknown }\n ): Promise<void> => {\n const queryKey = input\n ? baseDecorator.queryKey(input)\n : [...ctx.prefix, method, pathTemplate]\n await ctx.queryClient.cancelQueries({ queryKey })\n },\n\n refetch: async (\n input?: { params?: Record<string, string | number>; query?: unknown }\n ): Promise<void> => {\n const queryKey = input\n ? baseDecorator.queryKey(input)\n : [...ctx.prefix, method, pathTemplate]\n await ctx.queryClient.refetchQueries({ queryKey })\n }\n }\n\n return fn\n}\n\nfunction createEdenTQUtilsProxy(\n ctx: UtilsProxyContext,\n paths: string[] = []\n): any {\n return new Proxy(() => {}, {\n get(_, prop: string): any {\n if (isHttpMethod(prop)) {\n return createUtilsMethodDecorator(ctx, paths, prop)\n }\n\n return createEdenTQUtilsProxy(\n ctx,\n prop === 'index' ? paths : [...paths, prop]\n )\n },\n apply(_, __, [body]) {\n if (typeof body === 'object' && body !== null) {\n const paramValue = Object.values(body)[0] as string\n return createEdenTQUtilsProxy(ctx, [...paths, paramValue])\n }\n return createEdenTQUtilsProxy(ctx, paths)\n }\n })\n}\n\nexport function createEdenTQUtils<\n const App extends Elysia<any, any, any, any, any, any, any>\n>(\n eden: EdenTQ.Create<App>,\n queryClient: QueryClient\n): EdenTQUtils.Create<App> {\n const ctx = (eden as any)[CTX_SYMBOL] as ProxyContext\n\n if (!ctx) {\n throw new Error('Invalid eden instance. Make sure you pass the result of createEdenTQ.')\n }\n\n const utilsCtx: UtilsProxyContext = {\n ...ctx,\n queryClient\n }\n\n return createEdenTQUtilsProxy(utilsCtx) as EdenTQUtils.Create<App>\n}\n\nexport type { EdenTQ, EdenTQUtils, EdenQueryOptions, EdenInfiniteQueryOptions, EdenMutationOptions }\nexport type { QueryKey, QueryClient, InfiniteData } from '@tanstack/query-core'\n"],"mappings":"gDAWA,MAAM,EAAe,CACjB,MACA,OACA,MACA,SACA,QACA,UACA,OACH,CAID,SAAS,EAAa,EAAoC,CACtD,OAAO,EAAa,SAAS,EAAoB,CAGrD,SAAS,EACL,EACA,EACQ,CACR,IAAM,EAAmB,EAAE,CAE3B,IAAK,IAAM,KAAW,EAAO,CACzB,IAAM,EAAQ,gBAAgB,KAAK,EAAQ,CAC3C,GAAI,CAAC,EAAO,CACR,EAAO,KAAK,EAAQ,CACpB,SAGJ,IAAM,EAAY,EAAM,GAClB,EAAa,CAAC,CAAC,EAAM,GACrB,EAAQ,IAAS,GAEvB,GAAI,GAAS,KAAM,CACf,GAAI,EAAY,SAChB,MAAU,MAAM,sCAAsC,EAAU,GAAG,CAGvE,EAAO,KAA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3C,MAAM,EAAI,YAAY,cAAc,CAAE,WAAU,CAAC,EAGrD,QAAS,KACL,IACgB,CAChB,IAAM,EAAW,EACX,EAAc,SAAS,EAAM,CAC7B,CAAC,GAAG,EAAI,OAAQ,EAAQ,EAAa,CAC3C,MAAM,EAAI,YAAY,eAAe,CAAE,WAAU,CAAC,EAEzD,CAKL,SAAS,EACL,EACA,EAAkB,EAAE,CACjB,CACH,OAAO,IAAI,UAAY,GAAI,CACvB,IAAI,EAAG,EAAmB,CAKtB,OAJI,EAAa,EAAK,CACX,EAA2B,EAAK,EAAO,EAAK,CAGhD,EACH,EACA,IAAS,QAAU,EAAQ,CAAC,GAAG,EAAO,EAAK,CAC9C,EAEL,MAAM,EAAG,EAAI,CAAC,GAAO,CACjB,GAAI,OAAO,GAAS,UAAY,EAAe,CAC3C,IAAM,EAAa,OAAO,OAAO,EAAK,CAAC,GACvC,OAAO,EAAuB,EAAK,CAAC,GAAG,EAAO,EAAW,CAAC,CAE9D,OAAO,EAAuB,EAAK,EAAM,EAEhD,CAAC,CAGN,SAAgB,EAGZ,EACA,EACuB,CACvB,IAAM,EAAO,EAAa,GAE1B,GAAI,CAAC,EACD,MAAU,MAAM,wEAAwE,CAQ5F,OAAO,EAL6B,CAChC,GAAG,EACH,cACH,CAEsC"}
|
package/package.json
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eden-tanstack-query",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.3",
|
|
4
4
|
"description": "TanStack Query integration for Elysia Eden - type-safe queries and mutations",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Frank-III",
|
|
7
7
|
"url": "https://github.com/Frank-III"
|
|
8
8
|
},
|
|
9
|
-
"
|
|
10
|
-
"
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.mjs",
|
|
11
11
|
"exports": {
|
|
12
12
|
"./package.json": "./package.json",
|
|
13
13
|
".": {
|
|
14
|
-
"types": "./dist/index.d.
|
|
15
|
-
"import": "./dist/index.mjs"
|
|
16
|
-
"require": "./dist/index.js"
|
|
14
|
+
"types": "./dist/index.d.mts",
|
|
15
|
+
"import": "./dist/index.mjs"
|
|
17
16
|
}
|
|
18
17
|
},
|
|
19
|
-
"types": "./dist/index.d.
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
20
19
|
"files": [
|
|
21
20
|
"dist",
|
|
22
21
|
"src"
|
|
@@ -40,7 +39,7 @@
|
|
|
40
39
|
"bugs": "https://github.com/Frank-III/eden/issues",
|
|
41
40
|
"license": "MIT",
|
|
42
41
|
"scripts": {
|
|
43
|
-
"build": "
|
|
42
|
+
"build": "tsdown",
|
|
44
43
|
"test": "bun test",
|
|
45
44
|
"prepublishOnly": "bun run build"
|
|
46
45
|
},
|
|
@@ -55,7 +54,7 @@
|
|
|
55
54
|
"@types/bun": "^1.3.8",
|
|
56
55
|
"elysia": "^1.4.22",
|
|
57
56
|
"expect-type": "^1.3.0",
|
|
58
|
-
"
|
|
57
|
+
"tsdown": "^0.20.1",
|
|
59
58
|
"typescript": "^5.9.3"
|
|
60
59
|
}
|
|
61
60
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import type { Elysia } from 'elysia'
|
|
2
|
-
import type { QueryKey, QueryClient } from '@tanstack/query-core'
|
|
2
|
+
import type { QueryKey, QueryClient, QueryFunctionContext } from '@tanstack/query-core'
|
|
3
3
|
import { treaty, type Treaty } from '@elysiajs/eden/treaty2'
|
|
4
|
-
import type {
|
|
4
|
+
import type {
|
|
5
|
+
EdenTQ,
|
|
6
|
+
EdenTQUtils,
|
|
7
|
+
EdenQueryOptions,
|
|
8
|
+
EdenInfiniteQueryOptions,
|
|
9
|
+
EdenMutationOptions
|
|
10
|
+
} from './types'
|
|
5
11
|
|
|
6
12
|
const HTTP_METHODS = [
|
|
7
13
|
'get',
|
|
@@ -102,6 +108,21 @@ interface ProxyContext {
|
|
|
102
108
|
prefix: QueryKey
|
|
103
109
|
}
|
|
104
110
|
|
|
111
|
+
interface MethodDecoratorInput {
|
|
112
|
+
params?: Record<string, string | number>
|
|
113
|
+
body?: unknown
|
|
114
|
+
query?: unknown
|
|
115
|
+
headers?: unknown
|
|
116
|
+
fetch?: RequestInit
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
interface InfiniteQueryOpts<TData, TPageParam> {
|
|
120
|
+
initialPageParam: TPageParam
|
|
121
|
+
getNextPageParam: (lastPage: TData, allPages: TData[], lastPageParam: TPageParam, allPageParams: TPageParam[]) => TPageParam | undefined | null
|
|
122
|
+
getPreviousPageParam?: (firstPage: TData, allPages: TData[], firstPageParam: TPageParam, allPageParams: TPageParam[]) => TPageParam | undefined | null
|
|
123
|
+
cursorKey?: string
|
|
124
|
+
}
|
|
125
|
+
|
|
105
126
|
function createMethodDecorator(
|
|
106
127
|
ctx: ProxyContext,
|
|
107
128
|
paths: string[],
|
|
@@ -109,9 +130,7 @@ function createMethodDecorator(
|
|
|
109
130
|
) {
|
|
110
131
|
const pathTemplate = [...paths]
|
|
111
132
|
|
|
112
|
-
const fn = (
|
|
113
|
-
input?: { params?: Record<string, string | number>; body?: unknown; query?: unknown; headers?: unknown; fetch?: RequestInit }
|
|
114
|
-
) => {
|
|
133
|
+
const fn = (input?: MethodDecoratorInput) => {
|
|
115
134
|
const materializedPath = materializePath(pathTemplate, input?.params)
|
|
116
135
|
return callTreaty(ctx.raw, materializedPath, method, input)
|
|
117
136
|
}
|
|
@@ -123,7 +142,7 @@ function createMethodDecorator(
|
|
|
123
142
|
}
|
|
124
143
|
|
|
125
144
|
fn.queryOptions = <TData = unknown>(
|
|
126
|
-
input:
|
|
145
|
+
input: MethodDecoratorInput,
|
|
127
146
|
overrides?: Partial<EdenQueryOptions<TData>>
|
|
128
147
|
): EdenQueryOptions<TData> => {
|
|
129
148
|
return {
|
|
@@ -136,6 +155,33 @@ function createMethodDecorator(
|
|
|
136
155
|
}
|
|
137
156
|
}
|
|
138
157
|
|
|
158
|
+
fn.infiniteQueryOptions = <TData = unknown, TPageParam = unknown>(
|
|
159
|
+
input: { params?: Record<string, string | number>; query?: unknown; headers?: unknown; fetch?: RequestInit; cursorKey?: string },
|
|
160
|
+
opts: InfiniteQueryOpts<TData, TPageParam>,
|
|
161
|
+
overrides?: Partial<EdenInfiniteQueryOptions<TData, unknown, TPageParam>>
|
|
162
|
+
): EdenInfiniteQueryOptions<TData, unknown, TPageParam> => {
|
|
163
|
+
const cursorKey = opts.cursorKey ?? input.cursorKey ?? 'cursor'
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
queryKey: fn.queryKey(input),
|
|
167
|
+
queryFn: (context: QueryFunctionContext<QueryKey, TPageParam>) => {
|
|
168
|
+
const materializedPath = materializePath(pathTemplate, input?.params)
|
|
169
|
+
const queryWithCursor = {
|
|
170
|
+
...input.query as Record<string, unknown>,
|
|
171
|
+
[cursorKey]: context.pageParam
|
|
172
|
+
}
|
|
173
|
+
return dataOrThrow<TData>(callTreaty(ctx.raw, materializedPath, method, {
|
|
174
|
+
...input,
|
|
175
|
+
query: queryWithCursor
|
|
176
|
+
}))
|
|
177
|
+
},
|
|
178
|
+
initialPageParam: opts.initialPageParam,
|
|
179
|
+
getNextPageParam: opts.getNextPageParam,
|
|
180
|
+
getPreviousPageParam: opts.getPreviousPageParam,
|
|
181
|
+
...overrides
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
139
185
|
fn.mutationKey = (
|
|
140
186
|
input?: { params?: Record<string, string | number>; query?: unknown }
|
|
141
187
|
): QueryKey => {
|
|
@@ -148,7 +194,7 @@ function createMethodDecorator(
|
|
|
148
194
|
return {
|
|
149
195
|
mutationKey: [...ctx.prefix, method, pathTemplate],
|
|
150
196
|
mutationFn: (variables: TVariables) => {
|
|
151
|
-
const vars = variables as
|
|
197
|
+
const vars = variables as MethodDecoratorInput
|
|
152
198
|
const materializedPath = materializePath(pathTemplate, vars?.params)
|
|
153
199
|
return dataOrThrow(callTreaty(ctx.raw, materializedPath, method, vars))
|
|
154
200
|
},
|
|
@@ -167,15 +213,54 @@ function createMethodDecorator(
|
|
|
167
213
|
await queryClient.invalidateQueries({ queryKey, exact })
|
|
168
214
|
}
|
|
169
215
|
|
|
216
|
+
fn.prefetch = async (
|
|
217
|
+
queryClient: QueryClient,
|
|
218
|
+
input: MethodDecoratorInput
|
|
219
|
+
): Promise<void> => {
|
|
220
|
+
await queryClient.prefetchQuery(fn.queryOptions(input))
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
fn.ensureData = async <TData = unknown>(
|
|
224
|
+
queryClient: QueryClient,
|
|
225
|
+
input: MethodDecoratorInput
|
|
226
|
+
): Promise<TData> => {
|
|
227
|
+
return queryClient.ensureQueryData(fn.queryOptions<TData>(input))
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
fn.setData = <TData = unknown>(
|
|
231
|
+
queryClient: QueryClient,
|
|
232
|
+
input: { params?: Record<string, string | number>; query?: unknown },
|
|
233
|
+
updater: TData | ((old: TData | undefined) => TData | undefined)
|
|
234
|
+
): TData | undefined => {
|
|
235
|
+
return queryClient.setQueryData<TData>(fn.queryKey(input), updater)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
fn.getData = <TData = unknown>(
|
|
239
|
+
queryClient: QueryClient,
|
|
240
|
+
input: { params?: Record<string, string | number>; query?: unknown }
|
|
241
|
+
): TData | undefined => {
|
|
242
|
+
return queryClient.getQueryData<TData>(fn.queryKey(input))
|
|
243
|
+
}
|
|
244
|
+
|
|
170
245
|
return fn
|
|
171
246
|
}
|
|
172
247
|
|
|
248
|
+
const CTX_SYMBOL = Symbol.for('eden-tq-ctx')
|
|
249
|
+
|
|
173
250
|
function createEdenTQProxy(
|
|
174
251
|
ctx: ProxyContext,
|
|
175
252
|
paths: string[] = []
|
|
176
253
|
): any {
|
|
177
254
|
return new Proxy(() => {}, {
|
|
178
|
-
get(_, prop: string): any {
|
|
255
|
+
get(_, prop: string | symbol): any {
|
|
256
|
+
if (prop === CTX_SYMBOL) {
|
|
257
|
+
return ctx
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (typeof prop === 'symbol') {
|
|
261
|
+
return undefined
|
|
262
|
+
}
|
|
263
|
+
|
|
179
264
|
if (isHttpMethod(prop)) {
|
|
180
265
|
return createMethodDecorator(ctx, paths, prop)
|
|
181
266
|
}
|
|
@@ -213,5 +298,119 @@ export function createEdenTQ<
|
|
|
213
298
|
return createEdenTQProxy(ctx) as EdenTQ.Create<App>
|
|
214
299
|
}
|
|
215
300
|
|
|
216
|
-
|
|
217
|
-
|
|
301
|
+
interface UtilsProxyContext extends ProxyContext {
|
|
302
|
+
queryClient: QueryClient
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function createUtilsMethodDecorator(
|
|
306
|
+
ctx: UtilsProxyContext,
|
|
307
|
+
paths: string[],
|
|
308
|
+
method: string
|
|
309
|
+
) {
|
|
310
|
+
const pathTemplate = [...paths]
|
|
311
|
+
const baseDecorator = createMethodDecorator(ctx, paths, method)
|
|
312
|
+
|
|
313
|
+
const fn = {
|
|
314
|
+
queryKey: baseDecorator.queryKey,
|
|
315
|
+
queryOptions: baseDecorator.queryOptions,
|
|
316
|
+
infiniteQueryOptions: baseDecorator.infiniteQueryOptions,
|
|
317
|
+
mutationKey: baseDecorator.mutationKey,
|
|
318
|
+
mutationOptions: baseDecorator.mutationOptions,
|
|
319
|
+
|
|
320
|
+
invalidate: async (
|
|
321
|
+
input?: { params?: Record<string, string | number>; query?: unknown },
|
|
322
|
+
exact = false
|
|
323
|
+
): Promise<void> => {
|
|
324
|
+
return baseDecorator.invalidate(ctx.queryClient, input, exact)
|
|
325
|
+
},
|
|
326
|
+
|
|
327
|
+
prefetch: async (input: MethodDecoratorInput): Promise<void> => {
|
|
328
|
+
return baseDecorator.prefetch(ctx.queryClient, input)
|
|
329
|
+
},
|
|
330
|
+
|
|
331
|
+
ensureData: async <TData = unknown>(input: MethodDecoratorInput): Promise<TData> => {
|
|
332
|
+
return baseDecorator.ensureData<TData>(ctx.queryClient, input)
|
|
333
|
+
},
|
|
334
|
+
|
|
335
|
+
setData: <TData = unknown>(
|
|
336
|
+
input: { params?: Record<string, string | number>; query?: unknown },
|
|
337
|
+
updater: TData | ((old: TData | undefined) => TData | undefined)
|
|
338
|
+
): TData | undefined => {
|
|
339
|
+
return baseDecorator.setData<TData>(ctx.queryClient, input, updater)
|
|
340
|
+
},
|
|
341
|
+
|
|
342
|
+
getData: <TData = unknown>(
|
|
343
|
+
input: { params?: Record<string, string | number>; query?: unknown }
|
|
344
|
+
): TData | undefined => {
|
|
345
|
+
return baseDecorator.getData<TData>(ctx.queryClient, input)
|
|
346
|
+
},
|
|
347
|
+
|
|
348
|
+
cancel: async (
|
|
349
|
+
input?: { params?: Record<string, string | number>; query?: unknown }
|
|
350
|
+
): Promise<void> => {
|
|
351
|
+
const queryKey = input
|
|
352
|
+
? baseDecorator.queryKey(input)
|
|
353
|
+
: [...ctx.prefix, method, pathTemplate]
|
|
354
|
+
await ctx.queryClient.cancelQueries({ queryKey })
|
|
355
|
+
},
|
|
356
|
+
|
|
357
|
+
refetch: async (
|
|
358
|
+
input?: { params?: Record<string, string | number>; query?: unknown }
|
|
359
|
+
): Promise<void> => {
|
|
360
|
+
const queryKey = input
|
|
361
|
+
? baseDecorator.queryKey(input)
|
|
362
|
+
: [...ctx.prefix, method, pathTemplate]
|
|
363
|
+
await ctx.queryClient.refetchQueries({ queryKey })
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return fn
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function createEdenTQUtilsProxy(
|
|
371
|
+
ctx: UtilsProxyContext,
|
|
372
|
+
paths: string[] = []
|
|
373
|
+
): any {
|
|
374
|
+
return new Proxy(() => {}, {
|
|
375
|
+
get(_, prop: string): any {
|
|
376
|
+
if (isHttpMethod(prop)) {
|
|
377
|
+
return createUtilsMethodDecorator(ctx, paths, prop)
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
return createEdenTQUtilsProxy(
|
|
381
|
+
ctx,
|
|
382
|
+
prop === 'index' ? paths : [...paths, prop]
|
|
383
|
+
)
|
|
384
|
+
},
|
|
385
|
+
apply(_, __, [body]) {
|
|
386
|
+
if (typeof body === 'object' && body !== null) {
|
|
387
|
+
const paramValue = Object.values(body)[0] as string
|
|
388
|
+
return createEdenTQUtilsProxy(ctx, [...paths, paramValue])
|
|
389
|
+
}
|
|
390
|
+
return createEdenTQUtilsProxy(ctx, paths)
|
|
391
|
+
}
|
|
392
|
+
})
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export function createEdenTQUtils<
|
|
396
|
+
const App extends Elysia<any, any, any, any, any, any, any>
|
|
397
|
+
>(
|
|
398
|
+
eden: EdenTQ.Create<App>,
|
|
399
|
+
queryClient: QueryClient
|
|
400
|
+
): EdenTQUtils.Create<App> {
|
|
401
|
+
const ctx = (eden as any)[CTX_SYMBOL] as ProxyContext
|
|
402
|
+
|
|
403
|
+
if (!ctx) {
|
|
404
|
+
throw new Error('Invalid eden instance. Make sure you pass the result of createEdenTQ.')
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const utilsCtx: UtilsProxyContext = {
|
|
408
|
+
...ctx,
|
|
409
|
+
queryClient
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
return createEdenTQUtilsProxy(utilsCtx) as EdenTQUtils.Create<App>
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export type { EdenTQ, EdenTQUtils, EdenQueryOptions, EdenInfiniteQueryOptions, EdenMutationOptions }
|
|
416
|
+
export type { QueryKey, QueryClient, InfiniteData } from '@tanstack/query-core'
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import type { Elysia, ELYSIA_FORM_DATA } from 'elysia'
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
QueryKey,
|
|
4
|
+
QueryClient,
|
|
5
|
+
InfiniteData,
|
|
6
|
+
GetNextPageParamFunction,
|
|
7
|
+
GetPreviousPageParamFunction,
|
|
8
|
+
QueryFunctionContext
|
|
9
|
+
} from '@tanstack/query-core'
|
|
3
10
|
import type { Treaty } from '@elysiajs/eden/treaty2'
|
|
4
11
|
|
|
5
12
|
type IsNever<T> = [T] extends [never] ? true : false
|
|
@@ -87,6 +94,35 @@ type TQMethodParam<
|
|
|
87
94
|
export interface EdenQueryOptions<TData = unknown, TError = unknown> {
|
|
88
95
|
queryKey: QueryKey
|
|
89
96
|
queryFn: () => Promise<TData>
|
|
97
|
+
enabled?: boolean
|
|
98
|
+
staleTime?: number
|
|
99
|
+
gcTime?: number
|
|
100
|
+
refetchOnMount?: boolean | 'always'
|
|
101
|
+
refetchOnWindowFocus?: boolean | 'always'
|
|
102
|
+
refetchOnReconnect?: boolean | 'always'
|
|
103
|
+
refetchInterval?: number | false
|
|
104
|
+
retry?: boolean | number | ((failureCount: number, error: TError) => boolean)
|
|
105
|
+
retryDelay?: number | ((failureCount: number, error: TError) => number)
|
|
106
|
+
placeholderData?: TData | (() => TData | undefined)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface EdenInfiniteQueryOptions<
|
|
110
|
+
TData = unknown,
|
|
111
|
+
TError = unknown,
|
|
112
|
+
TPageParam = unknown
|
|
113
|
+
> {
|
|
114
|
+
queryKey: QueryKey
|
|
115
|
+
queryFn: (context: QueryFunctionContext<QueryKey, TPageParam>) => Promise<TData>
|
|
116
|
+
initialPageParam: TPageParam
|
|
117
|
+
getNextPageParam: GetNextPageParamFunction<TPageParam, TData>
|
|
118
|
+
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>
|
|
119
|
+
enabled?: boolean
|
|
120
|
+
staleTime?: number
|
|
121
|
+
gcTime?: number
|
|
122
|
+
refetchOnMount?: boolean | 'always'
|
|
123
|
+
refetchOnWindowFocus?: boolean | 'always'
|
|
124
|
+
refetchOnReconnect?: boolean | 'always'
|
|
125
|
+
maxPages?: number
|
|
90
126
|
}
|
|
91
127
|
|
|
92
128
|
export interface EdenMutationOptions<
|
|
@@ -96,6 +132,22 @@ export interface EdenMutationOptions<
|
|
|
96
132
|
> {
|
|
97
133
|
mutationKey: QueryKey
|
|
98
134
|
mutationFn: (variables: TVariables) => Promise<TData>
|
|
135
|
+
onMutate?: (variables: TVariables) => Promise<unknown> | unknown
|
|
136
|
+
onSuccess?: (data: TData, variables: TVariables, context: unknown) => Promise<unknown> | unknown
|
|
137
|
+
onError?: (error: TError, variables: TVariables, context: unknown) => Promise<unknown> | unknown
|
|
138
|
+
onSettled?: (data: TData | undefined, error: TError | null, variables: TVariables, context: unknown) => Promise<unknown> | unknown
|
|
139
|
+
retry?: boolean | number | ((failureCount: number, error: TError) => boolean)
|
|
140
|
+
retryDelay?: number | ((failureCount: number, error: TError) => number)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
type OmitQueryInput<T> = Omit<T, 'body' | 'headers' | 'fetch'>
|
|
144
|
+
|
|
145
|
+
export interface InfiniteQueryInput<TPageParam, Query, Params> {
|
|
146
|
+
params?: Params
|
|
147
|
+
query?: Omit<Query, 'cursor'> & { cursor?: TPageParam }
|
|
148
|
+
headers?: Record<string, string>
|
|
149
|
+
fetch?: RequestInit
|
|
150
|
+
cursorKey?: string
|
|
99
151
|
}
|
|
100
152
|
|
|
101
153
|
export interface EdenTQMethod<
|
|
@@ -110,14 +162,25 @@ export interface EdenTQMethod<
|
|
|
110
162
|
options?: RequestInit
|
|
111
163
|
): Promise<Treaty.TreatyResponse<Res>>
|
|
112
164
|
|
|
113
|
-
queryKey(input?: TQMethodParam<Body, Headers, Query, Params
|
|
165
|
+
queryKey(input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>): QueryKey
|
|
114
166
|
|
|
115
167
|
queryOptions<TData = ExtractData<Res>>(
|
|
116
168
|
input: TQMethodParam<Body, Headers, Query, Params>,
|
|
117
169
|
overrides?: Partial<EdenQueryOptions<TData, ExtractError<Res>>>
|
|
118
170
|
): EdenQueryOptions<TData, ExtractError<Res>>
|
|
119
171
|
|
|
120
|
-
|
|
172
|
+
infiniteQueryOptions<TData = ExtractData<Res>, TPageParam = unknown>(
|
|
173
|
+
input: InfiniteQueryInput<TPageParam, Query, Params>,
|
|
174
|
+
opts: {
|
|
175
|
+
initialPageParam: TPageParam
|
|
176
|
+
getNextPageParam: GetNextPageParamFunction<TPageParam, TData>
|
|
177
|
+
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>
|
|
178
|
+
cursorKey?: string
|
|
179
|
+
},
|
|
180
|
+
overrides?: Partial<Omit<EdenInfiniteQueryOptions<TData, ExtractError<Res>, TPageParam>, 'queryKey' | 'queryFn' | 'initialPageParam' | 'getNextPageParam' | 'getPreviousPageParam'>>
|
|
181
|
+
): EdenInfiniteQueryOptions<TData, ExtractError<Res>, TPageParam>
|
|
182
|
+
|
|
183
|
+
mutationKey(input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>): QueryKey
|
|
121
184
|
|
|
122
185
|
mutationOptions<TData = ExtractData<Res>>(
|
|
123
186
|
overrides?: Partial<EdenMutationOptions<TData, ExtractError<Res>, TQMethodParam<Body, Headers, Query, Params>>>
|
|
@@ -125,9 +188,30 @@ export interface EdenTQMethod<
|
|
|
125
188
|
|
|
126
189
|
invalidate(
|
|
127
190
|
queryClient: QueryClient,
|
|
128
|
-
input?: TQMethodParam<Body, Headers, Query, Params
|
|
191
|
+
input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>,
|
|
129
192
|
exact?: boolean
|
|
130
193
|
): Promise<void>
|
|
194
|
+
|
|
195
|
+
prefetch(
|
|
196
|
+
queryClient: QueryClient,
|
|
197
|
+
input: TQMethodParam<Body, Headers, Query, Params>
|
|
198
|
+
): Promise<void>
|
|
199
|
+
|
|
200
|
+
ensureData<TData = ExtractData<Res>>(
|
|
201
|
+
queryClient: QueryClient,
|
|
202
|
+
input: TQMethodParam<Body, Headers, Query, Params>
|
|
203
|
+
): Promise<TData>
|
|
204
|
+
|
|
205
|
+
setData<TData = ExtractData<Res>>(
|
|
206
|
+
queryClient: QueryClient,
|
|
207
|
+
input: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>,
|
|
208
|
+
updater: TData | ((old: TData | undefined) => TData | undefined)
|
|
209
|
+
): TData | undefined
|
|
210
|
+
|
|
211
|
+
getData<TData = ExtractData<Res>>(
|
|
212
|
+
queryClient: QueryClient,
|
|
213
|
+
input: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>
|
|
214
|
+
): TData | undefined
|
|
131
215
|
}
|
|
132
216
|
|
|
133
217
|
export namespace EdenTQ {
|
|
@@ -175,4 +259,107 @@ export namespace EdenTQ {
|
|
|
175
259
|
: never
|
|
176
260
|
}
|
|
177
261
|
|
|
178
|
-
export
|
|
262
|
+
export interface EdenTQUtilsMethod<
|
|
263
|
+
Body,
|
|
264
|
+
Headers,
|
|
265
|
+
Query,
|
|
266
|
+
Params,
|
|
267
|
+
Res extends Record<number, unknown>
|
|
268
|
+
> {
|
|
269
|
+
queryKey(input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>): QueryKey
|
|
270
|
+
|
|
271
|
+
queryOptions<TData = ExtractData<Res>>(
|
|
272
|
+
input: TQMethodParam<Body, Headers, Query, Params>,
|
|
273
|
+
overrides?: Partial<EdenQueryOptions<TData, ExtractError<Res>>>
|
|
274
|
+
): EdenQueryOptions<TData, ExtractError<Res>>
|
|
275
|
+
|
|
276
|
+
infiniteQueryOptions<TData = ExtractData<Res>, TPageParam = unknown>(
|
|
277
|
+
input: InfiniteQueryInput<TPageParam, Query, Params>,
|
|
278
|
+
opts: {
|
|
279
|
+
initialPageParam: TPageParam
|
|
280
|
+
getNextPageParam: GetNextPageParamFunction<TPageParam, TData>
|
|
281
|
+
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TData>
|
|
282
|
+
cursorKey?: string
|
|
283
|
+
},
|
|
284
|
+
overrides?: Partial<Omit<EdenInfiniteQueryOptions<TData, ExtractError<Res>, TPageParam>, 'queryKey' | 'queryFn' | 'initialPageParam' | 'getNextPageParam' | 'getPreviousPageParam'>>
|
|
285
|
+
): EdenInfiniteQueryOptions<TData, ExtractError<Res>, TPageParam>
|
|
286
|
+
|
|
287
|
+
mutationKey(input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>): QueryKey
|
|
288
|
+
|
|
289
|
+
mutationOptions<TData = ExtractData<Res>>(
|
|
290
|
+
overrides?: Partial<EdenMutationOptions<TData, ExtractError<Res>, TQMethodParam<Body, Headers, Query, Params>>>
|
|
291
|
+
): EdenMutationOptions<TData, ExtractError<Res>, TQMethodParam<Body, Headers, Query, Params>>
|
|
292
|
+
|
|
293
|
+
invalidate(
|
|
294
|
+
input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>,
|
|
295
|
+
exact?: boolean
|
|
296
|
+
): Promise<void>
|
|
297
|
+
|
|
298
|
+
prefetch(
|
|
299
|
+
input: TQMethodParam<Body, Headers, Query, Params>
|
|
300
|
+
): Promise<void>
|
|
301
|
+
|
|
302
|
+
ensureData<TData = ExtractData<Res>>(
|
|
303
|
+
input: TQMethodParam<Body, Headers, Query, Params>
|
|
304
|
+
): Promise<TData>
|
|
305
|
+
|
|
306
|
+
setData<TData = ExtractData<Res>>(
|
|
307
|
+
input: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>,
|
|
308
|
+
updater: TData | ((old: TData | undefined) => TData | undefined)
|
|
309
|
+
): TData | undefined
|
|
310
|
+
|
|
311
|
+
getData<TData = ExtractData<Res>>(
|
|
312
|
+
input: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>
|
|
313
|
+
): TData | undefined
|
|
314
|
+
|
|
315
|
+
cancel(
|
|
316
|
+
input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>
|
|
317
|
+
): Promise<void>
|
|
318
|
+
|
|
319
|
+
refetch(
|
|
320
|
+
input?: OmitQueryInput<TQMethodParam<Body, Headers, Query, Params>>
|
|
321
|
+
): Promise<void>
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export namespace EdenTQUtils {
|
|
325
|
+
export type Create<App extends Elysia<any, any, any, any, any, any, any>> =
|
|
326
|
+
App extends {
|
|
327
|
+
'~Routes': infer Schema extends Record<any, any>
|
|
328
|
+
}
|
|
329
|
+
? Prettify<Sign<Schema>> & CreateParams<Schema>
|
|
330
|
+
: 'Please install Elysia before using Eden'
|
|
331
|
+
|
|
332
|
+
export type Sign<in out Route extends Record<any, any>> = {
|
|
333
|
+
[K in keyof Route as K extends `:${string}`
|
|
334
|
+
? never
|
|
335
|
+
: K]: Route[K] extends {
|
|
336
|
+
body: infer Body
|
|
337
|
+
headers: infer Headers
|
|
338
|
+
params: infer Params
|
|
339
|
+
query: infer Query
|
|
340
|
+
response: infer Res extends Record<number, unknown>
|
|
341
|
+
}
|
|
342
|
+
? EdenTQUtilsMethod<Body, Headers, Query, Params, Res>
|
|
343
|
+
: CreateParams<Route[K]>
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
type CreateParams<Route extends Record<string, any>> =
|
|
347
|
+
Extract<keyof Route, `:${string}`> extends infer Path extends string
|
|
348
|
+
? IsNever<Path> extends true
|
|
349
|
+
? Prettify<Sign<Route>>
|
|
350
|
+
: (((params: {
|
|
351
|
+
[param in Path extends `:${infer Param}`
|
|
352
|
+
? Param extends `${infer P}?`
|
|
353
|
+
? P
|
|
354
|
+
: Param
|
|
355
|
+
: never]: string | number
|
|
356
|
+
}) => Prettify<Sign<Route[Path]>> &
|
|
357
|
+
CreateParams<Route[Path]>) &
|
|
358
|
+
Prettify<Sign<Route>>) &
|
|
359
|
+
(Path extends `:${string}?`
|
|
360
|
+
? CreateParams<Route[Path]>
|
|
361
|
+
: {})
|
|
362
|
+
: never
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export type { QueryKey, QueryClient, InfiniteData }
|
package/dist/index.d.ts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { Elysia, ELYSIA_FORM_DATA } from 'elysia';
|
|
2
|
-
import { QueryKey, QueryClient } from '@tanstack/query-core';
|
|
3
|
-
export { QueryClient, QueryKey } from '@tanstack/query-core';
|
|
4
|
-
import { Treaty } from '@elysiajs/eden/treaty2';
|
|
5
|
-
|
|
6
|
-
type IsNever<T> = [T] extends [never] ? true : false;
|
|
7
|
-
type Prettify<T> = {
|
|
8
|
-
[K in keyof T]: T[K];
|
|
9
|
-
} & {};
|
|
10
|
-
type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N ? Acc[number] : Enumerate<N, [...Acc, Acc['length']]>;
|
|
11
|
-
type IntegerRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>;
|
|
12
|
-
type SuccessCodeRange = IntegerRange<200, 300>;
|
|
13
|
-
type ExtractData<Res extends Record<number, unknown>> = Res[Extract<keyof Res, SuccessCodeRange>] extends {
|
|
14
|
-
[ELYSIA_FORM_DATA]: infer Data;
|
|
15
|
-
} ? Data : Res[Extract<keyof Res, SuccessCodeRange>];
|
|
16
|
-
type ExtractError<Res extends Record<number, unknown>> = Exclude<keyof Res, SuccessCodeRange> extends never ? {
|
|
17
|
-
status: unknown;
|
|
18
|
-
value: unknown;
|
|
19
|
-
} : {
|
|
20
|
-
[Status in keyof Res]: {
|
|
21
|
-
status: Status;
|
|
22
|
-
value: Res[Status] extends {
|
|
23
|
-
[ELYSIA_FORM_DATA]: infer Data;
|
|
24
|
-
} ? Data : Res[Status];
|
|
25
|
-
};
|
|
26
|
-
}[Exclude<keyof Res, SuccessCodeRange>];
|
|
27
|
-
interface TQParamBase {
|
|
28
|
-
fetch?: RequestInit;
|
|
29
|
-
}
|
|
30
|
-
type SerializeQueryParams<T> = T extends Record<string, any> ? {
|
|
31
|
-
[K in keyof T]: T[K] extends Date ? string : T[K] extends Date | undefined ? string | undefined : T[K];
|
|
32
|
-
} : T;
|
|
33
|
-
type IsEmptyObject<T> = T extends Record<string, never> ? [keyof T] extends [never] ? true : false : false;
|
|
34
|
-
type MaybeEmptyObject<T, K extends PropertyKey> = [T] extends [never] ? {} : [T] extends [undefined] ? {
|
|
35
|
-
[P in K]?: T;
|
|
36
|
-
} : IsEmptyObject<T> extends true ? {
|
|
37
|
-
[P in K]?: T;
|
|
38
|
-
} : undefined extends T ? {
|
|
39
|
-
[P in K]?: T;
|
|
40
|
-
} : {
|
|
41
|
-
[P in K]: T;
|
|
42
|
-
};
|
|
43
|
-
type TQMethodParam<Body, Headers, Query, Params> = MaybeEmptyObject<Headers, 'headers'> & MaybeEmptyObject<SerializeQueryParams<Query>, 'query'> & MaybeEmptyObject<Params, 'params'> & MaybeEmptyObject<Body, 'body'> & TQParamBase;
|
|
44
|
-
interface EdenQueryOptions<TData = unknown, TError = unknown> {
|
|
45
|
-
queryKey: QueryKey;
|
|
46
|
-
queryFn: () => Promise<TData>;
|
|
47
|
-
}
|
|
48
|
-
interface EdenMutationOptions<TData = unknown, TError = unknown, TVariables = unknown> {
|
|
49
|
-
mutationKey: QueryKey;
|
|
50
|
-
mutationFn: (variables: TVariables) => Promise<TData>;
|
|
51
|
-
}
|
|
52
|
-
interface EdenTQMethod<Body, Headers, Query, Params, Res extends Record<number, unknown>> {
|
|
53
|
-
<TQueryFnData = ExtractData<Res>>(input: TQMethodParam<Body, Headers, Query, Params>, options?: RequestInit): Promise<Treaty.TreatyResponse<Res>>;
|
|
54
|
-
queryKey(input?: TQMethodParam<Body, Headers, Query, Params>): QueryKey;
|
|
55
|
-
queryOptions<TData = ExtractData<Res>>(input: TQMethodParam<Body, Headers, Query, Params>, overrides?: Partial<EdenQueryOptions<TData, ExtractError<Res>>>): EdenQueryOptions<TData, ExtractError<Res>>;
|
|
56
|
-
mutationKey(input?: TQMethodParam<Body, Headers, Query, Params>): QueryKey;
|
|
57
|
-
mutationOptions<TData = ExtractData<Res>>(overrides?: Partial<EdenMutationOptions<TData, ExtractError<Res>, TQMethodParam<Body, Headers, Query, Params>>>): EdenMutationOptions<TData, ExtractError<Res>, TQMethodParam<Body, Headers, Query, Params>>;
|
|
58
|
-
invalidate(queryClient: QueryClient, input?: TQMethodParam<Body, Headers, Query, Params>, exact?: boolean): Promise<void>;
|
|
59
|
-
}
|
|
60
|
-
declare namespace EdenTQ {
|
|
61
|
-
export type Config = Treaty.Config & {
|
|
62
|
-
queryKeyPrefix?: QueryKey;
|
|
63
|
-
};
|
|
64
|
-
export type Create<App extends Elysia<any, any, any, any, any, any, any>> = App extends {
|
|
65
|
-
'~Routes': infer Schema extends Record<any, any>;
|
|
66
|
-
} ? Prettify<Sign<Schema>> & CreateParams<Schema> : 'Please install Elysia before using Eden';
|
|
67
|
-
export type Sign<in out Route extends Record<any, any>> = {
|
|
68
|
-
[K in keyof Route as K extends `:${string}` ? never : K]: Route[K] extends {
|
|
69
|
-
body: infer Body;
|
|
70
|
-
headers: infer Headers;
|
|
71
|
-
params: infer Params;
|
|
72
|
-
query: infer Query;
|
|
73
|
-
response: infer Res extends Record<number, unknown>;
|
|
74
|
-
} ? EdenTQMethod<Body, Headers, Query, Params, Res> : CreateParams<Route[K]>;
|
|
75
|
-
};
|
|
76
|
-
type CreateParams<Route extends Record<string, any>> = Extract<keyof Route, `:${string}`> extends infer Path extends string ? IsNever<Path> extends true ? Prettify<Sign<Route>> : (((params: {
|
|
77
|
-
[param in Path extends `:${infer Param}` ? Param extends `${infer P}?` ? P : Param : never]: string | number;
|
|
78
|
-
}) => Prettify<Sign<Route[Path]>> & CreateParams<Route[Path]>) & Prettify<Sign<Route>>) & (Path extends `:${string}?` ? CreateParams<Route[Path]> : {}) : never;
|
|
79
|
-
export { };
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
declare function createEdenTQ<const App extends Elysia<any, any, any, any, any, any, any>>(domain: string | App, config?: EdenTQ.Config): EdenTQ.Create<App>;
|
|
83
|
-
|
|
84
|
-
export { type EdenMutationOptions, type EdenQueryOptions, EdenTQ, createEdenTQ };
|
package/dist/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var c=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var T=Object.prototype.hasOwnProperty;var q=(e,r)=>{for(var s in r)c(e,s,{get:r[s],enumerable:!0})},k=(e,r,s,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let a of w(r))!T.call(e,a)&&a!==s&&c(e,a,{get:()=>r[a],enumerable:!(n=m(r,a))||n.enumerable});return e};var Q=e=>k(c({},"__esModule",{value:!0}),e);var O={};q(O,{createEdenTQ:()=>P});module.exports=Q(O);var g=require("@elysiajs/eden/treaty2"),h=["get","post","put","delete","patch","options","head"];function b(e){return h.includes(e)}function d(e,r){let s=[];for(let n of e){let a=/^:(.+?)(\?)?$/.exec(n);if(!a){s.push(n);continue}let t=a[1],o=!!a[2],i=r?.[t];if(i==null){if(o)continue;throw new Error(`Missing required route parameter: "${t}"`)}s.push(String(i))}return s}function f(e,r,s,n){return[...e,r,s,n?.params??null,n?.query??null]}function p(e,r,s,n){let a=e;for(let o of r)a=a[o];let t={};return n?.query!==void 0&&(t.query=n.query),n?.headers!==void 0&&(t.headers=n.headers),n?.fetch!==void 0&&Object.assign(t,n.fetch),s==="get"||s==="head"?a[s](Object.keys(t).length>0?t:void 0):a[s](n?.body,Object.keys(t).length>0?t:void 0)}async function l(e){let r=await e;if(r.error)throw r.error;return r.data}function E(e,r,s){let n=[...r],a=t=>{let o=d(n,t?.params);return p(e.raw,o,s,t)};return a.queryKey=t=>f(e.prefix,s,n,t),a.queryOptions=(t,o)=>({queryKey:a.queryKey(t),queryFn:()=>{let i=d(n,t?.params);return l(p(e.raw,i,s,t))},...o}),a.mutationKey=t=>f(e.prefix,s,n,t),a.mutationOptions=t=>({mutationKey:[...e.prefix,s,n],mutationFn:o=>{let i=o,y=d(n,i?.params);return l(p(e.raw,y,s,i))},...t}),a.invalidate=async(t,o,i=!1)=>{let y=o?a.queryKey(o):[...e.prefix,s,n];await t.invalidateQueries({queryKey:y,exact:i})},a}function u(e,r=[]){return new Proxy(()=>{},{get(s,n){return b(n)?E(e,r,n):u(e,n==="index"?r:[...r,n])},apply(s,n,[a]){if(typeof a=="object"&&a!==null){let t=Object.values(a)[0];return u(e,[...r,t])}return u(e,r)}})}function P(e,r={}){let{queryKeyPrefix:s=["eden"],...n}=r,t={raw:(0,g.treaty)(e,n),prefix:s};return u(t)}0&&(module.exports={createEdenTQ});
|