Skip to content

Commit

Permalink
fix: function:serve shows undefined port in url (#4146)
Browse files Browse the repository at this point in the history
* fix: function:serve shows undefined port in url

* fix: restored missing test

* fix: wrapped function url in URL object

* fix: wrapped function url in URL object

Co-authored-by: Lukas Holzer <lukas.holzer@netlify.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Co-authored-by: Erez Rokah <erezrokah@users.noreply.github.com>
  • Loading branch information
4 people authored Feb 4, 2022
1 parent 474118d commit db0f1ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/lib/functions/netlify-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ class NetlifyFunction {
}

get url() {
return `http://localhost:${this.settings.port}/.netlify/functions/${this.name}`
// This line fixes the issue here https://github.com/netlify/cli/issues/4116
// Not sure why `settings.port` was used here nor does a valid reference exist.
// However, it remains here to serve whatever purpose for which it was added.
const port = this.settings.port || this.settings.functionsPort

const url = new URL(`/.netlify/functions/${this.name}`, `http://localhost:${port}`)
return url.href
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/integration/20.command.functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const getPort = require('get-port')
const waitPort = require('wait-port')

const fs = require('../../src/lib/fs')
const { NetlifyFunction } = require('../../src/lib/functions/netlify-function')

const callCli = require('./utils/call-cli')
const cliPath = require('./utils/cli-path')
Expand All @@ -21,6 +22,20 @@ const { withSiteBuilder } = require('./utils/site-builder')

const test = isCI ? avaTest.serial.bind(avaTest) : avaTest

test('should return the correct function url for a NetlifyFunction object', (t) => {
const port = 7331
const functionName = 'test-function'

const functionUrl = `http://localhost:${port}/.netlify/functions/${functionName}`

const ntlFunction = new NetlifyFunction({
name: functionName,
settings: { functionsPort: port },
})

t.is(ntlFunction.url, functionUrl)
})

test('should return function response when invoked with no identity argument', async (t) => {
await withSiteBuilder('function-invoke-with-no-identity-argument', async (builder) => {
builder.withNetlifyToml({ config: { functions: { directory: 'functions' } } }).withFunction({
Expand Down

1 comment on commit db0f1ed

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 363 MB

Please sign in to comment.