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

loopback-connector-grpc

v2.0.0

Published

LoopBack connector for gRPC services

Downloads

715

Readme

loopback-connector-grpc

The gRPC connector enables LoopBack applications to interact with gRPC services.

Installation

In your application root directory, enter:

$ npm install loopback-connector-grpc --save

This will install the module from npm and add it as a dependency to the application's package.json file.

Configuration

To interact with a gRPC API, configure a data source backed by the gRPC connector:

With code:

  var ds = loopback.createDataSource('grpc', {
    connector: 'loopback-connector-grpc',
    spec: 'note.proto',
  });

With JSON in datasources.json (for example, with basic authentication):

"gRPCDataSource": {
    "name": "gRPCDataSource",
    "connector": "grpc",
    "spec": "note.proto",
    "security": {
      "type" : "basic", 
      "username": "the user name",
      "password": "thepassword"
}    

Data source properties

Specify the options for the data source with the following properties.

| Property | Description | Default | |----------|-------------|-----------| | connector | Must be 'loopback-connector-grpc' to specify gRPC connector| None | |spec | HTTP URL or path to the gRPC specification file (with file name extension .yaml/.yml or .json). File path must be relative to current working directory (process.cwd()).| None | |validate | When true, validates provided spec against gRPC specification 2.0 before initializing a data source. | false| | security | Security configuration for making authenticated requests to the API. | None |

Authentication

Basic authentication:

security: {
  rootCerts: 'rootCerts.crt', // Path to root certs
  key: 'gprc.key', // Path to client SSL private key
  cert: 'grpc.crt' // Path to client SSL certificate
}

Creating a model from the gRPC data source

The gRPC connector loads the API specification document asynchronously. As a result, the data source won't be ready to create models until it is connected. For best results, use an event handler for the connected event of data source:

ds.once('connected', function(){
  var PetService = ds.createModel('PetService', {});
  ...
});

Once the model is created, all available gRPC API operations can be accessed as model methods, for example:

...
PetService.getPetById({petId: 1}, function (err, res){
  ...
});

The model methods can also be called as promises:

PetService.getPetById({petId: 1}).then(function(res) {
  ...
}, function(err) {
  ...
});
// in async/await flavor
const res = await PetService.getPetById({petId: 1});

Extend a model to wrap/mediate API Operations

Once you define the model, you can wrap or mediate it to define new methods. The following example simplifies the getPetById operation to a method that takes petID and returns a Pet instance.

PetService.searchPet = function(petID, cb){
  PetService.getPetById({petId: petID}, function(err, res){
    if(err) cb(err, null);
    var result = res.data;
    cb(null, result);
  });
};

This custom method on the PetService model can be exposed as REST API end-point. It uses loopback.remoteMethod to define the mappings:

PetService.remoteMethod(
  'searchPet', {
    accepts: [
      { arg: 'petID', type: 'string', required: true,
        http: { source: 'query' }
      }
    ],
    returns: {arg: 'result', type: 'object', root: true },
    http: {verb: 'get', path: '/searchPet'}
  }
);

Example

Coming soon...