-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathform.js
More file actions
177 lines (148 loc) · 5.1 KB
/
form.js
File metadata and controls
177 lines (148 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Meteor package imports
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { sAlert } from 'meteor/juliancwirko:s-alert';
import { TAPi18n } from 'meteor/tap:i18n';
// Apinf import
import { ProxyBackends } from '/proxy_backends/collection';
import { Proxies } from '/proxies/collection';
// NPM import
import 'urijs';
Template.proxyBackend.onRendered(() => {
// Make Disable API Key field easier to use
// Fixes browser rendering issue
$("[name='apiUmbrella.settings.disable_api_key']").bootstrapSwitch();
});
Template.proxyBackend.helpers({
apiHost () {
// Get one proxy from the Proxies collection
// This assumes we have only one proxy
// TODO: refactor this method for multi-proxy support
const api = this.api;
// Construct URL object for proxy URL
const apiUrl = URI(api.url);
// Return the Proxy URL protocol
return apiUrl.host();
},
apiPortHelper () {
// Placeholder for port
let port;
// Get one proxy from the Proxies collection
// This assumes we have only one proxy
// TODO: refactor this method for multi-proxy support
const api = this.api;
// Construct URL object for proxy URL
const apiUrl = URI(api.url);
// Return the Proxy URL protocol
const protocol = apiUrl.protocol();
// Common default ports for HTTP/HTTPS
if (protocol === 'https') {
port = 443;
} else if (protocol === 'http') {
port = 80;
}
return port;
},
apiProxySettings () {
// Get API ID
const apiId = this.api._id;
// Look for existing proxy backend document for this API
const apiProxySettings = ProxyBackends.findOne({ apiId });
return apiProxySettings;
},
apiUrlProtocol () {
// Get one proxy from the Proxies collection
// This assumes we have only one proxy
// TODO: refactor this method for multi-proxy support
const api = this.api;
// Construct URL object for proxy URL
const apiUrl = URI(api.url);
// Return the Proxy URL protocol
return apiUrl.protocol();
},
formType () {
// Placeholder for form type
let formType;
// Get API ID
const apiId = this.api._id;
// Look for existing proxy backend document for this API
const existingSettings = ProxyBackends.findOne({ apiId });
if (existingSettings) {
formType = 'update';
} else {
formType = 'insert';
}
return formType;
},
proxy () {
// TODO: determine how to provide proxyId for the ProxyBackend form
// e.g. will we have more than one proxy?
// if no, we need also to limit the number of proxies that can be added
// Get a single Proxy
const proxy = Proxies.findOne();
return proxy;
},
proxyBackendsCollection () {
// Return a reference to ProxyBackends collection, for AutoForm
return ProxyBackends;
},
proxyHost () {
// TODO: determine how to provide proxyId for the ProxyBackend form
// e.g. will we have more than one proxy?
// if no, we need also to limit the number of proxies that can be added
// Get a single Proxy
const proxy = Proxies.findOne();
if (proxy && proxy.apiUmbrella) {
// Get frontend host from template instance
const frontend = URI(proxy.apiUmbrella.url);
return frontend.host();
}
},
});
Template.apiProxy.events({
'click #delete-proxy-button': () => {
/* Function procedure in generic form
1. Delete API Backend on proxy (eg. API Umbrella)
- call necessary functions by proxy type
2. Delete Proxy Backend on Apinf
*/
// Get template instance
const instance = Template.instance();
// Get proxyBackend from template data
const proxyBackend = instance.data.proxyBackend;
// Check proxyBackend exists, type is apiUmbrella, and it has id
if (proxyBackend &&
proxyBackend.apiUmbrella &&
proxyBackend.apiUmbrella.id) {
const umbrellaBackendId = instance.data.proxyBackend.apiUmbrella.id;
// Delete API Backend on API Umbrella
Meteor.call(
'deleteApiBackendOnApiUmbrella',
umbrellaBackendId,
(deleteError) => {
if (deleteError) {
const deleteErrorMessage = TAPi18n.__('proxyBackendForm_deleteErrorMessage');
sAlert.error(`${deleteErrorMessage}:\n ${deleteError}`);
} else {
// Publish changes for deleted API Backend on API Umbrella
Meteor.call(
'publishApiBackendOnApiUmbrella',
umbrellaBackendId,
(publishError) => {
if (publishError) {
const publishErrorMessage = TAPi18n.__('proxyBackendForm_publishErrorMessage');
sAlert.error(`${publishErrorMessage}:\n ${publishError}`);
} else if (proxyBackend._id) { // Check proxyBackend has _id
// Delete proxyBackend from Apinf
ProxyBackends.remove(proxyBackend._id);
const successMessage = TAPi18n.__('proxyBackendForm_deleteSuccessMessage');
sAlert.success(successMessage);
}
}
);
}
}
);
}
},
});