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