Skip to content

Instantly share code, notes, and snippets.

@wldcordeiro
Created April 28, 2018 04:52
Show Gist options
  • Save wldcordeiro/36fc6c4be13f12b20205f2447b1e1c7f to your computer and use it in GitHub Desktop.
Save wldcordeiro/36fc6c4be13f12b20205f2447b1e1c7f to your computer and use it in GitHub Desktop.
converting to RxJS 6
function retryAuth(req$, params) {
return req$.retryWhen(errors =>
errors.mergeMap(error => {
if (error.status === httpStatus.UNAUTHORIZED) {
// If rawAjax is successful, it will retry
return rawAjax({
...params,
url: '/authentication/reup',
}).catch(err => {
// Reupping was not allowed. Redirect to login
if (err.status === httpStatus.UNAUTHORIZED) {
redirectToAuth()
return Observable.throw(error)
}
return Observable.throw(error)
})
}
return Observable.throw(error)
})
)
}
const raw = (...params) => {
return retryAuth(ajax(...params), defaultParameters).catch(
err => {
// Report the error
ReportError.next(formatError(err))
// Return the original error, so that it can be handled individually
return Observable.throw(err)
}
)
}
const withMethod = method => (...params) => {
return retryAuth(
ajax({ ...params, method }),
defaultParameters
).catch(err => {
// Report the error
ReportError.next(formatError(err))
// Return the original error, so that it can be handled individually
return Observable.throw(err)
})
}
incommmming
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment