trpc-msw 4.3.3 → 4.5.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 +22 -22
- package/dist/index.d.ts +12 -6
- package/dist/index.js +41 -21
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -5,22 +5,28 @@
|
|
|
5
5
|
[![NPM downloads][badge-npm-downloads]][url-npm]
|
|
6
6
|
[![NPM Unpacked Size (with version)][badge-npm-unpacked-size]][url-npm]
|
|
7
7
|
|
|
8
|
-
tRPC
|
|
8
|
+
This package connects tRPC to [Mock Service Worker](https://mswjs.io), and tRPC's own handlers answer every client call at the network level. You register a resolver for each procedure, typed from your router.
|
|
9
|
+
|
|
10
|
+
Design notes live in [the specs](https://github.com/Esposter/Esposter/tree/main/openspec/specs/trpc-msw).
|
|
9
11
|
|
|
10
12
|
## Table of Contents
|
|
11
13
|
|
|
12
14
|
- 🚀 [Getting Started](#getting-started)
|
|
13
|
-
-
|
|
15
|
+
- 🧩 [Key Exports](#key-exports)
|
|
14
16
|
- ⚖️ [License](#license)
|
|
15
17
|
|
|
16
18
|
---
|
|
17
19
|
|
|
18
20
|
## <a name="getting-started">🚀 Getting Started</a>
|
|
19
21
|
|
|
22
|
+
1. Install the packages.
|
|
23
|
+
|
|
20
24
|
```bash
|
|
21
25
|
pnpm i -D trpc-msw msw @trpc/server
|
|
22
26
|
```
|
|
23
27
|
|
|
28
|
+
2. Create a test file from this code. The client receives each answer from its resolver.
|
|
29
|
+
|
|
24
30
|
```ts
|
|
25
31
|
import type { AppRouter } from "./server/router";
|
|
26
32
|
|
|
@@ -50,30 +56,24 @@ test("reads a post", async () => {
|
|
|
50
56
|
});
|
|
51
57
|
```
|
|
52
58
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
We highly recommend you take a look at the [documentation](https://github.com/Esposter/Esposter/tree/main/openspec/specs/trpc-msw) to level up. It covers the design and the upstream `msw-trpc` issues this package answers.
|
|
59
|
+
Batching, your transformer, your error formatter, `FormData` input and subscriptions over server-sent events or WebSockets behave exactly as they do against the real server.
|
|
56
60
|
|
|
57
|
-
|
|
61
|
+
## <a name="key-exports">🧩 Key Exports</a>
|
|
58
62
|
|
|
59
|
-
| Export | Description
|
|
60
|
-
| -------------------------- |
|
|
61
|
-
| `createTRPCMsw` | The factory
|
|
62
|
-
| `TRPCMswOptions` | `t
|
|
63
|
-
| `UnhandledProcedureAction` | `Error` (default) answers an unregistered procedure with tRPC's `NOT_FOUND
|
|
63
|
+
| Export | Description |
|
|
64
|
+
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
65
|
+
| `createTRPCMsw` | The factory. It returns the msw `handlers`, the typed `trpc` registration proxy and `reset`. |
|
|
66
|
+
| `TRPCMswOptions` | The options object. Required: `t` and `endpoint`. Optional: `allowMethodOverride`, `createContext`, `onUnhandledProcedure`, `router` and `webSocketUrl`. |
|
|
67
|
+
| `UnhandledProcedureAction` | `Error` (default) answers an unregistered procedure with tRPC's `NOT_FOUND`. `Bypass` passes the request to the next handler. |
|
|
64
68
|
|
|
65
|
-
Pass your real `router`
|
|
69
|
+
Pass your real `router` to check each call against its procedure's input parsers before the resolver runs.
|
|
66
70
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
pnpm test # vitest watch mode (coverage is run from the repo root)
|
|
74
|
-
pnpm lint:fix # auto-fix lint
|
|
75
|
-
pnpm typecheck # type check
|
|
76
|
-
```
|
|
71
|
+
- The parsers are Standard Schema validators or plain functions.
|
|
72
|
+
- A call with input that your server would reject gets the same `BAD_REQUEST`, not a mocked answer.
|
|
73
|
+
- The resolver receives the values that the parsers return, as it does on the server.
|
|
74
|
+
- A resolver receives `ctx`, `input`, `path` and `signal`, as a real resolver does.
|
|
75
|
+
- A resolver returns the procedure's output before the transformer. The transformer runs on the way out, as it does on the server.
|
|
76
|
+
- A thrown `TRPCError` reaches the client through your error formatter.
|
|
77
77
|
|
|
78
78
|
## <a name="license">⚖️ License</a>
|
|
79
79
|
|
package/dist/index.d.ts
CHANGED
|
@@ -62,18 +62,24 @@ export interface TRPCMswOptions<TContext extends object> {
|
|
|
62
62
|
webSocketUrl?: string;
|
|
63
63
|
}
|
|
64
64
|
export declare const createTRPCMsw: <TRouter extends AnyTRPCRouter, TContext extends object = object>({ allowMethodOverride, createContext, endpoint, onUnhandledProcedure, router, t, webSocketUrl }: TRPCMswOptions<TContext>) => TRPCMsw<TRouter, TContext>;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
#private;
|
|
65
|
+
declare class WebSocketClientAdapter extends EventEmitter {
|
|
66
|
+
readonly close: (code?: number, reason?: string) => void;
|
|
68
67
|
readonly CLOSED: number;
|
|
69
68
|
readonly CLOSING: number;
|
|
70
69
|
readonly CONNECTING: number;
|
|
71
70
|
readonly OPEN: number;
|
|
72
71
|
readyState: number;
|
|
72
|
+
readonly send: (data: string) => void;
|
|
73
|
+
readonly terminate: () => void;
|
|
74
|
+
constructor(transport: {
|
|
75
|
+
close: (code?: number, reason?: string) => void;
|
|
76
|
+
send: (data: string) => void;
|
|
77
|
+
terminate: () => void;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
type MswWebSocketClient = WebSocketHandlerConnection["client"];
|
|
81
|
+
export declare class MswWebSocketAdapter extends WebSocketClientAdapter {
|
|
73
82
|
constructor(client: MswWebSocketClient);
|
|
74
|
-
close(code?: number, reason?: string): void;
|
|
75
|
-
send(data: string): void;
|
|
76
|
-
terminate(): void;
|
|
77
83
|
}
|
|
78
84
|
export interface SubscriptionRegistration {
|
|
79
85
|
resolver: (options: ProcedureResolverOptions<unknown, unknown>) => Promisable<AsyncIterable<unknown>>;
|
package/dist/index.js
CHANGED
|
@@ -15,17 +15,45 @@ const toBuffer = (data) => {
|
|
|
15
15
|
else if (data instanceof Blob) throw new TypeError("A WebSocket message sent as a Blob cannot be read in order");
|
|
16
16
|
else return Buffer.from(data);
|
|
17
17
|
};
|
|
18
|
-
//
|
|
19
|
-
var
|
|
18
|
+
// ../trpc-nuxt-module/dist/runtime/server/models/WebSocketClientAdapter.js
|
|
19
|
+
var WebSocketClientAdapter = class extends EventEmitter {
|
|
20
|
+
close;
|
|
20
21
|
CLOSED = WebSocket.CLOSED;
|
|
21
22
|
CLOSING = WebSocket.CLOSING;
|
|
22
23
|
CONNECTING = WebSocket.CONNECTING;
|
|
23
24
|
OPEN = WebSocket.OPEN;
|
|
24
25
|
readyState = WebSocket.OPEN;
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
send;
|
|
27
|
+
terminate;
|
|
28
|
+
constructor(transport) {
|
|
27
29
|
super();
|
|
28
|
-
this
|
|
30
|
+
this.close = (code, reason) => {
|
|
31
|
+
this.readyState = WebSocket.CLOSING;
|
|
32
|
+
transport.close(code, reason);
|
|
33
|
+
};
|
|
34
|
+
this.send = (data) => {
|
|
35
|
+
transport.send(data);
|
|
36
|
+
};
|
|
37
|
+
this.terminate = () => {
|
|
38
|
+
this.readyState = WebSocket.CLOSING;
|
|
39
|
+
transport.terminate();
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
// src/models/MswWebSocketAdapter.ts
|
|
44
|
+
var MswWebSocketAdapter = class extends WebSocketClientAdapter {
|
|
45
|
+
constructor(client) {
|
|
46
|
+
super({
|
|
47
|
+
close: (code, reason) => {
|
|
48
|
+
client.close(code, reason);
|
|
49
|
+
},
|
|
50
|
+
send: (data) => {
|
|
51
|
+
client.send(data);
|
|
52
|
+
},
|
|
53
|
+
terminate: () => {
|
|
54
|
+
client.close();
|
|
55
|
+
}
|
|
56
|
+
});
|
|
29
57
|
client.addEventListener("message", (event) => {
|
|
30
58
|
this.emit("message", toBuffer(event.data), false);
|
|
31
59
|
});
|
|
@@ -34,16 +62,6 @@ var MswWebSocketAdapter = class extends EventEmitter {
|
|
|
34
62
|
this.emit("close", event.code, event.reason);
|
|
35
63
|
});
|
|
36
64
|
}
|
|
37
|
-
close(code, reason) {
|
|
38
|
-
this.readyState = WebSocket.CLOSING;
|
|
39
|
-
this.#client.close(code, reason);
|
|
40
|
-
}
|
|
41
|
-
send(data) {
|
|
42
|
-
this.#client.send(data);
|
|
43
|
-
}
|
|
44
|
-
terminate() {
|
|
45
|
-
this.close();
|
|
46
|
-
}
|
|
47
65
|
};
|
|
48
66
|
// src/models/UnhandledProcedureAction.ts
|
|
49
67
|
let UnhandledProcedureAction = /* @__PURE__ */ function(UnhandledProcedureAction) {
|
|
@@ -93,12 +111,14 @@ const getProcedurePaths = (url, endpointPathname) => decodeURIComponent(new URL(
|
|
|
93
111
|
// ../trpc-nuxt-module/dist/runtime/server/models/WebSocketServerAdapter.js
|
|
94
112
|
var WebSocketServerAdapter = class extends EventEmitter {
|
|
95
113
|
clients = /* @__PURE__ */ new Set();
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
this.clients.
|
|
114
|
+
constructor() {
|
|
115
|
+
super();
|
|
116
|
+
this.on("connection", (client) => {
|
|
117
|
+
this.clients.add(client);
|
|
118
|
+
client.once("close", () => {
|
|
119
|
+
this.clients.delete(client);
|
|
120
|
+
});
|
|
100
121
|
});
|
|
101
|
-
this.emit("connection", client, request);
|
|
102
122
|
}
|
|
103
123
|
};
|
|
104
124
|
// src/createTRPCMsw.ts
|
|
@@ -133,7 +153,7 @@ const createTRPCMsw = ({ allowMethodOverride, createContext, endpoint, onUnhandl
|
|
|
133
153
|
const request = new IncomingMessage(new Socket());
|
|
134
154
|
request.url = `${client.url.pathname}${client.url.search}`;
|
|
135
155
|
webSocketServer ??= createWebSocketServer();
|
|
136
|
-
webSocketServer.
|
|
156
|
+
webSocketServer.emit("connection", new MswWebSocketAdapter(client), request);
|
|
137
157
|
}));
|
|
138
158
|
const register = (path, registration) => {
|
|
139
159
|
registrations.set(path, registration);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trpc-msw",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.5.0",
|
|
4
4
|
"description": "tRPC for Mock Service Worker — answers queries, mutations and subscriptions over HTTP, batching, SSE and WebSockets with tRPC's own handlers, typed off your router.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"batch",
|
|
@@ -44,28 +44,28 @@
|
|
|
44
44
|
"lint": "eslint .",
|
|
45
45
|
"lint:fix": "eslint --fix .",
|
|
46
46
|
"test": "vitest",
|
|
47
|
-
"typecheck": "
|
|
47
|
+
"typecheck": "tsrs --fast"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@esposter/shared": "4.
|
|
50
|
+
"@esposter/shared": "4.5.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@esposter/configuration": "4.
|
|
53
|
+
"@esposter/configuration": "4.5.0",
|
|
54
54
|
"@standard-schema/spec": "^1.1.0",
|
|
55
|
-
"@trpc/client": "^11.
|
|
56
|
-
"@types/node": "^26.6.
|
|
55
|
+
"@trpc/client": "^11.24.0",
|
|
56
|
+
"@types/node": "^26.6.6",
|
|
57
57
|
"superjson": "^2.2.6",
|
|
58
|
-
"trpc-nuxt-module": "4.
|
|
59
|
-
"tsdown": "^0.23.
|
|
58
|
+
"trpc-nuxt-module": "4.5.0",
|
|
59
|
+
"tsdown": "^0.23.5",
|
|
60
60
|
"type-fest": "^5.10.0",
|
|
61
61
|
"zod": "^4.6.7"
|
|
62
62
|
},
|
|
63
63
|
"peerDependencies": {
|
|
64
|
-
"@trpc/server": "^11.
|
|
64
|
+
"@trpc/server": "^11.24.0",
|
|
65
65
|
"msw": "^3.0.3"
|
|
66
66
|
},
|
|
67
67
|
"inlinedDependencies": {
|
|
68
68
|
"type-fest": "5.10.0"
|
|
69
69
|
},
|
|
70
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "e30c001f3b4b15693e75fb53092d62a97ade4d2b"
|
|
71
71
|
}
|