1+ import { Template } from 'meteor/templating' ;
2+ import { ProxyBackends } from '/proxy_backends/collection' ;
3+
14import jsyaml from 'js-yaml' ;
25
3- Template . viewApiBackendExport . events ( {
6+ Template . apiExport . onCreated ( function ( ) {
7+ // Get reference to template instance
8+ const instance = this ;
9+
10+ // Get the API Backend ID from data context
11+ const apiId = instance . data . api . _id ;
12+
13+ // Subscribe to proxy settings for this API
14+ instance . subscribe ( 'apiProxySettings' , apiId ) ;
15+ } ) ;
16+
17+ Template . apiExport . events ( {
418 'click #exportJSONConfig' : function ( event , instance ) {
519 // Get API Backend from database collection
620 const api = instance . data . api ;
@@ -24,6 +38,38 @@ Template.viewApiBackendExport.events({
2438 // creates file object with content type of YAML
2539 const file = new Blob ( [ yaml ] , { type : 'application/x-yaml;charset=utf-8' } ) ;
2640
41+ // forces "save As" function allow user download file
42+ saveAs ( file , 'apiConfig.yaml' ) ;
43+ } ,
44+ 'click #exportJSONProxyConfig' : function ( event , instance ) {
45+ // Get the API Backend ID from data context
46+ const apiId = instance . data . api . _id ;
47+
48+ // Find proxy backends by API id
49+ const proxy = ProxyBackends . findOne ( { apiId } ) ;
50+
51+ // converts JSON object to JSON string and adds indentation
52+ const json = JSON . stringify ( proxy , null , '\t' ) ;
53+
54+ // creates file object with content type of JSON
55+ const file = new Blob ( [ json ] , { type : 'application/json;charset=utf-8' } ) ;
56+
57+ // forces "save As" function allow user download file
58+ saveAs ( file , 'apiProxyConfig.json' ) ;
59+ } ,
60+ 'click #exportYAMLProxyConfig' : function ( event , instance ) {
61+ // Get the API Backend ID from data context
62+ const apiId = instance . data . api . _id ;
63+
64+ // Find proxy backends by API id
65+ const proxy = ProxyBackends . findOne ( { apiId } ) ;
66+
67+ // converts from json to yaml
68+ const yaml = jsyaml . safeDump ( proxy ) ;
69+
70+ // creates file object with content type of YAML
71+ const file = new Blob ( [ yaml ] , { type : 'application/x-yaml;charset=utf-8' } ) ;
72+
2773 // forces "save As" function allow user download file
2874 saveAs ( file , 'apiConfig.yaml' ) ;
2975 } ,
0 commit comments