MCPcopy Index your code
hub / github.com/plotly/dash / apiThunk

Function apiThunk

dash/dash-renderer/src/actions/api.js:33–141  ·  view source on GitHub ↗
(endpoint, method, store, id, body)

Source from the content-addressed store, hash-verified

31const request = {GET, POST};
32
33export 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

Callers 3

requestDependenciesFunction · 0.85
componentDidMountMethod · 0.85

Calls 7

urlBaseFunction · 0.90
handleAsyncErrorFunction · 0.90
getStateFunction · 0.85
setConnectionStatusFunction · 0.85
textMethod · 0.80
indexOfMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…