A REST helper for Next.js APIs

Overview

REST controller for Next.js

How does it work?

Create restful controllers in Next.js

Example:

Inside /pages/api/auth/[...handler]

(filename must be a rest operator if you want customized URLs, but you can also use normal api handlers filenames, though they have to share the same URL in your controller)

// inside /pages/api/auth/[...handler]

import { Controller } from "next-rest-controller"

// The first argument is the 'base' segment that will be used to map the correct url.
const AuthHandler = Controller("/auth", {
    async "GET /auth"(req, res) {
        res.status(401)
        res.send("Forbidden")
    },
    async "GET /[id]/info"(req, res) {
        res.send("Info for " + req.query.id)
    },
    // Like Express
    async "GET /:id/info/:slug"(req, res) {
        res.send("Info for " + req.query.id)
    }
})

Explanation

When adding a handler/method, it should start with an HTTP verb, followed by a space, and a url to handle (with or without query params using square brackets, or like Express, by placing : before).

Sending, for example, a POST request that would be handled by a GET handler, will send a 405 status code

You might also like...

Quickly bootstrap your next TypeScript REST API project. Node 16+, auto OpenAPI, Prettier+ESLint, Jest

REST API template with autogenerated OpenAPI Quickly bootstrap your next TypeScript REST API project with the most up to date template. Included a sam

Oct 1, 2022

The Next.js reference implementation on how to design better APIs

The Next.js reference implementation on how to design better APIs

This is the Next.js reference implementation of my blog bost on How to design better APIs. Getting Started Clone the repository git clone https://gith

Nov 22, 2022

👩‍🎤 Headless, type-safe, UI components for the next generation Web3.Storage APIs.

👩‍🎤 Headless, type-safe, UI components for the next generation Web3.Storage APIs.

Headless, type-safe, UI components for the next generation Web3.Storage APIs. Documentation beta.ui.web3.storage Examples React Sign up / Sign in Sing

Dec 22, 2022

A JavaScript Library for things I use often, as well as some helper functions

Elements A JavaScript Library for things I use often, as well as some helper functions. Full documentation below. Inspired by Habitat, another library

Apr 21, 2022

Framework agnostic CLI tool for routes parsing and generation of a type-safe helper for safe route usage. 🗺️ Remix driver included. 🤟

Framework agnostic CLI tool for routes parsing and generation of a type-safe helper for safe route usage. 🗺️ Remix driver included. 🤟

About routes-gen is a framework agnostic CLI tool for routes parsing and generation of a type-safe helper for safe route usage. Think of it as Prisma,

Jan 2, 2023

Babel plugin and helper functions for interoperation between Node.js native ESM and Babel ESM

babel-plugin-node-cjs-interop and node-cjs-interop: fix the default import interoperability issue in Node.js The problem to solve Consider the followi

Nov 6, 2022

A visual helper in vscode to use github's workflow 'hubflow'

vscode-hubflow README This is the README for your extension "vscode-hubflow". After writing up a brief description, we recommend including the followi

Feb 7, 2022

A helper to use immer as Solid.js Signal to drive state

Solid Immer A helper to use immer as Solid.js Signal to drive state. Installation $ npm install solid-immer Usage Use createImmerSignal to create a im

Nov 22, 2022

A growing three.js helper library.

A growing three.js helper library.

kokomi.js A growing three.js helper library. Install npm i kokomi.js Hello world With just several lines, you can make a most basic 3D scene :d index.

Dec 30, 2022
Releases(v0.0.6)
  • v0.0.6(Jun 15, 2022)

    Add Express-like support for params

    Handlers can have query params defined with [] or :

    Example:

    Controller("/auth", {
      async "GET /[slug]"(req, res) {
         res.send(req.query.slug);
      },
      // Will work the same
      async "GET /:slug"(req, res) {
         res.send(req.query.slug)
      }
    })
    
    Source code(tar.gz)
    Source code(zip)
  • v0.0.5(Jun 15, 2022)

  • v0.0.3(Jun 15, 2022)

Owner
Atomic State
Atomic State
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Um bot de suporte feito usando threads para o Discord, 100% customizável, feito em JavaScript e inspirado no Rio Helper do servidor Elixir Lab e na Loritta Helper do serivdor de suporte da Loritta.

Ticket Bot Um bot de suporte feito usando threads para o Discord, 100% customizável, feito em JavaScript e inspirado no Rio Helper do servidor Elixir

ADG 6 Dec 21, 2022
Elrond blockchain CLI helper tools - interaction with APIs, smart contracts and protocol

Buildo Begins ?? Meet Buildo. He is here to help you start creating in the Elrond blockchain ecosystem. Here is where everything begins. I'm going on

Elrond's Dev Guild 22 Dec 30, 2022
This is a vanilla Node.js rest API created to show that it is possible to create a rest API using only vanilla Node.js

This is a vanilla Node.js rest API created to show that it is possible to create a rest API using only vanilla Node.js. But in most cases, I would recommend you to use something like Express in a production project for productivity purposes.

Eduardo Dantas 7 Jul 19, 2022
Full-Stack CRUD Application With Angular + Firebase Database + Authentication + REST APIs

BookCompany Full-Stack CRUD Application With Angular + Firebase Database + Authentication + REST APIs Technologies & Features Angular front-end framew

Rodrigo Bravo 3 Apr 10, 2022
Run REST APIs in Node.js applications frameworks (Express, Koa, Hapi and Fastify) on top of any Serverless Cloud.

?? Serverless Adapter Install | Usage | Support | Architecture | Credits Run REST APIs and other web applications using your existing Node.js applicat

Vinicius Lourenço 45 Jan 1, 2023
End-to-End type safety for REST APIs written in Fastify. Only problem is you have to explicity export and register route handlers. LOL

Chino intelligence in japaneese End-to-End type safety for REST APIs written in Fastify. Only problem is you have to explicity export and register rou

sambit sahoo 2 Sep 12, 2022
Shizuku Launcher is a simple AWS Virtual Machine helper. Now in Next.js

Shizuku Launcher Shizuku Launcher is a simple AWS Virtual Machine helper. Shizuku Launcher offers multiple solutions to keep your credential security

Seraphim Lou 50 Jan 3, 2023
The easiest way to animate your Next.js project. Scrollreveal.js helper package.

next-reveal The easiest way to animate your Next.js app Demo Introduction next-reveal makes it easy to add awesome scroll animations to your Next.js p

Zoltan Fodor 8 Nov 25, 2022