-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #858 from gouravmpk/apiUrlFromSwarmHost
moving apiFromSwarmHost
- Loading branch information
Showing
5 changed files
with
65 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { apiUrlFromSwarmHost } from '../index.ts' | ||
|
||
function mockWindowLocation(url) { | ||
delete global.window.location | ||
global.window = Object.create(window) | ||
global.window.location = new URL(url) | ||
global.window.host = global.window.location.host | ||
global.window.origin = global.window.location.origin | ||
} | ||
|
||
describe('apiUrlFromSwarmHost', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
it('returns "https://knowledge-graph.sphinx.chat/api" for the URL "https://second-brain.sphinx.chat"', () => { | ||
mockWindowLocation('https://second-brain.sphinx.chat') | ||
expect(apiUrlFromSwarmHost()).toBe('https://knowledge-graph.sphinx.chat/api') | ||
}) | ||
|
||
it('returns "https://knowledge-graph.sphinx.chat/api" for a URL containing "localhost"', () => { | ||
mockWindowLocation('http://localhost:3000') | ||
expect(apiUrlFromSwarmHost()).toBe('https://knowledge-graph.sphinx.chat/api') | ||
}) | ||
|
||
it('returns the original URL appended with /api for a URL not containing "swarm" and not hardcoded', () => { | ||
const nonSwarmUrl = 'https://knowledge-graph.sphinx.chat' | ||
|
||
mockWindowLocation(nonSwarmUrl) | ||
expect(apiUrlFromSwarmHost()).toBe(`${nonSwarmUrl}/api`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const { origin, host } = window.location | ||
|
||
const getUrlFormEnv = () => import.meta.env.VITE_APP_API_URL | ||
|
||
export const API_URL = getUrlFormEnv() || apiUrlFromSwarmHost() || 'https://knowledge-graph.sphinx.chat' | ||
|
||
export function apiUrlFromSwarmHost(): string | undefined { | ||
// for swarm deployments, always point to "boltwall" | ||
// for now, only if the URL contains "swarm" | ||
const originUrl = window.location.origin | ||
|
||
let url = originUrl | ||
|
||
if (host.includes('swarm')) { | ||
if (host.startsWith('nav')) { | ||
const hostArray = host.split('.') | ||
|
||
hostArray[0] = 'boltwall' | ||
|
||
const finalHost = hostArray.join('.') | ||
|
||
url = `https://${finalHost}` | ||
} | ||
} else if (originUrl === 'https://second-brain.sphinx.chat' || origin.includes('localhost')) { | ||
url = 'https://knowledge-graph.sphinx.chat' | ||
} | ||
|
||
return `${url}/api` | ||
} |