Skip to content

Commit

Permalink
add expand-responses option
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Dec 14, 2016
1 parent 03e779c commit 95f2da8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ ReDoc makes use of the following [vendor extensions](http://swagger.io/specifica
* `suppress-warnings` - if set, warnings are not rendered at the top of documentation (they still are logged to the console).
* `lazy-rendering` - if set, enables lazy rendering mode in ReDoc. This mode is useful for APIs with big number of operations (e.g. > 50). In this mode ReDoc shows initial screen ASAP and then renders the rest operations asynchronously while showing progress bar on the top. Check out the [demo](\\rebilly.github.io/ReDoc) for the example.
* `hide-hostname` - if set, the protocol and hostname is not shown in the method definition.
* `expand-responses` - specify which responses to expand by default by response codes. Values should be passed as comma-separated list without spaces e.g. `expand-responses="200,201"`. Special value `"all"` expands all responses by default. Be careful: this option can slow-down documentation rendering time.

## Advanced usage
Instead of adding `spec-url` attribute to the `<redoc>` element you can initialize ReDoc via globally exposed `Redoc` object:
Expand Down
4 changes: 2 additions & 2 deletions lib/components/ResponsesList/responses-list.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h2 class="responses-list-header" *ngIf="responses.length"> Responses </h2>
<zippy *ngFor="let response of responses;trackBy:trackByCode" [title]="response.code + ' ' + response.description | marked"
[type]="response.type" [empty]="response.empty" (open)="lazySchema.load()">
[type]="response.type" [visible]="response.expanded" [empty]="response.empty" (open)="lazySchema.load()">
<div *ngIf="response.headers" class="response-headers">
<header>
Headers
Expand All @@ -20,6 +20,6 @@ <h2 class="responses-list-header" *ngIf="responses.length"> Responses </h2>
<header *ngIf="response.schema">
Response Schema
</header>
<json-schema-lazy #lazySchema pointer="{{response.schema ? response.pointer + '/schema' : null}}">
<json-schema-lazy [auto]="response.expanded" #lazySchema pointer="{{response.schema ? response.pointer + '/schema' : null}}">
</json-schema-lazy>
</zippy>
7 changes: 7 additions & 0 deletions lib/components/ResponsesList/responses-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ export class ResponsesList extends BaseComponent implements OnInit {
resp.empty = !resp.schema;
resp.code = respCode;
resp.type = statusCodeType(resp.code);

if (this.options.expandResponses) {
if (this.options.expandResponses === 'all' || this.options.expandResponses.has(respCode.toString())) {
resp.expanded = true;
}
}

if (resp.headers && !(resp.headers instanceof Array)) {
resp.headers = Object.keys(resp.headers).map((k) => {
let respInfo = resp.headers[k];
Expand Down
17 changes: 16 additions & 1 deletion lib/services/options.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe('Options Service', () => {
}

afterEach(() => {
document.body.removeChild(tmpDiv);
if (tmpDiv) document.body.removeChild(tmpDiv);
tmpDiv = false;
});

beforeEach(() => {
Expand Down Expand Up @@ -47,4 +48,18 @@ describe('Options Service', () => {
optionsService.parseOptions(elem);
optionsService.options.scrollYOffset().should.be.equal(123);
});

it('should convert expandResponses options to Set', () => {
optionsService.options = { expandResponses: '200,300' };
optionsService._normalizeOptions();
optionsService.options.expandResponses.should.be.instanceof(Set);
Array.from(optionsService.options.expandResponses.values()).should.deepEqual(['200', '300']);
});

it('should preserve special value "all" as string', () => {
optionsService.options = { expandResponses: 'all' };
optionsService._normalizeOptions();
optionsService.options.expandResponses.should.be.of.type('string');
optionsService.options.expandResponses.should.be.equal('all');
});
});
11 changes: 9 additions & 2 deletions lib/services/options.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const OPTION_NAMES = new Set([
'specUrl',
'suppressWarnings',
'hideHostname',
'lazyRendering'
'lazyRendering',
'expandResponses'
]);

interface Options {
Expand All @@ -24,6 +25,7 @@ interface Options {
suppressWarnings?: boolean;
hideHostname?: boolean;
lazyRendering?: boolean;
expandResponses?: Set<string> | 'all';
$scrollParent?: HTMLElement | Window;
}

Expand All @@ -36,7 +38,7 @@ export class OptionsService {
this._normalizeOptions();
}

get options():Options {
get options(): Options {
return this._options;
}

Expand Down Expand Up @@ -89,5 +91,10 @@ export class OptionsService {
if (isString(this._options.suppressWarnings)) this._options.suppressWarnings = true;
if (isString(this._options.hideHostname)) this._options.hideHostname = true;
if (isString(this._options.lazyRendering)) this._options.lazyRendering = true;
if (isString(this._options.expandResponses)) {
let str = this._options.expandResponses as string;
if (str === 'all') return;
this._options.expandResponses = new Set(str.split(','));
}
}
}

0 comments on commit 95f2da8

Please sign in to comment.