-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathGithubApiHelper.js
More file actions
29 lines (25 loc) · 1.08 KB
/
GithubApiHelper.js
File metadata and controls
29 lines (25 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const axios = require('axios');
const User = require('../models/UserModel');
/**
* Creates an authenticated or unauthenticated GitHub API client based on session
* @param {Object} session - Express session object containing userId
* @returns {Promise<Object>} Axios instance configured for GitHub API
*/
const createGithubApi = async (session) => {
const headers = { 'Accept': 'application/vnd.github.v3+json' };
if (session?.userId) {
try {
const user = await User.findById(session.userId);
if (user?.githubAccessToken) {
headers['Authorization'] = `token ${user.githubAccessToken}`;
console.log(`Making authenticated GitHub API request for user ${user.username}.`);
return axios.create({ baseURL: 'https://api.github.com', headers });
}
} catch (dbError) {
console.error("Error fetching user for authenticated API call:", dbError.message);
}
}
console.log('Making unauthenticated GitHub API request (fallback).');
return axios.create({ baseURL: 'https://api.github.com', headers });
};
module.exports = { createGithubApi };