-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbl.js
308 lines (233 loc) · 8.87 KB
/
bl.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import _each from 'lodash/each'
import urls from './urls'
import { BL_MODELS, BL_CHAIN } from './utils/cache-tags'
import { encodePath } from './utils/path'
const hostedServices = appId => `${ urls.blBasePath(appId) }/generic`
const codelessServices = appId => `${ urls.blBasePath(appId) }/codeless`
const jsServices = appId => `${ urls.blBasePath(appId) }/js`
const marketplaceServices = appId => `${ urls.blBasePath(appId) }/marketplace`
const hostedServiceConfig = (appId, serviceId) => `${ hostedServices(appId) }/configure/${ serviceId }`
const marketplaceServiceConfig = (appId, serviceName) => `${ marketplaceServices(appId) }/configure/${ serviceName }`
const MODES = {
MARKETPLACE: 'MARKETPLACE',
PROD : 'PRODUCTION',
DEBUG : 'DEBUG',
DRAFT : 'DRAFT'
}
// TODO: remove this transformation when the format of config will be changed [CONSOLE-599]
const CONFIG_NAMES_MAP = {
displayName: 'label'
}
// TODO: remove this transformation when the format of config will be changed [CONSOLE-599]
const normalizeService = service => {
if (service.configuration) {
service.configDescriptions = service.configuration.map(normalizeServiceConfigDescription)
delete service.configuration
}
return service
}
// TODO: remove this transformation when the format of config will be changes [CONSOLE-599]
const normalizeServiceConfigDescription = configDescription => {
for (const key in configDescription) {
const normalizeName = CONFIG_NAMES_MAP[key]
if (normalizeName) {
configDescription[normalizeName] = configDescription[key]
delete configDescription[key]
}
}
return configDescription
}
const parseServiceSpec = spec => {
const { paths, ...summary } = spec
const methods = {}
_each(paths, (pathBody, path) => {
_each(pathBody, (methodBody, type) => {
const id = methodBody.operationId
methods[id] = {
...methodBody,
path,
type,
id
}
})
})
return { summary, methods }
}
const normalizeHandler = handler => {
if (handler.mode === MODES.MARKETPLACE) {
handler.mode = MODES.PROD
handler.fromMarketplace = true
}
return handler
}
const normalizeHandlers = handlers => handlers.map(normalizeHandler)
export default req => ({
getServices(appId) {
return req.get(urls.blBasePath(appId))
.then(services => services.map(normalizeService))
// TODO: remove this transformation when the format of config will be changes [CONSOLE-599]
},
getServiceSpec(appId, serviceId, transformSpec = true) {
const result = req.get(`${ urls.blBasePath(appId) }/${ serviceId }/api-docs`)
if (transformSpec) {
return result.then(parseServiceSpec)
}
return result
},
getServiceMethods(appId, serviceId) {
return req.get(`${ urls.blBasePath(appId) }/${ serviceId }/methods`)
},
importService(appId, { service, serviceURL, file }) {
let data
if (file) {
data = new FormData()
data.append('file', file)
} else {
data = service ? { appId, service } : { serviceURL }
}
return req.post(`${ urls.blBasePath(appId) }/import`, data)
},
createService(appId, data) {
let formData = data
if (!data.createFromSample) {
formData = new FormData()
formData.append('files', data.file)
formData.append('createFromSample', false)
formData.append('model', data.model)
}
return req.post(`${ urls.blBasePath(appId) }/generic`, formData)
.then(services => services.map(normalizeService))
// TODO: remove this transformation when the format of config will be changed [CONSOLE-599]
},
createAWSLambdaService(appId, credentials) {
return req.post(`${ urls.blBasePath(appId) }/aws-lambda`, { ...credentials, appId })
},
createCodelessService(appId, service) {
return req
.post(codelessServices(appId), service)
.cacheTags(BL_MODELS(appId, 'CODELESS'))
},
createJsService(appId, service) {
return req
.post(jsServices(appId), service)
.cacheTags(BL_MODELS(appId, 'JS'))
},
updateCodelessService(appId, serviceId, updates) {
return req.put(`${ codelessServices(appId) }/${ serviceId }`, updates)
},
deleteCodelessService(appId, serviceId) {
return req.delete(`${ codelessServices(appId) }/${ serviceId }`)
},
createCodelessMethod(appId, serviceId, method) {
return req.post(`${ codelessServices(appId) }/${ serviceId }/`, method)
},
updateCodelessMethod(appId, serviceId, methodId, method) {
return req.put(`${ codelessServices(appId) }/${ serviceId }/${ methodId }`, method)
},
deleteCodelessMethod(appId, serviceId, methodId) {
return req.delete(`${ codelessServices(appId) }/${ serviceId }/${ methodId }`)
},
getCodelessMethodLogic(appId, serviceId, methodId) {
return req.get(`${ codelessServices(appId) }/${ serviceId }/${ methodId }/logic`)
},
deployCodelessMethodLogic(appId, serviceId, methodId, logic, params) {
return req.put(`${ codelessServices(appId) }/${ serviceId }/${ methodId }/logic`, logic).query(params)
},
deleteService(appId, serviceId) {
return req.delete(hostedServices(appId), [serviceId])
},
updateService(appId, serviceId, updates) {
return req.put(`${ hostedServices(appId) }/${ serviceId }/update`, updates)
},
updateServiceDefaultModel(appId, updates) {
return req.put(`${urls.blBasePath(appId)}/default-model`, updates)
},
loadServiceConfig(appId, serviceId) {
return req.get(hostedServiceConfig(appId, serviceId))
},
setServiceConfig(appId, serviceId, config) {
return req.post(hostedServiceConfig(appId, serviceId), config)
},
setMarketplaceServiceConfig(appId, serviceName, config) {
return req.post(marketplaceServiceConfig(appId, serviceName), config)
},
testServiceConfig(appId, serviceId, config) {
return req.post(hostedServiceConfig(appId, `test/${serviceId}`), config)
},
getDraftFiles(appId, language, model) {
return req.get(urls.blDraft(appId, language, model))
},
saveDraftFiles(appId, language, model, files, sync = true) {
const encodedFiles = files.map(({ id, ...rest }) => ({ id: encodePath(id), ...rest }))
return req.put(urls.blDraft(appId, language, model), encodedFiles).query({ sync })
},
deployDraftFiles(appId, language, model, files, sync = true) {
return req.put(urls.blProd(appId, language, model), files).query({ sync })
},
getDraftFileContent(appId, language, model, fileId) {
return req.get(`${ urls.blDraft(appId, language, model) }/${ encodePath(fileId) }`)
},
getLanguages() {
return req.get(`${ urls.console() }/servercode/languages`)
},
getModels(appId, language) {
return req
.get(`${ urls.serverCode(appId) }/models/${ language }`)
.cacheTags(BL_MODELS(appId, language))
},
getAllModels(appId) {
return req.get(`${ urls.serverCode(appId) }/models`)
},
createModel(appId, language, model) {
const data = { appId, language, model }
return req.post(`${ urls.serverCode(appId) }/models/create`, data)
},
createEventHandler(appId, handler) {
const { category, mode, ...data } = handler
return req
.post(urls.blHandlersCategory(appId, mode, category), data)
.cacheTags(BL_MODELS(appId, handler.language))
},
updateEventHandler(appId, handler) {
const { id, category, fromMarketplace } = handler
if (fromMarketplace) {
handler.mode = MODES.MARKETPLACE
}
const url = `${ urls.blHandlersCategory(appId, handler.mode, category) }/${ id }`
return req.put(url, handler).then(normalizeHandler)
},
getEventHandlers(appId, modes = []) {
return req.get(urls.serverCode(appId)).query({ mode: modes }).then(normalizeHandlers)
},
deleteEventHandler(appId, handler) {
const { id, mode, category } = handler
const url = `${ urls.blHandlersCategory(appId, mode, category) }/${ id }`
return req.delete(url)
},
getHandlerInvocationChain(appId, eventId, context) {
return req.get(urls.blHandlersChain(appId, eventId, context))
.cacheTags(BL_CHAIN(appId, eventId, context))
},
updateHandlerInvocationChain(appId, eventId, context, updates) {
return req.put(urls.blHandlersChain(appId, eventId, context), updates)
.cacheTags(BL_CHAIN(appId, eventId, context))
},
invokeTimer(appId, timer) {
const { timername, mode, category } = timer
//POST /:applicationId/console/servercode/:mode/timers/:timerName/run
return req.post(`${ urls.blHandlersCategory(appId, mode, category) }/${ timername }/run`)
},
changeHandlerState(appId, mode, category, timerId, enabled) {
return req.put(`${urls.blHandlersCategory(appId, mode, category)}/${ timerId }/state`, { enabled })
},
getCategories(appId) {
return req.get(`${ urls.serverCode(appId) }/categories`)
},
getEvents(appId) {
return req.get(`${ urls.serverCode(appId) }/events`)
},
getTimerLogs(appId, query) {
return req.get(`${urls.serverCode(appId)}/timers/logs`)
.query(query)
}
})