const root = import.meta.env.PROD ? '': '/api'; const service = (uri, data, success, failure) => { post(uri, data, json => { if (json.code === 0 && success) { success(json.data); } else if (failure) { failure(json); } }, failure); } const post = (uri, body, success, failure) => { let header = {}; psid(header); fetch(root + uri, { method: 'POST', headers: header, body: JSON.stringify(body) }).then(response => { if (uri === '/user/sign-out') localStorage.removeItem('photon-session-id'); if (response.ok && success) response.json().then(success); }).catch(error => { if (failure) failure(error); }); } const upload = (name, file, fileName, success, progress) => { let xhr = new XMLHttpRequest(); xhr.upload.addEventListener('progress', event => { if (progress) progress(Math.round(100 * event.loaded / event.total)); }); xhr.addEventListener('load', event => { if (success) success(JSON.parse(xhr.responseText)); }); xhr.open('POST', root + '/photon/ctrl-http/upload'); let header = {}; psid(header); for (let key in header) xhr.setRequestHeader(key, header[key]); let body = new FormData(); if (fileName) body.append(name, file, fileName); else body.append(name, file); xhr.send(body); } const psid = (header) => { // localStorage.setItem('photon-session-id', 'bh4n8magnjwix9q0k51utrdy0uk9gpuqfp4ssn0z8xdna0nlaq09xdugawveuq3u'); // delete-on-build let psid = localStorage.getItem('photon-session-id'); if (!psid) { psid = ''; while (psid.length < 64) psid += Math.random().toString(36).substring(2); psid = psid.substring(0, 64); localStorage.setItem('photon-session-id', psid); } header['photon-session-id'] = psid; } const url = uri => { if (!uri) return null; if (uri.indexOf('://') > -1) return uri; return root + uri; } export { post, service, upload, url, }