npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-protocol-http

v0.15.0

Published

This module normalizes http responses using Simple Protocol. HTTP response bodies are parsed as JSON by default but gracefully fall back to text if the response body is not valid JSON.

Downloads

38

Readme

Simple Protocol Http

This module normalizes http responses using Simple Protocol. HTTP response bodies are parsed as JSON by default but gracefully fall back to text if the response body is not valid JSON.

What is simple protocol?

Simple protocol is simple:

  1. Never intentionally throw exceptions / always return with a 200 status code.
  2. Return a valid JSON object like this for a success:
{
  success: true,
  payload: {
    // the result of the operation, i.e. an http response body
  }
}
  1. Return a valid JSON object like this for an error:
{
  success: false,
  error: {
    // error details or object
  }
}

That's it! Both success and error cases are handled the same way and can follow the same code path.

Installation

npm i --save simple-protocol-http

API

const { get, post, put, remove} = require('simple-protocol-http')

let result = await get(url)
let result = await post(url, payload)
let result = await put(url, payload)
let result = await remove(url)

If you want to send custom fetch options (i.e. send custom headers, etc):

const { get, post, put, remove} = require('simple-protocol-http').options

let result = await get(fetchOptions, url)
let result = await post(fetchOptions, url, payload)
let result = await put(fetchOptions, url, payload)
let result = await remove(fetchOptions, url)

If you don't want credentials: include to be on by default:

const { get, post, put, remove} = require('simple-protocol-http').noCredentials

let result = await get(url)
let result = await post(url, payload)
let result = await put(url, payload)
let result = await remove(url)

The whole enchilada, i.e. full referential transparency:

const { get, post, put, remove} = require('simple-protocol-http').full

let result = await get(fetch, fetchOptions, url)
let result = await post(fetch, fetchOptions, url, payload)
let result = await put(fetch, fetchOptions, url, payload)
let result = await remove(fetch, fetchOptions, url)

Examples Making Requests to Restful Endpoints

For successful / 200-range responses

const { get } = require('simple-protocol-http')
let result = await get('http://www.example.com/api')

If the server returns this with a 200 status code:

{
  value: 'foo'
}

The value of result is:

{
  success: true,
  payload: {
    value: 'foo'
  },
  meta: {
    status: 200,
    statusText: 'OK',
    headers: {...}
  }
}

For error / non-200-range responses:

const { post } = require('simple-protocol-http')
let result = await post('http://www.example.com/api', {...})

If the server returns this with a 400 status code:

{
  type: 'ValidationError',
  message: 'Some field is invalid'
}

The value of result is:

{
  success: false,
  error: {
    type: 'ValidationError',
    message: 'Some field is invalid'
  },
  meta: {
    status: 400,
    statusText: 'Bad Request',
    headers: {...}
  }
}

Example Using Simple Protocol Endpoints

For Success Responses:

const { post } = require('simple-protocol-http')
let result = await post('http://www.example.com/api', {...})

If the server returns this:

{
  success: true,
  payload: {
    foo: 'bar'
  }
}

This module will return this:

{
  success: true,
  payload: {
    foo: 'bar'
  },
  meta: {
    status: 200,
    statusText: 'OK',
    headers: {...}
  }
}

For Unsuccessful Responses:

const { post } = require('simple-protocol-http')
let result = await post('http://www.example.com/api', {...})

If the server returns this:

{
  success: false,
  error: {
    message: 'You posted something invalid'
  }
}

This module will return this:

{
  success: false,
  error: {
    message: 'You posted something invalid'
  },
  meta: {
    status: 200,
    statusText: 'OK',
    headers: {...}
  }
}