Skip to content

Commit

Permalink
Migrate /hcp/api-docs/packer to split OpenAPI view
Browse files Browse the repository at this point in the history
  • Loading branch information
zchsh committed Nov 18, 2024
1 parent 93109c4 commit d3b67be
Showing 1 changed file with 66 additions and 60 deletions.
126 changes: 66 additions & 60 deletions src/pages/hcp/api-docs/packer/[[...page]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,80 @@
* SPDX-License-Identifier: MPL-2.0
*/

// Lib
import { fetchCloudApiVersionData } from 'lib/api-docs/fetch-cloud-api-version-data'
// View
import OpenApiDocsView from 'views/open-api-docs-view'
import OpenApiDocsViewV2 from 'views/open-api-docs-view-v2'
import {
schemaModComponent,
schemaModShortenHcp,
shortenProtobufAnyDescription,
} from 'views/open-api-docs-view/utils/massage-schema-utils'
import {
getStaticPaths,
getStaticProps as getOpenApiDocsStaticProps,
} from 'views/open-api-docs-view/server'
generateStaticPaths,
generateStaticPropsVersioned,
} from 'views/open-api-docs-view-v2/server'
// Utils
import { getOperationGroupKeyFromPath } from 'views/open-api-docs-view-v2/utils/get-operation-group-key-from-path'
// Schema transforms
import { schemaTransformShortenHcp } from 'views/open-api-docs-view-v2/schema-transforms/schema-transform-shorten-hcp'
import { schemaTransformComponent } from 'views/open-api-docs-view-v2/schema-transforms/schema-transform-component'
import { shortenProtobufAnyDescription } from 'views/open-api-docs-view-v2/schema-transforms/shorten-protobuf-any-description'
// Types
import type { GetStaticProps, GetStaticPropsContext } from 'next'
import type { OpenAPIV3 } from 'openapi-types'
import type {
OpenApiDocsParams,
OpenApiDocsViewProps,
OpenApiDocsPageConfig,
} from 'views/open-api-docs-view/types'
GetStaticPaths,
GetStaticProps,
GetStaticPropsContext,
} from 'next'
import type {
OpenApiDocsV2Params,
OpenApiDocsViewV2Props,
OpenApiDocsViewV2Config,
} from 'views/open-api-docs-view-v2/types'

/**
* Configure the specific spec file we want to fetch,
* as well as some other minor content details and bells & whistles.
* Configure this OpenAPI spec page, specifying the source,
* and additional configuration that doesn't fit in the schema itself.
*/
const PAGE_CONFIG: OpenApiDocsPageConfig = {
const PAGE_CONFIG: OpenApiDocsViewV2Config = {
backToLink: {
href: '/hcp',
text: 'HashiCorp Cloud Platform',
},
basePath: '/hcp/api-docs/packer',
productSlug: 'hcp',
serviceProductSlug: 'packer',
githubSourceDirectory: {
breadcrumbLinksPrefix: [
{
title: 'Developer',
url: '/',
},
{
title: 'HashiCorp Cloud Platform',
url: '/hcp',
},
],
schemaSource: {
owner: 'hashicorp',
repo: 'hcp-specs',
path: 'specs/cloud-packer-service',
ref: 'main',
},
productContext: 'hcp',
theme: 'packer',
statusIndicatorConfig: {
pageUrl: 'https://status.hashicorp.com',
endpointUrl:
'https://status.hashicorp.com/api/v2/components/0mbkqnrzg33w.json',
},
navResourceItems: [
resourceLinks: [
{
title: 'Tutorial Library',
text: 'Tutorial Library',
href: '/tutorials/library?product=packer&edition=hcp',
},
{
title: 'Community',
text: 'Community',
href: 'https://discuss.hashicorp.com/',
},
{
title: 'Support',
text: 'Support',
href: 'https://www.hashicorp.com/customer-success',
},
],
/**
* Massage the schema data a little bit
*/
massageSchemaForClient: (schemaData: OpenAPIV3.Document) => {
schemaTransforms: [
// Replace "HashiCorp Cloud Platform" with "HCP" in the title
const withShortTitle = schemaModShortenHcp(schemaData)
schemaTransformShortenHcp,
/**
* Shorten the description of the protobufAny schema
*
Expand All @@ -77,41 +90,34 @@ const PAGE_CONFIG: OpenApiDocsPageConfig = {
* Related task:
* https://app.asana.com/0/1207339219333499/1207339701271604/f
*/
const withShortProtobufDocs = schemaModComponent(
withShortTitle,
'google.protobuf.Any',
shortenProtobufAnyDescription
)
// Return the schema data with modifications
return withShortProtobufDocs
},
(schema) => {
return schemaTransformComponent(
schema,
'google.protobuf.Any',
shortenProtobufAnyDescription
)
},
],
}

/**
* Get static paths, using `versionData` fetched from GitHub.
* Get static paths, using the configured `schemaSource`.
*/
export { getStaticPaths }
export const getStaticPaths: GetStaticPaths<OpenApiDocsV2Params> = async () => {
return await generateStaticPaths({
schemaSource: PAGE_CONFIG.schemaSource,
schemaTransforms: PAGE_CONFIG.schemaTransforms,
})
}

/**
* Get static props, using `versionData` fetched from GitHub.
*
* Note that only a single version may be returned, in which case the
* version switcher will not be rendered.
* Get static paths, using the configured `schemaSource`.
*/
export const getStaticProps: GetStaticProps<
OpenApiDocsViewProps,
OpenApiDocsParams
> = async ({ params }: GetStaticPropsContext<OpenApiDocsParams>) => {
// Fetch all version data, based on remote `stable` & `preview` subfolders
const versionData = await fetchCloudApiVersionData(
PAGE_CONFIG.githubSourceDirectory
)
// Generate static props based on page configuration, params, and versionData
return await getOpenApiDocsStaticProps({
...PAGE_CONFIG,
context: { params },
versionData,
})
OpenApiDocsViewV2Props,
OpenApiDocsV2Params
> = async ({ params }: GetStaticPropsContext<OpenApiDocsV2Params>) => {
return await generateStaticPropsVersioned(PAGE_CONFIG, params?.page)
}

export default OpenApiDocsView
export default OpenApiDocsViewV2

0 comments on commit d3b67be

Please sign in to comment.