LayerVault API client
NodeJS client for accessing the LayerVault API.
The library is available on npm:
npm install layervault
var LayerVault = require'layervault';// Configurevar config =thisoauthKey = 'abc123'thisoauthSecret = 'foobar'// optional, if you have itthisaccessToken = 'sloths567'thisrefreshToken = 'blahblah';// Create the clientvar client = config;
// If configured without an access token, you will need// to authenticate and retrieve one. Here's the password flow:clientauthwithPassword'username' 'password'// 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.
clientauthon'authorized'// Store our tokens somewhere persistent.console.logtokensaccessToken tokensrefreshToken;;
This API client uses a lightweight resource model to make getting associations very easy.
// We can retrieve information about the logged in user.clientmeconsole.loguser;// Get the user's primary organizationvar org = userorganizations0;// Get the first project in the organizationclientorganizationorgprojectorgprojects0getconsole.logproject;;;
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.
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.
clientorganization'layervault'file'Test/Stuff' 'NewFile.psd'createlocalPath: "/path/to/file/on/disk/NewFile.psd"contentType: "image/vnd.adobe.photoshop"console.logresp;;
All asynchronous calls return a promise, which can be used as an alternative to providing a callback.
clientauthwithPassword'username' 'password'thenconsole.logrespaccessToken;return clientorganization'layervault'get;thenreturn orgprojects0files0previeww: 200;thenconsole.logpreview;;
- Allow file uploads to accept a file buffer
- Implement authorization code OAuth flow
Available under the MIT license. See the LICENSE file for more info.