@trpc/client 10.0.0-alpha.4 → 10.0.0-alpha.42
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 +58 -1
- package/dist/TRPCClientError.d.ts +3 -11
- package/dist/TRPCClientError.d.ts.map +1 -1
- package/dist/createTRPCClient.d.ts +2 -3
- package/dist/createTRPCClient.d.ts.map +1 -1
- package/dist/createTRPCClientProxy.d.ts +31 -0
- package/dist/createTRPCClientProxy.d.ts.map +1 -0
- package/dist/{httpUtils-c09d7b79.mjs → httpUtils-089853f6.mjs} +38 -30
- package/dist/{httpUtils-1f9e4e3c.js → httpUtils-a0169eef.js} +38 -29
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +256 -107
- package/dist/index.mjs +244 -99
- package/dist/internals/TRPCClient.d.ts +21 -18
- package/dist/internals/TRPCClient.d.ts.map +1 -1
- package/dist/internals/dataLoader.d.ts +7 -4
- package/dist/internals/dataLoader.d.ts.map +1 -1
- package/dist/links/dedupeLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.d.ts +5 -2
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +197 -69
- package/dist/links/httpBatchLink.mjs +197 -69
- package/dist/links/httpLink.d.ts +1 -1
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +5 -5
- package/dist/links/httpLink.mjs +5 -5
- package/dist/links/index.d.ts +9 -0
- package/dist/links/index.d.ts.map +1 -0
- package/dist/links/internals/createChain.d.ts.map +1 -1
- package/dist/links/internals/httpUtils.d.ts +30 -0
- package/dist/links/internals/httpUtils.d.ts.map +1 -0
- package/dist/links/internals/transformOperationResult.d.ts +17 -3
- package/dist/links/internals/transformOperationResult.d.ts.map +1 -1
- package/dist/links/loggerLink.d.ts.map +1 -1
- package/dist/links/loggerLink.js +2 -3
- package/dist/links/loggerLink.mjs +1 -2
- package/dist/links/retryLink.d.ts.map +1 -1
- package/dist/links/splitLink.d.ts +1 -1
- package/dist/links/splitLink.d.ts.map +1 -1
- package/dist/links/splitLink.js +3 -25
- package/dist/links/splitLink.mjs +2 -28
- package/dist/links/types.d.ts +12 -9
- package/dist/links/types.d.ts.map +1 -1
- package/dist/links/wsLink.d.ts +5 -3
- package/dist/links/wsLink.d.ts.map +1 -1
- package/dist/links/wsLink.js +12 -376
- package/dist/links/wsLink.mjs +10 -369
- package/dist/splitLink-0202cd7b.mjs +54 -0
- package/dist/splitLink-23c5ce5c.js +57 -0
- package/dist/wsLink-27b93bd0.mjs +451 -0
- package/dist/wsLink-44703a83.js +467 -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 +4 -4
- package/links/httpBatchLink/index.d.ts +1 -1
- package/links/httpBatchLink/index.js +1 -1
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +1 -1
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +1 -1
- package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +1 -1
- package/links/httpLink/index.d.ts +1 -1
- package/links/httpLink/index.js +1 -1
- package/links/loggerLink/index.d.ts +1 -1
- package/links/loggerLink/index.js +1 -1
- package/links/splitLink/index.d.ts +1 -1
- package/links/splitLink/index.js +1 -1
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +3 -1
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +3 -1
- package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +3 -1
- package/links/wsLink/index.d.ts +1 -1
- package/links/wsLink/index.js +1 -1
- package/package.json +23 -30
- package/src/TRPCClientError.ts +5 -13
- package/src/createTRPCClient.ts +7 -3
- package/src/createTRPCClientProxy.ts +98 -0
- package/src/index.ts +2 -1
- package/src/internals/TRPCClient.ts +70 -42
- package/src/internals/dataLoader.ts +120 -45
- package/src/links/dedupeLink.test.ts +1 -1
- package/src/links/dedupeLink.ts +6 -7
- package/src/links/httpBatchLink.ts +64 -31
- package/src/links/httpLink.ts +4 -4
- package/src/links/index.ts +8 -0
- package/src/links/internals/createChain.test.ts +5 -5
- package/src/links/internals/createChain.ts +8 -3
- package/src/links/internals/httpUtils.ts +116 -0
- package/src/links/internals/transformOperationResult.ts +40 -12
- package/src/links/loggerLink.ts +1 -2
- package/src/links/retryLink.ts +1 -2
- package/src/links/splitLink.test.ts +3 -3
- package/src/links/splitLink.ts +5 -5
- package/src/links/types.ts +14 -9
- package/src/links/wsLink.ts +31 -16
- package/dist/TRPCClientError-17b117e1.mjs +0 -81
- package/dist/TRPCClientError-ec6bd2ca.js +0 -94
- package/dist/createChain-389cc116.mjs +0 -25
- package/dist/createChain-864dbb30.js +0 -27
- package/dist/internals/transformRPCResponse.d.ts +0 -15
- package/dist/internals/transformRPCResponse.d.ts.map +0 -1
- package/dist/links/httpUtils.d.ts +0 -22
- package/dist/links/httpUtils.d.ts.map +0 -1
- package/dist/links/transformerLink.d.ts +0 -4
- package/dist/links/transformerLink.d.ts.map +0 -1
- package/dist/links/transformerLink.js +0 -75
- package/dist/links/transformerLink.mjs +0 -67
- package/dist/links/transformerLink.ts.js +0 -1
- package/dist/observable-7e66cb56.mjs +0 -102
- package/dist/observable-85669100.js +0 -104
- package/dist/rx/index.d.ts +0 -3
- package/dist/rx/index.d.ts.map +0 -1
- package/dist/rx/index.js +0 -9
- package/dist/rx/index.mjs +0 -1
- package/dist/rx/index.ts.js +0 -1
- package/dist/rx/observable.d.ts +0 -4
- package/dist/rx/observable.d.ts.map +0 -1
- package/dist/rx/operators/index.d.ts +0 -4
- package/dist/rx/operators/index.d.ts.map +0 -1
- package/dist/rx/operators/index.js +0 -38
- package/dist/rx/operators/index.mjs +0 -32
- package/dist/rx/operators/index.ts.js +0 -1
- package/dist/rx/operators/map.d.ts +0 -3
- package/dist/rx/operators/map.d.ts.map +0 -1
- package/dist/rx/operators/share.d.ts +0 -6
- package/dist/rx/operators/share.d.ts.map +0 -1
- package/dist/rx/operators/tap.d.ts +0 -3
- package/dist/rx/operators/tap.d.ts.map +0 -1
- package/dist/rx/types.d.ts +0 -29
- package/dist/rx/types.d.ts.map +0 -1
- package/dist/rx/util/identity.d.ts +0 -2
- package/dist/rx/util/identity.d.ts.map +0 -1
- package/dist/rx/util/observableToPromise.d.ts +0 -10
- package/dist/rx/util/observableToPromise.d.ts.map +0 -1
- package/dist/rx/util/pipe.d.ts +0 -4
- package/dist/rx/util/pipe.d.ts.map +0 -1
- package/dist/share-4d79dc49.js +0 -110
- package/dist/share-671b63fa.mjs +0 -108
- package/dist/tap-538fa07f.mjs +0 -30
- package/dist/tap-799a2d33.js +0 -32
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.d.ts +0 -1
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.dev.js +0 -67
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.js +0 -7
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.prod.js +0 -67
- package/links/transformerLink/dist/trpc-client-links-transformerLink.esm.js +0 -63
- package/links/transformerLink/index.d.ts +0 -1
- package/links/transformerLink/index.js +0 -1
- package/rx/dist/trpc-client-rx.cjs.d.ts +0 -1
- package/rx/dist/trpc-client-rx.cjs.dev.js +0 -9
- package/rx/dist/trpc-client-rx.cjs.js +0 -7
- package/rx/dist/trpc-client-rx.cjs.prod.js +0 -9
- package/rx/dist/trpc-client-rx.esm.js +0 -1
- package/rx/index.d.ts +0 -1
- package/rx/index.js +0 -1
- package/rx/operators/dist/trpc-client-rx-operators.cjs.d.ts +0 -1
- package/rx/operators/dist/trpc-client-rx-operators.cjs.dev.js +0 -39
- package/rx/operators/dist/trpc-client-rx-operators.cjs.js +0 -7
- package/rx/operators/dist/trpc-client-rx-operators.cjs.prod.js +0 -39
- package/rx/operators/dist/trpc-client-rx-operators.esm.js +0 -33
- package/rx/operators/index.d.ts +0 -1
- package/rx/operators/index.js +0 -1
- package/src/internals/transformRPCResponse.ts +0 -25
- package/src/links/httpUtils.ts +0 -94
- package/src/links/transformerLink.ts +0 -70
- package/src/rx/index.ts +0 -2
- package/src/rx/observable.test.ts +0 -83
- package/src/rx/observable.ts +0 -70
- package/src/rx/operators/index.ts +0 -3
- package/src/rx/operators/map.test.ts +0 -92
- package/src/rx/operators/map.ts +0 -25
- package/src/rx/operators/share.test.ts +0 -60
- package/src/rx/operators/share.ts +0 -67
- package/src/rx/operators/tap.ts +0 -26
- package/src/rx/types.ts +0 -69
- package/src/rx/util/identity.ts +0 -3
- package/src/rx/util/observableToPromise.ts +0 -49
- package/src/rx/util/pipe.ts +0 -22
|
@@ -1,18 +1,32 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
1
2
|
import { CancelFn, PromiseAndCancel } from '../links/types';
|
|
2
3
|
|
|
3
4
|
type BatchItem<TKey, TValue> = {
|
|
4
|
-
|
|
5
|
+
aborted: boolean;
|
|
5
6
|
key: TKey;
|
|
6
7
|
resolve: (value: TValue) => void;
|
|
7
8
|
reject: (error: Error) => void;
|
|
9
|
+
batch: Batch<TKey, TValue> | null;
|
|
8
10
|
};
|
|
9
11
|
type Batch<TKey, TValue> = {
|
|
10
12
|
items: BatchItem<TKey, TValue>[];
|
|
11
13
|
cancel: CancelFn;
|
|
12
14
|
};
|
|
13
|
-
type
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
type BatchLoader<TKey, TValue> = {
|
|
16
|
+
validate: (keys: TKey[]) => boolean;
|
|
17
|
+
fetch: (keys: TKey[]) => {
|
|
18
|
+
promise: Promise<TValue[]>;
|
|
19
|
+
cancel: CancelFn;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A function that should never be called unless we messed something up.
|
|
25
|
+
*/
|
|
26
|
+
const throwFatalError = () => {
|
|
27
|
+
throw new Error(
|
|
28
|
+
'Something went wrong. Please submit an issue at https://github.com/trpc/trpc/issues/new',
|
|
29
|
+
);
|
|
16
30
|
};
|
|
17
31
|
|
|
18
32
|
/**
|
|
@@ -20,67 +34,128 @@ type BatchLoadFn<TKey, TValue> = (keys: TKey[]) => {
|
|
|
20
34
|
* Less configuration, no caching, and allows you to cancel requests
|
|
21
35
|
* When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
|
|
22
36
|
*/
|
|
23
|
-
export function dataLoader<TKey, TValue>(
|
|
24
|
-
|
|
25
|
-
|
|
37
|
+
export function dataLoader<TKey, TValue>(
|
|
38
|
+
batchLoader: BatchLoader<TKey, TValue>,
|
|
39
|
+
) {
|
|
40
|
+
let pendingItems: BatchItem<TKey, TValue>[] | null = null;
|
|
41
|
+
let dispatchTimer: ReturnType<typeof setTimeout> | null = null;
|
|
26
42
|
|
|
27
|
-
const
|
|
43
|
+
const destroyTimerAndPendingItems = () => {
|
|
28
44
|
clearTimeout(dispatchTimer as any);
|
|
29
45
|
dispatchTimer = null;
|
|
30
|
-
|
|
46
|
+
pendingItems = null;
|
|
31
47
|
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Iterate through the items and split them into groups based on the `batchLoader`'s validate function
|
|
51
|
+
*/
|
|
52
|
+
function groupItems(items: BatchItem<TKey, TValue>[]) {
|
|
53
|
+
const groupedItems: BatchItem<TKey, TValue>[][] = [[]];
|
|
54
|
+
let index = 0;
|
|
55
|
+
while (true) {
|
|
56
|
+
const item = items[index];
|
|
57
|
+
if (!item) {
|
|
58
|
+
// we're done
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
const lastGroup = groupedItems[groupedItems.length - 1]!;
|
|
62
|
+
|
|
63
|
+
if (item.aborted) {
|
|
64
|
+
// Item was aborted before it was dispatched
|
|
65
|
+
item.reject(new Error('Aborted'));
|
|
66
|
+
index++;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const isValid = batchLoader.validate(
|
|
71
|
+
lastGroup.concat(item).map((it) => it.key),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
if (isValid) {
|
|
75
|
+
lastGroup.push(item);
|
|
76
|
+
index++;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (lastGroup.length === 0) {
|
|
81
|
+
item.reject(new Error('Input is too big for a single dispatch'));
|
|
82
|
+
index++;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
// Create new group, next iteration will try to add the item to that
|
|
86
|
+
groupedItems.push([]);
|
|
87
|
+
}
|
|
88
|
+
return groupedItems;
|
|
89
|
+
}
|
|
90
|
+
|
|
32
91
|
function dispatch() {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
92
|
+
const groupedItems = groupItems(pendingItems!);
|
|
93
|
+
destroyTimerAndPendingItems();
|
|
94
|
+
|
|
95
|
+
// Create batches for each group of items
|
|
96
|
+
for (const items of groupedItems) {
|
|
97
|
+
if (!items.length) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const batch: Batch<TKey, TValue> = {
|
|
101
|
+
items,
|
|
102
|
+
cancel: throwFatalError,
|
|
103
|
+
};
|
|
104
|
+
for (const item of items) {
|
|
105
|
+
item.batch = batch;
|
|
106
|
+
}
|
|
107
|
+
const { promise, cancel } = batchLoader.fetch(
|
|
108
|
+
batch.items.map((_item) => _item.key),
|
|
109
|
+
);
|
|
110
|
+
batch.cancel = cancel;
|
|
111
|
+
|
|
112
|
+
promise
|
|
113
|
+
.then((result) => {
|
|
114
|
+
for (let i = 0; i < result.length; i++) {
|
|
115
|
+
const value = result[i]!;
|
|
116
|
+
const item = batch.items[i]!;
|
|
117
|
+
item.resolve(value);
|
|
118
|
+
item.batch = null;
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
.catch((cause) => {
|
|
122
|
+
for (const item of batch.items) {
|
|
123
|
+
item.reject(cause);
|
|
124
|
+
item.batch = null;
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
51
128
|
}
|
|
52
129
|
function load(key: TKey): PromiseAndCancel<TValue> {
|
|
53
|
-
const
|
|
54
|
-
|
|
130
|
+
const item: BatchItem<TKey, TValue> = {
|
|
131
|
+
aborted: false,
|
|
55
132
|
key,
|
|
133
|
+
batch: null,
|
|
134
|
+
resolve: throwFatalError,
|
|
135
|
+
reject: throwFatalError,
|
|
56
136
|
};
|
|
57
137
|
|
|
58
|
-
if (!batch) {
|
|
59
|
-
batch = {
|
|
60
|
-
items: [],
|
|
61
|
-
cancel() {
|
|
62
|
-
destroyTimerAndBatch();
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
const thisBatch = batch;
|
|
67
138
|
const promise = new Promise<TValue>((resolve, reject) => {
|
|
68
|
-
const item = batchItem as any as BatchItem<TKey, TValue>;
|
|
69
139
|
item.reject = reject;
|
|
70
140
|
item.resolve = resolve;
|
|
71
|
-
|
|
141
|
+
|
|
142
|
+
if (!pendingItems) {
|
|
143
|
+
pendingItems = [];
|
|
144
|
+
}
|
|
145
|
+
pendingItems.push(item);
|
|
72
146
|
});
|
|
147
|
+
|
|
73
148
|
if (!dispatchTimer) {
|
|
74
149
|
dispatchTimer = setTimeout(dispatch);
|
|
75
150
|
}
|
|
76
151
|
const cancel = () => {
|
|
77
|
-
|
|
152
|
+
item.aborted = true;
|
|
78
153
|
|
|
79
|
-
if (
|
|
80
|
-
//
|
|
81
|
-
|
|
154
|
+
if (item.batch?.items.every((item) => item.aborted)) {
|
|
155
|
+
// All items in the batch have been cancelled
|
|
156
|
+
item.batch.cancel();
|
|
157
|
+
item.batch = null;
|
|
82
158
|
}
|
|
83
|
-
thisBatch.cancel();
|
|
84
159
|
};
|
|
85
160
|
|
|
86
161
|
return { promise, cancel };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { waitFor } from '@testing-library/dom';
|
|
2
2
|
import { AnyRouter } from '@trpc/server';
|
|
3
|
+
import { observable } from '@trpc/server/observable';
|
|
3
4
|
import { OperationLink } from '..';
|
|
4
|
-
import { observable } from '../rx/observable';
|
|
5
5
|
import { dedupeLink } from './dedupeLink';
|
|
6
6
|
import { createChain } from './internals/createChain';
|
|
7
7
|
|
package/src/links/dedupeLink.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { AnyRouter } from '@trpc/server';
|
|
2
|
-
import { observable } from '
|
|
3
|
-
import { share } from '../rx/operators';
|
|
4
|
-
import { Observable } from '../rx/types';
|
|
2
|
+
import { Observable, observable, share } from '@trpc/server/observable';
|
|
5
3
|
import { TRPCLink } from './types';
|
|
6
4
|
|
|
7
5
|
export function dedupeLink<
|
|
@@ -19,16 +17,17 @@ export function dedupeLink<
|
|
|
19
17
|
return next(op);
|
|
20
18
|
}
|
|
21
19
|
const key = JSON.stringify([op.path, op.input]);
|
|
22
|
-
|
|
20
|
+
const obs$ = pending[key];
|
|
21
|
+
if (obs$) {
|
|
23
22
|
// console.log('hooking into pending', { op });
|
|
24
|
-
return observable((observer) =>
|
|
23
|
+
return observable((observer) => obs$.subscribe(observer));
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
const shared$ = observable((observer) => {
|
|
28
27
|
function reset() {
|
|
29
28
|
delete pending[key];
|
|
30
29
|
}
|
|
31
|
-
const
|
|
30
|
+
const subscription = next(op).subscribe({
|
|
32
31
|
next(v) {
|
|
33
32
|
observer.next(v);
|
|
34
33
|
},
|
|
@@ -43,7 +42,7 @@ export function dedupeLink<
|
|
|
43
42
|
});
|
|
44
43
|
return () => {
|
|
45
44
|
reset();
|
|
46
|
-
|
|
45
|
+
subscription.unsubscribe();
|
|
47
46
|
};
|
|
48
47
|
}).pipe(share());
|
|
49
48
|
pending[key] = shared$;
|
|
@@ -1,47 +1,80 @@
|
|
|
1
1
|
import { AnyRouter, ProcedureType } from '@trpc/server';
|
|
2
|
+
import { observable } from '@trpc/server/observable';
|
|
2
3
|
import { dataLoader } from '../internals/dataLoader';
|
|
3
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
HTTPLinkOptions,
|
|
6
|
+
ResponseShape,
|
|
7
|
+
getUrl,
|
|
8
|
+
httpRequest,
|
|
9
|
+
} from './internals/httpUtils';
|
|
4
10
|
import { TRPCLink } from './types';
|
|
5
|
-
|
|
11
|
+
|
|
12
|
+
export interface HttpBatchLinkOptions extends HTTPLinkOptions {
|
|
13
|
+
maxURLLength?: number;
|
|
14
|
+
}
|
|
6
15
|
|
|
7
16
|
export function httpBatchLink<TRouter extends AnyRouter>(
|
|
8
|
-
opts:
|
|
17
|
+
opts: HttpBatchLinkOptions,
|
|
9
18
|
): TRPCLink<TRouter> {
|
|
10
|
-
|
|
19
|
+
// initialized config
|
|
11
20
|
return (runtime) => {
|
|
12
|
-
type
|
|
13
|
-
const fetcher = (type: ProcedureType) => (keyInputPairs: Key[]) => {
|
|
14
|
-
const path = keyInputPairs.map((op) => op.path).join(',');
|
|
15
|
-
const inputs = keyInputPairs.map((op) => op.input);
|
|
21
|
+
type BatchOperation = { id: number; path: string; input: unknown };
|
|
16
22
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
const maxURLLength = opts.maxURLLength || Infinity;
|
|
24
|
+
|
|
25
|
+
const batchLoader = (type: ProcedureType) => {
|
|
26
|
+
const validate = (batchOps: BatchOperation[]) => {
|
|
27
|
+
if (maxURLLength === Infinity) {
|
|
28
|
+
// escape hatch for quick calcs
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
const path = batchOps.map((op) => op.path).join(',');
|
|
32
|
+
const inputs = batchOps.map((op) => op.input);
|
|
33
|
+
|
|
34
|
+
const url = getUrl({ url: opts.url, runtime, type, path, inputs });
|
|
35
|
+
return url.length <= maxURLLength;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const fetch = (batchOps: BatchOperation[]) => {
|
|
39
|
+
const path = batchOps.map((op) => op.path).join(',');
|
|
40
|
+
const inputs = batchOps.map((op) => op.input);
|
|
24
41
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
42
|
+
const { promise, cancel } = httpRequest({
|
|
43
|
+
url: opts.url,
|
|
44
|
+
runtime,
|
|
45
|
+
type,
|
|
46
|
+
path,
|
|
47
|
+
inputs,
|
|
48
|
+
});
|
|
30
49
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
json
|
|
34
|
-
|
|
50
|
+
return {
|
|
51
|
+
promise: promise.then((res) => {
|
|
52
|
+
const resJSON = Array.isArray(res.json)
|
|
53
|
+
? res.json
|
|
54
|
+
: batchOps.map(() => res.json);
|
|
35
55
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
56
|
+
const result = resJSON.map((item) => ({
|
|
57
|
+
meta: res.meta,
|
|
58
|
+
json: item,
|
|
59
|
+
}));
|
|
60
|
+
|
|
61
|
+
return result;
|
|
62
|
+
}),
|
|
63
|
+
cancel,
|
|
64
|
+
};
|
|
39
65
|
};
|
|
66
|
+
|
|
67
|
+
return { validate, fetch };
|
|
40
68
|
};
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
69
|
+
|
|
70
|
+
const query = dataLoader<BatchOperation, ResponseShape>(
|
|
71
|
+
batchLoader('query'),
|
|
72
|
+
);
|
|
73
|
+
const mutation = dataLoader<BatchOperation, ResponseShape>(
|
|
74
|
+
batchLoader('mutation'),
|
|
75
|
+
);
|
|
76
|
+
const subscription = dataLoader<BatchOperation, ResponseShape>(
|
|
77
|
+
batchLoader('subscription'),
|
|
45
78
|
);
|
|
46
79
|
|
|
47
80
|
const loaders = { query, subscription, mutation };
|
package/src/links/httpLink.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AnyRouter } from '@trpc/server';
|
|
2
|
-
import { observable } from '
|
|
2
|
+
import { observable } from '@trpc/server/observable';
|
|
3
|
+
import { HTTPLinkOptions, httpRequest } from './internals/httpUtils';
|
|
3
4
|
import { TRPCLink } from './types';
|
|
4
|
-
import { HTTPLinkOptions, httpRequest } from './httpUtils';
|
|
5
5
|
|
|
6
6
|
export function httpLink<TRouter extends AnyRouter>(
|
|
7
7
|
opts: HTTPLinkOptions,
|
|
@@ -12,11 +12,11 @@ export function httpLink<TRouter extends AnyRouter>(
|
|
|
12
12
|
observable((observer) => {
|
|
13
13
|
const { path, input, type } = op;
|
|
14
14
|
const { promise, cancel } = httpRequest({
|
|
15
|
+
url,
|
|
15
16
|
runtime,
|
|
17
|
+
type,
|
|
16
18
|
path,
|
|
17
19
|
input,
|
|
18
|
-
type,
|
|
19
|
-
url,
|
|
20
20
|
});
|
|
21
21
|
promise
|
|
22
22
|
.then((res) => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { observable } from '@trpc/server/observable';
|
|
1
2
|
import { AnyRouter } from '@trpc/server/src';
|
|
2
3
|
import { createChain } from './createChain';
|
|
3
|
-
import { observable } from '../../rx/observable';
|
|
4
4
|
|
|
5
5
|
describe('chain', () => {
|
|
6
6
|
test('trivial', () => {
|
|
@@ -8,9 +8,9 @@ describe('chain', () => {
|
|
|
8
8
|
links: [
|
|
9
9
|
({ next, op }) => {
|
|
10
10
|
return observable((observer) => {
|
|
11
|
-
const
|
|
11
|
+
const subscription = next(op).subscribe(observer);
|
|
12
12
|
return () => {
|
|
13
|
-
|
|
13
|
+
subscription.unsubscribe();
|
|
14
14
|
};
|
|
15
15
|
});
|
|
16
16
|
},
|
|
@@ -62,9 +62,9 @@ describe('chain', () => {
|
|
|
62
62
|
},
|
|
63
63
|
},
|
|
64
64
|
});
|
|
65
|
-
const
|
|
65
|
+
const subscription = next(op).subscribe(observer);
|
|
66
66
|
return () => {
|
|
67
|
-
|
|
67
|
+
subscription.unsubscribe();
|
|
68
68
|
};
|
|
69
69
|
});
|
|
70
70
|
},
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AnyRouter } from '@trpc/server';
|
|
2
|
-
import { observable } from '
|
|
2
|
+
import { observable } from '@trpc/server/observable';
|
|
3
3
|
import { Operation, OperationLink, OperationResultObservable } from '../types';
|
|
4
4
|
|
|
5
5
|
/** @internal */
|
|
@@ -14,7 +14,12 @@ export function createChain<
|
|
|
14
14
|
return observable((observer) => {
|
|
15
15
|
function execute(index = 0, op = opts.op) {
|
|
16
16
|
const next = opts.links[index];
|
|
17
|
-
|
|
17
|
+
if (!next) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
'No more links to execute - did you forget to add an ending link?',
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
const subscription = next({
|
|
18
23
|
op,
|
|
19
24
|
next(nextOp) {
|
|
20
25
|
const nextObserver = execute(index + 1, nextOp);
|
|
@@ -22,7 +27,7 @@ export function createChain<
|
|
|
22
27
|
return nextObserver;
|
|
23
28
|
},
|
|
24
29
|
});
|
|
25
|
-
return
|
|
30
|
+
return subscription;
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
const obs$ = execute();
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { ProcedureType } from '@trpc/server';
|
|
2
|
+
import { PromiseAndCancel, TRPCClientRuntime } from '../types';
|
|
3
|
+
|
|
4
|
+
export interface HTTPLinkOptions {
|
|
5
|
+
url: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// https://github.com/trpc/trpc/pull/669
|
|
9
|
+
function arrayToDict(array: unknown[]) {
|
|
10
|
+
const dict: Record<number, unknown> = {};
|
|
11
|
+
for (let index = 0; index < array.length; index++) {
|
|
12
|
+
const element = array[index];
|
|
13
|
+
dict[index] = element;
|
|
14
|
+
}
|
|
15
|
+
return dict;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const METHOD = {
|
|
19
|
+
query: 'GET',
|
|
20
|
+
mutation: 'POST',
|
|
21
|
+
subscription: 'PATCH',
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
export interface ResponseShape {
|
|
25
|
+
json: unknown;
|
|
26
|
+
meta: {
|
|
27
|
+
response: Response;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type GetInputOptions = {
|
|
32
|
+
runtime: TRPCClientRuntime;
|
|
33
|
+
} & ({ inputs: unknown[] } | { input: unknown });
|
|
34
|
+
|
|
35
|
+
function getInput(opts: GetInputOptions) {
|
|
36
|
+
return 'input' in opts
|
|
37
|
+
? opts.runtime.transformer.serialize(opts.input)
|
|
38
|
+
: arrayToDict(
|
|
39
|
+
opts.inputs.map((_input) => opts.runtime.transformer.serialize(_input)),
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type HTTPRequestOptions = HTTPLinkOptions &
|
|
44
|
+
GetInputOptions & {
|
|
45
|
+
type: ProcedureType;
|
|
46
|
+
path: string;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export function getUrl(opts: HTTPRequestOptions) {
|
|
50
|
+
let url = opts.url + '/' + opts.path;
|
|
51
|
+
const queryParts: string[] = [];
|
|
52
|
+
if ('inputs' in opts) {
|
|
53
|
+
queryParts.push('batch=1');
|
|
54
|
+
}
|
|
55
|
+
if (opts.type === 'query') {
|
|
56
|
+
const input = getInput(opts);
|
|
57
|
+
if (input !== undefined) {
|
|
58
|
+
queryParts.push(`input=${encodeURIComponent(JSON.stringify(input))}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (queryParts.length) {
|
|
62
|
+
url += '?' + queryParts.join('&');
|
|
63
|
+
}
|
|
64
|
+
return url;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
type GetBodyOptions = { type: ProcedureType } & GetInputOptions;
|
|
68
|
+
|
|
69
|
+
export function getBody(opts: GetBodyOptions) {
|
|
70
|
+
if (opts.type === 'query') {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
const input = getInput(opts);
|
|
74
|
+
return input !== undefined ? JSON.stringify(input) : undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function httpRequest(
|
|
78
|
+
opts: HTTPRequestOptions,
|
|
79
|
+
): PromiseAndCancel<ResponseShape> {
|
|
80
|
+
const { type, runtime } = opts;
|
|
81
|
+
const ac = runtime.AbortController ? new runtime.AbortController() : null;
|
|
82
|
+
|
|
83
|
+
const promise = new Promise<ResponseShape>((resolve, reject) => {
|
|
84
|
+
const url = getUrl(opts);
|
|
85
|
+
const body = getBody(opts);
|
|
86
|
+
|
|
87
|
+
const meta = {} as any as ResponseShape['meta'];
|
|
88
|
+
Promise.resolve(runtime.headers())
|
|
89
|
+
.then((headers) =>
|
|
90
|
+
runtime.fetch(url, {
|
|
91
|
+
method: METHOD[type],
|
|
92
|
+
signal: ac?.signal,
|
|
93
|
+
body: body,
|
|
94
|
+
headers: {
|
|
95
|
+
'content-type': 'application/json',
|
|
96
|
+
...headers,
|
|
97
|
+
},
|
|
98
|
+
}),
|
|
99
|
+
)
|
|
100
|
+
.then((_res) => {
|
|
101
|
+
meta.response = _res;
|
|
102
|
+
return _res.json();
|
|
103
|
+
})
|
|
104
|
+
.then((json) => {
|
|
105
|
+
resolve({
|
|
106
|
+
json,
|
|
107
|
+
meta,
|
|
108
|
+
});
|
|
109
|
+
})
|
|
110
|
+
.catch(reject);
|
|
111
|
+
});
|
|
112
|
+
const cancel = () => {
|
|
113
|
+
ac?.abort();
|
|
114
|
+
};
|
|
115
|
+
return { promise, cancel };
|
|
116
|
+
}
|
|
@@ -1,25 +1,53 @@
|
|
|
1
1
|
import { AnyRouter } from '@trpc/server';
|
|
2
|
+
import { TRPCResultMessage } from '@trpc/server/rpc';
|
|
3
|
+
import { TRPCClientRuntime } from '..';
|
|
2
4
|
import { TRPCClientError } from '../../TRPCClientError';
|
|
3
5
|
import { OperationResult } from '../types';
|
|
4
6
|
|
|
5
7
|
/** @internal */
|
|
6
8
|
export function transformOperationResult<TRouter extends AnyRouter, TOutput>(
|
|
7
9
|
result: OperationResult<TRouter, TOutput>,
|
|
10
|
+
runtime: TRPCClientRuntime,
|
|
8
11
|
) {
|
|
9
12
|
const { context } = result;
|
|
10
13
|
|
|
11
14
|
if ('error' in result.data) {
|
|
12
|
-
const error = TRPCClientError.from<TRouter>(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
} as const;
|
|
15
|
+
const error = TRPCClientError.from<TRouter>({
|
|
16
|
+
...result.data,
|
|
17
|
+
error: runtime.transformer.deserialize(result.data.error),
|
|
18
|
+
});
|
|
19
|
+
return { ok: false, error, context } as const;
|
|
18
20
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
|
|
22
|
+
const data = runtime.transformer.deserialize(
|
|
23
|
+
(result.data.result as any).data,
|
|
24
|
+
) as TOutput;
|
|
25
|
+
return { ok: true, data, context } as const;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** @internal */
|
|
29
|
+
export function transformSubscriptionOperationResult<
|
|
30
|
+
TRouter extends AnyRouter,
|
|
31
|
+
TOutput,
|
|
32
|
+
>(result: OperationResult<TRouter, TOutput>, runtime: TRPCClientRuntime) {
|
|
33
|
+
const { context } = result;
|
|
34
|
+
|
|
35
|
+
if ('error' in result.data) {
|
|
36
|
+
const error = TRPCClientError.from<TRouter>(
|
|
37
|
+
{
|
|
38
|
+
...result.data,
|
|
39
|
+
error: runtime.transformer.deserialize(result.data.error),
|
|
40
|
+
},
|
|
41
|
+
{ meta: context },
|
|
42
|
+
);
|
|
43
|
+
return { ok: false, error, context } as const;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const data = {
|
|
47
|
+
...result.data.result,
|
|
48
|
+
...((result.data.result as any).type === 'data' && {
|
|
49
|
+
data: runtime.transformer.deserialize((result.data.result as any).data),
|
|
50
|
+
}),
|
|
51
|
+
} as TRPCResultMessage<TOutput>;
|
|
52
|
+
return { ok: true, data, context } as const;
|
|
25
53
|
}
|
package/src/links/loggerLink.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { AnyRouter } from '@trpc/server';
|
|
2
|
+
import { observable, tap } from '@trpc/server/observable';
|
|
2
3
|
import { TRPCClientError } from '..';
|
|
3
|
-
import { observable } from '../rx/observable';
|
|
4
|
-
import { tap } from '../rx/operators';
|
|
5
4
|
import { Operation, OperationResult, TRPCLink } from './types';
|
|
6
5
|
|
|
7
6
|
type ConsoleEsque = {
|
package/src/links/retryLink.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { AnyRouter } from '@trpc/server';
|
|
2
|
-
import { observable } from '
|
|
3
|
-
import { Unsubscribable } from '../rx/types';
|
|
2
|
+
import { Unsubscribable, observable } from '@trpc/server/observable';
|
|
4
3
|
import { TRPCLink } from './types';
|
|
5
4
|
|
|
6
5
|
export function retryLink<TRouter extends AnyRouter = AnyRouter>(opts: {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
2
|
+
import { observable } from '@trpc/server/observable';
|
|
3
|
+
import { splitLink } from '../';
|
|
4
|
+
import { OperationLink, TRPCLink } from '../';
|
|
2
5
|
import { AnyRouter } from '../../../server/src';
|
|
3
6
|
import { createChain } from '../links/internals/createChain';
|
|
4
|
-
import { splitLink } from '../links/splitLink';
|
|
5
|
-
import { OperationLink, TRPCLink } from '../links/types';
|
|
6
|
-
import { observable } from '../rx';
|
|
7
7
|
|
|
8
8
|
test('splitLink', () => {
|
|
9
9
|
const wsLinkSpy = jest.fn();
|