Skip to content

Commit

Permalink
gist11113
Browse files Browse the repository at this point in the history
  • Loading branch information
0xjaqbek committed Jul 13, 2024
1 parent bfa9d9e commit 05e02ca
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"vite-plugin-node-polyfills": "^0.7.0"
},
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/node": "^18.11.18",
"@types/react": "^18.0.27",
"@types/react-dom": "^18.0.10",
Expand Down
36 changes: 26 additions & 10 deletions src/gistService.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
import axios from 'axios';
import axios, { AxiosError } from 'axios';
import dotenv from 'dotenv';

dotenv.config();

const GIST_ID = 'c21b8e37060a00aadcae5f277543ef0c'; // Example Gist ID
const GITHUB_TOKEN = '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'; // Example GitHub Token
const GIST_ID = 'c21b8e37060a00aadcae5f277543ef0c';
const GITHUB_TOKEN = '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';

if (!GITHUB_TOKEN) {
throw new Error('GitHub token not found. Please set the GITHUB_TOKEN environment variable.');
}

const apiClient = axios.create({
baseURL: 'https://api.github.com',
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`, // Use "Bearer" instead of "token"
Authorization: `token ${GITHUB_TOKEN}`, // Use "token" instead of "Bearer"
'Content-Type': 'application/json',
},
});

const isAxiosError = (error: unknown): error is AxiosError => {
return axios.isAxiosError(error);
};

export const getLeaderboard = async () => {
try {
console.log('Fetching leaderboard...');
const response = await apiClient.get(`/gists/${GIST_ID}`);
console.log('Response from GitHub API:', response);
console.log('Response from GitHub API:', response.data);
const leaderboard = JSON.parse(response.data.files['leaderboard.json'].content);
console.log('Leaderboard:', leaderboard);
return leaderboard;
} catch (error) {
console.error('Error fetching leaderboard:', error);
} catch (error: unknown) {
if (isAxiosError(error)) {
console.error('Error fetching leaderboard:', error.response ? error.response.data : error.message);
} else {
console.error('Unexpected error fetching leaderboard:', error);
}
return [];
}
};
Expand All @@ -40,10 +52,14 @@ export const updateLeaderboard = async (leaderboard: { address: string; time: nu
},
},
});
console.log('Response from GitHub API:', response);
console.log('Response from GitHub API:', response.data);
return response.data;
} catch (error) {
console.error('Error updating leaderboard:', error);
} catch (error: unknown) {
if (isAxiosError(error)) {
console.error('Error updating leaderboard:', error.response ? error.response.data : error.message);
} else {
console.error('Unexpected error updating leaderboard:', error);
}
return null;
}
};
23 changes: 15 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,13 @@
resolved "https://registry.npmjs.org/@twa-dev/types/-/types-6.4.2.tgz"
integrity sha512-LwPdC5+CyYVqowiw3oxuHvL3F/oid72nFMqB+N+GyjwMhxKmwKtlMSBVKG5G1S3CNSwRUDZBFXOnukBvcC84/Q==

"@types/axios@^0.14.0":
version "0.14.0"
resolved "https://registry.npmjs.org/@types/axios/-/axios-0.14.0.tgz"
integrity sha512-KqQnQbdYE54D7oa/UmYVMZKq7CO4l8DEENzOKc4aBRwxCXSlJXGz83flFx5L7AWrOQnmuN3kVsRdt+GZPPjiVQ==
dependencies:
axios "*"

"@types/eslint-scope@^3.7.3":
version "3.7.7"
resolved "https://registry.npmjs.org/@types/eslint-scope/-/eslint-scope-3.7.7.tgz"
Expand Down Expand Up @@ -1153,14 +1160,7 @@ available-typed-arrays@^1.0.5:
resolved "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.5.tgz"
integrity sha512-DMD0KiN46eipeziST1LPP/STfDU0sufISXmjSgvVsoU2tqxctQeASejWcfNtxYKqETM1UxQ8sp2OrSBWpHY6sw==

axios@^0.25.0:
version "0.25.0"
resolved "https://registry.npmjs.org/axios/-/axios-0.25.0.tgz"
integrity sha512-cD8FOb0tRH3uuEe6+evtAbgJtfxr7ly3fQjYcMcuPlgkwVS9xboaVIpcDV+cYQe+yGykgwZCs1pzjntcGa6l5g==
dependencies:
follow-redirects "^1.14.7"

axios@^1.7.2:
axios@*, axios@^1.7.2:
version "1.7.2"
resolved "https://registry.npmjs.org/axios/-/axios-1.7.2.tgz"
integrity sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==
Expand All @@ -1169,6 +1169,13 @@ axios@^1.7.2:
form-data "^4.0.0"
proxy-from-env "^1.1.0"

axios@^0.25.0:
version "0.25.0"
resolved "https://registry.npmjs.org/axios/-/axios-0.25.0.tgz"
integrity sha512-cD8FOb0tRH3uuEe6+evtAbgJtfxr7ly3fQjYcMcuPlgkwVS9xboaVIpcDV+cYQe+yGykgwZCs1pzjntcGa6l5g==
dependencies:
follow-redirects "^1.14.7"

"babel-plugin-styled-components@>= 1.12.0":
version "2.0.7"
resolved "https://registry.npmjs.org/babel-plugin-styled-components/-/babel-plugin-styled-components-2.0.7.tgz"
Expand Down

0 comments on commit 05e02ca

Please sign in to comment.