Skip to content

Instantly share code, notes, and snippets.

@LukeMwila
Created May 8, 2019 19:07
Show Gist options
  • Save LukeMwila/d49831d43bafa25c741eb9ccd0125b49 to your computer and use it in GitHub Desktop.
Save LukeMwila/d49831d43bafa25c741eb9ccd0125b49 to your computer and use it in GitHub Desktop.
Function that handles making api requests
/**
* API Request
* @param endPoint - api endpoint
* @param httpMethod - the http method defining the type of request (POST/GET/PUT/PATCH)
* @param bodyParams - object with properties being passed with the request
*/
export const apiRequest = async (
endPoint: string,
httpMethod: string,
bodyParams?: object
): Promise<any> => {
const response = await fetch(endPoint, {
method: httpMethod,
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(bodyParams)
});
return await response.json();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment