Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

chickendinosaur-http

0.0.5 • Public • Published

http-js

Front-end, super slim, singleton, , http library with promises. The goal was to have a lightweight http library without having to use jQuery or having too many dependencies of libraries that will never get used anywhere else but still get loaded. Less is more!

Notes

Universal module defined to be used with requirejs, commonjs, node, or global scoped if no module loader is used.

  • All files in the dist folder are minified for production use.
  • All files in the src directory are the source code for development use.
  • Packages point at the dist minified code with source maps.

Development

Requirements

  • nodejs
  • npm install
  • npm install -g gulp

Test

gulp test

Gulp Commands

Each process is dependent upon the previous. If one fails the build process exits.

  • gulp
  • gulp test (Unit specifications)
  • gulp build (Test, folder clean-ups, minification, source maps, renaming)
  • gulp deploy (Test, build, versioning)

Usage

Installation

bower: bower install chickendinosaur-http

How to use...

Uses the es6-promise library which is a smaller subset of the RSVP promise library so please check the documentation of those for more functionality. It's the smallest and most used library that I was able to find at the moment without writing my own.

// Global usage.
var Http = ChickenDinosaur.Http;

// Basic 'GET' with a query parameter.
Http.get('chickenosaurus' {
    id: 'Snow Piercer'
}).then(function(res) {
    // res is the XHR.responseText object which has already parsed by JSON.parse if the response was JSON.
    console.log(res);
}, function(err) {
    // err is the entire XHR object to be able to access anything needed for debugging.
    console.log(err);
});

// Promise chaining
Http.get('chickenosaurus' {
    id: 'Snow Piercer'
})
.then(function(res) {
        // res is the XHR.responseText object which has already parsed by JSON.parse if the response was JSON.
        console.log(res);

        return Http.post('chickenosaurus', {
            name: 'Dylan Riley',
            skills: ['Sand Shredding', 'Skimming'],
            diet: 'Wild Turkey.',
            alias: 'Popcorn Frog'
        });
    },
    function(err) {
        // err is the entire XHR object to be able to access anything needed for debugging.
        console.log(err);
    })
.then(function(res) {
        // res is the XHR.responseText object which has already parsed by JSON.parse if the response was JSON.
        console.log(res);

        return Http.put('chickenosaurus', {
            name: 'Kevin Fincel',
            skills: ['Killing everything in South Dakota', 'Nerdpress'],
            diet: 'Jalepeno infused vodka.',
            alias: 'The Incubationer'
        }, {
            id: '3452EW3453UN5'
        });
    },
    function(err) {
        // err is the entire XHR object to be able to access anything needed for debugging.
        console.log(err);
    })
.then(function(res) {
        // res is the XHR.responseText object which has already parsed by JSON.parse if the response was JSON.
        console.log(res);

        return Http.delete('chickenosaurus', {
            id: '3452EW3453UN5'
        });
    },
    function(err) {
        // err is the entire XHR object to be able to access anything needed for debugging.
        console.log(err);
    })
.then(function(res) {
        // res is the XHR.responseText object which has already parsed by JSON.parse if the response was JSON.
        console.log(res);

        return Http.delete('chickenosaurus', {
            id: '3452EW3453UN5'
        });
    },
    function(err) {
        // err is the entire XHR object to be able to access anything needed for debugging.
        console.log(err);
    });

You could create each Http call seperately like:

	ES6Promise.all([promise1, promise2])
	.then(function(posts){
		// posts is an array of results.
		})
	.catch(function(reason){
		// if any promise fails.
		});

and have a single callback when they all have received a response or error thanks to the promise library which is the reason I went with it due to have needed the extra functionality for resolving multiple things in enterprise projects.

Release Notes

v0.0.1

install

npm i chickendinosaur-http

Downloadsweekly downloads

3

version

0.0.5

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar