nostr-tools icon indicating copy to clipboard operation
nostr-tools copied to clipboard

"bad" example for the use of queryProfile function

Open kafeltz opened this issue 1 year ago • 1 comments

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<ProfilePointer> (with throw new Error).

kafeltz avatar Jan 14 '24 14:01 kafeltz