mirror of
				https://github.com/Redocly/redoc.git
				synced 2025-10-31 15:57:30 +03:00 
			
		
		
		
	chore: refactor request samples
This commit is contained in:
		
							parent
							
								
									cbb9f50fa0
								
							
						
					
					
						commit
						448b1b48c8
					
				|  | @ -1,6 +1,6 @@ | ||||||
| import { observer } from 'mobx-react'; | import { observer } from 'mobx-react'; | ||||||
| import * as React from 'react'; | import * as React from 'react'; | ||||||
| import { OperationModel, RedocNormalizedOptions } from '../../services'; | import { isPayloadSample, OperationModel, RedocNormalizedOptions } from '../../services'; | ||||||
| import { PayloadSamples } from '../PayloadSamples/PayloadSamples'; | import { PayloadSamples } from '../PayloadSamples/PayloadSamples'; | ||||||
| import { SourceCodeWithCopy } from '../SourceCode/SourceCode'; | import { SourceCodeWithCopy } from '../SourceCode/SourceCode'; | ||||||
| 
 | 
 | ||||||
|  | @ -19,15 +19,10 @@ export class RequestSamples extends React.Component<RequestSamplesProps> { | ||||||
| 
 | 
 | ||||||
|   render() { |   render() { | ||||||
|     const { operation } = this.props; |     const { operation } = this.props; | ||||||
|     const requestBodyContent = operation.requestBody && operation.requestBody.content; |  | ||||||
|     const hasBodySample = requestBodyContent && requestBodyContent.hasSample; |  | ||||||
|     const samples = operation.codeSamples; |     const samples = operation.codeSamples; | ||||||
| 
 | 
 | ||||||
|     const hasSamples = hasBodySample || samples.length > 0; |     const hasSamples = samples.length > 0; | ||||||
|     const hideTabList = |     const hideTabList = samples.length === 1 ? this.context.hideSingleRequestSampleTab : false; | ||||||
|       samples.length + (hasBodySample ? 1 : 0) === 1 |  | ||||||
|         ? this.context.hideSingleRequestSampleTab |  | ||||||
|         : false; |  | ||||||
|     return ( |     return ( | ||||||
|       (hasSamples && ( |       (hasSamples && ( | ||||||
|         <div> |         <div> | ||||||
|  | @ -35,23 +30,21 @@ export class RequestSamples extends React.Component<RequestSamplesProps> { | ||||||
| 
 | 
 | ||||||
|           <Tabs defaultIndex={0}> |           <Tabs defaultIndex={0}> | ||||||
|             <TabList hidden={hideTabList}> |             <TabList hidden={hideTabList}> | ||||||
|               {hasBodySample && <Tab key="payload"> Payload </Tab>} |  | ||||||
|               {samples.map(sample => ( |               {samples.map(sample => ( | ||||||
|                 <Tab key={sample.lang + '_' + (sample.label || '')}> |                 <Tab key={sample.lang + '_' + (sample.label || '')}> | ||||||
|                   {sample.label !== undefined ? sample.label : sample.lang} |                   {sample.label !== undefined ? sample.label : sample.lang} | ||||||
|                 </Tab> |                 </Tab> | ||||||
|               ))} |               ))} | ||||||
|             </TabList> |             </TabList> | ||||||
|             {hasBodySample && ( |  | ||||||
|               <TabPanel key="payload"> |  | ||||||
|                 <div> |  | ||||||
|                   <PayloadSamples content={requestBodyContent!} /> |  | ||||||
|                 </div> |  | ||||||
|               </TabPanel> |  | ||||||
|             )} |  | ||||||
|             {samples.map(sample => ( |             {samples.map(sample => ( | ||||||
|               <TabPanel key={sample.lang}> |               <TabPanel key={sample.lang + '_' + (sample.label || '')}> | ||||||
|                 <SourceCodeWithCopy lang={sample.lang} source={sample.source} /> |                 {isPayloadSample(sample) ? ( | ||||||
|  |                   <div> | ||||||
|  |                     <PayloadSamples content={sample.requestBodyContent} /> | ||||||
|  |                   </div> | ||||||
|  |                 ) : ( | ||||||
|  |                   <SourceCodeWithCopy lang={sample.lang} source={sample.source} /> | ||||||
|  |                 )} | ||||||
|               </TabPanel> |               </TabPanel> | ||||||
|             ))} |             ))} | ||||||
|           </Tabs> |           </Tabs> | ||||||
|  |  | ||||||
|  | @ -27,9 +27,23 @@ import { ContentItemModel, ExtendedOpenAPIOperation } from '../MenuBuilder'; | ||||||
| import { OpenAPIParser } from '../OpenAPIParser'; | import { OpenAPIParser } from '../OpenAPIParser'; | ||||||
| import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; | import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; | ||||||
| import { FieldModel } from './Field'; | import { FieldModel } from './Field'; | ||||||
|  | import { MediaContentModel } from './MediaContent'; | ||||||
| import { RequestBodyModel } from './RequestBody'; | import { RequestBodyModel } from './RequestBody'; | ||||||
| import { ResponseModel } from './Response'; | import { ResponseModel } from './Response'; | ||||||
| 
 | 
 | ||||||
|  | interface XPayloadSample { | ||||||
|  |   lang: 'payload'; | ||||||
|  |   label: string; | ||||||
|  |   requestBodyContent: MediaContentModel; | ||||||
|  |   source: string; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export function isPayloadSample( | ||||||
|  |   sample: XPayloadSample | OpenAPIXCodeSample, | ||||||
|  | ): sample is XPayloadSample { | ||||||
|  |   return sample.lang === 'payload' && (sample as any).requestBodyContent; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * Operation model ready to be used by components |  * Operation model ready to be used by components | ||||||
|  */ |  */ | ||||||
|  | @ -62,7 +76,7 @@ export class OperationModel implements IMenuItem { | ||||||
|   path: string; |   path: string; | ||||||
|   servers: OpenAPIServer[]; |   servers: OpenAPIServer[]; | ||||||
|   security: SecurityRequirementModel[]; |   security: SecurityRequirementModel[]; | ||||||
|   codeSamples: OpenAPIXCodeSample[]; |   codeSamples: Array<OpenAPIXCodeSample | XPayloadSample>; | ||||||
|   extensions: Dict<any>; |   extensions: Dict<any>; | ||||||
| 
 | 
 | ||||||
|   constructor( |   constructor( | ||||||
|  | @ -89,8 +103,21 @@ export class OperationModel implements IMenuItem { | ||||||
|     this.httpVerb = operationSpec.httpVerb; |     this.httpVerb = operationSpec.httpVerb; | ||||||
|     this.deprecated = !!operationSpec.deprecated; |     this.deprecated = !!operationSpec.deprecated; | ||||||
|     this.operationId = operationSpec.operationId; |     this.operationId = operationSpec.operationId; | ||||||
|     this.codeSamples = operationSpec['x-code-samples'] || []; |  | ||||||
|     this.path = operationSpec.pathName; |     this.path = operationSpec.pathName; | ||||||
|  |     this.codeSamples = operationSpec['x-code-samples'] || []; | ||||||
|  | 
 | ||||||
|  |     const requestBodyContent = this.requestBody && this.requestBody.content; | ||||||
|  |     if (requestBodyContent && requestBodyContent.hasSample) { | ||||||
|  |       this.codeSamples = [ | ||||||
|  |         { | ||||||
|  |           lang: 'payload', | ||||||
|  |           label: 'Payload', | ||||||
|  |           source: '', | ||||||
|  |           requestBodyContent, | ||||||
|  |         }, | ||||||
|  |         ...this.codeSamples, | ||||||
|  |       ]; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     const pathInfo = parser.byRef<OpenAPIPath>( |     const pathInfo = parser.byRef<OpenAPIPath>( | ||||||
|       JsonPointer.compile(['paths', operationSpec.pathName]), |       JsonPointer.compile(['paths', operationSpec.pathName]), | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user