Skip to content

varna/noutaa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

noutaa

Quick Start

npm i noutaa
import { GET, POST } from 'noutaa'

let sweets = await GET(`/cafe`, {
  accept: 'json'
})

console.log(sweets) // ['cookies', 'cake']

const res = await POST(`/new-cafe`, {
  json: sweets
})

console.log(res) // Response object

Features

Functions for HTTP methods

import { GET, POST, PUT, PATCH, HEAD, DELETE } from 'noutaa'

GET(`/beer`) // just a normal Promise/Response object

Query String parameters

Add or merge query string parameters without playing with strings.

// GET /beer?strength=5
GET(`/beer`, {
  params: { strength: 5 }
})

// GET /beer?strength=5&price=free
GET(`/beer?strength=5`, {
  params: { price: 'free' }
})

JSON body

// with noutaa
const res = await POST(`/nudes`, {
  json: { url: 'http://gph.is/1oo2GZg' }
})

// without noutaa
const res = await fetch(`/nudes`, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json; charset=utf-8'
  },
  body: JSON.stringify({ url: 'http://gph.is/1oo2GZg' })
})

x-www-form-urlencoded body

// with noutaa
const res = await POST(`/form`, {
  urlencoded: { message: 'hello' }
})

// without noutaa
const body = new URLSearchParams()
body.append('message', 'hello')

const res = await fetch(`/form`, {
  method: 'POST',
  body,
  headers: {
    'application/x-www-form-urlencoded'
  }
})

Error catching

All response statuses that are not OK are thrown as HTTPError.
res.ok are all status codes in 200s range.

try {
  await DELETE(`/beer/999`);
} catch (error) {
  switch (error.code) {
    case '403': console.log(`You shalt not pass!`) break;
    case '404': console.log(`You missed!`) break;
    default: return;
  }
}

TODO

  • Hooks
  • Authentication header for easy JWT
  • form-data

License

MIT