Skip to content

Commit

Permalink
feat: fetch versions of external components
Browse files Browse the repository at this point in the history
  • Loading branch information
mint-dewit committed Dec 4, 2023
1 parent 4539c01 commit da2fb46
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 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

0 comments on commit da2fb46

Please sign in to comment.