Files
Folo/apps/ssr/src/meta-handler.ts
T

101 lines
2.4 KiB
TypeScript

import { env } from "@follow/shared/env.ssr"
import type { FollowClient } from "@follow-app/client-sdk"
import type { FastifyReply, FastifyRequest } from "fastify"
import { match } from "path-to-regexp"
import { createFollowClient } from "./lib/api-client"
import importer from "./meta-handler.map"
interface MetaTagdata {
type: "meta"
property: string
content: string
}
interface MetaOpenGraph {
type: "openGraph"
title: string
description?: string
image?: string | null
}
interface MetaTitle {
type: "title"
title: string
}
interface MetaDescription {
type: "description"
description: string
}
interface MetaHydrateData {
type: "hydrate"
data: any
key: string
}
export type MetaTag = MetaTagdata | MetaOpenGraph | MetaTitle | MetaHydrateData | MetaDescription
export async function injectMetaHandler(
req: FastifyRequest,
res: FastifyReply,
): Promise<MetaTag[]> {
const apiClient = createFollowClient()
const webOrigin = env.VITE_WEB_URL
const url = req.originalUrl
for (const [pattern, handler] of Object.entries(importer)) {
const matchFn = match(pattern, { decode: decodeURIComponent })
const parsedUrl = new URL(url, webOrigin)
const result = matchFn(parsedUrl.pathname)
if (result) {
return (await handler({
params: result.params as Record<string, string>,
searchParams: parsedUrl.searchParams,
url: parsedUrl,
req,
apiClient,
origin: webOrigin,
setStatus(status) {
res.status(status)
},
setStatusText(statusText) {
res.raw.statusMessage = statusText
},
throwError(status, message) {
throw new MetaError(status, message)
},
})) as MetaTag[]
}
}
return []
}
export function defineMetadata<Params extends Record<string, string>, T extends readonly MetaTag[]>(
fn: (args: {
req: FastifyRequest
url: URL
params: Params
apiClient: FollowClient
searchParams: URLSearchParams
origin: string
setStatus: (status: number) => void
setStatusText: (statusText: string) => void
throwError: (status: number, message: any) => never
}) => Promise<T> | T,
) {
return fn
}
export class MetaError extends Error {
status: number
metaMessage: object
constructor(status: number, message: object) {
super("Meta Error")
this.status = status
this.metaMessage = message
}
}