import { getRequestEvent, query } from '$app/server';
import { publisherQuerySchema } from '$lib/schema/publisher';
import { createQueryParams } from '$lib/utils';
import { error } from '@sveltejs/kit';

export const listPublishers = query(publisherQuerySchema, async (data) => {
	const { locals } = getRequestEvent();

	const params = createQueryParams(data);

	const response = await locals.api.get(`/publishers?${params.toString()}`);

	if (!response.ok) error(500, 'An unkown error occurred');

	return await response.json();
});
