Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/components/APIRequest.astro
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,19 @@ if (extraneousParameters.length > 0) {
}

for (const parameter of endpointParameters) {
if (parameter.in === "path") {
const encoded = encodeURIComponent(`{${parameter.name}}`);

if (parameters?.[parameter.name]) {
url.pathname = url.pathname.replace(encoded, parameters[parameter.name]);
const value = parameters?.[parameter.name];
if (value) {
if (parameter.in === "path") {
const encoded = encodeURIComponent(`{${parameter.name}}`);
url.pathname = url.pathname.replace(encoded, value);
} else if (parameter.in === "query") {
if (Array.isArray(value)) {
for (const v of value) {
url.searchParams.append(parameter.name, v);
}
} else {
url.searchParams.set(parameter.name, value);
}
}
}
}
Expand Down
28 changes: 26 additions & 2 deletions src/components/CURL.astro
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,42 @@ const props = z.object({
headers: z.record(z.string(), z.string()).default({}),
json: z.union([Record, z.array(Record)]).optional(),
form: Record.optional(),
query: z
.record(z.string(), z.union([z.string(), z.array(z.string())]))
.optional(),
code: z
.custom<Omit<ComponentProps<typeof Code>, "code" | "lang">>()
.optional(),
});

const { method, url, headers, json, form, code } = props.parse(Astro.props);
const {
method,
url: baseUrl,
headers,
json,
form,
query,
code,
} = props.parse(Astro.props);

if (json && form) {
throw new Error("[CURL] Cannot use both 'json' and 'form' properties.");
}

const lines = [`curl ${url}`, `\t--request ${method}`];
const url = new URL(baseUrl);
if (query) {
for (const [key, value] of Object.entries(query)) {
if (Array.isArray(value)) {
for (const v of value) {
url.searchParams.append(key, v);
}
} else {
url.searchParams.set(key, value);
}
}
}

const lines = [`curl "${url.toString()}"`, `\t--request ${method}`];

if (headers) {
for (const [key, value] of Object.entries(headers)) {
Expand Down
25 changes: 21 additions & 4 deletions src/content/docs/style-guide/components/api-request.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { APIRequest } from "~/components";
path="/zones/{zone_id}/api_gateway/settings/schema_validation"
method="PUT"
json={{
validation_default_mitigation_action: "block"
validation_default_mitigation_action: "block",
}}
/>

Expand All @@ -36,7 +36,7 @@ import { APIRequest } from "~/components";
method="POST"
form={{
requireSignedURLs: true,
metadata: '{"key":"value"}'
metadata: '{"key":"value"}',
}}
/>

Expand All @@ -54,6 +54,14 @@ import { APIRequest } from "~/components";
},
]}
/>

<APIRequest
path="/zones/{zone_id}/page_shield/scripts"
method="GET"
parameters={{
direction: "asc",
}}
/>
```

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

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

The path parameters to substitute.
The parameters to substitute - either in the URL path or as query parameters.

For example, `/zones/{zone_id}/page_shield/scripts` can be transformed into `/zones/123/page_shield/scripts?direction=asc` with the following:

```mdx
parameters={{
zone_id: "123",
direction: "asc"
}}
```

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

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

The FormData payload to send.

This field is not currently validated against the schema.
This field is not currently validated against the schema.
16 changes: 13 additions & 3 deletions src/content/docs/style-guide/components/curl.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ import { CURL } from "~/components";
json={{
key: "va'l'ue",
}}
query={{
foo: "bar",
bar: ["baz", "qux"],
}}
code={{
mark: "value"
mark: "value",
}}
/>

Expand All @@ -35,7 +39,7 @@ import { CURL } from "~/components";
key: "value",
}}
code={{
mark: "value"
mark: "value",
}}
/>
```
Expand Down Expand Up @@ -76,6 +80,12 @@ JSON data to include in the request.

The FormData payload to send.

### `query`

**type:** `Record<string, string>`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KianNH We should probably update the type here, since we now support Array values.


URL query parameters to append to the request URL.

### `code`

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

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