@trpc/client 10.0.0-proxy-alpha.76 → 10.0.0-proxy-alpha.77
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 +3 -3
- package/dist/TRPCClientError.d.ts.map +1 -1
- package/dist/createTRPCClient.d.ts +22 -3
- package/dist/createTRPCClient.d.ts.map +1 -1
- package/dist/createTRPCClientProxy.d.ts +7 -15
- package/dist/createTRPCClientProxy.d.ts.map +1 -1
- package/dist/{httpUtils-ad4258b6.mjs → httpUtils-5b17f7da.mjs} +36 -5
- package/dist/{httpUtils-0961d5e3.js → httpUtils-73a50af6.js} +37 -4
- package/dist/index.js +106 -138
- package/dist/index.mjs +107 -139
- package/dist/internals/TRPCClient.d.ts +5 -31
- package/dist/internals/TRPCClient.d.ts.map +1 -1
- package/dist/internals/fetchHelpers.d.ts +2 -1
- package/dist/internals/fetchHelpers.d.ts.map +1 -1
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +4 -3
- package/dist/links/httpBatchLink.mjs +4 -3
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +3 -3
- package/dist/links/httpLink.mjs +3 -3
- package/dist/links/internals/httpUtils.d.ts +26 -2
- package/dist/links/internals/httpUtils.d.ts.map +1 -1
- package/dist/links/types.d.ts +30 -3
- package/dist/links/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/TRPCClientError.ts +3 -3
- package/src/createTRPCClient.ts +35 -5
- package/src/createTRPCClientProxy.ts +17 -15
- package/src/internals/TRPCClient.ts +12 -78
- package/src/internals/fetchHelpers.ts +5 -3
- package/src/links/httpBatchLink.ts +10 -2
- package/src/links/httpLink.ts +7 -3
- package/src/links/internals/httpUtils.ts +44 -6
- package/src/links/types.ts +30 -3
package/src/TRPCClientError.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
|
+
AnyProcedure,
|
|
2
3
|
AnyRouter,
|
|
3
4
|
DefaultErrorShape,
|
|
4
5
|
Maybe,
|
|
5
|
-
Procedure,
|
|
6
6
|
inferRouterError,
|
|
7
7
|
} from '@trpc/server';
|
|
8
8
|
import { TRPCErrorResponse } from '@trpc/server/rpc';
|
|
9
9
|
|
|
10
|
-
type RouterOrProcedure = AnyRouter |
|
|
10
|
+
type RouterOrProcedure = AnyRouter | AnyProcedure;
|
|
11
11
|
|
|
12
|
-
type inferErrorShape<TRouterOrProcedure extends
|
|
12
|
+
type inferErrorShape<TRouterOrProcedure extends RouterOrProcedure> =
|
|
13
13
|
TRouterOrProcedure extends AnyRouter
|
|
14
14
|
? inferRouterError<TRouterOrProcedure>
|
|
15
15
|
: TRouterOrProcedure['_def']['_config']['errorShape'];
|
package/src/createTRPCClient.ts
CHANGED
|
@@ -1,27 +1,57 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
2
|
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
4
|
-
import type { AnyRouter } from '@trpc/server';
|
|
4
|
+
import type { AnyRouter, ClientDataTransformerOptions } from '@trpc/server';
|
|
5
5
|
import {
|
|
6
6
|
TRPCClient as Client,
|
|
7
7
|
CreateTRPCClientOptions,
|
|
8
8
|
} from './internals/TRPCClient';
|
|
9
|
+
import { httpBatchLink } from './links';
|
|
10
|
+
import { HTTPLinkOptions } from './links/internals/httpUtils';
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* @deprecated use `createTRPCProxyClient` instead
|
|
12
14
|
*/
|
|
13
15
|
export function createTRPCClient<TRouter extends AnyRouter>(
|
|
14
|
-
opts:
|
|
16
|
+
opts:
|
|
17
|
+
| CreateTRPCClientOptions<TRouter>
|
|
18
|
+
| {
|
|
19
|
+
transformer?: ClientDataTransformerOptions;
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated use `links` instead
|
|
22
|
+
*/
|
|
23
|
+
url: string;
|
|
24
|
+
/**
|
|
25
|
+
* @deprecated use `links` instead
|
|
26
|
+
*/
|
|
27
|
+
headers?: HTTPLinkOptions['headers'];
|
|
28
|
+
/**
|
|
29
|
+
* @deprecated use `links` instead
|
|
30
|
+
*/
|
|
31
|
+
fetch?: HTTPLinkOptions['fetch'];
|
|
32
|
+
/**
|
|
33
|
+
* @deprecated use `links` instead
|
|
34
|
+
*/
|
|
35
|
+
AbortController?: HTTPLinkOptions['AbortController'];
|
|
36
|
+
},
|
|
15
37
|
) {
|
|
16
|
-
const
|
|
38
|
+
const getLinks = () => {
|
|
39
|
+
if ('links' in opts) {
|
|
40
|
+
return opts.links;
|
|
41
|
+
}
|
|
42
|
+
return [httpBatchLink(opts)];
|
|
43
|
+
};
|
|
44
|
+
const client = new Client<TRouter>({
|
|
45
|
+
transformer: opts.transformer,
|
|
46
|
+
links: getLinks(),
|
|
47
|
+
});
|
|
17
48
|
return client;
|
|
18
49
|
}
|
|
19
50
|
|
|
20
51
|
// Also the client created above needs to somehow be like `TRPCClient<Router> & Omit<Router, 'createCaller' | 'createProcedure' | '_def' | 'transformer' | 'errorFormatter' | 'getErrorShape>`
|
|
21
52
|
|
|
22
53
|
export type {
|
|
23
|
-
TRPCRequestOptions,
|
|
24
54
|
CreateTRPCClientOptions,
|
|
25
55
|
TRPCClient,
|
|
26
|
-
|
|
56
|
+
TRPCRequestOptions,
|
|
27
57
|
} from './internals/TRPCClient';
|
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
4
4
|
import type {
|
|
5
|
+
AnyMutationProcedure,
|
|
6
|
+
AnyProcedure,
|
|
7
|
+
AnyQueryProcedure,
|
|
5
8
|
AnyRouter,
|
|
6
|
-
|
|
9
|
+
AnySubscriptionProcedure,
|
|
7
10
|
ProcedureArgs,
|
|
8
11
|
ProcedureRouterRecord,
|
|
9
12
|
ProcedureType,
|
|
@@ -13,23 +16,24 @@ import type {
|
|
|
13
16
|
Unsubscribable,
|
|
14
17
|
inferObservableValue,
|
|
15
18
|
} from '@trpc/server/observable';
|
|
16
|
-
import {
|
|
19
|
+
import { createProxy } from '@trpc/server/shared';
|
|
17
20
|
import { TRPCClientError } from './TRPCClientError';
|
|
18
21
|
import { CreateTRPCClientOptions, createTRPCClient } from './createTRPCClient';
|
|
19
22
|
import {
|
|
20
23
|
TRPCClient as Client,
|
|
24
|
+
TRPCClient,
|
|
21
25
|
TRPCSubscriptionObserver,
|
|
22
26
|
} from './internals/TRPCClient';
|
|
23
27
|
|
|
24
28
|
export type inferRouterProxyClient<TRouter extends AnyRouter> =
|
|
25
29
|
DecoratedProcedureRecord<TRouter['_def']['record'], TRouter>;
|
|
26
30
|
|
|
27
|
-
type Resolver<TProcedure extends
|
|
31
|
+
type Resolver<TProcedure extends AnyProcedure> = (
|
|
28
32
|
...args: ProcedureArgs<TProcedure['_def']>
|
|
29
33
|
) => Promise<inferProcedureOutput<TProcedure>>;
|
|
30
34
|
|
|
31
35
|
type SubscriptionResolver<
|
|
32
|
-
TProcedure extends
|
|
36
|
+
TProcedure extends AnyProcedure,
|
|
33
37
|
TRouter extends AnyRouter,
|
|
34
38
|
> = (
|
|
35
39
|
...args: [
|
|
@@ -45,24 +49,22 @@ type SubscriptionResolver<
|
|
|
45
49
|
) => Unsubscribable;
|
|
46
50
|
|
|
47
51
|
type DecorateProcedure<
|
|
48
|
-
TProcedure extends
|
|
52
|
+
TProcedure extends AnyProcedure,
|
|
49
53
|
TRouter extends AnyRouter,
|
|
50
|
-
> = TProcedure extends
|
|
54
|
+
> = TProcedure extends AnyQueryProcedure
|
|
51
55
|
? {
|
|
52
56
|
query: Resolver<TProcedure>;
|
|
53
57
|
}
|
|
54
|
-
: TProcedure extends
|
|
58
|
+
: TProcedure extends AnyMutationProcedure
|
|
55
59
|
? {
|
|
56
60
|
mutate: Resolver<TProcedure>;
|
|
57
61
|
}
|
|
58
|
-
: TProcedure extends
|
|
62
|
+
: TProcedure extends AnySubscriptionProcedure
|
|
59
63
|
? {
|
|
60
64
|
subscribe: SubscriptionResolver<TProcedure, TRouter>;
|
|
61
65
|
}
|
|
62
66
|
: never;
|
|
63
67
|
|
|
64
|
-
type assertProcedure<T> = T extends Procedure<any> ? T : never;
|
|
65
|
-
|
|
66
68
|
/**
|
|
67
69
|
* @internal
|
|
68
70
|
*/
|
|
@@ -70,14 +72,14 @@ type DecoratedProcedureRecord<
|
|
|
70
72
|
TProcedures extends ProcedureRouterRecord,
|
|
71
73
|
TRouter extends AnyRouter,
|
|
72
74
|
> = {
|
|
73
|
-
[TKey in keyof TProcedures]: TProcedures[TKey] extends
|
|
74
|
-
? never
|
|
75
|
-
: TProcedures[TKey] extends AnyRouter
|
|
75
|
+
[TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter
|
|
76
76
|
? DecoratedProcedureRecord<
|
|
77
77
|
TProcedures[TKey]['_def']['record'],
|
|
78
78
|
TProcedures[TKey]
|
|
79
79
|
>
|
|
80
|
-
:
|
|
80
|
+
: TProcedures[TKey] extends AnyProcedure
|
|
81
|
+
? DecorateProcedure<TProcedures[TKey], TRouter>
|
|
82
|
+
: never;
|
|
81
83
|
};
|
|
82
84
|
|
|
83
85
|
const clientCallTypeMap: Record<
|
|
@@ -110,7 +112,7 @@ export function createTRPCClientProxy<TRouter extends AnyRouter>(
|
|
|
110
112
|
export function createTRPCProxyClient<TRouter extends AnyRouter>(
|
|
111
113
|
opts: CreateTRPCClientOptions<TRouter>,
|
|
112
114
|
) {
|
|
113
|
-
const client =
|
|
115
|
+
const client = new TRPCClient<TRouter>(opts);
|
|
114
116
|
const proxy = createTRPCClientProxy(client);
|
|
115
117
|
return proxy;
|
|
116
118
|
}
|
|
@@ -2,11 +2,11 @@ import {
|
|
|
2
2
|
AnyRouter,
|
|
3
3
|
ClientDataTransformerOptions,
|
|
4
4
|
DataTransformer,
|
|
5
|
+
ProcedureRecord,
|
|
5
6
|
inferProcedureInput,
|
|
6
7
|
inferProcedureOutput,
|
|
7
8
|
inferSubscriptionOutput,
|
|
8
9
|
} from '@trpc/server';
|
|
9
|
-
import { ProcedureRecord } from '@trpc/server';
|
|
10
10
|
import {
|
|
11
11
|
Unsubscribable,
|
|
12
12
|
inferObservableValue,
|
|
@@ -14,32 +14,15 @@ import {
|
|
|
14
14
|
share,
|
|
15
15
|
} from '@trpc/server/observable';
|
|
16
16
|
import { TRPCClientError } from '../TRPCClientError';
|
|
17
|
-
import { getFetch } from '../getFetch';
|
|
18
|
-
import { httpBatchLink } from '../links';
|
|
19
17
|
import { createChain } from '../links/internals/createChain';
|
|
20
18
|
import {
|
|
21
|
-
HTTPHeaders,
|
|
22
19
|
OperationContext,
|
|
23
20
|
OperationLink,
|
|
24
21
|
TRPCClientRuntime,
|
|
25
22
|
TRPCLink,
|
|
26
23
|
} from '../links/types';
|
|
27
|
-
import { getAbortController } from './fetchHelpers';
|
|
28
24
|
|
|
29
25
|
interface CreateTRPCClientBaseOptions {
|
|
30
|
-
/**
|
|
31
|
-
* Add ponyfill for fetch
|
|
32
|
-
*/
|
|
33
|
-
fetch?: typeof fetch;
|
|
34
|
-
/**
|
|
35
|
-
* Add ponyfill for AbortController
|
|
36
|
-
*/
|
|
37
|
-
AbortController?: typeof AbortController;
|
|
38
|
-
/**
|
|
39
|
-
* Headers to be set on outgoing requests or a callback that of said headers
|
|
40
|
-
* @link http://localhost:3000/docs/v10/header
|
|
41
|
-
*/
|
|
42
|
-
headers?: HTTPHeaders | (() => HTTPHeaders | Promise<HTTPHeaders>);
|
|
43
26
|
/**
|
|
44
27
|
* Data transformer
|
|
45
28
|
* @link https://trpc.io/docs/data-transformers
|
|
@@ -69,32 +52,9 @@ export interface TRPCSubscriptionObserver<TValue, TError> {
|
|
|
69
52
|
* @internal
|
|
70
53
|
*/
|
|
71
54
|
export type CreateTRPCClientOptions<TRouter extends AnyRouter> =
|
|
72
|
-
| CreateTRPCClientBaseOptions &
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
links: TRPCLink<TRouter>[];
|
|
76
|
-
}
|
|
77
|
-
| {
|
|
78
|
-
url: string;
|
|
79
|
-
links?: never;
|
|
80
|
-
}
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
export type AssertType<T, K> = T extends K ? T : never;
|
|
84
|
-
/**
|
|
85
|
-
* @deprecated
|
|
86
|
-
*/
|
|
87
|
-
export type AssertLegacyDef<TRouter extends AnyRouter> =
|
|
88
|
-
TRouter['_def']['legacy'] extends Record<
|
|
89
|
-
'subscriptions' | 'queries' | 'mutations',
|
|
90
|
-
ProcedureRecord
|
|
91
|
-
>
|
|
92
|
-
? TRouter['_def']['legacy']
|
|
93
|
-
: {
|
|
94
|
-
subscriptions: {};
|
|
95
|
-
queries: {};
|
|
96
|
-
mutations: {};
|
|
97
|
-
};
|
|
55
|
+
| CreateTRPCClientBaseOptions & {
|
|
56
|
+
links: TRPCLink<TRouter>[];
|
|
57
|
+
};
|
|
98
58
|
|
|
99
59
|
export class TRPCClient<TRouter extends AnyRouter> {
|
|
100
60
|
private readonly links: OperationLink<TRouter>[];
|
|
@@ -105,18 +65,8 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
105
65
|
}
|
|
106
66
|
|
|
107
67
|
constructor(opts: CreateTRPCClientOptions<TRouter>) {
|
|
108
|
-
const _fetch = getFetch(opts?.fetch);
|
|
109
|
-
const AC = getAbortController(opts?.AbortController);
|
|
110
68
|
this.requestId = 0;
|
|
111
69
|
|
|
112
|
-
function getHeadersFn(): TRPCClientRuntime['headers'] {
|
|
113
|
-
if (opts.headers) {
|
|
114
|
-
const headers = opts.headers;
|
|
115
|
-
return typeof headers === 'function' ? headers : () => headers;
|
|
116
|
-
}
|
|
117
|
-
return () => ({});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
70
|
function getTransformer(): DataTransformer {
|
|
121
71
|
if (!opts.transformer)
|
|
122
72
|
return {
|
|
@@ -132,20 +82,10 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
132
82
|
}
|
|
133
83
|
|
|
134
84
|
this.runtime = {
|
|
135
|
-
AbortController: AC,
|
|
136
|
-
fetch: _fetch,
|
|
137
|
-
headers: getHeadersFn(),
|
|
138
85
|
transformer: getTransformer(),
|
|
139
86
|
};
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
if (opts.links) {
|
|
143
|
-
return opts.links.map((link) => link(this.runtime));
|
|
144
|
-
}
|
|
145
|
-
return [httpBatchLink({ url: opts.url })(this.runtime)];
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
this.links = getLinks();
|
|
87
|
+
// Initialize the links
|
|
88
|
+
this.links = opts.links.map((link) => link(this.runtime));
|
|
149
89
|
}
|
|
150
90
|
|
|
151
91
|
private $request<TInput = unknown, TOutput = unknown>({
|
|
@@ -197,11 +137,9 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
197
137
|
return abortablePromise;
|
|
198
138
|
}
|
|
199
139
|
public query<
|
|
200
|
-
TQueries extends
|
|
140
|
+
TQueries extends TRouter['_def']['queries'],
|
|
201
141
|
TPath extends string & keyof TQueries,
|
|
202
|
-
TInput extends inferProcedureInput<
|
|
203
|
-
AssertType<TQueries, ProcedureRecord>[TPath]
|
|
204
|
-
>,
|
|
142
|
+
TInput extends inferProcedureInput<TQueries[TPath]>,
|
|
205
143
|
>(path: TPath, input?: TInput, opts?: TRPCRequestOptions) {
|
|
206
144
|
type TOutput = inferProcedureOutput<TQueries[TPath]>;
|
|
207
145
|
return this.requestAsPromise<TInput, TOutput>({
|
|
@@ -213,11 +151,9 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
213
151
|
});
|
|
214
152
|
}
|
|
215
153
|
public mutation<
|
|
216
|
-
TMutations extends
|
|
154
|
+
TMutations extends TRouter['_def']['mutations'],
|
|
217
155
|
TPath extends string & keyof TMutations,
|
|
218
|
-
TInput extends inferProcedureInput<
|
|
219
|
-
AssertType<TMutations, ProcedureRecord>[TPath]
|
|
220
|
-
>,
|
|
156
|
+
TInput extends inferProcedureInput<TMutations[TPath]>,
|
|
221
157
|
>(path: TPath, input?: TInput, opts?: TRPCRequestOptions) {
|
|
222
158
|
type TOutput = inferProcedureOutput<TMutations[TPath]>;
|
|
223
159
|
return this.requestAsPromise<TInput, TOutput>({
|
|
@@ -229,12 +165,10 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
229
165
|
});
|
|
230
166
|
}
|
|
231
167
|
public subscription<
|
|
232
|
-
TSubscriptions extends
|
|
168
|
+
TSubscriptions extends TRouter['_def']['subscriptions'],
|
|
233
169
|
TPath extends string & keyof TSubscriptions,
|
|
234
170
|
TOutput extends inferSubscriptionOutput<TRouter, TPath>,
|
|
235
|
-
TInput extends inferProcedureInput<
|
|
236
|
-
AssertType<TSubscriptions, ProcedureRecord>[TPath]
|
|
237
|
-
>,
|
|
171
|
+
TInput extends inferProcedureInput<TSubscriptions[TPath]>,
|
|
238
172
|
>(
|
|
239
173
|
path: TPath,
|
|
240
174
|
input: TInput,
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Maybe } from '@trpc/server';
|
|
2
|
+
|
|
1
3
|
export function getWindow() {
|
|
2
4
|
if (typeof window !== 'undefined') {
|
|
3
5
|
return window;
|
|
@@ -5,7 +7,7 @@ export function getWindow() {
|
|
|
5
7
|
return globalThis;
|
|
6
8
|
}
|
|
7
9
|
export function getAbortController(
|
|
8
|
-
ac
|
|
9
|
-
): typeof AbortController |
|
|
10
|
-
return ac ?? getWindow().AbortController ??
|
|
10
|
+
ac: Maybe<typeof AbortController>,
|
|
11
|
+
): typeof AbortController | null {
|
|
12
|
+
return ac ?? getWindow().AbortController ?? null;
|
|
11
13
|
}
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
HTTPResult,
|
|
8
8
|
getUrl,
|
|
9
9
|
httpRequest,
|
|
10
|
+
resolveHTTPLinkOptions,
|
|
10
11
|
} from './internals/httpUtils';
|
|
11
12
|
import { transformResult } from './internals/transformResult';
|
|
12
13
|
import { TRPCLink } from './types';
|
|
@@ -18,6 +19,7 @@ export interface HttpBatchLinkOptions extends HTTPLinkOptions {
|
|
|
18
19
|
export function httpBatchLink<TRouter extends AnyRouter>(
|
|
19
20
|
opts: HttpBatchLinkOptions,
|
|
20
21
|
): TRPCLink<TRouter> {
|
|
22
|
+
const resolvedOpts = resolveHTTPLinkOptions(opts);
|
|
21
23
|
// initialized config
|
|
22
24
|
return (runtime) => {
|
|
23
25
|
type BatchOperation = { id: number; path: string; input: unknown };
|
|
@@ -33,7 +35,13 @@ export function httpBatchLink<TRouter extends AnyRouter>(
|
|
|
33
35
|
const path = batchOps.map((op) => op.path).join(',');
|
|
34
36
|
const inputs = batchOps.map((op) => op.input);
|
|
35
37
|
|
|
36
|
-
const url = getUrl({
|
|
38
|
+
const url = getUrl({
|
|
39
|
+
...resolvedOpts,
|
|
40
|
+
runtime,
|
|
41
|
+
type,
|
|
42
|
+
path,
|
|
43
|
+
inputs,
|
|
44
|
+
});
|
|
37
45
|
return url.length <= maxURLLength;
|
|
38
46
|
};
|
|
39
47
|
|
|
@@ -42,7 +50,7 @@ export function httpBatchLink<TRouter extends AnyRouter>(
|
|
|
42
50
|
const inputs = batchOps.map((op) => op.input);
|
|
43
51
|
|
|
44
52
|
const { promise, cancel } = httpRequest({
|
|
45
|
-
|
|
53
|
+
...resolvedOpts,
|
|
46
54
|
runtime,
|
|
47
55
|
type,
|
|
48
56
|
path,
|
package/src/links/httpLink.ts
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
import { AnyRouter } from '@trpc/server';
|
|
2
2
|
import { observable } from '@trpc/server/observable';
|
|
3
3
|
import { TRPCClientError } from '../TRPCClientError';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
HTTPLinkOptions,
|
|
6
|
+
httpRequest,
|
|
7
|
+
resolveHTTPLinkOptions,
|
|
8
|
+
} from './internals/httpUtils';
|
|
5
9
|
import { transformResult } from './internals/transformResult';
|
|
6
10
|
import { TRPCLink } from './types';
|
|
7
11
|
|
|
8
12
|
export function httpLink<TRouter extends AnyRouter>(
|
|
9
13
|
opts: HTTPLinkOptions,
|
|
10
14
|
): TRPCLink<TRouter> {
|
|
11
|
-
const
|
|
15
|
+
const resolvedOpts = resolveHTTPLinkOptions(opts);
|
|
12
16
|
return (runtime) =>
|
|
13
17
|
({ op }) =>
|
|
14
18
|
observable((observer) => {
|
|
15
19
|
const { path, input, type } = op;
|
|
16
20
|
const { promise, cancel } = httpRequest({
|
|
17
|
-
|
|
21
|
+
...resolvedOpts,
|
|
18
22
|
runtime,
|
|
19
23
|
type,
|
|
20
24
|
path,
|
|
@@ -1,9 +1,47 @@
|
|
|
1
1
|
import { ProcedureType } from '@trpc/server';
|
|
2
2
|
import { TRPCResponse } from '@trpc/server/rpc';
|
|
3
|
-
import {
|
|
3
|
+
import { getFetch } from '../../getFetch';
|
|
4
|
+
import { getAbortController } from '../../internals/fetchHelpers';
|
|
5
|
+
import { HTTPHeaders, PromiseAndCancel, TRPCClientRuntime } from '../types';
|
|
4
6
|
|
|
5
7
|
export interface HTTPLinkOptions {
|
|
6
8
|
url: string;
|
|
9
|
+
/**
|
|
10
|
+
* Add ponyfill for fetch
|
|
11
|
+
*/
|
|
12
|
+
fetch?: typeof fetch;
|
|
13
|
+
/**
|
|
14
|
+
* Add ponyfill for AbortController
|
|
15
|
+
*/
|
|
16
|
+
AbortController?: typeof AbortController | null;
|
|
17
|
+
/**
|
|
18
|
+
* Headers to be set on outgoing requests or a callback that of said headers
|
|
19
|
+
* @link http://trpc.io/docs/v10/header
|
|
20
|
+
*/
|
|
21
|
+
headers?: HTTPHeaders | (() => HTTPHeaders | Promise<HTTPHeaders>);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ResolvedHTTPLinkOptions {
|
|
25
|
+
url: string;
|
|
26
|
+
fetch: typeof fetch;
|
|
27
|
+
AbortController: typeof AbortController | null;
|
|
28
|
+
/**
|
|
29
|
+
* Headers to be set on outgoing request
|
|
30
|
+
* @link http://trpc.io/docs/v10/header
|
|
31
|
+
*/
|
|
32
|
+
headers: () => HTTPHeaders | Promise<HTTPHeaders>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function resolveHTTPLinkOptions(
|
|
36
|
+
opts: HTTPLinkOptions,
|
|
37
|
+
): ResolvedHTTPLinkOptions {
|
|
38
|
+
const headers = opts.headers || (() => ({}));
|
|
39
|
+
return {
|
|
40
|
+
url: opts.url,
|
|
41
|
+
fetch: getFetch(opts.fetch),
|
|
42
|
+
AbortController: getAbortController(opts.AbortController),
|
|
43
|
+
headers: typeof headers === 'function' ? headers : () => headers,
|
|
44
|
+
};
|
|
7
45
|
}
|
|
8
46
|
|
|
9
47
|
// https://github.com/trpc/trpc/pull/669
|
|
@@ -40,7 +78,7 @@ function getInput(opts: GetInputOptions) {
|
|
|
40
78
|
);
|
|
41
79
|
}
|
|
42
80
|
|
|
43
|
-
export type HTTPRequestOptions =
|
|
81
|
+
export type HTTPRequestOptions = ResolvedHTTPLinkOptions &
|
|
44
82
|
GetInputOptions & {
|
|
45
83
|
type: ProcedureType;
|
|
46
84
|
path: string;
|
|
@@ -77,20 +115,20 @@ export function getBody(opts: GetBodyOptions) {
|
|
|
77
115
|
export function httpRequest(
|
|
78
116
|
opts: HTTPRequestOptions,
|
|
79
117
|
): PromiseAndCancel<HTTPResult> {
|
|
80
|
-
const { type
|
|
81
|
-
const ac =
|
|
118
|
+
const { type } = opts;
|
|
119
|
+
const ac = opts.AbortController ? new opts.AbortController() : null;
|
|
82
120
|
|
|
83
121
|
const promise = new Promise<HTTPResult>((resolve, reject) => {
|
|
84
122
|
const url = getUrl(opts);
|
|
85
123
|
const body = getBody(opts);
|
|
86
124
|
|
|
87
125
|
const meta = {} as HTTPResult['meta'];
|
|
88
|
-
Promise.resolve(
|
|
126
|
+
Promise.resolve(opts.headers())
|
|
89
127
|
.then((headers) => {
|
|
90
128
|
if (type === 'subscription') {
|
|
91
129
|
throw new Error('Subscriptions should use wsLink');
|
|
92
130
|
}
|
|
93
|
-
return
|
|
131
|
+
return opts.fetch(url, {
|
|
94
132
|
method: METHOD[type],
|
|
95
133
|
signal: ac?.signal,
|
|
96
134
|
body: body,
|
package/src/links/types.ts
CHANGED
|
@@ -3,14 +3,26 @@ import { Observable, Observer } from '@trpc/server/observable';
|
|
|
3
3
|
import { TRPCResultMessage, TRPCSuccessResponse } from '@trpc/server/rpc';
|
|
4
4
|
import { TRPCClientError } from '../TRPCClientError';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
6
9
|
export type CancelFn = () => void;
|
|
7
10
|
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
8
14
|
export type PromiseAndCancel<TValue> = {
|
|
9
15
|
promise: Promise<TValue>;
|
|
10
16
|
cancel: CancelFn;
|
|
11
17
|
};
|
|
12
18
|
|
|
19
|
+
/**
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
13
22
|
export type OperationContext = Record<string, unknown>;
|
|
23
|
+
/**
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
14
26
|
export type Operation<TInput = unknown> = {
|
|
15
27
|
id: number;
|
|
16
28
|
type: 'query' | 'mutation' | 'subscription';
|
|
@@ -19,6 +31,9 @@ export type Operation<TInput = unknown> = {
|
|
|
19
31
|
context: OperationContext;
|
|
20
32
|
};
|
|
21
33
|
|
|
34
|
+
/**
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
22
37
|
export type HTTPHeaders = Record<string, string | string[] | undefined>;
|
|
23
38
|
|
|
24
39
|
/**
|
|
@@ -31,12 +46,12 @@ export type TRPCFetch = (
|
|
|
31
46
|
) => Promise<Response>;
|
|
32
47
|
|
|
33
48
|
export interface TRPCClientRuntime {
|
|
34
|
-
fetch: TRPCFetch;
|
|
35
|
-
AbortController?: typeof AbortController;
|
|
36
|
-
headers: () => HTTPHeaders | Promise<HTTPHeaders>;
|
|
37
49
|
transformer: DataTransformer;
|
|
38
50
|
}
|
|
39
51
|
|
|
52
|
+
/**
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
40
55
|
export interface OperationResultEnvelope<TOutput> {
|
|
41
56
|
result:
|
|
42
57
|
| TRPCSuccessResponse<TOutput>['result']
|
|
@@ -44,16 +59,25 @@ export interface OperationResultEnvelope<TOutput> {
|
|
|
44
59
|
context?: OperationContext;
|
|
45
60
|
}
|
|
46
61
|
|
|
62
|
+
/**
|
|
63
|
+
* @internal
|
|
64
|
+
*/
|
|
47
65
|
export type OperationResultObservable<
|
|
48
66
|
TRouter extends AnyRouter,
|
|
49
67
|
TOutput,
|
|
50
68
|
> = Observable<OperationResultEnvelope<TOutput>, TRPCClientError<TRouter>>;
|
|
51
69
|
|
|
70
|
+
/**
|
|
71
|
+
* @internal
|
|
72
|
+
*/
|
|
52
73
|
export type OperationResultObserver<
|
|
53
74
|
TRouter extends AnyRouter,
|
|
54
75
|
TOutput,
|
|
55
76
|
> = Observer<OperationResultEnvelope<TOutput>, TRPCClientError<TRouter>>;
|
|
56
77
|
|
|
78
|
+
/**
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
57
81
|
export type OperationLink<
|
|
58
82
|
TRouter extends AnyRouter,
|
|
59
83
|
TInput = unknown,
|
|
@@ -63,6 +87,9 @@ export type OperationLink<
|
|
|
63
87
|
next: (op: Operation<TInput>) => OperationResultObservable<TRouter, TOutput>;
|
|
64
88
|
}) => OperationResultObservable<TRouter, TOutput>;
|
|
65
89
|
|
|
90
|
+
/**
|
|
91
|
+
* @public
|
|
92
|
+
*/
|
|
66
93
|
export type TRPCLink<TRouter extends AnyRouter> = (
|
|
67
94
|
opts: TRPCClientRuntime,
|
|
68
95
|
) => OperationLink<TRouter>;
|