From 9c797578bed508d096d9600f11cbd3f203b82596 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Mon, 24 Jun 2024 09:06:57 -0400 Subject: [PATCH] Port `render-product-name.js` to TypeScript (#51276) --- .../middleware/context/render-product-name.js | 16 ---------- .../middleware/context/render-product-name.ts | 29 +++++++++++++++++++ src/frame/middleware/index.ts | 2 +- src/types.ts | 1 + 4 files changed, 31 insertions(+), 17 deletions(-) delete mode 100644 src/frame/middleware/context/render-product-name.js create mode 100644 src/frame/middleware/context/render-product-name.ts diff --git a/src/frame/middleware/context/render-product-name.js b/src/frame/middleware/context/render-product-name.js deleted file mode 100644 index ba6b7d65bd5d..000000000000 --- a/src/frame/middleware/context/render-product-name.js +++ /dev/null @@ -1,16 +0,0 @@ -import { renderContent } from '#src/content-render/index.js' - -export default async function renderProductName(req, res, next) { - const { productMap, currentProduct } = req.context - const productObject = productMap[currentProduct] - if (!productObject) { - // If the "currentProduct" isn't recognized, there's no point trying - // to render its name. Skip this middleware. - return next() - } - req.context.currentProductName = await renderContent(productObject.name, req.context, { - textOnly: true, - cache: true, - }) - return next() -} diff --git a/src/frame/middleware/context/render-product-name.ts b/src/frame/middleware/context/render-product-name.ts new file mode 100644 index 000000000000..786c3b614e86 --- /dev/null +++ b/src/frame/middleware/context/render-product-name.ts @@ -0,0 +1,29 @@ +import type { Response, NextFunction } from 'express' + +import type { ExtendedRequest } from '@/types' +import { renderContent } from '@/content-render/index.js' + +export default async function renderProductName( + req: ExtendedRequest, + res: Response, + next: NextFunction, +) { + if (!req.context) throw new Error('request is not contextualized') + const { productMap, currentProduct } = req.context + if (!productMap) throw new Error('request is not contextualized') + + // `currentProduct` might be an empty string, which is a valid value. + if (currentProduct === undefined) throw new Error('currentProduct is not contextualized') + + const productObject = productMap[currentProduct] + if (!productObject) { + // If the "currentProduct" isn't recognized, there's no point trying + // to render its name. Skip this middleware. + return next() + } + req.context.currentProductName = await renderContent(productObject.name, req.context, { + textOnly: true, + cache: true, + }) + return next() +} diff --git a/src/frame/middleware/index.ts b/src/frame/middleware/index.ts index 3a4a1bd0f1fd..54fc785fb203 100644 --- a/src/frame/middleware/index.ts +++ b/src/frame/middleware/index.ts @@ -44,7 +44,7 @@ import currentProductTree from './context/current-product-tree' import genericToc from './context/generic-toc' import breadcrumbs from './context/breadcrumbs' import glossaries from './context/glossaries' -import renderProductName from './context/render-product-name.js' +import renderProductName from './context/render-product-name' import features from '@/versions/middleware/features.js' import productExamples from './context/product-examples.js' import productGroups from './context/product-groups.js' diff --git a/src/types.ts b/src/types.ts index 5c7b0418cbb8..0ea548a8ab50 100644 --- a/src/types.ts +++ b/src/types.ts @@ -92,6 +92,7 @@ export type Context = { genericTocNested?: ToC[] breadcrumbs?: Breadcrumb[] glossaries?: Glossary[] + currentProductName?: string } export type Glossary = {