0.0.1 • Published 2 years ago

@opengram/command-args v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

The arguments plugin lets you parse & validate arguments of commands with ease.

CI codecov npm downloads javascript style guide Codacy Badge License: MIT

Features

  • Joi validation support
  • Arguments remapping

Docs

You can find documentation here

Installation

NPM

npm i @opengram/command-args

Yarn

yarn add @opengram/command-args

PNPM

pnpm add @opengram/command-args

Quick start

const { Opengram } = require('opengram')
const bot = new Opengram(process.env.BOT_TOKEN)

const Joi = require('joi')
const arguments = require('@opengram/command-args')
  
// Create middleware instance
const sumArgs = arguments({
  mapping: ['first', 'second'], // First argument to "first" property, second to "second" property
  errorHandler: (err, ctx) => ctx.reply(`Invalid arguments: ${err.message}`), // Error handler for validation errors
  // Validation schema
  schema: Joi.object({
    first: Joi
      .number()
      .integer()
      .required(),
    second: Joi
      .number()
      .integer()
      .required()
  })
})

bot.command('sum', sumArgs, ctx => {
  // Destructuring assignment from safe, validated object, with converted to number args
  const { first, second } = ctx.state.command.args
  // Send sum result
  return ctx.replyWithHTML(`<b>Result:</b> ${first + second}`)
})

bot.launch()