eden-tanstack-query 0.0.9 → 0.1.0-alpha.2
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 +69 -310
- package/dist/index.d.mts +84 -0
- package/dist/index.d.ts +75 -103
- package/dist/index.js +1 -226
- package/dist/index.mjs +1 -0
- package/package.json +59 -40
- package/src/index.ts +217 -0
- package/src/types.ts +178 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,112 +1,84 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { Elysia } from 'elysia';
|
|
1
|
+
import { Elysia, ELYSIA_FORM_DATA } from 'elysia';
|
|
2
|
+
import { QueryKey, QueryClient } from '@tanstack/query-core';
|
|
3
|
+
export { QueryClient, QueryKey } from '@tanstack/query-core';
|
|
5
4
|
import { Treaty } from '@elysiajs/eden/treaty2';
|
|
6
|
-
export { Treaty, treaty } from '@elysiajs/eden/treaty2';
|
|
7
5
|
|
|
8
|
-
type
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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;
|
|
15
29
|
}
|
|
16
|
-
type
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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;
|
|
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>;
|
|
26
47
|
}
|
|
27
|
-
interface
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
onError?: (context: EdenErrorContext) => void;
|
|
48
|
+
interface EdenMutationOptions<TData = unknown, TError = unknown, TVariables = unknown> {
|
|
49
|
+
mutationKey: QueryKey;
|
|
50
|
+
mutationFn: (variables: TVariables) => Promise<TData>;
|
|
31
51
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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;
|
|
44
|
-
interface EdenOptions<TEden = unknown> {
|
|
45
|
-
eden?: TEden;
|
|
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>;
|
|
46
59
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
* Used for the optional `options` parameter in `queryOptions()`.
|
|
51
|
-
*/
|
|
52
|
-
type EdenQueryOptions<TQueryFnData, TError = Error, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TEden = unknown> = Omit<QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>, 'queryKey' | 'queryFn'> & EdenOptions<TEden>;
|
|
53
|
-
/**
|
|
54
|
-
* Full query options returned by `queryOptions()`, compatible with createQuery.
|
|
55
|
-
* This type uses a simpler structure to ensure TypeScript properly infers TData
|
|
56
|
-
* when passed to createQuery.
|
|
57
|
-
*/
|
|
58
|
-
type EdenQueryOptionsResult<TQueryFnData, TError = Error, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = {
|
|
59
|
-
queryKey: TQueryKey;
|
|
60
|
-
queryFn: (context: QueryFunctionContext<TQueryKey>) => Promise<TQueryFnData>;
|
|
61
|
-
} & Omit<QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>, 'queryKey' | 'queryFn'>;
|
|
62
|
-
type EdenMutationOptions<TData, TError = Error, TVariables = void, TContext = unknown, TEden = unknown> = Omit<MutationOptions<TData, TError, TVariables, TContext>, 'mutationFn'> & EdenOptions<TEden>;
|
|
63
|
-
type EdenInfiniteQueryOptions<TQueryFnData, TError = Error, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown, TEden = unknown> = Omit<InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, 'queryKey' | 'queryFn'> & EdenOptions<TEden>;
|
|
64
|
-
type EdenPartial<T> = T extends object ? Partial<T> : never;
|
|
65
|
-
type AwaitedReturn<T> = T extends (...args: any[]) => infer R ? Awaited<R> : never;
|
|
66
|
-
type FirstArg<T> = T extends (arg1: infer A, ...args: any[]) => any ? A : never;
|
|
67
|
-
type SecondArg<T> = T extends (arg1: any, arg2: infer B, ...args: any[]) => any ? B : never;
|
|
68
|
-
type EdenData<T> = Extract<AwaitedReturn<T>, {
|
|
69
|
-
error: null;
|
|
70
|
-
}> extends {
|
|
71
|
-
data: infer D;
|
|
72
|
-
} ? D : never;
|
|
73
|
-
type EdenQueryMethod<TMethod extends Extract<HTTPMethod, 'get' | 'head'>, TCall extends (...args: any[]) => Promise<any>, TThrowOnError extends ThrowOnErrorOption = true> = TCall & {
|
|
74
|
-
queryKey: (input?: FirstArg<TCall>) => EdenQueryKey<FirstArg<TCall>, TMethod>;
|
|
75
|
-
queryFilter: (input?: FirstArg<TCall>) => QueryFilters<EdenQueryKey<FirstArg<TCall>, TMethod>>;
|
|
76
|
-
queryOptions: (input?: FirstArg<TCall>, options?: EdenQueryOptions<NarrowedData<EdenData<TCall>, TThrowOnError>, EdenFetchError | Error, NarrowedData<EdenData<TCall>, TThrowOnError>, EdenQueryKey<FirstArg<TCall>, TMethod>, EdenPartial<NonNullable<FirstArg<TCall>>>>) => EdenQueryOptionsResult<NarrowedData<EdenData<TCall>, TThrowOnError>, EdenFetchError | Error, NarrowedData<EdenData<TCall>, TThrowOnError>, EdenQueryKey<FirstArg<TCall>, TMethod>>;
|
|
77
|
-
};
|
|
78
|
-
type EdenMutationMethod<TMethod extends Extract<HTTPMethod, 'post' | 'put' | 'patch' | 'delete'>, TCall extends (...args: any[]) => Promise<any>, TThrowOnError extends ThrowOnErrorOption = true> = TCall & {
|
|
79
|
-
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>>>> & {
|
|
80
|
-
mutationKey: EdenQueryKey<undefined, TMethod>;
|
|
81
|
-
mutationFn: (variables: FirstArg<TCall>) => Promise<NarrowedData<EdenData<TCall>, TThrowOnError>>;
|
|
60
|
+
declare namespace EdenTQ {
|
|
61
|
+
export type Config = Treaty.Config & {
|
|
62
|
+
queryKeyPrefix?: QueryKey;
|
|
82
63
|
};
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
+
}
|
|
88
81
|
|
|
89
|
-
|
|
90
|
-
* Creates an Eden Query client with TanStack Query integration.
|
|
91
|
-
*
|
|
92
|
-
* @typeParam App - Your Elysia application type
|
|
93
|
-
* @typeParam TThrowOnError - Whether to throw on errors (inferred from options, defaults to true)
|
|
94
|
-
*
|
|
95
|
-
* When `throwOnError` is `true` (default), error-shaped objects are excluded from
|
|
96
|
-
* the data type in callbacks like `select` and `onSuccess`, since errors are thrown
|
|
97
|
-
* before reaching those callbacks.
|
|
98
|
-
*
|
|
99
|
-
* @example
|
|
100
|
-
* ```typescript
|
|
101
|
-
* // Default: throwOnError = true, data types exclude error shapes
|
|
102
|
-
* const eden = createEdenQuery<App>('http://localhost:8080')
|
|
103
|
-
*
|
|
104
|
-
* // Explicit: throwOnError = false, data types include full union
|
|
105
|
-
* const eden = createEdenQuery<App>('http://localhost:8080', { throwOnError: false })
|
|
106
|
-
* ```
|
|
107
|
-
*/
|
|
108
|
-
declare function createEdenQuery<App extends Elysia<any, any, any, any, any, any, any>, const TThrowOnError extends ThrowOnErrorOption = true>(domain: string, options?: EdenQueryConfig<TThrowOnError> & {
|
|
109
|
-
treaty?: Treaty.Config;
|
|
110
|
-
}): EdenQueryify<Treaty.Create<App>, TThrowOnError>;
|
|
82
|
+
declare function createEdenTQ<const App extends Elysia<any, any, any, any, any, any, any>>(domain: string | App, config?: EdenTQ.Config): EdenTQ.Create<App>;
|
|
111
83
|
|
|
112
|
-
export { type
|
|
84
|
+
export { type EdenMutationOptions, type EdenQueryOptions, EdenTQ, createEdenTQ };
|
package/dist/index.js
CHANGED
|
@@ -1,226 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { treaty } from "@elysiajs/eden/treaty2";
|
|
3
|
-
|
|
4
|
-
// src/query-options.ts
|
|
5
|
-
import { EdenFetchError } from "@elysiajs/eden";
|
|
6
|
-
|
|
7
|
-
// src/query-key.ts
|
|
8
|
-
function buildQueryKey(paths, input, method, prefix) {
|
|
9
|
-
if (prefix) {
|
|
10
|
-
return typeof prefix === "string" ? [prefix, ...paths, input, method] : [...prefix, ...paths, input, method];
|
|
11
|
-
}
|
|
12
|
-
return [...paths, input, method];
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// src/query-options.ts
|
|
16
|
-
function shouldThrowOnError(config, context) {
|
|
17
|
-
if (typeof config.throwOnError === "boolean") return config.throwOnError;
|
|
18
|
-
if (typeof config.throwOnError === "function") {
|
|
19
|
-
const fn = config.throwOnError;
|
|
20
|
-
return fn.length >= 2 ? fn(
|
|
21
|
-
context.queryKey,
|
|
22
|
-
context.status
|
|
23
|
-
) : fn(context);
|
|
24
|
-
}
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
function toEdenFetchError(error, status) {
|
|
28
|
-
if (error instanceof EdenFetchError) return error;
|
|
29
|
-
if (error && typeof error === "object" && "value" in error) {
|
|
30
|
-
return new EdenFetchError(status, error.value);
|
|
31
|
-
}
|
|
32
|
-
return new EdenFetchError(status, error);
|
|
33
|
-
}
|
|
34
|
-
function createQueryOptions(treatyCall, paths, method, globalConfig) {
|
|
35
|
-
return (input, options) => {
|
|
36
|
-
const eden = options?.eden;
|
|
37
|
-
const finalInput = input === void 0 && eden === void 0 ? void 0 : { ...input, ...eden };
|
|
38
|
-
const queryKey = buildQueryKey(
|
|
39
|
-
paths,
|
|
40
|
-
finalInput,
|
|
41
|
-
method,
|
|
42
|
-
globalConfig.queryKeyPrefix
|
|
43
|
-
);
|
|
44
|
-
const { eden: _eden, ...tanstackOptions } = options ?? {};
|
|
45
|
-
return {
|
|
46
|
-
...tanstackOptions,
|
|
47
|
-
queryKey,
|
|
48
|
-
queryFn: async (_context) => {
|
|
49
|
-
const result = await treatyCall(finalInput);
|
|
50
|
-
if (result.error) {
|
|
51
|
-
const error = toEdenFetchError(result.error, result.status);
|
|
52
|
-
error.queryKey = queryKey;
|
|
53
|
-
error.method = method;
|
|
54
|
-
error.path = paths;
|
|
55
|
-
error.input = finalInput;
|
|
56
|
-
error.response = result.response;
|
|
57
|
-
error.headers = result.headers;
|
|
58
|
-
globalConfig.onError?.({
|
|
59
|
-
error,
|
|
60
|
-
queryKey,
|
|
61
|
-
method,
|
|
62
|
-
path: paths,
|
|
63
|
-
input: finalInput,
|
|
64
|
-
type: "query"
|
|
65
|
-
});
|
|
66
|
-
if (shouldThrowOnError(globalConfig, {
|
|
67
|
-
queryKey,
|
|
68
|
-
status: result.status,
|
|
69
|
-
method,
|
|
70
|
-
path: paths,
|
|
71
|
-
input: finalInput
|
|
72
|
-
})) {
|
|
73
|
-
throw error;
|
|
74
|
-
}
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
return result.data;
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// src/mutation-options.ts
|
|
84
|
-
import { EdenFetchError as EdenFetchError2 } from "@elysiajs/eden";
|
|
85
|
-
function shouldThrowOnError2(config, context) {
|
|
86
|
-
if (typeof config.throwOnError === "boolean") return config.throwOnError;
|
|
87
|
-
if (typeof config.throwOnError === "function") {
|
|
88
|
-
const fn = config.throwOnError;
|
|
89
|
-
return fn.length >= 2 ? fn(
|
|
90
|
-
context.queryKey,
|
|
91
|
-
context.status
|
|
92
|
-
) : fn(context);
|
|
93
|
-
}
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
function toEdenFetchError2(error, status) {
|
|
97
|
-
if (error instanceof EdenFetchError2) return error;
|
|
98
|
-
if (error && typeof error === "object" && "value" in error) {
|
|
99
|
-
return new EdenFetchError2(status, error.value);
|
|
100
|
-
}
|
|
101
|
-
return new EdenFetchError2(status, error);
|
|
102
|
-
}
|
|
103
|
-
function createMutationOptions(treatyCall, paths, method, globalConfig) {
|
|
104
|
-
return (options) => {
|
|
105
|
-
const eden = options?.eden;
|
|
106
|
-
const mutationKey = buildQueryKey(
|
|
107
|
-
paths,
|
|
108
|
-
void 0,
|
|
109
|
-
method,
|
|
110
|
-
globalConfig.queryKeyPrefix
|
|
111
|
-
);
|
|
112
|
-
const { eden: _eden, ...tanstackOptions } = options ?? {};
|
|
113
|
-
return {
|
|
114
|
-
...tanstackOptions,
|
|
115
|
-
mutationKey,
|
|
116
|
-
mutationFn: async (variables) => {
|
|
117
|
-
const result = await treatyCall(variables, eden);
|
|
118
|
-
if (result.error) {
|
|
119
|
-
const error = toEdenFetchError2(result.error, result.status);
|
|
120
|
-
error.queryKey = mutationKey;
|
|
121
|
-
error.method = method;
|
|
122
|
-
error.path = paths;
|
|
123
|
-
error.input = variables;
|
|
124
|
-
error.response = result.response;
|
|
125
|
-
error.headers = result.headers;
|
|
126
|
-
globalConfig.onError?.({
|
|
127
|
-
error,
|
|
128
|
-
queryKey: mutationKey,
|
|
129
|
-
method,
|
|
130
|
-
path: paths,
|
|
131
|
-
input: variables,
|
|
132
|
-
type: "mutation"
|
|
133
|
-
});
|
|
134
|
-
if (shouldThrowOnError2(globalConfig, {
|
|
135
|
-
queryKey: mutationKey,
|
|
136
|
-
status: result.status,
|
|
137
|
-
method,
|
|
138
|
-
path: paths,
|
|
139
|
-
input: variables
|
|
140
|
-
})) {
|
|
141
|
-
throw error;
|
|
142
|
-
}
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
|
-
return result.data;
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// src/query-key-helpers.ts
|
|
152
|
-
function createQueryKeyGetter(paths, method, prefix) {
|
|
153
|
-
return (input) => buildQueryKey(paths, input, method, prefix);
|
|
154
|
-
}
|
|
155
|
-
function createQueryFilter(paths, method, prefix) {
|
|
156
|
-
return (input) => ({
|
|
157
|
-
queryKey: buildQueryKey(paths, input, method, prefix),
|
|
158
|
-
exact: true
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// src/proxy.ts
|
|
163
|
-
var HTTP_METHODS = ["get", "post", "put", "patch", "delete", "head", "options"];
|
|
164
|
-
function extendProxy(proxy, paths, config) {
|
|
165
|
-
const handler = {
|
|
166
|
-
get(target, prop) {
|
|
167
|
-
if (typeof prop === "symbol") return target[prop];
|
|
168
|
-
const value = target[prop];
|
|
169
|
-
if (typeof value === "function" && HTTP_METHODS.includes(prop)) {
|
|
170
|
-
const httpMethod = prop;
|
|
171
|
-
const callable = (...args) => value(...args);
|
|
172
|
-
return Object.assign(callable, {
|
|
173
|
-
...httpMethod === "get" || httpMethod === "head" ? {
|
|
174
|
-
queryOptions: createQueryOptions(
|
|
175
|
-
callable,
|
|
176
|
-
paths,
|
|
177
|
-
httpMethod,
|
|
178
|
-
config
|
|
179
|
-
),
|
|
180
|
-
queryKey: createQueryKeyGetter(paths, httpMethod, config.queryKeyPrefix),
|
|
181
|
-
queryFilter: createQueryFilter(paths, httpMethod, config.queryKeyPrefix)
|
|
182
|
-
} : {},
|
|
183
|
-
...httpMethod === "post" || httpMethod === "put" || httpMethod === "patch" || httpMethod === "delete" ? {
|
|
184
|
-
mutationOptions: createMutationOptions(
|
|
185
|
-
callable,
|
|
186
|
-
paths,
|
|
187
|
-
httpMethod,
|
|
188
|
-
config
|
|
189
|
-
)
|
|
190
|
-
} : {}
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
if (value !== null && (typeof value === "object" || typeof value === "function")) {
|
|
194
|
-
return extendProxy(value, [...paths, prop], config);
|
|
195
|
-
}
|
|
196
|
-
return value;
|
|
197
|
-
},
|
|
198
|
-
apply(target, thisArg, args) {
|
|
199
|
-
const result = Reflect.apply(target, thisArg, args);
|
|
200
|
-
if (result !== null && (typeof result === "object" || typeof result === "function")) {
|
|
201
|
-
return extendProxy(result, paths, config);
|
|
202
|
-
}
|
|
203
|
-
return result;
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
return new Proxy(proxy, handler);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// src/index.ts
|
|
210
|
-
import { treaty as treaty2 } from "@elysiajs/eden/treaty2";
|
|
211
|
-
function createEdenQuery(domain, options) {
|
|
212
|
-
const treatyInstance = treaty(domain, options?.treaty);
|
|
213
|
-
return extendProxy(
|
|
214
|
-
treatyInstance,
|
|
215
|
-
[],
|
|
216
|
-
{
|
|
217
|
-
throwOnError: options?.throwOnError ?? true,
|
|
218
|
-
queryKeyPrefix: options?.queryKeyPrefix,
|
|
219
|
-
onError: options?.onError
|
|
220
|
-
}
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
export {
|
|
224
|
-
createEdenQuery,
|
|
225
|
-
treaty2 as treaty
|
|
226
|
-
};
|
|
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});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{treaty as l}from"@elysiajs/eden/treaty2";var g=["get","post","put","delete","patch","options","head"];function m(r){return g.includes(r)}function c(r,s){let t=[];for(let e of r){let a=/^:(.+?)(\?)?$/.exec(e);if(!a){t.push(e);continue}let n=a[1],o=!!a[2],i=s?.[n];if(i==null){if(o)continue;throw new Error(`Missing required route parameter: "${n}"`)}t.push(String(i))}return t}function p(r,s,t,e){return[...r,s,t,e?.params??null,e?.query??null]}function d(r,s,t,e){let a=r;for(let o of s)a=a[o];let n={};return e?.query!==void 0&&(n.query=e.query),e?.headers!==void 0&&(n.headers=e.headers),e?.fetch!==void 0&&Object.assign(n,e.fetch),t==="get"||t==="head"?a[t](Object.keys(n).length>0?n:void 0):a[t](e?.body,Object.keys(n).length>0?n:void 0)}async function f(r){let s=await r;if(s.error)throw s.error;return s.data}function w(r,s,t){let e=[...s],a=n=>{let o=c(e,n?.params);return d(r.raw,o,t,n)};return a.queryKey=n=>p(r.prefix,t,e,n),a.queryOptions=(n,o)=>({queryKey:a.queryKey(n),queryFn:()=>{let i=c(e,n?.params);return f(d(r.raw,i,t,n))},...o}),a.mutationKey=n=>p(r.prefix,t,e,n),a.mutationOptions=n=>({mutationKey:[...r.prefix,t,e],mutationFn:o=>{let i=o,y=c(e,i?.params);return f(d(r.raw,y,t,i))},...n}),a.invalidate=async(n,o,i=!1)=>{let y=o?a.queryKey(o):[...r.prefix,t,e];await n.invalidateQueries({queryKey:y,exact:i})},a}function u(r,s=[]){return new Proxy(()=>{},{get(t,e){return m(e)?w(r,s,e):u(r,e==="index"?s:[...s,e])},apply(t,e,[a]){if(typeof a=="object"&&a!==null){let n=Object.values(a)[0];return u(r,[...s,n])}return u(r,s)}})}function q(r,s={}){let{queryKeyPrefix:t=["eden"],...e}=s,n={raw:l(r,e),prefix:t};return u(n)}export{q as createEdenTQ};
|
package/package.json
CHANGED
|
@@ -1,42 +1,61 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
2
|
+
"name": "eden-tanstack-query",
|
|
3
|
+
"version": "0.1.0-alpha.2",
|
|
4
|
+
"description": "TanStack Query integration for Elysia Eden - type-safe queries and mutations",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Frank-III",
|
|
7
|
+
"url": "https://github.com/Frank-III"
|
|
8
|
+
},
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"module": "./dist/index.mjs",
|
|
11
|
+
"exports": {
|
|
12
|
+
"./package.json": "./package.json",
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"require": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"src"
|
|
23
|
+
],
|
|
24
|
+
"keywords": [
|
|
25
|
+
"elysia",
|
|
26
|
+
"eden",
|
|
27
|
+
"tanstack-query",
|
|
28
|
+
"react-query",
|
|
29
|
+
"svelte-query",
|
|
30
|
+
"vue-query",
|
|
31
|
+
"solid-query",
|
|
32
|
+
"type-safe",
|
|
33
|
+
"rpc"
|
|
34
|
+
],
|
|
35
|
+
"homepage": "https://github.com/Frank-III/eden",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://github.com/Frank-III/eden"
|
|
39
|
+
},
|
|
40
|
+
"bugs": "https://github.com/Frank-III/eden/issues",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsup",
|
|
44
|
+
"test": "bun test",
|
|
45
|
+
"prepublishOnly": "bun run build"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@elysiajs/eden": ">=1.0.0",
|
|
49
|
+
"@tanstack/query-core": ">=5.0.0",
|
|
50
|
+
"elysia": ">=1.0.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@elysiajs/eden": "^1.4.6",
|
|
54
|
+
"@tanstack/query-core": "^5.90.20",
|
|
55
|
+
"@types/bun": "^1.3.8",
|
|
56
|
+
"elysia": "^1.4.22",
|
|
57
|
+
"expect-type": "^1.3.0",
|
|
58
|
+
"tsup": "^8.5.1",
|
|
59
|
+
"typescript": "^5.9.3"
|
|
60
|
+
}
|
|
42
61
|
}
|