Skip to content

Commit

Permalink
feat: allow ApiRequest component to create proxy requests
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheartman committed Apr 13, 2022
1 parent 93d2ef5 commit ecbae41
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
8 changes: 8 additions & 0 deletions website/src/components/ApiRequest/ApiRequest.stories.jsx
Expand Up @@ -53,3 +53,11 @@ DELETE.args = {
url: 'api/admin/projects/<project-id>/features/<feature-toggle-id>',
title: 'Create a feature toggle with impression data enabled.',
};

export const GETProxy = Template.bind({});
GETProxy.args = {
verb: 'get',
url: 'proxy',
title: 'Request toggles from the Unleash Proxy',
endpointType: 'Proxy API',
};
74 changes: 47 additions & 27 deletions website/src/components/ApiRequest/index.tsx
Expand Up @@ -18,47 +18,67 @@ import CodeBlock from '@theme/CodeBlock';
const indentation = 2;

type Props = {
verb: string,
payload?: any,
url: string,
title?: string
}
verb: string;
payload?: any;
url: string;
title?: string;
endpointType?: 'Proxy API' | 'Unleash server API';
};

const Component: React.FC<Props> = ({ verb, payload, url, title }) => {
const Component: React.FC<Props> = ({
verb,
payload,
url,
title,
endpointType = 'Unleash server API',
}) => {
const verbUpper = verb?.toUpperCase() || '';
const prettyPayload = JSON.stringify(payload, null, indentation);
const [baseUrl, authToken] =
endpointType === 'Unleash server API'
? ['unleash-url', 'API-token']
: ['proxy-url', 'proxy-client-key'];

const httpBlock = (payload ? `
${verbUpper} <unleash-url>/${url}
Authorization: <API-token>
const httpBlock = (
payload
? `
${verbUpper} <${baseUrl}>/${url}
Authorization: <${authToken}>
content-type: application/json
${prettyPayload}` :`
${verbUpper} <unleash-url>/${url}
Authorization: <API-token>
content-type: application/json`).trim()
${prettyPayload}`
: `
${verbUpper} <${baseUrl}>/${url}
Authorization: <${authToken}>
content-type: application/json`
).trim();

const curlBlock = (payload ? `
const curlBlock = (
payload
? `
curl -H "Content-Type: application/json" \\
-H "Authorization: <API-token>" \\
-H "Authorization: <${authToken}>" \\
-X ${verbUpper} \\
-d '${prettyPayload}' \\
<unleash-url>/${url}` : `
<${baseUrl}>/${url}`
: `
curl -H "Content-Type: application/json" \\
-H "Authorization: <API-token>" \\
-H "Authorization: <${authToken}>" \\
-X ${verbUpper} \\
<unleash-url>/${url}` ).trim()
<${baseUrl}>/${url}`
).trim();

const httpieBlock = (payload ?
`echo '${prettyPayload}' \\
const httpieBlock = (
payload
? `echo '${prettyPayload}' \\
| http ${verbUpper} \\
<unleash-url>/${url} \\
Authorization:<API-token>`
: `
<${baseUrl}>/${url} \\
Authorization:<${authToken}>`
: `
http ${verbUpper} \\
<unleash-url>/${url} \\
Authorization:<API-token>`.trim()
).trim()
<${baseUrl}>/${url} \\
Authorization:<${authToken}>`.trim()
).trim();

return (
<Tabs groupId="api-request">
Expand All @@ -69,7 +89,7 @@ http ${verbUpper} \\
</TabItem>
<TabItem value="curl" label="cURL">
<CodeBlock language="bash" title={title}>
{curlBlock}
{curlBlock}
</CodeBlock>
</TabItem>
<TabItem value="httpie" label="HTTPie">
Expand Down

0 comments on commit ecbae41

Please sign in to comment.