From c4c2988cb944f0d74ee6fcacf4909adcc56cf634 Mon Sep 17 00:00:00 2001 From: Rohin Bhargava Date: Mon, 17 Feb 2025 15:04:37 -0500 Subject: [PATCH] add undiscriminated union wrapper to various responses (#2173) --- .../endpoints/EndpointRequestSection.tsx | 34 ++++++- .../endpoints/EndpointResponseSection.tsx | 71 ++++++++++++++- ...beWrapTypeWithUndiscriminatedUnion.test.ts | 89 +++++++++++++++++++ .../maybeWrapTypeWithUndiscriminatedUnion.ts | 28 ++++++ 4 files changed, 217 insertions(+), 5 deletions(-) create mode 100644 packages/fern-docs/ui/src/api-reference/utils/__test__/maybeWrapTypeWithUndiscriminatedUnion.test.ts create mode 100644 packages/fern-docs/ui/src/api-reference/utils/maybeWrapTypeWithUndiscriminatedUnion.ts diff --git a/packages/fern-docs/ui/src/api-reference/endpoints/EndpointRequestSection.tsx b/packages/fern-docs/ui/src/api-reference/endpoints/EndpointRequestSection.tsx index 4bf3782bb0..b1a6e1c5bf 100644 --- a/packages/fern-docs/ui/src/api-reference/endpoints/EndpointRequestSection.tsx +++ b/packages/fern-docs/ui/src/api-reference/endpoints/EndpointRequestSection.tsx @@ -8,11 +8,27 @@ import { renderTypeShorthand } from "../../type-shorthand"; import { JsonPropertyPath } from "../examples/JsonPropertyPath"; import { TypeComponentSeparator } from "../types/TypeComponentSeparator"; import { TypeReferenceDefinitions } from "../types/type-reference/TypeReferenceDefinitions"; +import { maybeWrapTypeWithUndiscriminatedUnion } from "../utils/maybeWrapTypeWithUndiscriminatedUnion"; import { EndpointParameter, EndpointParameterContent, } from "./EndpointParameter"; +const FORM_DATA_BYTES_SHAPE: ApiDefinition.TypeShape = { + type: "alias", + value: { + type: "primitive", + value: { + type: "string", + format: "binary", + regex: undefined, + minLength: undefined, + maxLength: undefined, + default: undefined, + }, + }, +}; + export declare namespace EndpointRequestSection { export interface Props { request: ApiDefinition.HttpRequest; @@ -108,7 +124,21 @@ export const EndpointRequestSection: React.FC = ({ })} )), - bytes: () => null, + bytes: () => ( + + ), object: (obj) => ( = ({ ), alias: (alias) => ( + No content + + ); case "fileDownload": + return ( + + ); case "streamingText": - return null; + return ( + + ); case "stream": return ( ); - default: + default: { return ( ); + } } } diff --git a/packages/fern-docs/ui/src/api-reference/utils/__test__/maybeWrapTypeWithUndiscriminatedUnion.test.ts b/packages/fern-docs/ui/src/api-reference/utils/__test__/maybeWrapTypeWithUndiscriminatedUnion.test.ts new file mode 100644 index 0000000000..371617685d --- /dev/null +++ b/packages/fern-docs/ui/src/api-reference/utils/__test__/maybeWrapTypeWithUndiscriminatedUnion.test.ts @@ -0,0 +1,89 @@ +import { ApiDefinition } from "@fern-api/fdr-sdk"; +import { maybeWrapTypeWithUndiscriminatedUnion } from "../maybeWrapTypeWithUndiscriminatedUnion"; + +describe("maybeWrapTypeWithUndiscriminatedUnion", () => { + const types: Record = {}; + + it("wraps primitive types in undiscriminated union", () => { + const stringShape: ApiDefinition.TypeShape = { + type: "alias", + value: { + type: "primitive", + value: { + type: "string", + format: undefined, + regex: undefined, + minLength: undefined, + maxLength: undefined, + default: undefined, + }, + }, + }; + + const result = maybeWrapTypeWithUndiscriminatedUnion( + stringShape, + types, + "String Type" + ); + + expect(result).toEqual({ + type: "undiscriminatedUnion", + variants: [ + { + displayName: "String Type", + description: undefined, + shape: stringShape, + availability: undefined, + }, + ], + }); + }); + + it("does not wrap object types", () => { + const objectShape: ApiDefinition.TypeShape = { + type: "object", + properties: [], + extends: [], + extraProperties: undefined, + }; + + const result = maybeWrapTypeWithUndiscriminatedUnion(objectShape, types); + + expect(result).toBe(objectShape); + }); + + it("does not wrap enum types", () => { + const enumShape: ApiDefinition.TypeShape = { + type: "enum", + values: [], + default: undefined, + }; + + const result = maybeWrapTypeWithUndiscriminatedUnion(enumShape, types); + + expect(result).toBe(enumShape); + }); + + it("does not wrap discriminated union types", () => { + const unionShape: ApiDefinition.TypeShape = { + type: "discriminatedUnion", + discriminant: ApiDefinition.PropertyKey("type"), + variants: [], + }; + + const result = maybeWrapTypeWithUndiscriminatedUnion(unionShape, types); + + expect(result).toBe(unionShape); + }); + + it("does not wrap undiscriminated union types", () => { + const unionShape: ApiDefinition.TypeShape = { + type: "undiscriminatedUnion", + variants: [], + }; + + const result = maybeWrapTypeWithUndiscriminatedUnion(unionShape, types); + + expect(result).toBe(unionShape); + }); +}); diff --git a/packages/fern-docs/ui/src/api-reference/utils/maybeWrapTypeWithUndiscriminatedUnion.ts b/packages/fern-docs/ui/src/api-reference/utils/maybeWrapTypeWithUndiscriminatedUnion.ts new file mode 100644 index 0000000000..7f87e137ff --- /dev/null +++ b/packages/fern-docs/ui/src/api-reference/utils/maybeWrapTypeWithUndiscriminatedUnion.ts @@ -0,0 +1,28 @@ +import { ApiDefinition } from "@fern-api/fdr-sdk"; + +export function maybeWrapTypeWithUndiscriminatedUnion( + shape: ApiDefinition.TypeShape, + types: Record, + displayName?: string +): ApiDefinition.TypeShapeOrReference { + const unwrapped = ApiDefinition.unwrapReference(shape, types); + if ( + unwrapped.shape.type !== "object" && + unwrapped.shape.type !== "undiscriminatedUnion" && + unwrapped.shape.type !== "discriminatedUnion" && + unwrapped.shape.type !== "enum" + ) { + return { + type: "undiscriminatedUnion", + variants: [ + { + displayName, + description: undefined, + shape, + availability: undefined, + }, + ], + }; + } + return shape; +}