Skip to content

Commit 4e6dca4

Browse files
committed
feat: allow multiple api hosts (#55)
Do not assume a host in redirects. Retrieve host for token. Add tests.
1 parent 9b02bba commit 4e6dca4

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

src/index.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,20 @@ module.exports = {
9292
}
9393
} else {
9494
try {
95-
await utils.run.command(
96-
`nim auth login ${process.env.NIMBELLA_LOGIN_TOKEN}`
97-
)
95+
if (
96+
process.env.NIMBELLA_API_HOST &&
97+
process.env.NIMBELLA_API_HOST !== ''
98+
) {
99+
await utils.run.command(
100+
`nim auth login ${process.env.NIMBELLA_LOGIN_TOKEN} --apihost ${process.env.NIMBELLA_API_HOST}`
101+
)
102+
} else {
103+
// Delegate to default apihost configured in the cli.
104+
await utils.run.command(
105+
`nim auth login ${process.env.NIMBELLA_LOGIN_TOKEN}`
106+
)
107+
}
108+
98109
// Cache the nimbella config to avoid logging in for consecutive builds.
99110
await utils.cache.save(nimConfig)
100111
} catch (error) {
@@ -172,6 +183,10 @@ module.exports = {
172183
const redirectRules = []
173184
const redirects = []
174185
const redirectsFile = join(constants.PUBLISH_DIR, '_redirects')
186+
let {stdout: apihost} = await utils.run.command(
187+
`nim auth current --apihost`
188+
)
189+
apihost = apihost.replace(/^(https:\/\/)/, '')
175190

176191
if (isActions) {
177192
if (existsSync(redirectsFile)) {
@@ -198,7 +213,7 @@ module.exports = {
198213
) {
199214
const redirectPath = redirect.to.split('/.netlify/functions/')[1]
200215
redirectRules.push(
201-
`${redirect.from} https://apigcp.nimbella.io/api/v1/web/${namespace}/default/${redirectPath} 200!`
216+
`${redirect.from} https://${apihost}/api/v1/web/${namespace}/default/${redirectPath} 200!`
202217
)
203218
}
204219
}
@@ -211,13 +226,13 @@ module.exports = {
211226

212227
if (isProject) {
213228
redirectRules.push(
214-
`${redirectPath}* https://apigcp.nimbella.io/api/v1/web/${namespace}/:splat 200!`
229+
`${redirectPath}* https://${apihost}/api/v1/web/${namespace}/:splat 200!`
215230
)
216231
}
217232

218233
if (isActions && !isProject) {
219234
redirectRules.push(
220-
`${redirectPath}* https://apigcp.nimbella.io/api/v1/web/${namespace}/default/:splat 200!`
235+
`${redirectPath}* https://${apihost}/api/v1/web/${namespace}/default/:splat 200!`
221236
)
222237
}
223238

test/index.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path')
22

3+
const fs = require('fs')
34
const mockFs = require('mock-fs')
45
const build = require('netlify-lambda/lib/build')
56

@@ -33,6 +34,51 @@ afterEach(() => {
3334
})
3435

3536
describe('preBuild()', () => {
37+
test('Login to default api host', async () => {
38+
// Prepare
39+
process.env.NIMBELLA_LOGIN_TOKEN = 'somevalue'
40+
delete process.env.NIMBELLA_API_HOST
41+
utils.cache.has.mockResolvedValue(false)
42+
43+
const mockFiles = {
44+
[require('os').homedir()]: {}
45+
}
46+
mockFs(mockFiles)
47+
await plugin.onPreBuild({
48+
utils,
49+
constants: {},
50+
inputs: {}
51+
})
52+
mockFs.restore()
53+
54+
expect(utils.run.command.mock.calls[0][0]).toEqual(
55+
`nim auth login somevalue`
56+
)
57+
})
58+
59+
test('Login to specified api host', async () => {
60+
// Prepare
61+
process.env.NIMBELLA_LOGIN_TOKEN = 'somevalue'
62+
process.env.NIMBELLA_API_HOST = 'somehost'
63+
utils.cache.has.mockResolvedValue(false)
64+
65+
const mockFiles = {
66+
[require('os').homedir()]: {}
67+
}
68+
69+
mockFs(mockFiles)
70+
await plugin.onPreBuild({
71+
utils,
72+
constants: {},
73+
inputs: {}
74+
})
75+
mockFs.restore()
76+
77+
expect(utils.run.command.mock.calls[0][0]).toEqual(
78+
`nim auth login somevalue --apihost somehost`
79+
)
80+
})
81+
3682
test('show token not available message when login token not set', async () => {
3783
// Prepare
3884
process.env.NIMBELLA_LOGIN_TOKEN = ''
@@ -173,9 +219,12 @@ describe('onPostBuild()', () => {
173219
test('should rewrite existing redirects to .netlify/functions/ in netlify.toml if functions are used', async () => {
174220
process.env.NIMBELLA_LOGIN_TOKEN = 'somevalue'
175221
process.env.CONTEXT = 'production'
176-
utils.run.command.mockReturnValue({
177-
stdout: 'namespace'
222+
utils.run.command = jest.fn((cmd) => {
223+
if (cmd === 'nim auth current') return {stdout: 'namespace'}
224+
if (cmd === 'nim auth current --apihost') return {stdout: 'somehost'}
225+
return {stdout: '???'}
178226
})
227+
179228
const pluginInputs = {
180229
utils,
181230
constants: {CONFIG_PATH: 'netlify.toml', PUBLISH_DIR: ''},
@@ -199,9 +248,15 @@ describe('onPostBuild()', () => {
199248
})
200249
await plugin.onPreBuild(pluginInputs)
201250
await plugin.onPostBuild(pluginInputs)
251+
const redirects = String(fs.readFileSync('_redirects'))
202252
mockFs.restore()
253+
203254
expect(console.log.mock.calls[1][0]).toEqual(
204255
"Found redirect rules in netlify.toml. We will rewrite rules that redirect (200 rewrites) to '/.netlify/functions/*'."
205256
)
257+
258+
expect(redirects).toEqual(
259+
'/* https://somehost/api/v1/web/namespace/default/:splat 200!'
260+
)
206261
})
207262
})

0 commit comments

Comments
 (0)