Skip to content

Commit

Permalink
feat: generate download link for specs defined by an object
Browse files Browse the repository at this point in the history
closes #289
  • Loading branch information
RomanHotsiy committed Aug 17, 2017
1 parent f792273 commit 60e8cb4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/components/ApiInfo/api-info.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1>{{info.title}} <span class="api-info-version">({{info.version}})</span></h1>
<p class="download-openapi" *ngIf="specUrl">
Download OpenAPI specification:
<a class="openapi-button" download target="_blank" attr.href='{{specUrl}}'> Download </a>
<a class="openapi-button" [attr.download]="downloadFilename" target="_blank" [attr.href]="specUrl"> Download </a>
</p>
<p>
<!-- TODO: create separate components for contact and license ? -->
Expand Down
13 changes: 11 additions & 2 deletions lib/components/ApiInfo/api-info.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
import { Component, ChangeDetectionStrategy, OnInit, ElementRef } from '@angular/core';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { SpecManager, BaseComponent } from '../base';
import { OptionsService, Marker } from '../../services/index';

Expand All @@ -11,11 +12,13 @@ import { OptionsService, Marker } from '../../services/index';
})
export class ApiInfo extends BaseComponent implements OnInit {
info: any = {};
specUrl: String;
specUrl: String | SafeResourceUrl;
downloadFilename = '';
constructor(specMgr: SpecManager,
private optionsService: OptionsService,
elRef: ElementRef,
marker: Marker
marker: Marker,
private sanitizer: DomSanitizer
) {
super(specMgr);
marker.addElement(elRef.nativeElement);
Expand All @@ -24,6 +27,12 @@ export class ApiInfo extends BaseComponent implements OnInit {
init() {
this.info = this.componentSchema.info;
this.specUrl = this.specMgr.specUrl;
if (!this.specUrl && window.Blob && window.URL) {
const blob = new Blob([JSON.stringify(this.specMgr.rawSpec, null, 2)], {type : 'application/json'});
this.specUrl = this.sanitizer.bypassSecurityTrustResourceUrl(window.URL.createObjectURL(blob));
this.downloadFilename = 'swagger.json';
}

if (!isNaN(parseInt(this.info.version.toString().substring(0, 1)))) {
this.info.version = 'v' + this.info.version;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/spec-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface DescendantInfo {
@Injectable()
export class SpecManager {
public _schema: any = {};
public rawSpec: any;
public apiUrl: string;
public apiProtocol: string;
public swagger: string;
Expand All @@ -48,6 +49,7 @@ export class SpecManager {
if (typeof urlOrObject === 'string') {
this.specUrl = urlOrObject;
}
this.rawSpec = schema;
this._schema = snapshot(schema);
try {
this.init();
Expand Down

0 comments on commit 60e8cb4

Please sign in to comment.