|
| 1 | +import { UPDATE_TOAST } from '../context/actionsFeedbackExternalRecipient.js'; |
| 2 | +import qs from 'qs'; |
| 3 | + |
| 4 | +export const BASE_API_URL = import.meta.env.VITE_APP_API_URL |
| 5 | + ? import.meta.env.VITE_APP_API_URL |
| 6 | + : 'http://localhost:8080'; |
| 7 | + |
| 8 | +export const getAvatarURL = email => |
| 9 | + BASE_API_URL + |
| 10 | + '/services/member-profiles/member-photos/' + |
| 11 | + encodeURIComponent(email); |
| 12 | + |
| 13 | +function fetchAbsolute(fetch) { |
| 14 | + return baseUrl => (url, otherParams) => |
| 15 | + url.startsWith('/') |
| 16 | + ? fetch(baseUrl + url, { credentials: 'include', ...otherParams }) |
| 17 | + : fetch(url, { credentials: 'include', ...otherParams }); |
| 18 | +} |
| 19 | + |
| 20 | +let myFetch = null; |
| 21 | + |
| 22 | +export const getMyFetch = async () => { |
| 23 | + if (!myFetch) { |
| 24 | + myFetch = fetchAbsolute(fetch)(BASE_API_URL); |
| 25 | + |
| 26 | + /* |
| 27 | + I'm not sure this was working before, but we need to figure out an approach for fetch. I will |
| 28 | + open an issue for this. |
| 29 | +
|
| 30 | + myAxios.interceptors.response.use( |
| 31 | + // Any status code that lie within the range of 2xx cause this function to trigger |
| 32 | + (res) => { |
| 33 | + return res; |
| 34 | + }, |
| 35 | + // Any status codes that falls outside the range of 2xx cause this function to trigger |
| 36 | + (err) => { |
| 37 | + if (err.response.status !== 401) { |
| 38 | + return Promise.reject(err); |
| 39 | + } |
| 40 | +
|
| 41 | + // trade in refresh token for access token |
| 42 | + return axios.get('/oauth/access_token', { |
| 43 | + baseURL: BASE_API_URL, |
| 44 | + withCredentials: true, |
| 45 | + timeout: 30000 |
| 46 | + }) |
| 47 | + .then(() => { |
| 48 | + // retry original request |
| 49 | + return myAxios(err.config); |
| 50 | + }) |
| 51 | + .catch(() => { |
| 52 | + return Promise.reject(err); |
| 53 | + }) |
| 54 | + } |
| 55 | + ); |
| 56 | + */ |
| 57 | + } |
| 58 | + return myFetch; |
| 59 | +}; |
| 60 | + |
| 61 | +export const resolve = async payload => { |
| 62 | + let { url } = payload; |
| 63 | + const { params = null, data = null, ...rest } = payload; |
| 64 | + const myFetch = await getMyFetch(); |
| 65 | + |
| 66 | + // Convert params to fetch style... |
| 67 | + params && (url = `${url}?` + qs.stringify(params, { arrayFormat: 'repeat' })); |
| 68 | + |
| 69 | + // Convert data to body... |
| 70 | + data && (rest.body = JSON.stringify(data)); |
| 71 | + |
| 72 | + const promise = myFetch(url, rest); |
| 73 | + const resolved = { |
| 74 | + payload: null, |
| 75 | + error: null |
| 76 | + }; |
| 77 | + |
| 78 | + resolved.payload = await promise; |
| 79 | + |
| 80 | + if (!resolved.payload.ok) { |
| 81 | + try { |
| 82 | + resolved.error = await resolved.payload.json(); |
| 83 | + } catch (error) { |
| 84 | + resolved.error = resolved.payload.statusText; |
| 85 | + console.error(error); |
| 86 | + } |
| 87 | + |
| 88 | + if (window.snackDispatch) { |
| 89 | + window.snackDispatch({ |
| 90 | + type: UPDATE_TOAST, |
| 91 | + payload: { |
| 92 | + severity: 'error', |
| 93 | + toast: resolved?.error |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + } else { |
| 98 | + const contentType = resolved.payload.headers.get('Content-Type'); |
| 99 | + const contentLength = resolved.payload.headers.get('Content-Length'); |
| 100 | + if (contentType && contentType.indexOf('text/csv') !== -1) { |
| 101 | + resolved.payload.data = await resolved.payload.blob(); |
| 102 | + } else if (contentLength && contentLength > 0) { |
| 103 | + if (contentType && contentType.indexOf('application/json') !== -1) { |
| 104 | + resolved.payload.data = await resolved.payload.json(); |
| 105 | + } else { |
| 106 | + resolved.payload.data = await resolved.payload.text(); |
| 107 | + } |
| 108 | + } else { |
| 109 | + resolved.payload.data = null; |
| 110 | + } |
| 111 | + } |
| 112 | + return resolved; |
| 113 | +}; |
0 commit comments