# vizz.microservice-client

> Library for interact in the microservice with the api-gateway (register, unregister, do request to other microservices, etc)

Latest version **1.6.2** (published 2017-07-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install vizz.microservice-client
pnpm add vizz.microservice-client
yarn add vizz.microservice-client
bun add vizz.microservice-client
```

## 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.6.2 |
| Published | 2017-07-27 |
| First published | 2016-07-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Ra |
| Maintainers | raul.requero |
| Keywords | register, microservice, api-gateway, vizzuality |

## Links

- npm: https://www.npmjs.com/package/vizz.microservice-client
- Repository: https://github.com/Vizzuality/microservice-client
- Homepage: https://github.com/Vizzuality/microservice-client#readme
- Issues: https://github.com/Vizzuality/microservice-client/issues
- npm.io page: https://npm.io/package/vizz.microservice-client

## Dependencies (5)

- [co](https://npm.io/package/co.md) 4.6.0
- [url](https://npm.io/package/url.md) 0.11.0
- [debug](https://npm.io/package/debug.md) 2.2.0
- [yaml-js](https://npm.io/package/yaml-js.md) 0.1.3
- [co-request](https://npm.io/package/co-request.md) 1.0.0

## Recent versions

- 1.6.2 (latest) — 2017-07-27
- 1.6.1 — 2017-07-27
- 1.6.0 — 2017-07-27
- 1.5.4 — 2017-03-09
- 1.5.3 — 2016-11-07
- 1.5.2 — 2016-11-03
- 1.5.1 — 2016-10-26
- 1.5.0 — 2016-10-26
- 1.4.1 — 2016-10-26
- 1.4.0 — 2016-09-14
- 1.3.0 — 2016-09-01
- 1.2.6 — 2016-07-31
- 1.2.5 — 2016-07-30
- 1.2.4 — 2016-07-30

## README

# register-microservice-client
Library to register/unregister microservice in the api-gateway. This library implement the /info endpoint that the api-gateway uses to obtain info of the registered microservices.
**IMPORTANT** Now only support koajs 1.x framework. Soon, we support expressjs, etc.
## Install
````
npm install --save vizz.microservice-client
````

## Use in microservice
In listen callback of koajs app add the next code:
````
    var promise = require('register-microservice-client').register({
        id: config.get('service.id'),
        name: config.get('service.name'),
        dirConfig: path.join(__dirname, '../microservice'),
        dirPackage: path.join(__dirname, '../../'),
        logger: logger,
        app: app //koa app object,
        callbackUpdate: <function> // this callback is called when the api-gateway call to info endpoint. Is called with the new token and api-gateway url (same object that getInfo() returns)
    });
    p.then(function() {}, function(err) {
        logger.error(err);
        process.exit(1);
    });
````

This code, call to register library with the config of the microservice. All config is required. Is necesary defined 1 environment variables when you are developing in develop environment. This variables are:

*  API_GATEWAY_URL =  Url of the api-gateway. For example: http://192.168.99.100:8000


## API Reference
### register([opts]) => Return Promise Object
Register /info endpoint and in local environment, it make a request to /refresh endpoint in api-gateway to the api-gateway refresh his configuration.
```
// Config the microservice client to listen /info endpoint in this microservice.
    var p = require('vizz.microservice-client').register({
        id: config.get('service.id'),
        name: config.get('service.name'),
        dirConfig: path.join(__dirname, '../microservice'),
        dirPackage: path.join(__dirname, '../../'),
        logger: logger,
        app: app
    });
    p.then(function() {}, function(err) {
        logger.error(err);
        process.exit(1);
    });
```

| Param | Type | Description |
| --- | --- | --- |
| [opts] | <code>Object</code> |  |
| [opts.id] | <code>String (required)</code> | Id of the service. Is used to replace in register.json |
| [opts.name] | <code>String (required)</code> | Name of the service. Is used to replace in register.json |
| [opts.dirConfig] | <code>String (required)</code> | Folder dir where it is the config of the microservice (public-swagger.yml, swagger.yml and register.json) |
| [opts.dirPackage] | <code>String (required)</code> | Folder dir where it is the package.json file |
| [opts.logger] | <code>Object</code> | Object to show logs. If you don't give, library use console.log |
| [opts.app] | <code>Object (required)</code> | Koa app object. Is used to register /info endpoint |


### requestToMicroservice([opts]) => Return result of request.
Method to call to other microservices registered in the same api-gateway. Is a generator function. Add to opts, the token of authentication with the api-gateway. Use [co-request](https://github.com/denys/co-request#readme) library.
Example:
```
let result = yield require('vizz.microservice-client').requestToMicroservice({
            uri: '/geostore/' + hashGeoStore,
            method: 'GET',
            json: true
        });
```
| Param | Type | Description |
| --- | --- | --- |
| [opts] | <code>Request config Object</code> |  |

### setDataConnection([opts])
Method to manually config the authentication token and api-gateway url.
Example:
```
let result = yield require('vizz.microservice-client').setDataConnection({
            apiGatewayUrl:'http://192.168.1.10:5000',
            authenticationToken: 'a245614bca9...'
        });
```
| Param | Type | Description |
| --- | --- | --- |
| [opts] | <code>Object</code> |  |
| [opts.apiGatewayUrl] | <code>String</code> | Url of api-gateway |
| [opts.authenticationToken] | <code>String</code> | Authentication token |

### getInfo() => Return result of request.
Return api-gateway url and token obtained when api-gateway call to /info endpoint.
Example:
```
let result = yield require('vizz.microservice-client').getInfo();
```
| Result | Type | Description |
| --- | --- | --- |
| [result] | <code>Object</code> |  |
| [result.apiGatewayUrl] | <code>String</code> | Url of api-gateway |
| [result.authenticationToken] | <code>String</code> | Authentication token |

# TODO:
* [ ]  Add support to express framework

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