trpc-msw 4.1.2 → 4.2.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/package.json +5 -5
- package/dist/index.d.ts +0 -129
- package/dist/index.js +0 -175
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trpc-msw",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.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,17 +49,17 @@
|
|
|
49
49
|
"typecheck": "tsc"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@esposter/shared": "4.
|
|
52
|
+
"@esposter/shared": "4.2.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@arethetypeswrong/core": "^0.18.5",
|
|
56
|
-
"@esposter/configuration": "4.
|
|
56
|
+
"@esposter/configuration": "4.2.0",
|
|
57
57
|
"@standard-schema/spec": "^1.1.0",
|
|
58
58
|
"@trpc/client": "^11.19.0",
|
|
59
59
|
"@types/node": "^26.6.5",
|
|
60
60
|
"publint": "^0.3.25",
|
|
61
61
|
"superjson": "^2.2.6",
|
|
62
|
-
"trpc-nuxt-module": "4.
|
|
62
|
+
"trpc-nuxt-module": "4.2.0",
|
|
63
63
|
"tsdown": "^0.23.1",
|
|
64
64
|
"type-fest": "^5.10.0",
|
|
65
65
|
"vitest": "^5.0.4",
|
|
@@ -72,5 +72,5 @@
|
|
|
72
72
|
"inlinedDependencies": {
|
|
73
73
|
"type-fest": "5.10.0"
|
|
74
74
|
},
|
|
75
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "fef4971cf6a621a2c1850f32fcec19ee4ec0de99"
|
|
76
76
|
}
|
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 };
|