Abstraction layer for the DBC webservices. Handles the communication between the application- and the service layer providing optional transforms for transforming the data received from the services before returning it to the client.
A transform is a form of event that takes a request and calls one or more services.
When service calls are resolved the Transform transforms the response into the desired format.
A transform is created with provider.registerTransform
A ServiceClient is an implementation of a client that handles communication with a service
The provider needs to be initialized with a config file before usage.
const Provider = require('dbc-node-serviceprovider');
const provider = Provider(config);
Method for registering serviceclients. Serviceclients need a name that refers to a config namespace, and an init() that provides the configurations and should return the client methods.
import Recommendations from 'dbc-node-recommendations';
provider.registerServiceClient({
name: 'recommend',
init(config) {
return Recommendations(config.endpoint);
}
});
Method for registering transform classes. Transforms need events, requestTransform, responseTransform
provider.registerTransform({
event() {
return 'transformEvent';
},
requestTranform(request) {
// make a call to one or more services, using the callServiceClient method
return this.callServiceClient('moreinfo::method', request);
},
requestResponse(response) {
// do something with the reponse
return response;
}
});
On a transform it is possible to make calls to registered clients in the following format
const promise = this.callServiceClient('client', 'method', params);
callServiceClient returns a promise that should be returned from the requestTransform
Triggers an event on the provider. If the event does not exists an error is thrown.
A transform with the corresponding event needs to be registered first.
the trigger method returns a Promise
const recommendations = Provider.trigger('recommend', {like: ['123123123']});
recommendations
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error)
});
activates the bundled transforms and clients
provider.boostrap();
Setup socket api
const app = express();
const server = require('http').Server(app);
const socket = require('socket.io').listen(server);
provider.setupSockets(socket);
The provider inherits the Events API which is used to register events from the registered transforms
add an event object
Events.add('type', 'eventName', Function||Object);
Retrieve a single event object
const event = Events.get('type', 'eventName');
Returns a map of events of a specified type
const map = Events.getEventsOfType('type');
map.forEach((value, eventName) => console.log.bind(console));
The provider needs to be initialized with a config file before usage.
const Provider = require('dbc-node-serviceprovider');
const provider = Provider(someLoggerNotRequired);
Method for registering serviceclients. Serviceclients need a name that refers to a config namespace, and an initialized client
import Recommendations from 'dbc-node-recommendations';
const recommendations = Recommendations(config);
provider.registerServiceClient('recommend', recommendations);
Method for registering transform classes. Transforms need events, requestTransform, responseTransform
provider.registerTransform({
event() {
return 'transformEvent';
},
requestTranform(request) {
// make a call to one or more services, using the callServiceClient method
return this.clients.recommend.getRecommendations(request);
},
requestResponse(response) {
// do something with the reponse
return response;
}
});
Call serviceClients through the clients object on the transform
const promise = this.clients.nameSpace.method(params);
Client calls always returns a promise that should be returned from the requestTransform
Triggers an event on the provider. If the event does not exists an error is thrown.
A transform with the corresponding event needs to be registered first.
the trigger method returns a Promise
const recommendations = Provider.trigger('recommend', {like: ['123123123']});
recommendations
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error)
});
Setup socket api
const app = express();
const server = require('http').Server(app);
const socket = require('socket.io').listen(server);
provider.dispatcher(socket);
The new 3.0 api is almost identical to the 2.0 api, except it requires a socketcluster worker or something with an identical api.
module.exports.run = function (worker) {
const app = express();
const server = worker.httpServer;
const scServer = worker.getSCServer();
server.on('request', app);
...
provider.dispatcher(worker);
}