Skip to content

eldoy/vindu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vindu

Rate limiter for APIs.

Made for the Waveorb Web Application Development Framework.

Install

npm i vindu

Usage

var vindu = require('vindu')

var $ = { req, res, db }

var options = {
  // The key used to identify the API user, default is the request IP
  key: $.req.ip,

  // The name of the database collection to use
  collection: 'request'
}

// Returns the number of requests per minute, month and year
var { minute, month, year } = await vindu($, options)

function rateLimit(res) {
  res.statusCode = 429
  res.end('')
}

var apikey = $.req.params.apikey
var account = await $.db('account').get({ apikey })

if (minute > account.limit) {
  return rateLimit($.res)
}

MIT Licensed. Enjoy!

Created by Eldøy Projects