0.1.1 • Published 8 years ago

apicall v0.1.1

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

Facilitate request promise

apiCall

Service node to facilitate the request promise front end to back end.

Simple example:

var body = {user: 'user', password: 'pass'};
var api = require("apicall")("Your API_PREFIX","Your API_ENDPOINT",{bearer: "Your API_TOKEN"});
var test = api.apiCall("/", api.method.POST, body); //Returns a request promise

GET Exemple:

var body = null;
var api = require("apicall")("", "http://httpbin.org", null);
var test = api.apiCall("/", api.method.GET, body);
test
    .then(function(data){
        //data.data === resp server data
        //data.status === resp server status
        console.log(data);
     })
    .catch(function(error){
        //error === server error
        throw error;
    });

Method's:

var api = require("apicall")("","http://httpbin.org",null);

//GET
api.method.GET // Returns: 'GET'
//POST
api.method.POST // Returns: 'POST'
//PUT
api.method.PUT // Returns: 'PUT'
//DELETE
api.method.DELETE // Returns: 'DELETE'

Authentication token bearer example:

var prefix = "";
var endpoint = "http://localhost:3000";
var apiAuth = {bearer: "iLoveScothScoth"};
var api = require("apicall")(prefix, endpoint, apiAuth); //Authentication is configured with "Bearer iLoveScothScoth"

Data in the url or query parameter:

var prefix = "";
var endpoint = "http://localhost:3000";
var apiAuth = {bearer: "iLoveScothScoth"};
var api = require("apicall")(prefix, endpoint, apiAuth); //Authentication is configured with "Bearer iLoveScothScoth"

var data = {
    id: '1231231231231'
};

// Example 1
var test = api.apiCall('users/{id}', api.method.GET, data);
test
    .then(function(result){
        console.log(result.status);
        console.log(result.data);
    })
    .catch(function(error){
        console.log(error);
    });
        
// Example 2
var test2 = api.apiCall('users?id={id}', api.method.GET, data); // "?id={id}" {id} is the data object key
test2
    .then(function(result){
        console.log(result.status);
        console.log(result.data);
    })
    .catch(function(error){
        console.log(error);
    });

Catching errors

var api = require("apicall")("","http://httpbin.org", null);
api.apiGetErr(error,res);

Nodejs app example:

var express = require('express');
var app = express();
var api = require("apicall")("","http://httpbin.org", null);
 
app.get('/', function (req, res) {
    api.apiCall('/post', api.method.POST, {html: "<html><body><h1>Hello World</h1></body></html>"})
        .then(function(dataApi){
            var htmlReceived = JSON.parse(dataApi.data.data).html;
            res.status(dataApi.status).send(htmlReceived);
        })
        .catch(function(errorApi){
            api.apiGetErr(errorApi,res);
        })
})
 
app.listen(3000);
0.1.1

8 years ago

0.1.0

8 years ago

0.0.5-b

8 years ago

0.0.5

8 years ago

0.0.41

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago