# apicall

> Facilitate request promise # apiCall

Latest version **0.1.1** (published 2016-08-06) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install apicall
pnpm add apicall
yarn add apicall
bun add apicall
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2016-08-06 |
| First published | 2016-03-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 5 |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Clayton Santos |
| Maintainers | davidpvilaca |
| Keywords | api, apiCall, apicall |

## Links

- npm: https://www.npmjs.com/package/apicall
- Repository: https://github.com/xdevelsistemas/apiCall
- Homepage: https://github.com/xdevelsistemas/apiCall#readme
- Issues: https://github.com/xdevelsistemas/apiCall/issues
- npm.io page: https://npm.io/package/apicall

## Dependencies (3)

- [extend](https://npm.io/package/extend.md) ^3.0.0
- [string-format](https://npm.io/package/string-format.md) ^0.5.0
- [request-promise](https://npm.io/package/request-promise.md) ^2.0.1

## Recent versions

- 0.1.1 (latest) — 2016-08-06
- 0.1.0 — 2016-08-06
- 0.0.5-b — 2016-03-07
- 0.0.5 — 2016-03-07
- 0.0.41 — 2016-03-07
- 0.0.4 — 2016-03-07
- 0.0.3 — 2016-03-04
- 0.0.2 — 2016-03-04
- 0.0.1 — 2016-03-04

## README

<!--todo exemplificar melhor-->
# apiCall

Service [node](http://nodejs.org) to facilitate the request promise front end to back end.

Simple example:
```js
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:
```js
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:
```js
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:
```js
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:
```js
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
```js
var api = require("apicall")("","http://httpbin.org", null);
api.apiGetErr(error,res);
```


Nodejs app example:
```js
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);
```

---
_Source: https://npm.io/package/apicall · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
