-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
httpBatchLink.ts
137 lines (125 loc) · 3.98 KB
/
httpBatchLink.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
134
135
136
137
import type { AnyRouter, ProcedureType } from '@trpc/server';
import { observable } from '@trpc/server/observable';
import { transformResult } from '@trpc/server/unstable-core-do-not-import';
import type { BatchLoader } from '../internals/dataLoader';
import { dataLoader } from '../internals/dataLoader';
import type { NonEmptyArray } from '../internals/types';
import { TRPCClientError } from '../TRPCClientError';
import type { HTTPBatchLinkOptions } from './HTTPBatchLinkOptions';
import type { HTTPResult } from './internals/httpUtils';
import {
getUrl,
jsonHttpRequester,
resolveHTTPLinkOptions,
} from './internals/httpUtils';
import type { Operation, TRPCLink } from './types';
/**
* @see https://trpc.io/docs/client/links/httpBatchLink
*/
export function httpBatchLink<TRouter extends AnyRouter>(
opts: HTTPBatchLinkOptions<TRouter['_def']['_config']['$types']>,
): TRPCLink<TRouter> {
const resolvedOpts = resolveHTTPLinkOptions(opts);
const maxURLLength = opts.maxURLLength ?? Infinity;
return () => {
const batchLoader = (
type: ProcedureType,
): BatchLoader<Operation, HTTPResult> => {
return {
validate(batchOps) {
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;
},
fetch(batchOps) {
const path = batchOps.map((op) => op.path).join(',');
const inputs = batchOps.map((op) => op.input);
const requester = jsonHttpRequester({
...resolvedOpts,
path,
inputs,
type,
headers() {
if (!opts.headers) {
return {};
}
if (typeof opts.headers === 'function') {
return opts.headers({
opList: batchOps as NonEmptyArray<Operation>,
});
}
return opts.headers;
},
});
return {
cancel: requester.cancel,
promise: requester.promise.then((res) => {
const resJSON = Array.isArray(res.json)
? res.json
: batchOps.map(() => res.json);
const result = resJSON.map((item) => ({
meta: res.meta,
json: item,
}));
return result;
}),
};
},
};
};
const query = dataLoader(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();
};
});
};
};
}