Giter VIP home page Giter VIP logo

request-expect's Introduction

request-expect

build status NPM version node version

Easily sanitize Koa and Express request bodies, throwing errors when required parameters are missing or invalid. Helps make your APIs self documenting.

Installation

Install package with NPM and add it to your development dependencies:

npm install --save-dev request-expect

Usage

Example for Koa

const
	koa = require('koa'),
	bodyParser = require('koa-bodyparser'),
	requestExpect = require('request-expect'),

	app = koa();

app.use(bodyParser());
app.use(requestExpect.koa);
app.use(async function YourApiEndpoint(ctx) {
	// request-expect attaches itself to your request object
	ctx.request.expect((types) => {
		// return an object definition of what you expect to be sent in the request
		return {
			params: {
				id: types.integer
			},
			body: {
				email: types.string, // parameters are required by default.
				password: types.string,
				username: types.string.isOptional // optional parameter
			}
		};
	});

	// request-expect mutates your request object so everything
	// in ctx.request should now be safe.

	// do something...
});

app.listen(3000);

Example for Express

const
	express = require('express'),
	requestExpect = require('request-expect'),

	app = express();

app.use(requestExpect.express);
app.get('/', async function YourApiEndpoint(req, res) {
	// request-expect attaches itself to your request object
	req.expect((types) => {
		// return an object definition of what you expect to be sent in the request
		return {
			params: {
				id: types.integer
			},
			body: {
				email: types.string, // parameters are required by default.
				password: types.string,
				username: types.string.isOptional // optional parameter
			}
		};
	});

	// request-expect mutates your request object so everything
	// in req should now be safe.

	// do something...
});

app.listen(3000);

Types

  • any any type
  • string same as any but casts to a string
  • integer trims and runs parseInt
  • number same as integer, but runs parseFloat instead of parseInt if a decimal is present

All types are required by default. To make it optional, chain isOptional. See the above examples.

request-expect's People

Watchers

James Cloos avatar Arlen Anderson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.