|
1 | 1 | import { readFileSync } from 'node:fs' |
2 | 2 | import { join } from 'node:path' |
| 3 | +import type { MethodInformation } from 'fumadocs-openapi' |
| 4 | +import type { InlineCodeUsageGenerator } from 'fumadocs-openapi/requests/generators' |
3 | 5 | import { createOpenAPI } from 'fumadocs-openapi/server' |
| 6 | +import { buildAuthCodeSamples } from '@/lib/openapi-code-samples' |
4 | 7 | import { OPENAPI_SPEC_FILES } from '@/lib/openapi-specs' |
5 | 8 |
|
6 | 9 | export const openapi = createOpenAPI({ |
@@ -75,6 +78,109 @@ function getSpecs(): Record<string, unknown>[] { |
75 | 78 | return cachedSpecs |
76 | 79 | } |
77 | 80 |
|
| 81 | +type SecurityRequirement = Record<string, string[]> |
| 82 | + |
| 83 | +interface SecurityScheme { |
| 84 | + type?: string |
| 85 | + in?: string |
| 86 | + name?: string |
| 87 | + scheme?: string |
| 88 | +} |
| 89 | + |
| 90 | +interface SharedSecurity { |
| 91 | + security: SecurityRequirement[] |
| 92 | + schemes: Record<string, SecurityScheme> |
| 93 | +} |
| 94 | + |
| 95 | +const AUTH_SAMPLE_VALUE = 'YOUR_API_KEY' |
| 96 | + |
| 97 | +let cachedSharedSecurity: SharedSecurity | null = null |
| 98 | + |
| 99 | +/** |
| 100 | + * Document-level security shared by every rendered spec. Code samples are |
| 101 | + * generated from an operation alone, with no handle on the document that owns |
| 102 | + * it, so the specs must agree on their default security — a spec that diverges |
| 103 | + * would silently get another document's auth in its samples. |
| 104 | + */ |
| 105 | +function getSharedSecurity(): SharedSecurity { |
| 106 | + if (cachedSharedSecurity) return cachedSharedSecurity |
| 107 | + |
| 108 | + let shared: SharedSecurity | undefined |
| 109 | + let sharedFile: string | undefined |
| 110 | + |
| 111 | + getSpecs().forEach((spec, index) => { |
| 112 | + const file = OPENAPI_SPEC_FILES[index] |
| 113 | + const current: SharedSecurity = { |
| 114 | + security: (spec.security as SecurityRequirement[] | undefined) ?? [], |
| 115 | + schemes: |
| 116 | + ((spec.components as Record<string, unknown> | undefined)?.securitySchemes as |
| 117 | + | Record<string, SecurityScheme> |
| 118 | + | undefined) ?? {}, |
| 119 | + } |
| 120 | + |
| 121 | + if (!shared) { |
| 122 | + shared = current |
| 123 | + sharedFile = file |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + if (JSON.stringify(current) !== JSON.stringify(shared)) { |
| 128 | + throw new Error( |
| 129 | + `[docs] ${file} declares different default security than ${sharedFile}. Every OpenAPI spec must share one security scheme so generated code samples stay correct.` |
| 130 | + ) |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + cachedSharedSecurity = shared ?? { security: [], schemes: {} } |
| 135 | + return cachedSharedSecurity |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Resolve a security requirement to the request headers a sample must send. |
| 140 | + * The first non-empty alternative wins — an empty one means the operation also |
| 141 | + * accepts anonymous callers, which is not what a reference example should show. |
| 142 | + */ |
| 143 | +function resolveAuthHeaders( |
| 144 | + security: SecurityRequirement[], |
| 145 | + schemes: Record<string, SecurityScheme> |
| 146 | +): Record<string, string> { |
| 147 | + const requirement = security.find((item) => Object.keys(item).length > 0) |
| 148 | + if (!requirement) return {} |
| 149 | + |
| 150 | + const headers: Record<string, string> = {} |
| 151 | + for (const name of Object.keys(requirement)) { |
| 152 | + const scheme = schemes[name] |
| 153 | + if (!scheme) { |
| 154 | + throw new Error(`[docs] Operation references undefined security scheme "${name}"`) |
| 155 | + } |
| 156 | + if (scheme.type === 'apiKey' && scheme.in === 'header' && scheme.name) { |
| 157 | + headers[scheme.name] = AUTH_SAMPLE_VALUE |
| 158 | + continue |
| 159 | + } |
| 160 | + if (scheme.type === 'http' && scheme.scheme === 'bearer') { |
| 161 | + headers.Authorization = `Bearer ${AUTH_SAMPLE_VALUE}` |
| 162 | + continue |
| 163 | + } |
| 164 | + throw new Error( |
| 165 | + `[docs] Security scheme "${name}" (type ${scheme.type}) cannot be rendered as a request header in code samples` |
| 166 | + ) |
| 167 | + } |
| 168 | + return headers |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * Code samples for an operation, with its authentication header included. |
| 173 | + * Fumadocs derives sample requests from declared parameters only, so without |
| 174 | + * this every endpoint documents an unauthenticated call that returns `401`. |
| 175 | + */ |
| 176 | +export function getAuthenticatedCodeSamples(method: MethodInformation): InlineCodeUsageGenerator[] { |
| 177 | + const shared = getSharedSecurity() |
| 178 | + const security = (method.security as SecurityRequirement[] | undefined) ?? shared.security |
| 179 | + const headers = resolveAuthHeaders(security, shared.schemes) |
| 180 | + if (Object.keys(headers).length === 0) return [] |
| 181 | + return buildAuthCodeSamples(headers) |
| 182 | +} |
| 183 | + |
78 | 184 | /** |
79 | 185 | * Locate an operation by path + method across every rendered spec, returning the |
80 | 186 | * operation together with the spec that owns it so `$ref`s resolve within the |
|
0 commit comments