Skip to content

Commit bc17de3

Browse files
authored
[Docs Site] Add query prop to APIRequest and CURL (#21609)
* [Docs Site] Add query prop to APIRequest and CURL * validate apirequest query params via validate * fix lint * support array value in query * bad format * Update src/content/docs/style-guide/components/curl.mdx
1 parent 99ab0f7 commit bc17de3

File tree

4 files changed

+73
-14
lines changed

4 files changed

+73
-14
lines changed

src/components/APIRequest.astro

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,19 @@ if (extraneousParameters.length > 0) {
6767
}
6868
6969
for (const parameter of endpointParameters) {
70-
if (parameter.in === "path") {
71-
const encoded = encodeURIComponent(`{${parameter.name}}`);
72-
73-
if (parameters?.[parameter.name]) {
74-
url.pathname = url.pathname.replace(encoded, 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+
}
7583
}
7684
}
7785
}

src/components/CURL.astro

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,42 @@ 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
19+
.record(z.string(), z.union([z.string(), z.array(z.string())]))
20+
.optional(),
1821
code: z
1922
.custom<Omit<ComponentProps<typeof Code>, "code" | "lang">>()
2023
.optional(),
2124
});
2225
23-
const { method, url, headers, json, form, code } = props.parse(Astro.props);
26+
const {
27+
method,
28+
url: baseUrl,
29+
headers,
30+
json,
31+
form,
32+
query,
33+
code,
34+
} = props.parse(Astro.props);
2435
2536
if (json && form) {
2637
throw new Error("[CURL] Cannot use both 'json' and 'form' properties.");
2738
}
2839
29-
const lines = [`curl ${url}`, `\t--request ${method}`];
40+
const url = new URL(baseUrl);
41+
if (query) {
42+
for (const [key, value] of Object.entries(query)) {
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+
}
50+
}
51+
}
52+
53+
const lines = [`curl "${url.toString()}"`, `\t--request ${method}`];
3054
3155
if (headers) {
3256
for (const [key, value] of Object.entries(headers)) {

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { APIRequest } from "~/components";
1919
path="/zones/{zone_id}/api_gateway/settings/schema_validation"
2020
method="PUT"
2121
json={{
22-
validation_default_mitigation_action: "block"
22+
validation_default_mitigation_action: "block",
2323
}}
2424
/>
2525

@@ -36,7 +36,7 @@ import { APIRequest } from "~/components";
3636
method="POST"
3737
form={{
3838
requireSignedURLs: true,
39-
metadata: '{"key":"value"}'
39+
metadata: '{"key":"value"}',
4040
}}
4141
/>
4242

@@ -54,6 +54,14 @@ import { APIRequest } from "~/components";
5454
},
5555
]}
5656
/>
57+
58+
<APIRequest
59+
path="/zones/{zone_id}/page_shield/scripts"
60+
method="GET"
61+
parameters={{
62+
direction: "asc",
63+
}}
64+
/>
5765
```
5866

5967
## `<APIRequest>` Props
@@ -80,7 +88,16 @@ The HTTP method to use.
8088

8189
**type:** `Record<string, any>`
8290

83-
The path parameters to substitute.
91+
The parameters to substitute - either in the URL path or as query parameters.
92+
93+
For example, `/zones/{zone_id}/page_shield/scripts` can be transformed into `/zones/123/page_shield/scripts?direction=asc` with the following:
94+
95+
```mdx
96+
parameters={{
97+
zone_id: "123",
98+
direction: "asc"
99+
}}
100+
```
84101

85102
If not provided, the component will default to an environment variable. For example, `{setting_id}` will be replaced with `$SETTING_ID`.
86103

@@ -98,4 +115,4 @@ If required properties are missing, the component will throw an error.
98115

99116
The FormData payload to send.
100117

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

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ import { CURL } from "~/components";
2323
json={{
2424
key: "va'l'ue",
2525
}}
26+
query={{
27+
foo: "bar",
28+
bar: ["baz", "qux"],
29+
}}
2630
code={{
27-
mark: "value"
31+
mark: "value",
2832
}}
2933
/>
3034

@@ -35,7 +39,7 @@ import { CURL } from "~/components";
3539
key: "value",
3640
}}
3741
code={{
38-
mark: "value"
42+
mark: "value",
3943
}}
4044
/>
4145
```
@@ -76,6 +80,12 @@ JSON data to include in the request.
7680

7781
The FormData payload to send.
7882

83+
### `query`
84+
85+
**type:** `Record<string, string | string[]>`
86+
87+
URL query parameters to append to the request URL.
88+
7989
### `code`
8090

8191
**type:** `object`
@@ -84,4 +94,4 @@ An object of Expressive Code props, the following props are available:
8494

8595
- [Base Props](https://expressive-code.com/key-features/code-component/#available-props)
8696
- [Line Marker Props](https://expressive-code.com/key-features/text-markers/#props)
87-
- [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)