1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-01 01:18:10 +02:00

feat: allow ApiRequest component to create proxy requests

This commit is contained in:
Thomas Heartman 2022-04-13 15:15:08 +02:00
parent 93d2ef55b9
commit ecbae418d4
2 changed files with 55 additions and 27 deletions

View File

@ -53,3 +53,11 @@ DELETE.args = {
url: 'api/admin/projects/<project-id>/features/<feature-toggle-id>', url: 'api/admin/projects/<project-id>/features/<feature-toggle-id>',
title: 'Create a feature toggle with impression data enabled.', 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',
};

View File

@ -18,47 +18,67 @@ import CodeBlock from '@theme/CodeBlock';
const indentation = 2; const indentation = 2;
type Props = { type Props = {
verb: string, verb: string;
payload?: any, payload?: any;
url: string, url: string;
title?: 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 verbUpper = verb?.toUpperCase() || '';
const prettyPayload = JSON.stringify(payload, null, indentation); 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 ? ` const httpBlock = (
${verbUpper} <unleash-url>/${url} payload
Authorization: <API-token> ? `
${verbUpper} <${baseUrl}>/${url}
Authorization: <${authToken}>
content-type: application/json content-type: application/json
${prettyPayload}` :` ${prettyPayload}`
${verbUpper} <unleash-url>/${url} : `
Authorization: <API-token> ${verbUpper} <${baseUrl}>/${url}
content-type: application/json`).trim() Authorization: <${authToken}>
content-type: application/json`
).trim();
const curlBlock = (payload ? ` const curlBlock = (
payload
? `
curl -H "Content-Type: application/json" \\ curl -H "Content-Type: application/json" \\
-H "Authorization: <API-token>" \\ -H "Authorization: <${authToken}>" \\
-X ${verbUpper} \\ -X ${verbUpper} \\
-d '${prettyPayload}' \\ -d '${prettyPayload}' \\
<unleash-url>/${url}` : ` <${baseUrl}>/${url}`
: `
curl -H "Content-Type: application/json" \\ curl -H "Content-Type: application/json" \\
-H "Authorization: <API-token>" \\ -H "Authorization: <${authToken}>" \\
-X ${verbUpper} \\ -X ${verbUpper} \\
<unleash-url>/${url}` ).trim() <${baseUrl}>/${url}`
).trim();
const httpieBlock = (payload ? const httpieBlock = (
`echo '${prettyPayload}' \\ payload
? `echo '${prettyPayload}' \\
| http ${verbUpper} \\ | http ${verbUpper} \\
<unleash-url>/${url} \\ <${baseUrl}>/${url} \\
Authorization:<API-token>` Authorization:<${authToken}>`
: ` : `
http ${verbUpper} \\ http ${verbUpper} \\
<unleash-url>/${url} \\ <${baseUrl}>/${url} \\
Authorization:<API-token>`.trim() Authorization:<${authToken}>`.trim()
).trim() ).trim();
return ( return (
<Tabs groupId="api-request"> <Tabs groupId="api-request">
@ -69,7 +89,7 @@ http ${verbUpper} \\
</TabItem> </TabItem>
<TabItem value="curl" label="cURL"> <TabItem value="curl" label="cURL">
<CodeBlock language="bash" title={title}> <CodeBlock language="bash" title={title}>
{curlBlock} {curlBlock}
</CodeBlock> </CodeBlock>
</TabItem> </TabItem>
<TabItem value="httpie" label="HTTPie"> <TabItem value="httpie" label="HTTPie">