1
0
Fork
You've already forked pkg-batch-fetch
0
utility for batching individual queries into one single request
  • TypeScript 100%
2025年08月14日 09:30:22 +07:00
.vscode initial commit 2025年04月26日 15:26:54 +07:00
lib initial commit 2025年04月26日 15:26:54 +07:00
deno.json initial commit 2025年04月26日 15:26:54 +07:00
LICENSE initial commit 2025年04月26日 15:26:54 +07:00
README.md docs: add links to readme 2025年08月14日 09:30:22 +07:00

batch-fetch

JSR | source code

utility for batching individual queries into one single request.

type UserId = ReturnType<typeof crypto.randomUUID>;
interface User {
	id: UserId;
	name: string;
}
const fetchUser = createBatchedFetch<UserId, User>({
	limit: 50,
	async fetch(userIds, signal) {
		const url = new URL(`/api/users`, location.origin);
		for (const id of userIds) {
			url.searchParams.append('ids', id);
		}
		const response = await fetch(url, { signal });
		return await response.json() as unknown as User[];
	},
	idFromResource: (user) => user.id,
});
// these individual queries will be batched into one
{
	const p1 = fetchUser('c83e7271-c865-4eae-8c6a-d6f21acb17c1');
	const p2 = fetchUser('306e38a2-bea6-4bf7-80ea-21822aa24c40');
	const [user1, user2] = await Promise.all([p1, p2]);
	// ...
}