Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edge Runtime Router #200

Merged
merged 4 commits into from
Jul 6, 2022
Merged

Edge Runtime Router #200

merged 4 commits into from
Jul 6, 2022

Conversation

hoangvvo
Copy link
Owner

@hoangvvo hoangvvo commented Jul 6, 2022

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
Copy link

changeset-bot bot commented Jul 6, 2022

⚠️ No Changeset found

Latest commit: 07a985b

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

@hoangvvo hoangvvo changed the title Edge runtime router Edge Runtime Router Jul 6, 2022
@codecov
Copy link

codecov bot commented Jul 6, 2022

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.

@hoangvvo hoangvvo added the feature Adding or requesting core feature label Jul 6, 2022
@hoangvvo hoangvvo merged commit c5e1814 into main Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Adding or requesting core feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant