Skip to content

Commit

Permalink
improvement: add x-fern-base-path to asyncapi extensions (#3953)
Browse files Browse the repository at this point in the history
  • Loading branch information
armandobelardo authored Jun 28, 2024
1 parent 83e9429 commit d8575c9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/cli/openapi-ir-to-fern/src/FernDefnitionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 12 additions & 1 deletion packages/cli/openapi-parser/src/asyncapi/fernExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof FernAsyncAPIExtension>;
4 changes: 3 additions & 1 deletion packages/cli/openapi-parser/src/asyncapi/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { AsyncAPIV2 } from "./v2";
export interface AsyncAPIIntermediateRepresentation {
schemas: Record<SchemaId, Schema>;
channel: WebsocketChannel | undefined;
basePath: string | undefined;
}

export function parseAsyncAPI({
Expand Down Expand Up @@ -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<string | undefined>(document, FernAsyncAPIExtension.BASE_PATH)
};
}

Expand Down
3 changes: 3 additions & 0 deletions packages/cli/openapi-parser/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
Expand Down

0 comments on commit d8575c9

Please sign in to comment.