Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 1x 1x 4x 4x 3x 3x 1x 1x 5x 4x 3x 3x 1x 1x 4x 4x 2x 2x 6x 24x 16x | import JSend from 'jsend'; import JSendError from './JSendError'; const JSEND_FIELDS = ['status', 'code', 'data', 'message']; export default function apply(axios) { return axios.interceptors.response.use( response => { Iif (!response.data) return response; if (!JSend.isValid(response.data)) return response; const { data, ...jsend } = pick(JSEND_FIELDS, response.data); if (jsend.status !== 'success') throw createError(response, { jsend, data }); Object.assign(response, { jsend, data }); return response; }, err => { if (!err.response || !err.response.data) throw err; if (!JSend.isValid(err.response.data)) throw err; const { data, ...jsend } = pick(JSEND_FIELDS, err.response.data); if (jsend.status !== 'success') throw createError(err.response, { jsend, data }); Object.assign(err, { jsend, data }); throw err; }); } export { JSendError }; function createError(response, { jsend, data }) { const options = { jsend, data, response }; if (jsend.status === 'fail') { return new JSendError('Request failed', options); } return new JSendError(`Request returned an error: ${jsend.message}`, options); } function pick(props, obj) { return props.reduce((acc, key) => { if (!obj.hasOwnProperty(key)) return acc; return Object.assign(acc, { [key]: obj[key] }); }, {}); } |