Skip to content

API-3 - Extend API Info response #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 23 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"nyc": "^15.1.0",
"request": "^2.88.2",
"rimraf": "^3.0.2",
"socket.io": "^2.3.0",
"socket.io": "^2.5.1",
"terser-webpack-plugin": "^2.3.8",
"typescript": "^4.5.4",
"watch": "^1.0.2",
Expand All @@ -90,8 +90,8 @@
"webpack-cli": "^5.0.2"
},
"dependencies": {
"@babel/runtime": "^7.21.5",
"backendless-request": "^0.7.9",
"backendless-rt-client": "0.3.0"
"@babel/runtime": "^7.26.7",
"backendless-request": "^0.8.0",
"backendless-rt-client": "0.5.0"
}
}
14 changes: 14 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ class Backendless {

app.__removeService('LocalCache')

app.appInfoPromise()

const loggingConfig = Object.assign({ loadLevels: true, globalLevel: 'all', levels: {} }, config.logging)

if (app.__hasService('Logging')) {
Expand All @@ -166,6 +168,18 @@ class Backendless {
return app
}

appInfoPromise(reset) {
if (reset || !this.__appInfoPromise) {
this.__appInfoPromise = new Promise((resolve, reject) => {
this.request.get({ url: this.urls.appInfo() })
.then(resolve)
.catch(reject)
})
}

return this.__appInfoPromise
}

__hasService(name) {
return !!this[`__${name}`]
}
Expand Down
6 changes: 3 additions & 3 deletions src/logging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ export default class Logging {
}

loadLoggingLevels() {
this.app.request
.get({ url: this.app.urls.loggingLevels() })
.then(loggersList => {
this.app.appInfoPromise()
.then(appInfo => {
const { loggers: loggersList } = appInfo
const loggers = {}

loggersList.forEach(logger => {
Expand Down
16 changes: 6 additions & 10 deletions src/rt.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import Request from 'backendless-request'
import BackendlessRTClient from 'backendless-rt-client'

import Utils from './utils'

export const RTListeners = BackendlessRTClient.Listeners
export const RTScopeConnector = BackendlessRTClient.ScopeConnector

function loadAppInfo(appPath) {
return Request.get(`${appPath}/info`)
}

export default class RT extends BackendlessRTClient {
constructor(app) {
const { appId, apiKey, appPath, debugMode } = app
const { appId, apiKey, debugMode } = app

const clientId = Utils.uuid()
const lookupPath = `${appPath}/rt/lookup`

super({
appId: appId || undefined,
lookupPath,
debugMode,
connectQuery() {
const userToken = app.getCurrentUserToken()
Expand All @@ -31,10 +24,13 @@ export default class RT extends BackendlessRTClient {
}
},

hostResolver(){
return app.appInfoPromise().then(({ rtURL }) => rtURL)
},

socketConfigTransform: async socketConfig => {
if (!appId) {
const appInfo = await loadAppInfo(appPath)

const appInfo = await app.appInfoPromise()
socketConfig.url = `${socketConfig.host}/${appInfo.appId}`
socketConfig.options.path = `/${appInfo.appId}`
socketConfig.options.query.apiKey = appInfo.apiKey
Expand Down
6 changes: 6 additions & 0 deletions src/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export default class Urls {
return this.app.appPath
}

// app info

appInfo() {
return `${this.root()}/info`
}

//automations

automation() {
Expand Down
4 changes: 3 additions & 1 deletion test/unit/helpers/rt.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ export function createMockRTServer(appId = APP_ID) {
socket.on(RTSocketEvents.SUB_ON, data => onMessage(RTSocketEvents.SUB_ON, data))
socket.on(RTSocketEvents.SUB_OFF, data => onMessage(RTSocketEvents.SUB_OFF, data))

onMessage(RTSocketEvents.CONNECT, { apiKey, clientId, userToken: userToken === 'null' ? null : userToken })
onMessage(RTSocketEvents.CONNECT, {
apiKey, clientId, userToken: userToken === 'null' ? null : userToken, connectionId: socket.id
})
})

const emit = (type, data) => {
Expand Down
7 changes: 5 additions & 2 deletions test/unit/specs/data/rt.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai'
import { describe, it } from 'mocha'

import Backendless, { forTest, prepareMockRequest, createMockRTServer, Utils } from '../../helpers/sandbox'
import Backendless, { forTest, createMockRTServer, Utils } from '../../helpers/sandbox'

describe('<Data> RT', function() {

Expand All @@ -11,6 +11,7 @@ describe('<Data> RT', function() {

const tableName = 'TEST_TABLE_NAME'
const relationColumnName = 'TEST_REL_COLUMN_NAME'
const rtURL = 'http://localhost:12345'

let rtClient

Expand All @@ -20,7 +21,9 @@ describe('<Data> RT', function() {
beforeEach(async () => {
rtClient = await createMockRTServer()

prepareMockRequest(rtClient.host)
Backendless.appInfoPromise = chai.spy(() =>
Promise.resolve({ rtURL })
)

dataStore = Backendless.Data.of(tableName)
rtHandlers = dataStore.rt()
Expand Down
4 changes: 2 additions & 2 deletions test/unit/specs/files/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,14 +689,14 @@ describe('<Files> Basic', function() {

expect(req1).to.deep.include({
method : 'POST',
path : `${APP_PATH}/files/test/path/`,
path : `${APP_PATH}/files/test/path`,
headers: {},
body : undefined
})

expect(req2).to.deep.include({
method : 'POST',
path : `${APP_PATH}/files/test/path/`,
path : `${APP_PATH}/files/test/path`,
headers: {},
body : undefined
})
Expand Down
48 changes: 28 additions & 20 deletions test/unit/specs/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,16 +570,20 @@ describe('<Logging>', function() {
})

it('should apply log levels to logging config', async () => {
prepareMockRequest([
{
name : loggerName,
level: 'FATAL'
},
{
name : 'Global logger',
level: 'ERROR'
}
])
Backendless.appInfoPromise = chai.spy(() =>
Promise.resolve({
loggers: [
{
name : loggerName,
level: 'FATAL'
},
{
name : 'Global logger',
level: 'ERROR'
}
]
})
)

Backendless.initApp({
appId : APP_ID,
Expand Down Expand Up @@ -620,16 +624,20 @@ describe('<Logging>', function() {
})

it('should ignore all logs when global is OFF', async () => {
prepareMockRequest([
{
name : loggerName,
level: 'INFO'
},
{
name : 'Global logger',
level: 'OFF'
}
])
Backendless.appInfoPromise = chai.spy(() =>
Promise.resolve({
loggers: [
{
name : loggerName,
level: 'INFO'
},
{
name : 'Global logger',
level: 'OFF'
}
]
})
)

Backendless.initApp({
appId : APP_ID,
Expand Down
5 changes: 4 additions & 1 deletion test/unit/specs/messaging/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('<Messaging> Channel', function() {

const channelName = 'TEST_CHANNEL_NAME'
const message = 'TEST_MESSAGE'
const rtURL = 'http://localhost:12345'

const fakeResult = { foo: 123 }

Expand All @@ -22,7 +23,9 @@ describe('<Messaging> Channel', function() {
beforeEach(async () => {
rtClient = await createMockRTServer()

prepareMockRequest(rtClient.host)
Backendless.appInfoPromise = chai.spy(() =>
Promise.resolve({ rtURL })
)

channel = Backendless.Messaging.subscribe(channelName)

Expand Down
Loading