Skip to content

Commit

Permalink
fix: Fixes an invalid link for security schemes when using the Astro …
Browse files Browse the repository at this point in the history
…`base` option

Co-authored-by: HiDeoo <[email protected]>
  • Loading branch information
taiseimiyaji and HiDeoo authored Nov 27, 2024
1 parent d6296e7 commit 87fb6c8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
7 changes: 7 additions & 0 deletions packages/starlight-openapi/libs/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface ImportMetaEnv {
readonly BASE_URL: string
}

interface ImportMeta {
readonly env: ImportMetaEnv
}
18 changes: 17 additions & 1 deletion packages/starlight-openapi/libs/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import type { StarlightOpenAPISchemaConfig } from './schema'

export { slug } from 'github-slugger'

export function getBaseLink(config: StarlightOpenAPISchemaConfig) {
const base = stripTrailingSlash(import.meta.env.BASE_URL)

/**
* Does not take the Astro `base` configuration option into account.
* @see {@link getBaseLink} for a link that does.
*/
export function getBasePath(config: StarlightOpenAPISchemaConfig) {
const path = config.base
.split('/')
.map((part) => slug(part))
Expand All @@ -13,6 +19,16 @@ export function getBaseLink(config: StarlightOpenAPISchemaConfig) {
return `/${path}/`
}

/**
* Takes the Astro `base` configuration option into account.
* @see {@link getBasePath} for a slug that does not.
*/
export function getBaseLink(config: StarlightOpenAPISchemaConfig) {
const path = stripLeadingSlash(getBasePath(config))

return path ? `${base}/${path}` : `${base}/`
}

export function stripLeadingAndTrailingSlashes(path: string): string {
return stripLeadingSlash(stripTrailingSlash(path))
}
Expand Down
6 changes: 3 additions & 3 deletions packages/starlight-openapi/libs/pathItem.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getOperationsByTag, getWebhooksOperations } from './operation'
import { getBaseLink } from './path'
import { getBasePath } from './path'
import type { Schema } from './schema'
import { makeSidebarGroup, makeSidebarLink, type SidebarManualGroup } from './starlight'

export function getPathItemSidebarGroups({ config, document }: Schema): SidebarManualGroup['items'] {
const baseLink = getBaseLink(config)
const baseLink = getBasePath(config)
const operations = getOperationsByTag(document)

return [...operations.entries()].map(([tag, operations]) =>
Expand All @@ -17,7 +17,7 @@ export function getPathItemSidebarGroups({ config, document }: Schema): SidebarM
}

export function getWebhooksSidebarGroups({ config, document }: Schema): SidebarManualGroup['items'] {
const baseLink = getBaseLink(config)
const baseLink = getBasePath(config)
const operations = getWebhooksOperations(document)

if (operations.length === 0) {
Expand Down
8 changes: 4 additions & 4 deletions packages/starlight-openapi/libs/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import schemas from 'virtual:starlight-openapi-schemas'

import { getOperationsByTag, getWebhooksOperations, type PathItemOperation } from './operation'
import { getBaseLink, stripLeadingAndTrailingSlashes } from './path'
import { getBasePath, stripLeadingAndTrailingSlashes } from './path'
import type { Schema } from './schema'

export function getSchemaStaticPaths(): StarlighOpenAPIRoute[] {
return Object.values(schemas).flatMap((schema) => [
{
params: {
openAPISlug: stripLeadingAndTrailingSlashes(getBaseLink(schema.config)),
openAPISlug: stripLeadingAndTrailingSlashes(getBasePath(schema.config)),
},
props: {
schema,
Expand All @@ -21,7 +21,7 @@ export function getSchemaStaticPaths(): StarlighOpenAPIRoute[] {
}

function getPathItemStaticPaths(schema: Schema): StarlighOpenAPIRoute[] {
const baseLink = getBaseLink(schema.config)
const baseLink = getBasePath(schema.config)
const operations = getOperationsByTag(schema.document)

return [...operations.entries()].flatMap(([, operations]) =>
Expand All @@ -39,7 +39,7 @@ function getPathItemStaticPaths(schema: Schema): StarlighOpenAPIRoute[] {
}

function getWebhooksStaticPaths(schema: Schema): StarlighOpenAPIRoute[] {
const baseLink = getBaseLink(schema.config)
const baseLink = getBasePath(schema.config)
const operations = getWebhooksOperations(schema.document)

return operations.map((operation) => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/starlight-openapi/libs/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from 'astro/zod'
import type { OpenAPI } from 'openapi-types'

import { getBaseLink, stripLeadingAndTrailingSlashes } from './path'
import { getBasePath, stripLeadingAndTrailingSlashes } from './path'
import { getPathItemSidebarGroups, getWebhooksSidebarGroups } from './pathItem'
import { makeSidebarGroup, makeSidebarLink, type SidebarManualGroup } from './starlight'

Expand Down Expand Up @@ -32,7 +32,7 @@ export function getSchemaSidebarGroups(schema: Schema): SidebarManualGroup {
return makeSidebarGroup(
config.label ?? document.info.title,
[
makeSidebarLink('Overview', getBaseLink(config)),
makeSidebarLink('Overview', getBasePath(config)),
...getPathItemSidebarGroups(schema),
...getWebhooksSidebarGroups(schema),
],
Expand Down

0 comments on commit 87fb6c8

Please sign in to comment.