batch individual queries into one single request jsr.io/@mary/batch-fetch
typescript jsr
TypeScript 100.0%
2 1 0

Clone this repository

https://tangled.org/mary.my.id/pkg-batch-fetch
git@tangled.org:mary.my.id/pkg-batch-fetch

For self-hosted knots, clone URLs may differ based on your setup.

README.md

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]);
	// ...
}