Skip to content

A simple http request library using the fetch api and modern JS.

Notifications You must be signed in to change notification settings

bushblade/fetch-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple http request library using the fetch api

closure based library

instantiate passing in a base url as first argument and any custom headers as the second argument.

// with deno
import http from 'https://raw.githubusercontent.com/bushblade/fetch-library/master/index.js'
const api = http('https://jsonplaceholder.typicode.com')

Then use it

api
  .get('/posts/1')
  .then(({ data }) => {
    console.log('✅ Got a good response:-\n', data)
  })
  .catch(({ error, data }) => {
    console.error('❌ Got a bad response:-\n', error, data)
  })

Example that handles a bad response from a server

// fake bad response with message from server
const api = http('https://run.mocky.io/v3/7d8c436e-d1dc-4857-b0ac-7e3e8047aef8')

api
  .get('/posts')
  .then(({ data }) => {
    console.log('✅ Got a good response:-\n', data)
  })
  .catch(({ error, data }) => {
    console.error('❌ Got a bad response:-\n', error, data)
  })

Each instance has methods for

  • GET
  • POST
  • PUT
  • DELETE
request method
GET .get(endPoint)
POST .post(endPoint, body)
PUT .put(endPoint, body)
DELETE .delete(endPoint)

About

A simple http request library using the fetch api and modern JS.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published