-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add undiscriminated union wrapper to various responses (#2173)
- Loading branch information
1 parent
3abfe99
commit c4c2988
Showing
4 changed files
with
217 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...rn-docs/ui/src/api-reference/utils/__test__/maybeWrapTypeWithUndiscriminatedUnion.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { ApiDefinition } from "@fern-api/fdr-sdk"; | ||
import { maybeWrapTypeWithUndiscriminatedUnion } from "../maybeWrapTypeWithUndiscriminatedUnion"; | ||
|
||
describe("maybeWrapTypeWithUndiscriminatedUnion", () => { | ||
const types: Record<string, ApiDefinition.TypeDefinition> = {}; | ||
|
||
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); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
packages/fern-docs/ui/src/api-reference/utils/maybeWrapTypeWithUndiscriminatedUnion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { ApiDefinition } from "@fern-api/fdr-sdk"; | ||
|
||
export function maybeWrapTypeWithUndiscriminatedUnion( | ||
shape: ApiDefinition.TypeShape, | ||
types: Record<string, ApiDefinition.TypeDefinition>, | ||
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; | ||
} |