• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atlp-rwanda / alpha-ec-fe / c6466e9d-203f-4960-a1a5-75e7b0fa2088

28 Jun 2024 04:37PM UTC coverage: 86.228% (+0.5%) from 85.723%
c6466e9d-203f-4960-a1a5-75e7b0fa2088

Pull #33

circleci

Sophie Mukamugema
Adding product review
Pull Request #33: feat(Adding product review): A user should be able to leave a review on a product

91 of 125 branches covered (72.8%)

Branch coverage included in aggregate %.

34 of 35 new or added lines in 2 files covered. (97.14%)

1 existing line in 1 file now uncovered.

1349 of 1545 relevant lines covered (87.31%)

1.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.81
/src/utils/requests.ts
1
'use client';
1✔
2

1✔
3
import axios, {
1✔
4
  AxiosInstance,
1✔
5
  AxiosRequestConfig,
1✔
6
  Method,
1✔
7
  AxiosResponse,
1✔
8
  AxiosError,
1✔
9
  InternalAxiosRequestConfig,
1✔
10
  AxiosHeaders
1✔
11
} from 'axios';
1✔
12

1✔
13
export const URL = process.env.NEXT_PUBLIC_API_URL;
1✔
14

1✔
15
export const axiosInstance: AxiosInstance = axios.create({
1✔
16
  baseURL: `${URL}/api`,
1✔
17
  headers: {
1✔
18
    'Content-Type': 'application/json'
1✔
19
  }
1✔
20
});
1✔
21

1✔
22
interface CustomAxiosRequestConfig extends InternalAxiosRequestConfig {
1✔
23
  authenticate?: boolean;
1✔
24
}
1✔
25

1✔
26
interface CustomAxiosConfig extends AxiosRequestConfig {
1✔
27
  authenticate?: boolean;
1✔
28
}
1✔
29

1✔
30
export let token: string | null = null;
1✔
31
if (typeof window !== 'undefined') {
1✔
32
  const tokenString = localStorage.getItem('token');
1✔
33
  if (tokenString) {
1!
34
    try {
×
35
      token = JSON.parse(tokenString);
×
36
    } catch (error) {
×
37
      console.error('Failed to parse token from localStorage', error);
×
38
    }
×
39
  }
×
40
}
1✔
41

1✔
42
axiosInstance.interceptors.request.use(
1✔
43
  (config: CustomAxiosRequestConfig) => {
1✔
44
    if (config.authenticate && token) {
27!
45
      config.headers = config.headers || {};
×
46
      config.headers.Authorization = `Bearer ${token}`;
×
47
    }
×
48
    return config;
27✔
49
  },
27✔
50
  error => {
1✔
51
    return Promise.reject(error);
×
52
  }
×
53
);
1✔
54

1✔
55
// axiosInstance.interceptors.response.use(
1✔
56
//   response => {
1✔
57
//     return response;
1✔
58
//   },
1✔
59
//   (error: AxiosError) => {
1✔
60
//     if (error.response?.status === 401) {
1✔
61
//       localStorage.removeItem('token');
1✔
62
//       if (
1✔
63
//         typeof window !== 'undefined' &&
1✔
64
//         !window.location.pathname.includes('/login')
1✔
65
//       ) {
1✔
66
//         window.location.href = '/login';
1✔
67
//       }
1✔
68
//     }
1✔
69
//     if (error.response?.status === 403) {
1✔
70
//       window.location.href = '/';
1✔
71
//     }
1✔
72
//     return Promise.reject(error);
1✔
73
//   }
1✔
74
// );
1✔
75

1✔
76
export const axiosRequest = async <TRequest = any, TResponse = any>(
1✔
77
  method: Method,
27✔
78
  url: string,
27✔
79
  data?: TRequest,
27✔
80
  authenticate?: boolean
27✔
81
): Promise<AxiosResponse<TResponse>> => {
27✔
82
  const headers = { ...axiosInstance.defaults.headers.common };
27✔
83

27✔
84
  if (authenticate && token) {
27!
NEW
85
    headers['Authorization'] = `Bearer ${token}`;
×
UNCOV
86
  }
×
87

27✔
88
  const requestConfig: CustomAxiosConfig = {
27✔
89
    method,
27✔
90
    url,
27✔
91
    data,
27✔
92
    headers
27✔
93
  };
27✔
94

27✔
95
  return axiosInstance.request<TResponse>(requestConfig);
27✔
96
};
27✔
97

1✔
98
export default axiosInstance;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc