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

luno

v1.1.0

Published

Use the Luno API in Node.js

Downloads

11

Readme

luno-node

Use the Luno API in Node.js

This module is a very thin wrapper on the Luno API, handling authentication, request signing and errors.

Install

npm install --save luno

Usage

For documentation on all methods and routes, please see our docs.

var Luno = require('luno');
var luno = new Luno({
  key: 'YOUR-API-KEY', // Your Luno API key
  secret: 'YOUR-SECRET-KEY', // Your Luno secret key
  timeout: 10000, // Maximum request timeout (in milliseconds). Default 10000.
  sandbox: false // Set to true to enable Sandbox Mode for all requests (unless otherwise specified in params).  See https://luno.io/docs#sandbox
});

luno.get('/users/usr_cz2D0VOugcBVWW', {}, function(err, user) {

});

Methods

GET, POST, PUT, PATCH, DELETE and request.

// GET
// luno.get(route, query, callback);
luno.get('/users', {
  limit: 10
}, function(err, resp) { });

// POST
// luno.post(route, query, body, callback);
luno.post('/sessions', {}, {
  user_id: 'usr_cz2D0VOugcBVWW',
  ip: req.headers['cf-connecting-ip'] || req.headers['x-forwarded-for'] || req.connection.remoteAddress,
  user_agent: req.headers['user-agent'],
  details: {
    custom: 'session',
    properties: 'here'
  }
}, function(err, session) { });

// PUT
// luno.put(route, query, body, callback);
luno.put('/users/usr_cz2D0VOugcBVWW', {}, {
  name: 'Simon Tabor',
  profile: {
    properties: 'are',
    overwritten: true
  }
}, function(err, resp) { });

// PATCH
// luno.patch(route, query, body, callback);
luno.patch('/users/usr_cz2D0VOugcBVWW', {}, {
  name: 'Simon Tabor',
  profile: {
    properties: 'are',
    extended: true,
    old: 'properties',
    are: 'kept'
  }
}, function(err, resp) { });

// DELETE
// luno.delete(route, query, callback);
luno.delete('/users/usr_cz2D0VOugcBVWW', {}, function(err, resp) { });


// REQUEST (any method)
// luno.request(METHOD, route, query, body, callback);
luno.request('POST', '/users', {}, {
  email: '[email protected]',
  password: 'my-password'
}, function(err, user) { });

Middleware

Use the session middleware to quickly ensure a session is valid and fetch the details.

It'll set req.session to the session details (if the session is valid) and req.user to the user details (if the session is valid and there is an associated user).

// app is an express server

app.use(luno.session({
  cookieName: 'session', // default config
  cookieConfig: {
    maxAge: 1209600000, // 14 days
    httpOnly: true,
    // secure: true
  }
}));

// admin section of the app requires the session to be valid
// and to have an associated user
app.use('/admin', function(req, res, next) {
  if (!req.user) return res.redirect('/login');
  next();
});

app.get('/', function(req, res, next) {
  // req.session and req.user might be falsy

  if (req.user) {
    res.send('Welcome ' + req.user.first_name);
  } else {
    res.send('Welcome');
  }
});

app.get('/admin', function(req, res, next) {
  // req.session and req.user must be set

  res.send('Hello ' + req.user.first_name);
});

Registration

Let a user sign up and log them in.

Note: you may want to look at Luno CSRF to add CSRF protection using Luno.

app.get('/signup', function(req, res) {
  // Send a barebones signup form
  res.send('<form method="POST" action="/signup"><input type="email" name="email"><input type="password" name="password"><input type="submit"></form>');
});

app.post('/signup', function(req, res, next) {
  luno.post('/users', {}, {
    email: req.body.email,
    password: req.body.password,
    profile: {
      test: true
    }
  }, function(err, user) {
    if (err) return next(err);

    // automatically log this user in
    luno.post('/sessions', {}, {
      user_id: user.id,
      ip: req.connection.remoteAddress,
      user_agent: req.headers['user-agent']
    }, function(err, session) {
      if (err) return next(err);

      res.cookie('session', session.key, {
        maxAge: 1209600000, // 14 days
        httpOnly: true,
        secure: true
      });

      // redirect to the login-protected application
      res.redirect('/dashboard');
    });
  });
});