Skip to content

Commit 60e8cb4

Browse files
committed
feat: generate download link for specs defined by an object
closes #289
1 parent f792273 commit 60e8cb4

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

lib/components/ApiInfo/api-info.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<h1>{{info.title}} <span class="api-info-version">({{info.version}})</span></h1>
33
<p class="download-openapi" *ngIf="specUrl">
44
Download OpenAPI specification:
5-
<a class="openapi-button" download target="_blank" attr.href='{{specUrl}}'> Download </a>
5+
<a class="openapi-button" [attr.download]="downloadFilename" target="_blank" [attr.href]="specUrl"> Download </a>
66
</p>
77
<p>
88
<!-- TODO: create separate components for contact and license ? -->

lib/components/ApiInfo/api-info.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
import { Component, ChangeDetectionStrategy, OnInit, ElementRef } from '@angular/core';
3+
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
34
import { SpecManager, BaseComponent } from '../base';
45
import { OptionsService, Marker } from '../../services/index';
56

@@ -11,11 +12,13 @@ import { OptionsService, Marker } from '../../services/index';
1112
})
1213
export class ApiInfo extends BaseComponent implements OnInit {
1314
info: any = {};
14-
specUrl: String;
15+
specUrl: String | SafeResourceUrl;
16+
downloadFilename = '';
1517
constructor(specMgr: SpecManager,
1618
private optionsService: OptionsService,
1719
elRef: ElementRef,
18-
marker: Marker
20+
marker: Marker,
21+
private sanitizer: DomSanitizer
1922
) {
2023
super(specMgr);
2124
marker.addElement(elRef.nativeElement);
@@ -24,6 +27,12 @@ export class ApiInfo extends BaseComponent implements OnInit {
2427
init() {
2528
this.info = this.componentSchema.info;
2629
this.specUrl = this.specMgr.specUrl;
30+
if (!this.specUrl && window.Blob && window.URL) {
31+
const blob = new Blob([JSON.stringify(this.specMgr.rawSpec, null, 2)], {type : 'application/json'});
32+
this.specUrl = this.sanitizer.bypassSecurityTrustResourceUrl(window.URL.createObjectURL(blob));
33+
this.downloadFilename = 'swagger.json';
34+
}
35+
2736
if (!isNaN(parseInt(this.info.version.toString().substring(0, 1)))) {
2837
this.info.version = 'v' + this.info.version;
2938
}

lib/utils/spec-manager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface DescendantInfo {
2626
@Injectable()
2727
export class SpecManager {
2828
public _schema: any = {};
29+
public rawSpec: any;
2930
public apiUrl: string;
3031
public apiProtocol: string;
3132
public swagger: string;
@@ -48,6 +49,7 @@ export class SpecManager {
4849
if (typeof urlOrObject === 'string') {
4950
this.specUrl = urlOrObject;
5051
}
52+
this.rawSpec = schema;
5153
this._schema = snapshot(schema);
5254
try {
5355
this.init();

0 commit comments

Comments
 (0)