matthew-andrews / isomorphic-fetch

Isomorphic WHATWG Fetch API, for Node & Browserify
MIT License
6.96k stars 290 forks source link

fetch api #198

Open s-mostafa-d opened 2 years ago

s-mostafa-d commented 2 years ago

Hello guys, I am working on json lottie and I wrote a color editor for these files with JavaScript and now I want this editor on the site page to be connected to the lottie file with the API, and since the code for lottie files is very large, for example, Take the color key to changes the color I searched the internet a lot to find my problem and I realized it can be done with fetch functions I found a code on the Internet but it only loads the file on the site console I want to change the value and the user download the new color If my explanation was vague, the site https://lordicon.com/icons is exactly my explanation. I appreciate any help in this regard. This is my code.

     <script>
          const getBtn = document.getElementById('get-btn');
          const postBtn = document.getElementById('post-btn');
          const sendHttpRequest = (method, url, data) => {
            return fetch(url, {
              method: method,
              body: JSON.stringify(data),
              headers: data ? { 'Content-Type': 'application/json' } : {}
            }).then(response => {
              if (response.status >= 400) {
                // !response.ok
                return response.json().then(errResData => {
                  const error = new Error('Something went wrong!');
                  error.data = errResData;
                  throw error;
                });
              }
              return response.json();
            });
          };
          const getData = () => {
            sendHttpRequest('GET', 'https://maycoweb.com/library/2021/10/warm.json').then(responseData => {
              console.log(responseData);
            });
          };
          const sendData = () => {
            sendHttpRequest('POST', 'https://maycoweb.com/library/2021/10/warm.json', {
              email: 'eve.holt@reqres.in'
              // password: 'pistol'
            })
              .then(responseData => {
                console.log(responseData);
              })
              .catch(err => {
                console.log(err, err.data);
              });
          };
          getBtn.addEventListener('click', getData);
          postBtn.addEventListener('click', sendData);
          </script>