Skip to content

Commit 8c75ea2

Browse files
Benedikt Rötschaxe312ger
authored andcommitted
feat(httpClient): add basePath option for gateways with custom url patterns
fixes #38
1 parent 634c685 commit 8c75ea2

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

lib/create-http-client.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export default function createHttpClient (axios, options) {
4141
httpAgent: false,
4242
httpsAgent: false,
4343
timeout: 30000,
44-
proxy: false
44+
proxy: false,
45+
basePath: ''
4546
}
4647
const config = Object.assign({}, defaultConfig, options)
4748

@@ -65,7 +66,13 @@ export default function createHttpClient (axios, options) {
6566
hostname = parsed[0]
6667
}
6768
}
68-
const baseURL = `${protocol}://${hostname}:${port}/spaces/${space}`
69+
70+
// Ensure that basePath does start but not end with a slash
71+
if (config.basePath) {
72+
config.basePath = `/${config.basePath.split('/').filter(Boolean).join('/')}`
73+
}
74+
75+
const baseURL = `${protocol}://${hostname}:${port}${config.basePath}/spaces/${space}`
6976

7077
config.headers['Authorization'] = 'Bearer ' + config.accessToken
7178

test/unit/create-http-client-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function teardown () {
1616
createHttpClientRewireApi.__ResetDependency__('rateLimit')
1717
mock.reset()
1818
axios.create.restore()
19+
logHandlerStub.resetHistory()
1920
}
2021

2122
test('Calls axios with expected default URL', t => {
@@ -97,3 +98,31 @@ test('Fails with missing access token', t => {
9798
t.end()
9899
}
99100
})
101+
102+
test('Calls axios based on passed hostname with basePath', t => {
103+
setup()
104+
createHttpClient(axios, {
105+
accessToken: 'clientAccessToken',
106+
host: 'some.random.example.com',
107+
basePath: '/foo/bar'
108+
})
109+
110+
t.equals(axios.create.args[0][0].baseURL, 'https://some.random.example.com:443/foo/bar/spaces/')
111+
t.equals(logHandlerStub.callCount, 0, 'does not log anything')
112+
teardown()
113+
t.end()
114+
})
115+
116+
test('Calls axios based on passed hostname with invalid basePath and fixes the invalid one', t => {
117+
setup()
118+
createHttpClient(axios, {
119+
accessToken: 'clientAccessToken',
120+
host: 'some.random.example.com',
121+
basePath: 'foo/bar'
122+
})
123+
124+
t.equals(axios.create.args[0][0].baseURL, 'https://some.random.example.com:443/foo/bar/spaces/')
125+
t.equals(logHandlerStub.callCount, 0, 'does not log anything')
126+
teardown()
127+
t.end()
128+
})

0 commit comments

Comments
 (0)