|
| 1 | +import type { RequestHandler, Response } from 'express' |
| 2 | +import express from 'express' |
| 3 | + |
| 4 | +import { defaultCacheControl } from '@/frame/middleware/cache-control.js' |
| 5 | +import { Context } from '#src/types.js' |
| 6 | +import catchMiddlewareError from '@/observability/middleware/catch-middleware-error.js' |
| 7 | +import { ExtendedRequestWithPageInfo } from '../types' |
| 8 | +import { pageValidationMiddleware, pathValidationMiddleware } from './validation' |
| 9 | +import contextualize from '#src/frame/middleware/context/context.js' |
| 10 | + |
| 11 | +/** START helper functions */ |
| 12 | + |
| 13 | +// for now, we're just querying pageinfo, we'll likely replace /api/pageinfo |
| 14 | +// with /api/meta and move or reference that code here |
| 15 | +async function getArticleMetadata(req: ExtendedRequestWithPageInfo) { |
| 16 | + const queryString = new URLSearchParams(req.query as Record<string, string>).toString() |
| 17 | + const apiUrl = `${req.protocol}://${req.get('host')}/api/pageinfo${queryString ? `?${queryString}` : ''}` |
| 18 | + |
| 19 | + // Fetch the data from the pageinfo API |
| 20 | + const response = await fetch(apiUrl) |
| 21 | + |
| 22 | + // Check if the response is OK |
| 23 | + if (!response.ok) { |
| 24 | + const errorText = await response.text() |
| 25 | + throw new Error(`Failed to fetch metadata: ${response.status} ${errorText}`) |
| 26 | + } |
| 27 | + |
| 28 | + return await response.json() |
| 29 | +} |
| 30 | + |
| 31 | +async function getArticleBody(req: ExtendedRequestWithPageInfo) { |
| 32 | + // req.pageinfo is set from pageValidationMiddleware and pathValidationMiddleware |
| 33 | + // and is in the ExtendedRequestWithPageInfo |
| 34 | + const { page, pathname } = req.pageinfo |
| 35 | + |
| 36 | + // these parts allow us to render the page |
| 37 | + const mockedContext: Context = {} |
| 38 | + const renderingReq = { |
| 39 | + path: pathname, |
| 40 | + language: page.languageCode, |
| 41 | + pagePath: pathname, |
| 42 | + cookies: {}, |
| 43 | + context: mockedContext, |
| 44 | + headers: { |
| 45 | + 'content-type': 'text/markdown', |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + // contextualize and render the page |
| 50 | + await contextualize(renderingReq as ExtendedRequestWithPageInfo, {} as Response, () => {}) |
| 51 | + renderingReq.context.page = page |
| 52 | + renderingReq.context.markdownRequested = true |
| 53 | + return await page.render(renderingReq.context) |
| 54 | +} |
| 55 | + |
| 56 | +/** END helper functions */ |
| 57 | + |
| 58 | +/** START routes */ |
| 59 | +const router = express.Router() |
| 60 | + |
| 61 | +// For all these routes: |
| 62 | +// - pathValidationMiddleware ensures the path is properly structured and handles errors when it's not |
| 63 | +// - pageValidationMiddleware fetches the page from the pagelist, returns 404 to the user if not found |
| 64 | + |
| 65 | +router.get( |
| 66 | + '/', |
| 67 | + pathValidationMiddleware as RequestHandler, |
| 68 | + pageValidationMiddleware as RequestHandler, |
| 69 | + catchMiddlewareError(async function (req: ExtendedRequestWithPageInfo, res: Response) { |
| 70 | + // First, fetch metadata |
| 71 | + const metaData = await getArticleMetadata(req) |
| 72 | + const bodyContent = await getArticleBody(req) |
| 73 | + |
| 74 | + defaultCacheControl(res) |
| 75 | + return res.json({ |
| 76 | + meta: metaData, |
| 77 | + body: bodyContent, |
| 78 | + }) |
| 79 | + }), |
| 80 | +) |
| 81 | + |
| 82 | +router.get( |
| 83 | + '/body', |
| 84 | + pathValidationMiddleware as RequestHandler, |
| 85 | + pageValidationMiddleware as RequestHandler, |
| 86 | + catchMiddlewareError(async function (req: ExtendedRequestWithPageInfo, res: Response) { |
| 87 | + const rendered = await getArticleBody(req) |
| 88 | + defaultCacheControl(res) |
| 89 | + return res.type('text/markdown').send(rendered) |
| 90 | + }), |
| 91 | +) |
| 92 | + |
| 93 | +router.get( |
| 94 | + '/meta', |
| 95 | + pathValidationMiddleware as RequestHandler, |
| 96 | + pageValidationMiddleware as RequestHandler, |
| 97 | + catchMiddlewareError(async function (req: ExtendedRequestWithPageInfo, res: Response) { |
| 98 | + const metaData = await getArticleMetadata(req) |
| 99 | + defaultCacheControl(res) |
| 100 | + return res.json(metaData) |
| 101 | + }), |
| 102 | +) |
| 103 | + |
| 104 | +/** END routes */ |
| 105 | + |
| 106 | +export default router |
0 commit comments