eden2query 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -27
- package/dist/index.d.ts +15 -1270
- package/dist/index.js +30 -23
- package/package.json +11 -5
- package/dist/index.js.map +0 -1
package/README.md
CHANGED
|
@@ -12,26 +12,24 @@ bun add eden2query @elysiajs/eden @tanstack/react-query
|
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
14
|
import { treaty } from "@elysiajs/eden";
|
|
15
|
-
import {
|
|
15
|
+
import { edenQueryOptions, edenMutationOptions } from "eden2query";
|
|
16
16
|
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 =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
// GET → queryOptions (first arg is a thunk that calls the Eden GET function)
|
|
21
|
+
const resourceQuery = edenQueryOptions(
|
|
22
|
+
() => client.api.resource.get({ query: { q: "hello" } }),
|
|
23
|
+
{ queryKey: ["resource"] },
|
|
24
|
+
);
|
|
25
25
|
|
|
26
26
|
// POST / PUT / DELETE → mutationOptions
|
|
27
|
-
const createResource =
|
|
28
|
-
fn: client.api.resource.post,
|
|
29
|
-
});
|
|
27
|
+
const createResource = edenMutationOptions(client.api.resource.post);
|
|
30
28
|
|
|
31
29
|
// Parameterised routes — bind params first
|
|
32
|
-
const updateResource =
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
const updateResource = edenMutationOptions(
|
|
31
|
+
client.api.resource({ id: "some-id" }).put,
|
|
32
|
+
);
|
|
35
33
|
```
|
|
36
34
|
|
|
37
35
|
Then use them with React Query as usual:
|
|
@@ -40,7 +38,7 @@ 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
44
|
Works with `prefetchQuery`, `ensureQueryData`, `useSuspenseQuery`, etc:
|
|
@@ -50,28 +48,31 @@ const queryClient = new QueryClient();
|
|
|
50
48
|
await queryClient.prefetchQuery(resourceQuery);
|
|
51
49
|
```
|
|
52
50
|
|
|
53
|
-
You can also pass any standard React Query options
|
|
51
|
+
You can also pass any standard React Query options as the second argument:
|
|
54
52
|
|
|
55
53
|
```ts
|
|
56
|
-
const resourceQuery =
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
});
|
|
54
|
+
const resourceQuery = edenQueryOptions(
|
|
55
|
+
() => client.api.resource.get({ query: { q: "hello" } }),
|
|
56
|
+
{ queryKey: ["resource"], refetchInterval: 1000 },
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const createResource = edenMutationOptions(
|
|
60
|
+
client.api.resource.post,
|
|
61
|
+
{ onSuccess: () => console.log("created!") },
|
|
62
|
+
);
|
|
66
63
|
```
|
|
67
64
|
|
|
68
65
|
## API
|
|
69
66
|
|
|
70
|
-
**`
|
|
67
|
+
**`edenQueryOptions(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`.
|
|
71
68
|
|
|
72
|
-
**`
|
|
69
|
+
**`edenMutationOptions(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.
|
|
73
70
|
|
|
74
|
-
|
|
71
|
+
**`InferQueryOptions<TFn>`** — extracts the full `UseQueryOptions` type for a given Eden GET thunk.
|
|
72
|
+
|
|
73
|
+
**`InferMutationOptions<TFn>`** — extracts the full `UseMutationOptions` type for a given Eden mutation function.
|
|
74
|
+
|
|
75
|
+
All helpers infer data, error, and input types end-to-end from your Elysia route definitions. No manual generics needed.
|
|
75
76
|
|
|
76
77
|
## License
|
|
77
78
|
|