mirror of
https://github.com/umami-software/umami.git
synced 2026-02-07 22:27:16 +01:00
Merged auth into new parseRequest method.
This commit is contained in:
parent
e51f182403
commit
2d6428172b
39 changed files with 296 additions and 316 deletions
|
|
@ -1,7 +1,9 @@
|
|||
import { ZodObject } from 'zod';
|
||||
import { FILTER_COLUMNS } from 'lib/constants';
|
||||
import { badRequest, unauthorized } from 'lib/response';
|
||||
import { getAllowedUnits, getMinimumUnit } from './date';
|
||||
import { getWebsiteDateRange } from '../queries';
|
||||
import { FILTER_COLUMNS } from 'lib/constants';
|
||||
import { checkAuth } from 'lib/auth';
|
||||
|
||||
export async function getJsonBody(request: Request) {
|
||||
try {
|
||||
|
|
@ -11,14 +13,27 @@ export async function getJsonBody(request: Request) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function checkRequest(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 query = Object.fromEntries(url.searchParams);
|
||||
const body = await getJsonBody(request);
|
||||
|
||||
const result = schema.safeParse(request.method === 'GET' ? query : body);
|
||||
if (schema) {
|
||||
const result = schema.safeParse(request.method === 'GET' ? query : body);
|
||||
|
||||
return { query, body, error: result.error };
|
||||
if (result.error) {
|
||||
error = () => badRequest(result.error);
|
||||
}
|
||||
}
|
||||
|
||||
const auth = !error ? await checkAuth(request) : null;
|
||||
|
||||
if (!error && !auth) {
|
||||
error = () => unauthorized();
|
||||
}
|
||||
|
||||
return { url, query, body, auth, error };
|
||||
}
|
||||
|
||||
export async function getRequestDateRange(query: Record<string, any>) {
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@ export const pageInfo = {
|
|||
};
|
||||
|
||||
export const pagingParams = {
|
||||
page: z.coerce.number().int().positive(),
|
||||
pageSize: z.coerce.number().int().positive(),
|
||||
page: z.coerce.number().int().positive().optional(),
|
||||
pageSize: z.coerce.number().int().positive().optional(),
|
||||
orderBy: z.string().optional(),
|
||||
query: z.string().optional(),
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue