feat: improved relay-info job to return object

This commit is contained in:
Yury 2024-05-23 14:06:08 +03:00
parent d67d53b172
commit eba2472f2f

View File

@ -21,21 +21,21 @@ export const relayInfoJob = async (event: NDKEvent): Promise<string> => {
relays.push(input)
}
const wssPrefix = 'wss://'
const prefixes = { wss: 'wss://', https: 'https://', http: 'http://' }
const headers = {
headers: { Accept: 'application/nostr+json' }
}
const requests = relays.map((relay) => {
if (relay.startsWith(wssPrefix)) {
relay = relay.replace(wssPrefix, '')
if (relay.startsWith(prefixes.wss)) {
relay = relay.replace(prefixes.wss, '')
}
return axios
.get<RelayInfo>((relay = `https://${relay}`), headers)
.get<RelayInfo>((relay = `${prefixes.https}${relay}`), headers)
.catch(() => {
return axios
.get<RelayInfo>(`http://${relay}`, headers)
.get<RelayInfo>(`${prefixes.http}${relay}`, headers)
.catch(() => undefined)
})
})
@ -43,7 +43,15 @@ export const relayInfoJob = async (event: NDKEvent): Promise<string> => {
let responses = await Promise.all(requests)
responses = responses.filter((response) => response !== undefined)
const data = responses.map((response) => response.data)
const relaysInfo: { [key: string]: RelayInfo } = {}
return Promise.resolve(JSON.stringify(data))
responses.forEach((response) => {
const relayURI = response.config.url
.replace(prefixes.https, prefixes.wss)
.replace(prefixes.http, prefixes.wss)
relaysInfo[relayURI] = response.data
})
return Promise.resolve(JSON.stringify(relaysInfo))
}