Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@twolions/warmer-backend-utilities

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@twolions/warmer-backend-utilities

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

latest
Source
npmnpm
Version
0.0.6
Version published
Maintainers
2
Created
Source

@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 });

FAQs

Package last updated on 14 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts