Skip to content

Commit

Permalink
feat: allow configuration of the API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinEberhardt committed Feb 27, 2019
1 parent cde3ba9 commit 72f7b60
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/applause-button.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import "document-register-element/build/document-register-element";

const VERSION = "3.0.0";
const VERSION = "3.3.0";
const API = "https://api.applause-button.com";

const getClaps = url =>
const getClaps = (api, url) =>
// TODO: polyfill for IE (not edge)
fetch(`${API}/get-claps` + (url ? `?url=${url}` : ""), {
fetch(`${api}/get-claps` + (url ? `?url=${url}` : ""), {
headers: {
"Content-Type": "text/plain"
}
}).then(response => response.text());

const updateClaps = (claps, url) =>
const updateClaps = (api, claps, url) =>
// TODO: polyfill for IE (not edge)
fetch(`${API}/update-claps` + (url ? `?url=${url}` : ""), {
fetch(`${api}/update-claps` + (url ? `?url=${url}` : ""), {
method: "POST",
headers: {
"Content-Type": "text/plain"
Expand Down Expand Up @@ -112,7 +112,7 @@ class ApplauseButton extends HTMLCustomElement {
this._bufferedClaps,
MAX_MULTI_CLAP - this._totalClaps
);
updateClaps(increment, this.url);
updateClaps(this.api, increment, this.url);
this._totalClaps += increment;
this._bufferedClaps = 0;
}
Expand Down Expand Up @@ -162,7 +162,7 @@ class ApplauseButton extends HTMLCustomElement {
}
});

getClaps(this.url).then(claps => {
getClaps(this.api, this.url).then(claps => {
this.classList.remove("loading");
const clapCount = Number(claps);
initialClapCountResolve(clapCount);
Expand All @@ -182,6 +182,18 @@ class ApplauseButton extends HTMLCustomElement {
return this.getAttribute("color");
}

set api(api) {
if (api) {
this.setAttribute("api", api);
} else {
this.removeAttribute("api");
}
}

get api() {
return this.getAttribute("api") || API;
}

set color(color) {
if (color) {
this.setAttribute("color", color);
Expand Down

0 comments on commit 72f7b60

Please sign in to comment.