@cratestack/adapter-tanstack-query 0.5.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/LICENSE +21 -0
- package/README.md +36 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Stephane Segning
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @cratestack/adapter-tanstack-query
|
|
2
|
+
|
|
3
|
+
Generic [TanStack Query](https://tanstack.com/query) option builders over CrateStack's generated
|
|
4
|
+
TypeScript RPC client (`transport rpc` schemas), for hand-written query/mutation hooks that don't
|
|
5
|
+
go through the fully-generated `use{Model}Query`/`use{Model}Mutation` hooks
|
|
6
|
+
(`cratestack generate-typescript`'s own `rpc-react-query.ts.j2` output) — e.g. calling a
|
|
7
|
+
`procedure` the generated hooks don't cover yet, or using vue-query/solid-query/svelte-query
|
|
8
|
+
instead of `@tanstack/react-query`.
|
|
9
|
+
|
|
10
|
+
Framework-agnostic: everything here is typed against `@tanstack/query-core`, which every TanStack
|
|
11
|
+
Query framework binding builds on.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { rpcQueryOptions, rpcMutationOptions, isRpcErrorCode } from "@cratestack/adapter-tanstack-query";
|
|
17
|
+
import { useQuery, useMutation } from "@tanstack/react-query";
|
|
18
|
+
import { client } from "./generated/client"; // your project's generated client instance
|
|
19
|
+
|
|
20
|
+
function useWidget(id: number) {
|
|
21
|
+
return useQuery({
|
|
22
|
+
...rpcQueryOptions(client.runtime, "model.Widget.get", { id }),
|
|
23
|
+
retry: (failureCount, error) => !isRpcErrorCode(error, "not_found") && failureCount < 3,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function useCreateOrder() {
|
|
28
|
+
return useMutation(rpcMutationOptions(client.runtime, "model.Order.create"));
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`rpcQueryOptions`/`rpcMutationOptions` take an `RpcCaller` — any object with a
|
|
33
|
+
`call<I, O>(opId, input, options?)` method, which is exactly the shape of a generated client's
|
|
34
|
+
public `.runtime` field (`CratestackRpcRuntime`). Requests issued this way go through the same
|
|
35
|
+
`links` chain (`@cratestack/link-batch`, `@cratestack/link-logger`, etc.) as every other call on
|
|
36
|
+
that runtime.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { RpcCaller } from "@cratestack/ts-types";
|
|
2
|
+
import type { QueryKey } from "@tanstack/query-core";
|
|
3
|
+
export type { RpcCaller } from "@cratestack/ts-types";
|
|
4
|
+
/** The `[opId, input]` tuple every helper below keys its query/mutation
|
|
5
|
+
* on — exported so callers can `queryClient.invalidateQueries({ queryKey: rpcQueryKey(opId, input) })`
|
|
6
|
+
* without re-deriving the same shape by hand. */
|
|
7
|
+
export declare function rpcQueryKey(opId: string, input?: unknown): QueryKey;
|
|
8
|
+
/** Builds a `{ queryKey, queryFn }` pair for `useQuery` (or its
|
|
9
|
+
* vue-query/solid-query/svelte-query equivalents — all built on
|
|
10
|
+
* `@tanstack/query-core`) from a single unary RPC call. Framework-
|
|
11
|
+
* agnostic: pass the result straight through, or spread it alongside
|
|
12
|
+
* your own `staleTime`/`enabled`/etc. */
|
|
13
|
+
export declare function rpcQueryOptions<I, O>(client: RpcCaller, opId: string, input: I): {
|
|
14
|
+
queryKey: QueryKey;
|
|
15
|
+
queryFn: (context: {
|
|
16
|
+
signal: AbortSignal;
|
|
17
|
+
}) => Promise<O>;
|
|
18
|
+
};
|
|
19
|
+
/** Builds a `{ mutationKey, mutationFn }` pair for `useMutation`. The
|
|
20
|
+
* input is supplied at call time (`mutate(input)`), not here — mirrors
|
|
21
|
+
* the generated `useCreateXMutation`/`useUpdateXMutation` hooks'
|
|
22
|
+
* shape. */
|
|
23
|
+
export declare function rpcMutationOptions<I, O>(client: RpcCaller, opId: string): {
|
|
24
|
+
mutationKey: QueryKey;
|
|
25
|
+
mutationFn: (input: I) => Promise<O>;
|
|
26
|
+
};
|
|
27
|
+
/** True when `error` is a `CratestackRpcError`-shaped value (or a
|
|
28
|
+
* decoded `RpcErrorBody`) whose `code` matches — structural, not an
|
|
29
|
+
* `instanceof` check, since `CratestackRpcError` is a per-project
|
|
30
|
+
* generated class with no shared import path. Useful in
|
|
31
|
+
* `retry`/`throwOnError` callbacks, e.g. never retrying a
|
|
32
|
+
* `"not_found"`. */
|
|
33
|
+
export declare function isRpcErrorCode(error: unknown, code: string): boolean;
|
|
34
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAErD,YAAY,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEtD;;kDAEkD;AAClD,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,QAAQ,CAEnE;AAED;;;;0CAI0C;AAC1C,wBAAgB,eAAe,CAAC,CAAC,EAAE,CAAC,EAClC,MAAM,EAAE,SAAS,EACjB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,CAAC,GACP;IAAE,QAAQ,EAAE,QAAQ,CAAC;IAAC,OAAO,EAAE,CAAC,OAAO,EAAE;QAAE,MAAM,EAAE,WAAW,CAAA;KAAE,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;CAAE,CAKnF;AAED;;;aAGa;AACb,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,CAAC,EACrC,MAAM,EAAE,SAAS,EACjB,IAAI,EAAE,MAAM,GACX;IAAE,WAAW,EAAE,QAAQ,CAAC;IAAC,UAAU,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;CAAE,CAKjE;AAED;;;;;qBAKqB;AACrB,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAOpE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** The `[opId, input]` tuple every helper below keys its query/mutation
|
|
2
|
+
* on — exported so callers can `queryClient.invalidateQueries({ queryKey: rpcQueryKey(opId, input) })`
|
|
3
|
+
* without re-deriving the same shape by hand. */
|
|
4
|
+
export function rpcQueryKey(opId, input) {
|
|
5
|
+
return input === undefined ? [opId] : [opId, input];
|
|
6
|
+
}
|
|
7
|
+
/** Builds a `{ queryKey, queryFn }` pair for `useQuery` (or its
|
|
8
|
+
* vue-query/solid-query/svelte-query equivalents — all built on
|
|
9
|
+
* `@tanstack/query-core`) from a single unary RPC call. Framework-
|
|
10
|
+
* agnostic: pass the result straight through, or spread it alongside
|
|
11
|
+
* your own `staleTime`/`enabled`/etc. */
|
|
12
|
+
export function rpcQueryOptions(client, opId, input) {
|
|
13
|
+
return {
|
|
14
|
+
queryKey: rpcQueryKey(opId, input),
|
|
15
|
+
queryFn: ({ signal }) => client.call(opId, input, { signal }),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** Builds a `{ mutationKey, mutationFn }` pair for `useMutation`. The
|
|
19
|
+
* input is supplied at call time (`mutate(input)`), not here — mirrors
|
|
20
|
+
* the generated `useCreateXMutation`/`useUpdateXMutation` hooks'
|
|
21
|
+
* shape. */
|
|
22
|
+
export function rpcMutationOptions(client, opId) {
|
|
23
|
+
return {
|
|
24
|
+
mutationKey: [opId],
|
|
25
|
+
mutationFn: (input) => client.call(opId, input),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** True when `error` is a `CratestackRpcError`-shaped value (or a
|
|
29
|
+
* decoded `RpcErrorBody`) whose `code` matches — structural, not an
|
|
30
|
+
* `instanceof` check, since `CratestackRpcError` is a per-project
|
|
31
|
+
* generated class with no shared import path. Useful in
|
|
32
|
+
* `retry`/`throwOnError` callbacks, e.g. never retrying a
|
|
33
|
+
* `"not_found"`. */
|
|
34
|
+
export function isRpcErrorCode(error, code) {
|
|
35
|
+
return (typeof error === "object" &&
|
|
36
|
+
error !== null &&
|
|
37
|
+
"code" in error &&
|
|
38
|
+
error.code === code);
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA;;kDAEkD;AAClD,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,KAAe;IACvD,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AACtD,CAAC;AAED;;;;0CAI0C;AAC1C,MAAM,UAAU,eAAe,CAC7B,MAAiB,EACjB,IAAY,EACZ,KAAQ;IAER,OAAO;QACL,QAAQ,EAAE,WAAW,CAAC,IAAI,EAAE,KAAK,CAAC;QAClC,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAO,IAAI,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,CAAC;KACpE,CAAC;AACJ,CAAC;AAED;;;aAGa;AACb,MAAM,UAAU,kBAAkB,CAChC,MAAiB,EACjB,IAAY;IAEZ,OAAO;QACL,WAAW,EAAE,CAAC,IAAI,CAAC;QACnB,UAAU,EAAE,CAAC,KAAQ,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAO,IAAI,EAAE,KAAK,CAAC;KACzD,CAAC;AACJ,CAAC;AAED;;;;;qBAKqB;AACrB,MAAM,UAAU,cAAc,CAAC,KAAc,EAAE,IAAY;IACzD,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACd,KAA2B,CAAC,IAAI,KAAK,IAAI,CAC3C,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cratestack/adapter-tanstack-query",
|
|
3
|
+
"version": "0.5.2",
|
|
4
|
+
"description": "Generic TanStack Query option builders (query/mutation) over CrateStack's generated TypeScript RPC client.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/cratestack/cratestack.git",
|
|
9
|
+
"directory": "packages/cratestack-adapter-tanstack-query"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://cratestack.dev",
|
|
12
|
+
"bugs": "https://github.com/cratestack/cratestack/issues",
|
|
13
|
+
"keywords": ["cratestack", "cstack", "rpc", "tanstack-query", "react-query"],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc -p tsconfig.json",
|
|
27
|
+
"test": "vitest run",
|
|
28
|
+
"lint": "biome check ."
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@cratestack/ts-types": "0.5.2"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@tanstack/query-core": "^5.0.0"
|
|
35
|
+
},
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"@tanstack/query-core": { "optional": true }
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@tanstack/query-core": "^5.0.0",
|
|
41
|
+
"typescript": "^5.7.0",
|
|
42
|
+
"vitest": "^3.0.0"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18"
|
|
46
|
+
}
|
|
47
|
+
}
|