Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wire default values into the api reference ui #1218

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading