From 79d692c2c626f66d51ac2744d6633ce48876829e Mon Sep 17 00:00:00 2001 From: Max Richter Date: Fri, 31 Oct 2025 15:41:46 +0100 Subject: [PATCH] fix: dont import node:path in clientside components --- lib/marka.ts | 35 +++++++++++++++++++++++++++ lib/resources.ts | 37 ----------------------------- lib/search.ts | 2 +- routes/api/articles/[name].ts | 2 +- routes/api/articles/create/index.ts | 2 +- routes/api/articles/index.ts | 2 +- routes/api/movies/[name].ts | 7 +++--- routes/api/movies/enhance/[name].ts | 2 +- routes/api/movies/index.ts | 2 +- routes/api/recipes/[name].ts | 2 +- routes/api/recipes/index.ts | 2 +- routes/api/recommendation/all.ts | 2 +- routes/api/series/[name].ts | 7 +++--- routes/articles/[name].tsx | 4 ++-- routes/articles/index.tsx | 3 +-- routes/movies/[name].tsx | 2 +- routes/movies/index.tsx | 7 ++++-- routes/recipes/[name].tsx | 2 +- routes/recipes/index.tsx | 2 +- routes/series/[name].tsx | 9 ++++--- routes/series/index.tsx | 2 +- 21 files changed, 68 insertions(+), 67 deletions(-) create mode 100644 lib/marka.ts diff --git a/lib/marka.ts b/lib/marka.ts new file mode 100644 index 0000000..34bea07 --- /dev/null +++ b/lib/marka.ts @@ -0,0 +1,35 @@ +import { MARKA_API_KEY } from "./env.ts"; +const url = `https://marka.max-richter.dev/resources`; +//const url = "http://localhost:8080/resources"; + +export async function fetchResource(resource: string) { + try { + const response = await fetch( + `${url}/${resource}`, + ); + return response.json(); + } catch (_e) { + return []; + } +} + +export async function createResource( + path: string, + content: string | object | ArrayBuffer, +) { + const isJson = typeof content === "object"; + const fetchUrl = `${url}/${path}`; + console.log("Creating resource", { fetchUrl, content, isJson }); + const response = await fetch(fetchUrl, { + method: "POST", + headers: { + "Content-Type": isJson ? "application/json" : "", + "Authentication": MARKA_API_KEY, + }, + body: isJson ? JSON.stringify(content) : content, + }); + if (!response.ok) { + throw new Error(`Failed to create resource: ${response.status}`); + } + return response.json(); +} diff --git a/lib/resources.ts b/lib/resources.ts index dab006a..f7ec601 100644 --- a/lib/resources.ts +++ b/lib/resources.ts @@ -1,5 +1,3 @@ -import { MARKA_API_KEY } from "./env.ts"; - export const resources = { "home": { emoji: "House with Garden.png", @@ -32,38 +30,3 @@ export const resources = { prefix: "Media/series/", }, } as const; - -const url = `https://marka.max-richter.dev/resources`; -//const url = "http://localhost:8080/resources"; - -export async function fetchResource(resource: string) { - try { - const response = await fetch( - `${url}/${resource}`, - ); - return response.json(); - } catch (_e) { - return []; - } -} - -export async function createResource( - path: string, - content: string | object | ArrayBuffer, -) { - const isJson = typeof content === "object"; - const fetchUrl = `${url}/${path}`; - console.log("Creating resource", { fetchUrl, content, isJson }); - const response = await fetch(fetchUrl, { - method: "POST", - headers: { - "Content-Type": isJson ? "application/json" : "", - "Authentication": MARKA_API_KEY, - }, - body: isJson ? JSON.stringify(content) : content, - }); - if (!response.ok) { - throw new Error(`Failed to create resource: ${response.status}`); - } - return response.json(); -} diff --git a/lib/search.ts b/lib/search.ts index 44e4264..669d98e 100644 --- a/lib/search.ts +++ b/lib/search.ts @@ -6,7 +6,7 @@ import { Movie } from "@lib/resource/movies.ts"; import { Article } from "@lib/resource/articles.ts"; import { Recipe } from "@lib/resource/recipes.ts"; import { Series } from "@lib/resource/series.ts"; -import { fetchResource } from "./resources.ts"; +import { fetchResource } from "./marka.ts"; type ResourceType = keyof typeof resources; diff --git a/routes/api/articles/[name].ts b/routes/api/articles/[name].ts index 01f8e5b..f3bd0b6 100644 --- a/routes/api/articles/[name].ts +++ b/routes/api/articles/[name].ts @@ -1,6 +1,6 @@ import { Handlers } from "$fresh/server.ts"; import { json } from "@lib/helpers.ts"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; export const handler: Handlers = { async GET(_, ctx) { diff --git a/routes/api/articles/create/index.ts b/routes/api/articles/create/index.ts index 62614ef..59173ae 100644 --- a/routes/api/articles/create/index.ts +++ b/routes/api/articles/create/index.ts @@ -12,7 +12,7 @@ import { toUrlSafeString, } from "@lib/string.ts"; import { createLogger } from "@lib/log/index.ts"; -import { createResource } from "@lib/resources.ts"; +import { createResource } from "@lib/marka.ts"; import { webScrape } from "@lib/webScraper.ts"; const log = createLogger("api/article"); diff --git a/routes/api/articles/index.ts b/routes/api/articles/index.ts index c8772e8..63339aa 100644 --- a/routes/api/articles/index.ts +++ b/routes/api/articles/index.ts @@ -1,6 +1,6 @@ import { Handlers } from "$fresh/server.ts"; import { json } from "@lib/helpers.ts"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; export const handler: Handlers = { async GET() { diff --git a/routes/api/movies/[name].ts b/routes/api/movies/[name].ts index 71cd78f..44aa95e 100644 --- a/routes/api/movies/[name].ts +++ b/routes/api/movies/[name].ts @@ -5,7 +5,7 @@ import * as tmdb from "@lib/tmdb.ts"; import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts"; import { isString, safeFileName } from "@lib/string.ts"; import { AccessDeniedError } from "@lib/errors.ts"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; export const handler: Handlers = { async GET(_, ctx) { @@ -38,9 +38,8 @@ export const handler: Handlers = { const poster = await tmdb.getMoviePoster(posterPath); const extension = fileExtension(posterPath); - finalPath = `Media/movies/images/${ - safeFileName(name) - }_cover.${extension}`; + finalPath = `Media/movies/images/${safeFileName(name) + }_cover.${extension}`; await createDocument(finalPath, poster); } diff --git a/routes/api/movies/enhance/[name].ts b/routes/api/movies/enhance/[name].ts index 6d54cc5..19bf271 100644 --- a/routes/api/movies/enhance/[name].ts +++ b/routes/api/movies/enhance/[name].ts @@ -9,7 +9,7 @@ import { NotFoundError, } from "@lib/errors.ts"; import { createRecommendationResource } from "@lib/recommendation.ts"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; const POST = async ( req: Request, diff --git a/routes/api/movies/index.ts b/routes/api/movies/index.ts index 791aea9..cc2c9b7 100644 --- a/routes/api/movies/index.ts +++ b/routes/api/movies/index.ts @@ -1,6 +1,6 @@ import { Handlers } from "$fresh/server.ts"; import { json } from "@lib/helpers.ts"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; export const handler: Handlers = { async GET() { diff --git a/routes/api/recipes/[name].ts b/routes/api/recipes/[name].ts index a8eb3e1..13b2e3e 100644 --- a/routes/api/recipes/[name].ts +++ b/routes/api/recipes/[name].ts @@ -1,6 +1,6 @@ import { Handlers } from "$fresh/server.ts"; import { json } from "@lib/helpers.ts"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; export const handler: Handlers = { async GET(_, ctx) { diff --git a/routes/api/recipes/index.ts b/routes/api/recipes/index.ts index 2b3cfd9..f5d6139 100644 --- a/routes/api/recipes/index.ts +++ b/routes/api/recipes/index.ts @@ -1,6 +1,6 @@ import { Handlers } from "$fresh/server.ts"; import { json } from "@lib/helpers.ts"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; export const handler: Handlers = { async GET() { diff --git a/routes/api/recommendation/all.ts b/routes/api/recommendation/all.ts index 06e4fc3..8696f16 100644 --- a/routes/api/recommendation/all.ts +++ b/routes/api/recommendation/all.ts @@ -7,7 +7,7 @@ import { getRecommendation, } from "@lib/recommendation.ts"; import { AccessDeniedError } from "@lib/errors.ts"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; async function processUpdateRecommendations( streamResponse: ReturnType, diff --git a/routes/api/series/[name].ts b/routes/api/series/[name].ts index c9b40e3..79ef888 100644 --- a/routes/api/series/[name].ts +++ b/routes/api/series/[name].ts @@ -6,7 +6,7 @@ import { isString, safeFileName } from "@lib/string.ts"; import { createDocument } from "@lib/documents.ts"; import { AccessDeniedError } from "@lib/errors.ts"; import { Series } from "@lib/resource/series.ts"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; export const handler: Handlers = { async GET(_, ctx) { @@ -37,9 +37,8 @@ export const handler: Handlers = { const poster = await tmdb.getMoviePoster(posterPath); const extension = fileExtension(posterPath); - finalPath = `Media/series/images/${ - safeFileName(name) - }_cover.${extension}`; + finalPath = `Media/series/images/${safeFileName(name) + }_cover.${extension}`; await createDocument(finalPath, poster); } diff --git a/routes/articles/[name].tsx b/routes/articles/[name].tsx index bc9bcb4..5ca5f66 100644 --- a/routes/articles/[name].tsx +++ b/routes/articles/[name].tsx @@ -10,7 +10,7 @@ import { RedirectSearchHandler } from "@islands/Search.tsx"; import PageHero from "@components/PageHero.tsx"; import { Star } from "@components/Stars.tsx"; import { MetaTags } from "@components/MetaTags.tsx"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; export const handler: Handlers<{ article: Article; session: unknown }> = { async GET(_, ctx) { @@ -27,7 +27,7 @@ export default function Greet( ) { const { article, session } = props.data; - const { author = "", date = "", articleBody = "" } = (article?.content || {}); + const { author = "", date = "", articleBody = "" } = article?.content || {}; const content = renderMarkdown( removeImage(articleBody, article.content.image), diff --git a/routes/articles/index.tsx b/routes/articles/index.tsx index e292034..51ee505 100644 --- a/routes/articles/index.tsx +++ b/routes/articles/index.tsx @@ -9,8 +9,7 @@ import { parseResourceUrl, searchResource } from "@lib/search.ts"; import { GenericResource } from "@lib/types.ts"; import { ResourceCard } from "@components/Card.tsx"; import { Link } from "@islands/Link.tsx"; -import { fetchResource } from "@lib/resources.ts"; - +import { fetchResource } from "@lib/marka.ts"; export const handler: Handlers< { articles: Article[] | null; searchResults?: GenericResource[] } diff --git a/routes/movies/[name].tsx b/routes/movies/[name].tsx index 8607149..046373d 100644 --- a/routes/movies/[name].tsx +++ b/routes/movies/[name].tsx @@ -9,7 +9,7 @@ import PageHero from "@components/PageHero.tsx"; import { Star } from "@components/Stars.tsx"; import { MetaTags } from "@components/MetaTags.tsx"; import { parseRating } from "@lib/helpers.ts"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; export default async function Greet( props: PageProps<{ movie: Movie; session: Record }>, diff --git a/routes/movies/index.tsx b/routes/movies/index.tsx index 7e3f0bc..2818340 100644 --- a/routes/movies/index.tsx +++ b/routes/movies/index.tsx @@ -7,7 +7,7 @@ import { KMenu } from "@islands/KMenu.tsx"; import { RedirectSearchHandler } from "@islands/Search.tsx"; import { GenericResource } from "@lib/types.ts"; import { PageProps } from "$fresh/server.ts"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; import { parseResourceUrl, searchResource } from "@lib/search.ts"; export default async function Greet( @@ -20,7 +20,10 @@ export default async function Greet( const searchResults = searchParams && await searchResource({ ...searchParams, types: ["movie"] }); const movies = allMovies.sort((a, b) => - a?.content?.reviewRating?.ratingValue > b?.content?.reviewRating?.ratingValue ? -1 : 1 + a?.content?.reviewRating?.ratingValue > + b?.content?.reviewRating?.ratingValue + ? -1 + : 1 ); return ( diff --git a/routes/recipes/[name].tsx b/routes/recipes/[name].tsx index 4125be5..61bb2f9 100644 --- a/routes/recipes/[name].tsx +++ b/routes/recipes/[name].tsx @@ -11,7 +11,7 @@ import { Star } from "@components/Stars.tsx"; import { renderMarkdown } from "@lib/documents.ts"; import { isValidRecipe } from "@lib/recipeSchema.ts"; import { MetaTags } from "@components/MetaTags.tsx"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; export const handler: Handlers<{ recipe: Recipe; session: unknown } | null> = { async GET(_, ctx) { diff --git a/routes/recipes/index.tsx b/routes/recipes/index.tsx index a14a064..133079b 100644 --- a/routes/recipes/index.tsx +++ b/routes/recipes/index.tsx @@ -7,7 +7,7 @@ import { KMenu } from "@islands/KMenu.tsx"; import { RedirectSearchHandler } from "@islands/Search.tsx"; import { GenericResource } from "@lib/types.ts"; import { ResourceCard } from "@components/Card.tsx"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; import { parseResourceUrl, searchResource } from "@lib/search.ts"; export const handler: Handlers< diff --git a/routes/series/[name].tsx b/routes/series/[name].tsx index 0c574d0..f354326 100644 --- a/routes/series/[name].tsx +++ b/routes/series/[name].tsx @@ -9,7 +9,7 @@ import PageHero from "@components/PageHero.tsx"; import { Star } from "@components/Stars.tsx"; import { MetaTags } from "@components/MetaTags.tsx"; import { parseRating } from "@lib/helpers.ts"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; export const handler: Handlers<{ serie: Series; session: unknown }> = { async GET(_, ctx) { @@ -27,7 +27,7 @@ export default function Greet( ) { const { serie, session } = props.data; - const { author = "", date = "" } = (serie?.content || {}); + const { author = "", date = "" } = serie?.content || {}; const content = renderMarkdown( removeImage(serie.description || "", serie.content?.image), @@ -43,7 +43,10 @@ export default function Greet( - + {session && ( diff --git a/routes/series/index.tsx b/routes/series/index.tsx index a33f8ee..c950bc2 100644 --- a/routes/series/index.tsx +++ b/routes/series/index.tsx @@ -7,7 +7,7 @@ import { RedirectSearchHandler } from "@islands/Search.tsx"; import { KMenu } from "@islands/KMenu.tsx"; import { ResourceCard } from "@components/Card.tsx"; import { GenericResource } from "@lib/types.ts"; -import { fetchResource } from "@lib/resources.ts"; +import { fetchResource } from "@lib/marka.ts"; import { parseResourceUrl, searchResource } from "@lib/search.ts"; export const handler: Handlers<