import axios from 'axios'; export default async function fetchGraph(endpoint: string, token: string, extraHeaders = {}) { try { const url = `https://graph.microsoft.com/v1.0${endpoint}`; console.log('📡 Calling:', url); // Decode token to inspect (only for debugging) try { const tokenParts = token.split('.'); if (tokenParts.length === 3) { const payload = JSON.parse(Buffer.from(tokenParts[1], 'base64').toString()); console.log('🔍 Token info:', { aud: payload.aud, iss: payload.iss, exp: new Date(payload.exp * 1000).toISOString(), scp: payload.scp, upn: payload.upn || payload.preferred_username, }); } } catch (e) { console.log('⚠️ Could not decode token'); } const response = await axios.get(url, { headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', ...extraHeaders, }, }); return response.data; } catch (error: any) { if (error.response) { const errorData = error.response.data; console.error('Graph API Error:', { status: error.response.status, data: errorData, headers: error.response.headers, wwwAuthenticate: error.response.headers['www-authenticate'], }); throw new Error( `Graph API error (${error.response.status}): ${errorData.error?.message || JSON.stringify(errorData)}` ); } throw new Error(`Graph API request failed: ${error.message}`); } } export async function postGraph(endpoint: string, token: string, body: any, extraHeaders = {}) { try { const url = `https://graph.microsoft.com/v1.0${endpoint}`; console.log('📡 Posting to:', url); console.log('📦 Body:', JSON.stringify(body, null, 2)); const response = await axios.post(url, body, { headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', ...extraHeaders, }, }); return response.data; } catch (error: any) { if (error.response) { const errorData = error.response.data; console.error('Graph API Error:', { status: error.response.status, data: errorData, headers: error.response.headers, wwwAuthenticate: error.response.headers['www-authenticate'], }); throw new Error( `Graph API error (${error.response.status}): ${errorData.error?.message || JSON.stringify(errorData)}` ); } throw new Error(`Graph API request failed: ${error.message}`); } } export async function patchGraph(endpoint: string, token: string, body: any, extraHeaders = {}) { try { const url = `https://graph.microsoft.com/v1.0${endpoint}`; console.log('📡 Patching to:', url); console.log('📦 Body:', JSON.stringify(body, null, 2)); const response = await axios.patch(url, body, { headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', ...extraHeaders, }, }); return response.data; } catch (error: any) { if (error.response) { const errorData = error.response.data; console.error('Graph API Error:', { status: error.response.status, data: errorData, headers: error.response.headers, wwwAuthenticate: error.response.headers['www-authenticate'], }); throw new Error( `Graph API error (${error.response.status}): ${errorData.error?.message || JSON.stringify(errorData)}` ); } throw new Error(`Graph API request failed: ${error.message}`); } }