From d8575c97610b96e58a2858dc386ad61b8af55d9d Mon Sep 17 00:00:00 2001 From: Armando Belardo <11140328+armandobelardo@users.noreply.github.com> Date: Fri, 28 Jun 2024 17:56:28 -0400 Subject: [PATCH] improvement: add x-fern-base-path to asyncapi extensions (#3953) --- .../openapi-ir-to-fern/src/FernDefnitionBuilder.ts | 5 +++++ .../openapi-parser/src/asyncapi/fernExtensions.ts | 13 ++++++++++++- packages/cli/openapi-parser/src/asyncapi/parse.ts | 4 +++- packages/cli/openapi-parser/src/parse.ts | 3 +++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/cli/openapi-ir-to-fern/src/FernDefnitionBuilder.ts b/packages/cli/openapi-ir-to-fern/src/FernDefnitionBuilder.ts index faaa4399bcd..0c5dcc4d5f3 100644 --- a/packages/cli/openapi-ir-to-fern/src/FernDefnitionBuilder.ts +++ b/packages/cli/openapi-ir-to-fern/src/FernDefnitionBuilder.ts @@ -332,6 +332,11 @@ export class FernDefinitionBuilderImpl implements FernDefinitionBuilder { public addChannel(file: RelativeFilePath, { channel }: { channel: RawSchemas.WebSocketChannelSchema }): void { const fernFile = this.getOrCreateFile(file); fernFile.channel = channel; + + const basePath = this.basePath; + if (basePath != null) { + fernFile.channel.path = path.join(basePath, channel.path); + } } public addChannelExample( diff --git a/packages/cli/openapi-parser/src/asyncapi/fernExtensions.ts b/packages/cli/openapi-parser/src/asyncapi/fernExtensions.ts index 309ebf0a5b0..623e22055ab 100644 --- a/packages/cli/openapi-parser/src/asyncapi/fernExtensions.ts +++ b/packages/cli/openapi-parser/src/asyncapi/fernExtensions.ts @@ -28,7 +28,18 @@ export const FernAsyncAPIExtension = { * value: * data: "12340213" */ - FERN_EXAMPLES: "x-fern-examples" + FERN_EXAMPLES: "x-fern-examples", + + /** + * Prepends the configured base path to all of the endpoint paths. + * + * x-fern-base-path: /v1 + * servers: + * - url: https://api.example.com + * paths: + * /path/to/my/endpoint: + */ + BASE_PATH: "x-fern-base-path" } as const; export type FernAsyncAPIExtension = Values; diff --git a/packages/cli/openapi-parser/src/asyncapi/parse.ts b/packages/cli/openapi-parser/src/asyncapi/parse.ts index 9cbaa8ffd2c..da0667aa1fa 100644 --- a/packages/cli/openapi-parser/src/asyncapi/parse.ts +++ b/packages/cli/openapi-parser/src/asyncapi/parse.ts @@ -26,6 +26,7 @@ import { AsyncAPIV2 } from "./v2"; export interface AsyncAPIIntermediateRepresentation { schemas: Record; channel: WebsocketChannel | undefined; + basePath: string | undefined; } export function parseAsyncAPI({ @@ -214,7 +215,8 @@ export function parseAsyncAPI({ schemas: Object.fromEntries( Object.entries(schemas).map(([id, schema]) => [id, convertSchemaWithExampleToSchema(schema)]) ), - channel: parsedChannel + channel: parsedChannel, + basePath: getExtension(document, FernAsyncAPIExtension.BASE_PATH) }; } diff --git a/packages/cli/openapi-parser/src/parse.ts b/packages/cli/openapi-parser/src/parse.ts index 74300b7d7f3..9f4ad0ab2a5 100644 --- a/packages/cli/openapi-parser/src/parse.ts +++ b/packages/cli/openapi-parser/src/parse.ts @@ -109,6 +109,9 @@ export async function parse({ ...parsedAsyncAPI.schemas }; } + if (parsedAsyncAPI.basePath != null) { + ir.basePath = parsedAsyncAPI.basePath; + } } else { taskContext.failAndThrow(`${spec.absoluteFilepath} is not a valid OpenAPI or AsyncAPI file`); }