Skip to content

Commit

Permalink
fix: update share handler and serviceworker fetch handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ndom91 committed Jun 23, 2024
1 parent 85645a9 commit 0cc6ed9
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 1,673 deletions.
30 changes: 17 additions & 13 deletions apps/web/src/lib/utils/save-bookmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ import { db } from "$lib/prisma"
import { fetchBookmarkMetadata } from "$lib/server/fetchBookmarkMetadata"
import { PUBLIC_WORKER_URL } from "$env/static/public"

export async function saveBookmark(bookmarks: LoadBookmark[]) {
export async function saveBookmark(bookmarks: { url?: string }[]) {
const bookmarkData = await Promise.all(
bookmarks.map(async (bookmark) => {
const { imageUrl, imageBlur, metadata } = await fetchBookmarkMetadata(bookmark.url)
return {
...bookmark,
userId: "abc123",
image: imageUrl,
imageBlur,
desc: metadata.description,
title: metadata.description,
metadata,
}
}),
bookmarks
.map(async (bookmark) => {
if (!bookmark.url) return
const { imageUrl, imageBlur, metadata } = await fetchBookmarkMetadata(bookmark.url)
return {
...bookmark,
userId: "abc123",
image: imageUrl,
imageBlur,
desc: metadata.description,
title: metadata.description,
metadata,
}
})
.filter(Boolean),
)

const upsertResponse = await db.bookmark.createManyAndReturn({
// @ts-expect-error todo
data: bookmarkData,
skipDuplicates: true,
})
Expand Down
17 changes: 17 additions & 0 deletions apps/web/src/routes/api/v1/bookmarks/share/+server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { text } from "@sveltejs/kit"
import type { RequestHandler } from "./$types"
import { saveBookmark } from "$lib/utils/save-bookmark"

export const GET: RequestHandler = async (event) => {
try {
const url = event.url.searchParams.get("url")
if (!url) throw new Error("No URL provided")

console.log("PASSED_URL", url)

await saveBookmark([{ url }])

return Response.redirect("/")
} catch (error) {
console.error(String(error))
return new Response(String(error), { status: 401 })
}
}

export const fallback: RequestHandler = async ({ request }) => {
return text(`Invalid method ${request.method}`)
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ self.addEventListener("message", (event) => {
self.addEventListener("fetch", (event: FetchEvent) => {
const url = new URL(event.request.url)

if (event.request.method !== "POST" || !url.pathname.includes("/api/v1/bookmarks/share")) {
if (event.request.method !== "GET" || !url.pathname.includes("/api/v1/bookmarks/share")) {
return
}

Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@
"db:push": "pnpm run --filter sveltekasten-web db:push"
},
"devDependencies": {
"@antfu/eslint-config": "^2.19.1",
"@eslint/js": "^9.5.0",
"@types/eslint": "^8.56.10",
"@types/eslint-config-prettier": "^6.11.3",
"@types/eslint__js": "^8.42.3",
"eslint": "9.3.0",
"eslint": "9.5.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-format": "^0.1.1",
"globals": "^15.6.0",
"prettier": "3.2.5",
"tslib": "2.6.2",
"typescript": "5.4.5",
"prettier": "3.3.2",
"tslib": "2.6.3",
"typescript": "5.5.2",
"typescript-eslint": "^7.13.1",
"vite": "5.2.12"
"vite": "5.3.1"
},
"engines": {
"node": ">=20.11"
Expand Down
Loading

0 comments on commit 0cc6ed9

Please sign in to comment.