(endpoint, method, store, id, body)
| 31 | const request = {GET, POST}; |
| 32 | |
| 33 | export default function apiThunk(endpoint, method, store, id, body) { |
| 34 | return async (dispatch, getState) => { |
| 35 | let {config, hooks} = getState(); |
| 36 | let newHeaders = null; |
| 37 | |
| 38 | const url = `${urlBase(config)}${endpoint}`; |
| 39 | |
| 40 | function setConnectionStatus(connected) { |
| 41 | if (getState().error.backEndConnected !== connected) { |
| 42 | dispatch({ |
| 43 | type: 'SET_CONNECTION_STATUS', |
| 44 | payload: connected |
| 45 | }); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | dispatch({ |
| 50 | type: store, |
| 51 | payload: {id, status: 'loading'} |
| 52 | }); |
| 53 | |
| 54 | try { |
| 55 | let res; |
| 56 | for (let retry = 0; retry <= MAX_AUTH_RETRIES; retry++) { |
| 57 | try { |
| 58 | res = await request[method]( |
| 59 | url, |
| 60 | config.fetch, |
| 61 | body, |
| 62 | config |
| 63 | ); |
| 64 | } catch (e) { |
| 65 | // fetch rejection - this means the request didn't return, |
| 66 | // we don't get here from 400/500 errors, only network |
| 67 | // errors or unresponsive servers. |
| 68 | // eslint-disable-next-line no-console |
| 69 | console.log('fetch error', res); |
| 70 | setConnectionStatus(false); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | if ( |
| 75 | res.status === STATUS.UNAUTHORIZED || |
| 76 | res.status === STATUS.BAD_REQUEST |
| 77 | ) { |
| 78 | if (hooks.request_refresh_jwt) { |
| 79 | const body = await res.text(); |
| 80 | if (body.includes(JWT_EXPIRED_MESSAGE)) { |
| 81 | const newJwt = await hooks.request_refresh_jwt( |
| 82 | config.fetch.headers.Authorization.substr( |
| 83 | 'Bearer '.length |
| 84 | ) |
| 85 | ); |
| 86 | if (newJwt) { |
| 87 | newHeaders = { |
| 88 | Authorization: `Bearer ${newJwt}` |
| 89 | }; |
| 90 |
no test coverage detected
searching dependent graphs…