@cratestack/adapter-rtk 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 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,38 @@
1
+ # @cratestack/adapter-rtk
2
+
3
+ An [RTK Query](https://redux-toolkit.js.org/rtk-query/overview) `BaseQueryFn` adapter over
4
+ CrateStack's generated TypeScript RPC client (`transport rpc` schemas) — dispatches every endpoint
5
+ through the same runtime and `RpcLink` chain (`@cratestack/link-batch`, `@cratestack/link-logger`,
6
+ etc.) the rest of the generated client uses, instead of RTK Query's default `fetchBaseQuery`
7
+ reimplementing the wire protocol.
8
+
9
+ There is currently no fully-generated RTK Query endpoint set (unlike the generated
10
+ `rpc-react-query.ts.j2` hooks for `@tanstack/react-query`) — this package is the primitive for
11
+ hand-writing one.
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { createRpcBaseQuery } from "@cratestack/adapter-rtk";
17
+ import { createApi } from "@reduxjs/toolkit/query/react";
18
+ import { client } from "./generated/client"; // your project's generated client instance
19
+
20
+ export const api = createApi({
21
+ reducerPath: "api",
22
+ baseQuery: createRpcBaseQuery(client.runtime),
23
+ endpoints: (builder) => ({
24
+ getWidget: builder.query<Widget, number>({
25
+ query: (id) => ({ opId: "model.Widget.get", input: { id } }),
26
+ }),
27
+ createOrder: builder.mutation<Order, CreateOrderInput>({
28
+ query: (input) => ({ opId: "model.Order.create", input }),
29
+ }),
30
+ }),
31
+ });
32
+ ```
33
+
34
+ `createRpcBaseQuery` takes an `RpcCaller` — any object with a `call<I, O>(opId, input, options?)`
35
+ method, which is exactly the shape of a generated client's public `.runtime` field
36
+ (`CratestackRpcRuntime`). A failed call surfaces as `{ status: "RPC_ERROR", data: RpcErrorBody }`
37
+ when the server returned a structured error, or `{ status: "RPC_TRANSPORT_ERROR" }` for a network
38
+ failure with no such body.
@@ -0,0 +1,28 @@
1
+ import type { RpcCaller } from "@cratestack/ts-types";
2
+ import type { BaseQueryFn } from "@reduxjs/toolkit/query";
3
+ export type { RpcCaller } from "@cratestack/ts-types";
4
+ /** A single RTK Query endpoint request — the `args` shape every
5
+ * endpoint defined against {@link createRpcBaseQuery} passes to
6
+ * `query()`/`queryFn()`. */
7
+ export interface RpcBaseQueryArgs {
8
+ opId: string;
9
+ input?: unknown;
10
+ }
11
+ /** The error shape RTK Query endpoints see in `error` on a failed
12
+ * call — distinguishes a real server-side `RpcErrorBody` ("RPC_ERROR",
13
+ * `data` carries the decoded body) from a transport-level failure
14
+ * ("RPC_TRANSPORT_ERROR", no structured body). */
15
+ export interface RpcBaseQueryError {
16
+ status: "RPC_ERROR" | "RPC_TRANSPORT_ERROR";
17
+ error: string;
18
+ data?: unknown;
19
+ }
20
+ /** Adapts an `RpcCaller` (a generated client's `.runtime`, or the
21
+ * runtime itself) into an RTK Query `BaseQueryFn`, so
22
+ * `createApi({ baseQuery: createRpcBaseQuery(client.runtime) })`
23
+ * dispatches every endpoint through the same `RpcLink` chain
24
+ * (`@cratestack/link-batch`, `@cratestack/link-logger`, etc.) the rest
25
+ * of the generated client uses — instead of RTK Query's default
26
+ * `fetchBaseQuery` reimplementing the wire protocol. */
27
+ export declare function createRpcBaseQuery(client: RpcCaller): BaseQueryFn<RpcBaseQueryArgs, unknown, RpcBaseQueryError>;
28
+ //# 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,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAE1D,YAAY,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEtD;;6BAE6B;AAC7B,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;;mDAGmD;AACnD,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,WAAW,GAAG,qBAAqB,CAAC;IAC5C,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED;;;;;;yDAMyD;AACzD,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,SAAS,GAChB,WAAW,CAAC,gBAAgB,EAAE,OAAO,EAAE,iBAAiB,CAAC,CAS3D"}
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ /** Adapts an `RpcCaller` (a generated client's `.runtime`, or the
2
+ * runtime itself) into an RTK Query `BaseQueryFn`, so
3
+ * `createApi({ baseQuery: createRpcBaseQuery(client.runtime) })`
4
+ * dispatches every endpoint through the same `RpcLink` chain
5
+ * (`@cratestack/link-batch`, `@cratestack/link-logger`, etc.) the rest
6
+ * of the generated client uses — instead of RTK Query's default
7
+ * `fetchBaseQuery` reimplementing the wire protocol. */
8
+ export function createRpcBaseQuery(client) {
9
+ return async ({ opId, input }, api) => {
10
+ try {
11
+ const data = await client.call(opId, input ?? null, { signal: api.signal });
12
+ return { data };
13
+ }
14
+ catch (error) {
15
+ return { error: toBaseQueryError(error) };
16
+ }
17
+ };
18
+ }
19
+ function toBaseQueryError(error) {
20
+ if (typeof error === "object" && error !== null && "code" in error && "message" in error) {
21
+ const body = error;
22
+ return { status: "RPC_ERROR", error: body.message, data: body };
23
+ }
24
+ return {
25
+ status: "RPC_TRANSPORT_ERROR",
26
+ error: error instanceof Error ? error.message : String(error),
27
+ };
28
+ }
29
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAuBA;;;;;;yDAMyD;AACzD,MAAM,UAAU,kBAAkB,CAChC,MAAiB;IAEjB,OAAO,KAAK,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,GAAG,EAAE,EAAE;QACpC,IAAI,CAAC;YACH,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,IAAI,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;YAC5E,OAAO,EAAE,IAAI,EAAE,CAAC;QAClB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,EAAE,KAAK,EAAE,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;QAC5C,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAc;IACtC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,MAAM,IAAI,KAAK,IAAI,SAAS,IAAI,KAAK,EAAE,CAAC;QACzF,MAAM,IAAI,GAAG,KAA6D,CAAC;QAC3E,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IAClE,CAAC;IACD,OAAO;QACL,MAAM,EAAE,qBAAqB;QAC7B,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;KAC9D,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@cratestack/adapter-rtk",
3
+ "version": "0.5.2",
4
+ "description": "An RTK Query BaseQueryFn adapter 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-rtk"
10
+ },
11
+ "homepage": "https://cratestack.dev",
12
+ "bugs": "https://github.com/cratestack/cratestack/issues",
13
+ "keywords": ["cratestack", "cstack", "rpc", "rtk-query", "redux-toolkit"],
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
+ "@reduxjs/toolkit": "^2.0.0"
35
+ },
36
+ "peerDependenciesMeta": {
37
+ "@reduxjs/toolkit": { "optional": true }
38
+ },
39
+ "devDependencies": {
40
+ "@reduxjs/toolkit": "^2.0.0",
41
+ "typescript": "^5.7.0",
42
+ "vitest": "^3.0.0"
43
+ },
44
+ "engines": {
45
+ "node": ">=18"
46
+ }
47
+ }