-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
47 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,16 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { Env } from 'config/Env' | ||
import { Post } from 'features/posts/types' | ||
import makeApi from "libs/core/configureAxios"; | ||
|
||
const POSTS_BASE_URL = `${Env.API_BASE_URL}/posts` | ||
const api = makeApi(`${Env.API_BASE_URL}`); | ||
|
||
export const getPosts = async (): Promise<Post[]> => fetch(POSTS_BASE_URL).then(res => res.json()) | ||
const POSTS_BASE_URL = `/posts` | ||
|
||
export const createPost = async (post: Post): Promise<Post> => | ||
fetch(POSTS_BASE_URL, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(post), | ||
}).then(res => res.json()) | ||
export const getPosts = (): Promise<Post[]> => api.get(POSTS_BASE_URL) | ||
|
||
export const updatePost = async (post: Post): Promise<Post> => | ||
fetch(`${POSTS_BASE_URL}/${post.id}`, { | ||
method: 'PUT', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(post), | ||
}).then(res => res.json()) | ||
export const createPost = (post: Post): Promise<Post> => api.post(POSTS_BASE_URL, post); | ||
|
||
export const deletePost = async (post: Post): Promise<Post> => | ||
fetch(`${POSTS_BASE_URL}/${post.id}`, { | ||
method: 'DELETE', | ||
}).then(() => post) | ||
export const updatePost = (post: Post): Promise<Post> => api.put(`${POSTS_BASE_URL}/${post.id}`, post) | ||
|
||
export const deletePost = (post: Post): Promise<Post> => api.delete(`${POSTS_BASE_URL}/${post.id}`, { data: post}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import axios from 'axios' | ||
|
||
|
||
export default function makeApi(baseURL: string) { | ||
const api = axios.create({ | ||
baseURL, | ||
}) | ||
|
||
api.defaults.headers.post['Content-Type'] = "application/json"; | ||
api.defaults.headers.put['Content-Type'] = "application/json"; | ||
api.defaults.headers.delete['Content-Type'] = "application/json"; | ||
|
||
api.interceptors.request.use( | ||
(config) => { | ||
|
||
if(localStorage.getItem('authToken')) { | ||
config.headers = {...config.headers, Authorization: `Bearer ${localStorage.getItem('authToken')}`} | ||
} | ||
|
||
return config; | ||
}, | ||
(error) => Promise.reject(error) | ||
) | ||
|
||
api.interceptors.response.use( | ||
(response) => response.data, // return data object | ||
(error) => Promise.reject(error) | ||
) | ||
return api | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters