feat: generate download link for specs defined by an object

closes #289
This commit is contained in:
Roman Hotsiy 2017-08-17 11:42:12 +03:00
parent f792273be8
commit 60e8cb437a
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
3 changed files with 14 additions and 3 deletions

View File

@ -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 ? -->

View File

@ -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';
@ -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);
@ -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;
}

View File

@ -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;
@ -48,6 +49,7 @@ export class SpecManager {
if (typeof urlOrObject === 'string') {
this.specUrl = urlOrObject;
}
this.rawSpec = schema;
this._schema = snapshot(schema);
try {
this.init();