src/network/fetch.js
function wrappingFetch({ url, method, data }, optionsArg) {
    let requestStatus = createRequestStatus();
    // Here we are using destruct to filter properties we do not want to give to fetch.
    // CORS and isCORS are useless legacy code, xhrErrors is used only in error parsing
    // eslint-disable-next-line no-unused-vars
    let { CORS, isCORS, xhrErrors, ...config } = configGetter();
    const { noStringify, ...options } = optionsArg || {};
    const reqOptions = merge({ headers: {} }, config, options, { method, body: noStringify ? data : JSON.stringify(data) });
    //By default, add json content-type
    if (!reqOptions.noContentType && !reqOptions.headers['Content-Type']) {
        reqOptions.headers['Content-Type'] = 'application/json';
    }
    // Set the requesting as pending
    updateRequestStatus({ id: requestStatus.id, status: 'pending' });
    // Do the request
    return fetch(url, reqOptions)
        // Catch the possible TypeError from fetch
        .catch(error => {
            updateRequestStatus({ id: requestStatus.id, status: 'error' });
            return Promise.reject({ globalErrors: [error] });
        }).then(response => {
            updateRequestStatus({ id: requestStatus.id, status: response.ok ? 'success' : 'error' });
            const contentType = response.headers.get('content-type');
            return getResponseContent(response, reqOptions.dataType ? reqOptions.dataType : contentType && contentType.includes('application/json') ? 'json' : 'text');
        }).catch(data => {
            checkErrors(data, xhrErrors);
            return Promise.reject(data);
        });
}
function wrappingFetch({ url, method, data }, optionsArg) {
    let requestStatus = createRequestStatus();
    // Here we are using destruct to filter properties we do not want to give to fetch.
    // CORS and isCORS are useless legacy code, xhrErrors is used only in error parsing
    // eslint-disable-next-line no-unused-vars
    let { CORS, isCORS, xhrErrors, ...config } = configGetter();
    const { noStringify, ...options } = optionsArg || {};
    const reqOptions = merge({ headers: {} }, config, options, { method, body: noStringify ? data : JSON.stringify(data) });
    //By default, add json content-type
    if (!reqOptions.noContentType && !reqOptions.headers['Content-Type']) {
        reqOptions.headers['Content-Type'] = 'application/json';
    }
    // Set the requesting as pending
    updateRequestStatus({ id: requestStatus.id, status: 'pending' });
    // Do the request
    return fetch(url, reqOptions)
        // Catch the possible TypeError from fetch
        .catch(error => {
            updateRequestStatus({ id: requestStatus.id, status: 'error' });
            return Promise.reject({ globalErrors: [error] });
        }).then(response => {
            updateRequestStatus({ id: requestStatus.id, status: response.ok ? 'success' : 'error' });
            const contentType = response.headers.get('content-type');
            return getResponseContent(response, reqOptions.dataType ? reqOptions.dataType : contentType && contentType.includes('application/json') ? 'json' : 'text');
        }).catch(data => {
            checkErrors(data, xhrErrors);
            return Promise.reject(data);
        });
}
function wrappingFetch ( { url , method , data } , optionsArg ) { let requestStatus = createRequestStatus ( ) ; // Here we are using destruct to filter properties we do not want to give to fetch. // CORS and isCORS are useless legacy code, xhrErrors is used only in error parsing // eslint-disable-next-line no-unused-vars let { CORS , isCORS , xhrErrors , ... config } = configGetter ( ) ; const { noStringify , ... options } = optionsArg || { } ; const reqOptions = merge ( { headers : { } } , config , options , { method , body : noStringify ? data : JSON . stringify ( data ) } ) ; //By default, add json content-type if ( ! reqOptions . noContentType && ! reqOptions . headers [ 'Content-Type' ] ) { reqOptions . headers [ 'Content-Type' ] = 'application/json' ; } // Set the requesting as pending updateRequestStatus ( { id : requestStatus . id , status : 'pending' } ) ; // Do the request return fetch ( url , reqOptions ) // Catch the possible TypeError from fetch . catch ( error => { updateRequestStatus ( { id : requestStatus . id , status : 'error' } ) ; return Promise . reject ( { globalErrors : [ error ] } ) ; } ) . then ( response => { updateRequestStatus ( { id : requestStatus . id , status : response . ok ? 'success' : 'error' } ) ; const contentType = response . headers . get ( 'content-type' ) ; return getResponseContent ( response , reqOptions . dataType ? reqOptions . dataType : contentType && contentType . includes ( 'application/json' ) ? 'json' : 'text' ) ; } ) . catch ( data => { checkErrors ( data , xhrErrors ) ; return Promise . reject ( data ) ; } ) ; }
Fetch function to ease http request.
@param  {object} obj - method: http verb, url: http url, data:The json to save.
@param  {object} options - The options object.
@return {CancellablePromise} The promise of the execution of the HTTP request.
Fetch function to ease http request .