Files
xod/packages/xod-func-tools/src/fetch.js
Evgeny Kochetkov e6ab08e297 chore(infra): format code with prettier
Just `yarn lint  --fix`
2018-03-05 17:59:03 +03:00

30 lines
831 B
JavaScript

import * as R from 'ramda';
/**
* Universal function, that normilizes fetch result to
* rejected Promise. Useful to use with `retryOrFail` as `errFn`.
*
* There is two possible cases for `res` argument:
* - contains Error object (E.G. network error)
* it will just merge a payload to the Error.
* - contains response object (E.G. unwanted response status)
* it will create new Error with statusText and merged
* status, statusText and payload
*/
// :: Object -> (FetchResult | Error) -> Error
export const rejectFetchResult = R.curry(
(payload, res) =>
res.status
? Object.assign(
new Error(res.statusText),
{
status: res.status,
statusText: res.statusText,
},
payload
)
: Object.assign(res, payload)
);
export default {};