Micro-library to make http request simply in the browser or with a NodeJs server.
$ npm install molecularjs
$ bower install molecularjs
// First require the library
var Molecular = require('molecular');
Use Molecular like that
// Connect to your favorites APIs
Molecular.connect({
'Github': 'https://api.github.com',
'Slack' : 'http://api.slack.com'
});
// Set some options
// There are all options from the basic nodejs https module
Molecular.to('Github').setOptions({
headers: {
'user-agent': 'ArthurMialon'
}
});
// Make a simply request to get some events
Molecular.to('Github').get('/users/arthurmialon/events')
.progress(function(req) {
console.log("request progress");
})
.success(function(data, req) {
console.log(data, req);
})
.error(function(err, req) {
console.log(err);
});
Use it almost like in nodejs : You just have to import the file to your website
<!-- Import it to your website -->
<script src="molecular.js"></script>
More doc coming soon...
See all your connections to APIs
Name | Type |
---|---|
APIs | Object |
Molecular.connect({
'Github': 'https://api.github.com',
'Slack' : 'https://api.slack.com'
});
Name | Type |
---|---|
apiName | String |
@return | Object |
Molecular.to('ApiName')
Name | Type |
---|---|
url | String |
params | Object |
@return | Callback Object |
Molecular.get('http://your/api/endpoints', {limit: 2, orderby: "id", sort: "desc"});
Name | Type |
---|---|
url | String |
data | Object |
@return | Callback Object |
Molecular.post('http://your/api/endpoints', {});
Name | Type |
---|---|
url | String |
data | Object |
@return | Callback Object |
Molecular.put('http://your/api/endpoints', {});
Name | Type |
---|---|
url | String |
@return | Callback Object |
Molecular.delete('http://your/api/endpoints');
Name | Type |
---|---|
name | String |
callback | Function |
Molecular.setMethod('methodName', function(arguments, callback) {
// Do stuff and apply the callback
});
Name | Type |
---|---|
options | Object |
Molecular.setOptions({
headers: {
"ContentType": "Application/json"
}
});
Name | Type |
---|---|
method | String |
path | String |
data | Object / Boolean |
options | Object |
@return | Callback Object |
Molecular.sendRequest('GET', 'http://your/api/endpoint', false, {});
You can simply add some methods to your connections
For example if I want to get the last commit from a specific repo (i.e: SailsJs)
// Set a new method to the api
Molecular.to('Github').setMethod('lasCommit', function(owner, repo, callback) {
this.get('/repos/'+owner+'/'+repo+'/commits')
.success(function(data) {
// Add JSON.parse(data) in nodejs to data instead of data[0]
callback.apply(this, [false, data[0]]);
})
.error(function(err) {
callback.apply(this, [true, undefined]);
});
});
// Get the last commit from sailsJs
Molecular.to('Github').lasCommit('balderdashy', 'sails', function(err, commit) {
(err) ? console.error(err) : console.log(commit);
});
- Support http & https
- Better options management (JSON and default options etc...)
- Call en error if status code >= 200 & < 300 for http module nodejs
- Get the body response on error
- Fixes on POST/PUT request
- Fixes on options with xhr
- Minified version for bower
- Automatic JSON.parse on data