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

serverless-request-validator

v1.3.1

Published

Validates requests made to a Vercel serverless application in an Express-like way

Downloads

14

Readme

Serverless request validator

This is made to be used in Vercel serverless applications, it can be used with Javascript and Typescript.

To install it:

npm install serverless-request-validator

or

yarn add serverless-request-validator

Using the library

If you are using Javascript:

const Validate = require("serverless-request-validator");

With Typescript:

import Validate from "serverless-request-validator";

Handling one-method-only requests

If an endpoint in your app should only accept one HTTP method, use the property named as the only method allowed. For example:

Javascript

// /api/index.js
const Validate = require("serverless-request-validator")

// Only HTTP `GET` is allowed
module.exports = Validator.get((req, res)=>{
    res.send("get request")
})

Typescript

// /api/index.ts
import Validate from "serverless-request-validator";

// Only HTTP `GET` is allowed
export default Validate.get((req, res)=>{
    res.send("get request")
})

The default export means that the application endpoint will only allow a request using the GET method. If a request is made using a different method, it will respond with a 405 (method not allowed) status code, and saying the method can't be used in that url (like in Express)

Handling multi-method requests

This makes it possible for an endpoint to handle requests of different methods. Very similar to the previous example:

Javascript

// /api/index.js
const Validate = require("serverless-request-validator")

// GET, POST and PUT are allowed.
module.exports = Validate({
  get(req, res) {
    res.send("a get request");
  },
  post(req, res) {
    res.send(`A ${req.method} request`);
  },
  put(req, res) {
    res.send("a put request");
  },
})

Typescript

// /api/index.ts
import Validate from "serverless-request-validator";

// GET, POST and PUT are allowed
export default Validate({
  get(req, res) {
    res.send("Hello");
  },
  post(req, res) {
    res.send(`A ${req.method} request`);
  },
  put(req, res) {
    res.send("a PUT request");
  },
})

In this case, this function will handle GET,POST and PUT methods sent to it. As in the previous example, other methods different that the allowed ones will send a 405 status code and the message saying the method can't be used in that url.

Sending files

It is possible to use the sendFile() method available

// /api/index.ts
import Validate from "serverless-request-validator";
import path from "path"

export default Validate.get((req,res)=>{
  res.sendFile(path.resolve(__dirname,"../src/cat.png"))
})

If a file doesn't exist, a 404 status code will be sent to the user

That's basically it, hopefuly it makes it easier to have different reponses for different methods=)