Skip to content

Commit

Permalink
feat(playground): playground supports state for exploded form data pa…
Browse files Browse the repository at this point in the history
…rameters
  • Loading branch information
dsinghvi committed Feb 7, 2025
1 parent 8bf1791 commit 147cd1b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface PlaygroundEndpointAliasFormProps {
alias: ApiDefinition.HttpRequestBodyShape.Alias;
types: Record<ApiDefinition.TypeId, ApiDefinition.TypeDefinition>;
ignoreHeaders: boolean;
setBodyJson: (value: unknown) => void;
setBodyJson: (value: unknown, exploded?: boolean) => void;
value: unknown;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ export function PlaygroundEndpointMultipartForm({
[setFormDataEntry]
);

const handleFormDataExplodedChange = useCallback(
(key: string, value: unknown) => {
setFormDataEntry(key, (oldValue) => {
const newValue =
typeof value === "function" ? value(oldValue?.value) : value;
if (newValue === undefined) {
return undefined;
} else {
return { type: "exploded", value: newValue };
}
});
},
[setFormDataEntry]
);

const shownFields = formData.fields.filter((field) => {
return (
formDataFieldIsRequired(field, types) || !!formDataFormValue[field.key]
Expand Down Expand Up @@ -204,17 +219,32 @@ export function PlaygroundEndpointMultipartForm({
</li>
);
},
property: (bodyProperty) => (
<li key={field.key}>
<PlaygroundObjectPropertyForm
id="body"
property={bodyProperty}
onChange={handleFormDataJsonChange}
value={formDataFormValue[field.key]?.value}
types={types}
/>
</li>
),
property: (bodyProperty) => {
if (bodyProperty.exploded) {
return (
<li key={field.key}>
<PlaygroundObjectPropertyForm
id="body"
property={bodyProperty}
onChange={handleFormDataExplodedChange}
value={formDataFormValue[field.key]?.value}
types={types}
/>
</li>
);
}
return (
<li key={field.key}>
<PlaygroundObjectPropertyForm
id="body"
property={bodyProperty}
onChange={handleFormDataJsonChange}
value={formDataFormValue[field.key]?.value}
types={types}
/>
</li>
);
},
})
)}
{hiddenFields.length > 0 && (
Expand Down
12 changes: 11 additions & 1 deletion packages/fern-docs/ui/src/playground/types/formDataEntryValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ export declare namespace PlaygroundFormDataEntryValue {
type: "json";
value: unknown;
}

interface Exploded {
type: "exploded";
value: unknown[];
}
}

export type PlaygroundFormDataEntryValue =
| PlaygroundFormDataEntryValue.SingleFile
| PlaygroundFormDataEntryValue.MultipleFiles
| PlaygroundFormDataEntryValue.Json;
| PlaygroundFormDataEntryValue.Json
| PlaygroundFormDataEntryValue.Exploded;

export const PlaygroundFormDataEntryValue = {
isSingleFile: (
Expand All @@ -31,4 +37,8 @@ export const PlaygroundFormDataEntryValue = {
isJson: (
value: PlaygroundFormDataEntryValue
): value is PlaygroundFormDataEntryValue.Json => value.type === "json",
isExploded: (
value: PlaygroundFormDataEntryValue
): value is PlaygroundFormDataEntryValue.Exploded =>
value.type === "exploded",
};
6 changes: 6 additions & 0 deletions packages/fern-docs/ui/src/playground/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export function convertPlaygroundFormDataEntryValueToResolvedExampleEndpointRequ
value: value.value,
};
}
case "exploded": {
return {
type: "exploded",
value: value.value,
};
}
default:
assertNever(value);
}
Expand Down
14 changes: 14 additions & 0 deletions packages/fern-docs/ui/src/playground/utils/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ export async function serializeFormStateBody(

if (property?.exploded) {
// For exploded form fields, convert value to array if not already
console.log(
"In serialize.ts form-data json case. Key:",
key,
"Value:",
value
);
const arrayValue = Array.isArray(value.value)
? value.value
: [value.value];
Expand All @@ -85,6 +91,14 @@ export async function serializeFormStateBody(
};
break;
}
case "exploded":
formDataValue[key] = {
...value,
contentType: usesApplicationJsonInFormDataValue
? "application/json"
: undefined,
};
break;
default:
assertNever(value);
}
Expand Down

0 comments on commit 147cd1b

Please sign in to comment.