| | import type { Response, NextFunction } from 'express' |
| |
|
| | import type { ExtendedRequest, ProductExample } from '@/types' |
| | import { getDataByLanguage } from '@/data-directory/lib/get-data' |
| |
|
| | function getProductExampleData( |
| | product: string, |
| | key: string, |
| | language: string, |
| | ): ProductExample[] | undefined { |
| | |
| | |
| | |
| | |
| | |
| | try { |
| | return getDataByLanguage(`product-examples.${product}.${key}`, language) |
| | } catch (error) { |
| | if (error instanceof Error && (error as any).code === 'ENOENT') return |
| | throw error |
| | } |
| | } |
| |
|
| | export default async function productExamples( |
| | req: ExtendedRequest, |
| | res: Response, |
| | next: NextFunction, |
| | ) { |
| | if (!req.context) throw new Error('request is not contextualized') |
| | if (!req.context.page) return next() |
| | if (req.context.currentLayoutName !== 'product-landing') return next() |
| |
|
| | const { currentProduct, currentLanguage } = req.context |
| | if (currentProduct === undefined) throw new Error('currentProduct is not set') |
| | if (currentLanguage === undefined) throw new Error('currentLanguage is not set') |
| | if (currentProduct.includes('.')) |
| | throw new Error(`currentProduct cannot contain a . (${currentProduct})`) |
| |
|
| | req.context.productCommunityExamples = getProductExampleData( |
| | currentProduct, |
| | 'community-examples', |
| | currentLanguage, |
| | ) |
| |
|
| | req.context.productUserExamples = getProductExampleData( |
| | currentProduct, |
| | 'user-examples', |
| | currentLanguage, |
| | ) |
| |
|
| | return next() |
| | } |
| |
|