-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_testHelpers.ts
93 lines (91 loc) · 2.64 KB
/
_testHelpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
import AbortController from 'abort-controller';
import fetch from 'node-fetch';
import {
createWSClient,
WebSocketClientOptions,
TRPCWebSocketClient,
} from '../../client/src/links/wsLink';
import ws from 'ws';
import { createTRPCClient, CreateTRPCClientOptions } from '../../client/src';
import { AnyRouter, CreateHttpHandlerOptions } from '../src';
import { createHttpServer } from '../src/adapters/standalone';
import { applyWSSHandler, WSSHandlerOptions } from '../src/ws';
(global as any).fetch = fetch;
(global as any).AbortController = AbortController;
export function routerToServerAndClient<TRouter extends AnyRouter>(
router: TRouter,
opts?: {
server?: Partial<CreateHttpHandlerOptions<TRouter>>;
wssServer?: Partial<WSSHandlerOptions<TRouter>>;
wsClient?: Partial<WebSocketClientOptions>;
client?:
| Partial<CreateTRPCClientOptions<TRouter>>
| ((opts: {
/**
* @deprecated use `httpUrl`
*/
url: string;
httpUrl: string;
wssUrl: string;
wsClient: TRPCWebSocketClient;
}) => Partial<CreateTRPCClientOptions<TRouter>>);
},
) {
// http
const httpServer = createHttpServer({
router,
createContext: () => ({}),
...(opts?.server ?? {}),
});
const { port: httpPort } = httpServer.listen(0);
const httpUrl = `http://localhost:${httpPort}`;
// wss
const wss = new ws.Server({ port: 0 });
const wssPort = (wss.address() as any).port as number;
const applyWSSHandlerOpts: WSSHandlerOptions<TRouter> = {
wss,
router,
createContext: () => ({}),
...(opts?.wssServer ?? {}),
};
const wssHandler = applyWSSHandler(applyWSSHandlerOpts);
const wssUrl = `ws://localhost:${wssPort}`;
// client
const wsClient = createWSClient({
url: wssUrl,
...opts?.wsClient,
});
const trpcClientOptions: CreateTRPCClientOptions<typeof router> = {
url: httpUrl,
...(opts?.client
? typeof opts.client === 'function'
? opts.client({ url: httpUrl, httpUrl, wssUrl, wsClient })
: opts.client
: {}),
};
const client = createTRPCClient<typeof router>(trpcClientOptions);
return {
wsClient,
client,
close: async () => {
wsClient.close();
await Promise.all([
new Promise((resolve) => httpServer.server.close(resolve)),
new Promise((resolve) => wss.close(resolve)),
]);
},
router,
trpcClientOptions,
/**
* @deprecated
*/
port: httpPort,
httpPort,
wssPort,
applyWSSHandlerOpts,
wssHandler,
wss,
};
}