Skip to content

Commit 46052d3

Browse files
committed
return data object from axios
1 parent 2f570b6 commit 46052d3

4 files changed

Lines changed: 22 additions & 7 deletions

File tree

src/features/carbonAds/api/getAd.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ import { ExtractFnReturnType, QueryConfig } from 'src/lib/react-query';
33
import { Ad } from "../types";
44
import { axios } from 'src/lib/axios';
55

6-
const getAd = async (): Promise<Ad> => {
6+
type Response = {
7+
ads: Ad[]
8+
}
9+
10+
const getAd = async (): Promise<Ad | null> => {
711
const userAgent = new URLSearchParams(navigator.userAgent).toString()
812

9-
return axios.get('/monetization/', {
13+
return axios.get<Ad | null>('/monetization/', {
1014
params: {
1115
useragent: userAgent
1216
}
1317
}).then(response => {
14-
if (response.data?.ads?.length) {
15-
return response.data.ads[0];
16-
}
18+
const data = response as unknown as Response;
19+
if (!!data.ads.length) {
20+
return data.ads[0];
21+
}
22+
return null;
1723
});
1824
}
1925

src/features/changelog/api/getVersions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const getAd = async (): Promise<Version[]> => {
77

88
return axios.get<Version[]>('https://api.github.com/repos/medyo/hackertab.dev/releases')
99
.then(response => {
10-
const versions = response.data as unknown as Version[];
10+
const versions = response as unknown as Version[];
1111
return versions
1212
})
1313
}

src/lib/axios.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Axios from 'axios';
22
import { getBaseApi } from 'src/utils/DataUtils'
3+
import { ResponseInterceptor } from "./interceptors/ResponseInterceptor";
34

45
export const axios = Axios.create({
56
baseURL: getBaseApi(null)
6-
});
7+
});
8+
axios.interceptors.response.use(ResponseInterceptor);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { AxiosResponse } from 'axios';
2+
3+
export const ResponseInterceptor = (
4+
response: AxiosResponse<any>
5+
): AxiosResponse<any> => {
6+
return response.data;
7+
};

0 commit comments

Comments
 (0)