api.js
function requestWithTokenCheck (defOptions, credentials, token, options, callback) {
  var returnedPromise;
  
  options.headers = options.headers || {};
  extend(true, options, defOptions);
  
  if (Date.now() - token.Timestamp > tokenUpdateTime) {
    returnedPromise = signIn(defOptions, credentials).then(function (data) {
      if (data[0].statusCode !== 200) {
        throw new Error(`${data[0].statusCode}: ${data[0].body.Message}`);
      } else {
        token.AuthenticationToken = data[0].body.AuthenticationToken;
        token.Timestamp = Date.now();
        options.headers.Authorization = `Basic ${token.AuthenticationToken}`;
        return request(options);
      }
    });
  } else {
    options.headers.Authorization = `Basic ${token.AuthenticationToken}`;
    returnedPromise = request(options);
  }
  
  returnedPromise.nodeify(callback);
  return returnedPromise;
}
function requestWithTokenCheck (defOptions, credentials, token, options, callback) {
  var returnedPromise;
  
  options.headers = options.headers || {};
  extend(true, options, defOptions);
  
  if (Date.now() - token.Timestamp > tokenUpdateTime) {
    returnedPromise = signIn(defOptions, credentials).then(function (data) {
      if (data[0].statusCode !== 200) {
        throw new Error(`${data[0].statusCode}: ${data[0].body.Message}`);
      } else {
        token.AuthenticationToken = data[0].body.AuthenticationToken;
        token.Timestamp = Date.now();
        options.headers.Authorization = `Basic ${token.AuthenticationToken}`;
        return request(options);
      }
    });
  } else {
    options.headers.Authorization = `Basic ${token.AuthenticationToken}`;
    returnedPromise = request(options);
  }
  
  returnedPromise.nodeify(callback);
  return returnedPromise;
}
function requestWithTokenCheck ( defOptions , credentials , token , options , callback ) { var returnedPromise ; options . headers = options . headers || { } ; extend ( true , options , defOptions ) ; if ( Date . now ( ) - token . Timestamp > tokenUpdateTime ) { returnedPromise = signIn ( defOptions , credentials ) . then ( function ( data ) { if ( data [ 0 ] . statusCode !== 200 ) { throw new Error ( ` ${ data [ 0 ] . statusCode } ${ data [ 0 ] . body . Message } ` ) ; } else { token . AuthenticationToken = data [ 0 ] . body . AuthenticationToken ; token . Timestamp = Date . now ( ) ; options . headers . Authorization = ` ${ token . AuthenticationToken } ` ; return request ( options ) ; } } ) ; } else { options . headers . Authorization = ` ${ token . AuthenticationToken } ` ; returnedPromise = request ( options ) ; } returnedPromise . nodeify ( callback ) ; return returnedPromise ; }
Wrapper function for all Nfield API requests that checks if API token is not outdated, and refreshes it otherwise
Wrapper function for all Nfield API requests that checks if API token is not outdated and refreshes it otherwise