0
私は簡単なフェッチの上にカスタムレストクライアントビルドを実装しようとしています。 401-403応答が受信された場合、ログインページにアプリをリダイレクトする必要があります。authClientがAUTH_ERRORで呼び出されていない
マニュアルによれば、401-403エラーが受信された場合、AUTH_ERRORでauthClientを魔法のように呼び出しますが、そうではありません。
誰かが説明する方法、それを接続する方法はありますか?
私はコンポーネントから残りのクライアントを呼び出すようにしようとしている:それは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);
}
restClientとauthClientのコードを共有できますか? – Gildas