nbd-wtf / nostr-tools

Tools for developing Nostr clients.
The Unlicense
705 stars 197 forks source link

"bad" example for the use of queryProfile function #348

Closed kafeltz closed 3 months ago

kafeltz commented 9 months ago

It took me a while of debugging to understand that nip05.queryProfile does not work for NodeJS.

But it's possible to make it work, so, I propose in the readme a showcase how to do it in a simple way.

Check my pull-request for readme changes: https://github.com/nbd-wtf/nostr-tools/pull/347

Plus, I also do propose to change the implementation of queryProfile:

export async function queryProfile(fullname: string): Promise<ProfilePointer | null> {
  const match = fullname.match(NIP05_REGEX)
  if (!match) return null

  const [_, name = '_', domain] = match

  try {
    const res = await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
    const { names, relays } = parseNIP05Result(await res.json())

    const pubkey = names[name]
    return pubkey ? { pubkey, relays: relays?.[pubkey] } : null
  } catch (_e) {
    return null
  }
}

Remove the Try/Catch so it explodes and I can see what's the issue, because "null" tells me nothing. And, never return "null", change it for either returns: Promise<ProfilePointer | MyError> or Promise (with throw new Error).

fiatjaf commented 9 months ago

I agree with removing the try..catch.