diff --git a/lib/components/ApiInfo/api-info.html b/lib/components/ApiInfo/api-info.html index c853aaca..55f0a4e9 100644 --- a/lib/components/ApiInfo/api-info.html +++ b/lib/components/ApiInfo/api-info.html @@ -2,7 +2,7 @@

{{info.title}} ({{info.version}})

Download OpenAPI specification: - Download + Download

diff --git a/lib/components/ApiInfo/api-info.ts b/lib/components/ApiInfo/api-info.ts index d04423c4..1f6240c8 100644 --- a/lib/components/ApiInfo/api-info.ts +++ b/lib/components/ApiInfo/api-info.ts @@ -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; } diff --git a/lib/utils/spec-manager.ts b/lib/utils/spec-manager.ts index 3c3ce3ee..6a399da4 100644 --- a/lib/utils/spec-manager.ts +++ b/lib/utils/spec-manager.ts @@ -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();