From d1e7336c2a5e7261f85cf43788a45ccb5ab11aa6 Mon Sep 17 00:00:00 2001 From: Rohin Bhargava Date: Mon, 25 Nov 2024 16:42:46 -0700 Subject: [PATCH] extension examples --- .../3.1/extensions/availability-function.ts | 23 ++++++++++ .../3.1/extensions/availability-node.ts | 44 +++++++++++++++++++ .../extensions/x-fern-group-name-function.ts | 4 ++ .../3.1/extensions/x-fern-group-name-node.ts | 28 ++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 packages/parsers/src/openapi/3.1/extensions/availability-function.ts create mode 100644 packages/parsers/src/openapi/3.1/extensions/availability-node.ts create mode 100644 packages/parsers/src/openapi/3.1/extensions/x-fern-group-name-function.ts create mode 100644 packages/parsers/src/openapi/3.1/extensions/x-fern-group-name-node.ts diff --git a/packages/parsers/src/openapi/3.1/extensions/availability-function.ts b/packages/parsers/src/openapi/3.1/extensions/availability-function.ts new file mode 100644 index 0000000000..75775afe8e --- /dev/null +++ b/packages/parsers/src/openapi/3.1/extensions/availability-function.ts @@ -0,0 +1,23 @@ +import { FdrAPI } from "@fern-api/fdr-sdk"; +import { UnreachableCaseError } from "ts-essentials"; + +type Availability = "beta" | "in-development" | "deprecated"; + +export function availabilityFunction(input: T): FdrAPI.Availability | undefined { + const inputWithAvailability = input as T & { + "x-fern-availability"?: Availability; + }; + switch (inputWithAvailability["x-fern-availability"]) { + case "beta": + return FdrAPI.Availability.Beta; + case "in-development": + return FdrAPI.Availability.InDevelopment; + case "deprecated": + return FdrAPI.Availability.Deprecated; + case undefined: + return undefined; + default: + new UnreachableCaseError(inputWithAvailability["x-fern-availability"]); + return undefined; + } +} diff --git a/packages/parsers/src/openapi/3.1/extensions/availability-node.ts b/packages/parsers/src/openapi/3.1/extensions/availability-node.ts new file mode 100644 index 0000000000..ec313f82b2 --- /dev/null +++ b/packages/parsers/src/openapi/3.1/extensions/availability-node.ts @@ -0,0 +1,44 @@ +import { FdrAPI } from "@fern-api/fdr-sdk"; +import { UnreachableCaseError } from "ts-essentials"; +import { + BaseOpenApiV3_1ConverterNode, + BaseOpenApiV3_1ConverterNodeConstructorArgs, +} from "../../BaseOpenApiV3_1Converter.node"; + +type Availability = "beta" | "in-development" | "deprecated"; + +export class AvailabilityNode extends BaseOpenApiV3_1ConverterNode { + availability?: FdrAPI.Availability; + + constructor(args: BaseOpenApiV3_1ConverterNodeConstructorArgs) { + super(args); + this.safeParse(); + } + + parse(): void { + const inputWithAvailability = this.input as T & { + "x-fern-availability"?: Availability; + }; + switch (inputWithAvailability["x-fern-availability"]) { + case "beta": + this.availability = FdrAPI.Availability.Beta; + break; + case "in-development": + this.availability = FdrAPI.Availability.InDevelopment; + break; + case "deprecated": + this.availability = FdrAPI.Availability.Deprecated; + break; + case undefined: + this.availability = undefined; + break; + default: + new UnreachableCaseError(inputWithAvailability["x-fern-availability"]); + return undefined; + } + } + + convert(): FdrAPI.Availability | undefined { + return this.availability; + } +} diff --git a/packages/parsers/src/openapi/3.1/extensions/x-fern-group-name-function.ts b/packages/parsers/src/openapi/3.1/extensions/x-fern-group-name-function.ts new file mode 100644 index 0000000000..91dada20de --- /dev/null +++ b/packages/parsers/src/openapi/3.1/extensions/x-fern-group-name-function.ts @@ -0,0 +1,4 @@ +// This would be used to set a member on the node, that could be used upstream +export function xFernGroupNameFunction(input: { "x-fern-group-name"?: string }): string | undefined { + return input["x-fern-group-name"]; +} diff --git a/packages/parsers/src/openapi/3.1/extensions/x-fern-group-name-node.ts b/packages/parsers/src/openapi/3.1/extensions/x-fern-group-name-node.ts new file mode 100644 index 0000000000..66e673c654 --- /dev/null +++ b/packages/parsers/src/openapi/3.1/extensions/x-fern-group-name-node.ts @@ -0,0 +1,28 @@ +import { + BaseOpenApiV3_1ConverterNode, + BaseOpenApiV3_1ConverterNodeConstructorArgs, +} from "../../BaseOpenApiV3_1Converter.node"; + +export declare namespace XFernGroupNameConverterNode { + export interface Input { + "x-fern-group-name"?: string; + } +} + +export class XFernGroupNameConverterNode extends BaseOpenApiV3_1ConverterNode { + groupName?: string; + + constructor(args: BaseOpenApiV3_1ConverterNodeConstructorArgs) { + super(args); + this.safeParse(); + } + + // This would be used to set a member on the node + parse(): void { + this.groupName = (this.input as T & { "x-fern-group-name"?: string })["x-fern-group-name"]; + } + + convert(): string | undefined { + return this.groupName; + } +}