GithubHelp home page GithubHelp logo

danybeltran / next-api-validation Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 14 KB

Request validator implemented in Next.js:)

Home Page: https://www.npmjs.com/package/next-api-validation

License: MIT License

JavaScript 45.43% TypeScript 54.57%
nextjs next api-validator api-wrapper nextapirequest nextapiresponse

next-api-validation's Introduction

Next API Validation

This is basically the same as serverless-request-validator ready be used in Next.js

How to use it:

It is nice to be able to programmatically define what you want your API endpoint(s) to do deppending on the request method used.

In express it's something like:

app.get((req,res)=>{
    res.send("Something");
})

That will work only when using a GET request

In Next.js (and Vercel) apps, your API are files in a specific order in the project directory, each file with a default export being the actual handler that will handle that request.

First, install the module:

npm install next-api-validation

Or

yarn add next-api-validation

Using it in any of your API routes in Next.js:

import validation from "next-api-validation";

export default validation.get((req,res)=>{
    res.send("This only accepts GET request")
})

As you can see, using the get method in the validation object prevents the handler from being executed if a different request method is used.

And so with other methods:

// api/index.js or api/index.ts
import validation from "next-api-validation";

export default validation.post((req,res)=>{
    res.send("You just sent a POST request")
})

This handler a POST request

What if an endpoint should handle requests using more than one or two methods?

Creating a default export of the function should solve that:

// api/index.js or api/index.ts
import validate from "next-api-validation"

export default validate({
    get(req,res){
        res.send("A get request")
    },
    post(req,res){
        res.send("I only handle post requests"))
    },
    put(req,res){
        res.send("Did you put something?")
    }
})

The previous code handles requests that use three different methods, and calls only the necessary handler. An example of how it can be used:

// CRUD of a MongoDB Document model

import { Post } from "src/Models";
import { connectToDatabase } from "src/utils";
import validate from "next-api-validation";

connectToDatabase();

export default validate({
  get: async (req, res) => {
    const posts = await Post.find();
    res.send(posts);
  },
  post: async (req, res) => {
    const newPost = new Post(req.body);
    const saved = await newPost.save();
    res.send(saved);
  },
  put: async (req, res) => {
    const editedPost = await Post.findByIdAndUpdate(req.body._id, req.body);
    res.send(editedPost);
  },
  delete: async (req, res) => {
    const deletedPost = await Post.findByIdAndDelete(req.body._id);
    res.send(deletedPost);
  },
});

next-api-validation's People

Contributors

danybeltran avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.