@trpc/client 11.1.2 → 11.1.3-alpha-tmp-tsdown.23
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/dist/TRPCClientError.d.mts +33 -0
- package/dist/TRPCClientError.d.mts.map +1 -0
- package/dist/TRPCClientError.d.ts +29 -25
- package/dist/TRPCClientError.d.ts.map +1 -1
- package/dist/TRPCClientError.js +48 -72
- package/dist/TRPCClientError.mjs +48 -71
- package/dist/TRPCClientError.mjs.map +1 -0
- package/dist/_virtual/rolldown_runtime.js +39 -0
- package/dist/_virtual/rolldown_runtime.mjs +27 -0
- package/dist/createTRPCClient.d.mts +73 -0
- package/dist/createTRPCClient.d.mts.map +1 -0
- package/dist/createTRPCClient.d.ts +38 -34
- package/dist/createTRPCClient.d.ts.map +1 -1
- package/dist/createTRPCClient.js +34 -34
- package/dist/createTRPCClient.mjs +33 -31
- package/dist/createTRPCClient.mjs.map +1 -0
- package/dist/createTRPCUntypedClient.d.mts +9 -0
- package/dist/createTRPCUntypedClient.d.mts.map +1 -0
- package/dist/createTRPCUntypedClient.d.ts +8 -6
- package/dist/createTRPCUntypedClient.d.ts.map +1 -1
- package/dist/createTRPCUntypedClient.js +5 -6
- package/dist/createTRPCUntypedClient.mjs +6 -3
- package/dist/createTRPCUntypedClient.mjs.map +1 -0
- package/dist/getFetch.d.mts +10 -0
- package/dist/getFetch.d.mts.map +1 -0
- package/dist/getFetch.d.ts +9 -2
- package/dist/getFetch.d.ts.map +1 -1
- package/dist/getFetch.js +8 -13
- package/dist/getFetch.mjs +8 -11
- package/dist/getFetch.mjs.map +1 -0
- package/dist/index.d.mts +21 -0
- package/dist/index.d.ts +21 -16
- package/dist/index.js +41 -42
- package/dist/index.mjs +18 -15
- package/dist/internals/TRPCUntypedClient.d.mts +46 -0
- package/dist/internals/TRPCUntypedClient.d.mts.map +1 -0
- package/dist/internals/TRPCUntypedClient.d.ts +40 -33
- package/dist/internals/TRPCUntypedClient.d.ts.map +1 -1
- package/dist/internals/TRPCUntypedClient.js +94 -113
- package/dist/internals/TRPCUntypedClient.mjs +93 -110
- package/dist/internals/TRPCUntypedClient.mjs.map +1 -0
- package/dist/internals/dataLoader.js +101 -116
- package/dist/internals/dataLoader.mjs +101 -114
- package/dist/internals/dataLoader.mjs.map +1 -0
- package/dist/internals/inputWithTrackedEventId.js +9 -12
- package/dist/internals/inputWithTrackedEventId.mjs +9 -10
- package/dist/internals/inputWithTrackedEventId.mjs.map +1 -0
- package/dist/internals/signals.js +39 -40
- package/dist/internals/signals.mjs +39 -39
- package/dist/internals/signals.mjs.map +1 -0
- package/dist/internals/transformer.d.mts +47 -0
- package/dist/internals/transformer.d.mts.map +1 -0
- package/dist/internals/transformer.d.ts +28 -23
- package/dist/internals/transformer.d.ts.map +1 -1
- package/dist/internals/transformer.js +25 -26
- package/dist/internals/transformer.mjs +25 -24
- package/dist/internals/transformer.mjs.map +1 -0
- package/dist/internals/types.d.mts +85 -0
- package/dist/internals/types.d.mts.map +1 -0
- package/dist/internals/types.d.ts +45 -42
- package/dist/internals/types.d.ts.map +1 -1
- package/dist/links/HTTPBatchLinkOptions.d.mts +25 -0
- package/dist/links/HTTPBatchLinkOptions.d.mts.map +1 -0
- package/dist/links/HTTPBatchLinkOptions.d.ts +23 -18
- package/dist/links/HTTPBatchLinkOptions.d.ts.map +1 -1
- package/dist/links/httpBatchLink.d.mts +18 -0
- package/dist/links/httpBatchLink.d.mts.map +1 -0
- package/dist/links/httpBatchLink.d.ts +14 -4
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +90 -106
- package/dist/links/httpBatchLink.mjs +89 -103
- package/dist/links/httpBatchLink.mjs.map +1 -0
- package/dist/links/httpBatchStreamLink.d.mts +22 -0
- package/dist/links/httpBatchStreamLink.d.mts.map +1 -0
- package/dist/links/httpBatchStreamLink.d.ts +15 -5
- package/dist/links/httpBatchStreamLink.d.ts.map +1 -1
- package/dist/links/httpBatchStreamLink.js +120 -144
- package/dist/links/httpBatchStreamLink.mjs +119 -141
- package/dist/links/httpBatchStreamLink.mjs.map +1 -0
- package/dist/links/httpLink.d.mts +24 -0
- package/dist/links/httpLink.d.mts.map +1 -0
- package/dist/links/httpLink.d.ts +19 -12
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +74 -90
- package/dist/links/httpLink.mjs +73 -87
- package/dist/links/httpLink.mjs.map +1 -0
- package/dist/links/httpSubscriptionLink.d.mts +30 -0
- package/dist/links/httpSubscriptionLink.d.mts.map +1 -0
- package/dist/links/httpSubscriptionLink.d.ts +21 -17
- package/dist/links/httpSubscriptionLink.d.ts.map +1 -1
- package/dist/links/httpSubscriptionLink.js +134 -179
- package/dist/links/httpSubscriptionLink.mjs +133 -176
- package/dist/links/httpSubscriptionLink.mjs.map +1 -0
- package/dist/links/internals/contentTypes.d.mts +10 -0
- package/dist/links/internals/contentTypes.d.mts.map +1 -0
- package/dist/links/internals/contentTypes.d.ts +9 -3
- package/dist/links/internals/contentTypes.d.ts.map +1 -1
- package/dist/links/internals/contentTypes.js +6 -6
- package/dist/links/internals/contentTypes.mjs +6 -4
- package/dist/links/internals/contentTypes.mjs.map +1 -0
- package/dist/links/internals/createChain.js +23 -23
- package/dist/links/internals/createChain.mjs +22 -20
- package/dist/links/internals/createChain.mjs.map +1 -0
- package/dist/links/internals/httpUtils.d.mts +26 -0
- package/dist/links/internals/httpUtils.d.mts.map +1 -0
- package/dist/links/internals/httpUtils.d.ts +22 -64
- package/dist/links/internals/httpUtils.d.ts.map +1 -1
- package/dist/links/internals/httpUtils.js +93 -116
- package/dist/links/internals/httpUtils.mjs +94 -113
- package/dist/links/internals/httpUtils.mjs.map +1 -0
- package/dist/links/internals/subscriptions.d.mts +20 -0
- package/dist/links/internals/subscriptions.d.mts.map +1 -0
- package/dist/links/internals/subscriptions.d.ts +11 -8
- package/dist/links/internals/subscriptions.d.ts.map +1 -1
- package/dist/links/internals/urlWithConnectionParams.d.mts +27 -0
- package/dist/links/internals/urlWithConnectionParams.d.mts.map +1 -0
- package/dist/links/internals/urlWithConnectionParams.d.ts +22 -18
- package/dist/links/internals/urlWithConnectionParams.d.ts.map +1 -1
- package/dist/links/internals/urlWithConnectionParams.js +8 -6
- package/dist/links/internals/urlWithConnectionParams.mjs +8 -4
- package/dist/links/internals/urlWithConnectionParams.mjs.map +1 -0
- package/dist/links/localLink.d.mts +22 -0
- package/dist/links/localLink.d.mts.map +1 -0
- package/dist/links/localLink.d.ts +22 -0
- package/dist/links/localLink.d.ts.map +1 -0
- package/dist/links/localLink.js +192 -0
- package/dist/links/localLink.mjs +193 -0
- package/dist/links/localLink.mjs.map +1 -0
- package/dist/links/loggerLink.d.mts +56 -0
- package/dist/links/loggerLink.d.mts.map +1 -0
- package/dist/links/loggerLink.d.ts +40 -36
- package/dist/links/loggerLink.d.ts.map +1 -1
- package/dist/links/loggerLink.js +113 -166
- package/dist/links/loggerLink.mjs +112 -163
- package/dist/links/loggerLink.mjs.map +1 -0
- package/dist/links/retryLink.d.mts +35 -0
- package/dist/links/retryLink.d.mts.map +1 -0
- package/dist/links/retryLink.d.ts +27 -25
- package/dist/links/retryLink.d.ts.map +1 -1
- package/dist/links/retryLink.js +60 -68
- package/dist/links/retryLink.mjs +59 -65
- package/dist/links/retryLink.mjs.map +1 -0
- package/dist/links/splitLink.d.mts +21 -0
- package/dist/links/splitLink.d.mts.map +1 -0
- package/dist/links/splitLink.d.ts +19 -12
- package/dist/links/splitLink.d.ts.map +1 -1
- package/dist/links/splitLink.js +20 -21
- package/dist/links/splitLink.mjs +19 -18
- package/dist/links/splitLink.mjs.map +1 -0
- package/dist/links/types.d.mts +71 -0
- package/dist/links/types.d.mts.map +1 -0
- package/dist/links/types.d.ts +37 -29
- package/dist/links/types.d.ts.map +1 -1
- package/dist/links/wsLink/createWsClient.d.mts +10 -0
- package/dist/links/wsLink/createWsClient.d.mts.map +1 -0
- package/dist/links/wsLink/createWsClient.d.ts +9 -5
- package/dist/links/wsLink/createWsClient.d.ts.map +1 -1
- package/dist/links/wsLink/createWsClient.js +5 -5
- package/dist/links/wsLink/createWsClient.mjs +5 -2
- package/dist/links/wsLink/createWsClient.mjs.map +1 -0
- package/dist/links/wsLink/wsClient/options.d.mts +69 -0
- package/dist/links/wsLink/wsClient/options.d.mts.map +1 -0
- package/dist/links/wsLink/wsClient/options.d.ts +54 -64
- package/dist/links/wsLink/wsClient/options.d.ts.map +1 -1
- package/dist/links/wsLink/wsClient/options.js +14 -12
- package/dist/links/wsLink/wsClient/options.mjs +14 -10
- package/dist/links/wsLink/wsClient/options.mjs.map +1 -0
- package/dist/links/wsLink/wsClient/requestManager.js +133 -135
- package/dist/links/wsLink/wsClient/requestManager.mjs +133 -132
- package/dist/links/wsLink/wsClient/requestManager.mjs.map +1 -0
- package/dist/links/wsLink/wsClient/utils.js +64 -81
- package/dist/links/wsLink/wsClient/utils.mjs +64 -78
- package/dist/links/wsLink/wsClient/utils.mjs.map +1 -0
- package/dist/links/wsLink/wsClient/wsClient.d.mts +104 -0
- package/dist/links/wsLink/wsClient/wsClient.d.mts.map +1 -0
- package/dist/links/wsLink/wsClient/wsClient.d.ts +99 -79
- package/dist/links/wsLink/wsClient/wsClient.d.ts.map +1 -1
- package/dist/links/wsLink/wsClient/wsClient.js +273 -322
- package/dist/links/wsLink/wsClient/wsClient.mjs +272 -319
- package/dist/links/wsLink/wsClient/wsClient.mjs.map +1 -0
- package/dist/links/wsLink/wsClient/wsConnection.js +155 -178
- package/dist/links/wsLink/wsClient/wsConnection.mjs +154 -175
- package/dist/links/wsLink/wsClient/wsConnection.mjs.map +1 -0
- package/dist/links/wsLink/wsLink.d.mts +15 -0
- package/dist/links/wsLink/wsLink.d.mts.map +1 -0
- package/dist/links/wsLink/wsLink.d.ts +13 -9
- package/dist/links/wsLink/wsLink.d.ts.map +1 -1
- package/dist/links/wsLink/wsLink.js +30 -31
- package/dist/links/wsLink/wsLink.mjs +29 -28
- package/dist/links/wsLink/wsLink.mjs.map +1 -0
- package/dist/links.js +11 -0
- package/dist/links.mjs +11 -0
- package/dist/node_modules/.pnpm/@oxc-project_runtime@0.71.0/node_modules/@oxc-project/runtime/src/helpers/usingCtx.js +68 -0
- package/dist/node_modules/.pnpm/@oxc-project_runtime@0.71.0/node_modules/@oxc-project/runtime/src/helpers/usingCtx.mjs +66 -0
- package/dist/node_modules/.pnpm/@oxc-project_runtime@0.71.0/node_modules/@oxc-project/runtime/src/helpers/usingCtx.mjs.map +1 -0
- package/dist/unstable-internals.d.mts +3 -0
- package/dist/unstable-internals.d.ts +3 -3
- package/dist/unstable-internals.js +2 -6
- package/dist/unstable-internals.mjs +3 -1
- package/links/httpBatchLink/package.json +1 -0
- package/links/httpLink/package.json +1 -0
- package/links/loggerLink/package.json +1 -0
- package/links/splitLink/package.json +1 -0
- package/links/wsLink/wsLink/package.json +1 -0
- package/package.json +64 -29
- package/src/TRPCClientError.ts +4 -9
- package/src/internals/dataLoader.ts +2 -6
- package/src/internals/signals.ts +16 -0
- package/src/links/httpSubscriptionLink.ts +3 -18
- package/src/links/localLink.ts +277 -0
- package/src/links/wsLink/wsClient/requestManager.ts +9 -0
- package/src/links/wsLink/wsClient/wsClient.ts +1 -1
- package/src/links.ts +1 -0
- package/unstable-internals/package.json +1 -0
- package/dist/index.d.ts.map +0 -1
- package/dist/internals/dataLoader.d.ts +0 -13
- package/dist/internals/dataLoader.d.ts.map +0 -1
- package/dist/internals/inputWithTrackedEventId.d.ts +0 -2
- package/dist/internals/inputWithTrackedEventId.d.ts.map +0 -1
- package/dist/internals/signals.d.ts +0 -15
- package/dist/internals/signals.d.ts.map +0 -1
- package/dist/links/internals/createChain.d.ts +0 -8
- package/dist/links/internals/createChain.d.ts.map +0 -1
- package/dist/links/internals/dedupeLink.d.ts +0 -7
- package/dist/links/internals/dedupeLink.d.ts.map +0 -1
- package/dist/links/wsLink/wsClient/requestManager.d.ts +0 -102
- package/dist/links/wsLink/wsClient/requestManager.d.ts.map +0 -1
- package/dist/links/wsLink/wsClient/utils.d.ts +0 -38
- package/dist/links/wsLink/wsClient/utils.d.ts.map +0 -1
- package/dist/links/wsLink/wsClient/wsConnection.d.ts +0 -79
- package/dist/links/wsLink/wsClient/wsConnection.d.ts.map +0 -1
- package/dist/links.d.ts +0 -11
- package/dist/links.d.ts.map +0 -1
- package/dist/unstable-internals.d.ts.map +0 -1
|
@@ -1,185 +1,164 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { buildConnectionMessage, prepareUrl, withResolvers } from "./utils.mjs";
|
|
2
|
+
import { behaviorSubject } from "@trpc/server/observable";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
if (key in obj) {
|
|
6
|
-
Object.defineProperty(obj, key, {
|
|
7
|
-
value: value,
|
|
8
|
-
enumerable: true,
|
|
9
|
-
configurable: true,
|
|
10
|
-
writable: true
|
|
11
|
-
});
|
|
12
|
-
} else {
|
|
13
|
-
obj[key] = value;
|
|
14
|
-
}
|
|
15
|
-
return obj;
|
|
16
|
-
}
|
|
4
|
+
//#region src/links/wsLink/wsClient/wsConnection.ts
|
|
17
5
|
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
6
|
+
* Opens a WebSocket connection asynchronously and returns a promise
|
|
7
|
+
* that resolves when the connection is successfully established.
|
|
8
|
+
* The promise rejects if an error occurs during the connection attempt.
|
|
9
|
+
*/
|
|
10
|
+
function asyncWsOpen(ws) {
|
|
11
|
+
const { promise, resolve, reject } = withResolvers();
|
|
12
|
+
ws.addEventListener("open", () => {
|
|
13
|
+
ws.removeEventListener("error", reject);
|
|
14
|
+
resolve();
|
|
15
|
+
});
|
|
16
|
+
ws.addEventListener("error", reject);
|
|
17
|
+
return promise;
|
|
29
18
|
}
|
|
30
19
|
/**
|
|
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
|
-
});
|
|
20
|
+
* Sets up a periodic ping-pong mechanism to keep the WebSocket connection alive.
|
|
21
|
+
*
|
|
22
|
+
* - Sends "PING" messages at regular intervals defined by `intervalMs`.
|
|
23
|
+
* - If a "PONG" response is not received within the `pongTimeoutMs`, the WebSocket is closed.
|
|
24
|
+
* - The ping timer resets upon receiving any message to maintain activity.
|
|
25
|
+
* - Automatically starts the ping process when the WebSocket connection is opened.
|
|
26
|
+
* - Cleans up timers when the WebSocket is closed.
|
|
27
|
+
*
|
|
28
|
+
* @param ws - The WebSocket instance to manage.
|
|
29
|
+
* @param options - Configuration options for ping-pong intervals and timeouts.
|
|
30
|
+
*/
|
|
31
|
+
function setupPingInterval(ws, { intervalMs, pongTimeoutMs }) {
|
|
32
|
+
let pingTimeout;
|
|
33
|
+
let pongTimeout;
|
|
34
|
+
function start() {
|
|
35
|
+
pingTimeout = setTimeout(() => {
|
|
36
|
+
ws.send("PING");
|
|
37
|
+
pongTimeout = setTimeout(() => {
|
|
38
|
+
ws.close();
|
|
39
|
+
}, pongTimeoutMs);
|
|
40
|
+
}, intervalMs);
|
|
41
|
+
}
|
|
42
|
+
function reset() {
|
|
43
|
+
clearTimeout(pingTimeout);
|
|
44
|
+
start();
|
|
45
|
+
}
|
|
46
|
+
function pong() {
|
|
47
|
+
clearTimeout(pongTimeout);
|
|
48
|
+
reset();
|
|
49
|
+
}
|
|
50
|
+
ws.addEventListener("open", start);
|
|
51
|
+
ws.addEventListener("message", ({ data }) => {
|
|
52
|
+
clearTimeout(pingTimeout);
|
|
53
|
+
start();
|
|
54
|
+
if (data === "PONG") pong();
|
|
55
|
+
});
|
|
56
|
+
ws.addEventListener("close", () => {
|
|
57
|
+
clearTimeout(pingTimeout);
|
|
58
|
+
clearTimeout(pongTimeout);
|
|
59
|
+
});
|
|
72
60
|
}
|
|
73
61
|
/**
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
this.urlOptions = opts.urlOptions;
|
|
154
|
-
this.keepAliveOpts = opts.keepAlive;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
_define_property(WsConnection, "connectCount", 0);
|
|
62
|
+
* Manages a WebSocket connection with support for reconnection, keep-alive mechanisms,
|
|
63
|
+
* and observable state tracking.
|
|
64
|
+
*/
|
|
65
|
+
var WsConnection = class WsConnection {
|
|
66
|
+
static connectCount = 0;
|
|
67
|
+
id = ++WsConnection.connectCount;
|
|
68
|
+
WebSocketPonyfill;
|
|
69
|
+
urlOptions;
|
|
70
|
+
keepAliveOpts;
|
|
71
|
+
wsObservable = behaviorSubject(null);
|
|
72
|
+
constructor(opts) {
|
|
73
|
+
this.WebSocketPonyfill = opts.WebSocketPonyfill ?? WebSocket;
|
|
74
|
+
if (!this.WebSocketPonyfill) throw new Error("No WebSocket implementation found - you probably don't want to use this on the server, but if you do you need to pass a `WebSocket`-ponyfill");
|
|
75
|
+
this.urlOptions = opts.urlOptions;
|
|
76
|
+
this.keepAliveOpts = opts.keepAlive;
|
|
77
|
+
}
|
|
78
|
+
get ws() {
|
|
79
|
+
return this.wsObservable.get();
|
|
80
|
+
}
|
|
81
|
+
set ws(ws) {
|
|
82
|
+
this.wsObservable.next(ws);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Checks if the WebSocket connection is open and ready to communicate.
|
|
86
|
+
*/
|
|
87
|
+
isOpen() {
|
|
88
|
+
return !!this.ws && this.ws.readyState === this.WebSocketPonyfill.OPEN && !this.openPromise;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Checks if the WebSocket connection is closed or in the process of closing.
|
|
92
|
+
*/
|
|
93
|
+
isClosed() {
|
|
94
|
+
return !!this.ws && (this.ws.readyState === this.WebSocketPonyfill.CLOSING || this.ws.readyState === this.WebSocketPonyfill.CLOSED);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Manages the WebSocket opening process, ensuring that only one open operation
|
|
98
|
+
* occurs at a time. Tracks the ongoing operation with `openPromise` to avoid
|
|
99
|
+
* redundant calls and ensure proper synchronization.
|
|
100
|
+
*
|
|
101
|
+
* Sets up the keep-alive mechanism and necessary event listeners for the connection.
|
|
102
|
+
*
|
|
103
|
+
* @returns A promise that resolves once the WebSocket connection is successfully opened.
|
|
104
|
+
*/
|
|
105
|
+
openPromise = null;
|
|
106
|
+
async open() {
|
|
107
|
+
if (this.openPromise) return this.openPromise;
|
|
108
|
+
this.id = ++WsConnection.connectCount;
|
|
109
|
+
const wsPromise = prepareUrl(this.urlOptions).then((url) => new this.WebSocketPonyfill(url));
|
|
110
|
+
this.openPromise = wsPromise.then(async (ws) => {
|
|
111
|
+
this.ws = ws;
|
|
112
|
+
ws.addEventListener("message", function({ data }) {
|
|
113
|
+
if (data === "PING") this.send("PONG");
|
|
114
|
+
});
|
|
115
|
+
if (this.keepAliveOpts.enabled) setupPingInterval(ws, this.keepAliveOpts);
|
|
116
|
+
ws.addEventListener("close", () => {
|
|
117
|
+
if (this.ws === ws) this.ws = null;
|
|
118
|
+
});
|
|
119
|
+
await asyncWsOpen(ws);
|
|
120
|
+
if (this.urlOptions.connectionParams) ws.send(await buildConnectionMessage(this.urlOptions.connectionParams));
|
|
121
|
+
});
|
|
122
|
+
try {
|
|
123
|
+
await this.openPromise;
|
|
124
|
+
} finally {
|
|
125
|
+
this.openPromise = null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Closes the WebSocket connection gracefully.
|
|
130
|
+
* Waits for any ongoing open operation to complete before closing.
|
|
131
|
+
*/
|
|
132
|
+
async close() {
|
|
133
|
+
try {
|
|
134
|
+
await this.openPromise;
|
|
135
|
+
} finally {
|
|
136
|
+
this.ws?.close();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
158
140
|
/**
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
return {
|
|
179
|
-
id: connection.id,
|
|
180
|
-
state: 'connecting',
|
|
181
|
-
ws: connection.ws
|
|
182
|
-
};
|
|
141
|
+
* Provides a backward-compatible representation of the connection state.
|
|
142
|
+
*/
|
|
143
|
+
function backwardCompatibility(connection) {
|
|
144
|
+
if (connection.isOpen()) return {
|
|
145
|
+
id: connection.id,
|
|
146
|
+
state: "open",
|
|
147
|
+
ws: connection.ws
|
|
148
|
+
};
|
|
149
|
+
if (connection.isClosed()) return {
|
|
150
|
+
id: connection.id,
|
|
151
|
+
state: "closed",
|
|
152
|
+
ws: connection.ws
|
|
153
|
+
};
|
|
154
|
+
if (!connection.ws) return null;
|
|
155
|
+
return {
|
|
156
|
+
id: connection.id,
|
|
157
|
+
state: "connecting",
|
|
158
|
+
ws: connection.ws
|
|
159
|
+
};
|
|
183
160
|
}
|
|
184
161
|
|
|
162
|
+
//#endregion
|
|
185
163
|
export { WsConnection, backwardCompatibility };
|
|
164
|
+
//# sourceMappingURL=wsConnection.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wsConnection.mjs","names":["ws: WebSocket","pingTimeout: ReturnType<typeof setTimeout> | undefined","pongTimeout: ReturnType<typeof setTimeout> | undefined","opts: WebSocketConnectionOptions","connection: WsConnection"],"sources":["../../../../src/links/wsLink/wsClient/wsConnection.ts"],"sourcesContent":["import { behaviorSubject } from '@trpc/server/observable';\nimport type { UrlOptionsWithConnectionParams } from '../../internals/urlWithConnectionParams';\nimport { buildConnectionMessage, prepareUrl, withResolvers } from './utils';\n\n/**\n * Opens a WebSocket connection asynchronously and returns a promise\n * that resolves when the connection is successfully established.\n * The promise rejects if an error occurs during the connection attempt.\n */\nfunction asyncWsOpen(ws: WebSocket) {\n const { promise, resolve, reject } = withResolvers<void>();\n\n ws.addEventListener('open', () => {\n ws.removeEventListener('error', reject);\n resolve();\n });\n ws.addEventListener('error', reject);\n\n return promise;\n}\n\ninterface PingPongOptions {\n /**\n * The interval (in milliseconds) between \"PING\" messages.\n */\n intervalMs: number;\n\n /**\n * The timeout (in milliseconds) to wait for a \"PONG\" response before closing the connection.\n */\n pongTimeoutMs: number;\n}\n\n/**\n * Sets up a periodic ping-pong mechanism to keep the WebSocket connection alive.\n *\n * - Sends \"PING\" messages at regular intervals defined by `intervalMs`.\n * - If a \"PONG\" response is not received within the `pongTimeoutMs`, the WebSocket is closed.\n * - The ping timer resets upon receiving any message to maintain activity.\n * - Automatically starts the ping process when the WebSocket connection is opened.\n * - Cleans up timers when the WebSocket is closed.\n *\n * @param ws - The WebSocket instance to manage.\n * @param options - Configuration options for ping-pong intervals and timeouts.\n */\nfunction setupPingInterval(\n ws: WebSocket,\n { intervalMs, pongTimeoutMs }: PingPongOptions,\n) {\n let pingTimeout: ReturnType<typeof setTimeout> | undefined;\n let pongTimeout: ReturnType<typeof setTimeout> | undefined;\n\n function start() {\n pingTimeout = setTimeout(() => {\n ws.send('PING');\n pongTimeout = setTimeout(() => {\n ws.close();\n }, pongTimeoutMs);\n }, intervalMs);\n }\n\n function reset() {\n clearTimeout(pingTimeout);\n start();\n }\n\n function pong() {\n clearTimeout(pongTimeout);\n reset();\n }\n\n ws.addEventListener('open', start);\n ws.addEventListener('message', ({ data }) => {\n clearTimeout(pingTimeout);\n start();\n\n if (data === 'PONG') {\n pong();\n }\n });\n ws.addEventListener('close', () => {\n clearTimeout(pingTimeout);\n clearTimeout(pongTimeout);\n });\n}\n\nexport interface WebSocketConnectionOptions {\n WebSocketPonyfill?: typeof WebSocket;\n urlOptions: UrlOptionsWithConnectionParams;\n keepAlive: PingPongOptions & {\n enabled: boolean;\n };\n}\n\n/**\n * Manages a WebSocket connection with support for reconnection, keep-alive mechanisms,\n * and observable state tracking.\n */\nexport class WsConnection {\n static connectCount = 0;\n public id = ++WsConnection.connectCount;\n\n private readonly WebSocketPonyfill: typeof WebSocket;\n private readonly urlOptions: UrlOptionsWithConnectionParams;\n private readonly keepAliveOpts: WebSocketConnectionOptions['keepAlive'];\n public readonly wsObservable = behaviorSubject<WebSocket | null>(null);\n\n constructor(opts: WebSocketConnectionOptions) {\n this.WebSocketPonyfill = opts.WebSocketPonyfill ?? WebSocket;\n if (!this.WebSocketPonyfill) {\n throw new Error(\n \"No WebSocket implementation found - you probably don't want to use this on the server, but if you do you need to pass a `WebSocket`-ponyfill\",\n );\n }\n\n this.urlOptions = opts.urlOptions;\n this.keepAliveOpts = opts.keepAlive;\n }\n\n public get ws() {\n return this.wsObservable.get();\n }\n\n private set ws(ws) {\n this.wsObservable.next(ws);\n }\n\n /**\n * Checks if the WebSocket connection is open and ready to communicate.\n */\n public isOpen(): this is { ws: WebSocket } {\n return (\n !!this.ws &&\n this.ws.readyState === this.WebSocketPonyfill.OPEN &&\n !this.openPromise\n );\n }\n\n /**\n * Checks if the WebSocket connection is closed or in the process of closing.\n */\n public isClosed(): this is { ws: WebSocket } {\n return (\n !!this.ws &&\n (this.ws.readyState === this.WebSocketPonyfill.CLOSING ||\n this.ws.readyState === this.WebSocketPonyfill.CLOSED)\n );\n }\n\n /**\n * Manages the WebSocket opening process, ensuring that only one open operation\n * occurs at a time. Tracks the ongoing operation with `openPromise` to avoid\n * redundant calls and ensure proper synchronization.\n *\n * Sets up the keep-alive mechanism and necessary event listeners for the connection.\n *\n * @returns A promise that resolves once the WebSocket connection is successfully opened.\n */\n private openPromise: Promise<void> | null = null;\n public async open() {\n if (this.openPromise) return this.openPromise;\n\n this.id = ++WsConnection.connectCount;\n const wsPromise = prepareUrl(this.urlOptions).then(\n (url) => new this.WebSocketPonyfill(url),\n );\n this.openPromise = wsPromise.then(async (ws) => {\n this.ws = ws;\n\n // Setup ping listener\n ws.addEventListener('message', function ({ data }) {\n if (data === 'PING') {\n this.send('PONG');\n }\n });\n\n if (this.keepAliveOpts.enabled) {\n setupPingInterval(ws, this.keepAliveOpts);\n }\n\n ws.addEventListener('close', () => {\n if (this.ws === ws) {\n this.ws = null;\n }\n });\n\n await asyncWsOpen(ws);\n\n if (this.urlOptions.connectionParams) {\n ws.send(await buildConnectionMessage(this.urlOptions.connectionParams));\n }\n });\n\n try {\n await this.openPromise;\n } finally {\n this.openPromise = null;\n }\n }\n\n /**\n * Closes the WebSocket connection gracefully.\n * Waits for any ongoing open operation to complete before closing.\n */\n public async close() {\n try {\n await this.openPromise;\n } finally {\n this.ws?.close();\n }\n }\n}\n\n/**\n * Provides a backward-compatible representation of the connection state.\n */\nexport function backwardCompatibility(connection: WsConnection) {\n if (connection.isOpen()) {\n return {\n id: connection.id,\n state: 'open',\n ws: connection.ws,\n } as const;\n }\n\n if (connection.isClosed()) {\n return {\n id: connection.id,\n state: 'closed',\n ws: connection.ws,\n } as const;\n }\n\n if (!connection.ws) {\n return null;\n }\n\n return {\n id: connection.id,\n state: 'connecting',\n ws: connection.ws,\n } as const;\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { TRPCLink } from "../types.mjs";
|
|
2
|
+
import { TransformerOptions } from "../../internals/transformer.mjs";
|
|
3
|
+
import { WebSocketClientOptions } from "./wsClient/options.mjs";
|
|
4
|
+
import { TRPCWebSocketClient, createWSClient$1 as createWSClient } from "./createWsClient.mjs";
|
|
5
|
+
import { AnyRouter, inferClientTypes } from "@trpc/server/unstable-core-do-not-import";
|
|
6
|
+
|
|
7
|
+
//#region src/links/wsLink/wsLink.d.ts
|
|
8
|
+
type WebSocketLinkOptions<TRouter extends AnyRouter> = {
|
|
9
|
+
client: TRPCWebSocketClient;
|
|
10
|
+
} & TransformerOptions<inferClientTypes<TRouter>>;
|
|
11
|
+
declare function wsLink<TRouter extends AnyRouter>(opts: WebSocketLinkOptions<TRouter>): TRPCLink<TRouter>;
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
export { TRPCWebSocketClient, WebSocketClientOptions, WebSocketLinkOptions, createWSClient, wsLink };
|
|
15
|
+
//# sourceMappingURL=wsLink.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wsLink.d.mts","names":[],"sources":["../../../src/links/wsLink/wsLink.ts"],"sourcesContent":[],"mappings":";;;;;;;KAcY,qCAAqC;UACvC;AADV,CAAA,GAEI,kBAFQ,CAEW,gBAFS,CAEQ,OAFR,CAAA,CAAA;AAAA,iBAIhB,MAJgB,CAAA,gBAIO,SAJP,CAAA,CAAA,IAAA,EAKxB,oBALwB,CAKH,OALG,CAAA,CAAA,EAM7B,QAN6B,CAMpB,OANoB,CAAA"}
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { TRPCLink } from "../types.js";
|
|
2
|
+
import { TransformerOptions } from "../../internals/transformer.js";
|
|
3
|
+
import { WebSocketClientOptions } from "./wsClient/options.js";
|
|
4
|
+
import { TRPCWebSocketClient, createWSClient } from "./createWsClient.js";
|
|
5
|
+
import { AnyRouter, inferClientTypes } from "@trpc/server/unstable-core-do-not-import";
|
|
6
|
+
|
|
7
|
+
//#region src/links/wsLink/wsLink.d.ts
|
|
8
|
+
type WebSocketLinkOptions<TRouter extends AnyRouter> = {
|
|
9
|
+
client: TRPCWebSocketClient;
|
|
8
10
|
} & TransformerOptions<inferClientTypes<TRouter>>;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
declare function wsLink<TRouter extends AnyRouter>(opts: WebSocketLinkOptions<TRouter>): TRPCLink<TRouter>;
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
export { TRPCWebSocketClient, WebSocketClientOptions, WebSocketLinkOptions, createWSClient, wsLink };
|
|
11
15
|
//# sourceMappingURL=wsLink.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wsLink.d.ts","
|
|
1
|
+
{"version":3,"file":"wsLink.d.ts","names":[],"sources":["../../../src/links/wsLink/wsLink.ts"],"sourcesContent":[],"mappings":";;;;;;;KAcY,qCAAqC;UACvC;AADV,CAAA,GAEI,kBAFQ,CAEW,gBAFS,CAEQ,OAFR,CAAA,CAAA;AAAA,iBAIhB,MAJgB,CAAA,gBAIO,SAJP,CAAA,CAAA,IAAA,EAKxB,oBALwB,CAKH,OALG,CAAA,CAAA,EAM7B,QAN6B,CAMpB,OANoB,CAAA"}
|
|
@@ -1,35 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var createWsClient = require('./createWsClient.js');
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.js');
|
|
2
|
+
const require__Users_julius_dev_trpc_packages_client_src_internals_transformer = require('../../internals/transformer.js');
|
|
3
|
+
const require__Users_julius_dev_trpc_packages_client_src_links_wsLink_createWsClient = require('./createWsClient.js');
|
|
4
|
+
const __trpc_server_observable = require_rolldown_runtime.__toESM(require("@trpc/server/observable"));
|
|
6
5
|
|
|
6
|
+
//#region src/links/wsLink/wsLink.ts
|
|
7
7
|
function wsLink(opts) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
};
|
|
31
|
-
};
|
|
8
|
+
const { client } = opts;
|
|
9
|
+
const transformer = require__Users_julius_dev_trpc_packages_client_src_internals_transformer.getTransformer(opts.transformer);
|
|
10
|
+
return () => {
|
|
11
|
+
return ({ op }) => {
|
|
12
|
+
return (0, __trpc_server_observable.observable)((observer) => {
|
|
13
|
+
const connStateSubscription = op.type === "subscription" ? client.connectionState.subscribe({ next(result) {
|
|
14
|
+
observer.next({
|
|
15
|
+
result,
|
|
16
|
+
context: op.context
|
|
17
|
+
});
|
|
18
|
+
} }) : null;
|
|
19
|
+
const requestSubscription = client.request({
|
|
20
|
+
op,
|
|
21
|
+
transformer
|
|
22
|
+
}).subscribe(observer);
|
|
23
|
+
return () => {
|
|
24
|
+
requestSubscription.unsubscribe();
|
|
25
|
+
connStateSubscription?.unsubscribe();
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
};
|
|
32
30
|
}
|
|
33
31
|
|
|
34
|
-
|
|
35
|
-
exports.
|
|
32
|
+
//#endregion
|
|
33
|
+
exports.createWSClient = require__Users_julius_dev_trpc_packages_client_src_links_wsLink_createWsClient.createWSClient;
|
|
34
|
+
exports.wsLink = wsLink;
|
|
@@ -1,32 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { getTransformer } from "../../internals/transformer.mjs";
|
|
2
|
+
import { createWSClient } from "./createWsClient.mjs";
|
|
3
|
+
import { observable } from "@trpc/server/observable";
|
|
4
4
|
|
|
5
|
+
//#region src/links/wsLink/wsLink.ts
|
|
5
6
|
function wsLink(opts) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
};
|
|
29
|
-
};
|
|
7
|
+
const { client } = opts;
|
|
8
|
+
const transformer = getTransformer(opts.transformer);
|
|
9
|
+
return () => {
|
|
10
|
+
return ({ op }) => {
|
|
11
|
+
return observable((observer) => {
|
|
12
|
+
const connStateSubscription = op.type === "subscription" ? client.connectionState.subscribe({ next(result) {
|
|
13
|
+
observer.next({
|
|
14
|
+
result,
|
|
15
|
+
context: op.context
|
|
16
|
+
});
|
|
17
|
+
} }) : null;
|
|
18
|
+
const requestSubscription = client.request({
|
|
19
|
+
op,
|
|
20
|
+
transformer
|
|
21
|
+
}).subscribe(observer);
|
|
22
|
+
return () => {
|
|
23
|
+
requestSubscription.unsubscribe();
|
|
24
|
+
connStateSubscription?.unsubscribe();
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
};
|
|
30
29
|
}
|
|
31
30
|
|
|
32
|
-
|
|
31
|
+
//#endregion
|
|
32
|
+
export { createWSClient, wsLink };
|
|
33
|
+
//# sourceMappingURL=wsLink.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wsLink.mjs","names":["opts: WebSocketLinkOptions<TRouter>"],"sources":["../../../src/links/wsLink/wsLink.ts"],"sourcesContent":["import { observable } from '@trpc/server/observable';\nimport type {\n AnyRouter,\n inferClientTypes,\n} from '@trpc/server/unstable-core-do-not-import';\nimport type { TransformerOptions } from '../../unstable-internals';\nimport { getTransformer } from '../../unstable-internals';\nimport type { TRPCLink } from '../types';\nimport type {\n TRPCWebSocketClient,\n WebSocketClientOptions,\n} from './createWsClient';\nimport { createWSClient } from './createWsClient';\n\nexport type WebSocketLinkOptions<TRouter extends AnyRouter> = {\n client: TRPCWebSocketClient;\n} & TransformerOptions<inferClientTypes<TRouter>>;\n\nexport function wsLink<TRouter extends AnyRouter>(\n opts: WebSocketLinkOptions<TRouter>,\n): TRPCLink<TRouter> {\n const { client } = opts;\n const transformer = getTransformer(opts.transformer);\n return () => {\n return ({ op }) => {\n return observable((observer) => {\n const connStateSubscription =\n op.type === 'subscription'\n ? client.connectionState.subscribe({\n next(result) {\n observer.next({\n result,\n context: op.context,\n });\n },\n })\n : null;\n\n const requestSubscription = client\n .request({\n op,\n transformer,\n })\n .subscribe(observer);\n\n return () => {\n requestSubscription.unsubscribe();\n connStateSubscription?.unsubscribe();\n };\n });\n };\n };\n}\n\nexport { TRPCWebSocketClient, WebSocketClientOptions, createWSClient };\n"],"mappings":";;;;;AAkBA,SAAgB,OACdA,MACmB;CACnB,MAAM,EAAE,QAAQ,GAAG;CACnB,MAAM,cAAc,eAAe,KAAK,YAAY;AACpD,QAAO,MAAM;AACX,SAAO,CAAC,EAAE,IAAI,KAAK;AACjB,UAAO,WAAW,CAAC,aAAa;IAC9B,MAAM,wBACJ,GAAG,SAAS,iBACR,OAAO,gBAAgB,UAAU,EAC/B,KAAK,QAAQ;AACX,cAAS,KAAK;MACZ;MACA,SAAS,GAAG;KACb,EAAC;IACH,EACF,EAAC,GACF;IAEN,MAAM,sBAAsB,OACzB,QAAQ;KACP;KACA;IACD,EAAC,CACD,UAAU,SAAS;AAEtB,WAAO,MAAM;AACX,yBAAoB,aAAa;AACjC,4BAAuB,aAAa;IACrC;GACF,EAAC;EACH;CACF;AACF"}
|
package/dist/links.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const require__Users_julius_dev_trpc_packages_client_src_links_internals_contentTypes = require('./links/internals/contentTypes.js');
|
|
2
|
+
const require__Users_julius_dev_trpc_packages_client_src_links_httpBatchLink = require('./links/httpBatchLink.js');
|
|
3
|
+
const require__Users_julius_dev_trpc_packages_client_src_links_httpBatchStreamLink = require('./links/httpBatchStreamLink.js');
|
|
4
|
+
const require__Users_julius_dev_trpc_packages_client_src_links_httpLink = require('./links/httpLink.js');
|
|
5
|
+
const require__Users_julius_dev_trpc_packages_client_src_links_loggerLink = require('./links/loggerLink.js');
|
|
6
|
+
const require__Users_julius_dev_trpc_packages_client_src_links_splitLink = require('./links/splitLink.js');
|
|
7
|
+
const require__Users_julius_dev_trpc_packages_client_src_links_wsLink_createWsClient = require('./links/wsLink/createWsClient.js');
|
|
8
|
+
const require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsLink = require('./links/wsLink/wsLink.js');
|
|
9
|
+
const require__Users_julius_dev_trpc_packages_client_src_links_httpSubscriptionLink = require('./links/httpSubscriptionLink.js');
|
|
10
|
+
const require__Users_julius_dev_trpc_packages_client_src_links_retryLink = require('./links/retryLink.js');
|
|
11
|
+
const require__Users_julius_dev_trpc_packages_client_src_links_localLink = require('./links/localLink.js');
|
package/dist/links.mjs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { isFormData, isNonJsonSerializable, isOctetType } from "./links/internals/contentTypes.mjs";
|
|
2
|
+
import { httpBatchLink } from "./links/httpBatchLink.mjs";
|
|
3
|
+
import { httpBatchStreamLink, unstable_httpBatchStreamLink } from "./links/httpBatchStreamLink.mjs";
|
|
4
|
+
import { httpLink } from "./links/httpLink.mjs";
|
|
5
|
+
import { loggerLink } from "./links/loggerLink.mjs";
|
|
6
|
+
import { splitLink } from "./links/splitLink.mjs";
|
|
7
|
+
import { createWSClient } from "./links/wsLink/createWsClient.mjs";
|
|
8
|
+
import { wsLink } from "./links/wsLink/wsLink.mjs";
|
|
9
|
+
import { httpSubscriptionLink, unstable_httpSubscriptionLink } from "./links/httpSubscriptionLink.mjs";
|
|
10
|
+
import { retryLink } from "./links/retryLink.mjs";
|
|
11
|
+
import { experimental_localLink } from "./links/localLink.mjs";
|