Support Topics Documentation Slack YouTube Blog

Interceptors issue

angular

(Rajasekhar Puligadda) #1

How to use interceptors in angular for backendless calls


(Vladimir Upirov) #2

Hello Rajasekhar

Why do you need that?, our JS-SDK is completely ready for work without any middleware.

Anyways, you can patch Backendless Request module in way something like this:

  const backendlessRequestDecorator = (function decorateBackendlessRequest() {
  const preMiddleware = []
  const postMiddleware = []

  const _nativeRequestSend = Backendless.Request.prototype.send

  Backendless.Request.prototype.send = function() {
    const request = this

    preMiddleware.forEach(middleware => middleware(request))

  return _nativeRequestSend.apply(request, arguments)
    .then(result => {
      preMiddleware.forEach(middleware => middleware(request, null, result))

    return result
  })
  .catch(error => {
    postMiddleware.forEach(middleware => middleware(request, error))

    throw error
      })
  }

 return {
    addPreMiddleware : middleware => preMiddleware.push(middleware),
    addPostMiddleware: middleware => postMiddleware.push(middleware),
  }

})()

backendlessRequestDecorator.addPreMiddleware(request => {
  console.log('request.path', this.path)
  console.log('request.queryParams', this.queryParams)
  console.log('request.headers', this.headers)
  ...
  request.set('my-custom-header', 'some-value')
})

backendlessRequestDecorator.addPostMiddleware((request, error, result) => {
  if (error) {
    console.log('error', error)
  } else {
    console.log('result', result)
  }
}) 


Regards, Vlad


(Rajasekhar Puligadda) #3

can we check online/offline status or slow network detection using backendless?


(Vladimir Upirov) #4

I assume the question is not related to Backendless, you should search the solution somewhere on JS forums, for example: https://stackoverflow.com/


(Vladimir Upirov) #5

here is the first link what I found https://davidwalsh.name/detecting-online


(Rajasekhar Puligadda) #6

Thank you for your response