split api into multiple files

This commit is contained in:
jojopirker
2023-01-04 15:19:46 +01:00
parent dba693ee85
commit b08e1b986a
3 changed files with 29 additions and 7 deletions
+26
View File
@@ -0,0 +1,26 @@
import { boolean } from "boolean";
import { getToken } from "next-auth/jwt";
const handler = async (req, res) => {
const token = await getToken({ req });
// Return nothing if the user isn't registered.
if (!token) {
res.status(401).end();
return;
}
const messagesRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/messages`, {
method: "GET",
headers: {
"X-API-Key": process.env.FASTAPI_KEY,
"Content-Type": "application/json",
},
});
const messages = await messagesRes.json();
// Send recieved messages to the client.
res.status(200).json(messages);
};
export default handler;
@@ -1,4 +1,3 @@
import { boolean } from "boolean";
import { getToken } from "next-auth/jwt";
const handler = async (req, res) => {
@@ -11,9 +10,8 @@ const handler = async (req, res) => {
}
//TODO: add params if needed
const reqParams = req.query;
const params = new URLSearchParams({
username: boolean(reqParams.allUsers) ? "" : token.sub,
username: token.sub,
});
const messagesRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/messages?${params}`, {