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

parse-route-data

v3.5.11

Published

Parse the HTTP data and check for validating

Downloads

53

Readme

Node.js Package Build Status NPM version Package size code style: prettier

Why 🤔


This package is an express middleware who perform some actions on req.query or req.body object and allow you to define what you really want in a specific route by creating a config files filled with your API routes.

Concretely using a config file with route schemas we retrieve the data sent from the client either from req.query or req.body then we compare this data with your configuration file and if they do not match the program returns an error or he keeps running.

Install 🐙

$ npm i parse-route-data

Usage 💡

First you need to create a config object, create a file and export an object from this file, there are a couple of rules in order to correctly create the config object.

First level key

The first level of your config object should only include HTTP methods see the list below and their values should be object

Second level key

Inside your http method object you can write your API routes, the value keys must be string and their values object, let's say your API has three routes (two statics, one with dynamic params) one with the GET method, one with POST and finally one with DELETE, you can write those routes like this:

  • GET, /api/user,
  • POST /api/session/user,
  • DELETE /api/:user

Note - Do not forget to start all your routes with /, you may notice that to use dynamic data in your route you can use : there is nothing fancy here this is Express logic.

Third level key

At this level you can define which data your route should received,

inside the route object start creating your schema a route schema is simply an object with some keys which are object and contains some schemaType

| SchemaType | Constructor | Values | Description | | ------ | ----------- | ------- | --- | | type* | All | Mixed | Type define which data your field must be | | required | All | Boolean | Is this field must be include in your data| | default | All | Mixed | Define which value your field should be if not created| | maxLength | Array, String| Number | Which length your field must have| | itemsType | Array | Mixed | Which data your inner items must be| | itemsSchema| Array, Object| Object | Object with SchemaType| | match | String | RegExp | Should match the regExp expression| | canBe | String | Array | Array of strings your field should match, at least one item| | min | Number | Number | Value is less or equal| | max | Number | Number | Value is greater or equal| | notBefore | Date | Date | Must be before date specified| | strict | Object | Boolean | Must have the exact same length|

Real case exemple

// ./dev/foo/config.js
module.exports = {
  get: {
    '/api/foo': null,
    "/api/:foo/route/:foofoo": {
      filter: {
          type: String,
          required: true,
      },
      test: {
          type: Array,
          itemsType: [String, Boolean],
          required: true,
      },
    }
  },
  post: {
    '/api/user': {
      name: {
        type: String,
        required: true,
        maxLength: 10,
      }
    }
  },
  delete: {
    '/api/user/:id': null,
  }
}
// ./dev/foo/routes/routeApi.js
const configRoutes = require('../config.js');
const parseRouteData = require('parse-route-data');

function greatSuccess (res, res) {
  return res.status(200).json({ success: true });
}

// 200
app.get('/api/:foo', parseRouteData(configRoutes), greatSuccess);

// req.query = {},
// Error, require req.query = { filter: 'good', test: ['good', false]};
app.get('/api/:foo/route/:foofoo', parseRouteData(configRoutes), greateSuccess);

// 200
app.get('/api/:foo/route/:foofoo?filter=good&test=['good', false]', parseRouteData(configRoutes), greateSuccess);

// req.body = {};
// Error, require req.body = { name: 'no name'}
app.post('/api/user', parseRouteData(configRoutes), greateSuccess);

// req.body = { name: 'this is a very long name'};
//Error, require name less than 10 characters.
app.post('/api/user', parseRouteData(configRoutes), greateSuccess);

// req.body = { name: 'Pennywise'};
// 200
app.post('/api/user', parseRouteData(configRoutes), greateSuccess);

// other routes..

API

parseRouteData(config, [responseFn, options])

config

Type: object - required

The config object with routes schema types.


responseFn

Type: function - optional

Function who takes req, res, next arguments and must return response to the client.


options

Type: object - optional

errorServerCode

Type: number

Default: 500

The error code to return when server error.

errorRouteDataCode

Type: number

Default: 400

The error code to return when error with route.

envIsDev

Type: string

Default: production

Either the package is runnning in dev or production mode.


Contributing 🍰

Please make sure to read the Contributing Guide before making a pull request.

Thank you to all the people who already contributed to this project!

Maintainers 👷

List of maintainers, replace all href, src attributes by your maintainers datas.

License ⚖️

@MIT