@shangs/fetch

针对fetch封装

Usage no npm install needed!

<script type="module">
  import shangsFetch from 'https://cdn.skypack.dev/@shangs/fetch';
</script>

README

fetch

install

npm i @shangs/fetch --save-dev

useage

import fetchs from '@shangs/fetch';
let config = {
    mode: 'no-cors',
    credentials: 'same-origin',
    headers: {
        contentType: 'application/json',
        accept: 'application/json',
    }
    timeout: 5000
};
// GET
fetchs
    .get('http://localhost:3003/students', config)
    .then(response => {
        console.log(response);
    })
    .catch(e => {
        console.log(e);
    });

fetchs
    .get('http://localhost:3003/students', {params: {name: 'Lily'}}, config)
    .then(response => {
        console.log(response);
    })
    .catch(e => {
        console.log(e);
    });

// POST
fetchs
    .post(' http://localhost:3003/students', data, config)
    .then(response => {
        console.log(response);
    })
    .catch(e => {
        console.log(e);
    });
// interceptors
fetchs.interceptors.request(config => {
    config = {
        timeout: 5000,
        mode: 'cors',
        credentials: 'include'
    };
    return config;
});

fetchs.interceptors.response(
    reponse => {
        return reponse;
    },
    err => {}
);