(apiUrl = '', action)
| 8 | (uniqueFileId, load, error) => { } |
| 9 | */ |
| 10 | export const createRevertFunction = (apiUrl = '', action) => { |
| 11 | // is custom implementation |
| 12 | if (typeof action === 'function') { |
| 13 | return action; |
| 14 | } |
| 15 | |
| 16 | // no action supplied, return stub function, interface will work, but file won't be removed |
| 17 | if (!action || !isString(action.url)) { |
| 18 | return (uniqueFileId, load) => load(); |
| 19 | } |
| 20 | |
| 21 | // set onload hanlder |
| 22 | const onload = action.onload || (res => res); |
| 23 | const onerror = action.onerror || (res => null); |
| 24 | |
| 25 | // internal implementation |
| 26 | return (uniqueFileId, load, error) => { |
| 27 | const request = sendRequest( |
| 28 | uniqueFileId, |
| 29 | apiUrl + action.url, |
| 30 | action // contains method, headers and withCredentials properties |
| 31 | ); |
| 32 | request.onload = (xhr) => { |
| 33 | load( |
| 34 | createResponse( |
| 35 | 'load', |
| 36 | xhr.status, |
| 37 | onload(xhr.response), |
| 38 | xhr.getAllResponseHeaders() |
| 39 | ) |
| 40 | ) |
| 41 | }; |
| 42 | |
| 43 | request.onerror = (xhr) => { |
| 44 | error( |
| 45 | createResponse( |
| 46 | 'error', |
| 47 | xhr.status, |
| 48 | onerror(xhr.response) || xhr.statusText, |
| 49 | xhr.getAllResponseHeaders() |
| 50 | ) |
| 51 | ); |
| 52 | }; |
| 53 | |
| 54 | request.ontimeout = createTimeoutResponse(error); |
| 55 | |
| 56 | return request; |
| 57 | }; |
| 58 | }; |
no test coverage detected