28 lines
889 B
JavaScript
28 lines
889 B
JavaScript
import os from 'node:os'
|
|
|
|
/**
|
|
* Get the private IP addr of the dev machine to use as the API url.
|
|
* This is preferred to using localhost or 0.0.0.0 because it allows
|
|
* us to use the dev client from other devices (i.e. phones)
|
|
* @returns {string}
|
|
*/
|
|
export function getLocalApiUrl() {
|
|
const addresses = Object.values(os.networkInterfaces())
|
|
.flat()
|
|
.filter((addr) => !addr.internal)
|
|
.filter((addr) => addr.family === 'IPv4')
|
|
.map((addr) => addr.address)
|
|
|
|
let address = addresses.find((addr) => addr.startsWith('192.168')) ?? addresses.at(0)
|
|
|
|
if (address === undefined) {
|
|
console.warn("Couldn't identify the local address for the server. falling back to localhost")
|
|
address = 'localhost'
|
|
}
|
|
|
|
if (addresses.length > 1) {
|
|
console.warn(`chose API URL ${address} from possible choices: ${addresses.join(', ')}`)
|
|
}
|
|
|
|
return `http://${address}:7295`
|
|
}
|