trpc-msw 3.9.0 → 4.0.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 +3 -2
- package/dist/index.js +15 -10
- package/package.json +5 -5
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, so a client sending what your server would reject gets the same `BAD_REQUEST` instead of a mocked answer; the resolver still receives the input as decoded. 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
|
@@ -77,12 +77,13 @@ export interface TRPCMswOptions<TContext extends object> {
|
|
|
77
77
|
createContext?: (options: CreateContextOptions) => Promisable<TContext>;
|
|
78
78
|
endpoint: string;
|
|
79
79
|
onUnhandledProcedure?: UnhandledProcedureAction;
|
|
80
|
+
router?: AnyTRPCRouter;
|
|
80
81
|
t: TRPCMswRoot<TContext>;
|
|
81
82
|
webSocketUrl?: string;
|
|
82
83
|
}
|
|
83
84
|
//#endregion
|
|
84
85
|
//#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>;
|
|
86
|
+
export declare const createTRPCMsw: <TRouter extends AnyTRPCRouter, TContext extends object = object>({ allowMethodOverride, createContext, endpoint, onUnhandledProcedure, router, t, webSocketUrl }: TRPCMswOptions<TContext>) => TRPCMsw<TRouter, TContext>;
|
|
86
87
|
//#endregion
|
|
87
88
|
//#region src/models/MswWebSocketAdapter.d.ts
|
|
88
89
|
type MswWebSocketClient = WebSocketHandlerConnection["client"];
|
|
@@ -121,7 +122,7 @@ export declare class WebSocketServerAdapter<TClient extends EventEmitter> extend
|
|
|
121
122
|
export declare const checkIsProcedureType: (value: unknown) => value is TRPCProcedureType;
|
|
122
123
|
//#endregion
|
|
123
124
|
//#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
|
|
125
|
+
export declare const createMockRouter: <TContext extends object>({ procedure: baseProcedure, router }: TRPCMswRoot<TContext>, procedureTypes: ReadonlyMap<string, TRPCProcedureType>, registrations: ReadonlyMap<string, ProcedureRegistration>, inputRouter?: AnyTRPCRouter) => AnyTRPCRouter;
|
|
125
126
|
//#endregion
|
|
126
127
|
//#region src/services/getProcedurePaths.d.ts
|
|
127
128
|
export declare const getProcedurePaths: (url: string, endpointPathname: string) => string[];
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
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";
|
|
4
5
|
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
|
|
5
6
|
import { applyWSSHandler } from "@trpc/server/adapters/ws";
|
|
6
7
|
import { http, ws } from "msw";
|
|
@@ -14,7 +15,7 @@ const toBuffer = (data) => {
|
|
|
14
15
|
if (typeof data === "string") return Buffer.from(data);
|
|
15
16
|
else if (ArrayBuffer.isView(data)) return Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
16
17
|
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);
|
|
18
|
+
else return Buffer.from(data);
|
|
18
19
|
};
|
|
19
20
|
//#endregion
|
|
20
21
|
//#region src/models/MswWebSocketAdapter.ts
|
|
@@ -68,12 +69,12 @@ var WebSocketServerAdapter = class extends EventEmitter {
|
|
|
68
69
|
};
|
|
69
70
|
//#endregion
|
|
70
71
|
//#region src/services/checkIsProcedureType.ts
|
|
71
|
-
const
|
|
72
|
+
const PROCEDURE_TYPES = /* @__PURE__ */ new Set([
|
|
72
73
|
"mutation",
|
|
73
74
|
"query",
|
|
74
75
|
"subscription"
|
|
75
76
|
]);
|
|
76
|
-
const checkIsProcedureType = (value) =>
|
|
77
|
+
const checkIsProcedureType = (value) => PROCEDURE_TYPES.has(value);
|
|
77
78
|
//#endregion
|
|
78
79
|
//#region src/services/toResolverOptions.ts
|
|
79
80
|
const toResolverOptions = ({ batchIndex, ctx, input, path, signal }) => ({
|
|
@@ -85,8 +86,12 @@ const toResolverOptions = ({ batchIndex, ctx, input, path, signal }) => ({
|
|
|
85
86
|
});
|
|
86
87
|
//#endregion
|
|
87
88
|
//#region src/services/createMockRouter.ts
|
|
88
|
-
const createMockRouter = ({ procedure: baseProcedure, router }, procedureTypes, registrations) => router(Object.fromEntries(procedureTypes.entries().map(([path, type]) => {
|
|
89
|
-
const
|
|
89
|
+
const createMockRouter = ({ procedure: baseProcedure, router }, procedureTypes, registrations, inputRouter) => router(Object.fromEntries(procedureTypes.entries().map(([path, type]) => {
|
|
90
|
+
const parseFns = (inputRouter?._def.procedures[path]?._def.inputs ?? []).map((parser) => getParseFn(parser));
|
|
91
|
+
const procedure = baseProcedure.input(async (value) => {
|
|
92
|
+
for (const parseFn of parseFns) await parseFn(value);
|
|
93
|
+
return value;
|
|
94
|
+
});
|
|
90
95
|
const createNotFoundError = () => new TRPCError({
|
|
91
96
|
code: "NOT_FOUND",
|
|
92
97
|
message: `No "${type}"-procedure on path "${path}"`
|
|
@@ -95,8 +100,8 @@ const createMockRouter = ({ procedure: baseProcedure, router }, procedureTypes,
|
|
|
95
100
|
case "mutation":
|
|
96
101
|
case "query": return [path, procedure[type]((options) => {
|
|
97
102
|
const registration = registrations.get(path);
|
|
98
|
-
if (registration?.type
|
|
99
|
-
|
|
103
|
+
if (registration?.type === type) return registration.resolver(toResolverOptions(options));
|
|
104
|
+
else throw createNotFoundError();
|
|
100
105
|
})];
|
|
101
106
|
case "subscription": return [path, procedure.subscription(async function* (options) {
|
|
102
107
|
const registration = registrations.get(path);
|
|
@@ -111,7 +116,7 @@ const createMockRouter = ({ procedure: baseProcedure, router }, procedureTypes,
|
|
|
111
116
|
const getProcedurePaths = (url, endpointPathname) => decodeURIComponent(new URL(url).pathname.slice(endpointPathname.length + 1)).split(",");
|
|
112
117
|
//#endregion
|
|
113
118
|
//#region src/createTRPCMsw.ts
|
|
114
|
-
const createTRPCMsw = ({ allowMethodOverride, createContext, endpoint, onUnhandledProcedure = "Error", t, webSocketUrl }) => {
|
|
119
|
+
const createTRPCMsw = ({ allowMethodOverride, createContext, endpoint, onUnhandledProcedure = "Error", router, t, webSocketUrl }) => {
|
|
115
120
|
const registrations = /* @__PURE__ */ new Map();
|
|
116
121
|
const procedureTypes = /* @__PURE__ */ new Map();
|
|
117
122
|
const endpointPathname = new URL(endpoint, "http://localhost").pathname;
|
|
@@ -122,14 +127,14 @@ const createTRPCMsw = ({ allowMethodOverride, createContext, endpoint, onUnhandl
|
|
|
122
127
|
createContext,
|
|
123
128
|
endpoint: endpointPathname,
|
|
124
129
|
req: request,
|
|
125
|
-
router: createMockRouter(t, procedureTypes, registrations)
|
|
130
|
+
router: createMockRouter(t, procedureTypes, registrations, router)
|
|
126
131
|
});
|
|
127
132
|
})];
|
|
128
133
|
const createWebSocketServer = () => {
|
|
129
134
|
const webSocketServer = new WebSocketServerAdapter();
|
|
130
135
|
applyWSSHandler({
|
|
131
136
|
createContext,
|
|
132
|
-
router: createMockRouter(t, new Map(procedureTypes), registrations),
|
|
137
|
+
router: createMockRouter(t, new Map(procedureTypes), registrations, router),
|
|
133
138
|
wss: webSocketServer
|
|
134
139
|
});
|
|
135
140
|
return webSocketServer;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trpc-msw",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.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,11 +49,11 @@
|
|
|
49
49
|
"typecheck": "tsc"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@esposter/shared": "
|
|
52
|
+
"@esposter/shared": "4.0.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@arethetypeswrong/core": "^0.18.5",
|
|
56
|
-
"@esposter/configuration": "
|
|
56
|
+
"@esposter/configuration": "4.0.0",
|
|
57
57
|
"@trpc/client": "^11.19.0",
|
|
58
58
|
"@types/node": "^26.6.4",
|
|
59
59
|
"publint": "^0.3.25",
|
|
@@ -65,10 +65,10 @@
|
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
67
|
"@trpc/server": "^11.19.0",
|
|
68
|
-
"msw": "^3.0.
|
|
68
|
+
"msw": "^3.0.2"
|
|
69
69
|
},
|
|
70
70
|
"inlinedDependencies": {
|
|
71
71
|
"type-fest": "5.10.0"
|
|
72
72
|
},
|
|
73
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "7667bc8c9a2b4e9dcacd6b845ee8cc9951250ae7"
|
|
74
74
|
}
|