Skip to content

Commit

Permalink
BL-23 - Support setting automation URL (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-grin authored Mar 26, 2024
1 parent 77cf3b5 commit e805464
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
3 changes: 3 additions & 0 deletions backendless.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ declare module Backendless {

let debugMode: boolean;
let serverURL: string;
let automationServerURL: string;
let appId: string;
let apiKey: string;
let appPath: string;
let automationPath: string;
let domain: string;
let apiURI: string;
let XMLHttpRequest: any;
Expand All @@ -35,6 +37,7 @@ declare module Backendless {
apiKey?: string;
standalone?: boolean;
serverURL?: string;
automationServerURL?: string;
domain?: string;
debugMode?: boolean;
XMLHttpRequest?: XMLHttpRequest;
Expand Down
44 changes: 34 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import Utils from './utils'
import Expression from './expression'

const DEFAULT_PROPS = {
appId : null,
apiKey : null,
serverURL : 'https://api.backendless.com',
domain : null,
apiURI : '/api',
debugMode : false,
standalone : false,
XMLHttpRequest: typeof XMLHttpRequest !== 'undefined'
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,
}
Expand Down Expand Up @@ -226,8 +227,17 @@ class Backendless {
return this.__serverURL
}

set serverURL(serverURL) {
this.__serverURL = serverURL
set serverURL(url) {
this.__serverURL = url
}

///--------automationServerURL-------///
get automationServerURL() {
return this.__automationServerURL
}

set automationServerURL(url) {
this.__automationServerURL = url
}

///--------domain-------///
Expand Down Expand Up @@ -264,6 +274,20 @@ class Backendless {
)
}

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
Expand Down
2 changes: 1 addition & 1 deletion src/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class Urls {
//automations

automation() {
return `${this.root()}/automation`
return `${this.app.automationPath}/automation`
}

automationFlow() {
Expand Down
32 changes: 32 additions & 0 deletions test/unit/specs/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ describe('Namespace', function() {
expect(() => Backendless.appId = 'appId').to.throw()
expect(() => Backendless.apiKey = 'apiKey').to.throw()
expect(() => Backendless.appPath = 'appPath').to.throw()
expect(() => Backendless.automationPath = 'automationPath').to.throw()
expect(() => Backendless.standalone = 'standalone').to.throw()
expect(() => Backendless.device = 'device').to.throw()

expect(Backendless.appId).to.be.equal(APP_ID)
expect(Backendless.apiKey).to.be.equal(API_KEY)
expect(Backendless.appPath).to.be.equal(`http://foo.bar/${APP_ID}/${API_KEY}`)
expect(Backendless.automationPath).to.be.equal(`http://foo.bar/${APP_ID}/${API_KEY}`)
})
})

Expand Down Expand Up @@ -70,6 +72,32 @@ describe('Namespace', function() {
expect(app1.apiKey).to.be.equal('apiKey-1')
expect(app1.appPath).to.be.equal('http://my-server-url.com/appId-1/apiKey-1')
})

it('has custom automationServerURL', () => {
const app1 = Backendless.initApp({
automationServerURL : 'http://my-automation-server-url.com',
appId : 'appId-1',
apiKey : 'apiKey-1',
standalone: true
})

expect(app1.appId).to.be.equal('appId-1')
expect(app1.apiKey).to.be.equal('apiKey-1')
expect(app1.automationPath).to.be.equal('http://my-automation-server-url.com/appId-1/apiKey-1')
})

it('should take default app path as automationServerURL when no value set', () => {
const app1 = Backendless.initApp({
serverURL : 'http://my-server-url.com',
appId : 'appId-1',
apiKey : 'apiKey-1',
standalone: true
})

expect(app1.appId).to.be.equal('appId-1')
expect(app1.apiKey).to.be.equal('apiKey-1')
expect(app1.automationPath).to.be.equal('http://my-server-url.com/appId-1/apiKey-1')
})
})

describe('Custom Domain', () => {
Expand All @@ -81,6 +109,7 @@ describe('Namespace', function() {
expect(Backendless.apiKey).to.be.equal(null)
expect(Backendless.domain).to.be.equal(CUSTOM_DOMAIN)
expect(Backendless.appPath).to.be.equal(`${CUSTOM_DOMAIN}/api`)
expect(Backendless.automationPath).to.be.equal(`${CUSTOM_DOMAIN}/api`)
expect(Backendless.apiURI).to.be.equal('/api')
})

Expand All @@ -91,6 +120,7 @@ describe('Namespace', function() {
expect(Backendless.apiKey).to.be.equal(null)
expect(Backendless.domain).to.be.equal(CUSTOM_DOMAIN)
expect(Backendless.appPath).to.be.equal(`${CUSTOM_DOMAIN}/api`)
expect(Backendless.automationPath).to.be.equal(`${CUSTOM_DOMAIN}/api`)
expect(Backendless.apiURI).to.be.equal('/api')
})

Expand All @@ -100,6 +130,7 @@ describe('Namespace', function() {

expect(Backendless.domain).to.be.equal(CUSTOM_DOMAIN)
expect(Backendless.appPath).to.be.equal(`${CUSTOM_DOMAIN}/my-api-uri`)
expect(Backendless.automationPath).to.be.equal(`${CUSTOM_DOMAIN}/my-api-uri`)
expect(Backendless.apiURI).to.be.equal('/my-api-uri')

Backendless.apiURI = undefined
Expand Down Expand Up @@ -373,6 +404,7 @@ describe('Namespace', function() {
expect(Backendless.apiKey).to.be.equal(null)
expect(Backendless.domain).to.be.equal('https://foo.com')
expect(Backendless.appPath).to.be.equal('https://foo.com/api')
expect(Backendless.automationPath).to.be.equal('https://foo.com/api')
expect(Backendless.apiURI).to.be.equal('/api')

const appIdWarnMsg = 'getter/setter for Backendless.applicationId is deprecated, instead use Backendless.appId'
Expand Down

0 comments on commit e805464

Please sign in to comment.