Created
September 5, 2022 13:33
-
-
Save mqklin/948e7a7c708c2fb3b5644585a99d4151 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {StoredAuth} from 'r/utils/Stored'; | |
import sleep from 'r/utils/sleep'; | |
import showNotification from 'r/utils/showNotification'; | |
import fetch from 'r/utils/fetch'; | |
const GET = 'GET'; | |
const POST = 'POST'; | |
const PUT = 'PUT'; | |
const DELETE = 'DELETE'; | |
export const GET_USER_INFO = 'GET_USER_INFO'; | |
export const GET_WORLDSTATE = 'GET_WORLDSTATE'; | |
export const GET_ISSUER = 'GET_ISSUER'; | |
export const GET_COMPETITIONS = 'GET_COMPETITIONS'; | |
export const GET_COMPETITION_FORECASTS = 'GET_COMPETITION_FORECASTS'; | |
export const GET_COMPETITION_USERS = 'GET_COMPETITION_USERS'; | |
export const GET_USER_FORECASTS = 'GET_USER_FORECASTS'; | |
export const POST_SIGNALS = 'POST_SIGNALS'; | |
export const GET_AUTH = 'GET_AUTH'; | |
export const POST_AUTH = 'POST_AUTH'; | |
export const PUT_SIGNALS_CLOSE = 'PUT_SIGNALS_CLOSE'; | |
export const PUT_ISSUER = 'PUT_ISSUER'; | |
export const POST_ISSUER_GDPR = 'POST_ISSUER_GDPR'; | |
export const GET_TOTAL_ISSUERS = 'GET_TOTAL_ISSUERS'; | |
export const GET_TOTAL_FORECASTS = 'GET_TOTAL_FORECASTS'; | |
export const GET_NOTIFICATIONS = 'GET_NOTIFICATIONS'; | |
export const PUT_NOTIFICATION = 'PUT_NOTIFICATION'; | |
export const GET_FORECAST = 'GET_FORECAST'; | |
export const GET_KOVAN_SIGNAL = 'GET_KOVAN_SIGNAL'; | |
export const GET_L2_SIGNAL = 'GET_L2_SIGNAL'; | |
export const PUT_NOTIFICATIONS = 'PUT_NOTIFICATIONS'; | |
export const PUT_NOTIFICATIONS_READ = 'PUT_NOTIFICATIONS_READ'; | |
export const PUT_SIGNAL = 'PUT_SIGNAL'; | |
export const POST_NOTE_USER = 'POST_NOTE_USER'; | |
export const POST_AVATAR = 'POST_AVATAR'; | |
export const GET_TELEGRAM_AUTH = 'GET_TELEGRAM_AUTH'; | |
export const DELETE_OLD_FOLLOWING = 'DELETE_OLD_FOLLOWING'; | |
export const POST_NOTIFICATIONS = 'POST_NOTIFICATIONS'; | |
export const GET_SMARTS = 'GET_SMARTS'; | |
export const GET_ABI = 'GET_ABI'; | |
// window.x = () => fetchApiRaw(GET_NOTIFICATIONS, {queryParams: {unreadOnly: true}}); | |
if (!process.env.IS_SERVER) { | |
window.addNewsNotification = body => fetchApiRaw(POST_NOTIFICATIONS, {body}); // eslint-disable-line no-restricted-syntax | |
} | |
// addNewsNotification({id: '2021-05-12-see-your-performance'}) | |
// x({competitionId: '7', endDate: '2020-11-08T21:59:59.000Z'}) | |
export function fetchApiRaw(endpointId, {queryParams, urlParams, body} = {}, accessToken) { | |
const [method, base] = (() => { | |
switch (endpointId) { | |
case GET_USER_INFO: { | |
const [username] = urlParams; | |
return [ | |
GET, | |
`leaderboards/players/${username}`, | |
]; | |
} | |
case GET_WORLDSTATE: | |
return [ | |
GET, | |
'worldstate', | |
]; | |
case DELETE_OLD_FOLLOWING: { | |
const [issuerId] = urlParams; | |
return [ | |
DELETE, | |
`issuer/oldFollowing/${issuerId}`, | |
]; | |
} | |
case GET_TELEGRAM_AUTH: | |
return [ | |
GET, | |
'telegramAuth', | |
]; | |
case GET_ISSUER: | |
return [ | |
GET, | |
'issuer', | |
]; | |
case PUT_ISSUER: | |
return [ | |
PUT, | |
'issuer', | |
]; | |
case POST_AVATAR: | |
return [ | |
POST, | |
'issuer/avatar', | |
]; | |
case GET_COMPETITIONS: | |
return [ | |
GET, | |
'competitions', | |
]; | |
case GET_COMPETITION_FORECASTS: { | |
const [competitionId] = urlParams; | |
return [ | |
GET, | |
`competitions/${competitionId}/forecasts`, | |
]; | |
} | |
case GET_COMPETITION_USERS: { | |
const [competitionId] = urlParams; | |
return [ | |
GET, | |
`competitions/${competitionId}/issuers`, | |
]; | |
} | |
case GET_USER_FORECASTS: { | |
const [username] = urlParams; | |
return [ | |
GET, | |
`leaderboards/players/${username}/forecasts`, | |
]; | |
} | |
case POST_NOTE_USER: { | |
const [username] = urlParams; | |
return [ | |
POST, | |
`leaderboards/players/${username}/writeNote`, | |
]; | |
} | |
case POST_SIGNALS: | |
return [ | |
POST, | |
'signals', | |
]; | |
case GET_AUTH: | |
return [ | |
GET, | |
'auth', | |
]; | |
case POST_AUTH: | |
return [ | |
POST, | |
'auth', | |
]; | |
case PUT_SIGNAL: { | |
const [signalId] = urlParams; | |
return [ | |
PUT, | |
`signals/${signalId}`, | |
]; | |
} | |
case PUT_SIGNALS_CLOSE: { | |
const [signalId] = urlParams; | |
return [ | |
PUT, | |
`signals/${signalId}/close`, | |
]; | |
} | |
case GET_TOTAL_ISSUERS: { | |
return [ | |
GET, | |
'leaderboards/players', | |
]; | |
} | |
case GET_TOTAL_FORECASTS: | |
return [ | |
GET, | |
'leaderboards/forecasts', | |
]; | |
case GET_FORECAST: { | |
const [forecastId] = urlParams; | |
return [ | |
GET, | |
`signals/${forecastId}`, | |
]; | |
} | |
case GET_KOVAN_SIGNAL: { | |
const [signalId] = urlParams; | |
return [ | |
GET, | |
`signals/null/${signalId}`, | |
]; | |
} | |
case GET_L2_SIGNAL: { | |
const [contractAddress, signalId] = urlParams; | |
return [ | |
GET, | |
`signals/${contractAddress}/${signalId}`, | |
]; | |
} | |
case POST_ISSUER_GDPR: | |
return [ | |
POST, | |
'issuer/gdpr', | |
]; | |
case GET_NOTIFICATIONS: | |
return [ | |
GET, | |
'notifications', | |
]; | |
case PUT_NOTIFICATION: { | |
const [notificationId] = urlParams; | |
return [ | |
PUT, | |
`notifications/${notificationId}/read`, | |
]; | |
} | |
case PUT_NOTIFICATIONS: { | |
return [ | |
PUT, | |
'notifications', | |
]; | |
} | |
case PUT_NOTIFICATIONS_READ: { | |
return [ | |
PUT, | |
'notifications/read', | |
]; | |
} | |
case POST_NOTIFICATIONS: { | |
return [ | |
POST, | |
'notifications', | |
]; | |
} | |
case GET_SMARTS: { | |
return [ | |
GET, | |
'smarts', | |
]; | |
} | |
case GET_ABI: { | |
const [address] = urlParams; | |
return [ | |
GET, | |
`abi/${address}`, | |
]; | |
} | |
default: | |
throw new Error(`INVALID ENDPOINT ${endpointId}`); | |
} | |
})(); | |
const headers = {}; | |
if ([POST_AVATAR].includes(endpointId)) { | |
// https://stackoverflow.com/questions/49692745/express-using-multer-error-multipart-boundary-not-found-request-sent-by-pos | |
// headers['Content-Type'] = 'multipart/form-data'; | |
} | |
else { | |
headers['Content-Type'] = 'application/json'; | |
} | |
if (accessToken) { | |
headers.Authorization = `Bearer ${accessToken}`; | |
} | |
let url = `${process.env.BACKEND_URL_API}/${base}`; | |
if (queryParams) { | |
url += `?${Object.entries(queryParams).map(([key, value]) => `${key}=${value}`).join('&')}`; | |
} | |
const options = {method, headers}; | |
if (body) { | |
if ([POST_AVATAR].includes(endpointId)) { | |
options.body = body; | |
} | |
else { | |
options.body = JSON.stringify(body); | |
} | |
} | |
return fetch(url, options) | |
.catch(e => { | |
e.name = `fetchApi: ${e.name}`; | |
throw e; | |
}) | |
.then(async response => { | |
const responseBody = await response.json(); | |
if (response.status === 401 && responseBody?.message === 'JWT_EXPIRED') { | |
StoredAuth.set(null); | |
showNotification('error', 'Session is expired'); | |
window.location.href = '/'; | |
await sleep(9_999_999); | |
} | |
if (response.ok) { | |
return responseBody; | |
} | |
const error = new Error(`${endpointId}: ${response.status}`); | |
error.name = 'fetchApi'; | |
console.error(JSON.stringify(responseBody)); // eslint-disable-line no-console | |
console.error(JSON.stringify(options)); // eslint-disable-line no-console | |
error.fromFetchApi = true; | |
error.response = response; | |
error.body = responseBody; | |
error.extra = { | |
responseBody, | |
code: response.status, | |
url, | |
options, | |
}; | |
throw error; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment