Skip to content

Commit cdbfdb8

Browse files
committed
refactor[api]: simplify API slice by removing custom fetch function and reauthentication logic
1 parent cb63565 commit cdbfdb8

1 file changed

Lines changed: 8 additions & 42 deletions

File tree

src/shared/api/apiSlice.ts

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,17 @@
1-
import type {
2-
BaseQueryFn,
3-
FetchArgs,
4-
FetchBaseQueryError,
5-
} from '@reduxjs/toolkit/query';
61
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
72

83
import { ENV_CONFIG } from '@/shared/config';
94

10-
const baseQuery = fetchBaseQuery({
11-
baseUrl: ENV_CONFIG.API_URL,
12-
credentials: 'include',
13-
prepareHeaders: (headers) => {
14-
headers.set('Content-Type', 'application/json');
15-
return headers;
16-
},
17-
fetchFn: async (input, init) => {
18-
const url = typeof input === 'string' ? input : input.url;
19-
const fullUrl = url.startsWith('http')
20-
? url
21-
: `${ENV_CONFIG.API_URL}${url}`;
22-
23-
const response = await fetch(fullUrl, {
24-
...init,
25-
credentials: 'include',
26-
mode: 'cors',
27-
headers: {
28-
...init?.headers,
29-
'Content-Type': 'application/json',
30-
},
31-
});
32-
33-
return response;
34-
},
35-
});
36-
37-
const baseQueryWithReauth: BaseQueryFn<
38-
string | FetchArgs,
39-
unknown,
40-
FetchBaseQueryError
41-
> = async (args, api, extraOptions) => {
42-
const result = await baseQuery(args, api, extraOptions);
43-
return result;
44-
};
45-
465
export const api = createApi({
476
reducerPath: 'api',
48-
baseQuery: baseQueryWithReauth,
7+
baseQuery: fetchBaseQuery({
8+
baseUrl: ENV_CONFIG.API_URL,
9+
credentials: 'include',
10+
prepareHeaders: (headers) => {
11+
headers.set('Content-Type', 'application/json');
12+
return headers;
13+
},
14+
}),
4915
tagTypes: ['genres', 'movies', 'currentMovie', 'user'],
5016
endpoints: () => ({}),
5117
});

0 commit comments

Comments
 (0)