npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dbc-node-serviceprovider

v3.1.0

Published

Provider for the Node modules that accesesthe DBC webservices

Downloads

22

Readme

dbc-node-serviceprovider

David David Build Status Coverage Status

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.

Transforms

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

ServiceClients

A ServiceClient is an implementation of a client that handles communication with a service

API 1.0

Provider

The provider needs to be initialized with a config file before usage.

  const Provider = require('dbc-node-serviceprovider');
  const provider = Provider(config);

Provider.registerServiceClient(client):ServiceClient

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);
    }
  });

Provider#registerTransform(client):Transform

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;
    }
  });

Transform#callServiceClient:Promise

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

Provider#trigger(event, params):Promise

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)
    });  

Provider#bootstrap

activates the bundled transforms and clients

  provider.boostrap();

Provider#setupSockets

Setup socket api

  const app = express();
  const server = require('http').Server(app);
  const socket = require('socket.io').listen(server);
  
  provider.setupSockets(socket);

Events

The provider inherits the Events API which is used to register events from the registered transforms

Events#addEvent

add an event object

  Events.add('type', 'eventName', Function||Object);

Events#getEvent

Retrieve a single event object

  const event = Events.get('type', 'eventName');

Events#getEventsOfType

Returns a map of events of a specified type

  const map = Events.getEventsOfType('type');
  map.forEach((value, eventName) => console.log.bind(console));
  

Bundled clients

  • MoreInfo.client.js
  • OpenSearch.client.js
  • OpenSuggest.client.js
  • PopSuggest.client.js
  • Recommendations.client.js

Bundled transforms

  • CoverImage.transform.js
  • ResultList.transform.js
  • Work.transform.js
  • OpenSuggest.transform.js
  • PopSuggest.transform.js
  • Recommendations.transform.js

API 2.0

Provider

The provider needs to be initialized with a config file before usage.

  const Provider = require('dbc-node-serviceprovider');
  const provider = Provider(someLoggerNotRequired);

Provider.registerServiceClient(name, client):ServiceClient

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);

Provider#registerTransform(object):Transform

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;
    }
  });

Transform#callServiceClient:Promise (DEPRICATED)

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

Provider#trigger(event, params):Promise

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)
    });  

Provider#dispatcher

Setup socket api

  const app = express();
  const server = require('http').Server(app);
  const socket = require('socket.io').listen(server);
  
  provider.dispatcher(socket);

API 3.0

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.

Provider.dispatcher


  module.exports.run = function (worker) {
    const app = express();
    const server = worker.httpServer;
    const scServer = worker.getSCServer();
    
    server.on('request', app);
    
    ...
    
    provider.dispatcher(worker);
  }