Fix fetch to work in the edge and node environments, cleanup type issues
- Fix "TypeError: Invalid redirect value, must be one of "follow" or "manual" ("error" won't be implemented since it does not make sense at the edge; use "manual" and check the response status code)." that is thrown when trying to use fetch in the edge environment (e.g., workers) - Cleanup types and variable definitions.
This commit is contained in:
parent
c1d03cf00b
commit
94f841f347
26
nip05.ts
26
nip05.ts
|
@ -12,20 +12,24 @@ export type Nip05 = `${string}@${string}`
|
||||||
export const NIP05_REGEX = /^(?:([\w.+-]+)@)?([\w_-]+(\.[\w_-]+)+)$/
|
export const NIP05_REGEX = /^(?:([\w.+-]+)@)?([\w_-]+(\.[\w_-]+)+)$/
|
||||||
export const isNip05 = (value?: string | null): value is Nip05 => NIP05_REGEX.test(value || '')
|
export const isNip05 = (value?: string | null): value is Nip05 => NIP05_REGEX.test(value || '')
|
||||||
|
|
||||||
var _fetch: any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
let _fetch: any
|
||||||
|
|
||||||
try {
|
try {
|
||||||
_fetch = fetch
|
_fetch = fetch
|
||||||
} catch {}
|
} catch (_) {null}
|
||||||
|
|
||||||
export function useFetchImplementation(fetchImplementation: any) {
|
export function useFetchImplementation(fetchImplementation: unknown) {
|
||||||
_fetch = fetchImplementation
|
_fetch = fetchImplementation
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function searchDomain(domain: string, query = ''): Promise<{ [name: string]: string }> {
|
export async function searchDomain(domain: string, query = ''): Promise<{ [name: string]: string }> {
|
||||||
try {
|
try {
|
||||||
const url = `https://${domain}/.well-known/nostr.json?name=${query}`
|
const url = `https://${domain}/.well-known/nostr.json?name=${query}`
|
||||||
const res = await _fetch(url, { redirect: 'error' })
|
const res = await _fetch(url, { redirect: 'manual' })
|
||||||
|
if (res.status !== 200) {
|
||||||
|
throw Error("Wrong response code")
|
||||||
|
}
|
||||||
const json = await res.json()
|
const json = await res.json()
|
||||||
return json.names
|
return json.names
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
|
@ -37,20 +41,24 @@ export async function queryProfile(fullname: string): Promise<ProfilePointer | n
|
||||||
const match = fullname.match(NIP05_REGEX)
|
const match = fullname.match(NIP05_REGEX)
|
||||||
if (!match) return null
|
if (!match) return null
|
||||||
|
|
||||||
const [_, name = '_', domain] = match
|
const [, name = '_', domain] = match
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const url = `https://${domain}/.well-known/nostr.json?name=${name}`
|
const url = `https://${domain}/.well-known/nostr.json?name=${name}`
|
||||||
const res = await (await _fetch(url, { redirect: 'error' })).json()
|
const res = await _fetch(url, { redirect: 'manual' })
|
||||||
|
if (res.status !== 200) {
|
||||||
|
throw Error("Wrong response code")
|
||||||
|
}
|
||||||
|
const json = await res.json()
|
||||||
|
|
||||||
let pubkey = res.names[name]
|
const pubkey = json.names[name]
|
||||||
return pubkey ? { pubkey, relays: res.relays?.[pubkey] } : null
|
return pubkey ? { pubkey, relays: json.relays?.[pubkey] } : null
|
||||||
} catch (_e) {
|
} catch (_e) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function isValid(pubkey: string, nip05: Nip05): Promise<boolean> {
|
export async function isValid(pubkey: string, nip05: Nip05): Promise<boolean> {
|
||||||
let res = await queryProfile(nip05)
|
const res = await queryProfile(nip05)
|
||||||
return res ? res.pubkey === pubkey : false
|
return res ? res.pubkey === pubkey : false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue