yezihaohao / react-admin

:sparkles: react-admin system solution : react 后台管理系统解决方案
https://admiring-dijkstra-34cb29.netlify.com/
MIT License
6.65k stars 1.87k forks source link

refactor: enable axios get and post method pass customized callback #90

Closed fengertao closed 5 years ago

fengertao commented 5 years ago

axios发送get/post请求后,有时用户需要定制化的callback,而不是默认把resp=> resp.data

比如我的后台服务器Login API返回的response body的格式和react-admin默认不一样,需要如下定制的callback

export const login = loginJson =>
    post({
        url: config.MOCK_AUTH,
        data: loginJson,
        config: {
            headers: {
                'Content-Type': 'application/json',
            },
        },
        callback: resp => {
            if (resp.status === 200 && resp.data && resp.data.token) {
                setupAxiosInterceptors(resp.data.token);
                return {
                    uid: loginJson.username,
                    permissions: resp.data.permissions,
                    token: resp.data.token,
                };
            } else {
                return resp.data;
            }
        },
    });
yezihaohao commented 5 years ago

谢谢PR