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

lightblue.js

v0.7.0

Published

A lightblue client for JavaScript.

Downloads

18

Readme

lightblue.js

Build Status Coverage Status

A lightblue client written in Javascript for frontend or Node.JS applications.

The same library may be used for servers or clients, with special support for AngularJS applications.

Install

bower install lightblue.js --save

npm install lightblue.js --save

git clone https://github.com/alechenninger/lightblue.js.git

Import

Browserify/NodeJS (CommonJS) or RequireJS (AMD)

// commonjs or NodeJS
var lightblue = require("lightblue");

// asynchronous module definition (amd)
require(["lightblue"], function(lightblue) {
  ...
});

Vanilla.js

// No module framework (use window.lightblue)
<script src="lightblue.min.js" type="text/javascript"></script>

Usage

Once you have a lightblue object, you can get a client:

// Assumes /data and /metadata for data and metadata services respectively,
// but you can override.
var client = lightblue.getClient("http://my.lightblue.host.com/rest");

And you can use builder API's for more readable queries and autocomplete if your IDE supports it:

var field = lightblue.query.field;

// Use in queries...
field("firstName").equalTo("Bob").and(field("age").greaterThan(21));
field("lastName").equalTo(field("firstName"));

The query builder API is not yet fully flushed out, but adding functionality is trivial. See issue #11.

Example find request

// Query
var bobsOlderThan20 = field("firstName").equalTo("bob")
    .and(field("age").greaterThan(20));

// Projection
// No projection builder yet but it would be something like this:
var everything = include("*").recursively();

var find = client.data.find({
  entity: "User",
  version: "1.0.0",
  query: bobsOlderThan20,
  projection: everything
})
.then(console.log.bind(console));

AngularJS

If angular is detected, a "lightblue" module will be registered with a "lightblue" service as a namespace for lightblue facilities. In this environment, Angular's $http service will be used instead of making XHR requests directly. You can configure the host(s) to use using providers.

var app = angular.module("app", ["lightblue"]);

app.config(["lightblueProvider", function(lightblueProvider) {
  lightblueProvider.setHost("http://my.lightblue.com");
}]);

app.controller("ctrl", ["lightblue", function(lightblue) {
  var field = lightblue.query.field;

  lightblue.data.find(field("foo").equalTo("bar"))
      .then(function(response) {
        var entity = response.processed[0];
      });
}]);

Multiple lightblue service instances with Angular

Angular services are singletons, and therefore you only get to configure one lightblue backend to talk to. If for some reason your application needs to talk to more than one lightblue host, you can create separate lightblue services using the global lightblue namespace, and wrap them in specific Angular services for your needs. Don't use the global namespace directly: wrap it in a service.

myModule.factory("otherLightblueInstance", ["$http", function() {
  // `lightblue` is globally defined on `window` if needed.
  // Don't use it directly: wrap it in a service.
  // Pass $http in via config object unless you want the client to use XHR
  // directly.
  var client = lightblue.getClient("my.other.lightblue.com" {$http: $http});
  return {
    data: client.data,
    metadata: client.metadata
  };
});

Auth

Basic using lightblue object (client or server)

lightblue.getClient("foo.com", {auth: {username: "foo", password: "bar"}});

Basic w/ Angular service (client)

Since lightblue.js uses $http, you can add interceptors / common headers to all $http requests if that works for you. You can also configure the lightblue clients directly:

// Inject lightblue.http...
module.controller("login", ["lightblue.http", function(lightblueHttp) {
  $scope.login = function() {
    lightblueHttp.setAuth({
      username: $scope.username,
      password: $scope.password
    });
  };
}]);

And of course if needed you can still use the global lightblue object as above to get a new client with basic auth credentials, just remember to pass $http as per above example.

SSL certs (client)

This is handled by the user's browser, each in their own way. You will generally have to import your cert into the browser and select it once you visit the web application. The Javascript has no idea certs are involved. See each browser's documentation for more details.

SSL certs (server)

In the options object for the clients, you may define an "httpsAgent" key which has the same semantics as making an https request with node and defining an agent to use. If you pass undefined, it uses the global agent. Or, you may pass your own (via new https.Agent(options)). In either case, you will need to configure your certificates on the agent. See nodejs's https documentation for more information.

// Configure the global agent
var fs = require("fs");
var https = require("https");

https.globalAgent.key = fs.readFileSync('my-key.pem');
https.globalAgent.cert = fs.readFileSync('my-cert.pem');

// Defaults to global agent
var clientUsingGlobalAgent = lightblue.getClient("https://my.lightblue.com");

// Use a lightblue-specific agent
var clientUsingOwnAgent = lightblue.getClient("https://my.lightblue.com", {
  httpsAgent: new https.Agent({
    key: fs.readFileSync('my-key.pem'),
    cert: fs.readFileSync('my-cert.pem')
  })
});