Skip to content

Commit 3d7e257

Browse files
committed
♻️ waaaaay better axios default config
1 parent fe6bfe9 commit 3d7e257

File tree

10 files changed

+80
-294
lines changed

10 files changed

+80
-294
lines changed

next.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/** @type {import('next').NextConfig} */
22
const nextConfig = {
33
reactStrictMode: true,
4+
env: {
5+
API_URL: process.env.NEXT_PUBLIC_API_URL,
6+
},
47
images: {
58
domains: [
69
'lh3.googleusercontent.com',

pages/blog/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { GetStaticProps, NextPage } from 'next';
22
import Head from 'next/head';
33
import Link from 'next/link';
4-
import React from 'react';
4+
import React, { useEffect, useState } from 'react';
55
import PostCard from '../../src/components/Blog/PostCard';
66
import { getPosts } from '../../src/db/posts';
77
import { Post } from '../../src/types/Post';

src/config/axios.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import axios from 'axios';
2+
import Cookies from 'js-cookie';
3+
4+
const axiosInstance = axios.create({
5+
baseURL: process.env.NEXT_PUBLIC_API_URL,
6+
headers: {
7+
'Content-Type': 'application/json',
8+
Accept: "application/json",
9+
}
10+
});
11+
12+
// set standard authentication header
13+
axiosInstance.interceptors.request.use(
14+
(config) => {
15+
const token = Cookies.get('authToken');
16+
if (token && config && config.headers) {
17+
config.headers.Authorization = `Bearer ${token}`;
18+
}
19+
return config;
20+
},
21+
(error) => {
22+
Promise.reject(error);
23+
}
24+
);
25+
26+
export default axiosInstance;
27+

src/config/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export const API_URL = 'https://hitchlog-api.fly.dev';
1+
export * from './axios';
2+
export * from './statistics';

src/db/comments.ts

+3-25
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,7 @@
1-
import axios from 'axios';
2-
import Cookies from 'js-cookie';
3-
import { API_URL } from '../config';
1+
import axios from '../config/axios';
42

53
export const createPostComment = async (postId: number, values: any) =>
6-
axios.post(
7-
`${API_URL}/posts/${postId}/create_comment`,
8-
{ post_comment: values },
9-
{
10-
headers: {
11-
'Content-Type': 'application/json',
12-
Accept: 'application/json',
13-
Authorization: `Bearer ${Cookies.get('authToken')}`,
14-
},
15-
}
16-
);
4+
axios.post(`/posts/${postId}/create_comment`, { post_comment: values });
175

186
export const createTripComment = async (tripId: number, values: any) =>
19-
axios.post(
20-
`${API_URL}/trips/${tripId}/create_comment`,
21-
{ comment: values },
22-
{
23-
headers: {
24-
'Content-Type': 'application/json',
25-
Accept: 'application/json',
26-
Authorization: `Bearer ${Cookies.get('authToken')}`,
27-
},
28-
}
29-
);
7+
axios.post(`/trips/${tripId}/create_comment`, { comment: values });

src/db/posts.ts

+6-42
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,9 @@
1-
import axios from 'axios';
2-
import Cookies from 'js-cookie';
3-
import { API_URL } from '../config';
4-
5-
export const getPosts = async () =>
6-
axios.get(`${API_URL}/posts`, {
7-
headers: {
8-
'Content-Type': 'application/json',
9-
Accept: 'application/json',
10-
},
11-
});
12-
13-
export const createPost = async (data: any) =>
14-
axios.post(`${API_URL}/posts`, data, {
15-
headers: {
16-
'Content-Type': 'application/json',
17-
Accept: 'application/json',
18-
Authorization: `Bearer ${Cookies.get('authToken')}`,
19-
},
20-
});
1+
import axios from '../config/axios';
212

3+
export const getPosts = async () => axios.get('/posts');
4+
export const createPost = async (data: any) => axios.post('/posts', data);
225
export const updatePost = async (id: number, data: any) =>
23-
axios.put(`${API_URL}/posts/${id}`, data, {
24-
headers: {
25-
'Content-Type': 'application/json',
26-
Accept: 'application/json',
27-
Authorization: `Bearer ${Cookies.get('authToken')}`,
28-
},
29-
});
30-
31-
export const getPost = async (id: string) =>
32-
axios.get(`${API_URL}/posts/${id}`, {
33-
headers: {
34-
'Content-Type': 'application/json',
35-
Accept: 'application/json',
36-
},
37-
});
38-
6+
axios.put(`/posts/${id}`, data);
7+
export const getPost = async (id: string) => axios.get(`/posts/${id}`);
398
export const getPostComments = async (id: string) =>
40-
axios.get(`${API_URL}/posts/${id}/comments`, {
41-
headers: {
42-
'Content-Type': 'application/json',
43-
Accept: 'application/json',
44-
},
45-
});
9+
axios.get(`/posts/${id}/comments`);

src/db/ride.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
import axios from 'axios';
2-
import Cookies from 'js-cookie';
3-
import { API_URL } from '../config';
1+
import axios from '../config/axios';
42

53
export const putLikeRide = (id: number) => {
6-
return axios.put(
7-
`${API_URL}/rides/${id}/like`,
8-
{},
9-
{
10-
headers: {
11-
'Content-Type': 'application/json',
12-
Accept: 'application/json',
13-
Authorization: `Bearer ${Cookies.get('authToken')}`,
14-
},
15-
}
16-
);
4+
return axios.put(`/rides/${id}/like`);
175
};

src/db/statistics.ts

+7-37
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,13 @@
1-
import axios, { AxiosResponse } from 'axios';
2-
import { API_URL } from '../config';
1+
import { AxiosResponse } from 'axios';
2+
import axios from '../config/axios';
3+
34
import { AgeForTrip, LabelValue, Top10 } from '../types/Statistics';
45

56
export const getAgeForTrips = () =>
6-
axios.get<any, AxiosResponse<AgeForTrip[]>>(
7-
`${API_URL}/statistics/age_for_trips`,
8-
{
9-
headers: {
10-
'Content-Type': 'application/json',
11-
Accept: 'application/json',
12-
},
13-
}
14-
);
7+
axios.get<any, AxiosResponse<AgeForTrip[]>>('statistics/age_for_trips');
158
export const getTop10 = () =>
16-
axios.get<any, AxiosResponse<Top10[]>>(`${API_URL}/statistics/top_10`, {
17-
headers: {
18-
'Content-Type': 'application/json',
19-
Accept: 'application/json',
20-
},
21-
});
22-
9+
axios.get<any, AxiosResponse<Top10[]>>('/statistics/top_10');
2310
export const genderStats = () =>
24-
axios.get<any, AxiosResponse<LabelValue[]>>(
25-
`${API_URL}/statistics/users_by_gender`,
26-
{
27-
headers: {
28-
'Content-Type': 'application/json',
29-
Accept: 'application/json',
30-
},
31-
}
32-
);
33-
11+
axios.get<any, AxiosResponse<LabelValue[]>>('/statistics/users_by_gender');
3412
export const waitingTimeStats = () =>
35-
axios.get<any, AxiosResponse<LabelValue[]>>(
36-
`${API_URL}/statistics/waiting_time`,
37-
{
38-
headers: {
39-
'Content-Type': 'application/json',
40-
Accept: 'application/json',
41-
},
42-
}
43-
);
13+
axios.get<any, AxiosResponse<LabelValue[]>>('/statistics/waiting_time');

src/db/trips.ts

+12-82
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,22 @@
1-
import axios from 'axios';
2-
import Cookies from 'js-cookie';
3-
import { API_URL } from '../config';
4-
5-
export const getTrips = async () => {
6-
const res = await fetch(`${API_URL}/trips`, {
7-
method: 'GET',
8-
credentials: 'include',
9-
headers: { 'Content-Type': 'application/json' },
10-
});
11-
const data = await res.json();
12-
return data;
13-
};
1+
import axios from '../config/axios';
142

153
export const getTripsWithQuery = async (query: any) => {
16-
return axios.get(`${API_URL}/trips`, {
17-
params: query,
18-
headers: {
19-
'Content-Type': 'application/json',
20-
Accept: 'application/json',
21-
},
22-
});
4+
return axios.get('/trips', { params: query });
235
};
246

257
export const getRandomTrips = async (
268
type: 'videos' | 'stories' | 'photos' | '' = ''
27-
) => {
28-
return axios.get(`${API_URL}/trips/latest`, {
29-
params: type ? { [type]: true } : {},
30-
headers: {
31-
'Content-Type': 'application/json',
32-
Authorization: `Bearer ${Cookies.get('authToken')}`,
33-
Accept: 'application/json',
34-
},
35-
});
36-
};
9+
) => axios.get('/trips/latest', { params: type ? { [type]: true } : {} });
3710

38-
export const getTrip = async (trip_id: any) => {
39-
return axios.get(`${API_URL}/trips/${trip_id}`, {
40-
headers: {
41-
'Content-Type': 'application/json',
42-
Authorization: `Bearer ${Cookies.get('authToken')}`,
43-
Accept: 'application/json',
44-
},
45-
});
46-
};
11+
export const gettrip = async (trip_id: any) => axios.get(`/trips/${trip_id}`);
4712

4813
export const getTripsByLocation = async (
4914
north_lat: number,
5015
south_lat: number,
5116
west_lng: number,
5217
east_lng: number
5318
) =>
54-
axios.get(`${API_URL}/trips`, {
19+
axios.get(`/trips`, {
5520
params: {
5621
q: {
5722
from_lat_gt: south_lat,
@@ -60,47 +25,12 @@ export const getTripsByLocation = async (
6025
from_lng_lt: east_lng,
6126
},
6227
},
63-
headers: {
64-
'Content-Type': 'application/json',
65-
Accept: 'application/json',
66-
},
6728
});
6829

69-
export const createTrip = (payload: any) => {
70-
return axios.post(`${API_URL}/trips`, payload, {
71-
headers: {
72-
'Content-Type': 'application/json',
73-
Accept: 'application/json',
74-
Authorization: `Bearer ${Cookies.get('authToken')}`,
75-
},
76-
});
77-
};
78-
79-
export const deleteTrip = (id: number) => {
80-
return axios.delete(`${API_URL}/trips/${id}`, {
81-
headers: {
82-
Accept: 'application/json',
83-
Authorization: `Bearer ${Cookies.get('authToken')}`,
84-
},
85-
});
86-
};
87-
88-
export const updateTrip = (payload: any) => {
89-
return axios.put(`${API_URL}/trips/${payload.id}`, payload, {
90-
headers: {
91-
'Content-Type': 'application/json',
92-
Accept: 'application/json',
93-
Authorization: `Bearer ${Cookies.get('authToken')}`,
94-
},
95-
});
96-
};
97-
98-
export const updateRide = (payload: any) => {
99-
return axios.patch(`${API_URL}/rides/${payload.id}`, payload, {
100-
headers: {
101-
'Content-Type': 'multipart/form-data',
102-
Accept: 'application/json',
103-
Authorization: `Bearer ${Cookies.get('authToken')}`,
104-
},
105-
});
106-
};
30+
export const getTrip = async (trip_id: any) => axios.get(`/trips/${trip_id}`);
31+
export const createTrip = (payload: any) => axios.post(`/trips`, payload);
32+
export const deleteTrip = (id: number) => axios.delete(`/trips/${id}`);
33+
export const updateTrip = (payload: any) =>
34+
axios.put(`/trips/${payload.id}`, payload);
35+
export const updateRide = (payload: any) =>
36+
axios.patch(`/rides/${payload.id}`, payload);

0 commit comments

Comments
 (0)