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

layervault

v0.1.5

Published

LayerVault API client

Downloads

26

Readme

LayerVault JS Client

Build Status

NodeJS client for accessing the LayerVault API.

Installation

The library is available on npm:

npm install layervault

Configuration

var LayerVault = require('layervault');

// Configure
var config = new LayerVault.Configuration(function () {
  this.oauthKey = 'abc123'
  this.oauthSecret = 'foobar'

  // optional, if you have it
  this.accessToken = 'sloths567'
  this.refreshToken = 'blahblah'
});

// Create the client
var client = new LayerVault.Client(config);

Usage

Authentication

// If configured without an access token, you will need
// to authenticate and retrieve one. Here's the password flow:
client.auth.withPassword('username', 'password', function (err, tokens) {
  // We are now logged in. The configuration is automatically updated
  // with the access token and refresh token. We can store them somewhere
  // persistent if needed.
});

Because LayerVault uses OAuth refresh tokens, we need to watch out for their expiration and retrieve new tokens when they do. This is automatically handled internally in the library, and the new tokens are exposed via an event. This event is fired whenever authorization completes, so you can use it instead of providing a callback to withPassword like above.

client.auth.on('authorized', function (tokens) {
  // Store our tokens somewhere persistent.
  console.log(tokens.accessToken, tokens.refreshToken);
});

Retrieving Data

This API client uses a lightweight resource model to make getting associations very easy.

// We can retrieve information about the logged in user.
client.me(function (err, user) {
  console.log(user);

  // Get the user's primary organization
  var org = user.organizations[0];

  // Get the first project in the organization
  client.organization(org).project(org.projects[0]).get(function (err, project) {
    console.log(project);
  });
});

You can chain together objects to build a query. Responses are parsed to include proper object associations so you can continue to drill down data.

Arguments to the data types can be given as separate arguments (shown below), as path style ("Test/Stuff"), or a combination of both.

client.organization('layervault').folder('Test', 'Stuff').get(function (err, folder) {
  folder.files[0].revisions(function (err, revisions) {
    revisions[0].previews(function (err, urls) {
      console.log(urls);
    });
  });
});

Uploading a File/Revision

Creating a new file and uploading a new revision are the same process. While it is a multi-step process, the API client takes care of most of the steps for you.

To upload a file, all you need to provide is the path to the file on disk and the mime-type of the file.

client.organization('layervault').file('Test/Stuff', 'NewFile.psd').create({
  localPath: "/path/to/file/on/disk/NewFile.psd",
  contentType: "image/vnd.adobe.photoshop"
}, function (err, resp) {
  console.log(resp);
});

Promises

All asynchronous calls return a promise, which can be used as an alternative to providing a callback.

client.auth.withPassword('username', 'password').then(function (resp) {
  console.log(resp.accessToken);
  return client.organization('layervault').get();
}).then(function (org) {
  return org.projects[0].files[0].preview({w: 200});
}).then(function (preview) {
  console.log(preview);
});

TODO

  • Allow file uploads to accept a file buffer
  • Implement authorization code OAuth flow

Links

Author

Ryan LeFevre - GitHub, Twitter, Email

License

Available under the MIT license. See the LICENSE file for more info.