mirror of
https://github.com/filecoin-project/slate.git
synced 2024-12-25 18:13:10 +03:00
578 lines
14 KiB
JavaScript
578 lines
14 KiB
JavaScript
import "isomorphic-fetch";
|
|
|
|
import * as Websockets from "~/common/browser-websockets";
|
|
import * as Strings from "~/common/strings";
|
|
|
|
//NOTE(martina): call Websockets.checkWebsocket() before any api call that uses websockets to return updates
|
|
// to make sure that websockets are properly connected (and to reconnect them if they are not)
|
|
// otherwise updates may not occur properly
|
|
|
|
const REQUEST_HEADERS = {
|
|
Accept: "application/json",
|
|
"Content-Type": "application/json",
|
|
};
|
|
|
|
const API_HEADERS = {
|
|
Accept: "application/json",
|
|
"Content-Type": "application/json",
|
|
Authorization: "Basic SLA42887290-7073-4f7c-961d-db84aea29b41TE",
|
|
};
|
|
|
|
const API_OPTIONS = {
|
|
method: "POST",
|
|
headers: API_HEADERS,
|
|
credentials: "include",
|
|
};
|
|
|
|
const DEFAULT_OPTIONS = {
|
|
method: "POST",
|
|
headers: REQUEST_HEADERS,
|
|
credentials: "include",
|
|
};
|
|
|
|
const CORS_OPTIONS = {
|
|
method: "POST",
|
|
headers: REQUEST_HEADERS,
|
|
credentials: "omit",
|
|
};
|
|
|
|
const returnJSON = async (route, options) => {
|
|
const response = await fetch(route, options);
|
|
const json = await response.json();
|
|
|
|
return json;
|
|
};
|
|
|
|
export const createZipToken = async ({ files, resourceURI }) => {
|
|
return await returnJSON(`${resourceURI}/api/download/create-zip-token`, {
|
|
...CORS_OPTIONS,
|
|
body: JSON.stringify({ files }),
|
|
});
|
|
};
|
|
|
|
export const downloadZip = ({ token, name, resourceURI }) =>
|
|
`${resourceURI}/api/download/download-by-token?downloadId=${token}&name=${name}`;
|
|
|
|
export const health = async (data = {}) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/_`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data: { buckets: data.buckets } }),
|
|
});
|
|
};
|
|
|
|
export const sendFilecoin = async (data) => {
|
|
if (Strings.isEmpty(data.source)) {
|
|
return null;
|
|
}
|
|
|
|
if (Strings.isEmpty(data.target)) {
|
|
return null;
|
|
}
|
|
|
|
if (!data.amount) {
|
|
return null;
|
|
}
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/addresses/send`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const checkUsername = async (data) => {
|
|
return await returnJSON(`/api/users/check-username`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const checkEmail = async (data) => {
|
|
return await returnJSON(`/api/users/check-email`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
//NOTE(toast): this functionality comes with the upgraded sg plan
|
|
// export const validateEmail = async (data) => {
|
|
// return await returnJSON("/api/emails/validate", {
|
|
// ...DEFAULT_OPTIONS,
|
|
// body: JSON.stringify({ data }),
|
|
// });
|
|
// };
|
|
|
|
export const sendEmail = async (data) => {
|
|
return await returnJSON("/api/emails/send-email", {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const sendTemplateEmail = async (data) => {
|
|
return await returnJSON("/api/emails/send-template", {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const archive = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/data/archive`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const removeFromBucket = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/data/bucket-remove`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const getSlateById = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/slates/get`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const getSlatesByIds = async (data) => {
|
|
return await returnJSON(`/api/slates/get`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const getSocial = async (data) => {
|
|
return await returnJSON(`/api/users/get-social`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const deleteTrustRelationship = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/users/trust-delete`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const updateTrustRelationship = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/users/trust-update`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const createTrustRelationship = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/users/trust`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const createSubscription = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/subscribe`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const search = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
if (Strings.isEmpty(data.query)) {
|
|
return { decorator: "NO_SERVER_TRIP", data: { results: [] } };
|
|
}
|
|
|
|
if (Strings.isEmpty(data.resourceURI)) {
|
|
return { decorator: "NO_RESOURCE_URI", data: { results: [] } };
|
|
}
|
|
return await returnJSON(`${data.resourceURI}/search`, {
|
|
...CORS_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const createFile = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/data/create`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const addFileToSlate = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/slates/add-file`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const requestTwitterToken = async () => {
|
|
return await returnJSON(`/api/twitter/request-token`, {
|
|
...DEFAULT_OPTIONS,
|
|
});
|
|
};
|
|
|
|
export const authenticateViaTwitter = async (data) => {
|
|
return await returnJSON(`/api/twitter/authenticate`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const createUserViaTwitter = async (data) => {
|
|
return await returnJSON(`/api/twitter/signup`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const createUserViaTwitterWithVerification = async (data) => {
|
|
return await returnJSON(`/api/twitter/signup-with-verification`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const updateViewer = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/users/update`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const signIn = async (data) => {
|
|
return await returnJSON(`/api/sign-in`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const hydrateAuthenticatedUser = async () => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/hydrate`, {
|
|
...DEFAULT_OPTIONS,
|
|
});
|
|
};
|
|
|
|
export const deleteViewer = async () => {
|
|
return await returnJSON(`/api/users/delete`, {
|
|
...DEFAULT_OPTIONS,
|
|
});
|
|
};
|
|
|
|
export const createUser = async (data) => {
|
|
return await returnJSON(`/api/users/create`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const updateStatus = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/users/status-update`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const updateSearch = async (data) => {
|
|
return await returnJSON(`/api/search/update`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
// export const checkCIDStatus = async (data) => {
|
|
// return await returnJSON(`/api/data/cid-status`, {
|
|
// ...DEFAULT_OPTIONS,
|
|
// body: JSON.stringify({ data }),
|
|
// });
|
|
// };
|
|
|
|
export const createSlate = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/slates/create`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const updateSlate = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/slates/update`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const updateSlateLayout = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/slates/update-layout`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const deleteSlate = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/slates/delete`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const removeFileFromSlate = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/slates/remove-file`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const generateAPIKey = async () => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/keys/generate`, {
|
|
...DEFAULT_OPTIONS,
|
|
});
|
|
};
|
|
|
|
export const deleteAPIKey = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/keys/delete`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const saveCopy = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/data/save-copy`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const updateFile = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/data/update`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const toggleFilePrivacy = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/data/toggle-privacy`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const deleteFiles = async (data) => {
|
|
await Websockets.checkWebsocket();
|
|
return await returnJSON(`/api/data/delete`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const getSerializedSlate = async (data) => {
|
|
return await returnJSON(`/api/slates/get-serialized`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const getSerializedProfile = async (data) => {
|
|
return await returnJSON(`/api/users/get-serialized`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const createSupportMessage = async (data) => {
|
|
return await returnJSON(`/api/support-message`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const createDownloadActivity = async (data) => {
|
|
return await returnJSON(`/api/activity/create-download-activity`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const getActivity = async (data) => {
|
|
return await returnJSON(`/api/activity/get-activity`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const getExplore = async (data) => {
|
|
return await returnJSON(`/api/activity/get-explore`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const getZipFilePaths = async (data) => {
|
|
return await returnJSON(`/api/zip/get-paths`, {
|
|
...DEFAULT_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const cleanDatabase = async () => {
|
|
return await returnJSON(`/api/clean-up/users`, {
|
|
...DEFAULT_OPTIONS,
|
|
});
|
|
};
|
|
|
|
export const v1GetSlate = async (data) => {
|
|
return await returnJSON(`/api/v1/get-slate`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const v1Get = async (data) => {
|
|
return await returnJSON(`/api/v1/get`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const v1UpdateSlate = async (data) => {
|
|
return await returnJSON(`/api/v1/update-slate`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const v2GetSlate = async (data) => {
|
|
return await returnJSON(`/api/v2/get-slate`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const v2GetUser = async (data) => {
|
|
return await returnJSON(`/api/v2/get-user`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const v2Get = async (data) => {
|
|
return await returnJSON(`/api/v2/get`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const v2UpdateSlate = async (data) => {
|
|
return await returnJSON(`/api/v2/update-slate`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const v2UpdateFile = async (data) => {
|
|
return await returnJSON(`/api/v2/update-file`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const createTwitterEmailVerification = async (data) => {
|
|
return await returnJSON(`/api/verifications/twitter/create`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const verifyTwitterEmail = async (data) => {
|
|
return await returnJSON(`/api/verifications/twitter/verify`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const createPasswordResetVerification = async (data) => {
|
|
return await returnJSON(`/api/verifications/password-reset/create`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const verifyPasswordResetEmail = async (data) => {
|
|
return await returnJSON(`/api/verifications/password-reset/verify`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const resetPassword = async (data) => {
|
|
return await returnJSON(`/api/users/reset-password`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const createLegacyVerification = async (data) => {
|
|
return await returnJSON(`/api/verifications/legacy/create`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const migrateUser = async (data) => {
|
|
return await returnJSON(`/api/users/migrate`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const createVerification = async (data) => {
|
|
return await returnJSON(`/api/verifications/create`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const verifyEmail = async (data) => {
|
|
return await returnJSON(`/api/verifications/verify`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const resendVerification = async (data) => {
|
|
return await returnJSON(`/api/verifications/resend`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|
|
|
|
export const getUserVersion = async (data) => {
|
|
return await returnJSON(`/api/users/get-version`, {
|
|
...API_OPTIONS,
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
};
|