-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
createHTTPBatchLink.ts
133 lines (121 loc) · 3.62 KB
/
createHTTPBatchLink.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { observable } from '@trpc/server/observable';
import type {
AnyRootTypes,
AnyRouter,
inferClientTypes,
ProcedureType,
} from '@trpc/server/unstable-core-do-not-import';
import { transformResult } from '@trpc/server/unstable-core-do-not-import';
import { dataLoader } from '../../internals/dataLoader';
import { TRPCClientError } from '../../TRPCClientError';
import type { HTTPBatchLinkOptions } from '../HTTPBatchLinkOptions';
import type {
CancelFn,
Operation,
TRPCClientRuntime,
TRPCLink,
} from '../types';
import type { HTTPResult, ResolvedHTTPLinkOptions } from './httpUtils';
import { getUrl, resolveHTTPLinkOptions } from './httpUtils';
/**
* @internal
*/
export type RequesterFn<TOptions extends HTTPBatchLinkOptions<AnyRootTypes>> = (
requesterOpts: ResolvedHTTPLinkOptions & {
runtime: TRPCClientRuntime;
type: ProcedureType;
opts: TOptions;
},
) => (
batchOps: Operation[],
unitResolver: (index: number, value: NonNullable<HTTPResult>) => void,
) => {
promise: Promise<HTTPResult[]>;
cancel: CancelFn;
};
/**
* @internal
*/
export function createHTTPBatchLink(
requester: RequesterFn<HTTPBatchLinkOptions<AnyRootTypes>>,
) {
return function httpBatchLink<TRouter extends AnyRouter>(
opts: HTTPBatchLinkOptions<inferClientTypes<TRouter>>,
): TRPCLink<TRouter> {
const resolvedOpts = resolveHTTPLinkOptions(opts);
const maxURLLength = opts.maxURLLength ?? Infinity;
// initialized config
return (runtime) => {
const batchLoader = (type: ProcedureType) => {
const validate = (batchOps: Operation[]) => {
if (maxURLLength === Infinity) {
// escape hatch for quick calcs
return true;
}
const path = batchOps.map((op) => op.path).join(',');
const inputs = batchOps.map((op) => op.input);
const url = getUrl({
...resolvedOpts,
type,
path,
inputs,
});
return url.length <= maxURLLength;
};
const fetch = requester({
...resolvedOpts,
runtime,
type,
opts,
});
return { validate, fetch };
};
const query = dataLoader<Operation, HTTPResult>(batchLoader('query'));
const mutation = dataLoader<Operation, HTTPResult>(
batchLoader('mutation'),
);
const subscription = dataLoader<Operation, HTTPResult>(
batchLoader('subscription'),
);
const loaders = { query, subscription, mutation };
return ({ op }) => {
return observable((observer) => {
const loader = loaders[op.type];
const { promise, cancel } = loader.load(op);
let _res = undefined as HTTPResult | undefined;
promise
.then((res) => {
_res = res;
const transformed = transformResult(
res.json,
resolvedOpts.transformer.output,
);
if (!transformed.ok) {
observer.error(
TRPCClientError.from(transformed.error, {
meta: res.meta,
}),
);
return;
}
observer.next({
context: res.meta,
result: transformed.result,
});
observer.complete();
})
.catch((err) => {
observer.error(
TRPCClientError.from(err, {
meta: _res?.meta,
}),
);
});
return () => {
cancel();
};
});
};
};
};
}