saplings/profile/src/http.js
/**
 * Wrapper function to set up XHR.
 * @param {string}      method    HTTP method for the request
 * @param {string}      url       endpoint to make the request to
 * @param {object}      data      Byte array representation of the request body
 * @param {function}    headerFn  Function to set the correct request headers
 */
Wrapper function to set up XHR.
@param {string}      method    HTTP method for the request
@param {string}      url       endpoint to make the request to
@param {object}      data      Byte array representation of the request body
@param {function}    headerFn  Function to set the correct request headers
Wrapper function to set up XHR . @param { string } method HTTP method for the request @param { string } url endpoint to make the request to @param { object } data Byte array representation of the request body @param { function } headerFn Function to set the correct request headers
async function http(method, url, data, headerFn) {
  return new Promise((resolve, reject) => {
    const request = new XMLHttpRequest();
    request.open(method, url);
    request.withCredentials = true;
    if (headerFn) {
      headerFn(request);
    }
    request.onload = () => {
      if (request.status >= 200 && request.status < 300) {
        resolve(request.response);
      } else {
        reject(request);
      }
    };
    request.onerror = () => {
      reject(
        Error(
          'The server has encountered an error. Please contact the administrator.'
        )
      );
    };
    request.send(data);
  });
}
async function http ( method , url , data , headerFn ) { return new Promise ( ( resolve , reject ) => { const request = new XMLHttpRequest ( ) ; request . open ( method , url ) ; request . withCredentials = true ; if ( headerFn ) { headerFn ( request ) ; } request . onload = ( ) => { if ( request . status >= 200 && request . status < 300 ) { resolve ( request . response ) ; } else { reject ( request ) ; } } ; request . onerror = ( ) => { reject ( Error ( 'The server has encountered an error. Please contact the administrator.' ) ) ; } ; request . send ( data ) ; } ) ; }
Wrapper function to set up XHR.
Wrapper function to set up XHR .