diff --git a/src/lightning-address.ts b/src/lightning-address.ts index 3409af7..3e52b3e 100644 --- a/src/lightning-address.ts +++ b/src/lightning-address.ts @@ -82,26 +82,31 @@ export default class LightningAddress { if (!this.domain || !this.username) { return; } - const lnurlResult = await fetch(this.lnurlpUrl()); - const keysendResult = await fetch(this.keysendUrl()); - const nostrResult = await fetch(this.nostrUrl()); - let lnurlData: LnUrlRawData | undefined; - if (lnurlResult.ok) { - lnurlData = await lnurlResult.json(); - } - let keysendData: KeySendRawData | undefined; - if (keysendResult.ok) { - keysendData = await keysendResult.json(); - } - let nostrData: NostrResponse | undefined; - if (nostrResult.ok) { - nostrData = await nostrResult.json(); - } + const [lnurlData, keysendData, nostrData] = await Promise.all([ + this.requestLnurlData(), + this.requestKeysendData(), + this.requestNostrData(), + ]); await this.parseResponse(lnurlData, keysendData, nostrData); } + async fetchLnurlData() { + const lnurlData = await this.requestLnurlData(); + await this.parseResponse(lnurlData, undefined, undefined); + } + + async fetchKeysendData() { + const keysendData = await this.requestKeysendData(); + await this.parseResponse(undefined, keysendData, undefined); + } + + async fetchNostrData() { + const nostrData = await this.requestNostrData(); + await this.parseResponse(undefined, undefined, nostrData); + } + lnurlpUrl() { return `https://${this.domain}/.well-known/lnurlp/${this.username}`; } @@ -249,6 +254,27 @@ export default class LightningAddress { return response; } + private async requestLnurlData(): Promise { + const lnurlResult = await fetch(this.lnurlpUrl()); + if (lnurlResult.ok) { + return lnurlResult.json(); + } + } + + private async requestKeysendData(): Promise { + const keysendResult = await fetch(this.keysendUrl()); + if (keysendResult.ok) { + return keysendResult.json(); + } + } + + private async requestNostrData(): Promise { + const nostrResult = await fetch(this.nostrUrl()); + if (nostrResult.ok) { + return nostrResult.json(); + } + } + private async parseResponse( lnurlpData: LnUrlRawData | undefined, keysendData: KeySendRawData | undefined,