chickendinosaur-http
Version:
Front-end, super slim, ajax, xhr, http library with promises.
135 lines (105 loc) • 4.78 kB
Markdown
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!
<h1>Notes</h1>
Universal module defined to be used with <b>requirejs</b>, <b>commonjs</b>, <b>node</b>, or <b>global scoped</b> if no module loader is used.
- All files in the <b>dist</b> folder are minified for <b>production</b> use.
- All files in the <b>src</b> directory are the source code for <b>development</b> use.
- Packages point at the <b>dist</b> minified code with <b>source maps</b>.
<h1>Development</h1>
<h4>Requirements</h4>
- nodejs
- npm install
- npm install -g gulp
<h4>Test</h4>
gulp test
<h4>Gulp Commands</h4>
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)
<h1>Usage</h1>
<h4>Installation</h4>
bower: bower install chickendinosaur-http
<h4>How to use...</h4>
Uses the es6-promise library which is a smaller subset of the [RSVP](https://github.com/tildeio/rsvp.js) 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.
<h1>Release Notes</h1>
<h3>v0.0.1</h3>