lbry-desktop/ui/lbrysync.js

124 lines
3.1 KiB
JavaScript
Raw Normal View History

2022-09-18 20:12:32 +02:00
import { ipcRenderer } from 'electron';
const BASE_URL = process.env.LBRYSYNC_BASE_URL || 'https://dev.lbry.id';
const SYNC_API_DOWN = 'sync_api_down';
const DUPLICATE_EMAIL = 'duplicate_email';
const UNKNOWN_ERROR = 'unknown_api_error';
2022-09-18 20:12:32 +02:00
const NOT_FOUND = 'not_found';
console.log('process.env.', process.env.LBRYSYNC_BASE_URL);
const API_VERSION = 3;
const POST = 'POST';
const GET = 'GET';
// const API_URL = `${BASE_URL}/api/${API_VERSION}`;
const AUTH_ENDPOINT = '/auth/full';
const REGISTER_ENDPOINT = '/signup';
2022-09-18 20:12:32 +02:00
const WALLET_ENDPOINT = '/wallet';
const CLIENT_SALT_SEED = '/client-salt-seed';
const Lbrysync = {
apiRequestHeaders: { 'Content-Type': 'application/json' },
apiUrl: `${BASE_URL}/api/${API_VERSION}`,
2022-09-18 20:12:32 +02:00
setApiHeader: (key, value) => {
Lbrysync.apiRequestHeaders = Object.assign(Lbrysync.apiRequestHeaders, { [key]: value });
},
};
2022-09-18 20:12:32 +02:00
export async function fetchSaltSeed(email) {
const buff = Buffer.from(email.toString('utf8'));
const emailParam = buff.toString('base64');
const result = await callWithResult(GET, CLIENT_SALT_SEED, { email: emailParam });
return result;
}
export async function getAuthToken(email, password, deviceId) {
try {
const result = await callWithResult(POST, AUTH_ENDPOINT, { email, password, deviceId });
return { token: result };
} catch (e) {
return { error: e.message };
}
}
export async function register(email, password, saltSeed) {
try {
await callWithResult(POST, REGISTER_ENDPOINT, { email, password, clientSaltSeed: saltSeed });
return;
} catch (e) {
return { error: e.message };
}
}
export async function pushWallet(walletState, hmac, token) {
// token?
const body = {
token: token,
encryptedWallet: walletState.encryptedWallet,
sequence: walletState.sequence,
hmac: hmac,
};
await callWithResult(POST, WALLET_ENDPOINT, { token, hmac, sequence });
}
export async function pullWallet(token) {
try {
await callWithResult(GET, REGISTER_ENDPOINT, { token });
return;
} catch (e) {
return { error: e.message };
}
} // token
function callWithResult(method, endpoint, params = {}) {
return new Promise((resolve, reject) => {
apiCall(
2022-09-18 20:12:32 +02:00
method,
endpoint,
params,
(result) => {
resolve(result);
},
reject
);
});
}
2022-09-18 20:12:32 +02:00
function apiCall(method, endpoint, params, resolve, reject) {
const options = {
2022-09-18 20:12:32 +02:00
method: method,
};
2022-09-18 20:12:32 +02:00
let searchString = '';
if (method === GET) {
const search = new URLSearchParams(params);
searchString = `?${search}`;
} else if (method === POST) {
options.body = JSON.stringify(params);
}
return fetch(`${Lbrysync.apiUrl}${endpoint}${searchString}`, options)
.then(handleResponse)
.then((response) => {
2022-09-18 20:12:32 +02:00
return response;
})
.catch(reject);
}
function handleResponse(response) {
if (response.status >= 200 && response.status < 300) {
2022-09-18 20:12:32 +02:00
return response.json();
}
if (response.status === 500) {
2022-09-18 20:12:32 +02:00
return Promise.reject(500);
}
if (response.status === 409) {
2022-09-18 20:12:32 +02:00
return Promise.reject(409);
}
if (response.status === 404) {
return Promise.reject(404);
}
return Promise.reject(UNKNOWN_ERROR);
}
export default Lbrysync;