Giter VIP home page Giter VIP logo

simple-protocol-http's Introduction

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: {...}
  }
}

simple-protocol-http's People

Contributors

orourkedd avatar psfrankie avatar videoservices avatar

Watchers

 avatar  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.