1
1
mirror of https://github.com/bumi/lntip synced 2025-06-16 17:55:35 +00:00

Make URLs configurable

pass in `createInvoiceUrl`, `watchPaymentUrl`, `newAddressUrl` functions. Those must return the URL as string that is used for the API calls
This commit is contained in:
bumi 2021-07-11 12:01:30 +02:00
parent a87eb60051
commit 2c0b3b7ee1

View File

@ -57,12 +57,33 @@ class LnMe {
} else { } else {
this.baseURL = `${document.location.protocol}//${document.location.host}`; 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.value = parseInt(options.value || 0);
this.memo = options.memo || ''; this.memo = options.memo || '';
this.target = options.target; this.target = options.target;
this.loadStylesheet(); // load it early that styles are ready when the popup is opened 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() { loadStylesheet() {
if (document.getElementById('lnme-style')) { return; } if (document.getElementById('lnme-style')) { return; }
// get the CSS file from the same source as the JS widget file // get the CSS file from the same source as the JS widget file
@ -81,7 +102,7 @@ class LnMe {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.paymentWatcher = window.setInterval(() => { this.paymentWatcher = window.setInterval(() => {
this._fetch(`${this.baseURL}/v1/invoice/${this.invoice.payment_hash}`) this._fetch(this.watchPaymentUrl())
.then((invoice) => { .then((invoice) => {
if (invoice.settled) { if (invoice.settled) {
this.invoice.settled = true; this.invoice.settled = true;
@ -147,7 +168,7 @@ class LnMe {
body: JSON.stringify({ memo: this.memo, value: this.value }) body: JSON.stringify({ memo: this.memo, value: this.value })
}; };
return this._fetch( return this._fetch(
`${this.baseURL}/v1/invoices`, this.createInvoiceUrl(),
args args
).then((invoice) => { ).then((invoice) => {
this.invoice = invoice; this.invoice = invoice;
@ -161,7 +182,7 @@ class LnMe {
mode: 'cors', mode: 'cors',
header: { 'Content-Type': 'application/json' } header: { 'Content-Type': 'application/json' }
}; };
return this._fetch(`${this.baseURL}/v1/newaddress`, args) return this._fetch(this.newAddressUrl(), args)
.then(address => { .then(address => {
this.address = address; this.address = address;
return address; return address;