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

guardflux

v2.2.1

Published

A light callable lib to keep your API alive

Downloads

641

Readme

Guardflux

License Version

A light callable lib to keep your Node.Js API alive

Installation

npm i guardflux

Functions and usage

schema

This is expansion of Joi lib that we use it to verify API input. It can be query or body of a request. Here is the example of usage:

const ipSchema = schema.object({
    api_key: schema.string().min(10).max(10).required(),
})

checkObject()

Then you can verify the input with checkObject() function and it returns result with two elements isValid: boolean and log: any. isValid returns true if given object and schema match and log returns Joi log if an error happen. Also this function has an element called devMode that prints all logs in console to get what is happening in function and defult setted to true. Here is the full example:

import { checkObject, schema } from 'guardflux'
import { CheckResult } from 'guardflux/dist/lib/types'

...
// This is the apikey or user ip or id that you can pass to function
const user_given_api_key: string = "x2a45B78C0"

const ipSchema = schema.object({
    api_key: schema.string().min(10).max(10).required(),
})

const check: CheckResult = await checkObject({ api_key: user_given_api_key }, ipSchema)

You can use check.isValid to response the user that given data in request is valid or not.

rateLimit()

This is a very useful fucntion that make rate limit for every route based on user api-key/id/ip that you pass. We use MikroOrm because it is light and can handle several DBs. Because of needing to save data in DB we have to config the DB options first:

const dbConfig: DbConfig = {
    dbName: "guardflux", // Default name id "guardflux" but you can pass any name you want
    dbType: "mongodb", // Choose which DB you want to work with it. Supported DBs are 1-MySQL 2-MongoDB 3-PostgreSQL
    dbURI: MONGO_URI, // Pass URI of your DB
    dbDebug: true // Make MikroOrm debug mode on
}

If your DB has username and password, you can add it to you URI string. These are default DBs URI string that don't have username and password.

| Type | default connection url | | :--------- | :----------------------------------: | | mongo | mongodb://127.0.0.1:27017 | | mysql | mysql://[email protected]:3306 | | postgresql | postgresql://[email protected]:5432 |

After we config the DB, we have to add options for rateLimit function:

const rlOptions: RateLimitOptions = {
    route: "/api/test_route", // API route to specify ratelimit based on route
    cycleTime: 60, // Rate limit cycle based on secounds
    maxRequests: 5 // Max requests that a user can make in cycleTime
}

Now we pass all consts to function. This function works based on two keys: 1- User API-key/ip/id 2- Route path. At least you can turn devMode to see in console what is happen. Like checkObject function, this method has devMode too that is enabled by default.This option will prints all data of MikroOrm and what is happen in function. This function returns like checkObject and you can use it to response the user if you want. All example is here:

import { checkObject, schema } from 'guardflux'
import { CheckResult } from 'guardflux/dist/lib/types'

...
// This is the apikey or user ip or id that you can pass to function
const user_given_api_key: string = "x2a45B78C0"

const rlOptions: RateLimitOptions = {
    route: "/api/test_route", // API route to specify ratelimit based on route
    cycleTime: 60, // Rate limit cycle based on secounds
    maxRequests: 5 // Max requests that a user can make in cycleTime
}

const dbConfig: DbConfig = {
    dbName: "guardflux", // Default name id "guardflux" but you can pass any name you want
    dbType: "mongodb", // Choose which DB you want to work with it. Supported DBs are 1-MySQL 2-MongoDB 3-PostgreSQL
    dbURI: MONGO_URI, // Pass URI of your DB
    dbDebug: true // Make MikroOrm debug mode on
}

const rl: CheckResult = await rateLimit(user_given_api_key, rlOptions, dbConfig)

License

This project is licensed under the MIT License - see the LICENSE file for details.