Skip to content

Commit

Permalink
feat: wire default values into the api reference ui (#1218)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Jul 30, 2024
1 parent 8ff0d71 commit 373f7ef
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
57 changes: 35 additions & 22 deletions packages/ui/app/src/api-page/types/type-shorthand/TypeShorthand.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 (
<span className={clsx("fern-api-property-meta", className)}>
<span>{typeShorthand}</span>
Expand All @@ -37,30 +34,46 @@ export function renderTypeShorthandRoot(
) : !isResponse ? (
<span className="t-danger">Required</span>
) : null}
{defaultsTo != null && <span>{`Defaults to ${defaultsTo}`}</span>}
{defaultsTo != null && (
<span>
{"Defaults to "}
<code>{defaultsTo}</code>
</span>
)}
</span>
);
}

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 | undefined>({
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,
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/app/src/resolver/ApiTypeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class ApiTypeResolver {
files: this.mdxOptions?.files,
}),
availability,
default: enum_.default,
}),
undiscriminatedUnion: async (undiscriminatedUnion) => ({
type: "undiscriminatedUnion",
Expand Down
1 change: 1 addition & 0 deletions packages/ui/app/src/resolver/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ interface ResolvedEnumShape extends WithMetadata {
name: string | undefined;
type: "enum";
values: ResolvedEnumValue[];
default: string | undefined;
}

export interface ResolvedEnumValue extends WithMetadata {
Expand Down

0 comments on commit 373f7ef

Please sign in to comment.