Giter Site home page Giter Site logo

fetcher's Introduction

Fetcher

Fetcher is a utility designed to expedite working with http requests in Javascript. Fetcher makes the following assumptions:

  1. The API is RESTful
  2. The API returns JSON
  3. The user will construct the endpoint parameters correctly

Each request method a pre-baked method in Fetcher that can be modified by overloading the method and calling the super method.

  • getOptions
  • putOptions
  • patchOptions
  • postOptions
  • deleteOptions

Using Fetcher

template.secret.mjs

This template.secret.mjs file is included for convenience, although not sctrictly necessary. These are necessary for the components for the OddsAPI, Trello, and WeatherAPI. These are for testing and I recommend not using them. You can also use dotenv on NPM

the Constructor

constructor(base_url, parameters)

The constructor takes the base_url and query parameters that will remain consistent across all requests.

parameters method

the parameters() method returns a copy of the parameter object from the constructor.

Option methods

You can create a fetcher specific to the API you will work with. Each method follows the following format: method(endpoint, obj={body: {}, parameters: {}}). The exception is the GET method.

getData(endpiont, obj={parameters: {}})

The Get Methods don't have bodies, so even if you include one in the method will not use it.

PUT(endpoint, obj={body: {}, URLParameters: {}})
POST(endpoint, obj={body: {}, URLParameters: {}})
PATCH(endpoint, obj={body: {}, URLParameters: {}})
DELETE(endpoint, obj={body: {}, URLParameters: {}})

The body is the body payload. The URLParameters modifies the URL to include query parameters.

Extending Fetcher

Class Name

use 'Fetcher'

class ClassName extends Fetcher

the Methods

Options

There are options for each request type using the convention [request]Options(). They all return the same object with the minor difference of the method string.

{
    credentials: 'same-origin',
    headers: {
        Accept: 'application/json, text/plain, */*'
    },
    method: 'GET'
}
getOptions
putOptions
postOptions
patchOptions
deleteOptions

To modify the options, call super.getOptions() and append the object.

Credits

Cover photo by Mia Anderson on Unsplash

fetcher's People

Contributors

jauntyjocularjay avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.