如何在特定端点禁用prepareHeaders?,例如,我在登录或注册端点上不需要授权头,但在某些端点上我需要授权头。
export const backendService = createApi({
reducerPath: 'backend',
baseQuery: fetchBaseQuery({
baseUrl: `${Endpoint}`,
prepareHeaders: (headers, {getState}) => {
const token = getState().auth.token;
if (token) {
headers.set('authorization', `Bearer ${token}`);
}
headers.set('Access-Control-Allow-Origin', '*');
return headers;
},
}),
tagTypes: ['Question', 'Questions'],
endpoints: build => ({
registerUser: build.mutation({ <------ skip prepareHeaders in register
query: body => ({
url: 'auth/local/register',
method: 'POST',
body,
}),
}),
login: build.mutation({ <------- skip prepareHeaders on login
query: body => ({
url: 'auth/local',
method: 'POST',
body,
}),
}),
getCategories: build.query({ <------ apply prepareHeaders
query: () => {
return {
url: 'categories'
};
},
}),
}),
});
RTK 1.7的当前测试版将端点名称传递给prepareHeaders
,因此如果您尝试这样做,您应该能够解决此问题。
BaseQueryApi和prepareheaders参数现在包括端点名称字段,类型指示它是查询还是突变,并且即使已经有缓存条目,也强制指示重新获取。这些可用于帮助确定标头,如Cache-Control:no-Cache。