From abf0a2ef258a4fc8fc2a47abb4f8fb6e70f50151 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Mon, 10 Feb 2025 17:07:24 +0100 Subject: A somewhat working first version. --- masto.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 masto.js (limited to 'masto.js') 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(); + } +} -- cgit v1.2.3