@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
|
@@ -94,7 +94,7 @@ async function fetchHTTPResponse(opts) {
|
|
|
94
94
|
throwIfAborted(opts.signal);
|
|
95
95
|
const url = opts.getUrl(opts);
|
|
96
96
|
const body = opts.getBody(opts);
|
|
97
|
-
const { type
|
|
97
|
+
const { type } = opts;
|
|
98
98
|
const resolvedHeaders = await (async ()=>{
|
|
99
99
|
const heads = await opts.headers();
|
|
100
100
|
if (Symbol.iterator in heads) {
|
|
@@ -129,33 +129,5 @@ async function httpRequest(opts) {
|
|
|
129
129
|
meta
|
|
130
130
|
};
|
|
131
131
|
}
|
|
132
|
-
/**
|
|
133
|
-
* Merges multiple abort signals into a single one
|
|
134
|
-
* - When all signals have been aborted, the merged signal will be aborted
|
|
135
|
-
*/ function mergeAbortSignals(opts) {
|
|
136
|
-
const ac = new AbortController();
|
|
137
|
-
if (opts.some((o)=>!o.signal)) {
|
|
138
|
-
return ac;
|
|
139
|
-
}
|
|
140
|
-
const count = opts.length;
|
|
141
|
-
let abortedCount = 0;
|
|
142
|
-
const onAbort = ()=>{
|
|
143
|
-
if (++abortedCount === count) {
|
|
144
|
-
ac.abort();
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
for (const o of opts){
|
|
148
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
149
|
-
const signal = o.signal;
|
|
150
|
-
if (signal.aborted) {
|
|
151
|
-
onAbort();
|
|
152
|
-
} else {
|
|
153
|
-
signal.addEventListener('abort', onAbort, {
|
|
154
|
-
once: true
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
return ac;
|
|
159
|
-
}
|
|
160
132
|
|
|
161
|
-
export { fetchHTTPResponse, getBody, getInput, getUrl, httpRequest, jsonHttpRequester,
|
|
133
|
+
export { fetchHTTPResponse, getBody, getInput, getUrl, httpRequest, jsonHttpRequester, resolveHTTPLinkOptions };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface ConnectionStateBase<TError> {
|
|
2
|
+
type: 'state';
|
|
3
|
+
data?: never;
|
|
4
|
+
error: TError | null;
|
|
5
|
+
}
|
|
6
|
+
interface ConnectionIdleState<TError> extends ConnectionStateBase<TError> {
|
|
7
|
+
state: 'idle';
|
|
8
|
+
error: null;
|
|
9
|
+
}
|
|
10
|
+
interface ConnectionConnectingState<TError> extends ConnectionStateBase<TError> {
|
|
11
|
+
state: 'connecting';
|
|
12
|
+
error: TError | null;
|
|
13
|
+
}
|
|
14
|
+
interface ConnectionPendingState extends ConnectionStateBase<never> {
|
|
15
|
+
state: 'pending';
|
|
16
|
+
error: null;
|
|
17
|
+
}
|
|
18
|
+
export type TRPCConnectionState<TError> = ConnectionIdleState<TError> | ConnectionConnectingState<TError> | ConnectionPendingState;
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=subscriptions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscriptions.d.ts","sourceRoot":"","sources":["../../../src/links/internals/subscriptions.ts"],"names":[],"mappings":"AAAA,UAAU,mBAAmB,CAAC,MAAM;IAClC,IAAI,EAAE,OAAO,CAAC;IACd,IAAI,CAAC,EAAE,KAAK,CAAC;IACb,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED,UAAU,mBAAmB,CAAC,MAAM,CAAE,SAAQ,mBAAmB,CAAC,MAAM,CAAC;IACvE,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,IAAI,CAAC;CACb;AAED,UAAU,yBAAyB,CAAC,MAAM,CACxC,SAAQ,mBAAmB,CAAC,MAAM,CAAC;IACnC,KAAK,EAAE,YAAY,CAAC;IACpB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED,UAAU,sBAAuB,SAAQ,mBAAmB,CAAC,KAAK,CAAC;IACjE,KAAK,EAAE,SAAS,CAAC;IACjB,KAAK,EAAE,IAAI,CAAC;CACb;AAED,MAAM,MAAM,mBAAmB,CAAC,MAAM,IAClC,mBAAmB,CAAC,MAAM,CAAC,GAC3B,yBAAyB,CAAC,MAAM,CAAC,GACjC,sBAAsB,CAAC"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { type TRPCRequestInfo } from '@trpc/server/http';
|
|
2
2
|
/**
|
|
3
3
|
* Get the result of a value or function that returns a value
|
|
4
|
+
* It also optionally accepts typesafe arguments for the function
|
|
4
5
|
*/
|
|
5
|
-
export declare const resultOf: <T>(value: T | (() => T)) => T;
|
|
6
|
+
export declare const resultOf: <T, TArgs extends any[]>(value: T | ((...args: TArgs) => T), ...args: TArgs) => T;
|
|
6
7
|
/**
|
|
7
8
|
* A value that can be wrapped in callback
|
|
8
9
|
*/
|
|
@@ -1 +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
|
|
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;;;GAGG;AACH,eAAO,MAAM,QAAQ,GAAI,CAAC,EAAE,KAAK,SAAS,GAAG,EAAE,SACtC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,CAAC,CAAC,WACzB,KAAK,KACb,CAIF,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;AAE5D,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"}
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Get the result of a value or function that returns a value
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
* It also optionally accepts typesafe arguments for the function
|
|
6
|
+
*/ const resultOf = (value, ...args)=>{
|
|
7
|
+
return typeof value === 'function' ? value(...args) : value;
|
|
7
8
|
};
|
|
8
9
|
|
|
9
10
|
exports.resultOf = resultOf;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Get the result of a value or function that returns a value
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
* It also optionally accepts typesafe arguments for the function
|
|
4
|
+
*/ const resultOf = (value, ...args)=>{
|
|
5
|
+
return typeof value === 'function' ? value(...args) : value;
|
|
5
6
|
};
|
|
6
7
|
|
|
7
8
|
export { resultOf };
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type { AnyRouter } from '@trpc/server/unstable-core-do-not-import';
|
|
1
|
+
import type { AnyRouter, InferrableClientTypes } from '@trpc/server/unstable-core-do-not-import';
|
|
2
2
|
import type { TRPCClientError } from '../TRPCClientError';
|
|
3
3
|
import type { Operation, OperationResultEnvelope, TRPCLink } from './types';
|
|
4
4
|
type ConsoleEsque = {
|
|
5
5
|
log: (...args: any[]) => void;
|
|
6
6
|
error: (...args: any[]) => void;
|
|
7
7
|
};
|
|
8
|
-
type EnableFnOptions<TRouter extends
|
|
8
|
+
type EnableFnOptions<TRouter extends InferrableClientTypes> = {
|
|
9
9
|
direction: 'down';
|
|
10
|
-
result: OperationResultEnvelope<unknown
|
|
10
|
+
result: OperationResultEnvelope<unknown, TRPCClientError<TRouter>> | TRPCClientError<TRouter>;
|
|
11
11
|
} | (Operation & {
|
|
12
12
|
direction: 'up';
|
|
13
13
|
});
|
|
@@ -17,7 +17,7 @@ type LoggerLinkFnOptions<TRouter extends AnyRouter> = Operation & ({
|
|
|
17
17
|
* Request result
|
|
18
18
|
*/
|
|
19
19
|
direction: 'down';
|
|
20
|
-
result: OperationResultEnvelope<unknown
|
|
20
|
+
result: OperationResultEnvelope<unknown, TRPCClientError<TRouter>> | TRPCClientError<TRouter>;
|
|
21
21
|
elapsedMs: number;
|
|
22
22
|
} | {
|
|
23
23
|
/**
|
|
@@ -45,7 +45,7 @@ export interface LoggerLinkOptions<TRouter extends AnyRouter> {
|
|
|
45
45
|
withContext?: boolean;
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
|
-
* @
|
|
48
|
+
* @see https://trpc.io/docs/v11/client/links/loggerLink
|
|
49
49
|
*/
|
|
50
50
|
export declare function loggerLink<TRouter extends AnyRouter = AnyRouter>(opts?: LoggerLinkOptions<TRouter>): TRPCLink<TRouter>;
|
|
51
51
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loggerLink.d.ts","sourceRoot":"","sources":["../../src/links/loggerLink.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"loggerLink.d.ts","sourceRoot":"","sources":["../../src/links/loggerLink.ts"],"names":[],"mappings":"AAQA,OAAO,KA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AAG,MAAM,CAAC;AAEzC,MAAM,WAAW,iBAAiB,CAAC,OAAO,SAAS,SAAS;IAC1D,MAAM,CAAC,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC;IAC/B,OAAO,CAAC,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC;IAC7B;;OAEG;IACH,OAAO,CAAC,EAAE,YAAY,CAAC;IACvB;;;OAGG;IACH,SAAS,CAAC,EAAE,SAAS,CAAC;IAEtB;;OAEG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAqID;;GAEG;AACH,wBAAgB,UAAU,CAAC,OAAO,SAAS,SAAS,GAAG,SAAS,EAC9D,IAAI,GAAE,iBAAiB,CAAC,OAAO,CAAM,GACpC,QAAQ,CAAC,OAAO,CAAC,CAoDnB"}
|
package/dist/links/loggerLink.js
CHANGED
|
@@ -64,7 +64,7 @@ const palettes = {
|
|
|
64
64
|
}
|
|
65
65
|
};
|
|
66
66
|
function constructPartsAndArgs(opts) {
|
|
67
|
-
const { direction
|
|
67
|
+
const { direction, type, withContext, path, id, input } = opts;
|
|
68
68
|
const parts = [];
|
|
69
69
|
const args = [];
|
|
70
70
|
if (opts.colorMode === 'none') {
|
|
@@ -108,55 +108,59 @@ function constructPartsAndArgs(opts) {
|
|
|
108
108
|
};
|
|
109
109
|
}
|
|
110
110
|
// maybe this should be moved to it's own package
|
|
111
|
-
const defaultLogger = ({ c =console
|
|
111
|
+
const defaultLogger = ({ c = console, colorMode = 'css', withContext })=>(props)=>{
|
|
112
112
|
const rawInput = props.input;
|
|
113
113
|
const input = isFormData(rawInput) ? Object.fromEntries(rawInput) : rawInput;
|
|
114
|
-
const { parts
|
|
114
|
+
const { parts, args } = constructPartsAndArgs({
|
|
115
115
|
...props,
|
|
116
116
|
colorMode,
|
|
117
117
|
input,
|
|
118
118
|
withContext
|
|
119
119
|
});
|
|
120
|
-
const fn = props.direction === 'down' && props.result && (props.result instanceof Error || 'error' in props.result.result) ? 'error' : 'log';
|
|
120
|
+
const fn = props.direction === 'down' && props.result && (props.result instanceof Error || 'error' in props.result.result && props.result.result.error) ? 'error' : 'log';
|
|
121
121
|
c[fn].apply(null, [
|
|
122
122
|
parts.join(' ')
|
|
123
123
|
].concat(args));
|
|
124
124
|
};
|
|
125
125
|
/**
|
|
126
|
-
* @
|
|
126
|
+
* @see https://trpc.io/docs/v11/client/links/loggerLink
|
|
127
127
|
*/ function loggerLink(opts = {}) {
|
|
128
|
-
const { enabled =()=>true
|
|
128
|
+
const { enabled = ()=>true } = opts;
|
|
129
129
|
const colorMode = opts.colorMode ?? (typeof window === 'undefined' ? 'ansi' : 'css');
|
|
130
130
|
const withContext = opts.withContext ?? colorMode === 'css';
|
|
131
|
-
const { logger =defaultLogger({
|
|
131
|
+
const { logger = defaultLogger({
|
|
132
132
|
c: opts.console,
|
|
133
133
|
colorMode,
|
|
134
134
|
withContext
|
|
135
|
-
})
|
|
135
|
+
}) } = opts;
|
|
136
136
|
return ()=>{
|
|
137
|
-
return ({ op
|
|
137
|
+
return ({ op, next })=>{
|
|
138
138
|
return observable.observable((observer)=>{
|
|
139
139
|
// ->
|
|
140
|
-
enabled({
|
|
140
|
+
if (enabled({
|
|
141
141
|
...op,
|
|
142
142
|
direction: 'up'
|
|
143
|
-
})
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
143
|
+
})) {
|
|
144
|
+
logger({
|
|
145
|
+
...op,
|
|
146
|
+
direction: 'up'
|
|
147
|
+
});
|
|
148
|
+
}
|
|
147
149
|
const requestStartTime = Date.now();
|
|
148
150
|
function logResult(result) {
|
|
149
151
|
const elapsedMs = Date.now() - requestStartTime;
|
|
150
|
-
enabled({
|
|
152
|
+
if (enabled({
|
|
151
153
|
...op,
|
|
152
154
|
direction: 'down',
|
|
153
155
|
result
|
|
154
|
-
})
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
156
|
+
})) {
|
|
157
|
+
logger({
|
|
158
|
+
...op,
|
|
159
|
+
direction: 'down',
|
|
160
|
+
elapsedMs,
|
|
161
|
+
result
|
|
162
|
+
});
|
|
163
|
+
}
|
|
160
164
|
}
|
|
161
165
|
return next(op).pipe(observable.tap({
|
|
162
166
|
next (result) {
|
|
@@ -62,7 +62,7 @@ const palettes = {
|
|
|
62
62
|
}
|
|
63
63
|
};
|
|
64
64
|
function constructPartsAndArgs(opts) {
|
|
65
|
-
const { direction
|
|
65
|
+
const { direction, type, withContext, path, id, input } = opts;
|
|
66
66
|
const parts = [];
|
|
67
67
|
const args = [];
|
|
68
68
|
if (opts.colorMode === 'none') {
|
|
@@ -106,55 +106,59 @@ function constructPartsAndArgs(opts) {
|
|
|
106
106
|
};
|
|
107
107
|
}
|
|
108
108
|
// maybe this should be moved to it's own package
|
|
109
|
-
const defaultLogger = ({ c =console
|
|
109
|
+
const defaultLogger = ({ c = console, colorMode = 'css', withContext })=>(props)=>{
|
|
110
110
|
const rawInput = props.input;
|
|
111
111
|
const input = isFormData(rawInput) ? Object.fromEntries(rawInput) : rawInput;
|
|
112
|
-
const { parts
|
|
112
|
+
const { parts, args } = constructPartsAndArgs({
|
|
113
113
|
...props,
|
|
114
114
|
colorMode,
|
|
115
115
|
input,
|
|
116
116
|
withContext
|
|
117
117
|
});
|
|
118
|
-
const fn = props.direction === 'down' && props.result && (props.result instanceof Error || 'error' in props.result.result) ? 'error' : 'log';
|
|
118
|
+
const fn = props.direction === 'down' && props.result && (props.result instanceof Error || 'error' in props.result.result && props.result.result.error) ? 'error' : 'log';
|
|
119
119
|
c[fn].apply(null, [
|
|
120
120
|
parts.join(' ')
|
|
121
121
|
].concat(args));
|
|
122
122
|
};
|
|
123
123
|
/**
|
|
124
|
-
* @
|
|
124
|
+
* @see https://trpc.io/docs/v11/client/links/loggerLink
|
|
125
125
|
*/ function loggerLink(opts = {}) {
|
|
126
|
-
const { enabled =()=>true
|
|
126
|
+
const { enabled = ()=>true } = opts;
|
|
127
127
|
const colorMode = opts.colorMode ?? (typeof window === 'undefined' ? 'ansi' : 'css');
|
|
128
128
|
const withContext = opts.withContext ?? colorMode === 'css';
|
|
129
|
-
const { logger =defaultLogger({
|
|
129
|
+
const { logger = defaultLogger({
|
|
130
130
|
c: opts.console,
|
|
131
131
|
colorMode,
|
|
132
132
|
withContext
|
|
133
|
-
})
|
|
133
|
+
}) } = opts;
|
|
134
134
|
return ()=>{
|
|
135
|
-
return ({ op
|
|
135
|
+
return ({ op, next })=>{
|
|
136
136
|
return observable((observer)=>{
|
|
137
137
|
// ->
|
|
138
|
-
enabled({
|
|
138
|
+
if (enabled({
|
|
139
139
|
...op,
|
|
140
140
|
direction: 'up'
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
141
|
+
})) {
|
|
142
|
+
logger({
|
|
143
|
+
...op,
|
|
144
|
+
direction: 'up'
|
|
145
|
+
});
|
|
146
|
+
}
|
|
145
147
|
const requestStartTime = Date.now();
|
|
146
148
|
function logResult(result) {
|
|
147
149
|
const elapsedMs = Date.now() - requestStartTime;
|
|
148
|
-
enabled({
|
|
150
|
+
if (enabled({
|
|
149
151
|
...op,
|
|
150
152
|
direction: 'down',
|
|
151
153
|
result
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
154
|
+
})) {
|
|
155
|
+
logger({
|
|
156
|
+
...op,
|
|
157
|
+
direction: 'down',
|
|
158
|
+
elapsedMs,
|
|
159
|
+
result
|
|
160
|
+
});
|
|
161
|
+
}
|
|
158
162
|
}
|
|
159
163
|
return next(op).pipe(tap({
|
|
160
164
|
next (result) {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { InferrableClientTypes } from '@trpc/server/unstable-core-do-not-import';
|
|
2
|
+
import type { TRPCClientError } from '../TRPCClientError';
|
|
3
|
+
import type { Operation, TRPCLink } from './types';
|
|
4
|
+
interface RetryLinkOptions<TInferrable extends InferrableClientTypes> {
|
|
5
|
+
/**
|
|
6
|
+
* The retry function
|
|
7
|
+
*/
|
|
8
|
+
retry: (opts: RetryFnOptions<TInferrable>) => boolean;
|
|
9
|
+
}
|
|
10
|
+
interface RetryFnOptions<TInferrable extends InferrableClientTypes> {
|
|
11
|
+
/**
|
|
12
|
+
* The operation that failed
|
|
13
|
+
*/
|
|
14
|
+
op: Operation;
|
|
15
|
+
/**
|
|
16
|
+
* The error that occurred
|
|
17
|
+
*/
|
|
18
|
+
error: TRPCClientError<TInferrable>;
|
|
19
|
+
/**
|
|
20
|
+
* The number of attempts that have been made (including the first call)
|
|
21
|
+
*/
|
|
22
|
+
attempts: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @see https://trpc.io/docs/v11/client/links/retryLink
|
|
26
|
+
*/
|
|
27
|
+
export declare function retryLink<TInferrable extends InferrableClientTypes>(opts: RetryLinkOptions<TInferrable>): TRPCLink<TInferrable>;
|
|
28
|
+
export {};
|
|
29
|
+
//# sourceMappingURL=retryLink.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retryLink.d.ts","sourceRoot":"","sources":["../../src/links/retryLink.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,0CAA0C,CAAC;AAEtF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnD,UAAU,gBAAgB,CAAC,WAAW,SAAS,qBAAqB;IAClE;;OAEG;IACH,KAAK,EAAE,CAAC,IAAI,EAAE,cAAc,CAAC,WAAW,CAAC,KAAK,OAAO,CAAC;CACvD;AAED,UAAU,cAAc,CAAC,WAAW,SAAS,qBAAqB;IAChE;;OAEG;IACH,EAAE,EAAE,SAAS,CAAC;IACd;;OAEG;IACH,KAAK,EAAE,eAAe,CAAC,WAAW,CAAC,CAAC;IACpC;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,WAAW,SAAS,qBAAqB,EACjE,IAAI,EAAE,gBAAgB,CAAC,WAAW,CAAC,GAClC,QAAQ,CAAC,WAAW,CAAC,CAgEvB"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var observable = require('@trpc/server/observable');
|
|
4
|
+
var inputWithTrackedEventId = require('../internals/inputWithTrackedEventId.js');
|
|
5
|
+
|
|
6
|
+
/* istanbul ignore file -- @preserve */ // We're not actually exporting this link
|
|
7
|
+
/**
|
|
8
|
+
* @see https://trpc.io/docs/v11/client/links/retryLink
|
|
9
|
+
*/ function retryLink(opts) {
|
|
10
|
+
// initialized config
|
|
11
|
+
return ()=>{
|
|
12
|
+
// initialized in app
|
|
13
|
+
return (callOpts)=>{
|
|
14
|
+
// initialized for request
|
|
15
|
+
return observable.observable((observer)=>{
|
|
16
|
+
let next$;
|
|
17
|
+
let lastEventId = undefined;
|
|
18
|
+
attempt(1);
|
|
19
|
+
function opWithLastEventId() {
|
|
20
|
+
const op = callOpts.op;
|
|
21
|
+
if (!lastEventId) {
|
|
22
|
+
return op;
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
...op,
|
|
26
|
+
input: inputWithTrackedEventId.inputWithTrackedEventId(op.input, lastEventId)
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function attempt(attempts) {
|
|
30
|
+
const op = opWithLastEventId();
|
|
31
|
+
next$ = callOpts.next(op).subscribe({
|
|
32
|
+
error (error) {
|
|
33
|
+
const shouldRetry = opts.retry({
|
|
34
|
+
op,
|
|
35
|
+
attempts,
|
|
36
|
+
error
|
|
37
|
+
});
|
|
38
|
+
if (shouldRetry) {
|
|
39
|
+
attempt(attempts + 1);
|
|
40
|
+
} else {
|
|
41
|
+
observer.error(error);
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
next (envelope) {
|
|
45
|
+
//
|
|
46
|
+
if ((!envelope.result.type || envelope.result.type === 'data') && envelope.result.id) {
|
|
47
|
+
//
|
|
48
|
+
lastEventId = envelope.result.id;
|
|
49
|
+
}
|
|
50
|
+
observer.next(envelope);
|
|
51
|
+
},
|
|
52
|
+
complete () {
|
|
53
|
+
observer.complete();
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return ()=>{
|
|
58
|
+
next$.unsubscribe();
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
exports.retryLink = retryLink;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { observable } from '@trpc/server/observable';
|
|
2
|
+
import { inputWithTrackedEventId } from '../internals/inputWithTrackedEventId.mjs';
|
|
3
|
+
|
|
4
|
+
/* istanbul ignore file -- @preserve */ // We're not actually exporting this link
|
|
5
|
+
/**
|
|
6
|
+
* @see https://trpc.io/docs/v11/client/links/retryLink
|
|
7
|
+
*/ function retryLink(opts) {
|
|
8
|
+
// initialized config
|
|
9
|
+
return ()=>{
|
|
10
|
+
// initialized in app
|
|
11
|
+
return (callOpts)=>{
|
|
12
|
+
// initialized for request
|
|
13
|
+
return observable((observer)=>{
|
|
14
|
+
let next$;
|
|
15
|
+
let lastEventId = undefined;
|
|
16
|
+
attempt(1);
|
|
17
|
+
function opWithLastEventId() {
|
|
18
|
+
const op = callOpts.op;
|
|
19
|
+
if (!lastEventId) {
|
|
20
|
+
return op;
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
...op,
|
|
24
|
+
input: inputWithTrackedEventId(op.input, lastEventId)
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function attempt(attempts) {
|
|
28
|
+
const op = opWithLastEventId();
|
|
29
|
+
next$ = callOpts.next(op).subscribe({
|
|
30
|
+
error (error) {
|
|
31
|
+
const shouldRetry = opts.retry({
|
|
32
|
+
op,
|
|
33
|
+
attempts,
|
|
34
|
+
error
|
|
35
|
+
});
|
|
36
|
+
if (shouldRetry) {
|
|
37
|
+
attempt(attempts + 1);
|
|
38
|
+
} else {
|
|
39
|
+
observer.error(error);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
next (envelope) {
|
|
43
|
+
//
|
|
44
|
+
if ((!envelope.result.type || envelope.result.type === 'data') && envelope.result.id) {
|
|
45
|
+
//
|
|
46
|
+
lastEventId = envelope.result.id;
|
|
47
|
+
}
|
|
48
|
+
observer.next(envelope);
|
|
49
|
+
},
|
|
50
|
+
complete () {
|
|
51
|
+
observer.complete();
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return ()=>{
|
|
56
|
+
next$.unsubscribe();
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { retryLink };
|
package/dist/links/types.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { Observable, Observer } from '@trpc/server/observable';
|
|
|
2
2
|
import type { InferrableClientTypes, Maybe, TRPCResultMessage, TRPCSuccessResponse } from '@trpc/server/unstable-core-do-not-import';
|
|
3
3
|
import type { ResponseEsque } from '../internals/types';
|
|
4
4
|
import type { TRPCClientError } from '../TRPCClientError';
|
|
5
|
+
import type { TRPCConnectionState } from './internals/subscriptions';
|
|
5
6
|
export { isNonJsonSerializable, isFormData, isOctetType, } from './internals/contentTypes';
|
|
6
7
|
/**
|
|
7
8
|
* @internal
|
|
@@ -33,31 +34,11 @@ export type HTTPHeaders = HeadersInitEsque | Record<string, string[] | string |
|
|
|
33
34
|
export type TRPCFetch = (url: string, options?: RequestInit) => Promise<ResponseEsque>;
|
|
34
35
|
export interface TRPCClientRuntime {
|
|
35
36
|
}
|
|
36
|
-
export type ConnectionState = 'idle' | 'connecting' | 'pending' | 'error';
|
|
37
|
-
export interface ConnectionStateMessageBase {
|
|
38
|
-
type: 'state';
|
|
39
|
-
}
|
|
40
|
-
export interface IdleStateMessage extends ConnectionStateMessageBase {
|
|
41
|
-
state: 'idle';
|
|
42
|
-
}
|
|
43
|
-
export interface ConnectingStateMessage<TError> extends ConnectionStateMessageBase {
|
|
44
|
-
state: 'connecting';
|
|
45
|
-
data: TError | null;
|
|
46
|
-
}
|
|
47
|
-
export interface PendingStateMessage extends ConnectionStateMessageBase {
|
|
48
|
-
state: 'pending';
|
|
49
|
-
}
|
|
50
|
-
export interface ErrorStateMessage<TError> extends ConnectionStateMessageBase {
|
|
51
|
-
state: 'error';
|
|
52
|
-
data: TError;
|
|
53
|
-
}
|
|
54
|
-
export type TRPCConnectionStateMessage<TError> = IdleStateMessage | ConnectingStateMessage<TError> | PendingStateMessage | ErrorStateMessage<TError>;
|
|
55
|
-
export declare const isConnectionStateMessage: <TError>(msg: unknown) => msg is TRPCConnectionStateMessage<TError>;
|
|
56
37
|
/**
|
|
57
38
|
* @internal
|
|
58
39
|
*/
|
|
59
|
-
export interface OperationResultEnvelope<TOutput, TError
|
|
60
|
-
result:
|
|
40
|
+
export interface OperationResultEnvelope<TOutput, TError> {
|
|
41
|
+
result: TRPCResultMessage<TOutput>['result'] | TRPCSuccessResponse<TOutput>['result'] | TRPCConnectionState<TError>;
|
|
61
42
|
context?: OperationContext;
|
|
62
43
|
}
|
|
63
44
|
/**
|
|
@@ -67,7 +48,7 @@ export type OperationResultObservable<TInferrable extends InferrableClientTypes,
|
|
|
67
48
|
/**
|
|
68
49
|
* @internal
|
|
69
50
|
*/
|
|
70
|
-
export type OperationResultObserver<TInferrable extends InferrableClientTypes, TOutput> = Observer<OperationResultEnvelope<TOutput
|
|
51
|
+
export type OperationResultObserver<TInferrable extends InferrableClientTypes, TOutput> = Observer<OperationResultEnvelope<TOutput, TRPCClientError<TInferrable>>, TRPCClientError<TInferrable>>;
|
|
71
52
|
/**
|
|
72
53
|
* @internal
|
|
73
54
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/links/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACpE,OAAO,KAAK,EACV,qBAAqB,EACrB,KAAK,EACL,iBAAiB,EACjB,mBAAmB,EACpB,MAAM,0CAA0C,CAAC;AAClD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/links/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACpE,OAAO,KAAK,EACV,qBAAqB,EACrB,KAAK,EACL,iBAAiB,EACjB,mBAAmB,EACpB,MAAM,0CAA0C,CAAC;AAClD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAErE,OAAO,EACL,qBAAqB,EACrB,UAAU,EACV,WAAW,GACZ,MAAM,0BAA0B,CAAC;AAElC;;GAEG;AACH,MAAM,WAAW,gBAAiB,SAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAAG;AAEpE;;GAEG;AACH,MAAM,MAAM,SAAS,CAAC,MAAM,GAAG,OAAO,IAAI;IACxC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,UAAU,GAAG,OAAO,GAAG,cAAc,CAAC;IAC5C,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,gBAAgB,CAAC;IAC1B,MAAM,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC;CAC5B,CAAC;AAEF,UAAU,gBAAgB;IACx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