UNPKG

lrc-mcp

Version:
53 lines (46 loc) 1.53 kB
/** * Function to get an auth token from LoadRunner Cloud. * * @returns {Promise<string|Object>} - The access token or an error message object. */ const getAuthToken = async () => { const baseUrl = process.env.LRC_BASE_URL; const tenantId = process.env.LRC_TENANT_ID; const clientId = process.env.LRC_CLIENT_ID; const clientSecret = process.env.LRC_CLIENT_SECRET; try { const url = new URL(`${baseUrl}/auth-client`); url.searchParams.append('TENANTID', tenantId); const headers = { 'Content-Type': 'application/json', 'accept': 'application/json' }; const body = JSON.stringify({ client_id: clientId, client_secret: clientSecret }); const response = await fetch(url.toString(), { method: 'POST', headers, body }); if (!response.ok) { const text = await response.text(); try { const errorData = JSON.parse(text); throw new Error(JSON.stringify(errorData)); } catch (jsonErr) { // Not JSON, log the raw text console.error('Non-JSON error response:', text); throw new Error(text); } } const data = await response.json(); // Adjust according to actual API response structure return data.access_token || data.token || data; } catch (error) { console.error('Error retrieving auth token:', error); return { error: 'An error occurred while retrieving auth token.' }; } }; export { getAuthToken };