@trpc/client 10.32.0 → 10.33.1
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/createTRPCClient.d.ts +1 -1
- package/dist/createTRPCClient.d.ts.map +1 -1
- package/dist/createTRPCClientProxy.d.ts +1 -1
- package/dist/createTRPCClientProxy.d.ts.map +1 -1
- package/dist/getFetch.d.ts.map +1 -1
- package/dist/{httpBatchLink-38dcf5f2.js → httpBatchLink-76c6f2d4.js} +9 -6
- package/dist/{httpBatchLink-e66c0674.mjs → httpBatchLink-c184c799.mjs} +9 -6
- package/dist/{httpBatchLink-f35df745.js → httpBatchLink-c64f6cdb.js} +9 -6
- package/dist/{httpUtils-145e783b.js → httpUtils-a0ac1597.js} +15 -18
- package/dist/{httpUtils-b2fcc20c.mjs → httpUtils-a9242d4e.mjs} +15 -18
- package/dist/{httpUtils-21cbb35d.js → httpUtils-fa0d6f71.js} +15 -18
- package/dist/index.js +5 -3
- package/dist/index.mjs +7 -5
- package/dist/internals/TRPCUntypedClient.d.ts +2 -2
- package/dist/internals/TRPCUntypedClient.d.ts.map +1 -1
- package/dist/internals/dataLoader.d.ts.map +1 -1
- package/dist/internals/types.d.ts +4 -6
- package/dist/internals/types.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +2 -2
- package/dist/links/httpBatchLink.mjs +2 -2
- package/dist/links/httpBatchStreamLink.d.ts.map +1 -1
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +4 -2
- package/dist/links/httpLink.mjs +4 -2
- package/dist/links/internals/createHTTPBatchLink.d.ts.map +1 -1
- package/dist/links/internals/httpUtils.d.ts +5 -5
- package/dist/links/internals/httpUtils.d.ts.map +1 -1
- package/dist/links/internals/parseJSONStream.d.ts +1 -1
- package/dist/links/internals/parseJSONStream.d.ts.map +1 -1
- package/dist/links/loggerLink.d.ts +14 -9
- package/dist/links/loggerLink.d.ts.map +1 -1
- package/dist/links/loggerLink.js +109 -46
- package/dist/links/loggerLink.mjs +109 -46
- package/dist/links/types.d.ts +3 -3
- package/dist/links/types.d.ts.map +1 -1
- package/dist/links/wsLink.js +1 -1
- package/dist/links/wsLink.mjs +1 -1
- package/dist/shared/transformResult.d.ts +2 -2
- package/dist/shared/transformResult.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/TRPCClientError.ts +1 -1
- package/src/createTRPCClient.ts +2 -2
- package/src/createTRPCClientProxy.ts +7 -7
- package/src/getFetch.ts +2 -7
- package/src/internals/TRPCUntypedClient.ts +10 -8
- package/src/internals/dataLoader.ts +1 -2
- package/src/internals/types.ts +4 -6
- package/src/links/httpBatchStreamLink.ts +3 -1
- package/src/links/httpLink.ts +3 -1
- package/src/links/internals/createHTTPBatchLink.ts +7 -3
- package/src/links/internals/httpUtils.ts +19 -18
- package/src/links/internals/parseJSONStream.ts +2 -2
- package/src/links/loggerLink.ts +124 -51
- package/src/links/types.ts +4 -4
- package/src/links/wsLink.ts +2 -2
- package/src/shared/transformResult.ts +4 -4
package/src/getFetch.ts
CHANGED
|
@@ -4,11 +4,6 @@ type AnyFn = (...args: any[]) => unknown;
|
|
|
4
4
|
|
|
5
5
|
const isFunction = (fn: unknown): fn is AnyFn => typeof fn === 'function';
|
|
6
6
|
|
|
7
|
-
function _bind(fn: AnyFn, thisArg: unknown): AnyFn {
|
|
8
|
-
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
9
|
-
return isFunction(fn.bind) ? fn.bind(thisArg) : fn;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
7
|
export function getFetch(
|
|
13
8
|
customFetchImpl?: FetchEsque | NativeFetchEsque,
|
|
14
9
|
): FetchEsque {
|
|
@@ -17,11 +12,11 @@ export function getFetch(
|
|
|
17
12
|
}
|
|
18
13
|
|
|
19
14
|
if (typeof window !== 'undefined' && isFunction(window.fetch)) {
|
|
20
|
-
return
|
|
15
|
+
return window.fetch as FetchEsque;
|
|
21
16
|
}
|
|
22
17
|
|
|
23
18
|
if (typeof globalThis !== 'undefined' && isFunction(globalThis.fetch)) {
|
|
24
|
-
return
|
|
19
|
+
return globalThis.fetch as FetchEsque;
|
|
25
20
|
}
|
|
26
21
|
|
|
27
22
|
throw new Error('No fetch implementation found');
|
|
@@ -55,7 +55,7 @@ type CreateTRPCClientBaseOptions<TRouter extends AnyRouter> =
|
|
|
55
55
|
| CombinedDataTransformer;
|
|
56
56
|
};
|
|
57
57
|
|
|
58
|
-
type TRPCType = '
|
|
58
|
+
type TRPCType = 'mutation' | 'query' | 'subscription';
|
|
59
59
|
export interface TRPCRequestOptions {
|
|
60
60
|
/**
|
|
61
61
|
* Pass additional context to links
|
|
@@ -80,13 +80,13 @@ export type CreateTRPCClientOptions<TRouter extends AnyRouter> =
|
|
|
80
80
|
|
|
81
81
|
/** @internal */
|
|
82
82
|
export type UntypedClientProperties =
|
|
83
|
-
| 'links'
|
|
84
|
-
| 'runtime'
|
|
85
|
-
| 'requestId'
|
|
86
83
|
| '$request'
|
|
87
|
-
| '
|
|
88
|
-
| 'query'
|
|
84
|
+
| 'links'
|
|
89
85
|
| 'mutation'
|
|
86
|
+
| 'query'
|
|
87
|
+
| 'requestAsPromise'
|
|
88
|
+
| 'requestId'
|
|
89
|
+
| 'runtime'
|
|
90
90
|
| 'subscription';
|
|
91
91
|
|
|
92
92
|
export class TRPCUntypedClient<TRouter extends AnyRouter> {
|
|
@@ -204,8 +204,10 @@ export class TRPCUntypedClient<TRouter extends AnyRouter> {
|
|
|
204
204
|
public subscription(
|
|
205
205
|
path: string,
|
|
206
206
|
input: unknown,
|
|
207
|
-
opts:
|
|
208
|
-
|
|
207
|
+
opts: Partial<
|
|
208
|
+
TRPCSubscriptionObserver<unknown, TRPCClientError<AnyRouter>>
|
|
209
|
+
> &
|
|
210
|
+
TRPCRequestOptions,
|
|
209
211
|
): Unsubscribable {
|
|
210
212
|
const observable$ = this.$request({
|
|
211
213
|
type: 'subscription',
|
|
@@ -126,8 +126,7 @@ export function dataLoader<TKey, TValue>(
|
|
|
126
126
|
const value = result[i]!;
|
|
127
127
|
unitResolver(i, value);
|
|
128
128
|
}
|
|
129
|
-
for (
|
|
130
|
-
const item = batch.items[i]!;
|
|
129
|
+
for (const item of batch.items) {
|
|
131
130
|
item.reject?.(new Error('Missing result'));
|
|
132
131
|
item.batch = null;
|
|
133
132
|
}
|
package/src/internals/types.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
new (): AbortControllerInstanceEsque;
|
|
3
|
-
}
|
|
1
|
+
export type AbortControllerEsque = new () => AbortControllerInstanceEsque;
|
|
4
2
|
|
|
5
3
|
/**
|
|
6
4
|
* Allows you to abort one or more requests.
|
|
@@ -35,7 +33,7 @@ export type FetchEsque = (
|
|
|
35
33
|
* their own fetch types, such as undici and node-fetch.
|
|
36
34
|
*/
|
|
37
35
|
export type NativeFetchEsque = (
|
|
38
|
-
url:
|
|
36
|
+
url: URL | string,
|
|
39
37
|
init?: NodeFetchRequestInitEsque,
|
|
40
38
|
) => Promise<ResponseEsque>;
|
|
41
39
|
|
|
@@ -54,7 +52,7 @@ export interface RequestInitEsque {
|
|
|
54
52
|
/**
|
|
55
53
|
* Sets the request's body.
|
|
56
54
|
*/
|
|
57
|
-
body?:
|
|
55
|
+
body?: FormData | ReadableStream | string | null;
|
|
58
56
|
|
|
59
57
|
/**
|
|
60
58
|
* Sets the request's associated headers.
|
|
@@ -85,7 +83,7 @@ export type WebReadableStreamEsque = {
|
|
|
85
83
|
* @see Response from lib.dom.d.ts
|
|
86
84
|
*/
|
|
87
85
|
export interface ResponseEsque {
|
|
88
|
-
readonly body?:
|
|
86
|
+
readonly body?: NodeJS.ReadableStream | WebReadableStreamEsque | null;
|
|
89
87
|
/**
|
|
90
88
|
* @remarks
|
|
91
89
|
* The built-in Response::json() method returns Promise<any>, but
|
package/src/links/httpLink.ts
CHANGED
|
@@ -66,7 +66,9 @@ export function httpLinkFactory(factoryOpts: { requester: Requester }) {
|
|
|
66
66
|
});
|
|
67
67
|
observer.complete();
|
|
68
68
|
})
|
|
69
|
-
.catch((cause) =>
|
|
69
|
+
.catch((cause) => {
|
|
70
|
+
observer.error(TRPCClientError.from(cause));
|
|
71
|
+
});
|
|
70
72
|
|
|
71
73
|
return () => {
|
|
72
74
|
cancel();
|
|
@@ -39,7 +39,7 @@ export function createHTTPBatchLink<TOptions extends HTTPBatchLinkOptions>(
|
|
|
39
39
|
opts: TOptions,
|
|
40
40
|
): TRPCLink<TRouter> {
|
|
41
41
|
const resolvedOpts = resolveHTTPLinkOptions(opts);
|
|
42
|
-
const maxURLLength = opts.maxURLLength
|
|
42
|
+
const maxURLLength = opts.maxURLLength ?? Infinity;
|
|
43
43
|
|
|
44
44
|
// initialized config
|
|
45
45
|
return (runtime) => {
|
|
@@ -105,9 +105,13 @@ export function createHTTPBatchLink<TOptions extends HTTPBatchLinkOptions>(
|
|
|
105
105
|
});
|
|
106
106
|
observer.complete();
|
|
107
107
|
})
|
|
108
|
-
.catch((err) =>
|
|
108
|
+
.catch((err) => {
|
|
109
|
+
observer.error(TRPCClientError.from(err));
|
|
110
|
+
});
|
|
109
111
|
|
|
110
|
-
return () =>
|
|
112
|
+
return () => {
|
|
113
|
+
cancel();
|
|
114
|
+
};
|
|
111
115
|
});
|
|
112
116
|
};
|
|
113
117
|
};
|
|
@@ -29,7 +29,7 @@ export interface HTTPLinkBaseOptions {
|
|
|
29
29
|
|
|
30
30
|
export interface ResolvedHTTPLinkOptions {
|
|
31
31
|
url: string;
|
|
32
|
-
fetch
|
|
32
|
+
fetch?: FetchEsque;
|
|
33
33
|
AbortController: AbortControllerEsque | null;
|
|
34
34
|
}
|
|
35
35
|
|
|
@@ -38,7 +38,7 @@ export function resolveHTTPLinkOptions(
|
|
|
38
38
|
): ResolvedHTTPLinkOptions {
|
|
39
39
|
return {
|
|
40
40
|
url: opts.url,
|
|
41
|
-
fetch:
|
|
41
|
+
fetch: opts.fetch,
|
|
42
42
|
AbortController: getAbortController(opts.AbortController),
|
|
43
43
|
};
|
|
44
44
|
}
|
|
@@ -67,7 +67,7 @@ export interface HTTPResult {
|
|
|
67
67
|
|
|
68
68
|
type GetInputOptions = {
|
|
69
69
|
runtime: TRPCClientRuntime;
|
|
70
|
-
} & ({
|
|
70
|
+
} & ({ input: unknown } | { inputs: unknown[] });
|
|
71
71
|
|
|
72
72
|
function getInput(opts: GetInputOptions) {
|
|
73
73
|
return 'input' in opts
|
|
@@ -77,8 +77,8 @@ function getInput(opts: GetInputOptions) {
|
|
|
77
77
|
);
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
export type HTTPBaseRequestOptions =
|
|
81
|
-
|
|
80
|
+
export type HTTPBaseRequestOptions = GetInputOptions &
|
|
81
|
+
ResolvedHTTPLinkOptions & {
|
|
82
82
|
type: ProcedureType;
|
|
83
83
|
path: string;
|
|
84
84
|
};
|
|
@@ -136,8 +136,8 @@ export const jsonHttpRequester: Requester = (opts) => {
|
|
|
136
136
|
});
|
|
137
137
|
};
|
|
138
138
|
|
|
139
|
-
export type HTTPRequestOptions =
|
|
140
|
-
|
|
139
|
+
export type HTTPRequestOptions = ContentOptions &
|
|
140
|
+
HTTPBaseRequestOptions & {
|
|
141
141
|
headers: () => HTTPHeaders | Promise<HTTPHeaders>;
|
|
142
142
|
TextDecoder?: TextDecoderEsque;
|
|
143
143
|
};
|
|
@@ -149,25 +149,26 @@ export async function fetchHTTPResponse(
|
|
|
149
149
|
const url = opts.getUrl(opts);
|
|
150
150
|
const body = opts.getBody(opts);
|
|
151
151
|
const { type } = opts;
|
|
152
|
-
const
|
|
152
|
+
const resolvedHeaders = await opts.headers();
|
|
153
153
|
/* istanbul ignore if -- @preserve */
|
|
154
154
|
if (type === 'subscription') {
|
|
155
155
|
throw new Error('Subscriptions should use wsLink');
|
|
156
156
|
}
|
|
157
|
+
const headers = {
|
|
158
|
+
...(opts.contentTypeHeader
|
|
159
|
+
? { 'content-type': opts.contentTypeHeader }
|
|
160
|
+
: {}),
|
|
161
|
+
...(opts.batchModeHeader
|
|
162
|
+
? { 'trpc-batch-mode': opts.batchModeHeader }
|
|
163
|
+
: {}),
|
|
164
|
+
...resolvedHeaders,
|
|
165
|
+
};
|
|
157
166
|
|
|
158
|
-
return opts.fetch(url, {
|
|
167
|
+
return getFetch(opts.fetch)(url, {
|
|
159
168
|
method: METHOD[type],
|
|
160
169
|
signal: ac?.signal,
|
|
161
170
|
body: body,
|
|
162
|
-
headers
|
|
163
|
-
...(opts.contentTypeHeader
|
|
164
|
-
? { 'content-type': opts.contentTypeHeader }
|
|
165
|
-
: {}),
|
|
166
|
-
...(opts.batchModeHeader
|
|
167
|
-
? { 'trpc-batch-mode': opts.batchModeHeader }
|
|
168
|
-
: {}),
|
|
169
|
-
...headers,
|
|
170
|
-
},
|
|
171
|
+
headers,
|
|
171
172
|
});
|
|
172
173
|
}
|
|
173
174
|
|
|
@@ -26,7 +26,7 @@ export async function parseJSONStream<TReturn>(opts: {
|
|
|
26
26
|
/**
|
|
27
27
|
* As given by `(await fetch(url)).body`
|
|
28
28
|
*/
|
|
29
|
-
readableStream:
|
|
29
|
+
readableStream: NodeJS.ReadableStream | WebReadableStreamEsque;
|
|
30
30
|
/**
|
|
31
31
|
* Called for each line of the stream
|
|
32
32
|
*/
|
|
@@ -69,7 +69,7 @@ export async function parseJSONStream<TReturn>(opts: {
|
|
|
69
69
|
* @param onLine will be called for every line ('\n' delimited) in the stream
|
|
70
70
|
*/
|
|
71
71
|
async function readLines(
|
|
72
|
-
readableStream:
|
|
72
|
+
readableStream: NodeJS.ReadableStream | WebReadableStreamEsque,
|
|
73
73
|
onLine: (line: string) => void,
|
|
74
74
|
textDecoder: TextDecoderEsque,
|
|
75
75
|
) {
|
package/src/links/loggerLink.ts
CHANGED
|
@@ -16,25 +16,19 @@ type ConsoleEsque = {
|
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
type EnableFnOptions<TRouter extends AnyRouter> =
|
|
19
|
-
| (Operation & {
|
|
20
|
-
direction: 'up';
|
|
21
|
-
})
|
|
22
19
|
| {
|
|
23
20
|
direction: 'down';
|
|
24
21
|
result: OperationResultEnvelope<unknown> | TRPCClientError<TRouter>;
|
|
25
|
-
}
|
|
22
|
+
}
|
|
23
|
+
| (Operation & {
|
|
24
|
+
direction: 'up';
|
|
25
|
+
});
|
|
26
26
|
type EnabledFn<TRouter extends AnyRouter> = (
|
|
27
27
|
opts: EnableFnOptions<TRouter>,
|
|
28
28
|
) => boolean;
|
|
29
29
|
|
|
30
30
|
type LoggerLinkFnOptions<TRouter extends AnyRouter> = Operation &
|
|
31
31
|
(
|
|
32
|
-
| {
|
|
33
|
-
/**
|
|
34
|
-
* Request was just initialized
|
|
35
|
-
*/
|
|
36
|
-
direction: 'up';
|
|
37
|
-
}
|
|
38
32
|
| {
|
|
39
33
|
/**
|
|
40
34
|
* Request result
|
|
@@ -43,17 +37,18 @@ type LoggerLinkFnOptions<TRouter extends AnyRouter> = Operation &
|
|
|
43
37
|
result: OperationResultEnvelope<unknown> | TRPCClientError<TRouter>;
|
|
44
38
|
elapsedMs: number;
|
|
45
39
|
}
|
|
40
|
+
| {
|
|
41
|
+
/**
|
|
42
|
+
* Request was just initialized
|
|
43
|
+
*/
|
|
44
|
+
direction: 'up';
|
|
45
|
+
}
|
|
46
46
|
);
|
|
47
47
|
|
|
48
48
|
type LoggerLinkFn<TRouter extends AnyRouter> = (
|
|
49
49
|
opts: LoggerLinkFnOptions<TRouter>,
|
|
50
50
|
) => void;
|
|
51
51
|
|
|
52
|
-
const palette = {
|
|
53
|
-
query: ['72e3ff', '3fb0d8'],
|
|
54
|
-
mutation: ['c5a3fc', '904dfc'],
|
|
55
|
-
subscription: ['ff49e1', 'd83fbe'],
|
|
56
|
-
};
|
|
57
52
|
export interface LoggerLinkOptions<TRouter extends AnyRouter> {
|
|
58
53
|
logger?: LoggerLinkFn<TRouter>;
|
|
59
54
|
enabled?: EnabledFn<TRouter>;
|
|
@@ -61,6 +56,11 @@ export interface LoggerLinkOptions<TRouter extends AnyRouter> {
|
|
|
61
56
|
* Used in the built-in defaultLogger
|
|
62
57
|
*/
|
|
63
58
|
console?: ConsoleEsque;
|
|
59
|
+
/**
|
|
60
|
+
* Color mode
|
|
61
|
+
* @default typeof window === 'undefined' ? 'ansi' : 'css'
|
|
62
|
+
*/
|
|
63
|
+
colorMode?: 'ansi' | 'css';
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
function isFormData(value: unknown): value is FormData {
|
|
@@ -71,50 +71,120 @@ function isFormData(value: unknown): value is FormData {
|
|
|
71
71
|
return value instanceof FormData;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
const palettes = {
|
|
75
|
+
css: {
|
|
76
|
+
query: ['72e3ff', '3fb0d8'],
|
|
77
|
+
mutation: ['c5a3fc', '904dfc'],
|
|
78
|
+
subscription: ['ff49e1', 'd83fbe'],
|
|
79
|
+
},
|
|
80
|
+
ansi: {
|
|
81
|
+
regular: {
|
|
82
|
+
// Cyan background, black and white text respectively
|
|
83
|
+
query: ['\x1b[30;46m', '\x1b[97;46m'],
|
|
84
|
+
// Magenta background, black and white text respectively
|
|
85
|
+
mutation: ['\x1b[30;45m', '\x1b[97;45m'],
|
|
86
|
+
// Green background, black and white text respectively
|
|
87
|
+
subscription: ['\x1b[30;42m', '\x1b[97;42m'],
|
|
88
|
+
},
|
|
89
|
+
bold: {
|
|
90
|
+
query: ['\x1b[1;30;46m', '\x1b[1;97;46m'],
|
|
91
|
+
mutation: ['\x1b[1;30;45m', '\x1b[1;97;45m'],
|
|
92
|
+
subscription: ['\x1b[1;30;42m', '\x1b[1;97;42m'],
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
} as const;
|
|
96
|
+
|
|
97
|
+
function constructPartsAndArgs(
|
|
98
|
+
opts: LoggerLinkFnOptions<any> & {
|
|
99
|
+
colorMode: 'ansi' | 'css';
|
|
100
|
+
},
|
|
101
|
+
) {
|
|
102
|
+
const { direction, type, path, id, input } = opts;
|
|
103
|
+
|
|
104
|
+
const parts: string[] = [];
|
|
105
|
+
const args: any[] = [];
|
|
106
|
+
|
|
107
|
+
if (opts.colorMode === 'ansi') {
|
|
108
|
+
const [lightRegular, darkRegular] = palettes.ansi.regular[type];
|
|
109
|
+
const [lightBold, darkBold] = palettes.ansi.bold[type];
|
|
110
|
+
const reset = '\x1b[0m';
|
|
111
|
+
|
|
112
|
+
parts.push(
|
|
113
|
+
direction === 'up' ? lightRegular : darkRegular,
|
|
114
|
+
direction === 'up' ? '>>' : '<<',
|
|
115
|
+
type,
|
|
116
|
+
direction === 'up' ? lightBold : darkBold,
|
|
117
|
+
`#${id}`,
|
|
118
|
+
path,
|
|
119
|
+
reset,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
if (direction === 'up') {
|
|
123
|
+
args.push({ input: opts.input });
|
|
124
|
+
} else {
|
|
125
|
+
args.push({
|
|
126
|
+
input: opts.input,
|
|
127
|
+
// strip context from result cause it's too noisy in terminal wihtout collapse mode
|
|
128
|
+
result: 'result' in opts.result ? opts.result.result : opts.result,
|
|
129
|
+
elapsedMs: opts.elapsedMs,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return { parts, args };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const [light, dark] = palettes.css[type];
|
|
137
|
+
const css = `
|
|
138
|
+
background-color: #${direction === 'up' ? light : dark};
|
|
139
|
+
color: ${direction === 'up' ? 'black' : 'white'};
|
|
140
|
+
padding: 2px;
|
|
141
|
+
`;
|
|
142
|
+
|
|
143
|
+
parts.push(
|
|
144
|
+
'%c',
|
|
145
|
+
direction === 'up' ? '>>' : '<<',
|
|
146
|
+
type,
|
|
147
|
+
`#${id}`,
|
|
148
|
+
`%c${path}%c`,
|
|
149
|
+
'%O',
|
|
150
|
+
);
|
|
151
|
+
args.push(css, `${css}; font-weight: bold;`, `${css}; font-weight: normal;`);
|
|
152
|
+
|
|
153
|
+
if (direction === 'up') {
|
|
154
|
+
args.push({ input, context: opts.context });
|
|
155
|
+
} else {
|
|
156
|
+
args.push({
|
|
157
|
+
input,
|
|
158
|
+
result: opts.result,
|
|
159
|
+
elapsedMs: opts.elapsedMs,
|
|
160
|
+
context: opts.context,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return { parts, args };
|
|
165
|
+
}
|
|
166
|
+
|
|
74
167
|
// maybe this should be moved to it's own package
|
|
75
168
|
const defaultLogger =
|
|
76
|
-
<TRouter extends AnyRouter>(
|
|
77
|
-
c
|
|
78
|
-
|
|
169
|
+
<TRouter extends AnyRouter>({
|
|
170
|
+
c = console,
|
|
171
|
+
colorMode = 'css',
|
|
172
|
+
}: {
|
|
173
|
+
c?: ConsoleEsque;
|
|
174
|
+
colorMode?: 'ansi' | 'css';
|
|
175
|
+
}): LoggerLinkFn<TRouter> =>
|
|
79
176
|
(props) => {
|
|
80
|
-
const { direction, type, path, context, id } = props;
|
|
81
|
-
const [light, dark] = palette[type];
|
|
82
|
-
|
|
83
177
|
const rawInput = props.input;
|
|
84
|
-
|
|
85
178
|
const input = isFormData(rawInput)
|
|
86
179
|
? Object.fromEntries(rawInput)
|
|
87
180
|
: rawInput;
|
|
88
181
|
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
182
|
+
const { parts, args } = constructPartsAndArgs({
|
|
183
|
+
...props,
|
|
184
|
+
colorMode,
|
|
185
|
+
input,
|
|
186
|
+
});
|
|
94
187
|
|
|
95
|
-
const parts = [
|
|
96
|
-
'%c',
|
|
97
|
-
direction === 'up' ? '>>' : '<<',
|
|
98
|
-
type,
|
|
99
|
-
`#${id}`,
|
|
100
|
-
`%c${path}%c`,
|
|
101
|
-
'%O',
|
|
102
|
-
];
|
|
103
|
-
const args: any[] = [
|
|
104
|
-
css,
|
|
105
|
-
`${css}; font-weight: bold;`,
|
|
106
|
-
`${css}; font-weight: normal;`,
|
|
107
|
-
];
|
|
108
|
-
if (props.direction === 'up') {
|
|
109
|
-
args.push({ input, context: context });
|
|
110
|
-
} else {
|
|
111
|
-
args.push({
|
|
112
|
-
input,
|
|
113
|
-
result: props.result,
|
|
114
|
-
elapsedMs: props.elapsedMs,
|
|
115
|
-
context,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
188
|
const fn: 'error' | 'log' =
|
|
119
189
|
props.direction === 'down' &&
|
|
120
190
|
props.result &&
|
|
@@ -124,12 +194,15 @@ const defaultLogger =
|
|
|
124
194
|
|
|
125
195
|
c[fn].apply(null, [parts.join(' ')].concat(args));
|
|
126
196
|
};
|
|
197
|
+
|
|
127
198
|
export function loggerLink<TRouter extends AnyRouter = AnyRouter>(
|
|
128
199
|
opts: LoggerLinkOptions<TRouter> = {},
|
|
129
200
|
): TRPCLink<TRouter> {
|
|
130
201
|
const { enabled = () => true } = opts;
|
|
131
202
|
|
|
132
|
-
const
|
|
203
|
+
const colorMode =
|
|
204
|
+
opts.colorMode ?? (typeof window === 'undefined' ? 'ansi' : 'css');
|
|
205
|
+
const { logger = defaultLogger({ c: opts.console, colorMode }) } = opts;
|
|
133
206
|
|
|
134
207
|
return () => {
|
|
135
208
|
return ({ op, next }) => {
|
package/src/links/types.ts
CHANGED
|
@@ -31,7 +31,7 @@ export interface OperationContext extends Record<string, unknown> {}
|
|
|
31
31
|
*/
|
|
32
32
|
export type Operation<TInput = unknown> = {
|
|
33
33
|
id: number;
|
|
34
|
-
type: '
|
|
34
|
+
type: 'mutation' | 'query' | 'subscription';
|
|
35
35
|
input: TInput;
|
|
36
36
|
path: string;
|
|
37
37
|
context: OperationContext;
|
|
@@ -40,7 +40,7 @@ export type Operation<TInput = unknown> = {
|
|
|
40
40
|
/**
|
|
41
41
|
* @internal
|
|
42
42
|
*/
|
|
43
|
-
export type HTTPHeaders = Record<string, string | string
|
|
43
|
+
export type HTTPHeaders = Record<string, string[] | string | undefined>;
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
46
|
* The default `fetch` implementation has an overloaded signature. By convention this library
|
|
@@ -62,8 +62,8 @@ export interface TRPCClientRuntime {
|
|
|
62
62
|
*/
|
|
63
63
|
export interface OperationResultEnvelope<TOutput> {
|
|
64
64
|
result:
|
|
65
|
-
|
|
|
66
|
-
|
|
|
65
|
+
| TRPCResultMessage<TOutput>['result']
|
|
66
|
+
| TRPCSuccessResponse<TOutput>['result'];
|
|
67
67
|
context?: OperationContext;
|
|
68
68
|
}
|
|
69
69
|
|
package/src/links/wsLink.ts
CHANGED
|
@@ -67,7 +67,7 @@ export function createWSClient(opts: WebSocketClientOptions) {
|
|
|
67
67
|
let dispatchTimer: NodeJS.Timer | number | null = null;
|
|
68
68
|
let connectTimer: NodeJS.Timer | number | null = null;
|
|
69
69
|
let activeConnection = createWS();
|
|
70
|
-
let state: '
|
|
70
|
+
let state: 'closed' | 'connecting' | 'open' = 'connecting';
|
|
71
71
|
/**
|
|
72
72
|
* tries to send the list of messages
|
|
73
73
|
*/
|
|
@@ -90,7 +90,7 @@ export function createWSClient(opts: WebSocketClientOptions) {
|
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
92
|
function tryReconnect() {
|
|
93
|
-
if (connectTimer || state === 'closed') {
|
|
93
|
+
if (connectTimer !== null || state === 'closed') {
|
|
94
94
|
return;
|
|
95
95
|
}
|
|
96
96
|
const timeout = retryDelayFn(connectAttempt++);
|
|
@@ -14,8 +14,8 @@ import { TRPCClientError } from '../TRPCClientError';
|
|
|
14
14
|
/** @internal */
|
|
15
15
|
function transformResultInner<TRouter extends AnyRouter, TOutput>(
|
|
16
16
|
response:
|
|
17
|
-
|
|
|
18
|
-
|
|
|
17
|
+
| TRPCResponse<TOutput, inferRouterError<TRouter>>
|
|
18
|
+
| TRPCResponseMessage<TOutput, inferRouterError<TRouter>>,
|
|
19
19
|
runtime: TRPCClientRuntime,
|
|
20
20
|
) {
|
|
21
21
|
if ('error' in response) {
|
|
@@ -47,8 +47,8 @@ function transformResultInner<TRouter extends AnyRouter, TOutput>(
|
|
|
47
47
|
*/
|
|
48
48
|
export function transformResult<TRouter extends AnyRouter, TOutput>(
|
|
49
49
|
response:
|
|
50
|
-
|
|
|
51
|
-
|
|
|
50
|
+
| TRPCResponse<TOutput, inferRouterError<TRouter>>
|
|
51
|
+
| TRPCResponseMessage<TOutput, inferRouterError<TRouter>>,
|
|
52
52
|
runtime: TRPCClientRuntime,
|
|
53
53
|
): ReturnType<typeof transformResultInner> {
|
|
54
54
|
let result: ReturnType<typeof transformResultInner>;
|