Skip to content

Commit

Permalink
Revert "drop axios" (#86)
Browse files Browse the repository at this point in the history
Reverts #85
  • Loading branch information
bracesproul authored Mar 21, 2024
1 parent c98b3b7 commit e086d25
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 68 deletions.
87 changes: 21 additions & 66 deletions frontend/app/utils/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetExtractorQueryKey>): Promise<ExtractorData> => {
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<any, any> = 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 = {
Expand All @@ -81,13 +58,8 @@ export type ServerConfiguration = {

const getConfiguration = async (): Promise<ServerConfiguration> => {
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 = () => {
Expand All @@ -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 = {
Expand All @@ -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 = () => {
Expand Down
3 changes: 1 addition & 2 deletions frontend/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e086d25

Please sign in to comment.