umami/pages/api/me/websites.ts

37 lines
951 B
TypeScript
Raw Normal View History

2023-08-20 05:23:15 +00:00
import { useAuth, useCors, useValidate } from 'lib/middleware';
2023-08-11 18:37:01 +00:00
import { NextApiRequestQueryBody, SearchFilter, WebsiteSearchFilterType } from 'lib/types';
2023-08-20 05:23:15 +00:00
import { getFilterValidation } from 'lib/yup';
2023-02-08 00:29:25 +00:00
import { NextApiResponse } from 'next';
import { methodNotAllowed } from 'next-basics';
import userWebsites from 'pages/api/users/[id]/websites';
2023-08-20 05:23:15 +00:00
import * as yup from 'yup';
export interface MyWebsitesRequestQuery extends SearchFilter<WebsiteSearchFilterType> {
id: string;
}
2023-02-08 00:29:25 +00:00
2023-08-20 05:23:15 +00:00
const schema = {
GET: yup.object().shape({
...getFilterValidation(/All|Name|Domain/i),
}),
};
2023-08-11 05:50:41 +00:00
export default async (
req: NextApiRequestQueryBody<MyWebsitesRequestQuery, any>,
res: NextApiResponse,
) => {
2023-02-08 00:29:25 +00:00
await useCors(req, res);
await useAuth(req, res);
2023-08-20 05:23:15 +00:00
req.yup = schema;
await useValidate(req, res);
2023-02-08 00:29:25 +00:00
if (req.method === 'GET') {
req.query.id = req.auth.user.id;
2023-02-08 00:29:25 +00:00
return userWebsites(req, res);
2023-02-08 00:29:25 +00:00
}
return methodNotAllowed(res);
};