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

@twolions/warmer-backend-utilities

v0.0.6

Published

Collection of common NodeJS utilities used in Warmer's backend services

Downloads

4

Readme

@twolions/warmer-backend-utilities

Collection of common NodeJS utilities used in Warmer's backend services

Installation

npm i -S @twolions/warmer-backend-utilities

API

AMQPClient

Used to send and receive messages from a RabbitMQ broker.

AMQPClient expects RABBITMQ_URL and RABBITMQ_DLX to be defined in process.env.

Arguments

queue: string

The name of the queue to connect to.

Usage

const { AMQPClient } = require('@twolions/warmer-backend-utililies');

const client = new AMQPClient('<queue>');

// send a message to the queue
await client.send({ type: 'email:send', payload: {...} });

// receive messages from queue
//
// this is a process that last for the duration of the process itself; if you want to use it along with other processes (http server, etc...) do not `await client.receive`.
await client.receive((msg) => { ... });

// acknowledge a message
client.ack(msg);

// reject a message
// if requeue is false, messages will end up in DLQ, otherwise they will be sent back to the queue where they were consumed from
client.reject(msg, opts = { requeue = false });

// close connection
await client.close();

validateRequest

A Koa middleware to validate ctx.query, ctx.params, ctx.body.

validateRequest uses ajv under the hood to perform validations (defined with JSON Schema).

Arguments

schema: object

A Simplified representation of a JSON Schema:

  • Keys can be suffixed with a '?' to denote them as optional
  • By default, objects will have additionalProperties: false set; you can override this by having '*': true
  • All objects will have a required array, with the keys of non-optional fields
  • To create an array type, have the value of a key be an array with a single object, which is the description of the array items
  • Has support for nested objects

Return value

void

Usage

const { validateRequest } = require('@twolions/warmer-backend-utilities')

router.post(
  '/',
  validateRequest({
    params: {
      id: { type: 'string', minLength: 1 }
    },
    body: {
      'optional?': { type: 'string' }
      nested: {
        type: { type: 'string', minLength: 1 },
        'count?': { type: 'number', minLength: 1 },
      },
    },
  }),
  async (ctx) => {
    const { id } = ctx.request.params;
    const { optional, nested } = ctx.request.body;

    // optional and nested.count can be `null`
  },
)

validate

A function that uses the same simplified representation of a JSON Schema as validateRequest, but to validate any value you might require.

Arguments

schema: object

A Simplified representation of a JSON Schema:

value: any

The value to validate against schema

Return value

{
  valid: Boolean,
  errors: Array,
}

Usage

const { validate } = require('@twolions/warmer-backend-utilities')

sleep

async function

Returns a promise that resolves after ms miliseconds.

Arguments

ms: int

Number of miliseconds to wait before resolving promise

Return value

void

Usage

await sleep(1000); // will wait one second before resolving

retry

async function

Arguments

fn: function

Function to retry.

opts: object

Configuration options. Defaults to 3 attempts with no delay.

{
  attempts: 3,
  delay: 0,
}

Return value

Whatever the return value of fn argument is.

Throws if attempts are reached without fn resolving.

Usage

await retry(possiblyFailingFunction, { attempts: 10, delay: 100 });