-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.js
More file actions
586 lines (450 loc) · 12.5 KB
/
index.js
File metadata and controls
586 lines (450 loc) · 12.5 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
import Request from 'backendless-request'
import APIRequest from './request'
import Urls from './urls'
import Utils from './utils'
import Expression from './expression'
const DEFAULT_PROPS = {
appId : null,
apiKey : null,
serverURL : 'https://api.backendless.com',
automationServerURL: null,
domain : null,
apiURI : '/api',
debugMode : false,
standalone : false,
XMLHttpRequest : typeof XMLHttpRequest !== 'undefined'
? XMLHttpRequest
: undefined,
}
const STATELESS_PROPS = ['appId', 'apiKey', 'domain']
const previousBackendless = Utils.globalScope && Utils.globalScope.Backendless
const showLegacyDataWarning = () => {
if (!showLegacyDataWarning.isShown) {
// eslint-disable-next-line no-console
console.warn('Backendless.Persistence is deprecated namespace, use Backendless.Data instead')
showLegacyDataWarning.isShown = true
}
}
// Backendless supports three signatures for the initApp method
// two args - applicationId {String} and secretKey {String}
// one argument - domain {String}
// one argument - whole set of options {Object}
const parseInitConfig = (...args) => {
const [appId, apiKey] = args
if (appId && typeof appId === 'object') {
return appId
}
if (typeof appId === 'string' && !apiKey) {
return { domain: appId }
}
return {
appId,
apiKey
}
}
const validateConfig = config => {
if (config.domain) {
if (!config.domain.startsWith('https://') && !config.domain.startsWith('http://')) {
throw new Error(
'When initialize the SDK with a custom domain it should start with http:// or https://, ' +
'for example: Backendless.initApp(\'https://foobar.com\')'
)
}
}
}
const SERVICES = {
'Logging' : () => require('./logging').default,
'Counters' : () => require('./counters').default,
'Cache' : () => require('./cache').default,
'Commerce' : () => require('./commerce').default,
'Users' : () => require('./users').default,
'BL' : () => require('./bl').default,
'Data' : () => require('./data').default,
'Hive' : () => require('./hive').default,
'Messaging' : () => require('./messaging').default,
'Files' : () => require('./files').default,
'RT' : () => require('./rt').default,
'SharedObject': () => require('./rso').default,
'LocalCache' : () => require('./local-cache').default,
'UnitOfWork' : () => require('./unit-of-work').default,
'Management' : () => require('./management').default,
'Automations' : () => require('./automations').default,
}
class Backendless {
constructor(props) {
this.initConfig(props)
this.Request = Request
this.request = new APIRequest(this)
this.urls = new Urls(this)
}
/**
* @param {Object} config
*/
initConfig(config) {
config = { ...config }
if (config.domain) {
delete config.appId
delete config.apiKey
}
for (const key in DEFAULT_PROPS) {
if (DEFAULT_PROPS.hasOwnProperty(key)) {
const privateKey = `__${key}`
if (STATELESS_PROPS.includes(key)) {
delete this[privateKey]
}
const defaultValue = this[privateKey] === undefined
? DEFAULT_PROPS[key]
: this[privateKey]
this[privateKey] = config[key] === undefined
? defaultValue
: config[key]
}
}
}
/**
* @param {string|Object} appId|domain|config
* @param {string} [apiKey]
* @returns {Backendless}
*/
initApp() {
const config = parseInitConfig(...arguments)
validateConfig(config)
const app = config.standalone
? new Backendless(this)
: this
app.initConfig(config)
app.resetRT()
app.__removeService('LocalCache')
app.appInfoPromise()
const loggingConfig = Object.assign({ loadLevels: true, globalLevel: 'all', levels: {} }, config.logging)
if (app.__hasService('Logging')) {
app.Logging.reset()
app.Logging.setConfig(loggingConfig)
}
if (app.__hasService('Users')) {
app.Users.currentUser = null
}
delete this.__device
app.loggingConfig = loggingConfig
return app
}
appInfoPromise(reset) {
if (reset || !this.__appInfoPromise) {
this.__appInfoPromise = new Promise((resolve, reject) => {
this.request.get({ url: this.urls.appInfo() })
.then(resolve)
.catch(error => {
if (error.code === 3064) {
this.setCurrentUserToken(null)
return this.request.get({ url: this.urls.appInfo() })
}
throw error
})
.catch(reject)
})
}
this.__appInfoPromise
.catch(error => {
// eslint-disable-next-line no-console
console.error('could not load backendless app info', error)
})
return this.__appInfoPromise
}
__hasService(name) {
return !!this[`__${name}`]
}
__removeService(name) {
delete this[`__${name}`]
}
__getService(name) {
const privateName = `__${name}`
if (!this[privateName]) {
const Service = SERVICES[name]()
this[privateName] = new Service(this)
}
return this[privateName]
}
///--------SETTERS/GETTERS-------///
///--------standalone-------///
get standalone() {
return this.__standalone
}
set standalone(standalone) {
throw new Error(
'Setting value to Backendless.standalone directly is not possible, ' +
`instead you must use Backendless.initApp({ appId: [APP_ID], apiKey: [API_KEY], standalone: ${standalone} })`
)
}
get appId() {
return this.__appId
}
set appId(appId) {
throw new Error(
`Setting '${appId}' value to Backendless.appId directly is not possible, ` +
`instead you must use Backendless.initApp('${appId}', API_KEY)`
)
}
get apiKey() {
return this.__apiKey
}
set apiKey(apiKey) {
throw new Error(
`Setting '${apiKey}' value to Backendless.apiKey directly is not possible, ` +
`instead you must use Backendless.initApp(APP_ID, '${apiKey}')`
)
}
///--------serverURL-------///
get serverURL() {
return this.__serverURL
}
set serverURL(url) {
this.__serverURL = url
}
///--------automationServerURL-------///
get automationServerURL() {
return this.__automationServerURL
}
set automationServerURL(url) {
this.__automationServerURL = url
}
///--------domain-------///
get domain() {
return this.__domain
}
set domain(domain) {
this.__domain = domain
}
///--------apiURI-------///
get apiURI() {
return this.__apiURI
}
set apiURI(apiURI) {
this.__apiURI = apiURI
}
///--------appPath-------///
get appPath() {
if (this.domain) {
return this.domain + this.apiURI
}
if (this.appId && this.apiKey) {
return [this.serverURL, this.appId, this.apiKey].join('/')
}
return null
}
set appPath(appPath) {
throw new Error(
`Setting '${appPath}' value to Backendless.appPath directly is not possible, ` +
'instead you must use Backendless.initApp(APP_ID, API_KEY) for setup the value'
)
}
get automationPath() {
if (!this.automationServerURL) {
return this.appPath
}
return [this.automationServerURL, this.appId, this.apiKey].join('/')
}
set automationPath(automationPath) {
throw new Error(
`Setting '${automationPath}' value to Backendless.automationPath directly is not possible`
)
}
///--------debugMode-------///
get debugMode() {
return this.__debugMode
}
set debugMode(debugMode) {
debugMode = !!debugMode
if (this.__debugMode !== debugMode) {
this.__debugMode = debugMode
if (this.__RT) {
this.RT.setDebugMode(debugMode)
}
}
}
///--------XMLHttpRequestMode-------///
get XMLHttpRequest() {
return this.__XMLHttpRequest
}
set XMLHttpRequest(XMLHttpRequest) {
this.__XMLHttpRequest = XMLHttpRequest
}
///--------device-------///
get device() {
if (!this.__device) {
throw new Error('Device is not defined. Please, run the Backendless.setupDevice')
}
return this.__device
}
set device(props) {
throw new Error(
'Setting value to Backendless.device directly is not possible, ' +
'instead you must use Backendless.setupDevice(deviceProperties) for setup the device'
)
}
setupDevice(device) {
const Device = require('./device').default
this.__device = new Device(device)
}
///----------UTIL METHODS--------///
get Utils() {
return Utils
}
getCurrentUserToken() {
return this.Users.getCurrentUserToken()
}
setCurrentUserToken(userToken) {
this.Users.setCurrentUserToken(userToken)
}
get browser() {
return require('./user-agent').getUserAgent()
}
noConflict() {
if (Utils.globalScope) {
Utils.globalScope.Backendless = previousBackendless
}
return this
}
///-------------------------------------///
///-------------- SERVICES -------------///
get Logging() {
return this.__getService('Logging')
}
get Counters() {
return this.__getService('Counters')
}
get Cache() {
return this.__getService('Cache')
}
get Commerce() {
return this.__getService('Commerce')
}
get Users() {
return this.__getService('Users')
}
get User() {
return require('./users/user').default
}
get UserService() {
return this.Users
}
get BL() {
return this.__getService('BL')
}
get CustomServices() {
return this.BL.CustomServices
}
get APIServices() {
return this.BL.CustomServices
}
get Events() {
return this.BL.Events
}
get Data() {
return this.__getService('Data')
}
get Hive() {
return this.__getService('Hive')
}
get Messaging() {
return this.__getService('Messaging')
}
get Files() {
return this.__getService('Files')
}
get RT() {
return this.__getService('RT')
}
resetRT() {
if (this.__RT) {
this.__RT.terminate()
delete this.__RT
}
}
get SharedObject() {
return this.__getService('SharedObject')
}
get LocalCache() {
return this.__getService('LocalCache')
}
get UnitOfWork() {
return this.__getService('UnitOfWork')
}
get Management() {
return this.__getService('Management')
}
get Automations() {
return this.__getService('Automations')
}
///-------------- SERVICES -------------///
///-------------------------------------///
///-------------------------------------///
///--------BACKWARD COMPATIBILITY-------///
/** @deprecated */
get applicationId() {
// eslint-disable-next-line no-console
// temporary comment it because it breaks JS-CodeRunner version less than 6.3.0
// console.warn('getter/setter for Backendless.applicationId is deprecated, instead use Backendless.appId')
return this.appId
}
/** @deprecated */
set applicationId(appId) {
// eslint-disable-next-line no-console
console.warn('getter/setter for Backendless.applicationId is deprecated, instead use Backendless.appId')
this.appId = appId
}
/** @deprecated */
get secretKey() {
// eslint-disable-next-line no-console
console.warn('getter/setter for Backendless.secretKey is deprecated, instead use Backendless.apiKey')
return this.apiKey
}
/** @deprecated */
set secretKey(apiKey) {
// eslint-disable-next-line no-console
console.warn('getter/setter for Backendless.secretKey is deprecated, instead use Backendless.apiKey')
this.apiKey = apiKey
}
/** @deprecated */
get Persistence() {
showLegacyDataWarning()
return this.Data
}
get DataQueryBuilder() {
return this.Data.QueryBuilder
}
get GroupQueryBuilder() {
return this.Data.GroupQueryBuilder
}
get JSONUpdateBuilder() {
return this.Data.JSONUpdateBuilder
}
get LoadRelationsQueryBuilder() {
return this.Data.LoadRelationsQueryBuilder
}
get Bodyparts() {
return this.Messaging.Bodyparts
}
get PublishOptions() {
return this.Messaging.PublishOptions
}
get DeliveryOptions() {
return this.Messaging.DeliveryOptions
}
get PublishOptionsHeaders() {
return this.Messaging.PublishOptionsHeaders
}
get EmailEnvelope() {
return this.Messaging.EmailEnvelope
}
get Expression() {
return Expression
}
///--------BACKWARD COMPATIBILITY-------///
///-------------------------------------///
}
const backendless = new Backendless(DEFAULT_PROPS)
if (Utils.globalScope) {
Utils.globalScope.Backendless = backendless
}
exports = module.exports = backendless
export default backendless