trpc-msw 4.0.0 → 4.1.1
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 +1 -1
- package/dist/index.d.ts +2 -8
- package/dist/index.js +18 -20
- package/package.json +11 -9
package/README.md
CHANGED
|
@@ -62,7 +62,7 @@ We highly recommend you take a look at the [documentation](https://esposter.com/
|
|
|
62
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
|
-
Pass your real `router` and every call is first judged by that procedure's own input parsers, so a client sending what your server would reject gets the same `BAD_REQUEST` instead of a mocked answer; the resolver
|
|
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
|
|
@@ -112,12 +111,6 @@ export type ProcedureRegistration = SubscriptionRegistration | {
|
|
|
112
111
|
type: Exclude<TRPCProcedureType, "subscription">;
|
|
113
112
|
};
|
|
114
113
|
//#endregion
|
|
115
|
-
//#region src/models/WebSocketServerAdapter.d.ts
|
|
116
|
-
export declare class WebSocketServerAdapter<TClient extends EventEmitter> extends EventEmitter {
|
|
117
|
-
readonly clients: Set<TClient>;
|
|
118
|
-
addConnection(client: TClient, request: IncomingMessage): void;
|
|
119
|
-
}
|
|
120
|
-
//#endregion
|
|
121
114
|
//#region src/services/checkIsProcedureType.d.ts
|
|
122
115
|
export declare const checkIsProcedureType: (value: unknown) => value is TRPCProcedureType;
|
|
123
116
|
//#endregion
|
|
@@ -132,4 +125,5 @@ export declare const toBuffer: (data: WebSocketData) => Buffer;
|
|
|
132
125
|
//#endregion
|
|
133
126
|
//#region src/services/toResolverOptions.d.ts
|
|
134
127
|
export declare const toResolverOptions: ({ batchIndex, ctx, input, path, signal }: ProcedureResolverOptions<unknown, unknown>) => ProcedureResolverOptions<unknown, unknown>;
|
|
135
|
-
//#endregion
|
|
128
|
+
//#endregion
|
|
129
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from "node:events";
|
|
2
2
|
import { exhaustiveGuard } from "@esposter/shared";
|
|
3
3
|
import { TRPCError, createTRPCRecursiveProxy } from "@trpc/server";
|
|
4
|
-
import { getParseFn } from "@trpc/server/unstable-core-do-not-import";
|
|
5
4
|
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
|
|
6
5
|
import { applyWSSHandler } from "@trpc/server/adapters/ws";
|
|
7
6
|
import { http, ws } from "msw";
|
|
@@ -56,18 +55,6 @@ let UnhandledProcedureAction = /* @__PURE__ */ function(UnhandledProcedureAction
|
|
|
56
55
|
return UnhandledProcedureAction;
|
|
57
56
|
}({});
|
|
58
57
|
//#endregion
|
|
59
|
-
//#region src/models/WebSocketServerAdapter.ts
|
|
60
|
-
var WebSocketServerAdapter = class extends EventEmitter {
|
|
61
|
-
clients = /* @__PURE__ */ new Set();
|
|
62
|
-
addConnection(client, request) {
|
|
63
|
-
this.clients.add(client);
|
|
64
|
-
client.once("close", () => {
|
|
65
|
-
this.clients.delete(client);
|
|
66
|
-
});
|
|
67
|
-
this.emit("connection", client, request);
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
//#endregion
|
|
71
58
|
//#region src/services/checkIsProcedureType.ts
|
|
72
59
|
const PROCEDURE_TYPES = /* @__PURE__ */ new Set([
|
|
73
60
|
"mutation",
|
|
@@ -87,11 +74,7 @@ const toResolverOptions = ({ batchIndex, ctx, input, path, signal }) => ({
|
|
|
87
74
|
//#endregion
|
|
88
75
|
//#region src/services/createMockRouter.ts
|
|
89
76
|
const createMockRouter = ({ procedure: baseProcedure, router }, procedureTypes, registrations, inputRouter) => router(Object.fromEntries(procedureTypes.entries().map(([path, type]) => {
|
|
90
|
-
const
|
|
91
|
-
const procedure = baseProcedure.input(async (value) => {
|
|
92
|
-
for (const parseFn of parseFns) await parseFn(value);
|
|
93
|
-
return value;
|
|
94
|
-
});
|
|
77
|
+
const procedure = (inputRouter?._def.procedures[path]?._def.inputs ?? []).reduce((builder, parser) => builder.input(parser), baseProcedure);
|
|
95
78
|
const createNotFoundError = () => new TRPCError({
|
|
96
79
|
code: "NOT_FOUND",
|
|
97
80
|
message: `No "${type}"-procedure on path "${path}"`
|
|
@@ -115,19 +98,33 @@ const createMockRouter = ({ procedure: baseProcedure, router }, procedureTypes,
|
|
|
115
98
|
//#region src/services/getProcedurePaths.ts
|
|
116
99
|
const getProcedurePaths = (url, endpointPathname) => decodeURIComponent(new URL(url).pathname.slice(endpointPathname.length + 1)).split(",");
|
|
117
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
|
|
118
113
|
//#region src/createTRPCMsw.ts
|
|
119
114
|
const createTRPCMsw = ({ allowMethodOverride, createContext, endpoint, onUnhandledProcedure = "Error", router, t, webSocketUrl }) => {
|
|
120
115
|
const registrations = /* @__PURE__ */ new Map();
|
|
121
116
|
const procedureTypes = /* @__PURE__ */ new Map();
|
|
122
117
|
const endpointPathname = new URL(endpoint, "http://localhost").pathname;
|
|
118
|
+
let httpRouter;
|
|
123
119
|
const handlers = [http.all(`${endpoint}/*`, ({ request }) => {
|
|
124
120
|
if (onUnhandledProcedure === "Bypass" && !getProcedurePaths(request.url, endpointPathname).some((path) => registrations.has(path))) return void 0;
|
|
121
|
+
httpRouter ??= createMockRouter(t, procedureTypes, registrations, router);
|
|
125
122
|
return fetchRequestHandler({
|
|
126
123
|
allowMethodOverride,
|
|
127
124
|
createContext,
|
|
128
125
|
endpoint: endpointPathname,
|
|
129
126
|
req: request,
|
|
130
|
-
router:
|
|
127
|
+
router: httpRouter
|
|
131
128
|
});
|
|
132
129
|
})];
|
|
133
130
|
const createWebSocketServer = () => {
|
|
@@ -151,6 +148,7 @@ const createTRPCMsw = ({ allowMethodOverride, createContext, endpoint, onUnhandl
|
|
|
151
148
|
registrations.set(path, registration);
|
|
152
149
|
if (procedureTypes.get(path) === registration.type) return;
|
|
153
150
|
procedureTypes.set(path, registration.type);
|
|
151
|
+
httpRouter = void 0;
|
|
154
152
|
if (!webSocketServer) return;
|
|
155
153
|
for (const client of webSocketServer.clients) client.close(WEBSOCKET_ROUTER_REPLACED_CLOSE_CODE);
|
|
156
154
|
webSocketServer = void 0;
|
|
@@ -174,4 +172,4 @@ const createTRPCMsw = ({ allowMethodOverride, createContext, endpoint, onUnhandl
|
|
|
174
172
|
};
|
|
175
173
|
};
|
|
176
174
|
//#endregion
|
|
177
|
-
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": "4.
|
|
3
|
+
"version": "4.1.1",
|
|
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": "4.
|
|
52
|
+
"@esposter/shared": "4.1.1"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@arethetypeswrong/core": "^0.18.5",
|
|
56
|
-
"@esposter/configuration": "4.
|
|
56
|
+
"@esposter/configuration": "4.1.1",
|
|
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.1",
|
|
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": "47b7795ab557d7731aa60588d0b9d1cdff243328"
|
|
74
76
|
}
|