Created
January 9, 2019 13:19
-
-
Save JackFGreen/f48b8d2a962c88240e35b0a544d7275f to your computer and use it in GitHub Desktop.
handle api response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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