BohemiaInteractive/bi-service

View on GitHub
tutorials/4.SDK-integration.md

Summary

Maintainability
Test Coverage
Client SDKs can be autogenerated (see [bi-service-sdk](https://github.com/BohemiaInteractive/bi-service-sdk) plugin) for web services built with the [bi-service](https://github.com/BohemiaInteractive/bi-service) framework.  
Here, we go through integration of generated service SDKs with your other (`bi-service` based) services.

### config.json5
```javascript
services: {
    user: { //service name
        public: { //app name within the service
            host: 'localhost:3000',
            protocol: 'http', //optional - defaults to http(s) depending on `ssl` option value
            ssl: false,
            npm: 'user-public-sdk' //npm package which exports the client SDK module
        }
    }
}
```

### Initialization
```javascript
    const UserServiceSDK = require('user-public-sdk')['v1.0'];

    const service = new Service(config);
    const remoteServiceMgr = service.getRemoteServiceManager();

    //1. Looks for `services.user.public.npm` option value in config
    //2. Loads `user-public-sdk` npm module
    //3. Initializes the SDK, saves it into internal register and returns the SDK object
    remoteServiceMgr.buildRemoteService('user:public:v1.0', {/*sdk constructor options*/});

    //Manual initialization is an alternative for the `buildRemoteService` call above
    const sdk = new UserServiceSDK({
            errors: { // map custom Error constructors to request response codes
            400: RequestError,
            500: ServiceError
            //accepts also all `axios` options
        }
    });
    remoteServiceMgr.add('user:public', sdk);
```

### Accessing the SDKs

```javascript

    router.buildRoute({/*options*/}).main(function(req, res) {
        const remoteServiceMgr = this.app.service.getRemoteServiceManager();
        return remoteServiceMgr.get('user:public:v1.0').getUser({
            data: {username: 'happie'}
        }).then(function(response){
        });

        //or

        router.App.service.getRemoteServiceManager()//....
    });
```