From 373f7ef486589235ac41a323ff0414a280be7c06 Mon Sep 17 00:00:00 2001 From: Andrew Jiang Date: Tue, 30 Jul 2024 14:44:21 -0400 Subject: [PATCH] feat: wire default values into the api reference ui (#1218) --- .../types/type-shorthand/TypeShorthand.tsx | 57 ++++++++++++------- .../ui/app/src/resolver/ApiTypeResolver.ts | 1 + packages/ui/app/src/resolver/types.ts | 1 + 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/packages/ui/app/src/api-page/types/type-shorthand/TypeShorthand.tsx b/packages/ui/app/src/api-page/types/type-shorthand/TypeShorthand.tsx index 3fca11e1e9..eb38d1f4ac 100644 --- a/packages/ui/app/src/api-page/types/type-shorthand/TypeShorthand.tsx +++ b/packages/ui/app/src/api-page/types/type-shorthand/TypeShorthand.tsx @@ -1,9 +1,9 @@ import { APIV1Read } from "@fern-api/fdr-sdk"; import { visitDiscriminatedUnion } from "@fern-ui/core-utils"; import clsx from "clsx"; -import numeral from "numeral"; import { ReactNode } from "react"; import { + DereferencedTypeShape, ResolvedTypeDefinition, ResolvedTypeShape, unwrapAlias, @@ -25,10 +25,7 @@ export function renderTypeShorthandRoot( ): ReactNode { const typeShorthand = renderTypeShorthand(unwrapOptional(shape, types), { nullable: isResponse }, types); const unaliasedShape = unwrapAlias(shape, types); - const defaultsTo = - unaliasedShape.type === "optional" && unaliasedShape.shape.type === "primitive" - ? renderDefaultTo(unaliasedShape.shape.value) - : null; + const defaultsTo = renderDefaultsTo(unaliasedShape); return ( {typeShorthand} @@ -37,30 +34,46 @@ export function renderTypeShorthandRoot( ) : !isResponse ? ( Required ) : null} - {defaultsTo != null && {`Defaults to ${defaultsTo}`}} + {defaultsTo != null && ( + + {"Defaults to "} + {defaultsTo} + + )} ); } -function renderDefaultTo(shape: APIV1Read.PrimitiveType): string | undefined { +function renderDefaultsTo(shape: DereferencedTypeShape): string | undefined { + if (shape.type !== "optional") { + return undefined; + } + + if (shape.shape.type === "primitive") { + return renderDefaultToPrimitive(shape.shape.value); + } + + if (shape.shape.type === "enum") { + return shape.shape.default; + } + + return undefined; +} + +function renderDefaultToPrimitive(shape: APIV1Read.PrimitiveType): string | undefined { return visitDiscriminatedUnion(shape, "type")._visit({ - string: (string) => string.default, - integer: (integer) => { - if (integer.default == null) { - return undefined; - } - return numeral(integer.default).format("0,0"); - }, - double: (double) => double.default?.toString(), + string: (value) => value.default, + integer: (value) => value.default?.toString(), + double: (value) => value.default?.toString(), uint: () => undefined, uint64: () => undefined, - boolean: () => undefined, - long: () => undefined, - datetime: () => undefined, - uuid: () => undefined, - base64: () => undefined, - date: () => undefined, - bigInteger: () => undefined, + boolean: (value) => value.default?.toString(), + long: (value) => value.default?.toString(), + datetime: (datetime) => datetime.default, + uuid: (uuid) => uuid.default, + base64: (base64) => base64.default, + date: (value) => value.default, + bigInteger: (value) => value.default, _other: () => undefined, }); } diff --git a/packages/ui/app/src/resolver/ApiTypeResolver.ts b/packages/ui/app/src/resolver/ApiTypeResolver.ts index 399f293f6f..6d1b173085 100644 --- a/packages/ui/app/src/resolver/ApiTypeResolver.ts +++ b/packages/ui/app/src/resolver/ApiTypeResolver.ts @@ -69,6 +69,7 @@ export class ApiTypeResolver { files: this.mdxOptions?.files, }), availability, + default: enum_.default, }), undiscriminatedUnion: async (undiscriminatedUnion) => ({ type: "undiscriminatedUnion", diff --git a/packages/ui/app/src/resolver/types.ts b/packages/ui/app/src/resolver/types.ts index b7caf67bb2..030a2b14a5 100644 --- a/packages/ui/app/src/resolver/types.ts +++ b/packages/ui/app/src/resolver/types.ts @@ -528,6 +528,7 @@ interface ResolvedEnumShape extends WithMetadata { name: string | undefined; type: "enum"; values: ResolvedEnumValue[]; + default: string | undefined; } export interface ResolvedEnumValue extends WithMetadata {