Coder Social home page Coder Social logo

next-api-wrapper's Introduction

API Wrapper & Error Boundary for Next.js

This API Wrapper serves two main purposes:

  • HTTP Method Resolution (GET, POST, PUT, DELETE)
  • Error Boundary for Errors & Exceptions
    • Custom Exceptions return a 400..499 status code, along with Exception's message and optional data object in JSON
    • Errors return a 500 status code with "Internal Server Error" message, logging to server's console or logger function

Install

npm i next-api-wrapper

Example usage

// pages/api/hello.js
import Wrapper, { Exception } from 'next-api-wrapper'

export default Wrapper({
  // pass a direct result
  GET: 'Hello World',
  
  // pass a function
  POST: async req => {
    await new Promise(resolve =>
        setTimeout(resolve, 1000)
    )
    
    return {
      time: 1000,
      message: 'Your Post Was Lost'
    }
  },
  
  // throw an exception
  PUT: async req => {
    throw new Exception('Access Forbidden', 403, { minrole: ['editor'] })
  },
  
  // throw an error
  DELETE: async req => {
    throw new Error("You Can't Do That")
  }
})

Example responses

curl http://localhost:300/api/hello
## Status: 200
## "Hello World"

curl -X POST http://localhost:300/api/hello
## Status: 200
## { "time": 1000, "message": "Your Post Was Lost" }

curl -X PUT http://localhost:300/api/hello
## Status: 403
## { "error": 403, "message": "Access Forbidden", "data": { "minrole": [ "editor" ] } }

curl -X PATCH http://localhost:300/api/hello
## Status: 405
## { "error": 405, "message": "Method PATCH Not Supported" }

curl -X DELETE http://localhost:300/api/hello
## Error w/ stack printed to server's console or logger function
## Status: 500
## { "error": 500, "message": "Internal Server Error" }

Read more at Medium.com

next-api-wrapper's People

Contributors

deltazero-cz avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

kenmlee

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.