Skip to content

Commit fa0b7aa

Browse files
authored
[Docs Site] Support JSON arrays in APIRequest and CURL (#21297)
1 parent 7d69417 commit fa0b7aa

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

src/components/APIRequest.astro

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import Details from "./Details.astro";
88
99
type Props = z.input<typeof props>;
1010
11+
const Record = z.record(z.string(), z.any());
12+
1113
const props = z
1214
.object({
1315
path: z.string(),
1416
method: z.enum(["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"]),
1517
parameters: z.record(z.string(), z.any()).optional(),
16-
json: z.record(z.string(), z.any()).optional(),
17-
form: z.record(z.string(), z.any()).optional(),
18+
json: z.union([Record, z.array(Record)]).optional(),
19+
form: Record.optional(),
1820
})
1921
.strict();
2022
@@ -109,17 +111,27 @@ const jsonSchema = requestBody?.content?.["application/json"]?.schema as
109111
| undefined;
110112
111113
if (jsonSchema?.required) {
112-
const providedProperties = Object.keys(json ?? {});
113-
const requiredProperties = jsonSchema.required;
114-
115-
const missingProperties = requiredProperties.filter(
116-
(property) => !providedProperties.includes(property),
117-
);
114+
const checkProperties = (obj?: object) => {
115+
const providedProperties = Object.keys(obj ?? {});
116+
const requiredProperties = jsonSchema.required!;
118117
119-
if (missingProperties.length > 0) {
120-
throw new Error(
121-
`[APIRequest] Missing the following required properties for ${method} ${path}: ${missingProperties.join(", ")}`,
118+
const missingProperties = requiredProperties.filter(
119+
(property) => !providedProperties.includes(property),
122120
);
121+
122+
if (missingProperties.length > 0) {
123+
throw new Error(
124+
`[APIRequest] Missing the following required properties for ${method} ${path}: ${missingProperties.join(", ")}`,
125+
);
126+
}
127+
};
128+
129+
if (Array.isArray(json)) {
130+
for (const obj of json) {
131+
checkProperties(obj);
132+
}
133+
} else {
134+
checkProperties(json);
123135
}
124136
}
125137
@@ -136,7 +148,9 @@ const tokenGroups = operation["x-api-token-group"];
136148
</span>
137149
<ul>
138150
{tokenGroups.map((group) => (
139-
<li><code>{group}</code></li>
151+
<li>
152+
<code>{group}</code>
153+
</li>
140154
))}
141155
</ul>
142156
</Details>

src/components/CURL.astro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import { Code } from "@astrojs/starlight/components";
55
66
type Props = z.input<typeof props>;
77
8+
const Record = z.record(z.string(), z.any());
9+
810
const props = z.object({
911
method: z
1012
.enum(["GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"])
1113
.default("GET"),
1214
url: z.string().url(),
1315
headers: z.record(z.string(), z.string()).default({}),
14-
json: z.record(z.string(), z.any()).optional(),
15-
form: z.record(z.string(), z.any()).optional(),
16+
json: z.union([Record, z.array(Record)]).optional(),
17+
form: Record.optional(),
1618
code: z
1719
.custom<Omit<ComponentProps<typeof Code>, "code" | "lang">>()
1820
.optional(),

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ import { APIRequest } from "~/components";
3939
metadata: '{"key":"value"}'
4040
}}
4141
/>
42+
43+
<APIRequest
44+
path="/zones/{zone_id}/cloud_connector/rules"
45+
method="PUT"
46+
json={[
47+
{
48+
expression: 'http.request.uri.path wildcard "/images/*"',
49+
provider: "cloudflare_r2",
50+
description: "Connect to R2 bucket containing images",
51+
parameters: {
52+
host: "mybucketcustomdomain.example.com",
53+
},
54+
},
55+
]}
56+
/>
4257
```
4358

4459
## `<APIRequest>` Props
@@ -71,7 +86,7 @@ If not provided, the component will default to an environment variable. For exam
7186

7287
### `json`
7388

74-
**type:** `Record<string, any>`
89+
**type:** `Record<string, any> | Record<string, any>[]`
7590

7691
The JSON payload to send.
7792

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ The headers to include in the request.
6666

6767
### `json`
6868

69-
**type:** `Record<string, any>`
69+
**type:** `Record<string, any> | Record<string, any>[]`
7070

7171
JSON data to include in the request.
7272

0 commit comments

Comments
 (0)