hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2
https://www.npmjs.com/package/next-connect
MIT License
1.62k stars 65 forks source link

Edge Runtime Router #200

Closed hoangvvo closed 2 years ago

hoangvvo commented 2 years ago

Add a new Edge router that can support Edge API Routes and Middleware. Technically, it would work for other Edge runtime like Deno Deploy (or Deno itself), Cloudflare Workers, Netlify Edge Functions, etc.

import { createEdgeRouter } from "next-connect";

const router = createEdgeRouter();

router
  .use(async (req, evt, next) => {
    const start = Date.now();
    await next(); // call next in chain
    const end = Date.now();
    console.log(`Request took ${end - start}ms`);
  })
  .use(authMiddleware)
  .get((req, res) => {
    return new Response("Hello world");
  })
  .post(async (req, res) => {
    // use async/await
    const user = await insertUser(req.body.user);
    res.json({ user });
    return new Response(JSON.stringify({ user }), {
      status: 200,
      headers: {
        "content-type": "application/json",
      },
    });
  })
  .put(
    async (req, res) => {
      const user = await updateUser(req.body.user);
      return new Response(JSON.stringify({ user }), {
        status: 200,
        headers: {
          "content-type": "application/json",
        },
      });
    }
  );

// create a handler from router with custom
// onError and onNoMatch
export default router.handler({
  onError: (err, req, evt) => {
    console.error(err.stack);
    return new Response("Something broke!", {
      status: 500,
    });
  },
  onNoMatch: (req, res) => {
    return new Response("Page is not found", {
      status: 404,
    });
  },
});

Example: https://github.com/hoangvvo/next-connect/tree/edge-middleware/examples/nextjs/pages/api/edge-users

changeset-bot[bot] commented 2 years ago

⚠️ No Changeset found

Latest commit: 07a985bec5bd0652250f4d0fd9086f80c4fb02ee

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

codecov[bot] commented 2 years ago

Codecov Report

Merging #200 (07a985b) into main (77ad0e1) will not change coverage. The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main      #200   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         4    +1     
  Lines          238       321   +83     
  Branches        41        53   +12     
=========================================
+ Hits           238       321   +83     
Impacted Files Coverage Δ
src/edge.ts 100.00% <100.00%> (ø)
src/index.ts 100.00% <100.00%> (ø)
src/node.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 77ad0e1...07a985b. Read the comment docs.