# rest-collector

> Node.js and browser http library that allows you to merge data from multiple api endpoints

Latest version **1.0.10** (published 2019-09-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install rest-collector
pnpm add rest-collector
yarn add rest-collector
bun add rest-collector
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.10 |
| Published | 2019-09-04 |
| First published | 2019-06-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 32.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | avikl, erezalster |
| Keywords | api, rest, merge, collect, http, typescript |

## Links

- npm: https://www.npmjs.com/package/rest-collector
- Repository: https://github.com/taranisag/rest-collector
- Homepage: https://github.com/taranisag/rest-collector#readme
- Issues: https://github.com/taranisag/rest-collector/issues
- npm.io page: https://npm.io/package/rest-collector

## Dependencies (2)

- [p-retry](https://npm.io/package/p-retry.md) 4.1.0
- [superagent](https://npm.io/package/superagent.md) 5.0.8

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.10 (latest) — 2019-09-04
- 1.0.9 — 2019-08-15
- 1.0.8 — 2019-08-14
- 1.0.6 — 2019-06-21
- 1.0.5 — 2019-06-21
- 1.0.4 — 2019-06-20
- 1.0.3 — 2019-06-20
- 1.0.2 — 2019-06-19
- 1.0.1 — 2019-06-19
- 1.0.0 — 2019-06-19

## README

[![Build Status](https://travis-ci.org/taranisag/rest-collector.svg?branch=master)](https://travis-ci.org/taranisag/rest-collector)
[![Coverage Status](https://coveralls.io/repos/github/taranisag/rest-collector/badge.svg?branch=master)](https://coveralls.io/github/taranisag/rest-collector?branch=master)
[![npm version](https://img.shields.io/npm/v/rest-collector.svg)](https://www.npmjs.com/package/rest-collector) 

# rest-collector
> Rest-Collector is a Node.js and browser http library that allows you to merge data from multiple api endpoints. By adding multiple mappers (explenation bellow), the library allows you to join data from multiple sources into a single entity.
## Installing
``` bash
$ npm install rest-collector
```
## Interface
```typescript
import { RestCollectorResult } from "rest-collector";

interface RestCollectorResult<E = any> {
    data: E;
    headers: any;
}
```

## Demo: https://codesandbox.io/embed/holy-frost-n222p 

## API
### Basic APIs
Sending get request
```typescript
const client: RestCollectorClient = new RestCollectorClient("http://server/api/entity/{id}")
const resultArray: RestCollectorResult = await client.get();

console.log("data", resultArray.data);
console.log("headers", resultArray.headers);

const singleData: RestCollectorResult = await client.get({
    params: { id: 1 }
});

console.log("data", singleData.data);
```

Sending post request
```typescript
const client: RestCollectorClient = new RestCollectorClient("http://server/api/entity/{id}")
const result: RestCollectorResult = await client.post({
    data: { name: "entity #1" }
});
```

Sending put request
```typescript
const client: RestCollectorClient = new RestCollectorClient("http://server/api/entity/{id}")
const result: RestCollectorResult = await client.put({
    params: { id: 1 },
    data: { name: "entity #2" }
});
```

Sending delete request
```typescript
const client: RestCollectorClient = new RestCollectorClient("http://server/api/entity/{id}")
const result: RestCollectorResult = await client.delete({
    params: { id: 1 }
});
```
### Adding Mappers
When using microservices architecture you will need to join data from entities in the application level.
```typescript
const client: RestCollectorClient = new RestCollectorClient("http://server/api/entity/{id}");
client.addMapper({
    entityAttribute: "userId",
    restAPIAttribute: "id",
    restAPIURL: "http://secondserver/api/users",
    mergeEntities: (entity: any, possibleValue: any) => {
        if(possibleValue) {
            entity.email = possibleValue.email;
            return entity;
        }
    }
});
const result = await client.get();
console.log(result.data)
```

result:
```json
{
    "name": "entity #1",
    "email": "John@deo.com"
}
```

The mapper will join data from both apis, based on the entityAttribute and restAPIAttribute we defined before.
addMapper api: 
* `entityAttribute`: The attribute of the base api on which the joining of the data will take place.
* `restAPIAttribute`: The attribute of the other api on which the joining of the data will take place.
* `restAPIURL`: The endpoint of the other api.
* `before?`: optional - a function which receives the query(get method) or payload (other http methods) and returns a new query/payload to be sent to the other api. This is usefull when you need to adjust the query/payload that you send to the api.
* `method?`: optional - the http method for the second api. Defaults to `get`.
* `retry?`: optional - the retries configuration (applies also for the client) - an object with the following properties:
    * `retries`: number of retries.
    * `onFailedAttempt?`: optional - a callback on a failed attempt.
    * other options from node-retry: https://github.com/tim-kos/node-retry#retryoperationoptions



You can add more than one mapper to join data from more than one other api.

### Adding More Than One Mapper with before method
```typescript
const client: RestCollectorClient = new RestCollectorClient("http://server/api/entity/{id}");
client.addMapper({
    entityAttribute: "userId",
    restAPIAttribute: "id",
    restAPIURL: "http://secondserver/api/users",
    mergeEntities: (entity: any, possibleValue: any) => {
        if(possibleValue) {
            entity.email = possibleValue.email;
            return entity;
        }
    }
});
client.addMapper({
    entityAttribute: "userId",
    restAPIAttribute: "id",
    restAPIURL: "http://thirdserver/api/users-courses",
    method: "post",
    before: payload => {
        return {
            otherDataForTheApi: [1, 2, 3],
            users: payload,
        };
    },
    mergeEntities: (entity: any, possibleValue: any) => {
        if(possibleValue) {
            entity.course = possibleValue.course;
            return entity;
        }
    }
});
const result = await client.get();
console.log(result.data)
```

result:
```json
{
    "name": "entity #1",
    "email": "John@deo.com",
    "course": "Mathematics"
}
```

### Decorate Requests
In most scenarios you will want to add more meta data information for a specific request such as: transaction id, authentication header or custom headers. 
```typescript
const requestDecorator: DecorateRequest = {
    decorateRequest: (req: RestCollectorRequest, bag: any): void => {
        req.headers.Authorization = "yoursecret!";
        req.headers.transactionid = bag.transactionid;
    };
}
const client: RestCollectorClient = new RestCollectorClient("http://server/api/entity/{id}", requestDecorator);
const result = await client.get({
    bag: { transactionid = "transactionid" }
});
```

### More Usage Examples - https://github.com/taranisag/rest-collector/blob/master/test/test.ts

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