@trpc/client 11.0.0-next.91 → 11.0.0-rc.329
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/README.md +4 -4
- package/dist/TRPCClientError.d.ts +8 -7
- package/dist/TRPCClientError.d.ts.map +1 -1
- package/dist/{TRPCClientError-e224e397.js → TRPCClientError.js} +3 -3
- package/dist/{TRPCClientError-0de4d231.mjs → TRPCClientError.mjs} +4 -4
- package/dist/bundle-analysis.json +126 -155
- package/dist/createTRPCClient.d.ts +27 -19
- package/dist/createTRPCClient.d.ts.map +1 -1
- package/dist/createTRPCClient.js +50 -0
- package/dist/createTRPCClient.mjs +45 -0
- package/dist/createTRPCUntypedClient.d.ts +3 -2
- package/dist/createTRPCUntypedClient.d.ts.map +1 -1
- package/dist/createTRPCUntypedClient.js +10 -0
- package/dist/createTRPCUntypedClient.mjs +7 -0
- package/dist/getFetch.d.ts +1 -1
- package/dist/getFetch.d.ts.map +1 -1
- package/dist/getFetch.js +17 -0
- package/dist/getFetch.mjs +15 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +32 -370
- package/dist/index.mjs +9 -354
- package/dist/internals/TRPCUntypedClient.d.ts +5 -30
- package/dist/internals/TRPCUntypedClient.d.ts.map +1 -1
- package/dist/internals/TRPCUntypedClient.js +85 -0
- package/dist/internals/TRPCUntypedClient.mjs +83 -0
- package/dist/internals/dataLoader.d.ts +1 -1
- package/dist/internals/dataLoader.d.ts.map +1 -1
- package/dist/{httpBatchLink-204206a5.mjs → internals/dataLoader.js} +2 -117
- package/dist/internals/dataLoader.mjs +131 -0
- package/dist/internals/getAbortController.d.ts +2 -2
- package/dist/internals/getAbortController.d.ts.map +1 -1
- package/dist/internals/getAbortController.js +18 -0
- package/dist/internals/getAbortController.mjs +16 -0
- package/dist/internals/transformer.d.ts +42 -0
- package/dist/internals/transformer.d.ts.map +1 -0
- package/dist/internals/transformer.js +30 -0
- package/dist/internals/transformer.mjs +28 -0
- package/dist/internals/types.d.ts +4 -2
- package/dist/internals/types.d.ts.map +1 -1
- package/dist/links/HTTPBatchLinkOptions.d.ts +6 -5
- package/dist/links/HTTPBatchLinkOptions.d.ts.map +1 -1
- package/dist/links/httpBatchLink.d.ts +2 -2
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +37 -9
- package/dist/links/httpBatchLink.mjs +39 -5
- package/dist/links/httpBatchStreamLink.d.ts +6 -5
- package/dist/links/httpBatchStreamLink.d.ts.map +1 -1
- package/dist/links/httpBatchStreamLink.js +43 -0
- package/dist/links/httpBatchStreamLink.mjs +41 -0
- package/dist/links/httpFormDataLink.d.ts +1 -1
- package/dist/links/httpFormDataLink.d.ts.map +1 -1
- package/dist/links/httpFormDataLink.js +31 -0
- package/dist/links/httpFormDataLink.mjs +29 -0
- package/dist/links/httpLink.d.ts +8 -8
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +6 -9
- package/dist/links/httpLink.mjs +6 -7
- package/dist/links/internals/createChain.d.ts +2 -2
- package/dist/links/internals/createChain.d.ts.map +1 -1
- package/dist/{splitLink-f29e84be.js → links/internals/createChain.js} +0 -22
- package/dist/{splitLink-4c75f7be.mjs → links/internals/createChain.mjs} +1 -22
- package/dist/links/internals/createHTTPBatchLink.d.ts +6 -6
- package/dist/links/internals/createHTTPBatchLink.d.ts.map +1 -1
- package/dist/links/internals/createHTTPBatchLink.js +85 -0
- package/dist/links/internals/createHTTPBatchLink.mjs +83 -0
- package/dist/links/internals/dedupeLink.d.ts +2 -2
- package/dist/links/internals/dedupeLink.d.ts.map +1 -1
- package/dist/links/internals/getTextDecoder.d.ts +1 -1
- package/dist/links/internals/getTextDecoder.d.ts.map +1 -1
- package/dist/links/internals/getTextDecoder.js +18 -0
- package/dist/links/internals/getTextDecoder.mjs +16 -0
- package/dist/links/internals/httpUtils.d.ts +19 -11
- package/dist/links/internals/httpUtils.d.ts.map +1 -1
- package/dist/{httpUtils-c0e7bf5a.js → links/internals/httpUtils.js} +20 -39
- package/dist/{httpUtils-f58ceda1.mjs → links/internals/httpUtils.mjs} +20 -38
- package/dist/links/internals/parseJSONStream.d.ts +5 -6
- package/dist/links/internals/parseJSONStream.d.ts.map +1 -1
- package/dist/links/internals/parseJSONStream.js +118 -0
- package/dist/links/internals/parseJSONStream.mjs +115 -0
- package/dist/links/internals/retryLink.d.ts +2 -2
- package/dist/links/internals/retryLink.d.ts.map +1 -1
- package/dist/links/loggerLink.d.ts +4 -4
- package/dist/links/loggerLink.d.ts.map +1 -1
- package/dist/links/loggerLink.js +6 -4
- package/dist/links/loggerLink.mjs +6 -2
- package/dist/links/splitLink.d.ts +2 -2
- package/dist/links/splitLink.d.ts.map +1 -1
- package/dist/links/splitLink.js +23 -6
- package/dist/links/splitLink.mjs +25 -2
- package/dist/links/types.d.ts +15 -14
- package/dist/links/types.d.ts.map +1 -1
- package/dist/links/wsLink.d.ts +44 -13
- package/dist/links/wsLink.d.ts.map +1 -1
- package/dist/links/wsLink.js +64 -24
- package/dist/links/wsLink.mjs +64 -22
- package/dist/links.d.ts +10 -0
- package/dist/links.d.ts.map +1 -0
- package/dist/unstable-internals.d.ts +2 -0
- package/dist/unstable-internals.d.ts.map +1 -0
- package/dist/unstable-internals.js +7 -0
- package/dist/unstable-internals.mjs +1 -0
- package/package.json +26 -28
- package/src/TRPCClientError.ts +24 -13
- package/src/createTRPCClient.ts +58 -51
- package/src/createTRPCUntypedClient.ts +3 -5
- package/src/getFetch.ts +1 -1
- package/src/index.ts +2 -0
- package/src/internals/TRPCUntypedClient.ts +14 -84
- package/src/internals/dataLoader.ts +1 -1
- package/src/internals/getAbortController.ts +2 -2
- package/src/internals/transformer.ts +76 -0
- package/src/internals/types.ts +8 -1
- package/src/links/HTTPBatchLinkOptions.ts +17 -15
- package/src/links/httpBatchLink.ts +9 -8
- package/src/links/httpBatchStreamLink.ts +17 -17
- package/src/links/httpFormDataLink.ts +11 -12
- package/src/links/httpLink.ts +27 -19
- package/src/links/internals/createChain.ts +6 -2
- package/src/links/internals/createHTTPBatchLink.ts +24 -16
- package/src/links/internals/dedupeLink.ts +4 -3
- package/src/links/internals/getTextDecoder.ts +1 -1
- package/src/links/internals/httpUtils.ts +42 -21
- package/src/links/internals/parseJSONStream.ts +12 -14
- package/src/links/internals/retryLink.ts +4 -3
- package/src/links/loggerLink.ts +5 -5
- package/src/links/splitLink.ts +2 -2
- package/src/links/types.ts +27 -22
- package/src/links/wsLink.ts +124 -36
- package/src/links.ts +14 -0
- package/src/unstable-internals.ts +1 -0
- package/unstable-internals/index.d.ts +1 -0
- package/unstable-internals/index.js +1 -0
- package/dist/TRPCClientError-23c8aa93.js +0 -61
- package/dist/httpBatchLink-64fceaac.js +0 -251
- package/dist/httpBatchLink-92dab48e.js +0 -247
- package/dist/httpUtils-35e50476.js +0 -145
- package/dist/internals/isObject.d.ts +0 -2
- package/dist/internals/isObject.d.ts.map +0 -1
- package/dist/internals/retryDelay.d.ts +0 -2
- package/dist/internals/retryDelay.d.ts.map +0 -1
- package/dist/links/index.d.ts +0 -10
- package/dist/links/index.d.ts.map +0 -1
- package/dist/shared/index.d.ts +0 -2
- package/dist/shared/index.d.ts.map +0 -1
- package/dist/shared/index.js +0 -9
- package/dist/shared/index.mjs +0 -1
- package/dist/shared/transformResult.d.ts +0 -34
- package/dist/shared/transformResult.d.ts.map +0 -1
- package/dist/splitLink-0df96fdc.js +0 -41
- package/dist/transformResult-ace864b8.mjs +0 -58
- package/dist/transformResult-c1422cb5.js +0 -60
- package/dist/transformResult-dfce8f15.js +0 -61
- package/shared/index.d.ts +0 -1
- package/shared/index.js +0 -1
- package/src/internals/isObject.ts +0 -4
- package/src/internals/retryDelay.ts +0 -3
- package/src/links/index.ts +0 -14
- package/src/shared/index.ts +0 -1
- package/src/shared/transformResult.ts +0 -79
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from './internals/createHTTPBatchLink';
|
|
1
|
+
import type { AnyRootTypes } from '@trpc/server/unstable-core-do-not-import';
|
|
2
|
+
import type { NonEmptyArray } from '../internals/types';
|
|
3
|
+
import type { HTTPBatchLinkOptions } from './HTTPBatchLinkOptions';
|
|
4
|
+
import type { RequesterFn } from './internals/createHTTPBatchLink';
|
|
5
|
+
import { createHTTPBatchLink } from './internals/createHTTPBatchLink';
|
|
7
6
|
import { jsonHttpRequester } from './internals/httpUtils';
|
|
8
|
-
import { Operation } from './types';
|
|
7
|
+
import type { Operation } from './types';
|
|
9
8
|
|
|
10
|
-
const batchRequester: RequesterFn<HTTPBatchLinkOptions
|
|
9
|
+
const batchRequester: RequesterFn<HTTPBatchLinkOptions<AnyRootTypes>> = (
|
|
10
|
+
requesterOpts,
|
|
11
|
+
) => {
|
|
11
12
|
return (batchOps) => {
|
|
12
13
|
const path = batchOps.map((op) => op.path).join(',');
|
|
13
14
|
const inputs = batchOps.map((op) => op.input);
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from './internals/createHTTPBatchLink';
|
|
1
|
+
import type { AnyRootTypes } from '@trpc/server/unstable-core-do-not-import';
|
|
2
|
+
import type { NonEmptyArray } from '../internals/types';
|
|
3
|
+
import type { HTTPBatchLinkOptions } from './HTTPBatchLinkOptions';
|
|
4
|
+
import type { RequesterFn } from './internals/createHTTPBatchLink';
|
|
5
|
+
import { createHTTPBatchLink } from './internals/createHTTPBatchLink';
|
|
7
6
|
import { getTextDecoder } from './internals/getTextDecoder';
|
|
8
7
|
import { streamingJsonHttpRequester } from './internals/parseJSONStream';
|
|
9
|
-
import { TextDecoderEsque } from './internals/streamingUtils';
|
|
10
|
-
import { Operation } from './types';
|
|
8
|
+
import type { TextDecoderEsque } from './internals/streamingUtils';
|
|
9
|
+
import type { Operation } from './types';
|
|
11
10
|
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
export type HTTPBatchStreamLinkOptions<TRoot extends AnyRootTypes> =
|
|
12
|
+
HTTPBatchLinkOptions<TRoot> & {
|
|
13
|
+
/**
|
|
14
|
+
* Will default to the webAPI `TextDecoder`,
|
|
15
|
+
* but you can use this option if your client
|
|
16
|
+
* runtime doesn't provide it.
|
|
17
|
+
*/
|
|
18
|
+
textDecoder?: TextDecoderEsque;
|
|
19
|
+
};
|
|
20
20
|
|
|
21
|
-
const streamRequester: RequesterFn<HTTPBatchStreamLinkOptions
|
|
21
|
+
const streamRequester: RequesterFn<HTTPBatchStreamLinkOptions<AnyRootTypes>> = (
|
|
22
22
|
requesterOpts,
|
|
23
23
|
) => {
|
|
24
24
|
const textDecoder = getTextDecoder(requesterOpts.opts.textDecoder);
|
|
@@ -1,15 +1,6 @@
|
|
|
1
1
|
import { httpLinkFactory } from './httpLink';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
const getBody: GetBody = (opts) => {
|
|
5
|
-
if (!('input' in opts)) {
|
|
6
|
-
return undefined;
|
|
7
|
-
}
|
|
8
|
-
if (!(opts.input instanceof FormData)) {
|
|
9
|
-
throw new Error('Input is not FormData');
|
|
10
|
-
}
|
|
11
|
-
return opts.input;
|
|
12
|
-
};
|
|
2
|
+
import type { Requester } from './internals/httpUtils';
|
|
3
|
+
import { httpRequest } from './internals/httpUtils';
|
|
13
4
|
|
|
14
5
|
const formDataRequester: Requester = (opts) => {
|
|
15
6
|
if (opts.type !== 'mutation') {
|
|
@@ -21,7 +12,15 @@ const formDataRequester: Requester = (opts) => {
|
|
|
21
12
|
getUrl() {
|
|
22
13
|
return `${opts.url}/${opts.path}`;
|
|
23
14
|
},
|
|
24
|
-
getBody
|
|
15
|
+
getBody(opts) {
|
|
16
|
+
if (!('input' in opts)) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
if (!(opts.input instanceof FormData)) {
|
|
20
|
+
throw new Error('Input is not FormData');
|
|
21
|
+
}
|
|
22
|
+
return opts.input;
|
|
23
|
+
},
|
|
25
24
|
});
|
|
26
25
|
};
|
|
27
26
|
|
package/src/links/httpLink.ts
CHANGED
|
@@ -1,39 +1,44 @@
|
|
|
1
|
-
import { AnyRouter } from '@trpc/server';
|
|
2
1
|
import { observable } from '@trpc/server/observable';
|
|
3
|
-
import {
|
|
2
|
+
import type {
|
|
3
|
+
AnyRootTypes,
|
|
4
|
+
AnyRouter,
|
|
5
|
+
} from '@trpc/server/unstable-core-do-not-import';
|
|
6
|
+
import { transformResult } from '@trpc/server/unstable-core-do-not-import';
|
|
4
7
|
import { TRPCClientError } from '../TRPCClientError';
|
|
5
|
-
import {
|
|
8
|
+
import type {
|
|
6
9
|
HTTPLinkBaseOptions,
|
|
7
10
|
HTTPResult,
|
|
8
|
-
jsonHttpRequester,
|
|
9
11
|
Requester,
|
|
12
|
+
} from './internals/httpUtils';
|
|
13
|
+
import {
|
|
14
|
+
jsonHttpRequester,
|
|
10
15
|
resolveHTTPLinkOptions,
|
|
11
16
|
} from './internals/httpUtils';
|
|
12
|
-
import { HTTPHeaders, Operation, TRPCLink } from './types';
|
|
17
|
+
import type { HTTPHeaders, Operation, TRPCLink } from './types';
|
|
13
18
|
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
19
|
+
export type HTTPLinkOptions<TRoot extends AnyRootTypes> =
|
|
20
|
+
HTTPLinkBaseOptions<TRoot> & {
|
|
21
|
+
/**
|
|
22
|
+
* Headers to be set on outgoing requests or a callback that of said headers
|
|
23
|
+
* @link http://trpc.io/docs/client/headers
|
|
24
|
+
*/
|
|
25
|
+
headers?:
|
|
26
|
+
| HTTPHeaders
|
|
27
|
+
| ((opts: { op: Operation }) => HTTPHeaders | Promise<HTTPHeaders>);
|
|
28
|
+
};
|
|
23
29
|
|
|
24
30
|
export function httpLinkFactory(factoryOpts: { requester: Requester }) {
|
|
25
31
|
return <TRouter extends AnyRouter>(
|
|
26
|
-
opts: HTTPLinkOptions
|
|
32
|
+
opts: HTTPLinkOptions<TRouter['_def']['_config']['$types']>,
|
|
27
33
|
): TRPCLink<TRouter> => {
|
|
28
34
|
const resolvedOpts = resolveHTTPLinkOptions(opts);
|
|
29
35
|
|
|
30
|
-
return (
|
|
36
|
+
return () =>
|
|
31
37
|
({ op }) =>
|
|
32
38
|
observable((observer) => {
|
|
33
39
|
const { path, input, type } = op;
|
|
34
40
|
const { promise, cancel } = factoryOpts.requester({
|
|
35
41
|
...resolvedOpts,
|
|
36
|
-
runtime,
|
|
37
42
|
type,
|
|
38
43
|
path,
|
|
39
44
|
input,
|
|
@@ -53,7 +58,10 @@ export function httpLinkFactory(factoryOpts: { requester: Requester }) {
|
|
|
53
58
|
promise
|
|
54
59
|
.then((res) => {
|
|
55
60
|
meta = res.meta;
|
|
56
|
-
const transformed = transformResult(
|
|
61
|
+
const transformed = transformResult(
|
|
62
|
+
res.json,
|
|
63
|
+
resolvedOpts.transformer.output,
|
|
64
|
+
);
|
|
57
65
|
|
|
58
66
|
if (!transformed.ok) {
|
|
59
67
|
observer.error(
|
|
@@ -81,6 +89,6 @@ export function httpLinkFactory(factoryOpts: { requester: Requester }) {
|
|
|
81
89
|
}
|
|
82
90
|
|
|
83
91
|
/**
|
|
84
|
-
* @
|
|
92
|
+
* @link https://trpc.io/docs/v11/client/links/httpLink
|
|
85
93
|
*/
|
|
86
94
|
export const httpLink = httpLinkFactory({ requester: jsonHttpRequester });
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import { AnyRouter } from '@trpc/server';
|
|
2
1
|
import { observable } from '@trpc/server/observable';
|
|
3
|
-
import {
|
|
2
|
+
import type { AnyRouter } from '@trpc/server/unstable-core-do-not-import';
|
|
3
|
+
import type {
|
|
4
|
+
Operation,
|
|
5
|
+
OperationLink,
|
|
6
|
+
OperationResultObservable,
|
|
7
|
+
} from '../types';
|
|
4
8
|
|
|
5
9
|
/** @internal */
|
|
6
10
|
export function createChain<
|
|
@@ -1,21 +1,27 @@
|
|
|
1
|
-
import { AnyRouter, ProcedureType } from '@trpc/server';
|
|
2
1
|
import { observable } from '@trpc/server/observable';
|
|
2
|
+
import type {
|
|
3
|
+
AnyRootTypes,
|
|
4
|
+
AnyRouter,
|
|
5
|
+
inferClientTypes,
|
|
6
|
+
ProcedureType,
|
|
7
|
+
} from '@trpc/server/unstable-core-do-not-import';
|
|
8
|
+
import { transformResult } from '@trpc/server/unstable-core-do-not-import';
|
|
3
9
|
import { dataLoader } from '../../internals/dataLoader';
|
|
4
|
-
import { transformResult } from '../../shared/transformResult';
|
|
5
10
|
import { TRPCClientError } from '../../TRPCClientError';
|
|
6
|
-
import { HTTPBatchLinkOptions } from '../HTTPBatchLinkOptions';
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
} from './httpUtils';
|
|
11
|
+
import type { HTTPBatchLinkOptions } from '../HTTPBatchLinkOptions';
|
|
12
|
+
import type {
|
|
13
|
+
CancelFn,
|
|
14
|
+
Operation,
|
|
15
|
+
TRPCClientRuntime,
|
|
16
|
+
TRPCLink,
|
|
17
|
+
} from '../types';
|
|
18
|
+
import type { HTTPResult, ResolvedHTTPLinkOptions } from './httpUtils';
|
|
19
|
+
import { getUrl, resolveHTTPLinkOptions } from './httpUtils';
|
|
14
20
|
|
|
15
21
|
/**
|
|
16
22
|
* @internal
|
|
17
23
|
*/
|
|
18
|
-
export type RequesterFn<TOptions extends HTTPBatchLinkOptions
|
|
24
|
+
export type RequesterFn<TOptions extends HTTPBatchLinkOptions<AnyRootTypes>> = (
|
|
19
25
|
requesterOpts: ResolvedHTTPLinkOptions & {
|
|
20
26
|
runtime: TRPCClientRuntime;
|
|
21
27
|
type: ProcedureType;
|
|
@@ -32,11 +38,11 @@ export type RequesterFn<TOptions extends HTTPBatchLinkOptions> = (
|
|
|
32
38
|
/**
|
|
33
39
|
* @internal
|
|
34
40
|
*/
|
|
35
|
-
export function createHTTPBatchLink
|
|
36
|
-
requester: RequesterFn<
|
|
41
|
+
export function createHTTPBatchLink(
|
|
42
|
+
requester: RequesterFn<HTTPBatchLinkOptions<AnyRootTypes>>,
|
|
37
43
|
) {
|
|
38
44
|
return function httpBatchLink<TRouter extends AnyRouter>(
|
|
39
|
-
opts:
|
|
45
|
+
opts: HTTPBatchLinkOptions<inferClientTypes<TRouter>>,
|
|
40
46
|
): TRPCLink<TRouter> {
|
|
41
47
|
const resolvedOpts = resolveHTTPLinkOptions(opts);
|
|
42
48
|
const maxURLLength = opts.maxURLLength ?? Infinity;
|
|
@@ -54,7 +60,6 @@ export function createHTTPBatchLink<TOptions extends HTTPBatchLinkOptions>(
|
|
|
54
60
|
|
|
55
61
|
const url = getUrl({
|
|
56
62
|
...resolvedOpts,
|
|
57
|
-
runtime,
|
|
58
63
|
type,
|
|
59
64
|
path,
|
|
60
65
|
inputs,
|
|
@@ -91,7 +96,10 @@ export function createHTTPBatchLink<TOptions extends HTTPBatchLinkOptions>(
|
|
|
91
96
|
promise
|
|
92
97
|
.then((res) => {
|
|
93
98
|
_res = res;
|
|
94
|
-
const transformed = transformResult(
|
|
99
|
+
const transformed = transformResult(
|
|
100
|
+
res.json,
|
|
101
|
+
resolvedOpts.transformer.output,
|
|
102
|
+
);
|
|
95
103
|
|
|
96
104
|
if (!transformed.ok) {
|
|
97
105
|
observer.error(
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/* istanbul ignore file -- @preserve */
|
|
2
2
|
// We're not actually exporting this link
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import type { Observable } from '@trpc/server/observable';
|
|
4
|
+
import { observable, share } from '@trpc/server/observable';
|
|
5
|
+
import type { AnyRouter } from '@trpc/server/unstable-core-do-not-import';
|
|
6
|
+
import type { TRPCLink } from '../types';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* @internal used for testing
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import type {
|
|
2
|
+
AnyRootTypes,
|
|
3
|
+
CombinedDataTransformer,
|
|
4
|
+
ProcedureType,
|
|
5
|
+
TRPCResponse,
|
|
6
|
+
} from '@trpc/server/unstable-core-do-not-import';
|
|
3
7
|
import { getFetch } from '../../getFetch';
|
|
4
8
|
import { getAbortController } from '../../internals/getAbortController';
|
|
5
|
-
import {
|
|
9
|
+
import type {
|
|
6
10
|
AbortControllerEsque,
|
|
7
11
|
AbortControllerInstanceEsque,
|
|
8
12
|
FetchEsque,
|
|
@@ -10,13 +14,17 @@ import {
|
|
|
10
14
|
ResponseEsque,
|
|
11
15
|
} from '../../internals/types';
|
|
12
16
|
import { TRPCClientError } from '../../TRPCClientError';
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
17
|
+
import type { TransformerOptions } from '../../unstable-internals';
|
|
18
|
+
import { getTransformer } from '../../unstable-internals';
|
|
19
|
+
import type { TextDecoderEsque } from '../internals/streamingUtils';
|
|
20
|
+
import type { HTTPHeaders, PromiseAndCancel } from '../types';
|
|
15
21
|
|
|
16
22
|
/**
|
|
17
23
|
* @internal
|
|
18
24
|
*/
|
|
19
|
-
export
|
|
25
|
+
export type HTTPLinkBaseOptions<
|
|
26
|
+
TRoot extends Pick<AnyRootTypes, 'transformer'>,
|
|
27
|
+
> = {
|
|
20
28
|
url: string | URL;
|
|
21
29
|
/**
|
|
22
30
|
* Add ponyfill for fetch
|
|
@@ -26,21 +34,31 @@ export interface HTTPLinkBaseOptions {
|
|
|
26
34
|
* Add ponyfill for AbortController
|
|
27
35
|
*/
|
|
28
36
|
AbortController?: AbortControllerEsque | null;
|
|
29
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Send all requests `as POST`s requests regardless of the procedure type
|
|
39
|
+
* The HTTP handler must separately allow overriding the method. See:
|
|
40
|
+
* @link https://trpc.io/docs/rpc
|
|
41
|
+
*/
|
|
42
|
+
methodOverride?: 'POST';
|
|
43
|
+
} & TransformerOptions<TRoot>;
|
|
30
44
|
|
|
31
45
|
export interface ResolvedHTTPLinkOptions {
|
|
32
46
|
url: string;
|
|
33
47
|
fetch?: FetchEsque;
|
|
34
48
|
AbortController: AbortControllerEsque | null;
|
|
49
|
+
transformer: CombinedDataTransformer;
|
|
50
|
+
methodOverride?: 'POST';
|
|
35
51
|
}
|
|
36
52
|
|
|
37
53
|
export function resolveHTTPLinkOptions(
|
|
38
|
-
opts: HTTPLinkBaseOptions
|
|
54
|
+
opts: HTTPLinkBaseOptions<AnyRootTypes>,
|
|
39
55
|
): ResolvedHTTPLinkOptions {
|
|
40
56
|
return {
|
|
41
57
|
url: opts.url.toString().replace(/\/$/, ''), // Remove any trailing slashes
|
|
42
58
|
fetch: opts.fetch,
|
|
43
59
|
AbortController: getAbortController(opts.AbortController),
|
|
60
|
+
transformer: getTransformer(opts.transformer),
|
|
61
|
+
methodOverride: opts.methodOverride,
|
|
44
62
|
};
|
|
45
63
|
}
|
|
46
64
|
|
|
@@ -68,14 +86,14 @@ export interface HTTPResult {
|
|
|
68
86
|
}
|
|
69
87
|
|
|
70
88
|
type GetInputOptions = {
|
|
71
|
-
|
|
89
|
+
transformer: CombinedDataTransformer;
|
|
72
90
|
} & ({ input: unknown } | { inputs: unknown[] });
|
|
73
91
|
|
|
74
92
|
function getInput(opts: GetInputOptions) {
|
|
75
93
|
return 'input' in opts
|
|
76
|
-
? opts.
|
|
94
|
+
? opts.transformer.input.serialize(opts.input)
|
|
77
95
|
: arrayToDict(
|
|
78
|
-
opts.inputs.map((_input) => opts.
|
|
96
|
+
opts.inputs.map((_input) => opts.transformer.input.serialize(_input)),
|
|
79
97
|
);
|
|
80
98
|
}
|
|
81
99
|
|
|
@@ -85,11 +103,8 @@ export type HTTPBaseRequestOptions = GetInputOptions &
|
|
|
85
103
|
path: string;
|
|
86
104
|
};
|
|
87
105
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
opts: HTTPBaseRequestOptions,
|
|
91
|
-
) => RequestInitEsque['body'];
|
|
92
|
-
|
|
106
|
+
type GetUrl = (opts: HTTPBaseRequestOptions) => string;
|
|
107
|
+
type GetBody = (opts: HTTPBaseRequestOptions) => RequestInitEsque['body'];
|
|
93
108
|
export type ContentOptions = {
|
|
94
109
|
batchModeHeader?: 'stream';
|
|
95
110
|
contentTypeHeader?: string;
|
|
@@ -105,7 +120,7 @@ export const getUrl: GetUrl = (opts) => {
|
|
|
105
120
|
}
|
|
106
121
|
if (opts.type === 'query') {
|
|
107
122
|
const input = getInput(opts);
|
|
108
|
-
if (input !== undefined) {
|
|
123
|
+
if (input !== undefined && opts.methodOverride !== 'POST') {
|
|
109
124
|
queryParts.push(`input=${encodeURIComponent(JSON.stringify(input))}`);
|
|
110
125
|
}
|
|
111
126
|
}
|
|
@@ -116,7 +131,7 @@ export const getUrl: GetUrl = (opts) => {
|
|
|
116
131
|
};
|
|
117
132
|
|
|
118
133
|
export const getBody: GetBody = (opts) => {
|
|
119
|
-
if (opts.type === 'query') {
|
|
134
|
+
if (opts.type === 'query' && opts.methodOverride !== 'POST') {
|
|
120
135
|
return undefined;
|
|
121
136
|
}
|
|
122
137
|
const input = getInput(opts);
|
|
@@ -151,7 +166,13 @@ export async function fetchHTTPResponse(
|
|
|
151
166
|
const url = opts.getUrl(opts);
|
|
152
167
|
const body = opts.getBody(opts);
|
|
153
168
|
const { type } = opts;
|
|
154
|
-
const resolvedHeaders = await
|
|
169
|
+
const resolvedHeaders = await (async () => {
|
|
170
|
+
const heads = await opts.headers();
|
|
171
|
+
if (Symbol.iterator in heads) {
|
|
172
|
+
return Object.fromEntries(heads);
|
|
173
|
+
}
|
|
174
|
+
return heads;
|
|
175
|
+
})();
|
|
155
176
|
/* istanbul ignore if -- @preserve */
|
|
156
177
|
if (type === 'subscription') {
|
|
157
178
|
throw new Error('Subscriptions should use wsLink');
|
|
@@ -167,9 +188,9 @@ export async function fetchHTTPResponse(
|
|
|
167
188
|
};
|
|
168
189
|
|
|
169
190
|
return getFetch(opts.fetch)(url, {
|
|
170
|
-
method: METHOD[type],
|
|
191
|
+
method: opts.methodOverride ?? METHOD[type],
|
|
171
192
|
signal: ac?.signal,
|
|
172
|
-
body
|
|
193
|
+
body,
|
|
173
194
|
headers,
|
|
174
195
|
});
|
|
175
196
|
}
|
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
// Stream parsing adapted from https://www.loginradius.com/blog/engineering/guest-post/http-streaming-with-nodejs-and-fetch-api/
|
|
2
|
-
import { TRPCResponse } from '@trpc/server/
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from './httpUtils';
|
|
12
|
-
import { TextDecoderEsque } from './streamingUtils';
|
|
2
|
+
import type { TRPCResponse } from '@trpc/server/unstable-core-do-not-import';
|
|
3
|
+
import type {
|
|
4
|
+
NodeJSReadableStreamEsque,
|
|
5
|
+
WebReadableStreamEsque,
|
|
6
|
+
} from '../../internals/types';
|
|
7
|
+
import type { HTTPHeaders } from '../types';
|
|
8
|
+
import type { HTTPBaseRequestOptions, HTTPResult } from './httpUtils';
|
|
9
|
+
import { fetchHTTPResponse, getBody, getUrl } from './httpUtils';
|
|
10
|
+
import type { TextDecoderEsque } from './streamingUtils';
|
|
13
11
|
|
|
14
12
|
/**
|
|
15
13
|
* @internal
|
|
@@ -26,7 +24,7 @@ export async function parseJSONStream<TReturn>(opts: {
|
|
|
26
24
|
/**
|
|
27
25
|
* As given by `(await fetch(url)).body`
|
|
28
26
|
*/
|
|
29
|
-
readableStream:
|
|
27
|
+
readableStream: NodeJSReadableStreamEsque | WebReadableStreamEsque;
|
|
30
28
|
/**
|
|
31
29
|
* Called for each line of the stream
|
|
32
30
|
*/
|
|
@@ -69,7 +67,7 @@ export async function parseJSONStream<TReturn>(opts: {
|
|
|
69
67
|
* @param onLine will be called for every line ('\n' delimited) in the stream
|
|
70
68
|
*/
|
|
71
69
|
async function readLines(
|
|
72
|
-
readableStream:
|
|
70
|
+
readableStream: NodeJSReadableStreamEsque | WebReadableStreamEsque,
|
|
73
71
|
onLine: (line: string) => void,
|
|
74
72
|
textDecoder: TextDecoderEsque,
|
|
75
73
|
) {
|
|
@@ -106,7 +104,7 @@ async function readLines(
|
|
|
106
104
|
* Handle NodeJS stream
|
|
107
105
|
*/
|
|
108
106
|
function readNodeChunks(
|
|
109
|
-
stream:
|
|
107
|
+
stream: NodeJSReadableStreamEsque,
|
|
110
108
|
onChunk: (chunk: Uint8Array) => void,
|
|
111
109
|
) {
|
|
112
110
|
return new Promise<void>((resolve) => {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/* istanbul ignore file -- @preserve */
|
|
2
2
|
// We're not actually exporting this link
|
|
3
|
-
import {
|
|
4
|
-
import { observable
|
|
5
|
-
import {
|
|
3
|
+
import type { Unsubscribable } from '@trpc/server/observable';
|
|
4
|
+
import { observable } from '@trpc/server/observable';
|
|
5
|
+
import type { AnyRouter } from '@trpc/server/unstable-core-do-not-import';
|
|
6
|
+
import type { TRPCLink } from '../types';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* @internal used for testing
|
package/src/links/loggerLink.ts
CHANGED
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
// Using triple-slash directive makes sure that it will be available,
|
|
6
6
|
// even if end-user `tsconfig.json` omits it in the `lib` array.
|
|
7
7
|
|
|
8
|
-
import { AnyRouter } from '@trpc/server';
|
|
9
8
|
import { observable, tap } from '@trpc/server/observable';
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
9
|
+
import type { AnyRouter } from '@trpc/server/unstable-core-do-not-import';
|
|
10
|
+
import type { TRPCClientError } from '../TRPCClientError';
|
|
11
|
+
import type { Operation, OperationResultEnvelope, TRPCLink } from './types';
|
|
12
12
|
|
|
13
13
|
type ConsoleEsque = {
|
|
14
14
|
log: (...args: any[]) => void;
|
|
@@ -135,7 +135,7 @@ function constructPartsAndArgs(
|
|
|
135
135
|
|
|
136
136
|
const [light, dark] = palettes.css[type];
|
|
137
137
|
const css = `
|
|
138
|
-
background-color: #${direction === 'up' ? light : dark};
|
|
138
|
+
background-color: #${direction === 'up' ? light : dark};
|
|
139
139
|
color: ${direction === 'up' ? 'black' : 'white'};
|
|
140
140
|
padding: 2px;
|
|
141
141
|
`;
|
|
@@ -196,7 +196,7 @@ const defaultLogger =
|
|
|
196
196
|
};
|
|
197
197
|
|
|
198
198
|
/**
|
|
199
|
-
* @
|
|
199
|
+
* @link https://trpc.io/docs/v11/client/links/loggerLink
|
|
200
200
|
*/
|
|
201
201
|
export function loggerLink<TRouter extends AnyRouter = AnyRouter>(
|
|
202
202
|
opts: LoggerLinkOptions<TRouter> = {},
|
package/src/links/splitLink.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { AnyRouter } from '@trpc/server';
|
|
2
1
|
import { observable } from '@trpc/server/observable';
|
|
2
|
+
import type { AnyRouter } from '@trpc/server/unstable-core-do-not-import';
|
|
3
3
|
import { createChain } from './internals/createChain';
|
|
4
|
-
import { Operation, TRPCLink } from './types';
|
|
4
|
+
import type { Operation, TRPCLink } from './types';
|
|
5
5
|
|
|
6
6
|
function asArray<TType>(value: TType | TType[]) {
|
|
7
7
|
return Array.isArray(value) ? value : [value];
|
package/src/links/types.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { TRPCClientError } from '../TRPCClientError';
|
|
1
|
+
import type { Observable, Observer } from '@trpc/server/observable';
|
|
2
|
+
import type {
|
|
3
|
+
InferrableClientTypes,
|
|
4
|
+
TRPCResultMessage,
|
|
5
|
+
TRPCSuccessResponse,
|
|
6
|
+
} from '@trpc/server/unstable-core-do-not-import';
|
|
7
|
+
import type { ResponseEsque } from '../internals/types';
|
|
8
|
+
import type { TRPCClientError } from '../TRPCClientError';
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
11
|
* @internal
|
|
@@ -37,10 +36,16 @@ export type Operation<TInput = unknown> = {
|
|
|
37
36
|
context: OperationContext;
|
|
38
37
|
};
|
|
39
38
|
|
|
39
|
+
interface HeadersInitEsque {
|
|
40
|
+
[Symbol.iterator](): IterableIterator<[string, string]>;
|
|
41
|
+
}
|
|
42
|
+
|
|
40
43
|
/**
|
|
41
44
|
* @internal
|
|
42
45
|
*/
|
|
43
|
-
export type HTTPHeaders =
|
|
46
|
+
export type HTTPHeaders =
|
|
47
|
+
| HeadersInitEsque
|
|
48
|
+
| Record<string, string[] | string | undefined>;
|
|
44
49
|
|
|
45
50
|
/**
|
|
46
51
|
* The default `fetch` implementation has an overloaded signature. By convention this library
|
|
@@ -52,9 +57,7 @@ export type TRPCFetch = (
|
|
|
52
57
|
) => Promise<ResponseEsque>;
|
|
53
58
|
|
|
54
59
|
export interface TRPCClientRuntime {
|
|
55
|
-
|
|
56
|
-
// FIXME: we should be able to remove this - added as `withTRPC()` needs it, but we can have it as an extra option on SSR instead
|
|
57
|
-
combinedTransformer: CombinedDataTransformer;
|
|
60
|
+
// nothing here anymore
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
/**
|
|
@@ -71,33 +74,35 @@ export interface OperationResultEnvelope<TOutput> {
|
|
|
71
74
|
* @internal
|
|
72
75
|
*/
|
|
73
76
|
export type OperationResultObservable<
|
|
74
|
-
|
|
77
|
+
TInferrable extends InferrableClientTypes,
|
|
75
78
|
TOutput,
|
|
76
|
-
> = Observable<OperationResultEnvelope<TOutput>, TRPCClientError<
|
|
79
|
+
> = Observable<OperationResultEnvelope<TOutput>, TRPCClientError<TInferrable>>;
|
|
77
80
|
|
|
78
81
|
/**
|
|
79
82
|
* @internal
|
|
80
83
|
*/
|
|
81
84
|
export type OperationResultObserver<
|
|
82
|
-
|
|
85
|
+
TInferrable extends InferrableClientTypes,
|
|
83
86
|
TOutput,
|
|
84
|
-
> = Observer<OperationResultEnvelope<TOutput>, TRPCClientError<
|
|
87
|
+
> = Observer<OperationResultEnvelope<TOutput>, TRPCClientError<TInferrable>>;
|
|
85
88
|
|
|
86
89
|
/**
|
|
87
90
|
* @internal
|
|
88
91
|
*/
|
|
89
92
|
export type OperationLink<
|
|
90
|
-
|
|
93
|
+
TInferrable extends InferrableClientTypes,
|
|
91
94
|
TInput = unknown,
|
|
92
95
|
TOutput = unknown,
|
|
93
96
|
> = (opts: {
|
|
94
97
|
op: Operation<TInput>;
|
|
95
|
-
next: (
|
|
96
|
-
|
|
98
|
+
next: (
|
|
99
|
+
op: Operation<TInput>,
|
|
100
|
+
) => OperationResultObservable<TInferrable, TOutput>;
|
|
101
|
+
}) => OperationResultObservable<TInferrable, TOutput>;
|
|
97
102
|
|
|
98
103
|
/**
|
|
99
104
|
* @public
|
|
100
105
|
*/
|
|
101
|
-
export type TRPCLink<
|
|
106
|
+
export type TRPCLink<TInferrable extends InferrableClientTypes> = (
|
|
102
107
|
opts: TRPCClientRuntime,
|
|
103
|
-
) => OperationLink<
|
|
108
|
+
) => OperationLink<TInferrable>;
|