From 3c791efb7996496a446aee66694221be1cb53449 Mon Sep 17 00:00:00 2001 From: AbdBarho Date: Fri, 27 Jan 2023 21:59:29 +0100 Subject: [PATCH] Move api calls to `oasst_api_client` --- website/src/lib/oasst_api_client.ts | 20 +++++++++++++ website/src/lib/prismadb.ts | 2 +- website/src/pages/admin/manage_user/[id].tsx | 2 +- .../src/pages/api/messages/[id]/children.ts | 16 +++-------- .../pages/api/messages/[id]/conversation.ts | 16 +++-------- website/src/pages/api/messages/[id]/emoji.ts | 3 +- website/src/pages/api/messages/[id]/index.ts | 19 ++----------- website/src/pages/api/messages/[id]/parent.ts | 28 +++++-------------- website/src/pages/api/messages/index.ts | 16 +++-------- website/src/pages/api/messages/user.ts | 19 ++----------- website/src/types/Conversation.ts | 5 ++-- 11 files changed, 52 insertions(+), 94 deletions(-) diff --git a/website/src/lib/oasst_api_client.ts b/website/src/lib/oasst_api_client.ts index d7ca8281..b9a9489e 100644 --- a/website/src/lib/oasst_api_client.ts +++ b/website/src/lib/oasst_api_client.ts @@ -245,4 +245,24 @@ export class OasstApiClient { return await resp.json(); } + + fetch_my_messages(user: BackendUserCore) { + const params = new URLSearchParams({ + username: user.id, + auth_method: user.auth_method, + }); + return this.get(`/api/v1/messages?${params}`); + } + + fetch_recent_messages() { + return this.get(`/api/v1/messages`); + } + + fetch_message_children(messageId: string) { + return this.get(`/api/v1/messages/${messageId}/children`); + } + + fetch_conversation(messageId: string) { + return this.get(`/api/v1/messages/${messageId}/conversation`); + } } diff --git a/website/src/lib/prismadb.ts b/website/src/lib/prismadb.ts index 336d782e..296eda8b 100644 --- a/website/src/lib/prismadb.ts +++ b/website/src/lib/prismadb.ts @@ -3,7 +3,7 @@ declare global { // eslint-disable-next-line no-var var prisma: PrismaClient | undefined; } -console.trace() + const client = globalThis.prisma || new PrismaClient(); if (process.env.NODE_ENV !== "production") { globalThis.prisma = client; diff --git a/website/src/pages/admin/manage_user/[id].tsx b/website/src/pages/admin/manage_user/[id].tsx index 4a042c52..a68bca16 100644 --- a/website/src/pages/admin/manage_user/[id].tsx +++ b/website/src/pages/admin/manage_user/[id].tsx @@ -10,7 +10,7 @@ import { getAdminLayout } from "src/components/Layout"; import { Role, RoleSelect } from "src/components/RoleSelect"; import { UserMessagesCell } from "src/components/UserMessagesCell"; import { post } from "src/lib/api"; -import { userlessApiClient } from "src/lib/oasst_api_client"; +import { userlessApiClient } from "src/lib/oasst_client_factory"; import prisma from "src/lib/prismadb"; import useSWRMutation from "swr/mutation"; diff --git a/website/src/pages/api/messages/[id]/children.ts b/website/src/pages/api/messages/[id]/children.ts index 4a184c11..0185b615 100644 --- a/website/src/pages/api/messages/[id]/children.ts +++ b/website/src/pages/api/messages/[id]/children.ts @@ -1,18 +1,10 @@ import { withoutRole } from "src/lib/auth"; +import { createApiClient } from "src/lib/oasst_client_factory"; -const handler = withoutRole("banned", async (req, res) => { +const handler = withoutRole("banned", async (req, res, token) => { const { id } = req.query; - - const messagesRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/messages/${id}/children`, { - 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. + const client = await createApiClient(token); + const messages = await client.fetch_message_children(id as string); res.status(200).json(messages); }); diff --git a/website/src/pages/api/messages/[id]/conversation.ts b/website/src/pages/api/messages/[id]/conversation.ts index 0c401883..fc284e40 100644 --- a/website/src/pages/api/messages/[id]/conversation.ts +++ b/website/src/pages/api/messages/[id]/conversation.ts @@ -1,18 +1,10 @@ import { withoutRole } from "src/lib/auth"; +import { createApiClient } from "src/lib/oasst_client_factory"; -const handler = withoutRole("banned", async (req, res) => { +const handler = withoutRole("banned", async (req, res, token) => { const { id } = req.query; - - const messagesRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/messages/${id}/conversation`, { - 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. + const client = await createApiClient(token); + const messages = await client.fetch_conversation(id as string); res.status(200).json(messages); }); diff --git a/website/src/pages/api/messages/[id]/emoji.ts b/website/src/pages/api/messages/[id]/emoji.ts index 59d93f69..8d6257c1 100644 --- a/website/src/pages/api/messages/[id]/emoji.ts +++ b/website/src/pages/api/messages/[id]/emoji.ts @@ -1,5 +1,5 @@ import { withoutRole } from "src/lib/auth"; -import { oasstApiClient } from "src/lib/oasst_api_client"; +import { createApiClientFromUser } from "src/lib/oasst_client_factory"; import { getBackendUserCore } from "src/lib/users"; const handler = withoutRole("banned", async (req, res, token) => { @@ -15,6 +15,7 @@ const handler = withoutRole("banned", async (req, res, token) => { const { emoji, op } = req.body; const user = await getBackendUserCore(token.sub); + const oasstApiClient = createApiClientFromUser(user); try { await oasstApiClient.set_user_message_emoji(messageId, user, emoji, op); } catch (err) { diff --git a/website/src/pages/api/messages/[id]/index.ts b/website/src/pages/api/messages/[id]/index.ts index 28947feb..b3361a2d 100644 --- a/website/src/pages/api/messages/[id]/index.ts +++ b/website/src/pages/api/messages/[id]/index.ts @@ -1,25 +1,12 @@ import { withoutRole } from "src/lib/auth"; +import { createApiClientFromUser } from "src/lib/oasst_client_factory"; import { getBackendUserCore } from "src/lib/users"; const handler = withoutRole("banned", async (req, res, token) => { const { id } = req.query; - const user = await getBackendUserCore(token.sub); - const params = new URLSearchParams({ - username: user.id, - auth_method: user.auth_method, - }); - - const messageRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/messages/${id}/?${params}`, { - method: "GET", - headers: { - "X-API-Key": process.env.FASTAPI_KEY, - "Content-Type": "application/json", - }, - }); - const message = await messageRes.json(); - - // Send recieved messages to the client. + const client = createApiClientFromUser(user); + const message = await client.fetch_message(id as string, user); res.status(200).json(message); }); diff --git a/website/src/pages/api/messages/[id]/parent.ts b/website/src/pages/api/messages/[id]/parent.ts index ba332be9..9b8c64d4 100644 --- a/website/src/pages/api/messages/[id]/parent.ts +++ b/website/src/pages/api/messages/[id]/parent.ts @@ -1,6 +1,8 @@ import { withoutRole } from "src/lib/auth"; +import { createApiClient, createApiClientFromUser } from "src/lib/oasst_client_factory"; +import { getBackendUserCore } from "src/lib/users"; -const handler = withoutRole("banned", async (req, res) => { +const handler = withoutRole("banned", async (req, res, token) => { const { id } = req.query; if (!id) { @@ -8,32 +10,16 @@ const handler = withoutRole("banned", async (req, res) => { return; } - const messageRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/messages/${id}`, { - method: "GET", - headers: { - "X-API-Key": process.env.FASTAPI_KEY, - "Content-Type": "application/json", - }, - }); - - const message = await messageRes.json(); + const user = await getBackendUserCore(token.sub); + const client = createApiClientFromUser(user); + const message = await client.fetch_message(id as string, user); if (!message.parent_id) { res.status(404).end(); return; } - const parentRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/messages/${message.parent_id}`, { - method: "GET", - headers: { - "X-API-Key": process.env.FASTAPI_KEY, - "Content-Type": "application/json", - }, - }); - - const parent = await parentRes.json(); - - // Send recieved messages to the client. + const parent = await client.fetch_message(message.parent_id, user); res.status(200).json(parent); }); diff --git a/website/src/pages/api/messages/index.ts b/website/src/pages/api/messages/index.ts index fdd01313..fbcaee3c 100644 --- a/website/src/pages/api/messages/index.ts +++ b/website/src/pages/api/messages/index.ts @@ -1,17 +1,9 @@ import { withoutRole } from "src/lib/auth"; +import { createApiClient } from "src/lib/oasst_client_factory"; -const handler = withoutRole("banned", async (req, res) => { - // TODO: move to oasst_api_client - - const messagesRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/messages`, { - method: "GET", - headers: { - "X-API-Key": process.env.FASTAPI_KEY, - }, - }); - const messages = await messagesRes.json(); - - // Send recieved messages to the client. +const handler = withoutRole("banned", async (req, res, token) => { + const client = await createApiClient(token); + const messages = await client.fetch_recent_messages(); res.status(200).json(messages); }); diff --git a/website/src/pages/api/messages/user.ts b/website/src/pages/api/messages/user.ts index 3f2e739f..bffe2fb6 100644 --- a/website/src/pages/api/messages/user.ts +++ b/website/src/pages/api/messages/user.ts @@ -1,24 +1,11 @@ import { withoutRole } from "src/lib/auth"; +import { createApiClientFromUser } from "src/lib/oasst_client_factory"; import { getBackendUserCore } from "src/lib/users"; const handler = withoutRole("banned", async (req, res, token) => { const user = await getBackendUserCore(token.sub); - const params = new URLSearchParams({ - username: user.id, - auth_method: user.auth_method, - }); - - // TODO: move to oasst_api_client - - const messagesRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/messages?${params}`, { - method: "GET", - headers: { - "X-API-Key": process.env.FASTAPI_KEY, - }, - }); - const messages = await messagesRes.json(); - - // Send recieved messages to the client. + const client = createApiClientFromUser(user); + const messages = await client.fetch_my_messages(user); res.status(200).json(messages); }); diff --git a/website/src/types/Conversation.ts b/website/src/types/Conversation.ts index 9cc3a140..8b258a25 100644 --- a/website/src/types/Conversation.ts +++ b/website/src/types/Conversation.ts @@ -11,11 +11,12 @@ export interface MessageEmojis { } export interface Message extends MessageEmojis { + id: string; text: string; is_assistant: boolean; - id: string; - created_date: string; // iso date string lang: string; + created_date: string; // iso date string + parent_id: string; frontend_message_id?: string; }