Skip to content

Commit e6de3aa

Browse files
authored
Update apiProxy.ts
1 parent 33970d5 commit e6de3aa

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/services/apiProxy.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
1-
import axios from 'axios';
1+
// services/apiProxy.ts
2+
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
23

3-
// Create a proxy service to hide actual API endpoints from users
44
class ApiProxyService {
55
private userAgents = [
66
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
77
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
88
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:123.0) Gecko/20100101 Firefox/123.0'
99
];
1010

11-
// Fetch asset details from GitHub API
12-
async fetchAssetDetails(assetId: number): Promise<any> {
11+
// Method to fetch the GitHub release data.
12+
async fetchReleaseData(releaseId: string | number): Promise<any> {
13+
const endpoint = `https://api.github.com/repos/Pymmdrza/Rich-Address-Wallet/releases/${releaseId}`;
14+
// Use the existing proxyRequest method. This is MUCH better.
1315
try {
14-
const response = await axios.get(`/api/github/rich-address-wallet/releases/${assetId}`, {
15-
headers: {
16-
'Accept': 'application/json',
17-
'User-Agent': this.getRandomUserAgent()
18-
}
16+
const response = await this.proxyRequest(endpoint, {
17+
headers: {
18+
'Accept': 'application/vnd.github.v3+json', // Good practice to specify the API version.
19+
},
1920
});
20-
2121
return response.data;
2222
} catch (error) {
23-
console.error('Error fetching asset details:', error);
24-
throw error;
23+
console.error('Error fetching release data:', error);
24+
throw error; // Re-throw the error so the caller can handle it.
2525
}
2626
}
2727

2828
// Generic proxy method for any external API
2929
async proxyRequest(
30-
endpoint: string,
31-
options: Record<string, any> = {}
32-
): Promise<any> {
30+
endpoint: string,
31+
options: AxiosRequestConfig = {}
32+
): Promise<AxiosResponse> {
3333
try {
34+
// Add random user agent to avoid being blocked
3435
const headers = {
3536
...options.headers,
3637
'User-Agent': this.getRandomUserAgent()
3738
};
3839

40+
// Make the request through our proxy endpoint
3941
return await axios({
4042
...options,
41-
url: `/api/proxy?endpoint=${encodeURIComponent(endpoint)}`,
42-
headers
43+
url: `/api/proxy?endpoint=${encodeURIComponent(endpoint)}`, // Correctly encode the endpoint.
44+
headers,
4345
});
4446
} catch (error) {
4547
console.error('Error in proxy request:', error);
@@ -52,5 +54,6 @@ class ApiProxyService {
5254
}
5355
}
5456

57+
// Create a singleton instance
5558
const apiProxy = new ApiProxyService();
5659
export default apiProxy;

0 commit comments

Comments
 (0)