| import { GetServerSideProps } from 'next' |
| import { Operation } from '@/rest/components/types' |
| import { RestReferencePage } from '@/rest/components/RestReferencePage' |
| import { |
| addUINamespaces, |
| getMainContext, |
| MainContext, |
| MainContextT, |
| } from '@/frame/components/context/MainContext' |
| import { |
| AutomatedPageContext, |
| AutomatedPageContextT, |
| getAutomatedPageContextFromRequest, |
| } from '@/automated-pipelines/components/AutomatedPageContext' |
| import type { MiniTocItem } from '@/frame/components/context/ArticleContext' |
| import { |
| getTocLandingContextFromRequest, |
| TocLandingContext, |
| TocLandingContextT, |
| } from '@/frame/components/context/TocLandingContext' |
| import type { TocItem } from '@/landings/types' |
| import { TocLanding } from '@/landings/components/TocLanding' |
|
|
| type MinitocItemsT = { |
| restOperationsMiniTocItems: MiniTocItem[] |
| } |
|
|
| type Props = { |
| mainContext: MainContextT |
| tocLandingContext: TocLandingContextT |
| automatedPageContext: AutomatedPageContextT |
| restOperations: Operation[] |
| } |
|
|
| export default function Category({ |
| mainContext, |
| automatedPageContext, |
| tocLandingContext, |
| restOperations, |
| }: Props) { |
| const { relativePath } = mainContext |
|
|
| return ( |
| <MainContext.Provider value={mainContext}> |
| <AutomatedPageContext.Provider value={automatedPageContext}> |
| {/* When the page is the rest product landing page, we don't want to |
| render the rest-specific sidebar because toggling open the categories |
| won't have the minitoc items at that level. These are pages that have |
| category - subcategory - and operations */} |
| {relativePath?.endsWith('index.md') ? ( |
| <TocLandingContext.Provider value={tocLandingContext}> |
| <TocLanding /> |
| </TocLandingContext.Provider> |
| ) : ( |
| <RestReferencePage restOperations={restOperations} /> |
| )} |
| </AutomatedPageContext.Provider> |
| </MainContext.Provider> |
| ) |
| } |
|
|
| export const getServerSideProps: GetServerSideProps<Props> = async (context) => { |
| const { default: getRest, getRestMiniTocItems } = await import('@/rest/lib/index') |
| const nonEnterpriseDefaultVersionModule = await import( |
| '@/versions/lib/non-enterprise-default-version' |
| ) |
| const nonEnterpriseDefaultVersion = nonEnterpriseDefaultVersionModule.default as string |
|
|
| const req = context.req as any |
| const res = context.res as any |
| const tocLandingContext = getTocLandingContextFromRequest(req) |
| |
| const category = context.params!.category as string |
| let subcategory = context.params!.subcategory as string |
| const currentVersion = context.params!.versionId as string |
| const currentLanguage = req.context.currentLanguage as string |
| const allVersions = req.context.allVersions |
| const queryApiVersion = context.query.apiVersion |
| const apiVersion = allVersions[currentVersion].apiVersions.includes(queryApiVersion) |
| ? queryApiVersion |
| : allVersions[currentVersion].latestApiVersion |
|
|
| |
| |
| if (!subcategory) { |
| subcategory = category |
| } |
|
|
| const restData = await getRest(currentVersion, apiVersion) |
| const restOperations = (restData && restData[category] && restData[category][subcategory]) || [] |
|
|
| |
| |
| |
| |
| |
| |
| const restCategoryOperations = (restData && restData[category]) || {} |
| const restCategoryTocItems = [] |
|
|
| for (const [subCat, subCatOperations] of Object.entries(restCategoryOperations)) { |
| let versionPathSegment: string |
|
|
| |
| |
| |
| |
| if (context.params?.versionId === nonEnterpriseDefaultVersion) { |
| versionPathSegment = '/' |
| } else { |
| versionPathSegment = `/${context.params?.versionId}/` |
| } |
|
|
| const fullSubcategoryPath = `/${context.locale}${versionPathSegment}rest/${context.params?.category}/${subCat}` |
| |
| |
| |
| |
| |
| |
| let fullSubcategoryTitle |
|
|
| const pageTocItem = tocLandingContext.tocItems.find( |
| (tocItem) => tocItem.fullPath === fullSubcategoryPath, |
| ) |
|
|
| if (pageTocItem) { |
| fullSubcategoryTitle = pageTocItem.title |
| } else { |
| |
| |
| |
| fullSubcategoryTitle = `${subCat[0].toUpperCase()}${subCat.slice(1).replaceAll('-', ' ')}` |
| } |
|
|
| const restSubcategoryTocs: TocItem[] = [] |
| const miniTocItems = (await getRestMiniTocItems( |
| category, |
| subCat, |
| apiVersion, |
| subCatOperations, |
| currentLanguage, |
| currentVersion, |
| req.context, |
| )) as MinitocItemsT |
|
|
| for (const operationMinitoc of miniTocItems.restOperationsMiniTocItems) { |
| const { title, href: miniTocAnchor } = operationMinitoc.contents |
| const fullPath = `/${context.locale}${versionPathSegment}rest/${context.params?.category}/${subCat}${miniTocAnchor}` |
|
|
| restSubcategoryTocs.push({ |
| fullPath, |
| title, |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| restCategoryTocItems.push({ |
| fullPath: fullSubcategoryPath, |
| title: fullSubcategoryTitle, |
| childTocItems: restSubcategoryTocs, |
| }) |
| } |
|
|
| |
| |
| |
| const { miniTocItems } = getAutomatedPageContextFromRequest(req) |
|
|
| |
| |
|
|
| |
| |
| |
| if (restOperations) { |
| const { restOperationsMiniTocItems } = (await getRestMiniTocItems( |
| category, |
| subcategory, |
| apiVersion, |
| restOperations, |
| currentLanguage, |
| currentVersion, |
| req.context, |
| )) as MinitocItemsT |
|
|
| if (restOperationsMiniTocItems) { |
| miniTocItems.push(...restOperationsMiniTocItems) |
| } |
| } |
|
|
| |
| |
| tocLandingContext.tocItems = restCategoryTocItems |
|
|
| const mainContext = await getMainContext(req, res) |
| if (tocLandingContext.currentLearningTrack?.trackName) { |
| addUINamespaces(req, mainContext.data.ui, ['learning_track_nav']) |
| } |
|
|
| return { |
| props: { |
| restOperations, |
| mainContext, |
| automatedPageContext: getAutomatedPageContextFromRequest(req), |
| tocLandingContext, |
| }, |
| } |
| } |
|
|