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

express-gems

v0.1.1

Published

Encapsulated goodies for express-centered backend sources

Downloads

17

Readme

💎 express-gems

NPM

Encapsulated express related libraries for external utilities.

It's named "gems", but it has nothing to do with Ruby programming language, believe me. It is just a programmer trying to name things 🤷

Utility "gems" will keep expanding as I discover new needings with the libraries I am using.

⚙️ Setup

Via npm

You can install package by executing following command:

$ npm i express-gems -s

📦 Provided gems/goodies

✉️ MySQL Gem

.createPool(config, callback)

Creates traditional connection pool with given configs. However, callback is called with an err and pool object

Example:

const gemMySQL = require('express-gems').gemMySQL

let options = { /* ... */ }

gemMySQL.createPool(options, (err, pool) => {
    if (err) console.log('Error connecting to the DB.')
    // Tadaa! Connection established and pool is created successfully
})

.buildWhereTemplate(input, rules)

Helps you to build where templates.

const gemMySQL = require('express-gems').gemMySQL

let _pseudoReqBody = { foo:'Lorem ips', bar:'123.45', baz:'somevariable', unwanted:'malicious' }

let { where, params } = gemMySQL.buildWhereTemplate(_pseudoReqBody, {
    foo: { $clause:'=?', $prefix:'%', $postfix:'%' },
    bar: '=CAST(? AS UNSIGNED)',
    baz: '=?',
    locale: ['tr_TR', 'en_GB']
})

// "where" will be
///     "WHERE foo=? AND bar=CAST(? AS UNSIGNED) AND baz=? AND locale IN ('tr_TR', 'en_GB')"
// "params" will be
//     ['%Lorem ips%', '123.45', 'somevariable']

✉️ Express-validator Gem

.errors(req)

Returns a gathered up, response-ready format of errors within req. MUST BE used after express-validator middlewares are executed.

const express = require('express')
const gemValidator = require('express-gems').gemValidator

let app = express()

app.all('/some-endpoint', [ ... ], (req, res, next) => {
    const errors = gemValidator.errors(req)
    if (errors) return res.status(400).json({ errors })
})

errors is an object with checked fields in it. Error objects within the arrays are determined by the .withMessage(MSG) chain within the check rules.

{
    "checkedField1": [
        { "code":"SOME_ERROR_CODE", "arg1":"SOME_ARG" },
        { "code":"SOME_ERROR_CODE", "arg1":"SOME_ARG" },
    ],
    "checkedField2": [
        { "code":"SOME_ERROR_CODE", "arg1":"SOME_ARG" },
        { "code":"SOME_ERROR_CODE", "arg1":"SOME_ARG" },
    ],
    ...
}

.fillWithMessages(chains, errorTable?)

Traverses given chains array and fills them with appropriate error messages. (Helps you in cases you do not want to repeat same withMessage(MSG) statements)

const gemValidator = require('express-gems').gemValidator
const { check, body } = require('express-validator/check')

// With built-in standard messages
const validationRules = gemValidator.fillWithMessages([
    check('foo').exists().isInt({ min: 5 }),
    body('bar').optional().isLength({ max: 20 })
])

// With custom message builder table
const validationRules = gemValidator.fillWithMessages([
    check('foo').exists().isInt({ min: 5 }),
    body('bar').optional().isLength({ max: 20 })
], {
    isInt: (options) => ({ error: 'Should be an integer', someCustomField: 'Yea custom field' }),
    isLength: (options) => ({ error: 'Should be between', min: options[0].min, max: options[0].max })
})

// CAUTION: Please note that while using builder table
const validationRules = gemValidator.fillWithMessages([
    check('latlongfield').exists().isLatLong()
], {
    // isLatLong check is mapped into "_default"
    _default: (options) => ({ error: 'Should be a latlong value' })
})

‼️ Found a bug or have a suggestion?

Feel free to open up an issue to let me know if you;

  • Found a bug
  • Have a suggestion
  • Have a question

At: https://github.com/iGoodie/express-gems/issues