Skip to content

Commit 4b67afc

Browse files
authored
feat: add codegen to embedded request maker (stoplightio#1728)
* feat: add codegen to embedded request maker * chore: undo useEffect change * test: this is ugly but does it help timeout? * chore: address feedback * chore: angular typescript version * chore: add test
1 parent 5aa5885 commit 4b67afc

5 files changed

Lines changed: 30 additions & 6 deletions

File tree

examples/angular/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@
3838
"@angular/compiler-cli": "^12.0.4",
3939
"@angular/language-service": "^12.0.5",
4040
"angular-http-server": "^1.10.0",
41-
"typescript": "^4.2.4"
41+
"typescript": "4.2.4"
4242
}
4343
}

packages/elements-core/src/components/MarkdownViewer/CustomComponents/CodeComponent.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ export const CodeComponent: CustomComponentMapping['code'] = props => {
7070
return null;
7171
}
7272

73-
return <TryIt httpOperation={isHttpOperation(parsedValue) ? parsedValue : parseHttpRequest(parsedValue)} />;
73+
return (
74+
<TryIt httpOperation={isHttpOperation(parsedValue) ? parsedValue : parseHttpRequest(parsedValue)} embeddedInMd />
75+
);
7476
}
7577

7678
const DefaultCode = DefaultSMDComponents.code!;

packages/elements-core/src/components/RequestSamples/RequestSamples.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export interface RequestSamplesProps {
1313
* The HTTP request to generate code for.
1414
*/
1515
request: Request;
16+
/**
17+
* True when embedded in TryIt
18+
*/
19+
embeddedInMd?: boolean;
1620
}
1721

1822
const selectedLanguageAtom = persistAtom<string>('RequestSamples_selectedLanguage', atom('Shell'));
@@ -25,7 +29,7 @@ const fallbackText = 'Unable to generate code example';
2529
*
2630
* The programming language can be selected by the user and is remembered across instances and remounts.
2731
*/
28-
export const RequestSamples = React.memo<RequestSamplesProps>(({ request }) => {
32+
export const RequestSamples = React.memo<RequestSamplesProps>(({ request, embeddedInMd = false }) => {
2933
const [selectedLanguage, setSelectedLanguage] = useAtom(selectedLanguageAtom);
3034
const [selectedLibrary, setSelectedLibrary] = useAtom(selectedLibraryAtom);
3135

@@ -67,7 +71,7 @@ export const RequestSamples = React.memo<RequestSamplesProps>(({ request }) => {
6771
}, [selectedLanguage, selectedLibrary, setSelectedLanguage, setSelectedLibrary]);
6872

6973
return (
70-
<Panel rounded isCollapsible={false}>
74+
<Panel rounded={embeddedInMd ? undefined : true} isCollapsible={embeddedInMd}>
7175
<Panel.Titlebar rightComponent={<CopyButton size="sm" copyValue={requestSample || ''} />}>
7276
<Box ml={-2}>
7377
<Menu

packages/elements-core/src/components/TryIt/TryIt.spec.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ describe('TryIt', () => {
147147
expect(responseHeader).not.toBeInTheDocument();
148148
});
149149

150+
it('when embedded in markdown, shows request codegen', async () => {
151+
render(<TryItWithPersistence httpOperation={basicOperation} embeddedInMd />);
152+
153+
const requestSamplePanel = await screen.findByText('Request Sample: Shell / cURL');
154+
expect(requestSamplePanel).toBeVisible();
155+
});
156+
150157
describe('Credentials policy', () => {
151158
it('sets credentials correctly', async () => {
152159
render(<TryItWithPersistence httpOperation={basicOperation} tryItCredentialsPolicy="same-origin" />);

packages/elements-core/src/components/TryIt/TryIt.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as React from 'react';
1111
import { HttpCodeDescriptions, HttpMethodColors } from '../../constants';
1212
import { getHttpCodeColor } from '../../utils/http';
1313
import { getServersToDisplay } from '../../utils/http-spec/IServer';
14+
import { RequestSamples } from '../RequestSamples';
1415
import { TryItAuth } from './Auth/Auth';
1516
import { usePersistedSecuritySchemeWithValues } from './Auth/authentication-utils';
1617
import { FormDataBody } from './Body/FormDataBody';
@@ -42,6 +43,10 @@ export interface TryItProps {
4243
*/
4344
onRequestChange?: (currentRequest: HarRequest) => void;
4445
requestBodyIndex?: number;
46+
/**
47+
* True when TryIt is embedded in Markdown doc
48+
*/
49+
embeddedInMd?: boolean;
4550

4651
/**
4752
* Fetch credentials policy for TryIt component
@@ -73,12 +78,15 @@ export const TryIt: React.FC<TryItProps> = ({
7378
mockUrl,
7479
onRequestChange,
7580
requestBodyIndex,
81+
embeddedInMd = false,
7682
tryItCredentialsPolicy,
7783
corsProxy,
7884
}) => {
7985
const isDark = useThemeIsDark();
8086

8187
const [response, setResponse] = React.useState<ResponseState | ErrorState | undefined>();
88+
const [requestData, setRequestData] = React.useState<HarRequest | undefined>();
89+
8290
const [loading, setLoading] = React.useState<boolean>(false);
8391
const [validateParameters, setValidateParameters] = React.useState<boolean>(false);
8492

@@ -109,7 +117,7 @@ export const TryIt: React.FC<TryItProps> = ({
109117

110118
React.useEffect(() => {
111119
let isActive = true;
112-
if (onRequestChange) {
120+
if (onRequestChange || embeddedInMd) {
113121
buildHarRequest({
114122
mediaTypeContent,
115123
parameterValues: parameterValuesWithDefaults,
@@ -120,7 +128,8 @@ export const TryIt: React.FC<TryItProps> = ({
120128
chosenServer,
121129
corsProxy,
122130
}).then(request => {
123-
if (isActive) onRequestChange(request);
131+
if (onRequestChange && isActive) onRequestChange(request);
132+
if (embeddedInMd) setRequestData(request);
124133
});
125134
}
126135
return () => {
@@ -138,6 +147,7 @@ export const TryIt: React.FC<TryItProps> = ({
138147
mockingOptions,
139148
chosenServer,
140149
corsProxy,
150+
embeddedInMd,
141151
]);
142152

143153
const handleClick = async () => {
@@ -257,6 +267,7 @@ export const TryIt: React.FC<TryItProps> = ({
257267
)}
258268
</Panel.Content>
259269
</Panel>
270+
{requestData && embeddedInMd && <RequestSamples request={requestData} embeddedInMd />}
260271
{response && !('error' in response) && <TryItResponse response={response} />}
261272
{response && 'error' in response && <ResponseError state={response} />}
262273
</Box>

0 commit comments

Comments
 (0)