mirror of
				https://github.com/Redocly/redoc.git
				synced 2025-10-31 15:57:30 +03:00 
			
		
		
		
	feat: generate download link for specs defined by an object
closes #289
This commit is contained in:
		
							parent
							
								
									f792273be8
								
							
						
					
					
						commit
						60e8cb437a
					
				|  | @ -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 ? --> | ||||
|  |  | |||
|  | @ -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; | ||||
|     } | ||||
|  |  | |||
|  | @ -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(); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user