Skip to content

Commit

Permalink
extension examples
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava committed Nov 25, 2024
1 parent bba54cb commit d1e7336
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<T>(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;
}
}
44 changes: 44 additions & 0 deletions packages/parsers/src/openapi/3.1/extensions/availability-node.ts
Original file line number Diff line number Diff line change
@@ -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<T> extends BaseOpenApiV3_1ConverterNode<T, FdrAPI.Availability | undefined> {
availability?: FdrAPI.Availability;

constructor(args: BaseOpenApiV3_1ConverterNodeConstructorArgs<T>) {
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;
}
}
Original file line number Diff line number Diff line change
@@ -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"];
}
Original file line number Diff line number Diff line change
@@ -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<T> extends BaseOpenApiV3_1ConverterNode<T, string | undefined> {
groupName?: string;

constructor(args: BaseOpenApiV3_1ConverterNodeConstructorArgs<T>) {
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;
}
}

0 comments on commit d1e7336

Please sign in to comment.