@trpc/client 10.0.0-proxy-alpha.76 → 10.0.0-proxy-alpha.78
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 +1 -1
- 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 +103 -138
- package/dist/index.mjs +104 -139
- package/dist/internals/TRPCClient.d.ts +6 -36
- 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 +12 -6
- package/src/createTRPCClientProxy.ts +18 -16
- package/src/internals/TRPCClient.ts +13 -86
- 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,11 +1,9 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
-
|
|
3
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
4
1
|
import type { AnyRouter } from '@trpc/server';
|
|
5
2
|
import {
|
|
6
3
|
TRPCClient as Client,
|
|
7
4
|
CreateTRPCClientOptions,
|
|
8
5
|
} from './internals/TRPCClient';
|
|
6
|
+
import { httpBatchLink } from './links';
|
|
9
7
|
|
|
10
8
|
/**
|
|
11
9
|
* @deprecated use `createTRPCProxyClient` instead
|
|
@@ -13,15 +11,23 @@ import {
|
|
|
13
11
|
export function createTRPCClient<TRouter extends AnyRouter>(
|
|
14
12
|
opts: CreateTRPCClientOptions<TRouter>,
|
|
15
13
|
) {
|
|
16
|
-
const
|
|
14
|
+
const getLinks = () => {
|
|
15
|
+
if ('links' in opts) {
|
|
16
|
+
return opts.links;
|
|
17
|
+
}
|
|
18
|
+
return [httpBatchLink(opts)];
|
|
19
|
+
};
|
|
20
|
+
const client = new Client<TRouter>({
|
|
21
|
+
transformer: opts.transformer,
|
|
22
|
+
links: getLinks(),
|
|
23
|
+
});
|
|
17
24
|
return client;
|
|
18
25
|
}
|
|
19
26
|
|
|
20
27
|
// Also the client created above needs to somehow be like `TRPCClient<Router> & Omit<Router, 'createCaller' | 'createProcedure' | '_def' | 'transformer' | 'errorFormatter' | 'getErrorShape>`
|
|
21
28
|
|
|
22
29
|
export type {
|
|
23
|
-
TRPCRequestOptions,
|
|
24
30
|
CreateTRPCClientOptions,
|
|
25
31
|
TRPCClient,
|
|
26
|
-
|
|
32
|
+
TRPCRequestOptions,
|
|
27
33
|
} 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
|
-
import { CreateTRPCClientOptions
|
|
21
|
+
import { CreateTRPCClientOptions } 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
|
}
|
|
@@ -6,7 +6,6 @@ import {
|
|
|
6
6
|
inferProcedureOutput,
|
|
7
7
|
inferSubscriptionOutput,
|
|
8
8
|
} from '@trpc/server';
|
|
9
|
-
import { ProcedureRecord } from '@trpc/server';
|
|
10
9
|
import {
|
|
11
10
|
Unsubscribable,
|
|
12
11
|
inferObservableValue,
|
|
@@ -14,32 +13,15 @@ import {
|
|
|
14
13
|
share,
|
|
15
14
|
} from '@trpc/server/observable';
|
|
16
15
|
import { TRPCClientError } from '../TRPCClientError';
|
|
17
|
-
import { getFetch } from '../getFetch';
|
|
18
|
-
import { httpBatchLink } from '../links';
|
|
19
16
|
import { createChain } from '../links/internals/createChain';
|
|
20
17
|
import {
|
|
21
|
-
HTTPHeaders,
|
|
22
18
|
OperationContext,
|
|
23
19
|
OperationLink,
|
|
24
20
|
TRPCClientRuntime,
|
|
25
21
|
TRPCLink,
|
|
26
22
|
} from '../links/types';
|
|
27
|
-
import { getAbortController } from './fetchHelpers';
|
|
28
23
|
|
|
29
24
|
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
25
|
/**
|
|
44
26
|
* Data transformer
|
|
45
27
|
* @link https://trpc.io/docs/data-transformers
|
|
@@ -64,59 +46,20 @@ export interface TRPCSubscriptionObserver<TValue, TError> {
|
|
|
64
46
|
onComplete: () => void;
|
|
65
47
|
}
|
|
66
48
|
|
|
67
|
-
/**
|
|
68
|
-
* This type prohibits `url` from being provided along with `links`
|
|
69
|
-
* @internal
|
|
70
|
-
*/
|
|
49
|
+
/** @internal */
|
|
71
50
|
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
|
-
};
|
|
51
|
+
| CreateTRPCClientBaseOptions & {
|
|
52
|
+
links: TRPCLink<TRouter>[];
|
|
53
|
+
};
|
|
98
54
|
|
|
99
55
|
export class TRPCClient<TRouter extends AnyRouter> {
|
|
100
56
|
private readonly links: OperationLink<TRouter>[];
|
|
101
57
|
public readonly runtime: TRPCClientRuntime;
|
|
102
58
|
private requestId: number;
|
|
103
|
-
private getRequestId() {
|
|
104
|
-
return ++this.requestId;
|
|
105
|
-
}
|
|
106
59
|
|
|
107
60
|
constructor(opts: CreateTRPCClientOptions<TRouter>) {
|
|
108
|
-
const _fetch = getFetch(opts?.fetch);
|
|
109
|
-
const AC = getAbortController(opts?.AbortController);
|
|
110
61
|
this.requestId = 0;
|
|
111
62
|
|
|
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
63
|
function getTransformer(): DataTransformer {
|
|
121
64
|
if (!opts.transformer)
|
|
122
65
|
return {
|
|
@@ -132,20 +75,10 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
132
75
|
}
|
|
133
76
|
|
|
134
77
|
this.runtime = {
|
|
135
|
-
AbortController: AC,
|
|
136
|
-
fetch: _fetch,
|
|
137
|
-
headers: getHeadersFn(),
|
|
138
78
|
transformer: getTransformer(),
|
|
139
79
|
};
|
|
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();
|
|
80
|
+
// Initialize the links
|
|
81
|
+
this.links = opts.links.map((link) => link(this.runtime));
|
|
149
82
|
}
|
|
150
83
|
|
|
151
84
|
private $request<TInput = unknown, TOutput = unknown>({
|
|
@@ -162,7 +95,7 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
162
95
|
const chain$ = createChain<TRouter, TInput, TOutput>({
|
|
163
96
|
links: this.links as OperationLink<any, any, any>[],
|
|
164
97
|
op: {
|
|
165
|
-
id: this.
|
|
98
|
+
id: ++this.requestId,
|
|
166
99
|
type,
|
|
167
100
|
path,
|
|
168
101
|
input,
|
|
@@ -197,11 +130,9 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
197
130
|
return abortablePromise;
|
|
198
131
|
}
|
|
199
132
|
public query<
|
|
200
|
-
TQueries extends
|
|
133
|
+
TQueries extends TRouter['_def']['queries'],
|
|
201
134
|
TPath extends string & keyof TQueries,
|
|
202
|
-
TInput extends inferProcedureInput<
|
|
203
|
-
AssertType<TQueries, ProcedureRecord>[TPath]
|
|
204
|
-
>,
|
|
135
|
+
TInput extends inferProcedureInput<TQueries[TPath]>,
|
|
205
136
|
>(path: TPath, input?: TInput, opts?: TRPCRequestOptions) {
|
|
206
137
|
type TOutput = inferProcedureOutput<TQueries[TPath]>;
|
|
207
138
|
return this.requestAsPromise<TInput, TOutput>({
|
|
@@ -213,11 +144,9 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
213
144
|
});
|
|
214
145
|
}
|
|
215
146
|
public mutation<
|
|
216
|
-
TMutations extends
|
|
147
|
+
TMutations extends TRouter['_def']['mutations'],
|
|
217
148
|
TPath extends string & keyof TMutations,
|
|
218
|
-
TInput extends inferProcedureInput<
|
|
219
|
-
AssertType<TMutations, ProcedureRecord>[TPath]
|
|
220
|
-
>,
|
|
149
|
+
TInput extends inferProcedureInput<TMutations[TPath]>,
|
|
221
150
|
>(path: TPath, input?: TInput, opts?: TRPCRequestOptions) {
|
|
222
151
|
type TOutput = inferProcedureOutput<TMutations[TPath]>;
|
|
223
152
|
return this.requestAsPromise<TInput, TOutput>({
|
|
@@ -229,12 +158,10 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
229
158
|
});
|
|
230
159
|
}
|
|
231
160
|
public subscription<
|
|
232
|
-
TSubscriptions extends
|
|
161
|
+
TSubscriptions extends TRouter['_def']['subscriptions'],
|
|
233
162
|
TPath extends string & keyof TSubscriptions,
|
|
234
163
|
TOutput extends inferSubscriptionOutput<TRouter, TPath>,
|
|
235
|
-
TInput extends inferProcedureInput<
|
|
236
|
-
AssertType<TSubscriptions, ProcedureRecord>[TPath]
|
|
237
|
-
>,
|
|
164
|
+
TInput extends inferProcedureInput<TSubscriptions[TPath]>,
|
|
238
165
|
>(
|
|
239
166
|
path: TPath,
|
|
240
167
|
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>;
|