trpc-msw 4.3.1 → 4.4.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 +7 -18
- package/package.json +10 -10
- package/dist/index.d.ts +0 -129
- package/dist/index.js +0 -175
package/README.md
CHANGED
|
@@ -5,12 +5,14 @@
|
|
|
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 for [Mock Service Worker](https://mswjs.io)
|
|
8
|
+
tRPC for [Mock Service Worker](https://mswjs.io): register a resolver per procedure, typed off your router, and every call your client makes is answered at the network by tRPC's own handlers.
|
|
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
|
---
|
|
@@ -50,31 +52,18 @@ test("reads a post", async () => {
|
|
|
50
52
|
});
|
|
51
53
|
```
|
|
52
54
|
|
|
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.
|
|
55
|
+
The result is that batching, your transformer, your error formatter, `FormData` input and subscriptions over server-sent events or WebSockets all behave exactly as they do against the real server.
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
## <a name="key-exports">🧩 Key Exports</a>
|
|
58
58
|
|
|
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 `
|
|
62
|
+
| `TRPCMswOptions` | `t`, `endpoint`, and optionally `allowMethodOverride`, `createContext`, `onUnhandledProcedure`, `router` and `webSocketUrl` |
|
|
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
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
|
-
### Commands
|
|
68
|
-
|
|
69
|
-
Run from `packages/trpc-msw/`:
|
|
70
|
-
|
|
71
|
-
```bash
|
|
72
|
-
pnpm build # compile to dist/
|
|
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
|
-
```
|
|
77
|
-
|
|
78
67
|
## <a name="license">⚖️ License</a>
|
|
79
68
|
|
|
80
69
|
This project is licensed under the [Apache-2.0 license](https://github.com/Esposter/Esposter/blob/main/LICENSE).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trpc-msw",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.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.4.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@esposter/configuration": "4.
|
|
53
|
+
"@esposter/configuration": "4.4.0",
|
|
54
54
|
"@standard-schema/spec": "^1.1.0",
|
|
55
|
-
"@trpc/client": "^11.
|
|
56
|
-
"@types/node": "^26.6.
|
|
55
|
+
"@trpc/client": "^11.22.1",
|
|
56
|
+
"@types/node": "^26.6.6",
|
|
57
57
|
"superjson": "^2.2.6",
|
|
58
|
-
"trpc-nuxt-module": "4.
|
|
59
|
-
"tsdown": "
|
|
58
|
+
"trpc-nuxt-module": "4.4.0",
|
|
59
|
+
"tsdown": "0.23.2",
|
|
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.22.1",
|
|
65
65
|
"msw": "^3.0.3"
|
|
66
66
|
},
|
|
67
67
|
"inlinedDependencies": {
|
|
68
68
|
"type-fest": "5.10.0"
|
|
69
69
|
},
|
|
70
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "e1a416c4b91da69cc2aa259b50a4d656f204d272"
|
|
71
71
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from "node:events";
|
|
2
|
-
import { AnyTRPCProcedure, AnyTRPCRootTypes, AnyTRPCRouter, TRPCProcedureType, TRPCRootObject, TRPCRouterBuilder, TRPCRouterRecord, inferProcedureInput, inferProcedureOutput } from "@trpc/server";
|
|
3
|
-
import { FetchCreateContextFnOptions } from "@trpc/server/adapters/fetch";
|
|
4
|
-
import { CreateWSSContextFnOptions } from "@trpc/server/adapters/ws";
|
|
5
|
-
import { HttpHandler, WebSocketData, WebSocketHandler, WebSocketHandlerConnection } from "msw";
|
|
6
|
-
//#region src/constants.d.ts
|
|
7
|
-
export declare const WEBSOCKET_ROUTER_REPLACED_CLOSE_CODE = 4e3;
|
|
8
|
-
//#endregion
|
|
9
|
-
//#region src/models/ProcedureResolverOptions.d.ts
|
|
10
|
-
export interface ProcedureResolverOptions<TContext, TInput> {
|
|
11
|
-
batchIndex?: number;
|
|
12
|
-
ctx: TContext;
|
|
13
|
-
input: TInput;
|
|
14
|
-
path: string;
|
|
15
|
-
signal?: AbortSignal;
|
|
16
|
-
}
|
|
17
|
-
//#endregion
|
|
18
|
-
//#region ../../node_modules/.pnpm/type-fest@5.10.0/node_modules/type-fest/source/promisable.d.ts
|
|
19
|
-
/**
|
|
20
|
-
Create a type that represents either the value or the value wrapped in `PromiseLike`.
|
|
21
|
-
|
|
22
|
-
Use-cases:
|
|
23
|
-
- A function accepts a callback that may either return a value synchronously or may return a promised value.
|
|
24
|
-
- This type could be the return type of `Promise#then()`, `Promise#catch()`, and `Promise#finally()` callbacks.
|
|
25
|
-
|
|
26
|
-
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/31394) if you want to have this type as a built-in in TypeScript.
|
|
27
|
-
|
|
28
|
-
@example
|
|
29
|
-
```
|
|
30
|
-
import type {Promisable} from 'type-fest';
|
|
31
|
-
|
|
32
|
-
async function logger(getLogEntry: () => Promisable<string>): Promise<void> {
|
|
33
|
-
const entry = await getLogEntry();
|
|
34
|
-
console.log(entry);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
await logger(() => 'foo');
|
|
38
|
-
await logger(() => Promise.resolve('bar'));
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
@category Async
|
|
42
|
-
*/
|
|
43
|
-
type Promisable<T> = T | PromiseLike<T>;
|
|
44
|
-
//#endregion
|
|
45
|
-
//#region src/models/ProcedureResolver.d.ts
|
|
46
|
-
export type ProcedureResolver<TContext, TProcedure extends AnyTRPCProcedure> = (options: ProcedureResolverOptions<TContext, inferProcedureInput<TProcedure>>) => Promisable<inferProcedureOutput<TProcedure>>;
|
|
47
|
-
//#endregion
|
|
48
|
-
//#region src/models/TRPCMswRouterRecord.d.ts
|
|
49
|
-
export type TRPCMswRouterRecord<TContext, TRecord extends TRPCRouterRecord> = { readonly [TKey in keyof TRecord]: TRecord[TKey] extends AnyTRPCProcedure ? Readonly<Record<TRecord[TKey]["_def"]["type"], (resolver: ProcedureResolver<TContext, TRecord[TKey]>) => void>> : TRecord[TKey] extends TRPCRouterRecord ? TRPCMswRouterRecord<TContext, TRecord[TKey]> : never; };
|
|
50
|
-
//#endregion
|
|
51
|
-
//#region src/models/TRPCMsw.d.ts
|
|
52
|
-
export interface TRPCMsw<TRouter extends AnyTRPCRouter, TContext> {
|
|
53
|
-
handlers: (HttpHandler | WebSocketHandler)[];
|
|
54
|
-
reset: () => void;
|
|
55
|
-
trpc: TRPCMswRouterRecord<TContext, TRouter["_def"]["record"]>;
|
|
56
|
-
}
|
|
57
|
-
//#endregion
|
|
58
|
-
//#region src/models/CreateContextOptions.d.ts
|
|
59
|
-
export type CreateContextOptions = CreateWSSContextFnOptions | FetchCreateContextFnOptions;
|
|
60
|
-
//#endregion
|
|
61
|
-
//#region src/models/TRPCMswRoot.d.ts
|
|
62
|
-
export interface TRPCMswRoot<TContext extends object> {
|
|
63
|
-
procedure: TRPCRootObject<TContext, object, object>["procedure"];
|
|
64
|
-
router: TRPCRouterBuilder<AnyTRPCRootTypes>;
|
|
65
|
-
}
|
|
66
|
-
//#endregion
|
|
67
|
-
//#region src/models/UnhandledProcedureAction.d.ts
|
|
68
|
-
export declare enum UnhandledProcedureAction {
|
|
69
|
-
Bypass = "Bypass",
|
|
70
|
-
Error = "Error"
|
|
71
|
-
}
|
|
72
|
-
//#endregion
|
|
73
|
-
//#region src/models/TRPCMswOptions.d.ts
|
|
74
|
-
export interface TRPCMswOptions<TContext extends object> {
|
|
75
|
-
allowMethodOverride?: boolean;
|
|
76
|
-
createContext?: (options: CreateContextOptions) => Promisable<TContext>;
|
|
77
|
-
endpoint: string;
|
|
78
|
-
onUnhandledProcedure?: UnhandledProcedureAction;
|
|
79
|
-
router?: AnyTRPCRouter;
|
|
80
|
-
t: TRPCMswRoot<TContext>;
|
|
81
|
-
webSocketUrl?: string;
|
|
82
|
-
}
|
|
83
|
-
//#endregion
|
|
84
|
-
//#region src/createTRPCMsw.d.ts
|
|
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
|
-
//#endregion
|
|
87
|
-
//#region src/models/MswWebSocketAdapter.d.ts
|
|
88
|
-
type MswWebSocketClient = WebSocketHandlerConnection["client"];
|
|
89
|
-
export declare class MswWebSocketAdapter extends EventEmitter {
|
|
90
|
-
#private;
|
|
91
|
-
readonly CLOSED: number;
|
|
92
|
-
readonly CLOSING: number;
|
|
93
|
-
readonly CONNECTING: number;
|
|
94
|
-
readonly OPEN: number;
|
|
95
|
-
readyState: number;
|
|
96
|
-
constructor(client: MswWebSocketClient);
|
|
97
|
-
close(code?: number, reason?: string): void;
|
|
98
|
-
send(data: string): void;
|
|
99
|
-
terminate(): void;
|
|
100
|
-
}
|
|
101
|
-
//#endregion
|
|
102
|
-
//#region src/models/SubscriptionRegistration.d.ts
|
|
103
|
-
export interface SubscriptionRegistration {
|
|
104
|
-
resolver: (options: ProcedureResolverOptions<unknown, unknown>) => Promisable<AsyncIterable<unknown>>;
|
|
105
|
-
type: "subscription";
|
|
106
|
-
}
|
|
107
|
-
//#endregion
|
|
108
|
-
//#region src/models/ProcedureRegistration.d.ts
|
|
109
|
-
export type ProcedureRegistration = SubscriptionRegistration | {
|
|
110
|
-
resolver: (options: ProcedureResolverOptions<unknown, unknown>) => unknown;
|
|
111
|
-
type: Exclude<TRPCProcedureType, "subscription">;
|
|
112
|
-
};
|
|
113
|
-
//#endregion
|
|
114
|
-
//#region src/services/checkIsProcedureType.d.ts
|
|
115
|
-
export declare const checkIsProcedureType: (value: unknown) => value is TRPCProcedureType;
|
|
116
|
-
//#endregion
|
|
117
|
-
//#region src/services/createMockRouter.d.ts
|
|
118
|
-
export declare const createMockRouter: <TContext extends object>({ procedure: baseProcedure, router }: TRPCMswRoot<TContext>, procedureTypes: ReadonlyMap<string, TRPCProcedureType>, registrations: ReadonlyMap<string, ProcedureRegistration>, inputRouter?: AnyTRPCRouter) => AnyTRPCRouter;
|
|
119
|
-
//#endregion
|
|
120
|
-
//#region src/services/getProcedurePaths.d.ts
|
|
121
|
-
export declare const getProcedurePaths: (url: string, endpointPathname: string) => string[];
|
|
122
|
-
//#endregion
|
|
123
|
-
//#region src/services/toBuffer.d.ts
|
|
124
|
-
export declare const toBuffer: (data: WebSocketData) => Buffer;
|
|
125
|
-
//#endregion
|
|
126
|
-
//#region src/services/toResolverOptions.d.ts
|
|
127
|
-
export declare const toResolverOptions: ({ batchIndex, ctx, input, path, signal }: ProcedureResolverOptions<unknown, unknown>) => ProcedureResolverOptions<unknown, unknown>;
|
|
128
|
-
//#endregion
|
|
129
|
-
export {};
|
package/dist/index.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from "node:events";
|
|
2
|
-
import { exhaustiveGuard } from "@esposter/shared";
|
|
3
|
-
import { TRPCError, createTRPCRecursiveProxy } from "@trpc/server";
|
|
4
|
-
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
|
|
5
|
-
import { applyWSSHandler } from "@trpc/server/adapters/ws";
|
|
6
|
-
import { http, ws } from "msw";
|
|
7
|
-
import { IncomingMessage } from "node:http";
|
|
8
|
-
import { Socket } from "node:net";
|
|
9
|
-
//#region src/constants.ts
|
|
10
|
-
const WEBSOCKET_ROUTER_REPLACED_CLOSE_CODE = 4e3;
|
|
11
|
-
//#endregion
|
|
12
|
-
//#region src/services/toBuffer.ts
|
|
13
|
-
const toBuffer = (data) => {
|
|
14
|
-
if (typeof data === "string") return Buffer.from(data);
|
|
15
|
-
else if (ArrayBuffer.isView(data)) return Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
16
|
-
else if (data instanceof Blob) throw new TypeError("A WebSocket message sent as a Blob cannot be read in order");
|
|
17
|
-
else return Buffer.from(data);
|
|
18
|
-
};
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region src/models/MswWebSocketAdapter.ts
|
|
21
|
-
var MswWebSocketAdapter = class extends EventEmitter {
|
|
22
|
-
CLOSED = WebSocket.CLOSED;
|
|
23
|
-
CLOSING = WebSocket.CLOSING;
|
|
24
|
-
CONNECTING = WebSocket.CONNECTING;
|
|
25
|
-
OPEN = WebSocket.OPEN;
|
|
26
|
-
readyState = WebSocket.OPEN;
|
|
27
|
-
#client;
|
|
28
|
-
constructor(client) {
|
|
29
|
-
super();
|
|
30
|
-
this.#client = client;
|
|
31
|
-
client.addEventListener("message", (event) => {
|
|
32
|
-
this.emit("message", toBuffer(event.data), false);
|
|
33
|
-
});
|
|
34
|
-
client.addEventListener("close", (event) => {
|
|
35
|
-
this.readyState = WebSocket.CLOSED;
|
|
36
|
-
this.emit("close", event.code, event.reason);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
close(code, reason) {
|
|
40
|
-
this.readyState = WebSocket.CLOSING;
|
|
41
|
-
this.#client.close(code, reason);
|
|
42
|
-
}
|
|
43
|
-
send(data) {
|
|
44
|
-
this.#client.send(data);
|
|
45
|
-
}
|
|
46
|
-
terminate() {
|
|
47
|
-
this.close();
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
//#endregion
|
|
51
|
-
//#region src/models/UnhandledProcedureAction.ts
|
|
52
|
-
let UnhandledProcedureAction = /* @__PURE__ */ function(UnhandledProcedureAction) {
|
|
53
|
-
UnhandledProcedureAction["Bypass"] = "Bypass";
|
|
54
|
-
UnhandledProcedureAction["Error"] = "Error";
|
|
55
|
-
return UnhandledProcedureAction;
|
|
56
|
-
}({});
|
|
57
|
-
//#endregion
|
|
58
|
-
//#region src/services/checkIsProcedureType.ts
|
|
59
|
-
const PROCEDURE_TYPES = /* @__PURE__ */ new Set([
|
|
60
|
-
"mutation",
|
|
61
|
-
"query",
|
|
62
|
-
"subscription"
|
|
63
|
-
]);
|
|
64
|
-
const checkIsProcedureType = (value) => PROCEDURE_TYPES.has(value);
|
|
65
|
-
//#endregion
|
|
66
|
-
//#region src/services/toResolverOptions.ts
|
|
67
|
-
const toResolverOptions = ({ batchIndex, ctx, input, path, signal }) => ({
|
|
68
|
-
batchIndex,
|
|
69
|
-
ctx,
|
|
70
|
-
input,
|
|
71
|
-
path,
|
|
72
|
-
signal
|
|
73
|
-
});
|
|
74
|
-
//#endregion
|
|
75
|
-
//#region src/services/createMockRouter.ts
|
|
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);
|
|
78
|
-
const createNotFoundError = () => new TRPCError({
|
|
79
|
-
code: "NOT_FOUND",
|
|
80
|
-
message: `No "${type}"-procedure on path "${path}"`
|
|
81
|
-
});
|
|
82
|
-
switch (type) {
|
|
83
|
-
case "mutation":
|
|
84
|
-
case "query": return [path, procedure[type]((options) => {
|
|
85
|
-
const registration = registrations.get(path);
|
|
86
|
-
if (registration?.type === type) return registration.resolver(toResolverOptions(options));
|
|
87
|
-
else throw createNotFoundError();
|
|
88
|
-
})];
|
|
89
|
-
case "subscription": return [path, procedure.subscription(async function* (options) {
|
|
90
|
-
const registration = registrations.get(path);
|
|
91
|
-
if (registration?.type !== type) throw createNotFoundError();
|
|
92
|
-
yield* await registration.resolver(toResolverOptions(options));
|
|
93
|
-
})];
|
|
94
|
-
default: return exhaustiveGuard(type);
|
|
95
|
-
}
|
|
96
|
-
})));
|
|
97
|
-
//#endregion
|
|
98
|
-
//#region src/services/getProcedurePaths.ts
|
|
99
|
-
const getProcedurePaths = (url, endpointPathname) => decodeURIComponent(new URL(url).pathname.slice(endpointPathname.length + 1)).split(",");
|
|
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
|
-
//#region src/createTRPCMsw.ts
|
|
114
|
-
const createTRPCMsw = ({ allowMethodOverride, createContext, endpoint, onUnhandledProcedure = "Error", router, t, webSocketUrl }) => {
|
|
115
|
-
const registrations = /* @__PURE__ */ new Map();
|
|
116
|
-
const procedureTypes = /* @__PURE__ */ new Map();
|
|
117
|
-
const endpointPathname = new URL(endpoint, "http://localhost").pathname;
|
|
118
|
-
let httpRouter;
|
|
119
|
-
const handlers = [http.all(`${endpoint}/*`, ({ request }) => {
|
|
120
|
-
if (onUnhandledProcedure === "Bypass" && !getProcedurePaths(request.url, endpointPathname).some((path) => registrations.has(path))) return void 0;
|
|
121
|
-
httpRouter ??= createMockRouter(t, procedureTypes, registrations, router);
|
|
122
|
-
return fetchRequestHandler({
|
|
123
|
-
allowMethodOverride,
|
|
124
|
-
createContext,
|
|
125
|
-
endpoint: endpointPathname,
|
|
126
|
-
req: request,
|
|
127
|
-
router: httpRouter
|
|
128
|
-
});
|
|
129
|
-
})];
|
|
130
|
-
const createWebSocketServer = () => {
|
|
131
|
-
const webSocketServer = new WebSocketServerAdapter();
|
|
132
|
-
applyWSSHandler({
|
|
133
|
-
createContext,
|
|
134
|
-
router: createMockRouter(t, new Map(procedureTypes), registrations, router),
|
|
135
|
-
wss: webSocketServer
|
|
136
|
-
});
|
|
137
|
-
return webSocketServer;
|
|
138
|
-
};
|
|
139
|
-
let webSocketServer;
|
|
140
|
-
if (webSocketUrl) handlers.push(ws.link(webSocketUrl).addEventListener("connection", (connection) => {
|
|
141
|
-
const { client } = connection;
|
|
142
|
-
const request = new IncomingMessage(new Socket());
|
|
143
|
-
request.url = `${client.url.pathname}${client.url.search}`;
|
|
144
|
-
webSocketServer ??= createWebSocketServer();
|
|
145
|
-
webSocketServer.addConnection(new MswWebSocketAdapter(client), request);
|
|
146
|
-
}));
|
|
147
|
-
const register = (path, registration) => {
|
|
148
|
-
registrations.set(path, registration);
|
|
149
|
-
if (procedureTypes.get(path) === registration.type) return;
|
|
150
|
-
procedureTypes.set(path, registration.type);
|
|
151
|
-
httpRouter = void 0;
|
|
152
|
-
if (!webSocketServer) return;
|
|
153
|
-
for (const client of webSocketServer.clients) client.close(WEBSOCKET_ROUTER_REPLACED_CLOSE_CODE);
|
|
154
|
-
webSocketServer = void 0;
|
|
155
|
-
};
|
|
156
|
-
return {
|
|
157
|
-
handlers,
|
|
158
|
-
reset: () => {
|
|
159
|
-
registrations.clear();
|
|
160
|
-
},
|
|
161
|
-
trpc: createTRPCRecursiveProxy(({ args: [resolver], path }) => {
|
|
162
|
-
const type = path.at(-1);
|
|
163
|
-
if (!checkIsProcedureType(type) || typeof resolver !== "function") throw new TypeError(`Expected a resolver registered with query, mutation or subscription at "${path.join(".")}"`);
|
|
164
|
-
register(path.slice(0, -1).join("."), type === "subscription" ? {
|
|
165
|
-
resolver,
|
|
166
|
-
type
|
|
167
|
-
} : {
|
|
168
|
-
resolver,
|
|
169
|
-
type
|
|
170
|
-
});
|
|
171
|
-
})
|
|
172
|
-
};
|
|
173
|
-
};
|
|
174
|
-
//#endregion
|
|
175
|
-
export { MswWebSocketAdapter, UnhandledProcedureAction, WEBSOCKET_ROUTER_REPLACED_CLOSE_CODE, checkIsProcedureType, createMockRouter, createTRPCMsw, getProcedurePaths, toBuffer, toResolverOptions };
|