Skip to content

Commit 35509a9

Browse files
committed
[Docs Site] Add query prop to APIRequest and CURL
1 parent b876bf1 commit 35509a9

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

src/components/APIRequest.astro

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ const props = z
1717
parameters: z.record(z.string(), z.any()).optional(),
1818
json: z.union([Record, z.array(Record)]).optional(),
1919
form: Record.optional(),
20+
query: z.record(z.string(), z.string()).optional(),
2021
})
2122
.strict();
2223
23-
let { path, method, parameters, json, form } = props.parse(Astro.props);
24+
let { path, method, parameters, json, form, query } = props.parse(Astro.props);
2425
2526
if (json && form) {
2627
throw new Error(`[APIRequest] Cannot use both "json" and "form" properties.`);
@@ -67,12 +68,11 @@ if (extraneousParameters.length > 0) {
6768
}
6869
6970
for (const parameter of endpointParameters) {
70-
if (parameter.in === "path") {
71+
if (parameter.in === "path" && parameters?.[parameter.name]) {
7172
const encoded = encodeURIComponent(`{${parameter.name}}`);
72-
73-
if (parameters?.[parameter.name]) {
74-
url.pathname = url.pathname.replace(encoded, parameters[parameter.name]);
75-
}
73+
url.pathname = url.pathname.replace(encoded, parameters[parameter.name]);
74+
} else if (parameter.in === "query" && parameters?.[parameter.name]) {
75+
url.searchParams.set(parameter.name, parameters[parameter.name]);
7676
}
7777
}
7878
@@ -163,6 +163,7 @@ const tokenGroups = operation["x-api-token-group"];
163163
headers={headers}
164164
json={json}
165165
form={form}
166+
query={query}
166167
code={{
167168
title: operation.summary,
168169
}}

src/components/CURL.astro

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,34 @@ 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(),
1819
code: z
1920
.custom<Omit<ComponentProps<typeof Code>, "code" | "lang">>()
2021
.optional(),
2122
});
2223
23-
const { method, url, headers, json, form, code } = props.parse(Astro.props);
24+
const {
25+
method,
26+
url: baseUrl,
27+
headers,
28+
json,
29+
form,
30+
query,
31+
code,
32+
} = props.parse(Astro.props);
2433
2534
if (json && form) {
2635
throw new Error("[CURL] Cannot use both 'json' and 'form' properties.");
2736
}
2837
29-
const lines = [`curl ${url}`, `\t--request ${method}`];
38+
const url = new URL(baseUrl);
39+
if (query) {
40+
for (const [key, value] of Object.entries(query)) {
41+
url.searchParams.set(key, value);
42+
}
43+
}
44+
45+
const lines = [`curl "${url.toString()}"`, `\t--request ${method}`];
3046
3147
if (headers) {
3248
for (const [key, value] of Object.entries(headers)) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,10 @@ If required properties are missing, the component will throw an error.
9898

9999
The FormData payload to send.
100100

101-
This field is not currently validated against the schema.
101+
This field is not currently validated against the schema.
102+
103+
### `query`
104+
105+
**type:** `Record<string, string>`
106+
107+
URL query parameters to append to the request URL.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import { CURL } from "~/components";
2323
json={{
2424
key: "va'l'ue",
2525
}}
26+
query={{
27+
foo: "bar",
28+
bar: "baz",
29+
}}
2630
code={{
2731
mark: "value"
2832
}}
@@ -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>`
86+
87+
URL query parameters to append to the request URL.
88+
7989
### `code`
8090

8191
**type:** `object`

0 commit comments

Comments
 (0)