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) relays.push(input)
} }
const wssPrefix = 'wss://' const prefixes = { wss: 'wss://', https: 'https://', http: 'http://' }
const headers = { const headers = {
headers: { Accept: 'application/nostr+json' } headers: { Accept: 'application/nostr+json' }
} }
const requests = relays.map((relay) => { const requests = relays.map((relay) => {
if (relay.startsWith(wssPrefix)) { if (relay.startsWith(prefixes.wss)) {
relay = relay.replace(wssPrefix, '') relay = relay.replace(prefixes.wss, '')
} }
return axios return axios
.get<RelayInfo>((relay = `https://${relay}`), headers) .get<RelayInfo>((relay = `${prefixes.https}${relay}`), headers)
.catch(() => { .catch(() => {
return axios return axios
.get<RelayInfo>(`http://${relay}`, headers) .get<RelayInfo>(`${prefixes.http}${relay}`, headers)
.catch(() => undefined) .catch(() => undefined)
}) })
}) })
@ -43,7 +43,15 @@ export const relayInfoJob = async (event: NDKEvent): Promise<string> => {
let responses = await Promise.all(requests) let responses = await Promise.all(requests)
responses = responses.filter((response) => response !== undefined) 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))
} }