easier-requests

Package to make it easier to write simple networking code. Targeted towards students and hobbyists, but hopefully useful to anyone.

Usage no npm install needed!

<script type="module">
  import easierRequests from 'https://cdn.skypack.dev/easier-requests';
</script>

README

Table of Contents

  1. easier-requests
    1. Install
    2. Usage

easier-requests

easier-requests is a package to allow HTTP requests to be made in a more synchronous manner inside an async function.

Install

npm i easier-requests

Usage

// Import requester instance from easier-requests module
import requester from "easier-requests";

// Requester.get() is designed to be used in an async function
async function makeCards() {
  const cardsContainer = document.querySelector('.cards-container');

  const id = requester.createUniqueID(); // get a unique id
  // perform request
  await requester.get('https://example.herokuapp.com/articles', id);

  // retrieve response, checking for an error
  const response = requester.response(id);
  if (response === undefined) {
    const errorMessage = 'ERROR RETRIEVING ARTICLES: ' +
          `${requester.error(id)}`;
    const errorDisplay = document.createElement('h1');
    errorDisplay.textContent = errorMessage;
    cardsContainer.appendChild(errorDisplay);
    return;
  }

    // process data
    const topics = response.data.articles;
    for (let topic in topics)
      for (let article of topics[topic]) {
        const card = makeCard(article);
        cardsContainer.appendChild(card);
      };
  }