feat: client HTTP minimal pour l'API Mastodon de bokante
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
'use strict';
|
||||
|
||||
function getConfig() {
|
||||
return {
|
||||
instanceUrl: process.env.BOKANTE_INSTANCE_URL || 'https://bokante.o-k-i.net',
|
||||
accessToken: process.env.BOKANTE_ACCESS_TOKEN
|
||||
};
|
||||
}
|
||||
|
||||
async function mastodonFetch(url, options) {
|
||||
const {accessToken} = getConfig();
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
...options.headers
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const body = await response.text();
|
||||
throw new Error(`Mastodon ${response.status}: ${body}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function createStatus(text, {visibility = 'public'} = {}) {
|
||||
const {instanceUrl} = getConfig();
|
||||
return mastodonFetch(`${instanceUrl}/api/v1/statuses`, {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({status: text, visibility})
|
||||
});
|
||||
}
|
||||
|
||||
async function getStatusContext(statusId) {
|
||||
const {instanceUrl} = getConfig();
|
||||
return mastodonFetch(`${instanceUrl}/api/v1/statuses/${statusId}/context`, {
|
||||
method: 'GET'
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {createStatus, getStatusContext};
|
||||
Reference in New Issue
Block a user