INNER CODE UNIT · TypeScript
batch
logaretm/villus · packages/batch/src/index.ts:27
export function batch(opts?: Partial<BatchOptions>) {
const { fetch, timeout, maxOperationCount } = { ...defaultOpts(), ...(opts || {}) };
const fetchPluginInstance = fetchPlugin({ fetch });
let operations: { resolveOp: (r: any, opIdx: number, err?: Error) => void; body: string }[] = [];
let scheduledConsume: any;
return definePlugin(function batchPlugin(ctx) {
const { useResult, opContext, operation } = ctx;
if (opts?.exclude?.(ctx.operation, ctx)) {
return fetchPluginInstance(ctx);
}
async function consume() {
const pending = operations;
const body = `[${operations.map(o => o.body).join(',')}]`;
const fetchOpts = mergeFetchOpts(opContext, { headers: {}, body });