@trpc/client 11.0.0-alpha-tmp-subscription-connection-state.489 → 11.0.0-alpha-tmp-12-06-react.667
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.ts +1 -1
- package/dist/TRPCClientError.d.ts.map +1 -1
- package/dist/TRPCClientError.js +19 -1
- package/dist/TRPCClientError.mjs +19 -1
- package/dist/bundle-analysis.json +118 -92
- package/dist/createTRPCClient.d.ts +3 -2
- package/dist/createTRPCClient.d.ts.map +1 -1
- package/dist/createTRPCClient.js +1 -1
- package/dist/createTRPCClient.mjs +1 -1
- package/dist/index.js +6 -6
- package/dist/index.mjs +2 -2
- package/dist/internals/TRPCUntypedClient.d.ts +5 -4
- package/dist/internals/TRPCUntypedClient.d.ts.map +1 -1
- package/dist/internals/TRPCUntypedClient.js +42 -12
- package/dist/internals/TRPCUntypedClient.mjs +42 -12
- package/dist/internals/inputWithTrackedEventId.d.ts +2 -0
- package/dist/internals/inputWithTrackedEventId.d.ts.map +1 -0
- package/dist/internals/inputWithTrackedEventId.js +16 -0
- package/dist/internals/inputWithTrackedEventId.mjs +14 -0
- package/dist/internals/signals.d.ts +15 -0
- package/dist/internals/signals.d.ts.map +1 -0
- package/dist/internals/signals.js +47 -0
- package/dist/internals/signals.mjs +44 -0
- package/dist/internals/transformer.d.ts +2 -2
- package/dist/internals/types.d.ts +1 -1
- package/dist/internals/types.d.ts.map +1 -1
- package/dist/links/HTTPBatchLinkOptions.d.ts +1 -1
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +4 -3
- package/dist/links/httpBatchLink.mjs +5 -4
- package/dist/links/httpBatchStreamLink.d.ts.map +1 -1
- package/dist/links/httpBatchStreamLink.js +6 -4
- package/dist/links/httpBatchStreamLink.mjs +7 -5
- package/dist/links/httpLink.d.ts +2 -2
- package/dist/links/httpLink.js +3 -3
- package/dist/links/httpLink.mjs +3 -3
- package/dist/links/httpSubscriptionLink.d.ts +11 -6
- package/dist/links/httpSubscriptionLink.d.ts.map +1 -1
- package/dist/links/httpSubscriptionLink.js +130 -98
- package/dist/links/httpSubscriptionLink.mjs +132 -100
- package/dist/links/internals/contentTypes.d.ts +2 -2
- package/dist/links/internals/contentTypes.d.ts.map +1 -1
- package/dist/links/internals/httpUtils.d.ts +1 -8
- package/dist/links/internals/httpUtils.d.ts.map +1 -1
- package/dist/links/internals/httpUtils.js +1 -30
- package/dist/links/internals/httpUtils.mjs +2 -30
- package/dist/links/internals/subscriptions.d.ts +20 -0
- package/dist/links/internals/subscriptions.d.ts.map +1 -0
- package/dist/links/internals/urlWithConnectionParams.d.ts +2 -1
- package/dist/links/internals/urlWithConnectionParams.d.ts.map +1 -1
- package/dist/links/internals/urlWithConnectionParams.js +3 -2
- package/dist/links/internals/urlWithConnectionParams.mjs +3 -2
- package/dist/links/loggerLink.d.ts +5 -5
- package/dist/links/loggerLink.d.ts.map +1 -1
- package/dist/links/loggerLink.js +25 -21
- package/dist/links/loggerLink.mjs +25 -21
- package/dist/links/retryLink.d.ts +29 -0
- package/dist/links/retryLink.d.ts.map +1 -0
- package/dist/links/retryLink.js +65 -0
- package/dist/links/retryLink.mjs +63 -0
- package/dist/links/types.d.ts +4 -23
- package/dist/links/types.d.ts.map +1 -1
- package/dist/links/wsLink.d.ts +49 -5
- package/dist/links/wsLink.d.ts.map +1 -1
- package/dist/links/wsLink.js +210 -155
- package/dist/links/wsLink.mjs +211 -156
- package/dist/links.d.ts +1 -0
- package/dist/links.d.ts.map +1 -1
- package/dist/unstable-internals.d.ts +1 -0
- package/dist/unstable-internals.d.ts.map +1 -1
- package/package.json +14 -11
- package/src/TRPCClientError.ts +1 -1
- package/src/createTRPCClient.ts +28 -23
- package/src/internals/TRPCUntypedClient.ts +26 -15
- package/src/internals/inputWithTrackedEventId.ts +15 -0
- package/src/internals/signals.ts +54 -0
- package/src/internals/transformer.ts +2 -2
- package/src/internals/types.ts +1 -1
- package/src/links/HTTPBatchLinkOptions.ts +1 -1
- package/src/links/httpBatchLink.ts +3 -3
- package/src/links/httpBatchStreamLink.ts +7 -4
- package/src/links/httpLink.ts +2 -2
- package/src/links/httpSubscriptionLink.ts +172 -123
- package/src/links/internals/httpUtils.ts +1 -41
- package/src/links/internals/subscriptions.ts +26 -0
- package/src/links/internals/urlWithConnectionParams.ts +8 -2
- package/src/links/loggerLink.ts +21 -9
- package/src/links/retryLink.ts +101 -0
- package/src/links/types.ts +8 -46
- package/src/links/wsLink.ts +276 -173
- package/src/links.ts +1 -1
- package/src/unstable-internals.ts +1 -0
- package/dist/links/internals/retryLink.d.ts +0 -9
- package/dist/links/internals/retryLink.d.ts.map +0 -1
- package/dist/links/types.js +0 -7
- package/dist/links/types.mjs +0 -5
- package/src/links/internals/retryLink.ts +0 -53
package/src/createTRPCClient.ts
CHANGED
|
@@ -36,15 +36,20 @@ type ResolverDef = {
|
|
|
36
36
|
errorShape: any;
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
+
type coerceAsyncGeneratorToIterable<T> =
|
|
40
|
+
T extends AsyncGenerator<infer $T, infer $Return, infer $Next>
|
|
41
|
+
? AsyncIterable<$T, $Return, $Next>
|
|
42
|
+
: T;
|
|
43
|
+
|
|
39
44
|
/** @internal */
|
|
40
45
|
export type Resolver<TDef extends ResolverDef> = (
|
|
41
46
|
input: TDef['input'],
|
|
42
47
|
opts?: ProcedureOptions,
|
|
43
|
-
) => Promise<TDef['output']
|
|
48
|
+
) => Promise<coerceAsyncGeneratorToIterable<TDef['output']>>;
|
|
44
49
|
|
|
45
50
|
type SubscriptionResolver<TDef extends ResolverDef> = (
|
|
46
51
|
input: TDef['input'],
|
|
47
|
-
opts
|
|
52
|
+
opts: Partial<
|
|
48
53
|
TRPCSubscriptionObserver<TDef['output'], TRPCClientError<TDef>>
|
|
49
54
|
> &
|
|
50
55
|
ProcedureOptions,
|
|
@@ -58,14 +63,14 @@ type DecorateProcedure<
|
|
|
58
63
|
query: Resolver<TDef>;
|
|
59
64
|
}
|
|
60
65
|
: TType extends 'mutation'
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
? {
|
|
67
|
+
mutate: Resolver<TDef>;
|
|
68
|
+
}
|
|
69
|
+
: TType extends 'subscription'
|
|
70
|
+
? {
|
|
71
|
+
subscribe: SubscriptionResolver<TDef>;
|
|
72
|
+
}
|
|
73
|
+
: never;
|
|
69
74
|
|
|
70
75
|
/**
|
|
71
76
|
* @internal
|
|
@@ -78,19 +83,19 @@ type DecoratedProcedureRecord<
|
|
|
78
83
|
? $Value extends RouterRecord
|
|
79
84
|
? DecoratedProcedureRecord<TRouter, $Value>
|
|
80
85
|
: $Value extends AnyProcedure
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
86
|
+
? DecorateProcedure<
|
|
87
|
+
$Value['_def']['type'],
|
|
88
|
+
{
|
|
89
|
+
input: inferProcedureInput<$Value>;
|
|
90
|
+
output: inferTransformedProcedureOutput<
|
|
91
|
+
inferClientTypes<TRouter>,
|
|
92
|
+
$Value
|
|
93
|
+
>;
|
|
94
|
+
errorShape: inferClientTypes<TRouter>['errorShape'];
|
|
95
|
+
transformer: inferClientTypes<TRouter>['transformer'];
|
|
96
|
+
}
|
|
97
|
+
>
|
|
98
|
+
: never
|
|
94
99
|
: never;
|
|
95
100
|
};
|
|
96
101
|
|
|
@@ -5,16 +5,17 @@ import type {
|
|
|
5
5
|
import { observableToPromise, share } from '@trpc/server/observable';
|
|
6
6
|
import type {
|
|
7
7
|
AnyRouter,
|
|
8
|
+
inferAsyncIterableYield,
|
|
8
9
|
InferrableClientTypes,
|
|
9
10
|
Maybe,
|
|
10
11
|
TypeError,
|
|
11
12
|
} from '@trpc/server/unstable-core-do-not-import';
|
|
12
13
|
import { createChain } from '../links/internals/createChain';
|
|
14
|
+
import type { TRPCConnectionState } from '../links/internals/subscriptions';
|
|
13
15
|
import type {
|
|
14
16
|
OperationContext,
|
|
15
17
|
OperationLink,
|
|
16
18
|
TRPCClientRuntime,
|
|
17
|
-
TRPCConnectionStateMessage,
|
|
18
19
|
TRPCLink,
|
|
19
20
|
} from '../links/types';
|
|
20
21
|
import { TRPCClientError } from '../TRPCClientError';
|
|
@@ -30,11 +31,11 @@ export interface TRPCRequestOptions {
|
|
|
30
31
|
|
|
31
32
|
export interface TRPCSubscriptionObserver<TValue, TError> {
|
|
32
33
|
onStarted: (opts: { context: OperationContext | undefined }) => void;
|
|
33
|
-
onData: (value: TValue) => void;
|
|
34
|
+
onData: (value: inferAsyncIterableYield<TValue>) => void;
|
|
34
35
|
onError: (err: TError) => void;
|
|
35
36
|
onStopped: () => void;
|
|
36
37
|
onComplete: () => void;
|
|
37
|
-
|
|
38
|
+
onConnectionStateChange: (state: TRPCConnectionState<TError>) => void;
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
/** @internal */
|
|
@@ -134,21 +135,31 @@ export class TRPCUntypedClient<TRouter extends AnyRouter> {
|
|
|
134
135
|
type: 'subscription',
|
|
135
136
|
path,
|
|
136
137
|
input,
|
|
137
|
-
context: opts
|
|
138
|
-
signal:
|
|
138
|
+
context: opts.context,
|
|
139
|
+
signal: opts.signal,
|
|
139
140
|
});
|
|
140
141
|
return observable$.subscribe({
|
|
141
142
|
next(envelope) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
143
|
+
switch (envelope.result.type) {
|
|
144
|
+
case 'state': {
|
|
145
|
+
opts.onConnectionStateChange?.(envelope.result);
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
case 'started': {
|
|
149
|
+
opts.onStarted?.({
|
|
150
|
+
context: envelope.context,
|
|
151
|
+
});
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case 'stopped': {
|
|
155
|
+
opts.onStopped?.();
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case 'data':
|
|
159
|
+
case undefined: {
|
|
160
|
+
opts.onData?.(envelope.result.data);
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
152
163
|
}
|
|
153
164
|
},
|
|
154
165
|
error(err) {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function inputWithTrackedEventId(
|
|
2
|
+
input: unknown,
|
|
3
|
+
lastEventId: string | undefined,
|
|
4
|
+
) {
|
|
5
|
+
if (!lastEventId) {
|
|
6
|
+
return input;
|
|
7
|
+
}
|
|
8
|
+
if (input != null && typeof input !== 'object') {
|
|
9
|
+
return input;
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
...(input ?? {}),
|
|
13
|
+
lastEventId,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Maybe } from '@trpc/server/unstable-core-do-not-import';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Like `Promise.all()` but for abort signals
|
|
5
|
+
* - When all signals have been aborted, the merged signal will be aborted
|
|
6
|
+
* - If one signal is `null`, no signal will be aborted
|
|
7
|
+
*/
|
|
8
|
+
export function allAbortSignals(...signals: Maybe<AbortSignal>[]): AbortSignal {
|
|
9
|
+
const ac = new AbortController();
|
|
10
|
+
|
|
11
|
+
const count = signals.length;
|
|
12
|
+
|
|
13
|
+
let abortedCount = 0;
|
|
14
|
+
|
|
15
|
+
const onAbort = () => {
|
|
16
|
+
if (++abortedCount === count) {
|
|
17
|
+
ac.abort();
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
for (const signal of signals) {
|
|
22
|
+
if (signal?.aborted) {
|
|
23
|
+
onAbort();
|
|
24
|
+
} else {
|
|
25
|
+
signal?.addEventListener('abort', onAbort, {
|
|
26
|
+
once: true,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return ac.signal;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Like `Promise.race` but for abort signals
|
|
36
|
+
*
|
|
37
|
+
* Basically, a ponyfill for
|
|
38
|
+
* [`AbortSignal.any`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static).
|
|
39
|
+
*/
|
|
40
|
+
export function raceAbortSignals(
|
|
41
|
+
...signals: Maybe<AbortSignal>[]
|
|
42
|
+
): AbortSignal {
|
|
43
|
+
const ac = new AbortController();
|
|
44
|
+
|
|
45
|
+
for (const signal of signals) {
|
|
46
|
+
if (signal?.aborted) {
|
|
47
|
+
ac.abort();
|
|
48
|
+
} else {
|
|
49
|
+
signal?.addEventListener('abort', () => ac.abort(), { once: true });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return ac.signal;
|
|
54
|
+
}
|
|
@@ -17,7 +17,7 @@ type TransformerOptionYes = {
|
|
|
17
17
|
* Data transformer
|
|
18
18
|
*
|
|
19
19
|
* You must use the same transformer on the backend and frontend
|
|
20
|
-
* @
|
|
20
|
+
* @see https://trpc.io/docs/v11/data-transformers
|
|
21
21
|
**/
|
|
22
22
|
transformer: DataTransformerOptions;
|
|
23
23
|
};
|
|
@@ -26,7 +26,7 @@ type TransformerOptionNo = {
|
|
|
26
26
|
* Data transformer
|
|
27
27
|
*
|
|
28
28
|
* You must use the same transformer on the backend and frontend
|
|
29
|
-
* @
|
|
29
|
+
* @see https://trpc.io/docs/v11/data-transformers
|
|
30
30
|
**/
|
|
31
31
|
transformer?: TypeError<'You must define a transformer on your your `initTRPC`-object first'>;
|
|
32
32
|
};
|
package/src/internals/types.ts
CHANGED
|
@@ -8,7 +8,7 @@ export type HTTPBatchLinkOptions<TRoot extends AnyClientTypes> =
|
|
|
8
8
|
maxURLLength?: number;
|
|
9
9
|
/**
|
|
10
10
|
* Headers to be set on outgoing requests or a callback that of said headers
|
|
11
|
-
* @
|
|
11
|
+
* @see http://trpc.io/docs/client/headers
|
|
12
12
|
*/
|
|
13
13
|
headers?:
|
|
14
14
|
| HTTPHeaders
|
|
@@ -3,6 +3,7 @@ import { observable } from '@trpc/server/observable';
|
|
|
3
3
|
import { transformResult } from '@trpc/server/unstable-core-do-not-import';
|
|
4
4
|
import type { BatchLoader } from '../internals/dataLoader';
|
|
5
5
|
import { dataLoader } from '../internals/dataLoader';
|
|
6
|
+
import { allAbortSignals } from '../internals/signals';
|
|
6
7
|
import type { NonEmptyArray } from '../internals/types';
|
|
7
8
|
import { TRPCClientError } from '../TRPCClientError';
|
|
8
9
|
import type { HTTPBatchLinkOptions } from './HTTPBatchLinkOptions';
|
|
@@ -10,7 +11,6 @@ import type { HTTPResult } from './internals/httpUtils';
|
|
|
10
11
|
import {
|
|
11
12
|
getUrl,
|
|
12
13
|
jsonHttpRequester,
|
|
13
|
-
mergeAbortSignals,
|
|
14
14
|
resolveHTTPLinkOptions,
|
|
15
15
|
} from './internals/httpUtils';
|
|
16
16
|
import type { Operation, TRPCLink } from './types';
|
|
@@ -50,7 +50,7 @@ export function httpBatchLink<TRouter extends AnyRouter>(
|
|
|
50
50
|
async fetch(batchOps) {
|
|
51
51
|
const path = batchOps.map((op) => op.path).join(',');
|
|
52
52
|
const inputs = batchOps.map((op) => op.input);
|
|
53
|
-
const
|
|
53
|
+
const signal = allAbortSignals(...batchOps.map((op) => op.signal));
|
|
54
54
|
|
|
55
55
|
const res = await jsonHttpRequester({
|
|
56
56
|
...resolvedOpts,
|
|
@@ -68,7 +68,7 @@ export function httpBatchLink<TRouter extends AnyRouter>(
|
|
|
68
68
|
}
|
|
69
69
|
return opts.headers;
|
|
70
70
|
},
|
|
71
|
-
signal
|
|
71
|
+
signal,
|
|
72
72
|
});
|
|
73
73
|
const resJSON = Array.isArray(res.json)
|
|
74
74
|
? res.json
|
|
@@ -5,6 +5,7 @@ import type { AnyRootTypes } from '@trpc/server/unstable-core-do-not-import';
|
|
|
5
5
|
import { jsonlStreamConsumer } from '@trpc/server/unstable-core-do-not-import';
|
|
6
6
|
import type { BatchLoader } from '../internals/dataLoader';
|
|
7
7
|
import { dataLoader } from '../internals/dataLoader';
|
|
8
|
+
import { allAbortSignals, raceAbortSignals } from '../internals/signals';
|
|
8
9
|
import type { NonEmptyArray } from '../internals/types';
|
|
9
10
|
import { TRPCClientError } from '../TRPCClientError';
|
|
10
11
|
import type { HTTPBatchLinkOptions } from './HTTPBatchLinkOptions';
|
|
@@ -13,7 +14,6 @@ import {
|
|
|
13
14
|
fetchHTTPResponse,
|
|
14
15
|
getBody,
|
|
15
16
|
getUrl,
|
|
16
|
-
mergeAbortSignals,
|
|
17
17
|
resolveHTTPLinkOptions,
|
|
18
18
|
} from './internals/httpUtils';
|
|
19
19
|
import type { Operation, TRPCLink } from './types';
|
|
@@ -67,11 +67,14 @@ export function unstable_httpBatchStreamLink<TRouter extends AnyRouter>(
|
|
|
67
67
|
const path = batchOps.map((op) => op.path).join(',');
|
|
68
68
|
const inputs = batchOps.map((op) => op.input);
|
|
69
69
|
|
|
70
|
-
const
|
|
70
|
+
const batchSignals = allAbortSignals(
|
|
71
|
+
...batchOps.map((op) => op.signal),
|
|
72
|
+
);
|
|
73
|
+
const abortController = new AbortController();
|
|
71
74
|
|
|
72
75
|
const responsePromise = fetchHTTPResponse({
|
|
73
76
|
...resolvedOpts,
|
|
74
|
-
signal:
|
|
77
|
+
signal: raceAbortSignals(batchSignals, abortController.signal),
|
|
75
78
|
type,
|
|
76
79
|
contentTypeHeader: 'application/json',
|
|
77
80
|
trpcAcceptHeader: 'application/jsonl',
|
|
@@ -106,7 +109,7 @@ export function unstable_httpBatchStreamLink<TRouter extends AnyRouter>(
|
|
|
106
109
|
error,
|
|
107
110
|
});
|
|
108
111
|
},
|
|
109
|
-
abortController
|
|
112
|
+
abortController,
|
|
110
113
|
});
|
|
111
114
|
const promises = Object.keys(batchOps).map(
|
|
112
115
|
async (key): Promise<HTTPResult> => {
|
package/src/links/httpLink.ts
CHANGED
|
@@ -29,7 +29,7 @@ export type HTTPLinkOptions<TRoot extends AnyClientTypes> =
|
|
|
29
29
|
HTTPLinkBaseOptions<TRoot> & {
|
|
30
30
|
/**
|
|
31
31
|
* Headers to be set on outgoing requests or a callback that of said headers
|
|
32
|
-
* @
|
|
32
|
+
* @see http://trpc.io/docs/client/headers
|
|
33
33
|
*/
|
|
34
34
|
headers?:
|
|
35
35
|
| HTTPHeaders
|
|
@@ -70,7 +70,7 @@ const universalRequester: Requester = (opts) => {
|
|
|
70
70
|
};
|
|
71
71
|
|
|
72
72
|
/**
|
|
73
|
-
* @
|
|
73
|
+
* @see https://trpc.io/docs/client/links/httpLink
|
|
74
74
|
*/
|
|
75
75
|
export function httpLink<TRouter extends AnyRouter = AnyRouter>(
|
|
76
76
|
opts: HTTPLinkOptions<TRouter['_def']['_config']['$types']>,
|