Skip to content

Instantly share code, notes, and snippets.

@JackFGreen
Created January 9, 2019 13:19
Show Gist options
  • Save JackFGreen/f48b8d2a962c88240e35b0a544d7275f to your computer and use it in GitHub Desktop.
Save JackFGreen/f48b8d2a962c88240e35b0a544d7275f to your computer and use it in GitHub Desktop.
handle api response
/**
* handle api response
* @param {Object} res api response
* @param {Object} options handler res
* @param {Function} options.success
* @param {Object} options.fail each code with handler { code: handler }
* @param {Function|Boolean} options.error
*/
export function handleApiRes (res, { success, fail = null, error = true } = {}) {
const code = res.code
if (isFunction(success) && code === CODE_SUCCESS) {
success()
return
}
if (isObject(fail)) {
const handler = fail[code]
if (isFunction(handler)) {
handler()
return
}
}
if (isFunction(error)) {
error()
} else if (error) {
window.noti.error({
message: res.message
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment