@@ -59,6 +59,12 @@ import { TRANSPORTS } from '../../lib/transports';
5959import { SelectOption } from '../../lib/types' ;
6060import DebugInputs from './DebugInputs' ;
6161
62+ const stringifyHeaders = ( headers : [ string , string [ ] ] [ ] ) : string =>
63+ JSON . stringify (
64+ Object . fromEntries ( headers . map ( ( [ key , value ] ) => [ key , value . join ( ', ' ) ] ) ) ,
65+ null ,
66+ 2 ,
67+ ) ;
6268const useStyles = makeStyles ( ( theme : Theme ) =>
6369 createStyles ( {
6470 actionDialog : {
@@ -149,6 +155,9 @@ const DebugPage: React.FunctionComponent<Props> = ({
149155 const [ requestBody , setRequestBody ] = useState ( '' ) ;
150156 const [ debugResponse , setDebugResponse ] = useState ( '' ) ;
151157 const [ additionalQueries , setAdditionalQueries ] = useState ( '' ) ;
158+ const [ debugResponseHeaders , setDebugResponseHeaders ] = useState <
159+ [ string , string [ ] ] [ ]
160+ > ( [ ] ) ;
152161 const [ additionalPath , setAdditionalPath ] = useState ( '' ) ;
153162 const [ additionalHeaders , setAdditionalHeaders ] = useState ( '' ) ;
154163 const [ stickyHeaders , toggleStickyHeaders ] = useReducer ( toggle , false ) ;
@@ -159,13 +168,34 @@ const DebugPage: React.FunctionComponent<Props> = ({
159168 false ,
160169 ) ;
161170
171+ const [ currentApiId , setCurrentApiId ] = useState < string > ( method . id ) ;
172+ const [ responseCache , setResponseCache ] = useState <
173+ Record < string , { body : string ; headers : Map < string , string [ ] > } >
174+ > ( { } ) ;
175+
162176 const classes = useStyles ( ) ;
163177
164178 const transport = TRANSPORTS . getDebugTransport ( method ) ;
165179 if ( ! transport ) {
166180 throw new Error ( "This method doesn't have a debug transport." ) ;
167181 }
168182
183+ useEffect ( ( ) => {
184+ const apiId = method . id ;
185+ if ( apiId !== currentApiId ) {
186+ setCurrentApiId ( apiId ) ;
187+ if ( responseCache [ apiId ] ) {
188+ setDebugResponse ( responseCache [ apiId ] . body ) ;
189+ setDebugResponseHeaders (
190+ Array . from ( responseCache [ apiId ] . headers . entries ( ) ) ,
191+ ) ;
192+ } else {
193+ setDebugResponse ( '' ) ;
194+ setDebugResponseHeaders ( [ ] ) ;
195+ }
196+ }
197+ } , [ method , currentApiId , responseCache ] ) ;
198+
169199 useEffect ( ( ) => {
170200 const urlParams = new URLSearchParams ( location . search ) ;
171201
@@ -202,6 +232,7 @@ const DebugPage: React.FunctionComponent<Props> = ({
202232
203233 if ( ! keepDebugResponse ) {
204234 setDebugResponse ( '' ) ;
235+ setDebugResponseHeaders ( [ ] ) ;
205236 toggleKeepDebugResponse ( false ) ;
206237 }
207238 setSnackbarOpen ( false ) ;
@@ -364,6 +395,7 @@ const DebugPage: React.FunctionComponent<Props> = ({
364395
365396 const onClear = useCallback ( ( ) => {
366397 setDebugResponse ( '' ) ;
398+ setDebugResponseHeaders ( [ ] ) ;
367399 } , [ ] ) ;
368400
369401 const executeRequest = useCallback (
@@ -390,24 +422,29 @@ const DebugPage: React.FunctionComponent<Props> = ({
390422 const headersText = params . get ( 'headers' ) ;
391423 const headers = headersText ? JSON . parse ( headersText ) : { } ;
392424
393- let executedDebugResponse ;
394425 try {
395- executedDebugResponse = await transport . send (
426+ const { body , headers : responseHeaders } = await transport . send (
396427 method ,
397428 headers ,
398429 parseServerRootPath ( docServiceRoute ) ,
399430 executedRequestBody ,
400431 executedEndpointPath ,
401432 queries ,
402433 ) ;
434+ setDebugResponse ( body ) ;
435+ setDebugResponseHeaders ( Array . from ( responseHeaders . entries ( ) ) ) ;
436+ setResponseCache ( ( prev ) => ( {
437+ ...prev ,
438+ [ currentApiId ] : {
439+ body,
440+ headers : responseHeaders ,
441+ } ,
442+ } ) ) ;
403443 } catch ( e ) {
404- if ( e instanceof Object ) {
405- executedDebugResponse = e . toString ( ) ;
406- } else {
407- executedDebugResponse = '<unknown>' ;
408- }
444+ const message = e instanceof Object ? e . toString ( ) : '<unknown>' ;
445+ setDebugResponse ( message ) ;
446+ setDebugResponseHeaders ( [ ] ) ;
409447 }
410- setDebugResponse ( executedDebugResponse ) ;
411448 } ,
412449 [
413450 useRequestBody ,
@@ -416,6 +453,7 @@ const DebugPage: React.FunctionComponent<Props> = ({
416453 method ,
417454 transport ,
418455 docServiceRoute ,
456+ currentApiId ,
419457 ] ,
420458 ) ;
421459
@@ -572,7 +610,7 @@ const DebugPage: React.FunctionComponent<Props> = ({
572610 < Grid item xs = { 12 } sm = { 6 } >
573611 < Grid container spacing = { 1 } >
574612 < Grid item xs = "auto" >
575- < Tooltip title = "Copy response" >
613+ < Tooltip title = "Copy response body " >
576614 < div >
577615 < IconButton
578616 onClick = { onCopy }
@@ -596,13 +634,37 @@ const DebugPage: React.FunctionComponent<Props> = ({
596634 </ Tooltip >
597635 </ Grid >
598636 </ Grid >
599- < SyntaxHighlighter
600- language = "json"
601- style = { githubGist }
602- wrapLines = { false }
603- >
604- { debugResponse }
605- </ SyntaxHighlighter >
637+ { debugResponse && (
638+ < >
639+ { Object . keys ( debugResponseHeaders ) . length > 0 && (
640+ < >
641+ < Typography
642+ variant = "subtitle1"
643+ style = { { marginTop : '1rem' } }
644+ >
645+ Response Headers:
646+ </ Typography >
647+ < SyntaxHighlighter
648+ language = "json"
649+ style = { githubGist }
650+ wrapLines = { false }
651+ >
652+ { stringifyHeaders ( debugResponseHeaders ) }
653+ </ SyntaxHighlighter >
654+ </ >
655+ ) }
656+ < Typography variant = "subtitle1" style = { { marginTop : '1rem' } } >
657+ Response Body:
658+ </ Typography >
659+ < SyntaxHighlighter
660+ language = "json"
661+ style = { githubGist }
662+ wrapLines = { false }
663+ >
664+ { debugResponse }
665+ </ SyntaxHighlighter >
666+ </ >
667+ ) }
606668 </ Grid >
607669 </ Grid >
608670 < Snackbar
@@ -684,6 +746,26 @@ const DebugPage: React.FunctionComponent<Props> = ({
684746 </ Tooltip >
685747 </ Grid >
686748 </ Grid >
749+ { Object . keys ( debugResponseHeaders ) . length > 0 && (
750+ < >
751+ < Typography
752+ variant = "subtitle1"
753+ style = { { marginTop : '1rem' } }
754+ >
755+ Response Headers:
756+ </ Typography >
757+ < SyntaxHighlighter
758+ language = "json"
759+ style = { githubGist }
760+ wrapLines = { false }
761+ >
762+ { stringifyHeaders ( debugResponseHeaders ) }
763+ </ SyntaxHighlighter >
764+ </ >
765+ ) }
766+ < Typography variant = "subtitle1" style = { { marginTop : '1rem' } } >
767+ Response Body:
768+ </ Typography >
687769 < SyntaxHighlighter
688770 language = "json"
689771 style = { githubGist }
0 commit comments