From 2c0b3b7ee1736a14cc92d42d737c4f0e9b7d5a91 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Sun, 11 Jul 2021 12:01:30 +0200 Subject: [PATCH] Make URLs configurable pass in `createInvoiceUrl`, `watchPaymentUrl`, `newAddressUrl` functions. Those must return the URL as string that is used for the API calls --- files/assets/lnme.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/files/assets/lnme.js b/files/assets/lnme.js index 01569c8..f1bd42e 100644 --- a/files/assets/lnme.js +++ b/files/assets/lnme.js @@ -57,12 +57,33 @@ class LnMe { } else { this.baseURL = `${document.location.protocol}//${document.location.host}`; } + if (options.watchPaymentUrl) { + this.watchPaymentUrl = options.watchPaymentUrl.bind(this); + } + if (options.createInvoiceUrl) { + this.createInvoiceUrl = options.createInvoiceUrl.bind(this); + } + if (options.newAddressUrl) { + this.newAddressUrl = options.newAddressUrl.bind(this); + } this.value = parseInt(options.value || 0); this.memo = options.memo || ''; this.target = options.target; this.loadStylesheet(); // load it early that styles are ready when the popup is opened } + watchPaymentUrl() { + return `${this.baseURL}/v1/invoice/${this.invoice.payment_hash}`; + } + + createInvoiceUrl() { + return `${this.baseURL}/v1/invoices`; + } + + newAddressUrl() { + return `${this.baseURL}/v1/newaddress`; + } + loadStylesheet() { if (document.getElementById('lnme-style')) { return; } // get the CSS file from the same source as the JS widget file @@ -81,7 +102,7 @@ class LnMe { return new Promise((resolve, reject) => { this.paymentWatcher = window.setInterval(() => { - this._fetch(`${this.baseURL}/v1/invoice/${this.invoice.payment_hash}`) + this._fetch(this.watchPaymentUrl()) .then((invoice) => { if (invoice.settled) { this.invoice.settled = true; @@ -147,7 +168,7 @@ class LnMe { body: JSON.stringify({ memo: this.memo, value: this.value }) }; return this._fetch( - `${this.baseURL}/v1/invoices`, + this.createInvoiceUrl(), args ).then((invoice) => { this.invoice = invoice; @@ -161,7 +182,7 @@ class LnMe { mode: 'cors', header: { 'Content-Type': 'application/json' } }; - return this._fetch(`${this.baseURL}/v1/newaddress`, args) + return this._fetch(this.newAddressUrl(), args) .then(address => { this.address = address; return address;