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}`, {
+2 -4
View File
@@ -3,12 +3,10 @@ import {
Box,
CircularProgress,
HStack,
Image,
SimpleGrid,
Stack,
StackDivider,
Text,
useColorMode,
useColorModeValue,
} from "@chakra-ui/react";
import Head from "next/head";
@@ -68,13 +66,13 @@ const MessagesDashboard = () => {
const [messages, setMessages] = useState([]);
const [userMessages, setUserMessages] = useState([]);
const { isLoading: isLoadingAll } = useSWRImmutable("/api/messages?allUsers=true", fetcher, {
const { isLoading: isLoadingAll } = useSWRImmutable("/api/messages", fetcher, {
onSuccess: (data) => {
setMessages(data);
},
});
const { isLoading: isLoadingUser } = useSWRImmutable(`/api/messages`, fetcher, {
const { isLoading: isLoadingUser } = useSWRImmutable(`/api/messages/user`, fetcher, {
onSuccess: (data) => {
setUserMessages(data);
},