Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: switch from fetch to ky #128

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"clsx": "^2.1.1",
"drizzle-orm": "^0.33.0",
"geist": "^1.3.0",
"ky": "^1.7.0",
"lucide-react": "^0.414.0",
"next": "^14.2.4",
"next-themes": "^0.3.0",
Expand Down
1 change: 0 additions & 1 deletion src/app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { db } from "@/server/db";
import type { UsersTableType } from "@/server/db/schema";
import { users } from "@/server/db/schema";
import type { PaperlessDocumentsType } from "@/types";
import { auth } from "@clerk/nextjs/server";

/*
Expand Down
10 changes: 4 additions & 6 deletions src/app/api/whishperRecording/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getUserData } from "@/app/actions";
import ky from "ky";

export async function DELETE(req: Request) {
const url = new URL(req.url);
Expand All @@ -13,15 +14,12 @@ export async function DELETE(req: Request) {
if (!userData) {
return new Response("Unauthorized", { status: 401 });
}

const response = await fetch(
const response = await ky.delete(
`${userData.whishperURL}/api/transcriptions/${id}`,
{
method: "DELETE",
},
);

if (!response.ok) {
if (!(response.ok)) {
throw new Error("Network error");
}

Expand Down
67 changes: 28 additions & 39 deletions src/app/paperless/document/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import BodyMessage from "@/components/body-message";
import Link from "next/link";
import LoadingSpinner from "@/components/loading-spinner";
import OpenExternalLink from "@/components/external-link";
import ky from "ky";

const queryClient = new QueryClient();

Expand All @@ -36,19 +37,15 @@ export async function getPaperlessDocument(
): Promise<string | null> {
try {
const url = `${userData.paperlessURL}/api/documents/${documentId}/download/`;
const response = await fetch(url, {
headers: {
Authorization: `Token ${userData.paperlessToken}`,
},
});
if (response.ok) {
const blob = await response.blob();
const objectUrl = URL.createObjectURL(blob);
return objectUrl;
} else {
console.error("Failed to fetch PDF");
return null;
}
const blob = await ky
.get(url, {
headers: {
Authorization: `Token ${userData.paperlessToken}`,
},
})
.blob();
const objectUrl = URL.createObjectURL(blob);
return objectUrl;
} catch (error) {
console.error("Error fetching PDF:", error);
return null;
Expand All @@ -64,44 +61,36 @@ async function deleteDocument(documentId: number) {
documents: [documentId],
method: "delete",
};
const response = await fetch(
`${userData.paperlessURL}/api/documents/bulk_edit/ `,
const response = await ky.post(
`${userData.paperlessURL}/api/documents/bulk_edit/`,
{
method: "POST",
json: body,
headers: {
"Content-Type": "application/json",
Authorization: `Token ${userData.paperlessToken}`,
},
body: JSON.stringify(body),
},
);
return response;
}

const fetchUserData = async (): Promise<UsersTableType> => {
const response = await fetch(`/api/getUserData`);
if (!response.ok) {
throw new Error("Network error");
}
const data = (await response.json()) as UsersTableType;
return data;
};
async function fetchUserData(): Promise<UsersTableType> {
return await ky.get("/api/getUserData").json<UsersTableType>();
}

async function getPaperlessDocumentData(id: number, userData: UsersTableType) {
async function getPaperlessDocumentData(
id: number,
userData: UsersTableType,
): Promise<PaperlessDocumentType | null> {
try {
const url = `${userData.paperlessURL}/api/documents/${id}/?truncate_content=true`;
const response = await fetch(url, {
headers: {
Authorization: `Token ${userData.paperlessToken}`,
},
});
if (response.ok) {
const data = (await response.json()) as PaperlessDocumentType;
return data;
} else {
console.error("Failed to fetch PD dataF");
return null;
}
const data = await ky
.get(url, {
headers: {
Authorization: `Token ${userData.paperlessToken}`,
},
})
.json<PaperlessDocumentType>();
return data;
} catch (error) {
console.error("Error fetching PDF data:", error);
return null;
Expand Down
46 changes: 21 additions & 25 deletions src/app/paperless/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import OpenInternalLink from "@/components/internal-link";
import type { PaperlessDocumentsType } from "@/types";
import type { UsersTableType } from "@/server/db/schema";
import Image from "next/image";
import ky from "ky";

const queryClient = new QueryClient();

Expand All @@ -36,18 +37,17 @@ async function getPaperlessDocuments(query: string) {

if (!query || query == "null" || query.length < 3 || !userData) return null;

const response = await fetch(
`${userData.paperlessURL}/api/documents/?query=${query}&page=1&page_size=10&truncate_content=true`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Token ${userData.paperlessToken}`,
const data = await ky
.get(
`${userData.paperlessURL}/api/documents/?query=${query}&page=1&page_size=10&truncate_content=true`,
{
headers: {
"Content-Type": "application/json",
Authorization: `Token ${userData.paperlessToken}`,
},
},
},
);

const data = (await response.json()) as PaperlessDocumentsType;
)
.json<PaperlessDocumentsType>();

return data;
}
Expand All @@ -58,19 +58,15 @@ export async function getPaperlessThumbnail(
): Promise<string | null> {
try {
const url = `${userData.paperlessURL}/api/documents/${documentId}/thumb/`;
const response = await fetch(url, {
headers: {
Authorization: `Token ${userData.paperlessToken}`,
},
});
if (response.ok) {
const blob = await response.blob();
const objectUrl = URL.createObjectURL(blob);
return objectUrl;
} else {
console.error("Failed to fetch PDF");
return null;
}
const blob = await ky
.get(url, {
headers: {
Authorization: `Token ${userData.paperlessToken}`,
},
})
.blob();
const objectUrl = URL.createObjectURL(blob);
return objectUrl;
} catch (error) {
console.error("Error fetching PDF:", error);
return null;
Expand Down Expand Up @@ -232,7 +228,7 @@ function DocumentsPage() {
alt={document.title}
width={40}
height={128}
className="h-32 w-full rounded object-cover mb-2"
className="mb-2 h-32 w-full rounded object-cover"
/>
{document.title}
</Link>
Expand Down
8 changes: 5 additions & 3 deletions src/app/whishper/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import OpenExternalLink from "@/components/external-link";
import type { UsersTableType } from "@/server/db/schema";
import { BadgeCheck, Badge, BadgeAlert } from "lucide-react";
import type { WhishperRecordingType } from "@/types";
import ky from "ky";

const queryClient = new QueryClient();

Expand All @@ -53,9 +54,10 @@ async function getWhishperRecordings(

if (!query || query == "null" || query.length < 3 || !userData) return null;

const response = await fetch(`${userData.whishperURL}/api/transcriptions`);
const data = await ky
.get(`${userData.whishperURL}/api/transcriptions`)
.json<WhishperRecordingType[]>();

const data = (await response.json()) as WhishperRecordingType[];
const lowerCaseQuery = query.toLowerCase();
const filteredAndScored = data
.filter(
Expand Down Expand Up @@ -187,7 +189,7 @@ function RecordingsList() {

const WhishperRecordingsMap = WhishperRecordings.data;

if (!WhishperRecordingsMap ?? WhishperRecordingsMap.length === 0) {
if (WhishperRecordingsMap.length === 0) {
return <h1 className="text-2xl font-bold">No results!</h1>;
}

Expand Down
42 changes: 13 additions & 29 deletions src/components/audio-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,13 @@ import {
} from "@/components/ui/tooltip";
import { toast } from "sonner";
import BodyMessage from "@/components/body-message";
import ky from "ky";

const queryClient = new QueryClient();

const fetchUserData = async (): Promise<UsersTableType> => {
const response = await fetch("/api/getUserData");
if (!response.ok) {
throw new Error("Network error");
}
const data = (await response.json()) as UsersTableType;
return data;
};
async function fetchUserData(): Promise<UsersTableType> {
return await ky.get("/api/getUserData").json<UsersTableType>();
}

function SkeletonLoader() {
return (
Expand All @@ -68,26 +64,14 @@ function SkeletonLoader() {
);
}

async function fetchWhishperRecording(searchId: string, whishperURL: string) {
const response = await fetch(`${whishperURL}/api/transcriptions`);
const data = (await response.json()) as WhishperRecordingType[];
for (const recording of data) {
if (recording.id === searchId) {
return recording;
}
}
}

async function deleteWhishperRecording(url: string) {
const response = await fetch(url, {
method: "DELETE",
});

if (!response.ok) {
throw new Error("Network error");
}

return response;
async function fetchWhishperRecording(
searchId: string,
whishperURL: string,
): Promise<WhishperRecordingType | undefined> {
const data = await ky
.get(`${whishperURL}/api/transcriptions`)
.json<WhishperRecordingType[]>();
return data.find((recording) => recording.id === searchId);
}

type AudioInfoProps = {
Expand Down Expand Up @@ -220,7 +204,7 @@ function AudioInfo({ id }: AudioInfoProps) {
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
onClick={async () => {
const response = await deleteWhishperRecording(
const response = await ky.delete(
`${userData.whishperURL}/api/transcriptions/${id}`,
);
if (response.ok) {
Expand Down
Loading