mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
Updated request parsing.
This commit is contained in:
parent
2d6428172b
commit
dcac7b7c96
1 changed files with 11 additions and 6 deletions
|
|
@ -9,21 +9,26 @@ export async function getJsonBody(request: Request) {
|
|||
try {
|
||||
return await request.clone().json();
|
||||
} catch {
|
||||
return null;
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export async function parseRequest(request: Request, schema?: ZodObject<any>) {
|
||||
let error: () => void | undefined;
|
||||
const url = new URL(request.url);
|
||||
const query = Object.fromEntries(url.searchParams);
|
||||
const body = await getJsonBody(request);
|
||||
let query = Object.fromEntries(url.searchParams);
|
||||
let body = await getJsonBody(request);
|
||||
let error: () => void | undefined;
|
||||
|
||||
if (schema) {
|
||||
const result = schema.safeParse(request.method === 'GET' ? query : body);
|
||||
const isGet = request.method === 'GET';
|
||||
const result = schema.safeParse(isGet ? query : body);
|
||||
|
||||
if (result.error) {
|
||||
if (!result.success) {
|
||||
error = () => badRequest(result.error);
|
||||
} else if (isGet) {
|
||||
query = result.data;
|
||||
} else {
|
||||
body = result.data;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue