trpc-msw 3.9.0 → 4.1.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 +2 -2
- package/dist/index.d.ts +5 -10
- package/dist/index.js +26 -23
- package/package.json +11 -9
package/README.md
CHANGED
|
@@ -59,10 +59,10 @@ We highly recommend you take a look at the [documentation](https://esposter.com/
|
|
|
59
59
|
| Export | Description |
|
|
60
60
|
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
|
|
61
61
|
| `createTRPCMsw` | The factory — returns the msw `handlers`, the typed `trpc` registration proxy and `reset` |
|
|
62
|
-
| `TRPCMswOptions` | `t`, `endpoint`, and optionally `webSocketUrl`, `createContext`, `allowMethodOverride` and `onUnhandledProcedure`
|
|
62
|
+
| `TRPCMswOptions` | `t`, `endpoint`, and optionally `router`, `webSocketUrl`, `createContext`, `allowMethodOverride` and `onUnhandledProcedure` |
|
|
63
63
|
| `UnhandledProcedureAction` | `Error` (default) answers an unregistered procedure with tRPC's `NOT_FOUND`; `Bypass` passes the request on to the next handler |
|
|
64
64
|
|
|
65
|
-
A resolver receives what a real one does — `ctx`, `input`, `path`, `signal` — and returns the procedure's output before the transformer, which runs on the way out as it does on the server. A thrown `TRPCError` reaches the client through your error formatter.
|
|
65
|
+
Pass your real `router` and every call is first judged by that procedure's own input parsers (Standard Schema validators, or plain functions), so a client sending what your server would reject gets the same `BAD_REQUEST` instead of a mocked answer; the resolver receives what those parsers return, as it does on the server. A resolver receives what a real one does — `ctx`, `input`, `path`, `signal` — and returns the procedure's output before the transformer, which runs on the way out as it does on the server. A thrown `TRPCError` reaches the client through your error formatter.
|
|
66
66
|
|
|
67
67
|
### Commands
|
|
68
68
|
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { AnyTRPCProcedure, AnyTRPCRootTypes, AnyTRPCRouter, TRPCProcedureType, T
|
|
|
3
3
|
import { FetchCreateContextFnOptions } from "@trpc/server/adapters/fetch";
|
|
4
4
|
import { CreateWSSContextFnOptions } from "@trpc/server/adapters/ws";
|
|
5
5
|
import { HttpHandler, WebSocketData, WebSocketHandler, WebSocketHandlerConnection } from "msw";
|
|
6
|
-
import { IncomingMessage } from "node:http";
|
|
7
6
|
//#region src/constants.d.ts
|
|
8
7
|
export declare const WEBSOCKET_ROUTER_REPLACED_CLOSE_CODE = 4e3;
|
|
9
8
|
//#endregion
|
|
@@ -77,12 +76,13 @@ export interface TRPCMswOptions<TContext extends object> {
|
|
|
77
76
|
createContext?: (options: CreateContextOptions) => Promisable<TContext>;
|
|
78
77
|
endpoint: string;
|
|
79
78
|
onUnhandledProcedure?: UnhandledProcedureAction;
|
|
79
|
+
router?: AnyTRPCRouter;
|
|
80
80
|
t: TRPCMswRoot<TContext>;
|
|
81
81
|
webSocketUrl?: string;
|
|
82
82
|
}
|
|
83
83
|
//#endregion
|
|
84
84
|
//#region src/createTRPCMsw.d.ts
|
|
85
|
-
export declare const createTRPCMsw: <TRouter extends AnyTRPCRouter, TContext extends object = object>({ allowMethodOverride, createContext, endpoint, onUnhandledProcedure, t, webSocketUrl }: TRPCMswOptions<TContext>) => TRPCMsw<TRouter, TContext>;
|
|
85
|
+
export declare const createTRPCMsw: <TRouter extends AnyTRPCRouter, TContext extends object = object>({ allowMethodOverride, createContext, endpoint, onUnhandledProcedure, router, t, webSocketUrl }: TRPCMswOptions<TContext>) => TRPCMsw<TRouter, TContext>;
|
|
86
86
|
//#endregion
|
|
87
87
|
//#region src/models/MswWebSocketAdapter.d.ts
|
|
88
88
|
type MswWebSocketClient = WebSocketHandlerConnection["client"];
|
|
@@ -111,17 +111,11 @@ export type ProcedureRegistration = SubscriptionRegistration | {
|
|
|
111
111
|
type: Exclude<TRPCProcedureType, "subscription">;
|
|
112
112
|
};
|
|
113
113
|
//#endregion
|
|
114
|
-
//#region src/models/WebSocketServerAdapter.d.ts
|
|
115
|
-
export declare class WebSocketServerAdapter<TClient extends EventEmitter> extends EventEmitter {
|
|
116
|
-
readonly clients: Set<TClient>;
|
|
117
|
-
addConnection(client: TClient, request: IncomingMessage): void;
|
|
118
|
-
}
|
|
119
|
-
//#endregion
|
|
120
114
|
//#region src/services/checkIsProcedureType.d.ts
|
|
121
115
|
export declare const checkIsProcedureType: (value: unknown) => value is TRPCProcedureType;
|
|
122
116
|
//#endregion
|
|
123
117
|
//#region src/services/createMockRouter.d.ts
|
|
124
|
-
export declare const createMockRouter: <TContext extends object>({ procedure: baseProcedure, router }: TRPCMswRoot<TContext>, procedureTypes: ReadonlyMap<string, TRPCProcedureType>, registrations: ReadonlyMap<string, ProcedureRegistration
|
|
118
|
+
export declare const createMockRouter: <TContext extends object>({ procedure: baseProcedure, router }: TRPCMswRoot<TContext>, procedureTypes: ReadonlyMap<string, TRPCProcedureType>, registrations: ReadonlyMap<string, ProcedureRegistration>, inputRouter?: AnyTRPCRouter) => AnyTRPCRouter;
|
|
125
119
|
//#endregion
|
|
126
120
|
//#region src/services/getProcedurePaths.d.ts
|
|
127
121
|
export declare const getProcedurePaths: (url: string, endpointPathname: string) => string[];
|
|
@@ -131,4 +125,5 @@ export declare const toBuffer: (data: WebSocketData) => Buffer;
|
|
|
131
125
|
//#endregion
|
|
132
126
|
//#region src/services/toResolverOptions.d.ts
|
|
133
127
|
export declare const toResolverOptions: ({ batchIndex, ctx, input, path, signal }: ProcedureResolverOptions<unknown, unknown>) => ProcedureResolverOptions<unknown, unknown>;
|
|
134
|
-
//#endregion
|
|
128
|
+
//#endregion
|
|
129
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ const toBuffer = (data) => {
|
|
|
14
14
|
if (typeof data === "string") return Buffer.from(data);
|
|
15
15
|
else if (ArrayBuffer.isView(data)) return Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
16
16
|
else if (data instanceof Blob) throw new TypeError("A WebSocket message sent as a Blob cannot be read in order");
|
|
17
|
-
return Buffer.from(data);
|
|
17
|
+
else return Buffer.from(data);
|
|
18
18
|
};
|
|
19
19
|
//#endregion
|
|
20
20
|
//#region src/models/MswWebSocketAdapter.ts
|
|
@@ -55,25 +55,13 @@ let UnhandledProcedureAction = /* @__PURE__ */ function(UnhandledProcedureAction
|
|
|
55
55
|
return UnhandledProcedureAction;
|
|
56
56
|
}({});
|
|
57
57
|
//#endregion
|
|
58
|
-
//#region src/models/WebSocketServerAdapter.ts
|
|
59
|
-
var WebSocketServerAdapter = class extends EventEmitter {
|
|
60
|
-
clients = /* @__PURE__ */ new Set();
|
|
61
|
-
addConnection(client, request) {
|
|
62
|
-
this.clients.add(client);
|
|
63
|
-
client.once("close", () => {
|
|
64
|
-
this.clients.delete(client);
|
|
65
|
-
});
|
|
66
|
-
this.emit("connection", client, request);
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
//#endregion
|
|
70
58
|
//#region src/services/checkIsProcedureType.ts
|
|
71
|
-
const
|
|
59
|
+
const PROCEDURE_TYPES = /* @__PURE__ */ new Set([
|
|
72
60
|
"mutation",
|
|
73
61
|
"query",
|
|
74
62
|
"subscription"
|
|
75
63
|
]);
|
|
76
|
-
const checkIsProcedureType = (value) =>
|
|
64
|
+
const checkIsProcedureType = (value) => PROCEDURE_TYPES.has(value);
|
|
77
65
|
//#endregion
|
|
78
66
|
//#region src/services/toResolverOptions.ts
|
|
79
67
|
const toResolverOptions = ({ batchIndex, ctx, input, path, signal }) => ({
|
|
@@ -85,8 +73,8 @@ const toResolverOptions = ({ batchIndex, ctx, input, path, signal }) => ({
|
|
|
85
73
|
});
|
|
86
74
|
//#endregion
|
|
87
75
|
//#region src/services/createMockRouter.ts
|
|
88
|
-
const createMockRouter = ({ procedure: baseProcedure, router }, procedureTypes, registrations) => router(Object.fromEntries(procedureTypes.entries().map(([path, type]) => {
|
|
89
|
-
const procedure =
|
|
76
|
+
const createMockRouter = ({ procedure: baseProcedure, router }, procedureTypes, registrations, inputRouter) => router(Object.fromEntries(procedureTypes.entries().map(([path, type]) => {
|
|
77
|
+
const procedure = (inputRouter?._def.procedures[path]?._def.inputs ?? []).reduce((builder, parser) => builder.input(parser), baseProcedure);
|
|
90
78
|
const createNotFoundError = () => new TRPCError({
|
|
91
79
|
code: "NOT_FOUND",
|
|
92
80
|
message: `No "${type}"-procedure on path "${path}"`
|
|
@@ -95,8 +83,8 @@ const createMockRouter = ({ procedure: baseProcedure, router }, procedureTypes,
|
|
|
95
83
|
case "mutation":
|
|
96
84
|
case "query": return [path, procedure[type]((options) => {
|
|
97
85
|
const registration = registrations.get(path);
|
|
98
|
-
if (registration?.type
|
|
99
|
-
|
|
86
|
+
if (registration?.type === type) return registration.resolver(toResolverOptions(options));
|
|
87
|
+
else throw createNotFoundError();
|
|
100
88
|
})];
|
|
101
89
|
case "subscription": return [path, procedure.subscription(async function* (options) {
|
|
102
90
|
const registration = registrations.get(path);
|
|
@@ -110,26 +98,40 @@ const createMockRouter = ({ procedure: baseProcedure, router }, procedureTypes,
|
|
|
110
98
|
//#region src/services/getProcedurePaths.ts
|
|
111
99
|
const getProcedurePaths = (url, endpointPathname) => decodeURIComponent(new URL(url).pathname.slice(endpointPathname.length + 1)).split(",");
|
|
112
100
|
//#endregion
|
|
101
|
+
//#region ../trpc-nuxt-module/dist/runtime/server/models/WebSocketServerAdapter.js
|
|
102
|
+
var WebSocketServerAdapter = class extends EventEmitter {
|
|
103
|
+
clients = /* @__PURE__ */ new Set();
|
|
104
|
+
addConnection(client, request) {
|
|
105
|
+
this.clients.add(client);
|
|
106
|
+
client.once("close", () => {
|
|
107
|
+
this.clients.delete(client);
|
|
108
|
+
});
|
|
109
|
+
this.emit("connection", client, request);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
//#endregion
|
|
113
113
|
//#region src/createTRPCMsw.ts
|
|
114
|
-
const createTRPCMsw = ({ allowMethodOverride, createContext, endpoint, onUnhandledProcedure = "Error", t, webSocketUrl }) => {
|
|
114
|
+
const createTRPCMsw = ({ allowMethodOverride, createContext, endpoint, onUnhandledProcedure = "Error", router, t, webSocketUrl }) => {
|
|
115
115
|
const registrations = /* @__PURE__ */ new Map();
|
|
116
116
|
const procedureTypes = /* @__PURE__ */ new Map();
|
|
117
117
|
const endpointPathname = new URL(endpoint, "http://localhost").pathname;
|
|
118
|
+
let httpRouter;
|
|
118
119
|
const handlers = [http.all(`${endpoint}/*`, ({ request }) => {
|
|
119
120
|
if (onUnhandledProcedure === "Bypass" && !getProcedurePaths(request.url, endpointPathname).some((path) => registrations.has(path))) return void 0;
|
|
121
|
+
httpRouter ??= createMockRouter(t, procedureTypes, registrations, router);
|
|
120
122
|
return fetchRequestHandler({
|
|
121
123
|
allowMethodOverride,
|
|
122
124
|
createContext,
|
|
123
125
|
endpoint: endpointPathname,
|
|
124
126
|
req: request,
|
|
125
|
-
router:
|
|
127
|
+
router: httpRouter
|
|
126
128
|
});
|
|
127
129
|
})];
|
|
128
130
|
const createWebSocketServer = () => {
|
|
129
131
|
const webSocketServer = new WebSocketServerAdapter();
|
|
130
132
|
applyWSSHandler({
|
|
131
133
|
createContext,
|
|
132
|
-
router: createMockRouter(t, new Map(procedureTypes), registrations),
|
|
134
|
+
router: createMockRouter(t, new Map(procedureTypes), registrations, router),
|
|
133
135
|
wss: webSocketServer
|
|
134
136
|
});
|
|
135
137
|
return webSocketServer;
|
|
@@ -146,6 +148,7 @@ const createTRPCMsw = ({ allowMethodOverride, createContext, endpoint, onUnhandl
|
|
|
146
148
|
registrations.set(path, registration);
|
|
147
149
|
if (procedureTypes.get(path) === registration.type) return;
|
|
148
150
|
procedureTypes.set(path, registration.type);
|
|
151
|
+
httpRouter = void 0;
|
|
149
152
|
if (!webSocketServer) return;
|
|
150
153
|
for (const client of webSocketServer.clients) client.close(WEBSOCKET_ROUTER_REPLACED_CLOSE_CODE);
|
|
151
154
|
webSocketServer = void 0;
|
|
@@ -169,4 +172,4 @@ const createTRPCMsw = ({ allowMethodOverride, createContext, endpoint, onUnhandl
|
|
|
169
172
|
};
|
|
170
173
|
};
|
|
171
174
|
//#endregion
|
|
172
|
-
export { MswWebSocketAdapter, UnhandledProcedureAction, WEBSOCKET_ROUTER_REPLACED_CLOSE_CODE,
|
|
175
|
+
export { MswWebSocketAdapter, UnhandledProcedureAction, WEBSOCKET_ROUTER_REPLACED_CLOSE_CODE, checkIsProcedureType, createMockRouter, createTRPCMsw, getProcedurePaths, toBuffer, toResolverOptions };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trpc-msw",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.1.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",
|
|
@@ -49,26 +49,28 @@
|
|
|
49
49
|
"typecheck": "tsc"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@esposter/shared": "
|
|
52
|
+
"@esposter/shared": "4.1.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@arethetypeswrong/core": "^0.18.5",
|
|
56
|
-
"@esposter/configuration": "
|
|
56
|
+
"@esposter/configuration": "4.1.0",
|
|
57
|
+
"@standard-schema/spec": "^1.1.0",
|
|
57
58
|
"@trpc/client": "^11.19.0",
|
|
58
|
-
"@types/node": "^26.6.
|
|
59
|
+
"@types/node": "^26.6.5",
|
|
59
60
|
"publint": "^0.3.25",
|
|
60
61
|
"superjson": "^2.2.6",
|
|
61
|
-
"
|
|
62
|
+
"trpc-nuxt-module": "4.1.0",
|
|
63
|
+
"tsdown": "^0.23.1",
|
|
62
64
|
"type-fest": "^5.10.0",
|
|
63
|
-
"vitest": "^5.0.
|
|
64
|
-
"zod": "^4.6.
|
|
65
|
+
"vitest": "^5.0.4",
|
|
66
|
+
"zod": "^4.6.7"
|
|
65
67
|
},
|
|
66
68
|
"peerDependencies": {
|
|
67
69
|
"@trpc/server": "^11.19.0",
|
|
68
|
-
"msw": "^3.0.
|
|
70
|
+
"msw": "^3.0.3"
|
|
69
71
|
},
|
|
70
72
|
"inlinedDependencies": {
|
|
71
73
|
"type-fest": "5.10.0"
|
|
72
74
|
},
|
|
73
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "68de4c22fa7591bf6f6cc0fab2c9a906d8c4d71a"
|
|
74
76
|
}
|