From 94f841f347c800a7140b946d66b7c5cc16454cbd Mon Sep 17 00:00:00 2001 From: Fishcake <128653975+fishcakeday@users.noreply.github.com> Date: Sun, 20 Oct 2024 14:23:01 +0900 Subject: [PATCH] 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. --- nip05.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/nip05.ts b/nip05.ts index 2922453..a1e11d6 100644 --- a/nip05.ts +++ b/nip05.ts @@ -12,20 +12,24 @@ export type Nip05 = `${string}@${string}` export const NIP05_REGEX = /^(?:([\w.+-]+)@)?([\w_-]+(\.[\w_-]+)+)$/ 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 { _fetch = fetch -} catch {} +} catch (_) {null} -export function useFetchImplementation(fetchImplementation: any) { +export function useFetchImplementation(fetchImplementation: unknown) { _fetch = fetchImplementation } export async function searchDomain(domain: string, query = ''): Promise<{ [name: string]: string }> { try { 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() return json.names } catch (_) { @@ -37,20 +41,24 @@ export async function queryProfile(fullname: string): Promise { - let res = await queryProfile(nip05) + const res = await queryProfile(nip05) return res ? res.pubkey === pubkey : false }