Skip to content

Commit

Permalink
Port render-product-name.js to TypeScript (#51276)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe authored Jun 24, 2024
1 parent 8445e63 commit 9c79757
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
16 changes: 0 additions & 16 deletions src/frame/middleware/context/render-product-name.js

This file was deleted.

29 changes: 29 additions & 0 deletions src/frame/middleware/context/render-product-name.ts
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion src/frame/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export type Context = {
genericTocNested?: ToC[]
breadcrumbs?: Breadcrumb[]
glossaries?: Glossary[]
currentProductName?: string
}

export type Glossary = {
Expand Down

0 comments on commit 9c79757

Please sign in to comment.