mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +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 {
|
try {
|
||||||
return await request.clone().json();
|
return await request.clone().json();
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function parseRequest(request: Request, schema?: ZodObject<any>) {
|
export async function parseRequest(request: Request, schema?: ZodObject<any>) {
|
||||||
let error: () => void | undefined;
|
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
const query = Object.fromEntries(url.searchParams);
|
let query = Object.fromEntries(url.searchParams);
|
||||||
const body = await getJsonBody(request);
|
let body = await getJsonBody(request);
|
||||||
|
let error: () => void | undefined;
|
||||||
|
|
||||||
if (schema) {
|
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);
|
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