Giter Site home page Giter Site logo

http-transport's Introduction

HttpTransport

Build Status NPM version

A basic promise-driven Javascript interface for HTTP requests.

Installation

$ npm install http-transport

Rationale

  • Directly accessing HTTP data sources in domain code leads to difficulty in testing and is a violation of SRP.
  • Complicated data abstraction layers are often too heavy-handed or not flexible enough.
  • Writing isomorphic code that uses HTTP requests can be difficult.

Overview

This is a very small wrapper around the httpinvoke module that compresses the external API into the four primary HTTP verbs as methods that return a Promise.

The HttpTransport class is meant to be instantiated once and shared throughout the application when domain code needs access to the HTTP layer, e.g. calling REST services.

Using a simple facade to make GET request vs e.g $.get() or the Node http module allows for clean, testable, isomorphic code.

Methods

var HttpTransport = require('http-transport');

var transport = new HttpTransport();

transport.get(url, params?): Promise

Execute a GET request with optional query parameters and return a Promise for the response.

// Basic GET
transport.get('/users')
  .then(function(users) {
    ...
  });

// With query params (fetches /users?group=admins)
transport.get('/users', { group: 'admins' })
  .then(function(users) {
    ...
  });

transport.post(url, data?): Promise

Execute a POST request with optional object data passed as application/x-www-form-urlencoded and return a Promise for the response.

transport.post('/blogs', { content: 'Hello, world!' })
  .then(function(blog) {
    ...
  });

transport.put(url, data?): Promise

Execute a PUT request with optional object data passed as application/x-www-form-urlencoded and return a Promise for the response.

transport.put('/users', { id: 123, name: 'Billy' })
  .then(function(user) {
    ...
  });

transport.delete(url, params?): Promise

Execute a DELETE request with optional query parameters and return a Promise for the response.

transport.delete('/users', { id: 123 })
  .then(function(resp) {
    ...
  });

JSONP

The JsonpTransport class also implements the AbstractTransport interface and can be used the same as the HttpTransport class, except it will throw an error if any method other than GET is used.

var JsonpTransport = require('http-transport').JsonpTransport;

var transport = new JsonpTransport();

transport.get('http://api.external.com/').then(...);

Testing

$ npm test

License

MIT

http-transport's People

Contributors

bvalosek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

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