init create-fias
This commit is contained in:
6
src/shared/config/api/URLs.ts
Normal file
6
src/shared/config/api/URLs.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
const BASE_URL =
|
||||
import.meta.env.VITE_API_URL || 'https://jsonplaceholder.typicode.com';
|
||||
|
||||
const ENDP_POSTS = '/posts/';
|
||||
|
||||
export { BASE_URL, ENDP_POSTS };
|
||||
35
src/shared/config/api/httpClient.ts
Normal file
35
src/shared/config/api/httpClient.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import axios from 'axios';
|
||||
import { BASE_URL } from './URLs';
|
||||
import i18n from '@/shared/config/i18n';
|
||||
|
||||
const httpClient = axios.create({
|
||||
baseURL: BASE_URL,
|
||||
timeout: 10000,
|
||||
});
|
||||
|
||||
httpClient.interceptors.request.use(
|
||||
async (config) => {
|
||||
console.log(`API REQUEST to ${config.url}`, config);
|
||||
|
||||
// Language configs
|
||||
const language = i18n.language;
|
||||
config.headers['Accept-Language'] = language;
|
||||
// const accessToken = localStorage.getItem('accessToken');
|
||||
// if (accessToken) {
|
||||
// config.headers['Authorization'] = `Bearer ${accessToken}`;
|
||||
// }
|
||||
|
||||
return config;
|
||||
},
|
||||
(error) => Promise.reject(error),
|
||||
);
|
||||
|
||||
httpClient.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
console.error('API error:', error);
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
|
||||
export default httpClient;
|
||||
6
src/shared/config/api/test/test.model.ts
Normal file
6
src/shared/config/api/test/test.model.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export interface TestApiType {
|
||||
userId: number;
|
||||
id: number;
|
||||
title: string;
|
||||
body: string;
|
||||
}
|
||||
14
src/shared/config/api/test/test.request.ts
Normal file
14
src/shared/config/api/test/test.request.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import httpClient from '@/shared/config/api/httpClient';
|
||||
import type { TestApiType } from '@/shared/config/api/test/test.model';
|
||||
import type { ReqWithPagination } from '@/shared/config/api/types';
|
||||
import { ENDP_POSTS } from '@/shared/config/api/URLs';
|
||||
import type { AxiosResponse } from 'axios';
|
||||
|
||||
const getPosts = async (
|
||||
pagination?: ReqWithPagination,
|
||||
): Promise<AxiosResponse<TestApiType>> => {
|
||||
const response = await httpClient.get(ENDP_POSTS, { params: pagination });
|
||||
return response;
|
||||
};
|
||||
|
||||
export { getPosts };
|
||||
20
src/shared/config/api/types.ts
Normal file
20
src/shared/config/api/types.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
export interface ResWithPagination<T> {
|
||||
success: boolean;
|
||||
message: string;
|
||||
links: Links;
|
||||
total_items: number;
|
||||
total_pages: number;
|
||||
page_size: number;
|
||||
current_page: number;
|
||||
data: T[];
|
||||
}
|
||||
|
||||
interface Links {
|
||||
next: number | null;
|
||||
previous: number | null;
|
||||
}
|
||||
|
||||
export interface ReqWithPagination {
|
||||
_start?: number;
|
||||
_limit?: number;
|
||||
}
|
||||
Reference in New Issue
Block a user