From e086d255f2a50121c4c7885675e34a8e553a80e6 Mon Sep 17 00:00:00 2001 From: Brace Sproul Date: Thu, 21 Mar 2024 14:10:43 -0700 Subject: [PATCH] Revert "drop axios" (#86) Reverts langchain-ai/langchain-extract#85 --- frontend/app/utils/api.tsx | 87 +++++++++----------------------------- frontend/middleware.ts | 3 +- 2 files changed, 22 insertions(+), 68 deletions(-) diff --git a/frontend/app/utils/api.tsx b/frontend/app/utils/api.tsx index dbc1df7..93820b8 100644 --- a/frontend/app/utils/api.tsx +++ b/frontend/app/utils/api.tsx @@ -21,56 +21,33 @@ type GetExtractorQueryKey = [string, string, boolean]; // [queryKey, uuid, isSha type OnSuccessFn = (data: { uuid: string }) => void; +axios.defaults.withCredentials = true; + const getExtractor = async ({ queryKey, }: QueryFunctionContext): Promise => { const [, uuid, isShared] = queryKey; const baseUrl = getBaseApiUrl(); if (isShared) { - const response = await fetch(`${baseUrl}/shared/extractors/${uuid}`, { - credentials: "include", - }); - if (!response.ok) { - throw new Error("Network response was not ok"); - } - return response.json(); + const response = await axios.get(`${baseUrl}/shared/extractors/${uuid}`); + return response.data; } else { - const response = await fetch(`${baseUrl}/extractors/${uuid}`, { - credentials: "include", - }); - if (!response.ok) { - throw new Error("Network response was not ok"); - } - return response.json(); + const response = await axios.get(`${baseUrl}/extractors/${uuid}`); + return response.data; } }; const listExtractors = async () => { const baseUrl = getBaseApiUrl(); - const response = await fetch(`${baseUrl}/extractors`, { - credentials: "include", - }); - if (!response.ok) { - throw new Error("Network response was not ok"); - } - return response.json(); + const response = await axios.get(`${baseUrl}/extractors`); + return response.data; }; // eslint-disable-next-line @typescript-eslint/no-explicit-any const createExtractor: MutationFunction = async (extractor) => { const baseUrl = getBaseApiUrl(); - const response = await fetch(`${baseUrl}/extractors`, { - method: "POST", - credentials: "include", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(extractor), - }); - if (!response.ok) { - throw new Error("Network response was not ok"); - } - return response.json(); + const response = await axios.post(`${baseUrl}/extractors`, extractor); + return response.data; }; export type ServerConfiguration = { @@ -81,13 +58,8 @@ export type ServerConfiguration = { const getConfiguration = async (): Promise => { const baseUrl = getBaseApiUrl(); - const response = await fetch(`${baseUrl}/configuration`, { - credentials: "include", - }); - if (!response.ok) { - throw new Error("Network response was not ok"); - } - return response.json(); + const response = await axios.get(`${baseUrl}/configuration`); + return response.data; }; export const useConfiguration = () => { @@ -108,21 +80,11 @@ export const suggestExtractor = async ({ return {}; } const baseUrl = getBaseApiUrl(); - const response = await fetch(`${baseUrl}/suggest`, { - method: "POST", - credentials: "include", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - description, - jsonSchema, - }), + const response = await axios.post(`${baseUrl}/suggest`, { + description, + jsonSchema, }); - if (!response.ok) { - throw new Error("Network response was not ok"); - } - return response.json(); + return response.data; }; type ExtractionRequest = { @@ -141,18 +103,11 @@ export const runExtraction: MutationFunction< > = async ([extractionRequest, isShared]) => { const endpoint = isShared ? "extract/shared" : "extract"; const baseUrl = getBaseApiUrl(); - const response = await fetch(`${baseUrl}/${endpoint}`, { - method: "POST", - credentials: "include", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(extractionRequest), - }); - if (!response.ok) { - throw new Error("Network response was not ok"); - } - return response.json(); + const response = await axios.postForm( + `${baseUrl}/${endpoint}`, + extractionRequest, + ); + return response.data; }; export const useRunExtraction = () => { diff --git a/frontend/middleware.ts b/frontend/middleware.ts index 5355a24..b4eb336 100644 --- a/frontend/middleware.ts +++ b/frontend/middleware.ts @@ -14,10 +14,9 @@ export function middleware(request: NextRequest) { if (!userId) { response.cookies.set(USER_ID_COOKIE_KEY, uuidv4(), { maxAge: 60 * 60 * 24 * 365, // 1 year - sameSite: "none", + sameSite: "strict", path: "/", httpOnly: true, - secure: process.env.NODE_ENV === "production", }); } return response;