Skip to content

Commit 8081dda

Browse files
authored
[Docs Site] Support FormData in APIRequest (#20583)
1 parent 5e57bed commit 8081dda

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

src/components/APIRequest.astro

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ const props = z
1414
method: z.enum(["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"]),
1515
parameters: z.record(z.string(), z.any()).optional(),
1616
json: z.record(z.string(), z.any()).optional(),
17+
form: z.record(z.string(), z.any()).optional(),
1718
})
1819
.strict();
1920
20-
let { path, method, parameters, json } = props.parse(Astro.props);
21+
let { path, method, parameters, json, form } = props.parse(Astro.props);
22+
23+
if (json && form) {
24+
throw new Error(`[APIRequest] Cannot use both "json" and "form" properties.`);
25+
}
2126
2227
const schema = await getSchema();
2328
@@ -52,7 +57,7 @@ const extraneousParameters = providedParameters.filter(
5257
5358
if (extraneousParameters.length > 0) {
5459
throw new Error(
55-
`[APIRequest] Provided parameters ${extraneousParameters.join(", ")} not found in schema.`,
60+
`[APIRequest] Provided parameters ${extraneousParameters.join(", ")} not found in ${method} ${path} schema.`,
5661
);
5762
}
5863
@@ -101,21 +106,17 @@ const jsonSchema = requestBody?.content?.["application/json"]?.schema as
101106
| undefined;
102107
103108
if (jsonSchema?.required) {
104-
json ??= {};
105-
106-
const providedProperties = Object.keys(json);
109+
const providedProperties = Object.keys(json ?? {});
107110
const requiredProperties = jsonSchema.required;
108111
109112
const missingProperties = requiredProperties.filter(
110113
(property) => !providedProperties.includes(property),
111114
);
112115
113-
for (const property of missingProperties) {
114-
const defaultValue =
115-
(jsonSchema.properties?.[property] as OpenAPIV3.SchemaObject).default ??
116-
property;
117-
118-
json[property] = defaultValue;
116+
if (missingProperties.length > 0) {
117+
throw new Error(
118+
`[APIRequest] Missing the following required properties for ${method} ${path}: ${missingProperties.join(", ")}`,
119+
);
119120
}
120121
}
121122
@@ -144,6 +145,7 @@ const tokenGroups = operation["x-api-token-group"];
144145
method={method}
145146
headers={headers}
146147
json={json}
148+
form={form}
147149
code={{
148150
title: operation.summary,
149151
}}

src/components/CURL.astro

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ const props = z.object({
1212
url: z.string().url(),
1313
headers: z.record(z.string(), z.string()).default({}),
1414
json: z.record(z.string(), z.any()).optional(),
15+
form: z.record(z.string(), z.any()).optional(),
1516
code: z
1617
.custom<Omit<ComponentProps<typeof Code>, "code" | "lang">>()
1718
.optional(),
1819
});
1920
20-
const { method, url, headers, json, code } = props.parse(Astro.props);
21+
const { method, url, headers, json, form, code } = props.parse(Astro.props);
22+
23+
if (json && form) {
24+
throw new Error("[CURL] Cannot use both 'json' and 'form' properties.");
25+
}
2126
2227
const lines = [`curl ${url}`, `\t--request ${method}`];
2328
@@ -33,6 +38,13 @@ if (json) {
3338
3439
lines.push(`\t--json '${jsonLines.join("\n")}'`);
3540
}
41+
42+
if (form) {
43+
const formLines = Object.entries(form).map(
44+
([key, value]) => `\t--form "${key}=${value}"`,
45+
);
46+
lines.push(...formLines);
47+
}
3648
---
3749

3850
<Code {...code} lang="bash" code={lines.join(" \\\n")} />

src/content/docs/style-guide/components/api-request.mdx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ import { APIRequest } from "~/components";
2828
setting_id: "ciphers",
2929
}}
3030
/>
31+
32+
<APIRequest
33+
path="/accounts/{account_id}/images/v2/direct_upload"
34+
method="POST"
35+
form={{
36+
requireSignedURLs: true,
37+
metadata: '{"key":"value"}'
38+
}}
39+
/>
3140
```
3241

3342
## `<APIRequest>` Props
@@ -64,4 +73,12 @@ If not provided, the component will default to an environment variable. For exam
6473

6574
The JSON payload to send.
6675

67-
Default values can be omitted, and the component will use default values for any missing fields.
76+
If required properties are missing, the component will throw an error.
77+
78+
### `form`
79+
80+
**type:** `Record<string, any>`
81+
82+
The FormData payload to send.
83+
84+
This field is not currently validated against the schema.

src/content/docs/style-guide/components/curl.mdx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { CURL } from "~/components";
1616
import { CURL } from "~/components";
1717

1818
<CURL
19-
url="https://example.com"
19+
url="https://httpbin.org/anything"
2020
method="POST"
2121
json={{
2222
key: "value",
@@ -25,6 +25,17 @@ import { CURL } from "~/components";
2525
mark: "value"
2626
}}
2727
/>
28+
29+
<CURL
30+
url="https://httpbin.org/anything"
31+
method="POST"
32+
form={{
33+
key: "value",
34+
}}
35+
code={{
36+
mark: "value"
37+
}}
38+
/>
2839
```
2940

3041
## `<CURL>` Props
@@ -57,6 +68,12 @@ The headers to include in the request.
5768

5869
JSON data to include in the request.
5970

71+
### `form`
72+
73+
**type:** `Record<string, any>`
74+
75+
The FormData payload to send.
76+
6077
### `code`
6178

6279
**type:** `object`

0 commit comments

Comments
 (0)