(
method: 'GET' | 'POST',
group: BatchLinkPluginGroup<T>,
groupItems: typeof this.pending extends Map<any, infer U> ? U : never,
)
| 210 | } |
| 211 | |
| 212 | async #executeBatch( |
| 213 | method: 'GET' | 'POST', |
| 214 | group: BatchLinkPluginGroup<T>, |
| 215 | groupItems: typeof this.pending extends Map<any, infer U> ? U : never, |
| 216 | ): Promise<void> { |
| 217 | if (!groupItems.length) { |
| 218 | return |
| 219 | } |
| 220 | |
| 221 | const batchItems = groupItems as [typeof groupItems[number], ...typeof groupItems[number][]] |
| 222 | |
| 223 | if (batchItems.length === 1) { |
| 224 | batchItems[0][0].next().then(batchItems[0][1]).catch(batchItems[0][2]) |
| 225 | return |
| 226 | } |
| 227 | |
| 228 | try { |
| 229 | const options = batchItems.map(([options]) => options) as [ |
| 230 | InterceptorOptions<StandardLinkClientInterceptorOptions<T>, Promise<StandardLazyResponse>>, |
| 231 | ...InterceptorOptions<StandardLinkClientInterceptorOptions<T>, Promise<StandardLazyResponse>>[], |
| 232 | ] |
| 233 | |
| 234 | const maxSize = await value(this.maxSize, options) |
| 235 | |
| 236 | if (batchItems.length > maxSize) { |
| 237 | const [first, second] = splitInHalf(batchItems) |
| 238 | this.#executeBatch(method, group, first) |
| 239 | this.#executeBatch(method, group, second) |
| 240 | return |
| 241 | } |
| 242 | |
| 243 | const batchUrl = new URL(await value(this.batchUrl, options)) |
| 244 | const batchHeaders = await value(this.batchHeaders, options) |
| 245 | const mappedItems = batchItems.map(([options]) => this.mapRequestItem({ ...options, batchUrl, batchHeaders })) |
| 246 | |
| 247 | const batchRequest = toBatchRequest({ |
| 248 | method, |
| 249 | url: batchUrl, |
| 250 | headers: batchHeaders, |
| 251 | requests: mappedItems, |
| 252 | }) |
| 253 | |
| 254 | const maxUrlLength = await value(this.maxUrlLength, options) |
| 255 | |
| 256 | if (batchRequest.url.toString().length > maxUrlLength) { |
| 257 | const [first, second] = splitInHalf(batchItems) |
| 258 | this.#executeBatch(method, group, first) |
| 259 | this.#executeBatch(method, group, second) |
| 260 | return |
| 261 | } |
| 262 | |
| 263 | const mode = value(this.mode, options) |
| 264 | |
| 265 | try { |
| 266 | const lazyResponse = await options[0].next({ |
| 267 | request: { ...batchRequest, headers: { ...batchRequest.headers, 'x-orpc-batch': mode } }, |
| 268 | signal: batchRequest.signal, |
| 269 | context: group.context, |
no test coverage detected