@trpc/client 10.28.1 → 10.29.0
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 +1 -1
- package/dist/createTRPCClientProxy.d.ts.map +1 -1
- package/dist/httpBatchLink-38dcf5f2.js +243 -0
- package/dist/httpBatchLink-e66c0674.mjs +240 -0
- package/dist/httpBatchLink-f35df745.js +239 -0
- package/dist/{httpUtils-00c8b54e.js → httpUtils-145e783b.js} +27 -23
- package/dist/{httpUtils-90a6bccb.js → httpUtils-21cbb35d.js} +27 -20
- package/dist/{httpUtils-8a5c637a.mjs → httpUtils-b2fcc20c.mjs} +26 -21
- package/dist/index.js +149 -4
- package/dist/index.mjs +151 -6
- package/dist/internals/TRPCUntypedClient.d.ts +1 -1
- package/dist/internals/TRPCUntypedClient.d.ts.map +1 -1
- package/dist/internals/dataLoader.d.ts +1 -1
- package/dist/internals/dataLoader.d.ts.map +1 -1
- package/dist/internals/types.d.ts +2 -0
- package/dist/internals/types.d.ts.map +1 -1
- package/dist/links/HTTPBatchLinkOptions.d.ts +20 -0
- package/dist/links/HTTPBatchLinkOptions.d.ts.map +1 -0
- package/dist/links/httpBatchLink.d.ts +1 -15
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +5 -217
- package/dist/links/httpBatchLink.mjs +4 -220
- package/dist/links/httpBatchStreamLink.d.ts +2 -0
- package/dist/links/httpBatchStreamLink.d.ts.map +1 -0
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +1 -1
- package/dist/links/httpLink.mjs +1 -1
- package/dist/links/index.d.ts +2 -0
- package/dist/links/index.d.ts.map +1 -1
- package/dist/links/internals/createHTTPBatchLink.d.ts +20 -0
- package/dist/links/internals/createHTTPBatchLink.d.ts.map +1 -0
- package/dist/links/internals/httpUtils.d.ts +3 -1
- package/dist/links/internals/httpUtils.d.ts.map +1 -1
- package/dist/links/internals/parseJSONStream.d.ts +36 -0
- package/dist/links/internals/parseJSONStream.d.ts.map +1 -0
- package/dist/links/types.d.ts +1 -1
- package/dist/links/types.d.ts.map +1 -1
- package/dist/links/wsLink.d.ts +1 -1
- package/dist/links/wsLink.d.ts.map +1 -1
- package/dist/shared/transformResult.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/TRPCClientError.ts +1 -1
- package/src/createTRPCClient.ts +1 -1
- package/src/createTRPCClientProxy.ts +1 -1
- package/src/internals/TRPCUntypedClient.ts +2 -2
- package/src/internals/dataLoader.ts +21 -7
- package/src/internals/types.ts +1 -0
- package/src/links/HTTPBatchLinkOptions.ts +21 -0
- package/src/links/httpBatchLink.ts +44 -133
- package/src/links/httpBatchStreamLink.ts +47 -0
- package/src/links/httpFormDataLink.ts +1 -1
- package/src/links/httpLink.ts +2 -2
- package/src/links/index.ts +2 -0
- package/src/links/internals/createHTTPBatchLink.ts +113 -0
- package/src/links/internals/httpUtils.ts +33 -24
- package/src/links/internals/parseJSONStream.ts +165 -0
- package/src/links/retryLink.ts +1 -1
- package/src/links/types.ts +1 -1
- package/src/links/wsLink.ts +3 -3
- package/src/shared/transformResult.ts +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnyProcedure, AnyRouter, DefaultErrorShape,
|
|
1
|
+
import { AnyProcedure, AnyRouter, DefaultErrorShape, inferRouterError, Maybe } from '@trpc/server';
|
|
2
2
|
import { TRPCErrorResponse, TRPCErrorShape } from '@trpc/server/rpc';
|
|
3
3
|
type ErrorInferrable = AnyRouter | AnyProcedure | TRPCErrorShape<number>;
|
|
4
4
|
type inferErrorShape<TInferrable extends ErrorInferrable> = TInferrable extends AnyRouter ? inferRouterError<TInferrable> : TInferrable extends AnyProcedure ? TInferrable['_def']['_config']['$types']['errorShape'] : TInferrable;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TRPCClientError.d.ts","sourceRoot":"","sources":["../src/TRPCClientError.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,SAAS,EACT,iBAAiB,EACjB,
|
|
1
|
+
{"version":3,"file":"TRPCClientError.d.ts","sourceRoot":"","sources":["../src/TRPCClientError.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,SAAS,EACT,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,EACN,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAErE,KAAK,eAAe,GAAG,SAAS,GAAG,YAAY,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;AAEzE,KAAK,eAAe,CAAC,WAAW,SAAS,eAAe,IACtD,WAAW,SAAS,SAAS,GACzB,gBAAgB,CAAC,WAAW,CAAC,GAC7B,WAAW,SAAS,YAAY,GAChC,WAAW,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,CAAC,YAAY,CAAC,GACtD,WAAW,CAAC;AAElB,MAAM,WAAW,mBAAmB,CAAC,MAAM,SAAS,iBAAiB;IACnE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAC9B,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;CACtC;AACD,MAAM,MAAM,mBAAmB,CAAC,kBAAkB,SAAS,eAAe,IACxE,mBAAmB,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC,CAAC;AAE3D,qBAAa,eAAe,CAAC,kBAAkB,SAAS,eAAe,CACrE,SAAQ,KACR,YAAW,mBAAmB,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC;IAEnE,SAAgB,KAAK,oBAAC;IACtB,SAAgB,KAAK,EAAE,KAAK,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC,CAAC;IAClE,SAAgB,IAAI,EAAE,KAAK,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;IACzE,SAAgB,IAAI,sCAAC;gBAGnB,OAAO,EAAE,MAAM,EACf,IAAI,CAAC,EAAE;QACL,MAAM,CAAC,EAAE,KAAK,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC,CAAC;QACpD,KAAK,CAAC,EAAE,KAAK,CAAC;QACd,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KAChC;WAkBW,IAAI,CAAC,kBAAkB,SAAS,eAAe,EAC3D,KAAK,EAAE,KAAK,GAAG,iBAAiB,CAAC,GAAG,CAAC,EACrC,IAAI,GAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAO,GAC5C,eAAe,CAAC,kBAAkB,CAAC;CAqBvC"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { AnyRouter, inferProcedureInput, inferProcedureOutput, inferSubscriptionOutput } from '@trpc/server';
|
|
2
2
|
import { Unsubscribable } from '@trpc/server/observable';
|
|
3
3
|
import { inferTransformedProcedureOutput } from '@trpc/server/shared';
|
|
4
|
-
import { TRPCClientError } from './TRPCClientError';
|
|
5
4
|
import { CreateTRPCClientOptions, TRPCRequestOptions, TRPCSubscriptionObserver } from './internals/TRPCUntypedClient';
|
|
6
5
|
import { TRPCClientRuntime } from './links';
|
|
6
|
+
import { TRPCClientError } from './TRPCClientError';
|
|
7
7
|
/**
|
|
8
8
|
* @deprecated
|
|
9
9
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createTRPCClient.d.ts","sourceRoot":"","sources":["../src/createTRPCClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,mBAAmB,E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C,UAAU,CAAC,KAAK,CAAC,CAAC,EAErD,IAAI,EAAE,KAAK,EACX,KAAK,CAAC,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,kBAAkB,GACxB,OAAO,CAAC,+BAA+B,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAE/D,YAAY,CACV,cAAc,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,eAAe,CAAC,EACvD,KAAK,SAAS,MAAM,GAAG,MAAM,cAAc,EAE3C,OAAO,SAAS,uBAAuB,CAAC,OAAO,EAAE,KAAK,CAAC,EACvD,MAAM,SAAS,mBAAmB,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,EAEzD,IAAI,EAAE,KAAK,EACX,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,kBAAkB,GACtB,OAAO,CAAC,wBAAwB,CAAC,OAAO,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC,GACrE,cAAc,CAAC;CACnB;AACD;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,SAAS,SAAS,EACxD,IAAI,EAAE,uBAAuB,CAAC,OAAO,CAAC,uBAIvC"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { AnyMutationProcedure, AnyProcedure, AnyQueryProcedure, AnyRouter, AnySubscriptionProcedure, IntersectionError, ProcedureArgs, ProcedureRouterRecord, ProcedureType } from '@trpc/server';
|
|
2
2
|
import type { Unsubscribable } from '@trpc/server/observable';
|
|
3
3
|
import { inferTransformedProcedureOutput, inferTransformedSubscriptionOutput } from '@trpc/server/shared';
|
|
4
|
-
import { TRPCClientError } from './TRPCClientError';
|
|
5
4
|
import { TRPCClient } from './createTRPCClient';
|
|
6
5
|
import { CreateTRPCClientOptions } from './createTRPCUntypedClient';
|
|
7
6
|
import { TRPCSubscriptionObserver, UntypedClientProperties } from './internals/TRPCUntypedClient';
|
|
7
|
+
import { TRPCClientError } from './TRPCClientError';
|
|
8
8
|
/** @public */
|
|
9
9
|
export type inferRouterProxyClient<TRouter extends AnyRouter> = DecoratedProcedureRecord<TRouter['_def']['record']>;
|
|
10
10
|
/** @internal */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createTRPCClientProxy.d.ts","sourceRoot":"","sources":["../src/createTRPCClientProxy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,oBAAoB,EACpB,YAAY,EACZ,iBAAiB,EACjB,SAAS,EACT,wBAAwB,EACxB,iBAAiB,EACjB,aAAa,EACb,qBAAqB,EACrB,aAAa,EACd,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAGL,+BAA+B,EAC/B,kCAAkC,EACnC,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"createTRPCClientProxy.d.ts","sourceRoot":"","sources":["../src/createTRPCClientProxy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,oBAAoB,EACpB,YAAY,EACZ,iBAAiB,EACjB,SAAS,EACT,wBAAwB,EACxB,iBAAiB,EACjB,aAAa,EACb,qBAAqB,EACrB,aAAa,EACd,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAGL,+BAA+B,EAC/B,kCAAkC,EACnC,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EACL,wBAAwB,EAExB,uBAAuB,EACxB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,cAAc;AACd,MAAM,MAAM,sBAAsB,CAAC,OAAO,SAAS,SAAS,IAC1D,wBAAwB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,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;AAQF,gBAAgB;AAChB,eAAO,MAAM,6BAA6B,mBACxB,MAAM,KACrB,aAEF,CAAC;AAEF,MAAM,MAAM,qBAAqB,CAAC,OAAO,SAAS,SAAS,IACzD,wBAAwB,CACtB,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAC1B,SAAS,MAAM,gBAAgB,GAC5B,uBAAuB,GAAG,MAAM,gBAAgB,SAAS,KAAK,GAC5D,gBAAgB,GAChB,iBAAiB,CAAC,uBAAuB,GAAG,MAAM,gBAAgB,CAAC,GACrE,KAAK,CAAC;AAEZ;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,SAAS,SAAS,EAC7D,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,kCAe5B;AAED,wBAAgB,qBAAqB,CAAC,OAAO,SAAS,SAAS,EAC7D,IAAI,EAAE,uBAAuB,CAAC,OAAO,CAAC,kCAKvC"}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var observable = require('@trpc/server/observable');
|
|
4
|
+
var transformResult = require('./transformResult-70f95ffb.js');
|
|
5
|
+
var httpUtils = require('./httpUtils-21cbb35d.js');
|
|
6
|
+
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */ /**
|
|
8
|
+
* A function that should never be called unless we messed something up.
|
|
9
|
+
*/ const throwFatalError = ()=>{
|
|
10
|
+
throw new Error('Something went wrong. Please submit an issue at https://github.com/trpc/trpc/issues/new');
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Dataloader that's very inspired by https://github.com/graphql/dataloader
|
|
14
|
+
* Less configuration, no caching, and allows you to cancel requests
|
|
15
|
+
* When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
|
|
16
|
+
*/ function dataLoader(batchLoader) {
|
|
17
|
+
let pendingItems = null;
|
|
18
|
+
let dispatchTimer = null;
|
|
19
|
+
const destroyTimerAndPendingItems = ()=>{
|
|
20
|
+
clearTimeout(dispatchTimer);
|
|
21
|
+
dispatchTimer = null;
|
|
22
|
+
pendingItems = null;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Iterate through the items and split them into groups based on the `batchLoader`'s validate function
|
|
26
|
+
*/ function groupItems(items) {
|
|
27
|
+
const groupedItems = [
|
|
28
|
+
[]
|
|
29
|
+
];
|
|
30
|
+
let index = 0;
|
|
31
|
+
while(true){
|
|
32
|
+
const item = items[index];
|
|
33
|
+
if (!item) {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
const lastGroup = groupedItems[groupedItems.length - 1];
|
|
37
|
+
if (item.aborted) {
|
|
38
|
+
// Item was aborted before it was dispatched
|
|
39
|
+
item.reject?.(new Error('Aborted'));
|
|
40
|
+
index++;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const isValid = batchLoader.validate(lastGroup.concat(item).map((it)=>it.key));
|
|
44
|
+
if (isValid) {
|
|
45
|
+
lastGroup.push(item);
|
|
46
|
+
index++;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (lastGroup.length === 0) {
|
|
50
|
+
item.reject?.(new Error('Input is too big for a single dispatch'));
|
|
51
|
+
index++;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
// Create new group, next iteration will try to add the item to that
|
|
55
|
+
groupedItems.push([]);
|
|
56
|
+
}
|
|
57
|
+
return groupedItems;
|
|
58
|
+
}
|
|
59
|
+
function dispatch() {
|
|
60
|
+
const groupedItems = groupItems(pendingItems);
|
|
61
|
+
destroyTimerAndPendingItems();
|
|
62
|
+
// Create batches for each group of items
|
|
63
|
+
for (const items of groupedItems){
|
|
64
|
+
if (!items.length) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const batch = {
|
|
68
|
+
items,
|
|
69
|
+
cancel: throwFatalError
|
|
70
|
+
};
|
|
71
|
+
for (const item of items){
|
|
72
|
+
item.batch = batch;
|
|
73
|
+
}
|
|
74
|
+
const unitResolver = (index, value)=>{
|
|
75
|
+
const item = batch.items[index];
|
|
76
|
+
item.resolve?.(value);
|
|
77
|
+
item.batch = null;
|
|
78
|
+
item.reject = null;
|
|
79
|
+
item.resolve = null;
|
|
80
|
+
};
|
|
81
|
+
const { promise , cancel } = batchLoader.fetch(batch.items.map((_item)=>_item.key), unitResolver);
|
|
82
|
+
batch.cancel = cancel;
|
|
83
|
+
promise.then((result)=>{
|
|
84
|
+
for(let i = 0; i < result.length; i++){
|
|
85
|
+
const value = result[i];
|
|
86
|
+
unitResolver(i, value);
|
|
87
|
+
}
|
|
88
|
+
for(let i1 = 0; i1 < batch.items.length; i1++){
|
|
89
|
+
const item = batch.items[i1];
|
|
90
|
+
item.reject?.(new Error('Missing result'));
|
|
91
|
+
item.batch = null;
|
|
92
|
+
}
|
|
93
|
+
}).catch((cause)=>{
|
|
94
|
+
for (const item of batch.items){
|
|
95
|
+
item.reject?.(cause);
|
|
96
|
+
item.batch = null;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function load(key) {
|
|
102
|
+
const item = {
|
|
103
|
+
aborted: false,
|
|
104
|
+
key,
|
|
105
|
+
batch: null,
|
|
106
|
+
resolve: throwFatalError,
|
|
107
|
+
reject: throwFatalError
|
|
108
|
+
};
|
|
109
|
+
const promise = new Promise((resolve, reject)=>{
|
|
110
|
+
item.reject = reject;
|
|
111
|
+
item.resolve = resolve;
|
|
112
|
+
if (!pendingItems) {
|
|
113
|
+
pendingItems = [];
|
|
114
|
+
}
|
|
115
|
+
pendingItems.push(item);
|
|
116
|
+
});
|
|
117
|
+
if (!dispatchTimer) {
|
|
118
|
+
dispatchTimer = setTimeout(dispatch);
|
|
119
|
+
}
|
|
120
|
+
const cancel = ()=>{
|
|
121
|
+
item.aborted = true;
|
|
122
|
+
if (item.batch?.items.every((item)=>item.aborted)) {
|
|
123
|
+
// All items in the batch have been cancelled
|
|
124
|
+
item.batch.cancel();
|
|
125
|
+
item.batch = null;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
return {
|
|
129
|
+
promise,
|
|
130
|
+
cancel
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
load
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* @internal
|
|
140
|
+
*/ function createHTTPBatchLink(requester) {
|
|
141
|
+
return function httpBatchLink(opts) {
|
|
142
|
+
const resolvedOpts = httpUtils.resolveHTTPLinkOptions(opts);
|
|
143
|
+
const maxURLLength = opts.maxURLLength || Infinity;
|
|
144
|
+
// initialized config
|
|
145
|
+
return (runtime)=>{
|
|
146
|
+
const batchLoader = (type)=>{
|
|
147
|
+
const validate = (batchOps)=>{
|
|
148
|
+
if (maxURLLength === Infinity) {
|
|
149
|
+
// escape hatch for quick calcs
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
const path = batchOps.map((op)=>op.path).join(',');
|
|
153
|
+
const inputs = batchOps.map((op)=>op.input);
|
|
154
|
+
const url = httpUtils.getUrl({
|
|
155
|
+
...resolvedOpts,
|
|
156
|
+
runtime,
|
|
157
|
+
type,
|
|
158
|
+
path,
|
|
159
|
+
inputs
|
|
160
|
+
});
|
|
161
|
+
return url.length <= maxURLLength;
|
|
162
|
+
};
|
|
163
|
+
const fetch = requester({
|
|
164
|
+
...resolvedOpts,
|
|
165
|
+
runtime,
|
|
166
|
+
type,
|
|
167
|
+
opts
|
|
168
|
+
});
|
|
169
|
+
return {
|
|
170
|
+
validate,
|
|
171
|
+
fetch
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
const query = dataLoader(batchLoader('query'));
|
|
175
|
+
const mutation = dataLoader(batchLoader('mutation'));
|
|
176
|
+
const subscription = dataLoader(batchLoader('subscription'));
|
|
177
|
+
const loaders = {
|
|
178
|
+
query,
|
|
179
|
+
subscription,
|
|
180
|
+
mutation
|
|
181
|
+
};
|
|
182
|
+
return ({ op })=>{
|
|
183
|
+
return observable.observable((observer)=>{
|
|
184
|
+
const loader = loaders[op.type];
|
|
185
|
+
const { promise , cancel } = loader.load(op);
|
|
186
|
+
promise.then((res)=>{
|
|
187
|
+
const transformed = transformResult.transformResult(res.json, runtime);
|
|
188
|
+
if (!transformed.ok) {
|
|
189
|
+
observer.error(transformResult.TRPCClientError.from(transformed.error, {
|
|
190
|
+
meta: res.meta
|
|
191
|
+
}));
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
observer.next({
|
|
195
|
+
context: res.meta,
|
|
196
|
+
result: transformed.result
|
|
197
|
+
});
|
|
198
|
+
observer.complete();
|
|
199
|
+
}).catch((err)=>observer.error(transformResult.TRPCClientError.from(err)));
|
|
200
|
+
return ()=>cancel();
|
|
201
|
+
});
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const batchRequester = (requesterOpts)=>{
|
|
208
|
+
return (batchOps)=>{
|
|
209
|
+
const path = batchOps.map((op)=>op.path).join(',');
|
|
210
|
+
const inputs = batchOps.map((op)=>op.input);
|
|
211
|
+
const { promise , cancel } = httpUtils.jsonHttpRequester({
|
|
212
|
+
...requesterOpts,
|
|
213
|
+
path,
|
|
214
|
+
inputs,
|
|
215
|
+
headers () {
|
|
216
|
+
if (!requesterOpts.opts.headers) {
|
|
217
|
+
return {};
|
|
218
|
+
}
|
|
219
|
+
if (typeof requesterOpts.opts.headers === 'function') {
|
|
220
|
+
return requesterOpts.opts.headers({
|
|
221
|
+
opList: batchOps
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
return requesterOpts.opts.headers;
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
return {
|
|
228
|
+
promise: promise.then((res)=>{
|
|
229
|
+
const resJSON = Array.isArray(res.json) ? res.json : batchOps.map(()=>res.json);
|
|
230
|
+
const result = resJSON.map((item)=>({
|
|
231
|
+
meta: res.meta,
|
|
232
|
+
json: item
|
|
233
|
+
}));
|
|
234
|
+
return result;
|
|
235
|
+
}),
|
|
236
|
+
cancel
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
const httpBatchLink = createHTTPBatchLink(batchRequester);
|
|
241
|
+
|
|
242
|
+
exports.createHTTPBatchLink = createHTTPBatchLink;
|
|
243
|
+
exports.httpBatchLink = httpBatchLink;
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { observable } from '@trpc/server/observable';
|
|
2
|
+
import { t as transformResult, T as TRPCClientError } from './transformResult-9a244fe7.mjs';
|
|
3
|
+
import { r as resolveHTTPLinkOptions, g as getUrl, j as jsonHttpRequester } from './httpUtils-b2fcc20c.mjs';
|
|
4
|
+
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */ /**
|
|
6
|
+
* A function that should never be called unless we messed something up.
|
|
7
|
+
*/ const throwFatalError = ()=>{
|
|
8
|
+
throw new Error('Something went wrong. Please submit an issue at https://github.com/trpc/trpc/issues/new');
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Dataloader that's very inspired by https://github.com/graphql/dataloader
|
|
12
|
+
* Less configuration, no caching, and allows you to cancel requests
|
|
13
|
+
* When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
|
|
14
|
+
*/ function dataLoader(batchLoader) {
|
|
15
|
+
let pendingItems = null;
|
|
16
|
+
let dispatchTimer = null;
|
|
17
|
+
const destroyTimerAndPendingItems = ()=>{
|
|
18
|
+
clearTimeout(dispatchTimer);
|
|
19
|
+
dispatchTimer = null;
|
|
20
|
+
pendingItems = null;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Iterate through the items and split them into groups based on the `batchLoader`'s validate function
|
|
24
|
+
*/ function groupItems(items) {
|
|
25
|
+
const groupedItems = [
|
|
26
|
+
[]
|
|
27
|
+
];
|
|
28
|
+
let index = 0;
|
|
29
|
+
while(true){
|
|
30
|
+
const item = items[index];
|
|
31
|
+
if (!item) {
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
const lastGroup = groupedItems[groupedItems.length - 1];
|
|
35
|
+
if (item.aborted) {
|
|
36
|
+
// Item was aborted before it was dispatched
|
|
37
|
+
item.reject?.(new Error('Aborted'));
|
|
38
|
+
index++;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const isValid = batchLoader.validate(lastGroup.concat(item).map((it)=>it.key));
|
|
42
|
+
if (isValid) {
|
|
43
|
+
lastGroup.push(item);
|
|
44
|
+
index++;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (lastGroup.length === 0) {
|
|
48
|
+
item.reject?.(new Error('Input is too big for a single dispatch'));
|
|
49
|
+
index++;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
// Create new group, next iteration will try to add the item to that
|
|
53
|
+
groupedItems.push([]);
|
|
54
|
+
}
|
|
55
|
+
return groupedItems;
|
|
56
|
+
}
|
|
57
|
+
function dispatch() {
|
|
58
|
+
const groupedItems = groupItems(pendingItems);
|
|
59
|
+
destroyTimerAndPendingItems();
|
|
60
|
+
// Create batches for each group of items
|
|
61
|
+
for (const items of groupedItems){
|
|
62
|
+
if (!items.length) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const batch = {
|
|
66
|
+
items,
|
|
67
|
+
cancel: throwFatalError
|
|
68
|
+
};
|
|
69
|
+
for (const item of items){
|
|
70
|
+
item.batch = batch;
|
|
71
|
+
}
|
|
72
|
+
const unitResolver = (index, value)=>{
|
|
73
|
+
const item = batch.items[index];
|
|
74
|
+
item.resolve?.(value);
|
|
75
|
+
item.batch = null;
|
|
76
|
+
item.reject = null;
|
|
77
|
+
item.resolve = null;
|
|
78
|
+
};
|
|
79
|
+
const { promise , cancel } = batchLoader.fetch(batch.items.map((_item)=>_item.key), unitResolver);
|
|
80
|
+
batch.cancel = cancel;
|
|
81
|
+
promise.then((result)=>{
|
|
82
|
+
for(let i = 0; i < result.length; i++){
|
|
83
|
+
const value = result[i];
|
|
84
|
+
unitResolver(i, value);
|
|
85
|
+
}
|
|
86
|
+
for(let i1 = 0; i1 < batch.items.length; i1++){
|
|
87
|
+
const item = batch.items[i1];
|
|
88
|
+
item.reject?.(new Error('Missing result'));
|
|
89
|
+
item.batch = null;
|
|
90
|
+
}
|
|
91
|
+
}).catch((cause)=>{
|
|
92
|
+
for (const item of batch.items){
|
|
93
|
+
item.reject?.(cause);
|
|
94
|
+
item.batch = null;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function load(key) {
|
|
100
|
+
const item = {
|
|
101
|
+
aborted: false,
|
|
102
|
+
key,
|
|
103
|
+
batch: null,
|
|
104
|
+
resolve: throwFatalError,
|
|
105
|
+
reject: throwFatalError
|
|
106
|
+
};
|
|
107
|
+
const promise = new Promise((resolve, reject)=>{
|
|
108
|
+
item.reject = reject;
|
|
109
|
+
item.resolve = resolve;
|
|
110
|
+
if (!pendingItems) {
|
|
111
|
+
pendingItems = [];
|
|
112
|
+
}
|
|
113
|
+
pendingItems.push(item);
|
|
114
|
+
});
|
|
115
|
+
if (!dispatchTimer) {
|
|
116
|
+
dispatchTimer = setTimeout(dispatch);
|
|
117
|
+
}
|
|
118
|
+
const cancel = ()=>{
|
|
119
|
+
item.aborted = true;
|
|
120
|
+
if (item.batch?.items.every((item)=>item.aborted)) {
|
|
121
|
+
// All items in the batch have been cancelled
|
|
122
|
+
item.batch.cancel();
|
|
123
|
+
item.batch = null;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
return {
|
|
127
|
+
promise,
|
|
128
|
+
cancel
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
load
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @internal
|
|
138
|
+
*/ function createHTTPBatchLink(requester) {
|
|
139
|
+
return function httpBatchLink(opts) {
|
|
140
|
+
const resolvedOpts = resolveHTTPLinkOptions(opts);
|
|
141
|
+
const maxURLLength = opts.maxURLLength || Infinity;
|
|
142
|
+
// initialized config
|
|
143
|
+
return (runtime)=>{
|
|
144
|
+
const batchLoader = (type)=>{
|
|
145
|
+
const validate = (batchOps)=>{
|
|
146
|
+
if (maxURLLength === Infinity) {
|
|
147
|
+
// escape hatch for quick calcs
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
const path = batchOps.map((op)=>op.path).join(',');
|
|
151
|
+
const inputs = batchOps.map((op)=>op.input);
|
|
152
|
+
const url = getUrl({
|
|
153
|
+
...resolvedOpts,
|
|
154
|
+
runtime,
|
|
155
|
+
type,
|
|
156
|
+
path,
|
|
157
|
+
inputs
|
|
158
|
+
});
|
|
159
|
+
return url.length <= maxURLLength;
|
|
160
|
+
};
|
|
161
|
+
const fetch = requester({
|
|
162
|
+
...resolvedOpts,
|
|
163
|
+
runtime,
|
|
164
|
+
type,
|
|
165
|
+
opts
|
|
166
|
+
});
|
|
167
|
+
return {
|
|
168
|
+
validate,
|
|
169
|
+
fetch
|
|
170
|
+
};
|
|
171
|
+
};
|
|
172
|
+
const query = dataLoader(batchLoader('query'));
|
|
173
|
+
const mutation = dataLoader(batchLoader('mutation'));
|
|
174
|
+
const subscription = dataLoader(batchLoader('subscription'));
|
|
175
|
+
const loaders = {
|
|
176
|
+
query,
|
|
177
|
+
subscription,
|
|
178
|
+
mutation
|
|
179
|
+
};
|
|
180
|
+
return ({ op })=>{
|
|
181
|
+
return observable((observer)=>{
|
|
182
|
+
const loader = loaders[op.type];
|
|
183
|
+
const { promise , cancel } = loader.load(op);
|
|
184
|
+
promise.then((res)=>{
|
|
185
|
+
const transformed = transformResult(res.json, runtime);
|
|
186
|
+
if (!transformed.ok) {
|
|
187
|
+
observer.error(TRPCClientError.from(transformed.error, {
|
|
188
|
+
meta: res.meta
|
|
189
|
+
}));
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
observer.next({
|
|
193
|
+
context: res.meta,
|
|
194
|
+
result: transformed.result
|
|
195
|
+
});
|
|
196
|
+
observer.complete();
|
|
197
|
+
}).catch((err)=>observer.error(TRPCClientError.from(err)));
|
|
198
|
+
return ()=>cancel();
|
|
199
|
+
});
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const batchRequester = (requesterOpts)=>{
|
|
206
|
+
return (batchOps)=>{
|
|
207
|
+
const path = batchOps.map((op)=>op.path).join(',');
|
|
208
|
+
const inputs = batchOps.map((op)=>op.input);
|
|
209
|
+
const { promise , cancel } = jsonHttpRequester({
|
|
210
|
+
...requesterOpts,
|
|
211
|
+
path,
|
|
212
|
+
inputs,
|
|
213
|
+
headers () {
|
|
214
|
+
if (!requesterOpts.opts.headers) {
|
|
215
|
+
return {};
|
|
216
|
+
}
|
|
217
|
+
if (typeof requesterOpts.opts.headers === 'function') {
|
|
218
|
+
return requesterOpts.opts.headers({
|
|
219
|
+
opList: batchOps
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
return requesterOpts.opts.headers;
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
return {
|
|
226
|
+
promise: promise.then((res)=>{
|
|
227
|
+
const resJSON = Array.isArray(res.json) ? res.json : batchOps.map(()=>res.json);
|
|
228
|
+
const result = resJSON.map((item)=>({
|
|
229
|
+
meta: res.meta,
|
|
230
|
+
json: item
|
|
231
|
+
}));
|
|
232
|
+
return result;
|
|
233
|
+
}),
|
|
234
|
+
cancel
|
|
235
|
+
};
|
|
236
|
+
};
|
|
237
|
+
};
|
|
238
|
+
const httpBatchLink = createHTTPBatchLink(batchRequester);
|
|
239
|
+
|
|
240
|
+
export { createHTTPBatchLink as c, httpBatchLink as h };
|