Skip to content

Commit

Permalink
feat: add metadata service
Browse files Browse the repository at this point in the history
  • Loading branch information
cptchloroplast committed Aug 28, 2024
1 parent d2dd552 commit a9bc9bc
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 137 deletions.
15 changes: 6 additions & 9 deletions src/components/Footer.astro
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
---
import metadata from "../metadata"
import { PostService } from "@services"
const metadata = Astro.locals.metadata
const { author: { name, email }, repo } = metadata
const build = process.env.CF_PAGES_COMMIT_SHA
const href = `${repo}/commit/${build}`
const current = new Date().getUTCFullYear()
// https://github.com/withastro/rfcs/discussions/118
const initial = 2018
// const initial = Astro.fetchContent<Post>("../pages/posts/*.md")
// .reduce((result, post) => {
// const year = new Date(post.published).getUTCFullYear()
// return year < result ? year : result
// }, current)
const service = PostService(Astro.locals.runtime.env.DB)
const earliest = await service.getEarliest()
const initial = earliest?.published.getUTCFullYear() ?? current
---
<footer>
<small>
Expand Down
7 changes: 4 additions & 3 deletions src/components/Header.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script lang="ts">
import metadata from "../metadata"
import type { Navigation } from "@schemas";
import { Item } from "./navigation"
import { Icon } from "./ui"
export let navigation: Navigation[]
let show = false
const { navigation } = metadata
const toggle = () => show = !show
const close = (event: any) => {
Expand Down
5 changes: 4 additions & 1 deletion src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ export type Environment = {
}

type Runtime = import("@astrojs/cloudflare").Runtime<Environment>
type Metadata = import("@schemas").Metadata
declare global {
namespace App {
interface Locals extends Runtime { }
interface Locals extends Runtime {
metadata: Metadata
}
}
}
6 changes: 3 additions & 3 deletions src/layouts/BaseLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type Props = {
code?: boolean
}
import metadata from "../metadata.ts"
const { author: { name, email }, profile: { bio, contact } } = metadata
const metadata = Astro.locals.metadata
const { author: { name, email }, profile: { bio, contact }, navigation } = metadata
const {
title,
Expand Down Expand Up @@ -42,7 +42,7 @@ const { origin, hostname } = Astro.site!
}
</head>
<body>
<Header client:load />
<Header navigation={navigation} client:load />
<div>
<main>
<slot />
Expand Down
115 changes: 0 additions & 115 deletions src/metadata.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MetadataSchema } from "@schemas"
import { MetadataService } from "@services"
import type { APIContext, MiddlewareNext } from "astro"

export async function onRequest(context: APIContext, next: MiddlewareNext) {
const service = MetadataService(context.locals.runtime.env.BLOG)
const metadata = await service.get()
if (!metadata) throw new Error("Missing metadata")
MetadataSchema.parse(metadata)
context.locals.metadata = metadata
return next()
}
7 changes: 2 additions & 5 deletions src/pages/rss.xml.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import rss from "@astrojs/rss"
import metadata from "../metadata"
import { PostService } from "@services"
import type { APIContext } from "astro"

const { description } = metadata

export async function GET(context: APIContext) {
const metadata = context.locals.metadata
const service = PostService(context.locals.runtime.env.DB)
const posts = await service.list()
console.log(posts)
return rss({
title: context.site!.origin,
description: description,
description: metadata.description,
site: context.site!,
items: posts.map((post) => ({
title: post.title,
Expand Down
15 changes: 15 additions & 0 deletions src/services/MetadataService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { R2Bucket } from "@cloudflare/workers-types"
import type { Metadata } from "@schemas"

type MetadataService = {
get(): Promise<Metadata | undefined>
}

export function MetadataService(blog: R2Bucket): MetadataService {
return {
async get() {
const object = await blog.get("metadata.json")
return object?.json<Metadata>()
},
}
}
3 changes: 2 additions & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./EmailService"
export * from "./Repository"
export * from "./GearService"
export * from "./PostService"
export * from "./PostService"
export * from "./MetadataService"

0 comments on commit a9bc9bc

Please sign in to comment.