/* * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ var axios = require('axios'); /** * Attaches a given access token to a MS Graph API call * @param endpoint: REST API endpoint to call * @param accessToken: raw access token string */ async function fetch(endpoint, accessToken, param) { const options = { headers: { Authorization: `Bearer ${accessToken}` } }; if (!param) { param = {}; } console.log(`request made to ${endpoint} at: ` + new Date().toString()); try { const response = await axios.get(endpoint, options, param); return await response.data; } catch (error) { throw new Error(error); } } async function updateFetch(endpoint, accessToken, params) { const options = { headers: { Authorization: `Bearer ${accessToken}` } }; console.log(`request made to ${endpoint} at: ` + new Date().toString()); if (!params) { params = {}; } else { params = JSON.parse(params); } try { const response = await axios.post(endpoint, params, options); return await response.data; } catch (error) { throw new Error(error); } } module.exports = { fetch : (endpoint, accessToken)=> fetch(endpoint, accessToken), updateFetch: (endpoint, accessToken, params)=> updateFetch(endpoint, accessToken, params), }