Add api/item/filter proxy request
This commit is contained in:
parent
d87b702a9d
commit
a1560304d4
23
src/routes/api/item/filter/+server.ts
Normal file
23
src/routes/api/item/filter/+server.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import type { ErrorMessage } from "$lib/api";
|
||||
import { filter, type ItemFilter, type Item } from "$lib/entities";
|
||||
import type { RequestHandler } from "./$types";
|
||||
|
||||
function isErrorMessage(value: any): value is ErrorMessage {
|
||||
return value && typeof value.message === 'string';
|
||||
}
|
||||
|
||||
export const POST: RequestHandler = async ({ request, cookies }): Promise<Response> => {
|
||||
const session = cookies.get('session')
|
||||
|
||||
if (!session) {
|
||||
throw new Response(JSON.stringify("no cookies"), { status: 401 })
|
||||
}
|
||||
|
||||
const data: ItemFilter = await request.json();
|
||||
const result = await filter<ItemFilter, Item>("item", data, session);
|
||||
if (isErrorMessage(result)) {
|
||||
return new Response(JSON.stringify(result), { status: 500 })
|
||||
}
|
||||
return new Response(JSON.stringify(result), { status: 200 })
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user