gotosocial/web/source/settings-panel/lib/api/index.js

122 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-09-11 17:41:29 +01:00
/*
GoToSocial
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
const Promise = require("bluebird");
const { APIError } = require("../errors");
const { setInstanceInfo } = require("../../redux/reducers/instances").actions;
2022-09-11 18:05:06 +01:00
const oauth = require("../../redux/reducers/oauth").actions;
2022-09-11 18:42:13 +01:00
function apiCall(method, route, payload, type="json") {
2022-09-11 18:05:06 +01:00
return function (dispatch, getState) {
const state = getState();
let base = state.oauth.instance;
let auth = state.oauth.token;
console.log(method, base, route, "auth:", auth != undefined);
return Promise.try(() => {
let url = new URL(base);
url.pathname = route;
let body;
let headers = {
"Accept": "application/json",
};
2022-09-11 18:42:13 +01:00
if (payload != undefined) {
if (type == "json") {
headers["Content-Type"] = "application/json";
body = JSON.stringify(payload);
} else if (type == "form") {
const formData = new FormData();
Object.entries(payload).forEach(([key, val]) => {
formData.set(key, val);
});
body = formData;
}
}
2022-09-11 18:05:06 +01:00
if (auth != undefined) {
headers["Authorization"] = auth;
}
return fetch(url.toString(), {
method,
headers,
body
});
}).then((res) => {
// try parse json even with error
let json = res.json().catch((e) => {
throw new APIError(`JSON parsing error: ${e.message}`);
});
return Promise.all([res, json]);
}).then(([res, json]) => {
if (!res.ok) {
if (auth != undefined && res.status == 401) {
// stored access token is invalid
dispatch(oauth.remove());
throw new APIError("Stored OAUTH login was no longer valid, please log in again.");
}
throw new APIError(json.error, {json});
} else {
return json;
}
2022-09-11 17:41:29 +01:00
});
2022-09-11 18:05:06 +01:00
};
2022-09-11 17:41:29 +01:00
}
function getCurrentUrl() {
return `${window.location.origin}${window.location.pathname}`;
}
function fetchInstance(domain) {
return function(dispatch, getState) {
return Promise.try(() => {
let lookup = getState().instances.info[domain];
if (lookup != undefined) {
return lookup;
}
// apiCall expects to pull the domain from state,
// but we don't want to store it there yet
// so we mock the API here with our function argument
let fakeState = {
oauth: {instance: domain}
};
2022-09-11 18:05:06 +01:00
return apiCall("GET", "/api/v1/instance")(dispatch, () => fakeState);
2022-09-11 17:41:29 +01:00
}).then((json) => {
if (json && json.uri) { // TODO: validate instance json more?
dispatch(setInstanceInfo([domain, json]));
2022-09-11 17:41:29 +01:00
return json;
}
});
};
}
module.exports = {
instance: {
fetch: fetchInstance
},
2022-09-11 18:05:06 +01:00
oauth: require("./oauth")({apiCall, getCurrentUrl}),
user: require("./user")({apiCall})
2022-09-11 17:41:29 +01:00
};