Skip to content

Commit 14f84c4

Browse files
committed
support array value in query
1 parent 23c853b commit 14f84c4

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

src/components/APIRequest.astro

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,20 @@ if (extraneousParameters.length > 0) {
6767
}
6868
6969
for (const parameter of endpointParameters) {
70-
if (parameter.in === "path" && parameters?.[parameter.name]) {
71-
const encoded = encodeURIComponent(`{${parameter.name}}`);
72-
url.pathname = url.pathname.replace(encoded, parameters[parameter.name]);
73-
} else if (parameter.in === "query" && parameters?.[parameter.name]) {
74-
url.searchParams.set(parameter.name, parameters[parameter.name]);
70+
const value = parameters?.[parameter.name];
71+
if (value) {
72+
if (parameter.in === "path") {
73+
const encoded = encodeURIComponent(`{${parameter.name}}`);
74+
url.pathname = url.pathname.replace(encoded, value);
75+
} else if (parameter.in === "query") {
76+
if (Array.isArray(value)) {
77+
for (const v of value) {
78+
url.searchParams.append(parameter.name, v);
79+
}
80+
} else {
81+
url.searchParams.set(parameter.name, value);
82+
}
83+
}
7584
}
7685
}
7786

src/components/CURL.astro

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ const props = z.object({
1515
headers: z.record(z.string(), z.string()).default({}),
1616
json: z.union([Record, z.array(Record)]).optional(),
1717
form: Record.optional(),
18-
query: z.record(z.string(), z.string()).optional(),
18+
query: z
19+
.record(z.string(), z.union([z.string(), z.array(z.string())]))
20+
.optional(),
1921
code: z
2022
.custom<Omit<ComponentProps<typeof Code>, "code" | "lang">>()
2123
.optional(),
@@ -38,7 +40,13 @@ if (json && form) {
3840
const url = new URL(baseUrl);
3941
if (query) {
4042
for (const [key, value] of Object.entries(query)) {
41-
url.searchParams.set(key, value);
43+
if (Array.isArray(value)) {
44+
for (const v of value) {
45+
url.searchParams.append(key, v);
46+
}
47+
} else {
48+
url.searchParams.set(key, value);
49+
}
4250
}
4351
}
4452

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ styleGuide:
88

99
```mdx
1010
import { APIRequest } from "~/components";
11+
12+
;
1113
```
1214

1315
## Usage
@@ -19,7 +21,7 @@ import { APIRequest } from "~/components";
1921
path="/zones/{zone_id}/api_gateway/settings/schema_validation"
2022
method="PUT"
2123
json={{
22-
validation_default_mitigation_action: "block"
24+
validation_default_mitigation_action: "block",
2325
}}
2426
/>
2527

@@ -36,7 +38,7 @@ import { APIRequest } from "~/components";
3638
method="POST"
3739
form={{
3840
requireSignedURLs: true,
39-
metadata: '{"key":"value"}'
41+
metadata: '{"key":"value"}',
4042
}}
4143
/>
4244

@@ -59,7 +61,7 @@ import { APIRequest } from "~/components";
5961
path="/zones/{zone_id}/page_shield/scripts"
6062
method="GET"
6163
parameters={{
62-
direction: "asc"
64+
direction: "asc",
6365
}}
6466
/>
6567
```
@@ -115,4 +117,4 @@ If required properties are missing, the component will throw an error.
115117

116118
The FormData payload to send.
117119

118-
This field is not currently validated against the schema.
120+
This field is not currently validated against the schema.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import { CURL } from "~/components";
2525
}}
2626
query={{
2727
foo: "bar",
28-
bar: "baz",
28+
bar: ["baz", "qux"],
2929
}}
3030
code={{
31-
mark: "value"
31+
mark: "value",
3232
}}
3333
/>
3434

@@ -39,7 +39,7 @@ import { CURL } from "~/components";
3939
key: "value",
4040
}}
4141
code={{
42-
mark: "value"
42+
mark: "value",
4343
}}
4444
/>
4545
```
@@ -94,4 +94,4 @@ An object of Expressive Code props, the following props are available:
9494

9595
- [Base Props](https://expressive-code.com/key-features/code-component/#available-props)
9696
- [Line Marker Props](https://expressive-code.com/key-features/text-markers/#props)
97-
- [Collapsible Sections Props](https://expressive-code.com/plugins/collapsible-sections/#props)
97+
- [Collapsible Sections Props](https://expressive-code.com/plugins/collapsible-sections/#props)

0 commit comments

Comments
 (0)