eden2query 0.1.0 → 0.1.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 +28 -26
- package/dist/index.d.ts +14 -20
- package/dist/index.js +14 -14
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -17,21 +17,19 @@ import type { App } from "./server"; // your Elysia app type
|
|
|
17
17
|
|
|
18
18
|
const client = treaty<App>("localhost:3000");
|
|
19
19
|
|
|
20
|
-
// GET → queryOptions
|
|
21
|
-
const resourceQuery = treatyQueryOptions(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
// GET → queryOptions (first arg is a thunk that calls the Eden GET function)
|
|
21
|
+
const resourceQuery = treatyQueryOptions(
|
|
22
|
+
() => client.api.resource.get({ query: { q: "hello" } }),
|
|
23
|
+
{ queryKey: ["resource"] },
|
|
24
|
+
);
|
|
25
25
|
|
|
26
26
|
// POST / PUT / DELETE → mutationOptions
|
|
27
|
-
const createResource = treatyMutationOptions(
|
|
28
|
-
fn: client.api.resource.post,
|
|
29
|
-
});
|
|
27
|
+
const createResource = treatyMutationOptions(client.api.resource.post);
|
|
30
28
|
|
|
31
29
|
// Parameterised routes — bind params first
|
|
32
|
-
const updateResource = treatyMutationOptions(
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
const updateResource = treatyMutationOptions(
|
|
31
|
+
client.api.resource({ id: "some-id" }).put,
|
|
32
|
+
);
|
|
35
33
|
```
|
|
36
34
|
|
|
37
35
|
Then use them with React Query as usual:
|
|
@@ -40,31 +38,35 @@ Then use them with React Query as usual:
|
|
|
40
38
|
const { data } = useQuery(resourceQuery);
|
|
41
39
|
const mutation = useMutation(createResource);
|
|
42
40
|
|
|
43
|
-
mutation.mutate({ name: "New item" }); // fully typed input
|
|
41
|
+
mutation.mutate({ body: { name: "New item" }, query: { q: "hello" } }); // fully typed input
|
|
44
42
|
```
|
|
45
43
|
|
|
46
|
-
Works with `prefetchQuery`, `ensureQueryData`, `useSuspenseQuery`, etc
|
|
44
|
+
Works with `prefetchQuery`, `ensureQueryData`, `useSuspenseQuery`, etc:
|
|
47
45
|
|
|
48
|
-
|
|
46
|
+
```ts
|
|
47
|
+
const queryClient = new QueryClient();
|
|
48
|
+
await queryClient.prefetchQuery(resourceQuery);
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
You can also pass any standard React Query options as the second argument:
|
|
49
52
|
|
|
50
53
|
```ts
|
|
51
|
-
const resourceQuery = treatyQueryOptions(
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
});
|
|
54
|
+
const resourceQuery = treatyQueryOptions(
|
|
55
|
+
() => client.api.resource.get({ query: { q: "hello" } }),
|
|
56
|
+
{ queryKey: ["resource"], refetchInterval: 1000 },
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const createResource = treatyMutationOptions(
|
|
60
|
+
client.api.resource.post,
|
|
61
|
+
{ onSuccess: () => console.log("created!") },
|
|
62
|
+
);
|
|
61
63
|
```
|
|
62
64
|
|
|
63
65
|
## API
|
|
64
66
|
|
|
65
|
-
**`treatyQueryOptions(
|
|
67
|
+
**`treatyQueryOptions(fn, queryOptions)`** — wraps an Eden GET call into `queryOptions`. `fn` is a **thunk** (zero-argument function) that calls the Eden GET function, e.g. `() => client.api.resource.get({ query: { ... } })`. This lets you bind query parameters, headers, or any other Eden options at definition time. The second argument accepts all `queryOptions` fields except `queryFn`. Extracts `data` from the response and throws on `error`.
|
|
66
68
|
|
|
67
|
-
**`treatyMutationOptions(
|
|
69
|
+
**`treatyMutationOptions(fn, mutationOptions?)`** — wraps an Eden mutation function into `mutationOptions`. The optional second argument accepts all `mutationOptions` fields except `mutationFn` (e.g. `onSuccess`, `onSettled`, `onMutate`). The `mutate` call receives a single object with `body` and any other Eden options (like `query`) as the input.
|
|
68
70
|
|
|
69
71
|
Both infer data, error, and input types end-to-end from your Elysia route definitions. No manual generics needed.
|
|
70
72
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,22 +1,16 @@
|
|
|
1
1
|
import { Treaty } from "@elysiajs/eden";
|
|
2
|
-
import {
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
declare function
|
|
15
|
-
|
|
16
|
-
P extends Parameters<T>,
|
|
17
|
-
R extends Awaited<ReturnType<T>>
|
|
18
|
-
>(params: {
|
|
19
|
-
queryKey: string[];
|
|
20
|
-
fn: T;
|
|
21
|
-
} & Omit<Parameters<typeof queryOptions<R["data"], R["error"], P[0]>>[0], "queryFn">);
|
|
2
|
+
import { UseMutationOptions, UseQueryOptions } from "@tanstack/react-query";
|
|
3
|
+
type EdenMutationFn<
|
|
4
|
+
TBody = any,
|
|
5
|
+
TOptions = any,
|
|
6
|
+
TResponse extends Record<number, unknown> = Record<number, unknown>
|
|
7
|
+
> = ((body: TBody, options: TOptions) => Promise<Treaty.TreatyResponse<TResponse>>);
|
|
8
|
+
type EdenQueryFn<TResponse extends Record<number, unknown> = Record<number, unknown>> = (() => Promise<Treaty.TreatyResponse<TResponse>>);
|
|
9
|
+
type InferMutationVariables<TFn extends EdenMutationFn> = (unknown extends Parameters<TFn>[0] ? Parameters<TFn>[1] : Parameters<TFn>[1] & {
|
|
10
|
+
body: Parameters<TFn>[0];
|
|
11
|
+
});
|
|
12
|
+
type InferMutationOptions<TFn extends EdenMutationFn> = (UseMutationOptions<Awaited<ReturnType<TFn>>["data"], Awaited<ReturnType<TFn>>["error"], InferMutationVariables<TFn>>);
|
|
13
|
+
type InferQueryOptions<TFn extends EdenQueryFn> = (UseQueryOptions<Awaited<ReturnType<TFn>>["data"], Awaited<ReturnType<TFn>>["error"]>);
|
|
14
|
+
declare function treatyMutationOptions<TFn extends EdenMutationFn>(fn: TFn, options?: Omit<InferMutationOptions<TFn>, "mutationFn">): InferMutationOptions<TFn>;
|
|
15
|
+
declare function treatyQueryOptions<TFn extends EdenQueryFn>(fn: TFn, options: Omit<InferQueryOptions<TFn>, "queryFn">): InferQueryOptions<TFn>;
|
|
22
16
|
export { treatyQueryOptions, treatyMutationOptions };
|
package/dist/index.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
// src/lib.ts
|
|
2
2
|
import { mutationOptions, queryOptions } from "@tanstack/react-query";
|
|
3
|
-
function treatyMutationOptions(
|
|
3
|
+
function treatyMutationOptions(fn, options) {
|
|
4
4
|
return mutationOptions({
|
|
5
5
|
mutationFn: async (variables) => {
|
|
6
|
-
const
|
|
6
|
+
const { body, ...rest } = variables;
|
|
7
|
+
const response = await fn(body, rest);
|
|
7
8
|
const { data, error } = response;
|
|
8
|
-
if (error)
|
|
9
|
+
if (error)
|
|
9
10
|
throw error;
|
|
10
|
-
}
|
|
11
11
|
return data;
|
|
12
12
|
},
|
|
13
|
-
...
|
|
13
|
+
...options
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
-
function treatyQueryOptions(
|
|
16
|
+
function treatyQueryOptions(fn, options) {
|
|
17
17
|
return queryOptions({
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const { data, error } =
|
|
21
|
-
if (error)
|
|
18
|
+
queryFn: async () => {
|
|
19
|
+
const response = await fn();
|
|
20
|
+
const { data, error } = response;
|
|
21
|
+
if (error)
|
|
22
22
|
throw error;
|
|
23
|
-
}
|
|
24
23
|
return data;
|
|
25
|
-
}
|
|
24
|
+
},
|
|
25
|
+
...options
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
28
|
export {
|
|
@@ -30,5 +30,5 @@ export {
|
|
|
30
30
|
treatyMutationOptions
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
-
//# debugId=
|
|
34
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
33
|
+
//# debugId=42FF79B46773B5DF64756E2164756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UNwQyxRQUFRLE1BQU0sVUFBVTtBQUFBLE1BQ3hCLElBQUk7QUFBQSxRQUFPLE1BQU07QUFBQSxNQUNqQixPQUFPO0FBQUE7QUFBQSxPQUVOO0FBQUEsRUFDTCxDQUFDO0FBQUE7QUFHSSxTQUFTLGtCQUVmLENBQ0MsSUFDQSxTQUN3QjtBQUFBLEVBQ3hCLE9BQU8sYUFBYTtBQUFBLElBQ2xCLFNBQVMsWUFBWTtBQUFBLE1BQ25CLE1BQU0sV0FBVyxNQUFNLEdBQUc7QUFBQSxNQUMxQixRQUFRLE1BQU0sVUFBVTtBQUFBLE1BQ3hCLElBQUk7QUFBQSxRQUFPLE1BQU07QUFBQSxNQUNqQixPQUFPO0FBQUE7QUFBQSxPQUVOO0FBQUEsRUFDTCxDQUFDO0FBQUE7IiwKICAiZGVidWdJZCI6ICI0MkZGNzlCNDY3NzNCNURGNjQ3NTZFMjE2NDc1NkUyMSIsCiAgIm5hbWVzIjogW10KfQ==
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eden2query",
|
|
3
3
|
"description": "Type-safe Eden Treaty to React Query helpers",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
@@ -18,6 +18,14 @@
|
|
|
18
18
|
"./package.json": "./package.json"
|
|
19
19
|
},
|
|
20
20
|
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/eyueldk/eden2query.git"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/eyueldk/eden2query#readme",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/eyueldk/eden2query/issues"
|
|
28
|
+
},
|
|
21
29
|
"keywords": [
|
|
22
30
|
"elysia",
|
|
23
31
|
"eden",
|
|
@@ -35,8 +43,6 @@
|
|
|
35
43
|
"typescript": "^5"
|
|
36
44
|
},
|
|
37
45
|
"devDependencies": {
|
|
38
|
-
"@elysiajs/eden": "^1.4.6",
|
|
39
|
-
"@tanstack/react-query": "^5.90.20",
|
|
40
46
|
"@types/bun": "latest",
|
|
41
47
|
"bunup": "^0.16.22",
|
|
42
48
|
"elysia": "^1.4.22"
|