umami/pages/api/share/[id].ts

38 lines
846 B
TypeScript
Raw Normal View History

import { NextApiRequestQueryBody } from 'lib/types';
2022-08-29 05:20:54 +02:00
import { secret } from 'lib/crypto';
import { NextApiResponse } from 'next';
import { createToken, methodNotAllowed, notFound, ok } from 'next-basics';
import { getWebsite } from 'queries';
export interface ShareRequestQuery {
id: string;
}
export interface ShareResponse {
id: string;
token: string;
}
2020-08-15 10:17:15 +02:00
export default async (
req: NextApiRequestQueryBody<ShareRequestQuery>,
res: NextApiResponse<ShareResponse>,
) => {
2022-11-01 07:42:37 +01:00
const { id: shareId } = req.query;
2020-08-15 10:17:15 +02:00
if (req.method === 'GET') {
2022-11-01 07:42:37 +01:00
const website = await getWebsite({ shareId });
2020-08-15 10:17:15 +02:00
if (website) {
2022-11-01 07:42:37 +01:00
const { id } = website;
const data = { id };
2022-10-25 04:48:10 +02:00
const token = createToken(data, secret());
2022-10-25 04:48:10 +02:00
return ok(res, { ...data, token });
2020-08-15 10:17:15 +02:00
}
return notFound(res);
}
return methodNotAllowed(res);
};