diff --git a/packages/chronicle/src/components/api/api-overview.tsx b/packages/chronicle/src/components/api/api-overview.tsx index 76c4b01..8fc4aaf 100644 --- a/packages/chronicle/src/components/api/api-overview.tsx +++ b/packages/chronicle/src/components/api/api-overview.tsx @@ -21,7 +21,7 @@ interface ApiOverviewProps { auth?: { type: string; header: string; placeholder?: string } } -export function ApiOverview({ method, path, operation, auth }: ApiOverviewProps) { +export function ApiOverview({ method, path, operation, serverUrl, auth }: ApiOverviewProps) { const params = (operation.parameters ?? []) as OpenAPIV3.ParameterObject[] const body = getRequestBody(operation.requestBody as OpenAPIV3.RequestBodyObject | undefined) @@ -36,7 +36,7 @@ export function ApiOverview({ method, path, operation, auth }: ApiOverviewProps) ? headerFields : [] - const fullUrl = '{domain}' + path + const fullUrl = serverUrl + path const snippetHeaders: Record = {} if (auth) snippetHeaders[auth.header] = auth.placeholder ?? 'YOUR_API_KEY' if (body) snippetHeaders['Content-Type'] = body.contentType ?? 'application/json' diff --git a/packages/chronicle/src/lib/env.ts b/packages/chronicle/src/lib/env.ts new file mode 100644 index 0000000..50682de --- /dev/null +++ b/packages/chronicle/src/lib/env.ts @@ -0,0 +1,9 @@ +export function substituteEnvVars(value: string): string { + return value.replace(/\$\{(\w+)\}/g, (_, name) => { + const val = process.env[name]; + if (val === undefined) { + throw new Error(`Environment variable '${name}' is not set`); + } + return val; + }); +} diff --git a/packages/chronicle/src/lib/openapi.ts b/packages/chronicle/src/lib/openapi.ts index ef9183d..3116bf3 100644 --- a/packages/chronicle/src/lib/openapi.ts +++ b/packages/chronicle/src/lib/openapi.ts @@ -3,6 +3,7 @@ import path from 'node:path' import { parse as parseYaml } from 'yaml' import type { OpenAPIV2, OpenAPIV3 } from 'openapi-types' import type { ApiConfig, ApiServerConfig, ApiAuthConfig } from '@/types/config' +import { substituteEnvVars } from '@/lib/env' type JsonObject = Record @@ -41,7 +42,7 @@ export async function loadApiSpec(config: ApiConfig, projectRoot: string): Promi return { name: config.name, basePath: config.basePath, - server: config.server, + server: { ...config.server, url: substituteEnvVars(config.server.url) }, auth: config.auth, document: v3Doc, }