@trpc/client 11.0.0-rc.433 → 11.0.0-rc.436
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/bundle-analysis.json +47 -32
- package/dist/links/httpSubscriptionLink.d.ts +3 -6
- package/dist/links/httpSubscriptionLink.d.ts.map +1 -1
- package/dist/links/httpSubscriptionLink.js +11 -6
- package/dist/links/httpSubscriptionLink.mjs +11 -6
- package/dist/links/internals/httpUtils.d.ts.map +1 -1
- package/dist/links/internals/httpUtils.js +5 -1
- package/dist/links/internals/httpUtils.mjs +5 -1
- package/dist/links/internals/urlWithConnectionParams.d.ts +23 -0
- package/dist/links/internals/urlWithConnectionParams.d.ts.map +1 -0
- package/dist/links/internals/urlWithConnectionParams.js +9 -0
- package/dist/links/internals/urlWithConnectionParams.mjs +7 -0
- package/dist/links/wsLink.d.ts +7 -6
- package/dist/links/wsLink.d.ts.map +1 -1
- package/dist/links/wsLink.js +33 -11
- package/dist/links/wsLink.mjs +33 -11
- package/package.json +4 -4
- package/src/links/httpSubscriptionLink.ts +22 -14
- package/src/links/internals/httpUtils.ts +7 -1
- package/src/links/internals/urlWithConnectionParams.ts +27 -0
- package/src/links/wsLink.ts +44 -17
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
|
-
"bundleSize":
|
|
3
|
-
"bundleOrigSize":
|
|
4
|
-
"bundleReduction": 25.
|
|
2
|
+
"bundleSize": 52206,
|
|
3
|
+
"bundleOrigSize": 69751,
|
|
4
|
+
"bundleReduction": 25.15,
|
|
5
5
|
"modules": [
|
|
6
6
|
{
|
|
7
7
|
"id": "/src/links/wsLink.ts",
|
|
8
|
-
"size":
|
|
9
|
-
"origSize":
|
|
8
|
+
"size": 13035,
|
|
9
|
+
"origSize": 14542,
|
|
10
10
|
"renderedExports": [
|
|
11
11
|
"createWSClient",
|
|
12
12
|
"wsLink"
|
|
13
13
|
],
|
|
14
14
|
"removedExports": [],
|
|
15
15
|
"dependents": [],
|
|
16
|
-
"percent":
|
|
17
|
-
"reduction":
|
|
16
|
+
"percent": 24.97,
|
|
17
|
+
"reduction": 10.36
|
|
18
18
|
},
|
|
19
19
|
{
|
|
20
20
|
"id": "/src/links/httpBatchStreamLink.ts",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
],
|
|
26
26
|
"removedExports": [],
|
|
27
27
|
"dependents": [],
|
|
28
|
-
"percent":
|
|
28
|
+
"percent": 11.95,
|
|
29
29
|
"reduction": 2.55
|
|
30
30
|
},
|
|
31
31
|
{
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
],
|
|
38
38
|
"removedExports": [],
|
|
39
39
|
"dependents": [],
|
|
40
|
-
"percent": 10.
|
|
40
|
+
"percent": 10.45,
|
|
41
41
|
"reduction": 18.48
|
|
42
42
|
},
|
|
43
43
|
{
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"/src/links/httpBatchLink.ts",
|
|
53
53
|
"/src/links/httpBatchStreamLink.ts"
|
|
54
54
|
],
|
|
55
|
-
"percent": 8.
|
|
55
|
+
"percent": 8.63,
|
|
56
56
|
"reduction": 6.34
|
|
57
57
|
},
|
|
58
58
|
{
|
|
@@ -64,13 +64,13 @@
|
|
|
64
64
|
],
|
|
65
65
|
"removedExports": [],
|
|
66
66
|
"dependents": [],
|
|
67
|
-
"percent": 7.
|
|
67
|
+
"percent": 7.72,
|
|
68
68
|
"reduction": 3.43
|
|
69
69
|
},
|
|
70
70
|
{
|
|
71
71
|
"id": "/src/links/internals/httpUtils.ts",
|
|
72
|
-
"size":
|
|
73
|
-
"origSize":
|
|
72
|
+
"size": 3543,
|
|
73
|
+
"origSize": 6008,
|
|
74
74
|
"renderedExports": [
|
|
75
75
|
"resolveHTTPLinkOptions",
|
|
76
76
|
"getInput",
|
|
@@ -82,25 +82,25 @@
|
|
|
82
82
|
],
|
|
83
83
|
"removedExports": [],
|
|
84
84
|
"dependents": [
|
|
85
|
-
"/src/links/httpBatchLink.ts",
|
|
86
85
|
"/src/links/httpLink.ts",
|
|
86
|
+
"/src/links/httpBatchLink.ts",
|
|
87
87
|
"/src/links/httpBatchStreamLink.ts",
|
|
88
88
|
"/src/links/httpSubscriptionLink.ts"
|
|
89
89
|
],
|
|
90
90
|
"percent": 6.79,
|
|
91
|
-
"reduction": 41.
|
|
91
|
+
"reduction": 41.03
|
|
92
92
|
},
|
|
93
93
|
{
|
|
94
94
|
"id": "/src/links/httpSubscriptionLink.ts",
|
|
95
|
-
"size":
|
|
96
|
-
"origSize":
|
|
95
|
+
"size": 3531,
|
|
96
|
+
"origSize": 3761,
|
|
97
97
|
"renderedExports": [
|
|
98
98
|
"unstable_httpSubscriptionLink"
|
|
99
99
|
],
|
|
100
100
|
"removedExports": [],
|
|
101
101
|
"dependents": [],
|
|
102
|
-
"percent": 6.
|
|
103
|
-
"reduction":
|
|
102
|
+
"percent": 6.76,
|
|
103
|
+
"reduction": 6.12
|
|
104
104
|
},
|
|
105
105
|
{
|
|
106
106
|
"id": "/src/links/httpLink.ts",
|
|
@@ -111,7 +111,7 @@
|
|
|
111
111
|
],
|
|
112
112
|
"removedExports": [],
|
|
113
113
|
"dependents": [],
|
|
114
|
-
"percent": 6.
|
|
114
|
+
"percent": 6.07,
|
|
115
115
|
"reduction": 14.36
|
|
116
116
|
},
|
|
117
117
|
{
|
|
@@ -126,7 +126,7 @@
|
|
|
126
126
|
"/src/createTRPCUntypedClient.ts",
|
|
127
127
|
"/src/createTRPCClient.ts"
|
|
128
128
|
],
|
|
129
|
-
"percent": 4.
|
|
129
|
+
"percent": 4.54,
|
|
130
130
|
"reduction": 44.91
|
|
131
131
|
},
|
|
132
132
|
{
|
|
@@ -139,15 +139,15 @@
|
|
|
139
139
|
"removedExports": [],
|
|
140
140
|
"dependents": [
|
|
141
141
|
"/src/index.ts",
|
|
142
|
-
"/src/links/httpBatchLink.ts",
|
|
143
142
|
"/src/links/httpLink.ts",
|
|
143
|
+
"/src/links/httpBatchLink.ts",
|
|
144
144
|
"/src/links/wsLink.ts",
|
|
145
145
|
"/src/links/httpBatchStreamLink.ts",
|
|
146
146
|
"/src/links/httpSubscriptionLink.ts",
|
|
147
147
|
"/src/internals/TRPCUntypedClient.ts",
|
|
148
148
|
"/src/links/internals/httpUtils.ts"
|
|
149
149
|
],
|
|
150
|
-
"percent": 3.
|
|
150
|
+
"percent": 3.57,
|
|
151
151
|
"reduction": 45.71
|
|
152
152
|
},
|
|
153
153
|
{
|
|
@@ -164,7 +164,7 @@
|
|
|
164
164
|
"dependents": [
|
|
165
165
|
"/src/index.ts"
|
|
166
166
|
],
|
|
167
|
-
"percent": 2.
|
|
167
|
+
"percent": 2.27,
|
|
168
168
|
"reduction": 73.19
|
|
169
169
|
},
|
|
170
170
|
{
|
|
@@ -179,7 +179,7 @@
|
|
|
179
179
|
"/src/links/splitLink.ts",
|
|
180
180
|
"/src/internals/TRPCUntypedClient.ts"
|
|
181
181
|
],
|
|
182
|
-
"percent": 1.
|
|
182
|
+
"percent": 1.32,
|
|
183
183
|
"reduction": 32.75
|
|
184
184
|
},
|
|
185
185
|
{
|
|
@@ -191,7 +191,7 @@
|
|
|
191
191
|
],
|
|
192
192
|
"removedExports": [],
|
|
193
193
|
"dependents": [],
|
|
194
|
-
"percent": 1.
|
|
194
|
+
"percent": 1.17,
|
|
195
195
|
"reduction": 44.95
|
|
196
196
|
},
|
|
197
197
|
{
|
|
@@ -205,7 +205,7 @@
|
|
|
205
205
|
"dependents": [
|
|
206
206
|
"/src/unstable-internals.ts"
|
|
207
207
|
],
|
|
208
|
-
"percent": 1.
|
|
208
|
+
"percent": 1.08,
|
|
209
209
|
"reduction": 66.75
|
|
210
210
|
},
|
|
211
211
|
{
|
|
@@ -220,7 +220,7 @@
|
|
|
220
220
|
"/src/index.ts",
|
|
221
221
|
"/src/links/internals/httpUtils.ts"
|
|
222
222
|
],
|
|
223
|
-
"percent": 0.
|
|
223
|
+
"percent": 0.82,
|
|
224
224
|
"reduction": 33.54
|
|
225
225
|
},
|
|
226
226
|
{
|
|
@@ -234,7 +234,7 @@
|
|
|
234
234
|
"dependents": [
|
|
235
235
|
"/src/links/internals/httpUtils.ts"
|
|
236
236
|
],
|
|
237
|
-
"percent": 0.
|
|
237
|
+
"percent": 0.76,
|
|
238
238
|
"reduction": 30.4
|
|
239
239
|
},
|
|
240
240
|
{
|
|
@@ -248,9 +248,24 @@
|
|
|
248
248
|
],
|
|
249
249
|
"removedExports": [],
|
|
250
250
|
"dependents": [],
|
|
251
|
-
"percent": 0.
|
|
251
|
+
"percent": 0.63,
|
|
252
252
|
"reduction": 15.17
|
|
253
253
|
},
|
|
254
|
+
{
|
|
255
|
+
"id": "/src/links/internals/urlWithConnectionParams.ts",
|
|
256
|
+
"size": 158,
|
|
257
|
+
"origSize": 857,
|
|
258
|
+
"renderedExports": [
|
|
259
|
+
"resultOf"
|
|
260
|
+
],
|
|
261
|
+
"removedExports": [],
|
|
262
|
+
"dependents": [
|
|
263
|
+
"/src/links/wsLink.ts",
|
|
264
|
+
"/src/links/httpSubscriptionLink.ts"
|
|
265
|
+
],
|
|
266
|
+
"percent": 0.3,
|
|
267
|
+
"reduction": 81.56
|
|
268
|
+
},
|
|
254
269
|
{
|
|
255
270
|
"id": "/src/createTRPCUntypedClient.ts",
|
|
256
271
|
"size": 100,
|
|
@@ -262,7 +277,7 @@
|
|
|
262
277
|
"dependents": [
|
|
263
278
|
"/src/index.ts"
|
|
264
279
|
],
|
|
265
|
-
"percent": 0.
|
|
280
|
+
"percent": 0.19,
|
|
266
281
|
"reduction": 82.58
|
|
267
282
|
},
|
|
268
283
|
{
|
|
@@ -290,5 +305,5 @@
|
|
|
290
305
|
"reduction": 100
|
|
291
306
|
}
|
|
292
307
|
],
|
|
293
|
-
"moduleCount":
|
|
308
|
+
"moduleCount": 21
|
|
294
309
|
}
|
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
import type { AnyClientTypes, inferClientTypes, InferrableClientTypes
|
|
1
|
+
import type { AnyClientTypes, inferClientTypes, InferrableClientTypes } from '@trpc/server/unstable-core-do-not-import';
|
|
2
2
|
import { type TransformerOptions } from '../unstable-internals';
|
|
3
|
+
import { type UrlOptionsWithConnectionParams } from './internals/urlWithConnectionParams';
|
|
3
4
|
import type { TRPCLink } from './types';
|
|
4
5
|
type HTTPSubscriptionLinkOptions<TRoot extends AnyClientTypes> = {
|
|
5
|
-
/**
|
|
6
|
-
* The URL to connect to (can be a function that returns a URL)
|
|
7
|
-
*/
|
|
8
|
-
url: string | (() => MaybePromise<string>);
|
|
9
6
|
/**
|
|
10
7
|
* EventSource options
|
|
11
8
|
*/
|
|
12
9
|
eventSourceOptions?: EventSourceInit;
|
|
13
|
-
} & TransformerOptions<TRoot
|
|
10
|
+
} & TransformerOptions<TRoot> & UrlOptionsWithConnectionParams;
|
|
14
11
|
/**
|
|
15
12
|
* @see https://trpc.io/docs/client/links/httpSubscriptionLink
|
|
16
13
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"httpSubscriptionLink.d.ts","sourceRoot":"","sources":["../../src/links/httpSubscriptionLink.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EACd,gBAAgB,EAChB,qBAAqB,
|
|
1
|
+
{"version":3,"file":"httpSubscriptionLink.d.ts","sourceRoot":"","sources":["../../src/links/httpSubscriptionLink.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EAEtB,MAAM,0CAA0C,CAAC;AAMlD,OAAO,EAAkB,KAAK,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAEhF,OAAO,EAEL,KAAK,8BAA8B,EACpC,MAAM,qCAAqC,CAAC;AAC7C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAiBxC,KAAK,2BAA2B,CAAC,KAAK,SAAS,cAAc,IAAI;IAC/D;;OAEG;IACH,kBAAkB,CAAC,EAAE,eAAe,CAAC;CACtC,GAAG,kBAAkB,CAAC,KAAK,CAAC,GAC3B,8BAA8B,CAAC;AAEjC;;GAEG;AACH,wBAAgB,6BAA6B,CAC3C,WAAW,SAAS,qBAAqB,EAEzC,IAAI,EAAE,2BAA2B,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAC,GAC/D,QAAQ,CAAC,WAAW,CAAC,CA8EvB"}
|
|
@@ -5,12 +5,17 @@ var unstableCoreDoNotImport = require('@trpc/server/unstable-core-do-not-import'
|
|
|
5
5
|
var TRPCClientError = require('../TRPCClientError.js');
|
|
6
6
|
var transformer = require('../internals/transformer.js');
|
|
7
7
|
var httpUtils = require('./internals/httpUtils.js');
|
|
8
|
+
var urlWithConnectionParams$1 = require('./internals/urlWithConnectionParams.js');
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
async function urlWithConnectionParams(opts) {
|
|
11
|
+
let url = await urlWithConnectionParams$1.resultOf(opts.url);
|
|
12
|
+
if (opts.connectionParams) {
|
|
13
|
+
const params = await urlWithConnectionParams$1.resultOf(opts.connectionParams);
|
|
14
|
+
const prefix = url.includes('?') ? '&' : '?';
|
|
15
|
+
url += prefix + 'connectionParams=' + encodeURIComponent(JSON.stringify(params));
|
|
16
|
+
}
|
|
17
|
+
return url;
|
|
18
|
+
}
|
|
14
19
|
/**
|
|
15
20
|
* @see https://trpc.io/docs/client/links/httpSubscriptionLink
|
|
16
21
|
*/ function unstable_httpSubscriptionLink(opts) {
|
|
@@ -27,7 +32,7 @@ var httpUtils = require('./internals/httpUtils.js');
|
|
|
27
32
|
unstableCoreDoNotImport.run(async ()=>{
|
|
28
33
|
const url = httpUtils.getUrl({
|
|
29
34
|
transformer: transformer$1,
|
|
30
|
-
url: await
|
|
35
|
+
url: await urlWithConnectionParams(opts),
|
|
31
36
|
input,
|
|
32
37
|
path,
|
|
33
38
|
type,
|
|
@@ -3,12 +3,17 @@ import { run, sseStreamConsumer } from '@trpc/server/unstable-core-do-not-import
|
|
|
3
3
|
import { TRPCClientError } from '../TRPCClientError.mjs';
|
|
4
4
|
import { getTransformer } from '../internals/transformer.mjs';
|
|
5
5
|
import { getUrl } from './internals/httpUtils.mjs';
|
|
6
|
+
import { resultOf } from './internals/urlWithConnectionParams.mjs';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
async function urlWithConnectionParams(opts) {
|
|
9
|
+
let url = await resultOf(opts.url);
|
|
10
|
+
if (opts.connectionParams) {
|
|
11
|
+
const params = await resultOf(opts.connectionParams);
|
|
12
|
+
const prefix = url.includes('?') ? '&' : '?';
|
|
13
|
+
url += prefix + 'connectionParams=' + encodeURIComponent(JSON.stringify(params));
|
|
14
|
+
}
|
|
15
|
+
return url;
|
|
16
|
+
}
|
|
12
17
|
/**
|
|
13
18
|
* @see https://trpc.io/docs/client/links/httpSubscriptionLink
|
|
14
19
|
*/ function unstable_httpSubscriptionLink(opts) {
|
|
@@ -25,7 +30,7 @@ import { getUrl } from './internals/httpUtils.mjs';
|
|
|
25
30
|
run(async ()=>{
|
|
26
31
|
const url = getUrl({
|
|
27
32
|
transformer,
|
|
28
|
-
url: await
|
|
33
|
+
url: await urlWithConnectionParams(opts),
|
|
29
34
|
input,
|
|
30
35
|
path,
|
|
31
36
|
type,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"httpUtils.d.ts","sourceRoot":"","sources":["../../../src/links/internals/httpUtils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,uBAAuB,EACvB,aAAa,EACb,gBAAgB,EAChB,YAAY,EACb,MAAM,0CAA0C,CAAC;AAGlD,OAAO,KAAK,EACV,oBAAoB,EACpB,4BAA4B,EAC5B,UAAU,EACV,gBAAgB,EAChB,aAAa,EACd,MAAM,uBAAuB,CAAC;AAE/B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAEnE,OAAO,KAA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AE,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;CACnD,KACE,gBAAgB,CAAC,UAAU,CAAC,CAAC;AAElC,eAAO,MAAM,iBAAiB,EAAE,SAO/B,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG,cAAc,GAC7C,sBAAsB,GAAG;IACvB,OAAO,EAAE,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;CACnD,CAAC;AAEJ,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,kBAAkB,EACxB,EAAE,CAAC,EAAE,4BAA4B,GAAG,IAAI,0BA4BzC;AAED,wBAAgB,WAAW,CACzB,IAAI,EAAE,kBAAkB,GACvB,gBAAgB,CAAC,UAAU,CAAC,CA8B9B"}
|
|
@@ -32,9 +32,13 @@ function getInput(opts) {
|
|
|
32
32
|
return 'input' in opts ? opts.transformer.input.serialize(opts.input) : arrayToDict(opts.inputs.map((_input)=>opts.transformer.input.serialize(_input)));
|
|
33
33
|
}
|
|
34
34
|
const getUrl = (opts)=>{
|
|
35
|
-
const
|
|
35
|
+
const parts = opts.url.split('?');
|
|
36
|
+
const base = parts[0].replace(/\/$/, ''); // Remove any trailing slashes
|
|
36
37
|
let url = base + '/' + opts.path;
|
|
37
38
|
const queryParts = [];
|
|
39
|
+
if (parts[1]) {
|
|
40
|
+
queryParts.push(parts[1]);
|
|
41
|
+
}
|
|
38
42
|
if ('inputs' in opts) {
|
|
39
43
|
queryParts.push('batch=1');
|
|
40
44
|
}
|
|
@@ -30,9 +30,13 @@ function getInput(opts) {
|
|
|
30
30
|
return 'input' in opts ? opts.transformer.input.serialize(opts.input) : arrayToDict(opts.inputs.map((_input)=>opts.transformer.input.serialize(_input)));
|
|
31
31
|
}
|
|
32
32
|
const getUrl = (opts)=>{
|
|
33
|
-
const
|
|
33
|
+
const parts = opts.url.split('?');
|
|
34
|
+
const base = parts[0].replace(/\/$/, ''); // Remove any trailing slashes
|
|
34
35
|
let url = base + '/' + opts.path;
|
|
35
36
|
const queryParts = [];
|
|
37
|
+
if (parts[1]) {
|
|
38
|
+
queryParts.push(parts[1]);
|
|
39
|
+
}
|
|
36
40
|
if ('inputs' in opts) {
|
|
37
41
|
queryParts.push('batch=1');
|
|
38
42
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type TRPCRequestInfo } from '@trpc/server/http';
|
|
2
|
+
/**
|
|
3
|
+
* Get the result of a value or function that returns a value
|
|
4
|
+
*/
|
|
5
|
+
export declare const resultOf: <T>(value: T | (() => T)) => T;
|
|
6
|
+
/**
|
|
7
|
+
* A value that can be wrapped in callback
|
|
8
|
+
*/
|
|
9
|
+
type CallbackOrValue<T> = T | (() => T | Promise<T>);
|
|
10
|
+
export interface UrlOptionsWithConnectionParams {
|
|
11
|
+
/**
|
|
12
|
+
* The URL to connect to (can be a function that returns a URL)
|
|
13
|
+
*/
|
|
14
|
+
url: CallbackOrValue<string>;
|
|
15
|
+
/**
|
|
16
|
+
* Connection params that are available in `createContext()`
|
|
17
|
+
* - For `wsLink`/`wsClient`, these are sent as the first message
|
|
18
|
+
* - For `httpSubscriptionLink`, these are serialized as part of the URL under the `connectionParams` query
|
|
19
|
+
*/
|
|
20
|
+
connectionParams?: CallbackOrValue<TRPCRequestInfo['connectionParams']>;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
23
|
+
//# sourceMappingURL=urlWithConnectionParams.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"urlWithConnectionParams.d.ts","sourceRoot":"","sources":["../../../src/links/internals/urlWithConnectionParams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEzD;;GAEG;AACH,eAAO,MAAM,QAAQ,GAAI,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,KAAG,CAElD,CAAC;AAEF;;GAEG;AACH,KAAK,eAAe,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;AAErD,MAAM,WAAW,8BAA8B;IAC7C;;OAEG;IACH,GAAG,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;IAE7B;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,eAAe,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC,CAAC;CACzE"}
|
package/dist/links/wsLink.d.ts
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import type { Observer, UnsubscribeFn } from '@trpc/server/observable';
|
|
2
|
-
import type { AnyRouter, inferClientTypes, inferRouterError,
|
|
2
|
+
import type { AnyRouter, inferClientTypes, inferRouterError, TRPCResponseMessage } from '@trpc/server/unstable-core-do-not-import';
|
|
3
3
|
import { TRPCClientError } from '../TRPCClientError';
|
|
4
4
|
import type { TransformerOptions } from '../unstable-internals';
|
|
5
|
+
import { type UrlOptionsWithConnectionParams } from './internals/urlWithConnectionParams';
|
|
5
6
|
import type { Operation, TRPCLink } from './types';
|
|
6
7
|
type WSCallbackResult<TRouter extends AnyRouter, TOutput> = TRPCResponseMessage<TOutput, inferRouterError<TRouter>>;
|
|
7
8
|
type WSCallbackObserver<TRouter extends AnyRouter, TOutput> = Observer<WSCallbackResult<TRouter, TOutput>, TRPCClientError<TRouter>>;
|
|
8
9
|
declare const exponentialBackoff: (attemptIndex: number) => number;
|
|
9
|
-
export interface WebSocketClientOptions {
|
|
10
|
-
/**
|
|
11
|
-
* The URL to connect to (can be a function that returns a URL)
|
|
12
|
-
*/
|
|
13
|
-
url: string | (() => MaybePromise<string>);
|
|
10
|
+
export interface WebSocketClientOptions extends UrlOptionsWithConnectionParams {
|
|
14
11
|
/**
|
|
15
12
|
* Ponyfill which WebSocket implementation to use
|
|
16
13
|
*/
|
|
@@ -61,6 +58,10 @@ export declare function createWSClient(opts: WebSocketClientOptions): {
|
|
|
61
58
|
state: "connecting";
|
|
62
59
|
ws?: WebSocket;
|
|
63
60
|
})) | null;
|
|
61
|
+
/**
|
|
62
|
+
* Reconnect to the WebSocket server
|
|
63
|
+
*/
|
|
64
|
+
reconnect: () => void;
|
|
64
65
|
};
|
|
65
66
|
export type TRPCWebSocketClient = ReturnType<typeof createWSClient>;
|
|
66
67
|
export type WebSocketLinkOptions<TRouter extends AnyRouter> = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wsLink.d.ts","sourceRoot":"","sources":["../../src/links/wsLink.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"wsLink.d.ts","sourceRoot":"","sources":["../../src/links/wsLink.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAGvE,OAAO,KAAK,EACV,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAMhB,mBAAmB,EACpB,MAAM,0CAA0C,CAAC;AAElD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAEhE,OAAO,EAEL,KAAK,8BAA8B,EACpC,MAAM,qCAAqC,CAAC;AAC7C,OAAO,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAInD,KAAK,gBAAgB,CAAC,OAAO,SAAS,SAAS,EAAE,OAAO,IAAI,mBAAmB,CAC7E,OAAO,EACP,gBAAgB,CAAC,OAAO,CAAC,CAC1B,CAAC;AAEF,KAAK,kBAAkB,CAAC,OAAO,SAAS,SAAS,EAAE,OAAO,IAAI,QAAQ,CACpE,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,EAClC,eAAe,CAAC,OAAO,CAAC,CACzB,CAAC;AAEF,QAAA,MAAM,kBAAkB,iBAAkB,MAAM,WACoB,CAAC;AAErE,MAAM,WAAW,sBAAuB,SAAQ,8BAA8B;IAC5E;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,SAAS,CAAC;IAC7B;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,kBAAkB,CAAC;IACzC;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC;IACpB;;OAEG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;IAC9C;;OAEG;IACH,IAAI,CAAC,EAAE;QACL;;;WAGG;QACH,OAAO,EAAE,OAAO,CAAC;QACjB;;;WAGG;QACH,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;CACH;AAOD,wBAAgB,cAAc,CAAC,IAAI,EAAE,sBAAsB;;kBAuTpC,SAAS,wDAA0B,aAAa;;YAxQ/D,MAAM;;eAGC,MAAM;YACT,SAAS;;eAGN,QAAQ;YACX,SAAS;;eAGN,YAAY;aACd,SAAS;;IA2TlB;;OAEG;;EAGN;AACD,MAAM,MAAM,mBAAmB,GAAG,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC;AAEpE,MAAM,MAAM,oBAAoB,CAAC,OAAO,SAAS,SAAS,IAAI;IAC5D,MAAM,EAAE,mBAAmB,CAAC;CAC7B,GAAG,kBAAkB,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC;AASlD;;GAEG;AACH,wBAAgB,MAAM,CAAC,OAAO,SAAS,SAAS,EAC9C,IAAI,EAAE,oBAAoB,CAAC,OAAO,CAAC,GAClC,QAAQ,CAAC,OAAO,CAAC,CA8CnB"}
|
package/dist/links/wsLink.js
CHANGED
|
@@ -4,6 +4,7 @@ var observable = require('@trpc/server/observable');
|
|
|
4
4
|
var unstableCoreDoNotImport = require('@trpc/server/unstable-core-do-not-import');
|
|
5
5
|
var TRPCClientError = require('../TRPCClientError.js');
|
|
6
6
|
var transformer = require('../internals/transformer.js');
|
|
7
|
+
var urlWithConnectionParams = require('./internals/urlWithConnectionParams.js');
|
|
7
8
|
|
|
8
9
|
const run = (fn)=>fn();
|
|
9
10
|
const exponentialBackoff = (attemptIndex)=>attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
|
|
@@ -12,7 +13,7 @@ const lazyDefaults = {
|
|
|
12
13
|
closeMs: 0
|
|
13
14
|
};
|
|
14
15
|
function createWSClient(opts) {
|
|
15
|
-
const {
|
|
16
|
+
const { WebSocket: WebSocketImpl = WebSocket , retryDelayMs: retryDelayFn = exponentialBackoff , onOpen , onClose , } = opts;
|
|
16
17
|
const lazyOpts = {
|
|
17
18
|
...lazyDefaults,
|
|
18
19
|
...opts.lazy
|
|
@@ -128,19 +129,37 @@ function createWSClient(opts) {
|
|
|
128
129
|
}
|
|
129
130
|
};
|
|
130
131
|
run(async ()=>{
|
|
131
|
-
|
|
132
|
-
|
|
132
|
+
let url = await urlWithConnectionParams.resultOf(opts.url);
|
|
133
|
+
if (opts.connectionParams) {
|
|
134
|
+
// append `?connectionParams=1` when connection params are used
|
|
135
|
+
const prefix = url.includes('?') ? '&' : '?';
|
|
136
|
+
url += prefix + 'connectionParams=1';
|
|
137
|
+
}
|
|
138
|
+
const ws = new WebSocketImpl(url);
|
|
133
139
|
self.ws = ws;
|
|
134
140
|
clearTimeout(connectTimer);
|
|
135
141
|
connectTimer = undefined;
|
|
136
142
|
ws.addEventListener('open', ()=>{
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
143
|
+
run(async ()=>{
|
|
144
|
+
/* istanbul ignore next -- @preserve */ if (activeConnection?.ws !== ws) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (opts.connectionParams) {
|
|
148
|
+
const connectMsg = {
|
|
149
|
+
method: 'connectionParams',
|
|
150
|
+
data: await urlWithConnectionParams.resultOf(opts.connectionParams)
|
|
151
|
+
};
|
|
152
|
+
ws.send(JSON.stringify(connectMsg));
|
|
153
|
+
}
|
|
154
|
+
connectAttempt = 0;
|
|
155
|
+
self.state = 'open';
|
|
156
|
+
onOpen?.();
|
|
157
|
+
dispatch();
|
|
158
|
+
}).catch((cause)=>{
|
|
159
|
+
ws.close(// "Status codes in the range 3000-3999 are reserved for use by libraries, frameworks, and applications"
|
|
160
|
+
3000, cause);
|
|
161
|
+
onError();
|
|
162
|
+
});
|
|
144
163
|
});
|
|
145
164
|
ws.addEventListener('error', onError);
|
|
146
165
|
const handleIncomingRequest = (req)=>{
|
|
@@ -275,7 +294,10 @@ function createWSClient(opts) {
|
|
|
275
294
|
request,
|
|
276
295
|
get connection () {
|
|
277
296
|
return activeConnection;
|
|
278
|
-
}
|
|
297
|
+
},
|
|
298
|
+
/**
|
|
299
|
+
* Reconnect to the WebSocket server
|
|
300
|
+
*/ reconnect
|
|
279
301
|
};
|
|
280
302
|
}
|
|
281
303
|
class TRPCWebSocketClosedError extends Error {
|
package/dist/links/wsLink.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { observable } from '@trpc/server/observable';
|
|
|
2
2
|
import { transformResult } from '@trpc/server/unstable-core-do-not-import';
|
|
3
3
|
import { TRPCClientError } from '../TRPCClientError.mjs';
|
|
4
4
|
import { getTransformer } from '../internals/transformer.mjs';
|
|
5
|
+
import { resultOf } from './internals/urlWithConnectionParams.mjs';
|
|
5
6
|
|
|
6
7
|
const run = (fn)=>fn();
|
|
7
8
|
const exponentialBackoff = (attemptIndex)=>attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
|
|
@@ -10,7 +11,7 @@ const lazyDefaults = {
|
|
|
10
11
|
closeMs: 0
|
|
11
12
|
};
|
|
12
13
|
function createWSClient(opts) {
|
|
13
|
-
const {
|
|
14
|
+
const { WebSocket: WebSocketImpl = WebSocket , retryDelayMs: retryDelayFn = exponentialBackoff , onOpen , onClose , } = opts;
|
|
14
15
|
const lazyOpts = {
|
|
15
16
|
...lazyDefaults,
|
|
16
17
|
...opts.lazy
|
|
@@ -126,19 +127,37 @@ function createWSClient(opts) {
|
|
|
126
127
|
}
|
|
127
128
|
};
|
|
128
129
|
run(async ()=>{
|
|
129
|
-
|
|
130
|
-
|
|
130
|
+
let url = await resultOf(opts.url);
|
|
131
|
+
if (opts.connectionParams) {
|
|
132
|
+
// append `?connectionParams=1` when connection params are used
|
|
133
|
+
const prefix = url.includes('?') ? '&' : '?';
|
|
134
|
+
url += prefix + 'connectionParams=1';
|
|
135
|
+
}
|
|
136
|
+
const ws = new WebSocketImpl(url);
|
|
131
137
|
self.ws = ws;
|
|
132
138
|
clearTimeout(connectTimer);
|
|
133
139
|
connectTimer = undefined;
|
|
134
140
|
ws.addEventListener('open', ()=>{
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
141
|
+
run(async ()=>{
|
|
142
|
+
/* istanbul ignore next -- @preserve */ if (activeConnection?.ws !== ws) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (opts.connectionParams) {
|
|
146
|
+
const connectMsg = {
|
|
147
|
+
method: 'connectionParams',
|
|
148
|
+
data: await resultOf(opts.connectionParams)
|
|
149
|
+
};
|
|
150
|
+
ws.send(JSON.stringify(connectMsg));
|
|
151
|
+
}
|
|
152
|
+
connectAttempt = 0;
|
|
153
|
+
self.state = 'open';
|
|
154
|
+
onOpen?.();
|
|
155
|
+
dispatch();
|
|
156
|
+
}).catch((cause)=>{
|
|
157
|
+
ws.close(// "Status codes in the range 3000-3999 are reserved for use by libraries, frameworks, and applications"
|
|
158
|
+
3000, cause);
|
|
159
|
+
onError();
|
|
160
|
+
});
|
|
142
161
|
});
|
|
143
162
|
ws.addEventListener('error', onError);
|
|
144
163
|
const handleIncomingRequest = (req)=>{
|
|
@@ -273,7 +292,10 @@ function createWSClient(opts) {
|
|
|
273
292
|
request,
|
|
274
293
|
get connection () {
|
|
275
294
|
return activeConnection;
|
|
276
|
-
}
|
|
295
|
+
},
|
|
296
|
+
/**
|
|
297
|
+
* Reconnect to the WebSocket server
|
|
298
|
+
*/ reconnect
|
|
277
299
|
};
|
|
278
300
|
}
|
|
279
301
|
class TRPCWebSocketClosedError extends Error {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trpc/client",
|
|
3
|
-
"version": "11.0.0-rc.
|
|
3
|
+
"version": "11.0.0-rc.436+fbb4a2fb4",
|
|
4
4
|
"description": "The tRPC client library",
|
|
5
5
|
"author": "KATT",
|
|
6
6
|
"license": "MIT",
|
|
@@ -76,10 +76,10 @@
|
|
|
76
76
|
"!**/*.test.*"
|
|
77
77
|
],
|
|
78
78
|
"peerDependencies": {
|
|
79
|
-
"@trpc/server": "11.0.0-rc.
|
|
79
|
+
"@trpc/server": "11.0.0-rc.436+fbb4a2fb4"
|
|
80
80
|
},
|
|
81
81
|
"devDependencies": {
|
|
82
|
-
"@trpc/server": "11.0.0-rc.
|
|
82
|
+
"@trpc/server": "11.0.0-rc.436+fbb4a2fb4",
|
|
83
83
|
"@types/isomorphic-fetch": "^0.0.39",
|
|
84
84
|
"@types/node": "^20.10.0",
|
|
85
85
|
"eslint": "^8.56.0",
|
|
@@ -96,5 +96,5 @@
|
|
|
96
96
|
"funding": [
|
|
97
97
|
"https://trpc.io/sponsor"
|
|
98
98
|
],
|
|
99
|
-
"gitHead": "
|
|
99
|
+
"gitHead": "fbb4a2fb4b71d2f5ab7ee97617d951f3a92517c8"
|
|
100
100
|
}
|
|
@@ -3,7 +3,6 @@ import type {
|
|
|
3
3
|
AnyClientTypes,
|
|
4
4
|
inferClientTypes,
|
|
5
5
|
InferrableClientTypes,
|
|
6
|
-
MaybePromise,
|
|
7
6
|
SSEMessage,
|
|
8
7
|
} from '@trpc/server/unstable-core-do-not-import';
|
|
9
8
|
import {
|
|
@@ -13,25 +12,34 @@ import {
|
|
|
13
12
|
import { TRPCClientError } from '../TRPCClientError';
|
|
14
13
|
import { getTransformer, type TransformerOptions } from '../unstable-internals';
|
|
15
14
|
import { getUrl } from './internals/httpUtils';
|
|
15
|
+
import {
|
|
16
|
+
resultOf,
|
|
17
|
+
type UrlOptionsWithConnectionParams,
|
|
18
|
+
} from './internals/urlWithConnectionParams';
|
|
16
19
|
import type { TRPCLink } from './types';
|
|
17
20
|
|
|
21
|
+
async function urlWithConnectionParams(
|
|
22
|
+
opts: UrlOptionsWithConnectionParams,
|
|
23
|
+
): Promise<string> {
|
|
24
|
+
let url = await resultOf(opts.url);
|
|
25
|
+
if (opts.connectionParams) {
|
|
26
|
+
const params = await resultOf(opts.connectionParams);
|
|
27
|
+
|
|
28
|
+
const prefix = url.includes('?') ? '&' : '?';
|
|
29
|
+
url +=
|
|
30
|
+
prefix + 'connectionParams=' + encodeURIComponent(JSON.stringify(params));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return url;
|
|
34
|
+
}
|
|
35
|
+
|
|
18
36
|
type HTTPSubscriptionLinkOptions<TRoot extends AnyClientTypes> = {
|
|
19
|
-
/**
|
|
20
|
-
* The URL to connect to (can be a function that returns a URL)
|
|
21
|
-
*/
|
|
22
|
-
url: string | (() => MaybePromise<string>);
|
|
23
37
|
/**
|
|
24
38
|
* EventSource options
|
|
25
39
|
*/
|
|
26
40
|
eventSourceOptions?: EventSourceInit;
|
|
27
|
-
} & TransformerOptions<TRoot
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Get the result of a value or function that returns a value
|
|
31
|
-
*/
|
|
32
|
-
const resultOf = <T>(value: T | (() => T)): T => {
|
|
33
|
-
return typeof value === 'function' ? (value as () => T)() : value;
|
|
34
|
-
};
|
|
41
|
+
} & TransformerOptions<TRoot> &
|
|
42
|
+
UrlOptionsWithConnectionParams;
|
|
35
43
|
|
|
36
44
|
/**
|
|
37
45
|
* @see https://trpc.io/docs/client/links/httpSubscriptionLink
|
|
@@ -57,7 +65,7 @@ export function unstable_httpSubscriptionLink<
|
|
|
57
65
|
run(async () => {
|
|
58
66
|
const url = getUrl({
|
|
59
67
|
transformer,
|
|
60
|
-
url: await
|
|
68
|
+
url: await urlWithConnectionParams(opts),
|
|
61
69
|
input,
|
|
62
70
|
path,
|
|
63
71
|
type,
|
|
@@ -115,9 +115,15 @@ export type ContentOptions = {
|
|
|
115
115
|
};
|
|
116
116
|
|
|
117
117
|
export const getUrl: GetUrl = (opts) => {
|
|
118
|
-
const
|
|
118
|
+
const parts = opts.url.split('?') as [string, string?];
|
|
119
|
+
const base = parts[0].replace(/\/$/, ''); // Remove any trailing slashes
|
|
120
|
+
|
|
119
121
|
let url = base + '/' + opts.path;
|
|
120
122
|
const queryParts: string[] = [];
|
|
123
|
+
|
|
124
|
+
if (parts[1]) {
|
|
125
|
+
queryParts.push(parts[1]);
|
|
126
|
+
}
|
|
121
127
|
if ('inputs' in opts) {
|
|
122
128
|
queryParts.push('batch=1');
|
|
123
129
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type TRPCRequestInfo } from '@trpc/server/http';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get the result of a value or function that returns a value
|
|
5
|
+
*/
|
|
6
|
+
export const resultOf = <T>(value: T | (() => T)): T => {
|
|
7
|
+
return typeof value === 'function' ? (value as () => T)() : value;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A value that can be wrapped in callback
|
|
12
|
+
*/
|
|
13
|
+
type CallbackOrValue<T> = T | (() => T | Promise<T>);
|
|
14
|
+
|
|
15
|
+
export interface UrlOptionsWithConnectionParams {
|
|
16
|
+
/**
|
|
17
|
+
* The URL to connect to (can be a function that returns a URL)
|
|
18
|
+
*/
|
|
19
|
+
url: CallbackOrValue<string>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Connection params that are available in `createContext()`
|
|
23
|
+
* - For `wsLink`/`wsClient`, these are sent as the first message
|
|
24
|
+
* - For `httpSubscriptionLink`, these are serialized as part of the URL under the `connectionParams` query
|
|
25
|
+
*/
|
|
26
|
+
connectionParams?: CallbackOrValue<TRPCRequestInfo['connectionParams']>;
|
|
27
|
+
}
|
package/src/links/wsLink.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { Observer, UnsubscribeFn } from '@trpc/server/observable';
|
|
2
2
|
import { observable } from '@trpc/server/observable';
|
|
3
|
+
import type { TRPCConnectionParamsMessage } from '@trpc/server/rpc';
|
|
3
4
|
import type {
|
|
4
5
|
AnyRouter,
|
|
5
6
|
inferClientTypes,
|
|
6
7
|
inferRouterError,
|
|
7
|
-
MaybePromise,
|
|
8
8
|
ProcedureType,
|
|
9
9
|
TRPCClientIncomingMessage,
|
|
10
10
|
TRPCClientIncomingRequest,
|
|
@@ -16,6 +16,10 @@ import { transformResult } from '@trpc/server/unstable-core-do-not-import';
|
|
|
16
16
|
import { TRPCClientError } from '../TRPCClientError';
|
|
17
17
|
import type { TransformerOptions } from '../unstable-internals';
|
|
18
18
|
import { getTransformer } from '../unstable-internals';
|
|
19
|
+
import {
|
|
20
|
+
resultOf,
|
|
21
|
+
type UrlOptionsWithConnectionParams,
|
|
22
|
+
} from './internals/urlWithConnectionParams';
|
|
19
23
|
import type { Operation, TRPCLink } from './types';
|
|
20
24
|
|
|
21
25
|
const run = <TResult>(fn: () => TResult): TResult => fn();
|
|
@@ -33,11 +37,7 @@ type WSCallbackObserver<TRouter extends AnyRouter, TOutput> = Observer<
|
|
|
33
37
|
const exponentialBackoff = (attemptIndex: number) =>
|
|
34
38
|
attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
|
|
35
39
|
|
|
36
|
-
export interface WebSocketClientOptions {
|
|
37
|
-
/**
|
|
38
|
-
* The URL to connect to (can be a function that returns a URL)
|
|
39
|
-
*/
|
|
40
|
-
url: string | (() => MaybePromise<string>);
|
|
40
|
+
export interface WebSocketClientOptions extends UrlOptionsWithConnectionParams {
|
|
41
41
|
/**
|
|
42
42
|
* Ponyfill which WebSocket implementation to use
|
|
43
43
|
*/
|
|
@@ -79,7 +79,6 @@ const lazyDefaults: LazyOptions = {
|
|
|
79
79
|
};
|
|
80
80
|
export function createWSClient(opts: WebSocketClientOptions) {
|
|
81
81
|
const {
|
|
82
|
-
url,
|
|
83
82
|
WebSocket: WebSocketImpl = WebSocket,
|
|
84
83
|
retryDelayMs: retryDelayFn = exponentialBackoff,
|
|
85
84
|
onOpen,
|
|
@@ -251,23 +250,47 @@ export function createWSClient(opts: WebSocketClientOptions) {
|
|
|
251
250
|
}
|
|
252
251
|
};
|
|
253
252
|
run(async () => {
|
|
254
|
-
|
|
255
|
-
|
|
253
|
+
let url = await resultOf(opts.url);
|
|
254
|
+
if (opts.connectionParams) {
|
|
255
|
+
// append `?connectionParams=1` when connection params are used
|
|
256
|
+
const prefix = url.includes('?') ? '&' : '?';
|
|
257
|
+
url += prefix + 'connectionParams=1';
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const ws = new WebSocketImpl(url);
|
|
256
261
|
self.ws = ws;
|
|
257
262
|
|
|
258
263
|
clearTimeout(connectTimer);
|
|
259
264
|
connectTimer = undefined;
|
|
260
265
|
|
|
261
266
|
ws.addEventListener('open', () => {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
267
|
+
run(async () => {
|
|
268
|
+
/* istanbul ignore next -- @preserve */
|
|
269
|
+
if (activeConnection?.ws !== ws) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (opts.connectionParams) {
|
|
273
|
+
const connectMsg: TRPCConnectionParamsMessage = {
|
|
274
|
+
method: 'connectionParams',
|
|
275
|
+
data: await resultOf(opts.connectionParams),
|
|
276
|
+
};
|
|
268
277
|
|
|
269
|
-
|
|
270
|
-
|
|
278
|
+
ws.send(JSON.stringify(connectMsg));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
connectAttempt = 0;
|
|
282
|
+
self.state = 'open';
|
|
283
|
+
|
|
284
|
+
onOpen?.();
|
|
285
|
+
dispatch();
|
|
286
|
+
}).catch((cause) => {
|
|
287
|
+
ws.close(
|
|
288
|
+
// "Status codes in the range 3000-3999 are reserved for use by libraries, frameworks, and applications"
|
|
289
|
+
3000,
|
|
290
|
+
cause,
|
|
291
|
+
);
|
|
292
|
+
onError();
|
|
293
|
+
});
|
|
271
294
|
});
|
|
272
295
|
ws.addEventListener('error', onError);
|
|
273
296
|
const handleIncomingRequest = (req: TRPCClientIncomingRequest) => {
|
|
@@ -428,6 +451,10 @@ export function createWSClient(opts: WebSocketClientOptions) {
|
|
|
428
451
|
get connection() {
|
|
429
452
|
return activeConnection;
|
|
430
453
|
},
|
|
454
|
+
/**
|
|
455
|
+
* Reconnect to the WebSocket server
|
|
456
|
+
*/
|
|
457
|
+
reconnect,
|
|
431
458
|
};
|
|
432
459
|
}
|
|
433
460
|
export type TRPCWebSocketClient = ReturnType<typeof createWSClient>;
|