@trpc/client 10.0.0-alpha.8 → 10.0.0-proxy-alpha.53
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 +16 -20
- 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 +37 -0
- package/dist/createTRPCClientProxy.d.ts.map +1 -0
- package/dist/httpUtils-44781613.mjs +76 -0
- package/dist/httpUtils-801112a6.js +79 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +260 -280
- package/dist/index.mjs +253 -271
- package/dist/internals/TRPCClient.d.ts +39 -21
- package/dist/internals/TRPCClient.d.ts.map +1 -1
- package/dist/internals/dataLoader.d.ts +7 -6
- package/dist/internals/dataLoader.d.ts.map +1 -1
- package/dist/internals/fetchHelpers.d.ts +1 -1
- package/dist/internals/fetchHelpers.d.ts.map +1 -1
- package/dist/internals/retryDelay.d.ts.map +1 -1
- package/dist/links/dedupeLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.d.ts +2 -2
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +189 -172
- package/dist/links/httpBatchLink.mjs +189 -172
- package/dist/links/httpLink.d.ts +1 -1
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +31 -35
- package/dist/links/httpLink.mjs +31 -35
- 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 +31 -0
- package/dist/links/internals/httpUtils.d.ts.map +1 -0
- 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 +90 -96
- package/dist/links/loggerLink.mjs +90 -91
- 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 +13 -12
- package/dist/links/types.d.ts.map +1 -1
- package/dist/links/wsLink.d.ts +12 -5
- package/dist/links/wsLink.d.ts.map +1 -1
- package/dist/links/wsLink.js +250 -358
- package/dist/links/wsLink.mjs +250 -348
- package/dist/splitLink-695ae288.js +48 -0
- package/dist/splitLink-ad44a55d.mjs +45 -0
- package/dist/transformResult-106791d7.mjs +62 -0
- package/dist/transformResult-e15ccdf6.js +65 -0
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +15 -13
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +15 -13
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +12 -10
- 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/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/package.json +23 -34
- package/src/TRPCClientError.ts +38 -28
- package/src/createTRPCClient.ts +8 -3
- package/src/createTRPCClientProxy.ts +113 -0
- package/src/index.ts +2 -1
- package/src/internals/TRPCClient.ts +118 -77
- package/src/internals/dataLoader.ts +116 -53
- package/src/internals/fetchHelpers.ts +2 -2
- package/src/links/dedupeLink.test.ts +9 -15
- package/src/links/dedupeLink.ts +6 -7
- package/src/links/httpBatchLink.ts +73 -36
- package/src/links/httpLink.ts +18 -6
- package/src/links/index.ts +8 -0
- package/src/links/internals/createChain.test.ts +23 -38
- package/src/links/internals/createChain.ts +8 -3
- package/src/links/internals/httpUtils.ts +117 -0
- package/src/links/internals/transformResult.ts +40 -0
- package/src/links/loggerLink.ts +6 -7
- package/src/links/retryLink.ts +2 -3
- package/src/links/splitLink.test.ts +3 -3
- package/src/links/splitLink.ts +5 -5
- package/src/links/types.ts +15 -12
- package/src/links/wsLink.ts +57 -23
- 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/httpUtils-1f9e4e3c.js +0 -97
- package/dist/httpUtils-c09d7b79.mjs +0 -91
- package/dist/index.ts.js +0 -1
- package/dist/internals/transformRPCResponse.d.ts +0 -15
- package/dist/internals/transformRPCResponse.d.ts.map +0 -1
- package/dist/links/httpBatchLink.ts.js +0 -1
- package/dist/links/httpLink.ts.js +0 -1
- package/dist/links/httpUtils.d.ts +0 -22
- package/dist/links/httpUtils.d.ts.map +0 -1
- package/dist/links/internals/transformOperationResult.d.ts +0 -16
- package/dist/links/internals/transformOperationResult.d.ts.map +0 -1
- package/dist/links/loggerLink.ts.js +0 -1
- package/dist/links/splitLink.ts.js +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/links/wsLink.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/internals/transformOperationResult.ts +0 -25
- 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,16 +1,17 @@
|
|
|
1
1
|
import { CancelFn, PromiseAndCancel } from '../links/types';
|
|
2
|
-
declare type
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
declare type BatchLoader<TKey, TValue> = {
|
|
3
|
+
validate: (keys: TKey[]) => boolean;
|
|
4
|
+
fetch: (keys: TKey[]) => {
|
|
5
|
+
promise: Promise<TValue[]>;
|
|
6
|
+
cancel: CancelFn;
|
|
7
|
+
};
|
|
5
8
|
};
|
|
6
9
|
/**
|
|
7
10
|
* Dataloader that's very inspired by https://github.com/graphql/dataloader
|
|
8
11
|
* Less configuration, no caching, and allows you to cancel requests
|
|
9
12
|
* When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
|
|
10
13
|
*/
|
|
11
|
-
export declare function dataLoader<TKey, TValue>(
|
|
12
|
-
maxBatchSize?: number;
|
|
13
|
-
}): {
|
|
14
|
+
export declare function dataLoader<TKey, TValue>(batchLoader: BatchLoader<TKey, TValue>): {
|
|
14
15
|
load: (key: TKey) => PromiseAndCancel<TValue>;
|
|
15
16
|
};
|
|
16
17
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dataLoader.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"dataLoader.d.ts","sourceRoot":"","sources":["../../../../packages/client/src/internals/dataLoader.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAa5D,aAAK,WAAW,CAAC,IAAI,EAAE,MAAM,IAAI;IAC/B,QAAQ,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,OAAO,CAAC;IACpC,KAAK,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK;QACvB,OAAO,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QAC3B,MAAM,EAAE,QAAQ,CAAC;KAClB,CAAC;CACH,CAAC;AAWF;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EACrC,WAAW,EAAE,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC;gBA2FnB,IAAI,KAAG,iBAAiB,MAAM,CAAC;EAsCnD"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export declare function getWindow(): typeof globalThis;
|
|
2
|
-
export declare function getAbortController(ac?: typeof AbortController): typeof AbortController |
|
|
2
|
+
export declare function getAbortController(ac?: typeof AbortController): typeof AbortController | undefined;
|
|
3
3
|
//# sourceMappingURL=fetchHelpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchHelpers.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"fetchHelpers.d.ts","sourceRoot":"","sources":["../../../../packages/client/src/internals/fetchHelpers.ts"],"names":[],"mappings":"AAAA,wBAAgB,SAAS,sBAKxB;AACD,wBAAgB,kBAAkB,CAChC,EAAE,CAAC,EAAE,OAAO,eAAe,GAC1B,OAAO,eAAe,GAAG,SAAS,CAGpC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retryDelay.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"retryDelay.d.ts","sourceRoot":"","sources":["../../../../packages/client/src/internals/retryDelay.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,UAAU,iBAAkB,MAAM,WACqB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dedupeLink.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"dedupeLink.d.ts","sourceRoot":"","sources":["../../../../packages/client/src/links/dedupeLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnC,wBAAgB,UAAU,CACxB,OAAO,SAAS,SAAS,GAAG,SAAS,KAClC,QAAQ,CAAC,OAAO,CAAC,CA6CrB"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { AnyRouter } from '@trpc/server';
|
|
2
|
+
import { HTTPLinkOptions } from './internals/httpUtils';
|
|
2
3
|
import { TRPCLink } from './types';
|
|
3
|
-
import { HTTPLinkOptions } from './httpUtils';
|
|
4
4
|
export interface HttpBatchLinkOptions extends HTTPLinkOptions {
|
|
5
|
-
|
|
5
|
+
maxURLLength?: number;
|
|
6
6
|
}
|
|
7
7
|
export declare function httpBatchLink<TRouter extends AnyRouter>(opts: HttpBatchLinkOptions): TRPCLink<TRouter>;
|
|
8
8
|
//# sourceMappingURL=httpBatchLink.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"httpBatchLink.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"httpBatchLink.d.ts","sourceRoot":"","sources":["../../../../packages/client/src/links/httpBatchLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAiB,MAAM,cAAc,CAAC;AAIxD,OAAO,EACL,eAAe,EAIhB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnC,MAAM,WAAW,oBAAqB,SAAQ,eAAe;IAC3D,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,wBAAgB,aAAa,CAAC,OAAO,SAAS,SAAS,EACrD,IAAI,EAAE,oBAAoB,GACzB,QAAQ,CAAC,OAAO,CAAC,CA4FnB"}
|
|
@@ -2,194 +2,211 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var observable = require('
|
|
6
|
-
var
|
|
7
|
-
require('
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
5
|
+
var observable = require('@trpc/server/observable');
|
|
6
|
+
var transformResult = require('../transformResult-e15ccdf6.js');
|
|
7
|
+
var httpUtils = require('../httpUtils-801112a6.js');
|
|
8
|
+
|
|
9
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */ /**
|
|
10
|
+
* A function that should never be called unless we messed something up.
|
|
11
|
+
*/ const throwFatalError = ()=>{
|
|
12
|
+
throw new Error('Something went wrong. Please submit an issue at https://github.com/trpc/trpc/issues/new');
|
|
13
|
+
};
|
|
15
14
|
/**
|
|
16
15
|
* Dataloader that's very inspired by https://github.com/graphql/dataloader
|
|
17
16
|
* Less configuration, no caching, and allows you to cancel requests
|
|
18
17
|
* When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
dispatchTimer = null;
|
|
27
|
-
batch = null;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
function dispatch() {
|
|
31
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
32
|
-
var batchCopy = batch;
|
|
33
|
-
destroyTimerAndBatch();
|
|
34
|
-
|
|
35
|
-
var _fetchMany = fetchMany(batchCopy.items.map(function (v) {
|
|
36
|
-
return v.key;
|
|
37
|
-
})),
|
|
38
|
-
promise = _fetchMany.promise,
|
|
39
|
-
cancel = _fetchMany.cancel;
|
|
40
|
-
|
|
41
|
-
batchCopy.cancel = cancel;
|
|
42
|
-
promise.then(function (result) {
|
|
43
|
-
for (var i = 0; i < result.length; i++) {
|
|
44
|
-
var _value = result[i];
|
|
45
|
-
batchCopy.items[i].resolve(_value);
|
|
46
|
-
}
|
|
47
|
-
}).catch(function (cause) {
|
|
48
|
-
var _iterator = _createForOfIteratorHelper(batchCopy.items),
|
|
49
|
-
_step;
|
|
50
|
-
|
|
51
|
-
try {
|
|
52
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
53
|
-
var item = _step.value;
|
|
54
|
-
item.reject(cause);
|
|
55
|
-
}
|
|
56
|
-
} catch (err) {
|
|
57
|
-
_iterator.e(err);
|
|
58
|
-
} finally {
|
|
59
|
-
_iterator.f();
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function load(key) {
|
|
65
|
-
var batchItem = {
|
|
66
|
-
cancelled: false,
|
|
67
|
-
key: key
|
|
18
|
+
*/ function dataLoader(batchLoader) {
|
|
19
|
+
let pendingItems = null;
|
|
20
|
+
let dispatchTimer = null;
|
|
21
|
+
const destroyTimerAndPendingItems = ()=>{
|
|
22
|
+
clearTimeout(dispatchTimer);
|
|
23
|
+
dispatchTimer = null;
|
|
24
|
+
pendingItems = null;
|
|
68
25
|
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Iterate through the items and split them into groups based on the `batchLoader`'s validate function
|
|
28
|
+
*/ function groupItems(items) {
|
|
29
|
+
const groupedItems = [
|
|
30
|
+
[]
|
|
31
|
+
];
|
|
32
|
+
let index = 0;
|
|
33
|
+
while(true){
|
|
34
|
+
const item = items[index];
|
|
35
|
+
if (!item) {
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
const lastGroup = groupedItems[groupedItems.length - 1];
|
|
39
|
+
if (item.aborted) {
|
|
40
|
+
// Item was aborted before it was dispatched
|
|
41
|
+
item.reject(new Error('Aborted'));
|
|
42
|
+
index++;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const isValid = batchLoader.validate(lastGroup.concat(item).map((it)=>it.key));
|
|
46
|
+
if (isValid) {
|
|
47
|
+
lastGroup.push(item);
|
|
48
|
+
index++;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (lastGroup.length === 0) {
|
|
52
|
+
item.reject(new Error('Input is too big for a single dispatch'));
|
|
53
|
+
index++;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
// Create new group, next iteration will try to add the item to that
|
|
57
|
+
groupedItems.push([]);
|
|
75
58
|
}
|
|
76
|
-
|
|
59
|
+
return groupedItems;
|
|
77
60
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
61
|
+
function dispatch() {
|
|
62
|
+
const groupedItems = groupItems(pendingItems);
|
|
63
|
+
destroyTimerAndPendingItems();
|
|
64
|
+
// Create batches for each group of items
|
|
65
|
+
for (const items of groupedItems){
|
|
66
|
+
if (!items.length) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const batch = {
|
|
70
|
+
items,
|
|
71
|
+
cancel: throwFatalError
|
|
72
|
+
};
|
|
73
|
+
for (const item of items){
|
|
74
|
+
item.batch = batch;
|
|
75
|
+
}
|
|
76
|
+
const { promise , cancel } = batchLoader.fetch(batch.items.map((_item)=>_item.key));
|
|
77
|
+
batch.cancel = cancel;
|
|
78
|
+
promise.then((result)=>{
|
|
79
|
+
for(let i = 0; i < result.length; i++){
|
|
80
|
+
const value = result[i];
|
|
81
|
+
const item = batch.items[i];
|
|
82
|
+
item.resolve(value);
|
|
83
|
+
item.batch = null;
|
|
84
|
+
}
|
|
85
|
+
}).catch((cause)=>{
|
|
86
|
+
for (const item of batch.items){
|
|
87
|
+
item.reject(cause);
|
|
88
|
+
item.batch = null;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function load(key) {
|
|
94
|
+
const item = {
|
|
95
|
+
aborted: false,
|
|
96
|
+
key,
|
|
97
|
+
batch: null,
|
|
98
|
+
resolve: throwFatalError,
|
|
99
|
+
reject: throwFatalError
|
|
100
|
+
};
|
|
101
|
+
const promise = new Promise((resolve, reject)=>{
|
|
102
|
+
item.reject = reject;
|
|
103
|
+
item.resolve = resolve;
|
|
104
|
+
if (!pendingItems) {
|
|
105
|
+
pendingItems = [];
|
|
106
|
+
}
|
|
107
|
+
pendingItems.push(item);
|
|
108
|
+
});
|
|
109
|
+
if (!dispatchTimer) {
|
|
110
|
+
dispatchTimer = setTimeout(dispatch);
|
|
111
|
+
}
|
|
112
|
+
const cancel = ()=>{
|
|
113
|
+
item.aborted = true;
|
|
114
|
+
if (item.batch?.items.every((item)=>item.aborted)) {
|
|
115
|
+
// All items in the batch have been cancelled
|
|
116
|
+
item.batch.cancel();
|
|
117
|
+
item.batch = null;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
return {
|
|
121
|
+
promise,
|
|
122
|
+
cancel
|
|
123
|
+
};
|
|
94
124
|
}
|
|
95
|
-
|
|
96
|
-
var cancel = function cancel() {
|
|
97
|
-
batchItem.cancelled = true;
|
|
98
|
-
|
|
99
|
-
if (thisBatch.items.some(function (item) {
|
|
100
|
-
return !item.cancelled;
|
|
101
|
-
})) {
|
|
102
|
-
// there are still things that can be resolved
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
thisBatch.cancel();
|
|
107
|
-
};
|
|
108
|
-
|
|
109
125
|
return {
|
|
110
|
-
|
|
111
|
-
cancel: cancel
|
|
126
|
+
load
|
|
112
127
|
};
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return {
|
|
116
|
-
load: load
|
|
117
|
-
};
|
|
118
128
|
}
|
|
119
129
|
|
|
120
130
|
function httpBatchLink(opts) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
131
|
+
// initialized config
|
|
132
|
+
return (runtime)=>{
|
|
133
|
+
const maxURLLength = opts.maxURLLength || Infinity;
|
|
134
|
+
const batchLoader = (type)=>{
|
|
135
|
+
const validate = (batchOps)=>{
|
|
136
|
+
if (maxURLLength === Infinity) {
|
|
137
|
+
// escape hatch for quick calcs
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
const path = batchOps.map((op)=>op.path).join(',');
|
|
141
|
+
const inputs = batchOps.map((op)=>op.input);
|
|
142
|
+
const url = httpUtils.getUrl({
|
|
143
|
+
url: opts.url,
|
|
144
|
+
runtime,
|
|
145
|
+
type,
|
|
146
|
+
path,
|
|
147
|
+
inputs
|
|
148
|
+
});
|
|
149
|
+
return url.length <= maxURLLength;
|
|
150
|
+
};
|
|
151
|
+
const fetch = (batchOps)=>{
|
|
152
|
+
const path = batchOps.map((op)=>op.path).join(',');
|
|
153
|
+
const inputs = batchOps.map((op)=>op.input);
|
|
154
|
+
const { promise , cancel } = httpUtils.httpRequest({
|
|
155
|
+
url: opts.url,
|
|
156
|
+
runtime,
|
|
157
|
+
type,
|
|
158
|
+
path,
|
|
159
|
+
inputs
|
|
160
|
+
});
|
|
161
|
+
return {
|
|
162
|
+
promise: promise.then((res)=>{
|
|
163
|
+
const resJSON = Array.isArray(res.json) ? res.json : batchOps.map(()=>res.json);
|
|
164
|
+
const result = resJSON.map((item)=>({
|
|
165
|
+
meta: res.meta,
|
|
166
|
+
json: item
|
|
167
|
+
}));
|
|
168
|
+
return result;
|
|
169
|
+
}),
|
|
170
|
+
cancel
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
return {
|
|
174
|
+
validate,
|
|
175
|
+
fetch
|
|
176
|
+
};
|
|
157
177
|
};
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
cancel();
|
|
178
|
+
const query = dataLoader(batchLoader('query'));
|
|
179
|
+
const mutation = dataLoader(batchLoader('mutation'));
|
|
180
|
+
const subscription = dataLoader(batchLoader('subscription'));
|
|
181
|
+
const loaders = {
|
|
182
|
+
query,
|
|
183
|
+
subscription,
|
|
184
|
+
mutation
|
|
185
|
+
};
|
|
186
|
+
return ({ op })=>{
|
|
187
|
+
return observable.observable((observer)=>{
|
|
188
|
+
const loader = loaders[op.type];
|
|
189
|
+
const { promise , cancel } = loader.load(op);
|
|
190
|
+
promise.then((res)=>{
|
|
191
|
+
const transformed = transformResult.transformResult(res.json, runtime);
|
|
192
|
+
if (!transformed.ok) {
|
|
193
|
+
observer.error(transformResult.TRPCClientError.from(transformed.error, {
|
|
194
|
+
meta: res.meta
|
|
195
|
+
}));
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
observer.next({
|
|
199
|
+
context: res.meta,
|
|
200
|
+
result: transformed.result
|
|
201
|
+
});
|
|
202
|
+
observer.complete();
|
|
203
|
+
}).catch((err)=>observer.error(transformResult.TRPCClientError.from(err)));
|
|
204
|
+
return ()=>{
|
|
205
|
+
cancel();
|
|
206
|
+
};
|
|
207
|
+
});
|
|
189
208
|
};
|
|
190
|
-
});
|
|
191
209
|
};
|
|
192
|
-
};
|
|
193
210
|
}
|
|
194
211
|
|
|
195
212
|
exports.httpBatchLink = httpBatchLink;
|