根据RTK 查询文档,我可以创建如下查询:
export const projectsApi = createApi({
reducerPath: 'projects',
baseQuery: fetchBaseQuery({
baseUrl: "https://some-site.com/api/",
prepareHeaders: (headers) => {
headers.set('Authorization', `Bearer ${accessToken}`);
return headers;
},
}),
tagTypes: ['Project'],
endpoints: (builder) => ({
fetchProjects: builder.query<Project[], void>({
query: () => ({ url: `projects` }),
}),
}),
});
以上按预期工作。但是,如果想使用Kitsu 之类的外部库来获取和转换数据,则会出现以下错误:
这是我在使用 Kitsu 时更新的代码:
export const projectsApi = createApi({
reducerPath: 'projects',
baseQuery: fetchBaseQuery({
baseUrl: "https://some-site.com/api/",
}),
tagTypes: ['Project'],
endpoints: (builder) => ({
fetchProjects: builder.query<Project[], void>({
query: api.get('projects'),
}),
}),
});
这是api函数的代码:
const kitsu = (accessToken: string) => {
return new Kitsu({
baseURL: "https://some-site.com/api/",
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`,
},
});
};
const api = kitsu("ey....");
是否可以使用 Kitsu 或任何 3rd 方包提供的功能覆盖 RTK Query/Redux Toolkit的baseQuery功能?