Skip to content

Commit 7cc48a9

Browse files
Vladimir Upirovvengrov
authored andcommitted
5.1 (#120)
* - fix sending logs to the server * - upgrade backendless-rt-client to version 0.0.16 * - load runtime dependencies only when it necessary - fix tests
1 parent 71b66d5 commit 7cc48a9

File tree

9 files changed

+188
-87
lines changed

9 files changed

+188
-87
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@
8484
},
8585
"dependencies": {
8686
"backendless-request": "~0.0.11",
87-
"backendless-rt-client": "0.0.15"
87+
"backendless-rt-client": "0.0.16"
8888
}
8989
}

src/index.js

Lines changed: 142 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,7 @@
11
import Request from 'backendless-request'
22

3-
import Logging from './logging'
4-
import Counters from './counters'
5-
import Cache from './cache'
6-
import Commerce from './commerce'
7-
import Users from './users'
8-
import User from './users/user'
9-
import CustomServices from './bl/custom-services'
10-
import Events from './bl/events'
11-
import Geo from './geo'
12-
import Data from './data'
13-
import Messaging from './messaging'
14-
import Device from './device'
15-
import Files from './files'
16-
import RT, { setRTDebugMode } from './rt'
17-
import SharedObject from './rso'
18-
import LocalCache from './local-cache'
193
import LocalVars from './local-vars'
204

21-
import { initApp } from './init-app'
22-
import { getUserAgent } from './user-agent'
23-
import { getCurrentUserToken } from './users/current-user'
24-
255
const root = (typeof self === 'object' && self.self === self && self) ||
266
(typeof global === 'object' && global.global === global && global)
277

@@ -36,7 +16,7 @@ const Backendless = {
3616
set debugMode(debugMode) {
3717
LocalVars.debugMode = !!debugMode
3818

39-
setRTDebugMode(LocalVars.debugMode)
19+
require('./rt').setRTDebugMode(LocalVars.debugMode)
4020
},
4121

4222
get serverURL() {
@@ -59,14 +39,35 @@ const Backendless = {
5939
return LocalVars.applicationId
6040
},
6141

42+
set applicationId(appId) {
43+
throw new Error(
44+
`Setting '${appId}' value to Backendless.applicationId directly is not possible, ` +
45+
`instead you must use Backendless.initApp('${appId}', API_KEY)`
46+
)
47+
},
48+
6249
get secretKey() {
6350
return LocalVars.secretKey
6451
},
6552

53+
set secretKey(apiKey) {
54+
throw new Error(
55+
`Setting '${apiKey}' value to Backendless.secretKey directly is not possible, ` +
56+
`instead you must use Backendless.initApp(APP_ID, '${apiKey}')`
57+
)
58+
},
59+
6660
get appPath() {
6761
return LocalVars.appPath
6862
},
6963

64+
set appPath(appPath) {
65+
throw new Error(
66+
`Setting '${appPath}' value to Backendless.appPath directly is not possible, ` +
67+
'instead you must use Backendless.initApp(APP_ID, API_KEY) for setup the value'
68+
)
69+
},
70+
7071
get ServerCode() {
7172
return LocalVars.ServerCode
7273
},
@@ -75,13 +76,23 @@ const Backendless = {
7576
LocalVars.ServerCode = ServerCode
7677
},
7778

78-
initApp,
79+
initApp(...args) {
80+
require('./init-app').initApp(...args)
81+
},
82+
83+
getCurrentUserToken() {
84+
return require('./users/current-user').getCurrentUserToken()
85+
},
7986

80-
getCurrentUserToken,
87+
setupDevice(...args) {
88+
const { default: Device } = require('./device')
8189

82-
setupDevice: Device.setup,
90+
Device.setup(...args)
91+
},
8392

84-
browser: getUserAgent(),
93+
get browser() {
94+
return require('./user-agent').getUserAgent()
95+
},
8596

8697
Request,
8798

@@ -96,20 +107,61 @@ const Backendless = {
96107
///-------------------------------------///
97108
///-------------- SERVICES -------------///
98109

99-
Logging : Logging,
100-
Counters : Counters,
101-
Cache : Cache,
102-
Commerce : Commerce,
103-
Users : Users,
104-
User : User,
105-
CustomServices: CustomServices,
106-
Events : Events,
107-
Geo : Geo,
108-
Data : Data,
109-
Messaging : Messaging,
110-
Files : Files,
111-
RT : RT,
112-
SharedObject : SharedObject,
110+
get Logging() {
111+
return require('./logging').default
112+
},
113+
114+
get Counters() {
115+
return require('./counters').default
116+
},
117+
118+
get Cache() {
119+
return require('./cache').default
120+
},
121+
122+
get Commerce() {
123+
return require('./commerce').default
124+
},
125+
126+
get Users() {
127+
return require('./users').default
128+
},
129+
130+
get User() {
131+
return require('./users/user').default
132+
},
133+
134+
get CustomServices() {
135+
return require('./bl/custom-services').default
136+
},
137+
138+
get Events() {
139+
return require('./bl/events').default
140+
},
141+
142+
get Geo() {
143+
return require('./geo').default
144+
},
145+
146+
get Data() {
147+
return require('./data').default
148+
},
149+
150+
get Messaging() {
151+
return require('./messaging').default
152+
},
153+
154+
get Files() {
155+
return require('./files').default
156+
},
157+
158+
get RT() {
159+
return require('./rt').default
160+
},
161+
162+
get SharedObject() {
163+
return require('./rso').default
164+
},
113165

114166
///-------------- SERVICES -------------///
115167
///-------------------------------------///
@@ -118,22 +170,59 @@ const Backendless = {
118170
///--------BACKWARD COMPATIBILITY-------///
119171

120172
//TODO: do we need to remove it?
121-
UserService : Users,
122-
GeoQuery : Geo.Query,
123-
GeoPoint : Geo.Point,
124-
GeoCluster : Geo.Cluster,
125-
Persistence : Data,
126-
DataQueryBuilder : Data.QueryBuilder,
127-
LoadRelationsQueryBuilder: Data.LoadRelationsQueryBuilder,
128-
Bodyparts : Messaging.Bodyparts,
129-
PublishOptions : Messaging.PublishOptions,
130-
DeliveryOptions : Messaging.DeliveryOptions,
131-
PublishOptionsHeaders : Messaging.PublishOptionsHeaders,
132-
133-
LocalCache,
173+
174+
get UserService() {
175+
return Backendless.Users
176+
},
177+
178+
get GeoQuery() {
179+
return Backendless.Geo.Query
180+
},
181+
182+
get GeoPoint() {
183+
return Backendless.Geo.Point
184+
},
185+
186+
get GeoCluster() {
187+
return Backendless.Geo.Cluster
188+
},
189+
190+
get Persistence() {
191+
return Backendless.Data
192+
},
193+
194+
get DataQueryBuilder() {
195+
return Backendless.Data.QueryBuilder
196+
},
197+
198+
get LoadRelationsQueryBuilder() {
199+
return Backendless.Data.LoadRelationsQueryBuilder
200+
},
201+
202+
get Bodyparts() {
203+
return Backendless.Messaging.Bodyparts
204+
},
205+
206+
get PublishOptions() {
207+
return Backendless.Messaging.PublishOptions
208+
},
209+
210+
get DeliveryOptions() {
211+
return Backendless.Messaging.DeliveryOptions
212+
},
213+
214+
get PublishOptionsHeaders() {
215+
return Backendless.Messaging.PublishOptionsHeaders
216+
},
217+
218+
get LocalCache() {
219+
return require('./local-cache').default
220+
},
134221

135222
/** @deprecated */
136-
SubscriptionOptions : Messaging.SubscriptionOptions,
223+
get SubscriptionOptions() {
224+
return Backendless.Messaging.SubscriptionOptions
225+
},
137226

138227
///--------BACKWARD COMPATIBILITY-------///
139228
///-------------------------------------///

src/init-app.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import LocalVars from './local-vars'
2-
import LoggingCollector from './logging/collector'
3-
import GeoTrackerMonitor from './geo/tracker-monitor'
4-
import { initRTClient } from './rt'
5-
import { setLocalCurrentUser } from './users/current-user'
6-
71
export function initApp(appId, secretKey) {
2+
const { default: LocalVars } = require('./local-vars')
3+
const { default: LoggingCollector } = require('./logging/collector')
4+
const { default: GeoTracker } = require('./geo/tracker-monitor/tracker')
5+
const { initRTClient } = require('./rt')
6+
const { setLocalCurrentUser } = require('./users/current-user')
7+
88
LocalVars.applicationId = appId
99
LocalVars.secretKey = secretKey
1010
LocalVars.appPath = [LocalVars.serverURL, appId, secretKey].join('/')
1111

1212
initRTClient()
1313

1414
LoggingCollector.reset()
15-
GeoTrackerMonitor.reset()
15+
GeoTracker.reset()
1616

1717
setLocalCurrentUser()
1818
}

src/logging/collector.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import Urls from '../urls'
33
import Request from '../request'
44
import Async from '../request/async'
55

6-
import Logger from './logger'
7-
86
let lastFlushListeners
97

108
function flush(asyncHandler) {
@@ -58,6 +56,8 @@ const LoggingCollector = {
5856
},
5957

6058
getLogger(loggerName) {
59+
const { default: Logger } = require('./logger')
60+
6161
if (!Utils.isString(loggerName)) {
6262
throw new Error("Invalid 'loggerName' value. LoggerName must be a string value")
6363
}
@@ -71,7 +71,7 @@ const LoggingCollector = {
7171
message,
7272
exception,
7373
'log-level': logLevel,
74-
timestamp: Date.now()
74+
timestamp : Date.now()
7575
}
7676

7777
LoggingCollector.pool.push(messageObj)
@@ -89,7 +89,9 @@ const LoggingCollector = {
8989
flushSync: Utils.synchronized(flush),
9090

9191
sendRequest() {
92-
LoggingCollector.flushInterval = setTimeout(() => LoggingCollector.flush(), LoggingCollector.timeFrequency * 1000)
92+
if (!LoggingCollector.flushInterval) {
93+
LoggingCollector.flushInterval = setTimeout(() => LoggingCollector.flush(), LoggingCollector.timeFrequency * 1000)
94+
}
9395
},
9496

9597
setLogReportingPolicy(numOfMessages, timeFrequency) {

src/user-agent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import Utils from './utils'
2-
31
export function getUserAgent() {
2+
const { default: Utils } = require('./utils')
3+
44
let ua = 'NodeJS'
55

66
if (Utils.isBrowser) {

src/users/current-user.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import Utils from '../utils'
2-
import User from './user'
3-
import Data from '../data'
42
import Urls from '../urls'
53
import Request from '../request'
64
import Async from '../request/async'
@@ -40,14 +38,17 @@ export const getCurrentUser = asyncHandler => {
4038
const currentUserId = stayLoggedIn && LocalCache.get('current-user-id')
4139

4240
if (currentUserId) {
41+
const { default: Data } = require('../data')
42+
const { default: User } = require('./user')
43+
4344
return Data.of(User).findById(currentUserId, asyncHandler)
4445
}
4546

4647
return asyncHandler ? asyncHandler.success(null) : null
4748
}
4849

4950
export function isValidLogin(/** async */) {
50-
const userToken = LocalCache.get('user-token')
51+
const userToken = getCurrentUserToken()
5152
const responder = Utils.extractResponder(arguments)
5253
const isAsync = !!responder
5354

test/e2e/specs/init-app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ describe('initApp', function() {
2222

2323
const appPath = Backendless.appPath
2424

25-
expect(() => Backendless.applicationId = 'applicationId').to.throwError // eslint-disable-line
26-
expect(() => Backendless.secretKey = 'secretKey').to.throwError // eslint-disable-line
27-
expect(() => Backendless.appPath = 'appPath').to.throwError // eslint-disable-lin
25+
expect(() => Backendless.applicationId = 'applicationId').to.throw() // eslint-disable-line
26+
expect(() => Backendless.secretKey = 'secretKey').to.throw() // eslint-disable-line
27+
expect(() => Backendless.appPath = 'appPath').to.throw() // eslint-disable-line
2828

2929
expect(Backendless.applicationId).to.be.equal(APP_ID)
3030
expect(Backendless.secretKey).to.be.equal(SECRET_KEY)

0 commit comments

Comments
 (0)