@trpc/client 10.0.0-alpha.40 → 10.0.0-alpha.43
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 +20 -7
- package/dist/createTRPCClientProxy.d.ts.map +1 -1
- package/dist/index.js +38 -91
- package/dist/index.mjs +35 -88
- package/dist/internals/TRPCClient.d.ts +28 -6
- package/dist/internals/TRPCClient.d.ts.map +1 -1
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +18 -1
- package/dist/links/httpBatchLink.mjs +18 -1
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +21 -2
- package/dist/links/httpLink.mjs +21 -2
- package/dist/links/internals/httpUtils.d.ts +4 -3
- package/dist/links/internals/httpUtils.d.ts.map +1 -1
- package/dist/links/internals/transformResult.d.ts +28 -0
- package/dist/links/internals/transformResult.d.ts.map +1 -0
- package/dist/links/loggerLink.d.ts +3 -3
- package/dist/links/loggerLink.d.ts.map +1 -1
- package/dist/links/loggerLink.js +1 -1
- package/dist/links/loggerLink.mjs +1 -1
- package/dist/links/types.d.ts +6 -8
- package/dist/links/types.d.ts.map +1 -1
- package/dist/links/wsLink.d.ts +11 -4
- package/dist/links/wsLink.d.ts.map +1 -1
- package/dist/links/wsLink.js +400 -12
- package/dist/links/wsLink.mjs +391 -10
- package/dist/transformResult-06bec648.mjs +108 -0
- package/dist/transformResult-3ccc74ea.js +122 -0
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +4 -4
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +4 -4
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +1 -1
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +4 -4
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +4 -4
- package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +1 -1
- package/package.json +4 -4
- package/src/TRPCClientError.ts +1 -1
- package/src/createTRPCClient.ts +1 -0
- package/src/createTRPCClientProxy.ts +64 -10
- package/src/internals/TRPCClient.ts +58 -38
- package/src/links/dedupeLink.test.ts +8 -14
- package/src/links/httpBatchLink.ts +17 -7
- package/src/links/httpLink.ts +14 -2
- package/src/links/internals/createChain.test.ts +18 -33
- package/src/links/internals/httpUtils.ts +6 -5
- package/src/links/internals/transformResult.ts +40 -0
- package/src/links/loggerLink.ts +5 -5
- package/src/links/retryLink.ts +1 -1
- package/src/links/types.ts +8 -10
- package/src/links/wsLink.ts +40 -7
- package/dist/links/internals/transformOperationResult.d.ts +0 -30
- package/dist/links/internals/transformOperationResult.d.ts.map +0 -1
- package/dist/wsLink-11ee4922.mjs +0 -439
- package/dist/wsLink-807f4ad9.js +0 -455
- package/src/links/internals/transformOperationResult.ts +0 -53
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var TRPCClientError = require('../../../dist/TRPCClientError-e9bf96e9.cjs.prod.js');
|
|
6
|
-
var
|
|
6
|
+
var httpRequest = require('../../../dist/httpRequest-66400297.cjs.prod.js');
|
|
7
7
|
var _createForOfIteratorHelper = require('@babel/runtime/helpers/createForOfIteratorHelper');
|
|
8
8
|
require('@babel/runtime/helpers/objectSpread2');
|
|
9
9
|
require('@babel/runtime/helpers/classCallCheck');
|
|
@@ -133,7 +133,7 @@ function httpBatchLink(opts) {
|
|
|
133
133
|
return op.input;
|
|
134
134
|
});
|
|
135
135
|
|
|
136
|
-
var _httpRequest =
|
|
136
|
+
var _httpRequest = httpRequest.httpRequest({
|
|
137
137
|
url: url,
|
|
138
138
|
inputs: inputs,
|
|
139
139
|
path: path,
|
|
@@ -194,13 +194,13 @@ function httpBatchLink(opts) {
|
|
|
194
194
|
};
|
|
195
195
|
|
|
196
196
|
onDestroy(function () {
|
|
197
|
-
prevOnce(TRPCClientError.TRPCClientError.from(new
|
|
197
|
+
prevOnce(TRPCClientError.TRPCClientError.from(new httpRequest.TRPCAbortError(), {
|
|
198
198
|
isDone: true
|
|
199
199
|
}));
|
|
200
200
|
cancel();
|
|
201
201
|
});
|
|
202
202
|
promise.then(function (envelope) {
|
|
203
|
-
prevOnce(
|
|
203
|
+
prevOnce(httpRequest.transformRPCResponse({
|
|
204
204
|
envelope: envelope,
|
|
205
205
|
runtime: runtime
|
|
206
206
|
}));
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { T as TRPCClientError } from '../../../dist/TRPCClientError-09b8a26b.esm.js';
|
|
2
|
-
import { T as TRPCAbortError, t as transformRPCResponse, h as httpRequest } from '../../../dist/
|
|
2
|
+
import { T as TRPCAbortError, t as transformRPCResponse, h as httpRequest } from '../../../dist/httpRequest-62c5cce4.esm.js';
|
|
3
3
|
import _createForOfIteratorHelper from '@babel/runtime/helpers/esm/createForOfIteratorHelper';
|
|
4
4
|
import '@babel/runtime/helpers/objectSpread2';
|
|
5
5
|
import '@babel/runtime/helpers/classCallCheck';
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var TRPCClientError = require('../../../dist/TRPCClientError-bfee2bf5.cjs.dev.js');
|
|
6
|
-
var
|
|
6
|
+
var httpRequest = require('../../../dist/httpRequest-6b1f0c6f.cjs.dev.js');
|
|
7
7
|
require('@babel/runtime/helpers/objectSpread2');
|
|
8
8
|
require('@babel/runtime/helpers/classCallCheck');
|
|
9
9
|
require('@babel/runtime/helpers/createClass');
|
|
@@ -25,7 +25,7 @@ function httpLink(opts) {
|
|
|
25
25
|
input = op.input,
|
|
26
26
|
type = op.type;
|
|
27
27
|
|
|
28
|
-
var _httpRequest =
|
|
28
|
+
var _httpRequest = httpRequest.httpRequest({
|
|
29
29
|
runtime: runtime,
|
|
30
30
|
type: type,
|
|
31
31
|
input: input,
|
|
@@ -47,13 +47,13 @@ function httpLink(opts) {
|
|
|
47
47
|
};
|
|
48
48
|
|
|
49
49
|
onDestroy(function () {
|
|
50
|
-
prevOnce(TRPCClientError.TRPCClientError.from(new
|
|
50
|
+
prevOnce(TRPCClientError.TRPCClientError.from(new httpRequest.TRPCAbortError(), {
|
|
51
51
|
isDone: true
|
|
52
52
|
}));
|
|
53
53
|
cancel();
|
|
54
54
|
});
|
|
55
55
|
promise.then(function (envelope) {
|
|
56
|
-
prevOnce(
|
|
56
|
+
prevOnce(httpRequest.transformRPCResponse({
|
|
57
57
|
envelope: envelope,
|
|
58
58
|
runtime: runtime
|
|
59
59
|
}));
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var TRPCClientError = require('../../../dist/TRPCClientError-e9bf96e9.cjs.prod.js');
|
|
6
|
-
var
|
|
6
|
+
var httpRequest = require('../../../dist/httpRequest-66400297.cjs.prod.js');
|
|
7
7
|
require('@babel/runtime/helpers/objectSpread2');
|
|
8
8
|
require('@babel/runtime/helpers/classCallCheck');
|
|
9
9
|
require('@babel/runtime/helpers/createClass');
|
|
@@ -25,7 +25,7 @@ function httpLink(opts) {
|
|
|
25
25
|
input = op.input,
|
|
26
26
|
type = op.type;
|
|
27
27
|
|
|
28
|
-
var _httpRequest =
|
|
28
|
+
var _httpRequest = httpRequest.httpRequest({
|
|
29
29
|
runtime: runtime,
|
|
30
30
|
type: type,
|
|
31
31
|
input: input,
|
|
@@ -47,13 +47,13 @@ function httpLink(opts) {
|
|
|
47
47
|
};
|
|
48
48
|
|
|
49
49
|
onDestroy(function () {
|
|
50
|
-
prevOnce(TRPCClientError.TRPCClientError.from(new
|
|
50
|
+
prevOnce(TRPCClientError.TRPCClientError.from(new httpRequest.TRPCAbortError(), {
|
|
51
51
|
isDone: true
|
|
52
52
|
}));
|
|
53
53
|
cancel();
|
|
54
54
|
});
|
|
55
55
|
promise.then(function (envelope) {
|
|
56
|
-
prevOnce(
|
|
56
|
+
prevOnce(httpRequest.transformRPCResponse({
|
|
57
57
|
envelope: envelope,
|
|
58
58
|
runtime: runtime
|
|
59
59
|
}));
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { T as TRPCClientError } from '../../../dist/TRPCClientError-09b8a26b.esm.js';
|
|
2
|
-
import { h as httpRequest, T as TRPCAbortError, t as transformRPCResponse } from '../../../dist/
|
|
2
|
+
import { h as httpRequest, T as TRPCAbortError, t as transformRPCResponse } from '../../../dist/httpRequest-62c5cce4.esm.js';
|
|
3
3
|
import '@babel/runtime/helpers/objectSpread2';
|
|
4
4
|
import '@babel/runtime/helpers/classCallCheck';
|
|
5
5
|
import '@babel/runtime/helpers/createClass';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trpc/client",
|
|
3
|
-
"version": "10.0.0-alpha.
|
|
3
|
+
"version": "10.0.0-alpha.43",
|
|
4
4
|
"description": "tRPC Client lib",
|
|
5
5
|
"author": "KATT",
|
|
6
6
|
"license": "MIT",
|
|
@@ -67,13 +67,13 @@
|
|
|
67
67
|
"@babel/runtime": "^7.9.0"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
|
-
"@trpc/server": "10.0.0-alpha.
|
|
70
|
+
"@trpc/server": "10.0.0-alpha.43"
|
|
71
71
|
},
|
|
72
72
|
"devDependencies": {
|
|
73
|
-
"@trpc/server": "10.0.0-alpha.
|
|
73
|
+
"@trpc/server": "10.0.0-alpha.43"
|
|
74
74
|
},
|
|
75
75
|
"publishConfig": {
|
|
76
76
|
"access": "public"
|
|
77
77
|
},
|
|
78
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "aa47943ffce5844c9bc5d48a4103d766e14d42d9"
|
|
79
79
|
}
|
package/src/TRPCClientError.ts
CHANGED
|
@@ -19,7 +19,7 @@ export class TRPCClientError<TRouter extends AnyRouter>
|
|
|
19
19
|
constructor(
|
|
20
20
|
message: string,
|
|
21
21
|
opts?: {
|
|
22
|
-
result
|
|
22
|
+
result?: Maybe<TRPCErrorResponse<inferRouterError<TRouter>>>;
|
|
23
23
|
cause?: Error;
|
|
24
24
|
meta?: Record<string, unknown>;
|
|
25
25
|
},
|
package/src/createTRPCClient.ts
CHANGED
|
@@ -10,20 +10,51 @@ import type {
|
|
|
10
10
|
ProcedureType,
|
|
11
11
|
inferProcedureOutput,
|
|
12
12
|
} from '@trpc/server';
|
|
13
|
+
import type {
|
|
14
|
+
Unsubscribable,
|
|
15
|
+
inferObservableValue,
|
|
16
|
+
} from '@trpc/server/observable';
|
|
17
|
+
import type { TRPCResultMessage } from '@trpc/server/rpc';
|
|
13
18
|
import { createProxy } from '@trpc/server/shared';
|
|
14
|
-
import {
|
|
19
|
+
import { TRPCClientError } from './TRPCClientError';
|
|
20
|
+
import { CreateTRPCClientOptions, createTRPCClient } from './createTRPCClient';
|
|
21
|
+
import {
|
|
22
|
+
TRPCClient as Client,
|
|
23
|
+
TRPCSubscriptionObserver,
|
|
24
|
+
} from './internals/TRPCClient';
|
|
15
25
|
|
|
16
26
|
type Resolver<TProcedure extends Procedure<any>> = (
|
|
17
27
|
...args: ProcedureArgs<TProcedure['_def']>
|
|
18
28
|
) => Promise<inferProcedureOutput<TProcedure>>;
|
|
19
29
|
|
|
20
|
-
type
|
|
30
|
+
type SubscriptionResolver<
|
|
31
|
+
TProcedure extends Procedure<any>,
|
|
32
|
+
TRouter extends AnyRouter,
|
|
33
|
+
> = (
|
|
34
|
+
...args: [
|
|
35
|
+
input: ProcedureArgs<TProcedure['_def']>[0],
|
|
36
|
+
opts: ProcedureArgs<TProcedure['_def']>[1] &
|
|
37
|
+
Partial<
|
|
38
|
+
TRPCSubscriptionObserver<
|
|
39
|
+
TRPCResultMessage<
|
|
40
|
+
inferObservableValue<inferProcedureOutput<TProcedure>>
|
|
41
|
+
>,
|
|
42
|
+
TRPCClientError<TRouter>
|
|
43
|
+
>
|
|
44
|
+
>,
|
|
45
|
+
]
|
|
46
|
+
) => Unsubscribable;
|
|
47
|
+
|
|
48
|
+
type DecorateProcedure<
|
|
49
|
+
TProcedure extends Procedure<any>,
|
|
50
|
+
TRouter extends AnyRouter,
|
|
51
|
+
> = OmitNeverKeys<{
|
|
21
52
|
query: TProcedure extends { _query: true } ? Resolver<TProcedure> : never;
|
|
22
53
|
|
|
23
54
|
mutate: TProcedure extends { _mutation: true } ? Resolver<TProcedure> : never;
|
|
24
55
|
|
|
25
56
|
subscribe: TProcedure extends { _subscription: true }
|
|
26
|
-
?
|
|
57
|
+
? SubscriptionResolver<TProcedure, TRouter>
|
|
27
58
|
: never;
|
|
28
59
|
}>;
|
|
29
60
|
|
|
@@ -32,28 +63,51 @@ type assertProcedure<T> = T extends Procedure<any> ? T : never;
|
|
|
32
63
|
/**
|
|
33
64
|
* @internal
|
|
34
65
|
*/
|
|
35
|
-
type DecoratedProcedureRecord<
|
|
66
|
+
type DecoratedProcedureRecord<
|
|
67
|
+
TProcedures extends ProcedureRouterRecord,
|
|
68
|
+
TRouter extends AnyRouter,
|
|
69
|
+
> = OmitNeverKeys<{
|
|
36
70
|
[TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter
|
|
37
|
-
? DecoratedProcedureRecord<
|
|
38
|
-
|
|
39
|
-
|
|
71
|
+
? DecoratedProcedureRecord<
|
|
72
|
+
TProcedures[TKey]['_def']['record'],
|
|
73
|
+
TProcedures[TKey]
|
|
74
|
+
>
|
|
75
|
+
: assertProcedure<TProcedures[TKey]>['_def']['_old'] extends true
|
|
76
|
+
? never
|
|
77
|
+
: DecorateProcedure<assertProcedure<TProcedures[TKey]>, TRouter>;
|
|
78
|
+
}>;
|
|
40
79
|
|
|
41
|
-
const clientCallTypeMap: Record<
|
|
80
|
+
const clientCallTypeMap: Record<
|
|
81
|
+
keyof DecorateProcedure<any, any>,
|
|
82
|
+
ProcedureType
|
|
83
|
+
> = {
|
|
42
84
|
query: 'query',
|
|
43
85
|
mutate: 'mutation',
|
|
44
86
|
subscribe: 'subscription',
|
|
45
87
|
};
|
|
46
88
|
|
|
89
|
+
/**
|
|
90
|
+
* @deprecated use createTRPCProxyClient instead
|
|
91
|
+
* @internal
|
|
92
|
+
*/
|
|
47
93
|
export function createTRPCClientProxy<TRouter extends AnyRouter>(
|
|
48
94
|
client: Client<TRouter>,
|
|
49
95
|
) {
|
|
50
96
|
const proxy = createProxy(({ path, args }) => {
|
|
51
97
|
const pathCopy = [...path];
|
|
52
|
-
const clientCallType = pathCopy.pop()! as keyof DecorateProcedure<any>;
|
|
98
|
+
const clientCallType = pathCopy.pop()! as keyof DecorateProcedure<any, any>;
|
|
53
99
|
const procedureType = clientCallTypeMap[clientCallType];
|
|
54
100
|
|
|
55
101
|
const fullPath = pathCopy.join('.');
|
|
56
102
|
return (client as any)[procedureType](fullPath, ...args);
|
|
57
103
|
});
|
|
58
|
-
return proxy as DecoratedProcedureRecord<TRouter['_def']['record']>;
|
|
104
|
+
return proxy as DecoratedProcedureRecord<TRouter['_def']['record'], TRouter>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function createTRPCProxyClient<TRouter extends AnyRouter>(
|
|
108
|
+
opts: CreateTRPCClientOptions<TRouter>,
|
|
109
|
+
) {
|
|
110
|
+
const client = createTRPCClient<TRouter>(opts);
|
|
111
|
+
const proxy = createTRPCClientProxy(client);
|
|
112
|
+
return proxy;
|
|
59
113
|
}
|
|
@@ -7,23 +7,18 @@ import {
|
|
|
7
7
|
inferProcedureOutput,
|
|
8
8
|
inferSubscriptionOutput,
|
|
9
9
|
} from '@trpc/server';
|
|
10
|
+
import { ProcedureRecord } from '@trpc/server';
|
|
10
11
|
import {
|
|
11
|
-
Observer,
|
|
12
12
|
Unsubscribable,
|
|
13
13
|
inferObservableValue,
|
|
14
14
|
observableToPromise,
|
|
15
15
|
share,
|
|
16
16
|
} from '@trpc/server/observable';
|
|
17
|
-
import { TRPCResultMessage } from '@trpc/server/rpc';
|
|
18
17
|
import { CancelFn } from '..';
|
|
19
18
|
import { TRPCClientError } from '../TRPCClientError';
|
|
20
19
|
import { getFetch } from '../getFetch';
|
|
21
20
|
import { httpBatchLink } from '../links';
|
|
22
21
|
import { createChain } from '../links/internals/createChain';
|
|
23
|
-
import {
|
|
24
|
-
transformOperationResult,
|
|
25
|
-
transformSubscriptionOperationResult,
|
|
26
|
-
} from '../links/internals/transformOperationResult';
|
|
27
22
|
import {
|
|
28
23
|
HTTPHeaders,
|
|
29
24
|
OperationContext,
|
|
@@ -84,13 +79,38 @@ export interface TRPCRequestOptions {
|
|
|
84
79
|
/**
|
|
85
80
|
* Pass additional context to links
|
|
86
81
|
*/
|
|
87
|
-
|
|
82
|
+
requestContext?: OperationContext;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface TRPCSubscriptionObserver<TValue, TError> {
|
|
86
|
+
onStarted: () => void;
|
|
87
|
+
onData: (value: TValue) => void;
|
|
88
|
+
onError: (err: TError) => void;
|
|
89
|
+
onStopped: () => void;
|
|
90
|
+
onComplete: () => void;
|
|
88
91
|
}
|
|
89
92
|
|
|
90
93
|
/** @internal */
|
|
91
94
|
export type CreateTRPCClientOptions<TRouter extends AnyRouter> =
|
|
92
95
|
| CreateTRPCClientWithLinksOptions<TRouter>
|
|
93
96
|
| CreateTRPCClientWithURLOptions;
|
|
97
|
+
|
|
98
|
+
export type AssertType<T, K> = T extends K ? T : never;
|
|
99
|
+
/**
|
|
100
|
+
* @deprecated
|
|
101
|
+
*/
|
|
102
|
+
export type AssertLegacyDef<TRouter extends AnyRouter> =
|
|
103
|
+
TRouter['_def']['legacy'] extends Record<
|
|
104
|
+
'subscriptions' | 'queries' | 'mutations',
|
|
105
|
+
ProcedureRecord
|
|
106
|
+
>
|
|
107
|
+
? TRouter['_def']['legacy']
|
|
108
|
+
: {
|
|
109
|
+
subscriptions: {};
|
|
110
|
+
queries: {};
|
|
111
|
+
mutations: {};
|
|
112
|
+
};
|
|
113
|
+
|
|
94
114
|
export class TRPCClient<TRouter extends AnyRouter> {
|
|
95
115
|
private readonly links: OperationLink<TRouter>[];
|
|
96
116
|
public readonly runtime: TRPCClientRuntime;
|
|
@@ -173,13 +193,8 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
173
193
|
const cancellablePromise: CancellablePromise<any> = new Promise<TOutput>(
|
|
174
194
|
(resolve, reject) => {
|
|
175
195
|
promise
|
|
176
|
-
.then((
|
|
177
|
-
|
|
178
|
-
if (transformed.ok) {
|
|
179
|
-
resolve(transformed.data);
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
reject(transformed.error);
|
|
196
|
+
.then((envelope) => {
|
|
197
|
+
resolve((envelope.result as any).data);
|
|
183
198
|
})
|
|
184
199
|
.catch((err) => {
|
|
185
200
|
reject(TRPCClientError.from(err));
|
|
@@ -191,15 +206,18 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
191
206
|
return cancellablePromise;
|
|
192
207
|
}
|
|
193
208
|
public query<
|
|
194
|
-
TQueries extends TRouter['
|
|
209
|
+
TQueries extends AssertLegacyDef<TRouter>['queries'],
|
|
195
210
|
TPath extends string & keyof TQueries,
|
|
196
211
|
>(
|
|
197
212
|
path: TPath,
|
|
198
|
-
...args: [
|
|
213
|
+
...args: [
|
|
214
|
+
...inferHandlerInput<AssertType<TQueries, ProcedureRecord>[TPath]>,
|
|
215
|
+
TRPCRequestOptions?,
|
|
216
|
+
]
|
|
199
217
|
) {
|
|
200
|
-
const context = (args[1] as TRPCRequestOptions | undefined)?.
|
|
218
|
+
const context = (args[1] as TRPCRequestOptions | undefined)?.requestContext;
|
|
201
219
|
return this.requestAsPromise<
|
|
202
|
-
inferHandlerInput<TQueries[TPath]>,
|
|
220
|
+
inferHandlerInput<AssertType<TQueries, ProcedureRecord>[TPath]>,
|
|
203
221
|
inferProcedureOutput<TQueries[TPath]>
|
|
204
222
|
>({
|
|
205
223
|
type: 'query',
|
|
@@ -209,15 +227,18 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
209
227
|
});
|
|
210
228
|
}
|
|
211
229
|
public mutation<
|
|
212
|
-
TMutations extends TRouter['
|
|
230
|
+
TMutations extends AssertLegacyDef<TRouter>['mutations'],
|
|
213
231
|
TPath extends string & keyof TMutations,
|
|
214
232
|
>(
|
|
215
233
|
path: TPath,
|
|
216
|
-
...args: [
|
|
234
|
+
...args: [
|
|
235
|
+
...inferHandlerInput<AssertType<TMutations, ProcedureRecord>[TPath]>,
|
|
236
|
+
TRPCRequestOptions?,
|
|
237
|
+
]
|
|
217
238
|
) {
|
|
218
|
-
const context = (args[1] as TRPCRequestOptions | undefined)?.
|
|
239
|
+
const context = (args[1] as TRPCRequestOptions | undefined)?.requestContext;
|
|
219
240
|
return this.requestAsPromise<
|
|
220
|
-
inferHandlerInput<TMutations[TPath]>,
|
|
241
|
+
inferHandlerInput<AssertType<TMutations, ProcedureRecord>[TPath]>,
|
|
221
242
|
inferProcedureOutput<TMutations[TPath]>
|
|
222
243
|
>({
|
|
223
244
|
type: 'mutation',
|
|
@@ -227,40 +248,39 @@ export class TRPCClient<TRouter extends AnyRouter> {
|
|
|
227
248
|
});
|
|
228
249
|
}
|
|
229
250
|
public subscription<
|
|
230
|
-
TSubscriptions extends TRouter['
|
|
251
|
+
TSubscriptions extends AssertLegacyDef<TRouter>['subscriptions'],
|
|
231
252
|
TPath extends string & keyof TSubscriptions,
|
|
232
253
|
TOutput extends inferSubscriptionOutput<TRouter, TPath>,
|
|
233
|
-
TInput extends inferProcedureInput<
|
|
254
|
+
TInput extends inferProcedureInput<
|
|
255
|
+
AssertType<TSubscriptions, ProcedureRecord>[TPath]
|
|
256
|
+
>,
|
|
234
257
|
>(
|
|
235
258
|
path: TPath,
|
|
236
259
|
input: TInput,
|
|
237
260
|
opts: TRPCRequestOptions &
|
|
238
|
-
Partial<
|
|
261
|
+
Partial<TRPCSubscriptionObserver<TOutput, TRPCClientError<TRouter>>>,
|
|
239
262
|
): Unsubscribable {
|
|
240
263
|
const observable$ = this.$request<TInput, TOutput>({
|
|
241
264
|
type: 'subscription',
|
|
242
265
|
path,
|
|
243
266
|
input,
|
|
244
|
-
context: opts.
|
|
267
|
+
context: opts.requestContext,
|
|
245
268
|
});
|
|
246
|
-
const runtime = this.runtime;
|
|
247
269
|
return observable$.subscribe({
|
|
248
|
-
next(
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
opts.
|
|
255
|
-
return;
|
|
270
|
+
next(envelope) {
|
|
271
|
+
if (envelope.result.type === 'started') {
|
|
272
|
+
opts.onStarted?.();
|
|
273
|
+
} else if (envelope.result.type === 'stopped') {
|
|
274
|
+
opts.onStopped?.();
|
|
275
|
+
} else {
|
|
276
|
+
opts.onData?.((envelope.result as any).data);
|
|
256
277
|
}
|
|
257
|
-
opts.error?.(transformed.error);
|
|
258
278
|
},
|
|
259
279
|
error(err) {
|
|
260
|
-
opts.
|
|
280
|
+
opts.onError?.(err);
|
|
261
281
|
},
|
|
262
282
|
complete() {
|
|
263
|
-
opts.
|
|
283
|
+
opts.onComplete?.();
|
|
264
284
|
},
|
|
265
285
|
});
|
|
266
286
|
}
|
|
@@ -17,13 +17,10 @@ test('dedupeLink', async () => {
|
|
|
17
17
|
const timer = setTimeout(() => {
|
|
18
18
|
timerTriggered();
|
|
19
19
|
subscribe.next({
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
data: {
|
|
25
|
-
input: op.input,
|
|
26
|
-
},
|
|
20
|
+
result: {
|
|
21
|
+
type: 'data',
|
|
22
|
+
data: {
|
|
23
|
+
input: op.input,
|
|
27
24
|
},
|
|
28
25
|
},
|
|
29
26
|
});
|
|
@@ -80,13 +77,10 @@ test('dedupe - cancel one does not cancel the other', async () => {
|
|
|
80
77
|
const timer = setTimeout(() => {
|
|
81
78
|
timerTriggered();
|
|
82
79
|
subscribe.next({
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
data: {
|
|
88
|
-
input: op.input,
|
|
89
|
-
},
|
|
80
|
+
result: {
|
|
81
|
+
type: 'data',
|
|
82
|
+
data: {
|
|
83
|
+
input: op.input,
|
|
90
84
|
},
|
|
91
85
|
},
|
|
92
86
|
});
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { AnyRouter, ProcedureType } from '@trpc/server';
|
|
2
2
|
import { observable } from '@trpc/server/observable';
|
|
3
|
+
import { TRPCClientError } from '../TRPCClientError';
|
|
3
4
|
import { dataLoader } from '../internals/dataLoader';
|
|
4
5
|
import {
|
|
5
6
|
HTTPLinkOptions,
|
|
6
|
-
|
|
7
|
+
HTTPResult,
|
|
7
8
|
getUrl,
|
|
8
9
|
httpRequest,
|
|
9
10
|
} from './internals/httpUtils';
|
|
11
|
+
import { transformResult } from './internals/transformResult';
|
|
10
12
|
import { TRPCLink } from './types';
|
|
11
13
|
|
|
12
14
|
export interface HttpBatchLinkOptions extends HTTPLinkOptions {
|
|
@@ -67,13 +69,11 @@ export function httpBatchLink<TRouter extends AnyRouter>(
|
|
|
67
69
|
return { validate, fetch };
|
|
68
70
|
};
|
|
69
71
|
|
|
70
|
-
const query = dataLoader<BatchOperation,
|
|
71
|
-
|
|
72
|
-
);
|
|
73
|
-
const mutation = dataLoader<BatchOperation, ResponseShape>(
|
|
72
|
+
const query = dataLoader<BatchOperation, HTTPResult>(batchLoader('query'));
|
|
73
|
+
const mutation = dataLoader<BatchOperation, HTTPResult>(
|
|
74
74
|
batchLoader('mutation'),
|
|
75
75
|
);
|
|
76
|
-
const subscription = dataLoader<BatchOperation,
|
|
76
|
+
const subscription = dataLoader<BatchOperation, HTTPResult>(
|
|
77
77
|
batchLoader('subscription'),
|
|
78
78
|
);
|
|
79
79
|
|
|
@@ -85,9 +85,19 @@ export function httpBatchLink<TRouter extends AnyRouter>(
|
|
|
85
85
|
|
|
86
86
|
promise
|
|
87
87
|
.then((res) => {
|
|
88
|
+
const transformed = transformResult(res.json, runtime);
|
|
89
|
+
|
|
90
|
+
if (!transformed.ok) {
|
|
91
|
+
observer.error(
|
|
92
|
+
TRPCClientError.from(transformed.error, {
|
|
93
|
+
meta: res.meta,
|
|
94
|
+
}),
|
|
95
|
+
);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
88
98
|
observer.next({
|
|
89
99
|
context: res.meta,
|
|
90
|
-
|
|
100
|
+
result: transformed.result,
|
|
91
101
|
});
|
|
92
102
|
observer.complete();
|
|
93
103
|
})
|
package/src/links/httpLink.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { AnyRouter } from '@trpc/server';
|
|
2
2
|
import { observable } from '@trpc/server/observable';
|
|
3
|
+
import { TRPCClientError } from '../TRPCClientError';
|
|
3
4
|
import { HTTPLinkOptions, httpRequest } from './internals/httpUtils';
|
|
5
|
+
import { transformResult } from './internals/transformResult';
|
|
4
6
|
import { TRPCLink } from './types';
|
|
5
7
|
|
|
6
8
|
export function httpLink<TRouter extends AnyRouter>(
|
|
@@ -20,13 +22,23 @@ export function httpLink<TRouter extends AnyRouter>(
|
|
|
20
22
|
});
|
|
21
23
|
promise
|
|
22
24
|
.then((res) => {
|
|
25
|
+
const transformed = transformResult(res.json, runtime);
|
|
26
|
+
|
|
27
|
+
if (!transformed.ok) {
|
|
28
|
+
observer.error(
|
|
29
|
+
TRPCClientError.from(transformed.error, {
|
|
30
|
+
meta: res.meta,
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
23
35
|
observer.next({
|
|
24
36
|
context: res.meta,
|
|
25
|
-
|
|
37
|
+
result: transformed.result,
|
|
26
38
|
});
|
|
27
39
|
observer.complete();
|
|
28
40
|
})
|
|
29
|
-
.catch(
|
|
41
|
+
.catch((cause) => TRPCClientError.from(cause));
|
|
30
42
|
|
|
31
43
|
return () => {
|
|
32
44
|
cancel();
|
|
@@ -18,13 +18,10 @@ describe('chain', () => {
|
|
|
18
18
|
return observable((observer) => {
|
|
19
19
|
observer.next({
|
|
20
20
|
context: {},
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
data: {
|
|
26
|
-
input: op.input,
|
|
27
|
-
},
|
|
21
|
+
result: {
|
|
22
|
+
type: 'data',
|
|
23
|
+
data: {
|
|
24
|
+
input: op.input,
|
|
28
25
|
},
|
|
29
26
|
},
|
|
30
27
|
});
|
|
@@ -54,12 +51,9 @@ describe('chain', () => {
|
|
|
54
51
|
return observable((observer) => {
|
|
55
52
|
observer.next({
|
|
56
53
|
context: {},
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
type: 'data',
|
|
61
|
-
data: 'from cache',
|
|
62
|
-
},
|
|
54
|
+
result: {
|
|
55
|
+
type: 'data',
|
|
56
|
+
data: 'from cache',
|
|
63
57
|
},
|
|
64
58
|
});
|
|
65
59
|
const subscription = next(op).subscribe(observer);
|
|
@@ -71,13 +65,10 @@ describe('chain', () => {
|
|
|
71
65
|
({ op }) => {
|
|
72
66
|
return observable((observer) => {
|
|
73
67
|
observer.next({
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
data: {
|
|
79
|
-
input: op.input,
|
|
80
|
-
},
|
|
68
|
+
result: {
|
|
69
|
+
type: 'data',
|
|
70
|
+
data: {
|
|
71
|
+
input: op.input,
|
|
81
72
|
},
|
|
82
73
|
},
|
|
83
74
|
});
|
|
@@ -103,12 +94,9 @@ describe('chain', () => {
|
|
|
103
94
|
Array [
|
|
104
95
|
Object {
|
|
105
96
|
"context": Object {},
|
|
106
|
-
"
|
|
107
|
-
"
|
|
108
|
-
"
|
|
109
|
-
"data": "from cache",
|
|
110
|
-
"type": "data",
|
|
111
|
-
},
|
|
97
|
+
"result": Object {
|
|
98
|
+
"data": "from cache",
|
|
99
|
+
"type": "data",
|
|
112
100
|
},
|
|
113
101
|
},
|
|
114
102
|
]
|
|
@@ -116,14 +104,11 @@ describe('chain', () => {
|
|
|
116
104
|
expect(next.mock.calls[1]).toMatchInlineSnapshot(`
|
|
117
105
|
Array [
|
|
118
106
|
Object {
|
|
119
|
-
"
|
|
120
|
-
"
|
|
121
|
-
|
|
122
|
-
"data": Object {
|
|
123
|
-
"input": "world",
|
|
124
|
-
},
|
|
125
|
-
"type": "data",
|
|
107
|
+
"result": Object {
|
|
108
|
+
"data": Object {
|
|
109
|
+
"input": "world",
|
|
126
110
|
},
|
|
111
|
+
"type": "data",
|
|
127
112
|
},
|
|
128
113
|
},
|
|
129
114
|
]
|