feat: support text-plain response sample

closes #270
This commit is contained in:
Roman Hotsiy 2017-08-17 11:23:12 +03:00
parent 955ef7101d
commit b84177c157
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
5 changed files with 30 additions and 7 deletions

View File

@ -619,6 +619,7 @@ paths:
examples:
application/json: OK
application/xml: <message> OK </message>
text/plain: OK
headers:
X-Rate-Limit:
type: integer

View File

@ -20,7 +20,15 @@
<div class="action-buttons">
<span copy-button [copyText]="xmlSample" class="hint--top-left hint--inversed"> <a>Copy</a> </span>
</div>
<pre class="xml-sample" [innerHtml]="xmlSample | prism:'xml'"></pre>
<pre class="response-sample" [innerHtml]="xmlSample | prism:'xml'"></pre>
</div>
</tab>
<tab tabTitle="text/plain" *ngIf="textSample">
<div class="snippet">
<div class="action-buttons">
<span copy-button [copyText]="xmlSample" class="hint--top-left hint--inversed"> <a>Copy</a> </span>
</div>
<pre class="response-sample">{{textSample}}</pre>
</div>
</tab>
</tabs>

View File

@ -131,7 +131,7 @@ pre {
padding-left: 6px;
}
.redoc-json, .xml-sample {
.redoc-json, .response-sample {
overflow-x: auto;
padding: 20px;
border-radius: $border-radius*2;

View File

@ -6,7 +6,7 @@ import * as OpenAPISampler from 'openapi-sampler';
import JsonPointer from '../../utils/JsonPointer';
import { BaseComponent, SpecManager } from '../base';
import { SchemaNormalizer } from '../../services/schema-normalizer.service';
import { getJsonLikeSample, getXmlLikeSample} from '../../utils/helpers';
import { getJsonLikeSample, getXmlLikeSample, getTextLikeSample } from '../../utils/helpers';
@Component({
selector: 'schema-sample',
@ -21,6 +21,7 @@ export class SchemaSample extends BaseComponent implements OnInit {
element: any;
sample: any;
xmlSample: string;
textSample: string;
enableButtons: boolean = false;
private _normalizer:SchemaNormalizer;
@ -51,10 +52,8 @@ export class SchemaSample extends BaseComponent implements OnInit {
base.examples = requestExamples;
}
let xmlLikeSample = base.examples && getXmlLikeSample(base.examples);
if (xmlLikeSample) {
this.xmlSample = xmlLikeSample;
}
this.xmlSample = base.examples && getXmlLikeSample(base.examples);
this.textSample = base.examples && getTextLikeSample(base.examples);
let jsonLikeSample = base.examples && getJsonLikeSample(base.examples);
if (jsonLikeSample) {

View File

@ -142,6 +142,10 @@ export function isXmlLike(contentType: string): boolean {
return contentType.search(/xml/i) !== -1;
}
export function isTextLike(contentType: string): boolean {
return contentType.search(/text\/plain/i) !== -1;
}
export function getJsonLikeSample(samples: Object = {}) {
const jsonLikeKeys = Object.keys(samples).filter(isJsonLike);
@ -161,3 +165,14 @@ export function getXmlLikeSample(samples: Object = {}) {
return samples[xmlLikeKeys[0]];
}
export function getTextLikeSample(samples: Object = {}) {
const textLikeKeys = Object.keys(samples).filter(isTextLike);
if (!textLikeKeys.length) {
return false;
}
return samples[textLikeKeys[0]];
}