authClient不与AUTH_ERROR调用
问题描述:
我试图实现自定义休息客户端构建上的简单获取。 如果收到401-403响应,它必须“重定向”应用程序到登录页面。authClient不与AUTH_ERROR调用
根据文档,如果收到401-403错误,它会奇迹般地调用authClient与AUTH_ERROR,但它不会。
有人可以解释,如何连接它?
我试图从组件调用REST客户端:它是“simpleRestClient”
componentDidMount() {
restClient(CREATE, 'api/method', {
CurrentTime: new Date()
})
.then(o =>
{
this.setState({ Msg: Object.values(o.data.ServerTime) });
});
}
RESTClient实现实现简单的重新实现:
export const fetchJson = (url, options = {}) => {
const requestHeaders =
options.headers ||
new Headers({
Accept: 'application/json',
});
if (
!requestHeaders.has('Content-Type') &&
!(options && options.body && options.body instanceof FormData)
) {
requestHeaders.set('Content-Type', 'application/json');
}
if (options.user && options.user.authenticated && options.user.token) {
requestHeaders.set('Authorization', options.user.token);
}
return fetch(url, { ...options, headers: requestHeaders })
.then(response =>
response.text().then(text => ({
status: response.status,
statusText: response.statusText,
headers: response.headers,
body: text,
}))
)
.then(({ status, statusText, headers, body }) => {
if (status < 200 || status >= 300) {
return Promise.reject(
new HttpError(
(json && json.message) || statusText,
status,
json
)
);
}
let json;
try {
json = JSON.parse(body);
} catch (e) {
// not json, no big deal
}
return { status, headers, body, json };
});
};
const httpClient = (url, options = {}) => {
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/json' });
}
return fetchJson(url, options);
}
答
你试过拒绝与Error
而非承诺一个HttpError
?
你可以分享你的restClient和authClient的代码吗? – Gildas