eden2query 0.2.0 → 0.3.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 +13 -14
- package/dist/index.d.ts +14 -14
- package/dist/index.js +15 -13
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -12,22 +12,22 @@ bun add eden2query @elysiajs/eden @tanstack/react-query
|
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
14
|
import { treaty } from "@elysiajs/eden";
|
|
15
|
-
import {
|
|
15
|
+
import { treatyQueryOptions, treatyMutationOptions } 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 (first arg is a thunk that calls the
|
|
21
|
-
const resourceQuery =
|
|
20
|
+
// GET → queryOptions (first arg is a thunk that calls the Treaty GET function)
|
|
21
|
+
const resourceQuery = treatyQueryOptions(
|
|
22
22
|
() => client.api.resource.get({ query: { q: "hello" } }),
|
|
23
23
|
{ queryKey: ["resource"] },
|
|
24
24
|
);
|
|
25
25
|
|
|
26
26
|
// POST / PUT / DELETE → mutationOptions
|
|
27
|
-
const createResource =
|
|
27
|
+
const createResource = treatyMutationOptions(client.api.resource.post);
|
|
28
28
|
|
|
29
29
|
// Parameterised routes — bind params first
|
|
30
|
-
const updateResource =
|
|
30
|
+
const updateResource = treatyMutationOptions(
|
|
31
31
|
client.api.resource({ id: "some-id" }).put,
|
|
32
32
|
);
|
|
33
33
|
```
|
|
@@ -51,26 +51,25 @@ await queryClient.prefetchQuery(resourceQuery);
|
|
|
51
51
|
You can also pass any standard React Query options as the second argument:
|
|
52
52
|
|
|
53
53
|
```ts
|
|
54
|
-
const resourceQuery =
|
|
54
|
+
const resourceQuery = treatyQueryOptions(
|
|
55
55
|
() => client.api.resource.get({ query: { q: "hello" } }),
|
|
56
56
|
{ queryKey: ["resource"], refetchInterval: 1000 },
|
|
57
57
|
);
|
|
58
58
|
|
|
59
|
-
const createResource =
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
);
|
|
59
|
+
const createResource = treatyMutationOptions(client.api.resource.post, {
|
|
60
|
+
onSuccess: () => console.log("created!"),
|
|
61
|
+
});
|
|
63
62
|
```
|
|
64
63
|
|
|
65
64
|
## API
|
|
66
65
|
|
|
67
|
-
**`
|
|
66
|
+
**`treatyQueryOptions(fn, queryOptions)`** — wraps a Treaty GET call into `queryOptions`. `fn` is a **thunk** (zero-argument function) that calls the Treaty GET function, e.g. `() => client.api.resource.get({ query: { ... } })`. This lets you bind query parameters, headers, or any other Treaty options at definition time. The second argument accepts all `queryOptions` fields except `queryFn`. Extracts `data` from the response and throws on `error`.
|
|
68
67
|
|
|
69
|
-
**`
|
|
68
|
+
**`treatyMutationOptions(fn, mutationOptions?)`** — wraps a Treaty 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 Treaty options (like `query`) as the input.
|
|
70
69
|
|
|
71
|
-
**`
|
|
70
|
+
**`InferTreatyQueryOptions<T>`** — extracts the full `UseQueryOptions` type for a given Treaty query function.
|
|
72
71
|
|
|
73
|
-
**`
|
|
72
|
+
**`InferTreatyMutationOptions<T>`** — extracts the full `UseMutationOptions` type for a given Treaty mutation function.
|
|
74
73
|
|
|
75
74
|
All helpers infer data, error, and input types end-to-end from your Elysia route definitions. No manual generics needed.
|
|
76
75
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { Treaty } from "@elysiajs/eden";
|
|
2
2
|
import { UseMutationOptions, UseQueryOptions } from "@tanstack/react-query";
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
TResponse extends
|
|
7
|
-
> = (
|
|
8
|
-
type
|
|
9
|
-
type
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
type
|
|
13
|
-
type
|
|
14
|
-
declare function
|
|
15
|
-
declare function
|
|
16
|
-
export {
|
|
3
|
+
type AnyResponse = Record<number, unknown>;
|
|
4
|
+
type TreatyMutationFunction<
|
|
5
|
+
TArgs = unknown,
|
|
6
|
+
TResponse extends AnyResponse = AnyResponse
|
|
7
|
+
> = (args: TArgs) => Promise<Treaty.TreatyResponse<TResponse>>;
|
|
8
|
+
type TreatyQueryFunction<TResponse extends AnyResponse = AnyResponse> = () => Promise<Treaty.TreatyResponse<TResponse>>;
|
|
9
|
+
type InferTreatyData<T extends TreatyMutationFunction | TreatyQueryFunction> = Awaited<ReturnType<T>>["data"];
|
|
10
|
+
type InferTreatyError<T extends TreatyMutationFunction | TreatyQueryFunction> = Awaited<ReturnType<T>>["error"];
|
|
11
|
+
type InferTreatyVariables<T extends TreatyMutationFunction> = Parameters<T> extends [] ? void : Parameters<T>[0];
|
|
12
|
+
type InferTreatyMutationOptions<T extends TreatyMutationFunction> = UseMutationOptions<InferTreatyData<T>, InferTreatyError<T>, InferTreatyVariables<T>>;
|
|
13
|
+
type InferTreatyQueryOptions<T extends TreatyQueryFunction> = UseQueryOptions<InferTreatyData<T>, InferTreatyError<T>>;
|
|
14
|
+
declare function treatyMutationOptions<T extends TreatyMutationFunction>(fn: T, options: Omit<InferTreatyMutationOptions<T>, "mutationFn">): InferTreatyMutationOptions<T>;
|
|
15
|
+
declare function treatyQueryOptions<T extends TreatyQueryFunction>(fn: T, options: Omit<InferTreatyQueryOptions<T>, "queryFn">): InferTreatyQueryOptions<T>;
|
|
16
|
+
export { treatyQueryOptions, treatyMutationOptions, TreatyQueryFunction, TreatyMutationFunction, InferTreatyVariables, InferTreatyQueryOptions, InferTreatyMutationOptions, InferTreatyError, InferTreatyData };
|
package/dist/index.js
CHANGED
|
@@ -1,34 +1,36 @@
|
|
|
1
1
|
// src/lib.ts
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
mutationOptions,
|
|
4
|
+
queryOptions
|
|
5
|
+
} from "@tanstack/react-query";
|
|
6
|
+
function treatyMutationOptions(fn, options) {
|
|
4
7
|
return mutationOptions({
|
|
8
|
+
...options,
|
|
5
9
|
mutationFn: async (variables) => {
|
|
6
|
-
const
|
|
7
|
-
const response = await fn(body, rest);
|
|
10
|
+
const response = await fn(variables);
|
|
8
11
|
const { data, error } = response;
|
|
9
12
|
if (error)
|
|
10
13
|
throw error;
|
|
11
14
|
return data;
|
|
12
|
-
}
|
|
13
|
-
...options
|
|
15
|
+
}
|
|
14
16
|
});
|
|
15
17
|
}
|
|
16
|
-
function
|
|
18
|
+
function treatyQueryOptions(fn, options) {
|
|
17
19
|
return queryOptions({
|
|
20
|
+
...options,
|
|
18
21
|
queryFn: async () => {
|
|
19
22
|
const response = await fn();
|
|
20
23
|
const { data, error } = response;
|
|
21
24
|
if (error)
|
|
22
25
|
throw error;
|
|
23
26
|
return data;
|
|
24
|
-
}
|
|
25
|
-
...options
|
|
27
|
+
}
|
|
26
28
|
});
|
|
27
29
|
}
|
|
28
30
|
export {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
treatyQueryOptions,
|
|
32
|
+
treatyMutationOptions
|
|
31
33
|
};
|
|
32
34
|
|
|
33
|
-
//# debugId=
|
|
34
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
35
|
+
//# debugId=1F341F776DA068AF64756E2164756E21
|
|
36
|
+
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsic3JjXFxsaWIudHMiXSwKICAic291cmNlc0NvbnRlbnQiOiBbCiAgICAiaW1wb3J0IHR5cGUgeyBUcmVhdHkgfSBmcm9tIFwiQGVseXNpYWpzL2VkZW5cIjtcclxuaW1wb3J0IHtcclxuICBtdXRhdGlvbk9wdGlvbnMsXHJcbiAgcXVlcnlPcHRpb25zLFxyXG4gIHR5cGUgVXNlTXV0YXRpb25PcHRpb25zLFxyXG4gIHR5cGUgVXNlUXVlcnlPcHRpb25zLFxyXG59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BQSxNQUMvQixNQUFNLFdBQVcsTUFBTSxHQUFHLFNBQVM7QUFBQSxNQUNuQyxRQUFRLE1BQU0sVUFBVTtBQUFBLE1BQ3hCLElBQUk7QUFBQSxRQUFPLE1BQU07QUFBQSxNQUNqQixPQUFPO0FBQUE7QUFBQSxFQUVYLENBQUM7QUFBQTtBQUdJLFNBQVMsa0JBQWlELENBQy9ELElBQ0EsU0FDNEI7QUFBQSxFQUc1QixPQUFPLGFBQTRCO0FBQUEsT0FDOUI7QUFBQSxJQUNILFNBQVMsWUFBWTtBQUFBLE1BQ25CLE1BQU0sV0FBVyxNQUFNLEdBQUc7QUFBQSxNQUMxQixRQUFRLE1BQU0sVUFBVTtBQUFBLE1BQ3hCLElBQUk7QUFBQSxRQUFPLE1BQU07QUFBQSxNQUNqQixPQUFPO0FBQUE7QUFBQSxFQUVYLENBQUM7QUFBQTsiLAogICJkZWJ1Z0lkIjogIjFGMzQxRjc3NkRBMDY4QUY2NDc1NkUyMTY0NzU2RTIxIiwKICAibmFtZXMiOiBbXQp9
|
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.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
],
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "bunup",
|
|
38
|
+
"format": "prettier --write .",
|
|
38
39
|
"prepublishOnly": "bun run build"
|
|
39
40
|
},
|
|
40
41
|
"peerDependencies": {
|
|
@@ -45,6 +46,7 @@
|
|
|
45
46
|
"devDependencies": {
|
|
46
47
|
"@types/bun": "latest",
|
|
47
48
|
"bunup": "^0.16.22",
|
|
48
|
-
"elysia": "^1.4.22"
|
|
49
|
+
"elysia": "^1.4.22",
|
|
50
|
+
"prettier": "^3.4.2"
|
|
49
51
|
}
|
|
50
52
|
}
|