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> <h1>{{info.title}} <span class="api-info-version">({{info.version}})</span></h1>
<p class="download-openapi" *ngIf="specUrl"> <p class="download-openapi" *ngIf="specUrl">
Download OpenAPI specification: 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>
<p> <p>
<!-- TODO: create separate components for contact and license ? --> <!-- TODO: create separate components for contact and license ? -->

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
import { Component, ChangeDetectionStrategy, OnInit, ElementRef } from '@angular/core'; import { Component, ChangeDetectionStrategy, OnInit, ElementRef } from '@angular/core';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { SpecManager, BaseComponent } from '../base'; import { SpecManager, BaseComponent } from '../base';
import { OptionsService, Marker } from '../../services/index'; import { OptionsService, Marker } from '../../services/index';
@ -11,11 +12,13 @@ import { OptionsService, Marker } from '../../services/index';
}) })
export class ApiInfo extends BaseComponent implements OnInit { export class ApiInfo extends BaseComponent implements OnInit {
info: any = {}; info: any = {};
specUrl: String; specUrl: String | SafeResourceUrl;
downloadFilename = '';
constructor(specMgr: SpecManager, constructor(specMgr: SpecManager,
private optionsService: OptionsService, private optionsService: OptionsService,
elRef: ElementRef, elRef: ElementRef,
marker: Marker marker: Marker,
private sanitizer: DomSanitizer
) { ) {
super(specMgr); super(specMgr);
marker.addElement(elRef.nativeElement); marker.addElement(elRef.nativeElement);
@ -24,6 +27,12 @@ export class ApiInfo extends BaseComponent implements OnInit {
init() { init() {
this.info = this.componentSchema.info; this.info = this.componentSchema.info;
this.specUrl = this.specMgr.specUrl; 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)))) { if (!isNaN(parseInt(this.info.version.toString().substring(0, 1)))) {
this.info.version = 'v' + this.info.version; this.info.version = 'v' + this.info.version;
} }

View File

@ -26,6 +26,7 @@ export interface DescendantInfo {
@Injectable() @Injectable()
export class SpecManager { export class SpecManager {
public _schema: any = {}; public _schema: any = {};
public rawSpec: any;
public apiUrl: string; public apiUrl: string;
public apiProtocol: string; public apiProtocol: string;
public swagger: string; public swagger: string;
@ -48,6 +49,7 @@ export class SpecManager {
if (typeof urlOrObject === 'string') { if (typeof urlOrObject === 'string') {
this.specUrl = urlOrObject; this.specUrl = urlOrObject;
} }
this.rawSpec = schema;
this._schema = snapshot(schema); this._schema = snapshot(schema);
try { try {
this.init(); this.init();