Add to api/[entity]/all proxy request to accept and push through all query parameters to backen

This commit is contained in:
qowevisa 2024-11-24 00:29:23 +02:00
parent 1785e24623
commit 85ef628560
2 changed files with 5 additions and 4 deletions

View File

@ -142,8 +142,8 @@ export type EntityType<T extends EntityName> =
// //
export async function getAll<T>(groupName: string, session?: string): Promise<T[] | ErrorMessage> { export async function getAll<T>(groupName: string, session?: string, queryParams?: string): Promise<T[] | ErrorMessage> {
const url = `${BASE_API_URL}/${groupName}/all` const url = queryParams ? `${BASE_API_URL}/${groupName}/all?${queryParams}` : `${BASE_API_URL}/${groupName}/all`
const defaultHeaders = { const defaultHeaders = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}; };

View File

@ -6,10 +6,11 @@ function isErrorMessage(value: any): value is ErrorMessage {
return value && typeof value.message === 'string'; return value && typeof value.message === 'string';
} }
export const GET: RequestHandler = async ({ cookies, params }): Promise<Response> => { export const GET: RequestHandler = async ({ cookies, params, url }): Promise<Response> => {
const session = cookies.get('session'); const session = cookies.get('session');
const { entity } = params; const { entity } = params;
const queryParams = url.searchParams.toString();
// Check if the entity is valid // Check if the entity is valid
if (!session) { if (!session) {
return new Response(JSON.stringify("no cookies"), { status: 401 }); return new Response(JSON.stringify("no cookies"), { status: 401 });
@ -21,7 +22,7 @@ export const GET: RequestHandler = async ({ cookies, params }): Promise<Response
// TypeScript type inference for entity // TypeScript type inference for entity
const entityName = entity as EntityName const entityName = entity as EntityName
const result = await getAll<EntityType<typeof entityName>>(entityName, session); const result = await getAll<EntityType<typeof entityName>>(entityName, session, queryParams);
if (isErrorMessage(result)) { if (isErrorMessage(result)) {
console.log("ERROR"); console.log("ERROR");