我正在尝试为访问令牌因 RTK 查询而变得无效的情况构建拦截器。我已经通过文档中的示例构建了它,因此如下所示:
const baseQuery = fetchBaseQuery({
baseUrl: BASE_URL,
prepareHeaders: (headers, { getState }) => {
const {
auth: {
user: { accessToken },
},
} = getState() as RootState;
if (accessToken) {
headers.set('authorization', `Bearer ${accessToken}`);
}
return headers;
},
});
const baseQueryWithReauth: BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError> = async (
args,
api,
extraOptions
) => {
let result = await baseQuery(args, api, extraOptions);
if (result.error && result.error.status === 401) {
const refreshResult = await baseQuery('token/refresh/', api, extraOptions);
if (refreshResult.data) {
api.dispatch(tokenUpdated({ accessToken: refreshResult.data as string }));
// retry the initial query
result = await baseQuery(args, api, extraOptions);
} else {
api.dispatch(logout());
}
}
return result;
};
export const baseApi = createApi({
reducerPath: 'baseApi',
baseQuery: baseQueryWithReauth,
endpoints: () => ({}),
});
问题是token/refresh/
期望一个POST
带有刷新令牌的请求它的主体,我无法弄清楚如何重建这条线const refreshResult = await baseQuery('token/refresh/', api, extraOptions);
以使其接受参数并发出POST
请求。