Skip to content

Commit

Permalink
Merge pull request #118 from nrkno/feat/ext-versions
Browse files Browse the repository at this point in the history
feat: fetch versions of external components
  • Loading branch information
mint-dewit authored Dec 6, 2023
2 parents 4539c01 + f0033e3 commit a8c077b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/controllers/serverVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ function compileVersions(component: any, prefix = ''): Versions {
Object.keys(compiledVersions).forEach((key) => (versions[key] = compiledVersions[key]))
})
}
if (component._extVersions) {
component._extVersions.forEach((child: PromiseSettledResult<any>) => {
if (child.status === 'rejected' || child.value.error) return
const compiledVersions = compileVersions(child.value, '~' + child.value.name + '.')
Object.keys(compiledVersions).forEach((key) => (versions[key] = compiledVersions[key]))
})
}
return versions
}

Expand Down
26 changes: 26 additions & 0 deletions src/lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fetch from 'node-fetch'
import { URL, URLSearchParams } from 'url'

const HEALTH_PATH = 'health'
const EXT_VERSIONS = ['external/sisyfos/health']

export async function getServerHealth(serverHost: string): Promise<any> {
if (serverHost !== '') {
Expand All @@ -26,6 +27,30 @@ export async function getServerHealth(serverHost: string): Promise<any> {
}
}

export async function getExternalVersions(serverHost: string): Promise<PromiseSettledResult<any>[]> {
const result: any[] = []
if (serverHost !== '') {
const serverLocation = /^https?:\/\//.test(serverHost) ? new URL(serverHost) : new URL(`http://${serverHost}`)

return Promise.allSettled(
EXT_VERSIONS.map(async (extUrl) => {
const url = `${serverLocation.href}${extUrl}`

try {
const response = await fetch(url, { method: 'GET' })
return await response.json()
} catch (error) {
return {
error: new Error(`Unable to fetch resource ${url}: ${error}`),
}
}
})
)
}

return result
}

/**
* Accepts a list of server hosts and returns the responses from a query to
* each server's /health service.
Expand All @@ -41,6 +66,7 @@ export async function getServerData(serverHosts: Array<string>): Promise<Array<a
return Promise.all(
serverHosts.map(async (host) => {
const serverHealth = await getServerHealth(host)
serverHealth._extVersions = await getExternalVersions(host)
serverHealth._host = host
return serverHealth
})
Expand Down
7 changes: 5 additions & 2 deletions test/serverStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ describe('GET /serverStatus', () => {
expect(dom.window.document.characterSet).toEqual('UTF-8')
dom.window.document.getElementById('versions')

expect(fetch).toHaveBeenCalledTimes(1)
expect(fetch).toHaveBeenCalledWith('http://localhost:3000/health', {
expect(fetch).toHaveBeenCalledTimes(2)
expect(fetch).toHaveBeenNthCalledWith(1, 'http://localhost:3000/health', {
method: 'GET',
})
expect(fetch).toHaveBeenNthCalledWith(2, 'http://localhost:3000/external/sisyfos/health', {
method: 'GET',
})
})
Expand Down
7 changes: 5 additions & 2 deletions test/serverVersions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ describe('GET /serverVersions', () => {
expect(dom.window.document.characterSet).toEqual('UTF-8')
dom.window.document.getElementById('versions')

expect(fetch).toHaveBeenCalledTimes(1)
expect(fetch).toHaveBeenCalledWith('http://localhost:3000/health', {
expect(fetch).toHaveBeenCalledTimes(2)
expect(fetch).toHaveBeenNthCalledWith(1, 'http://localhost:3000/health', {
method: 'GET',
})
expect(fetch).toHaveBeenNthCalledWith(2, 'http://localhost:3000/external/sisyfos/health', {
method: 'GET',
})
})
Expand Down

0 comments on commit a8c077b

Please sign in to comment.