diff options
Diffstat (limited to 'masto.js')
-rw-r--r-- | masto.js | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/masto.js b/masto.js new file mode 100644 index 0000000..773ba9a --- /dev/null +++ b/masto.js @@ -0,0 +1,34 @@ +export class Mastodon { + constructor(instance, token) { + if (instance.indexOf('://') == -1) { + instance = 'https://' + instance; + } + this.instance = instance; + this.token = token; + } + + async get(endpoint, options) { + let url = this.instance + endpoint + '?' + new URLSearchParams(options); + const res = await fetch(url, { + headers: { + 'Authorization': 'Bearer ' + this.token, + }, + }); + if (res.status == 401) { + throw 401; + } + return res.json(); + } + + async post(endpoint, body) { + const fd = new FormData(); + for (let key in body) { + fd.append(key, body[key]); + } + const res = await fetch(this.instance + endpoint, { + method: 'POST', + body: fd, + }); + return res.json(); + } +} |