-
Notifications
You must be signed in to change notification settings - Fork 2
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
add support for edge #82
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
/// <reference types="next/navigation-types/compat/navigation" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
// This file changes the routing to allow top-level prefixes | ||
|
||
module.exports = { | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
reactStrictMode: true, | ||
async rewrites() { | ||
return { | ||
beforeFiles: [ | ||
// Nextjs by default requires a /api prefix, let's remove that | ||
{ | ||
source: "/:path*", | ||
destination: "/api/:path*", | ||
}, | ||
// REVIEW: I was not able to make the redirect work | ||
// { | ||
// source: "/:path*", | ||
// destination: "/api/:path*", | ||
// }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @itelo I think it would be a lot better if we created an |
||
], | ||
} | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,15 @@ | ||||||||||
import { withRouteSpecEdge } from "@/lib/middlewares" | ||||||||||
import { z } from "zod" | ||||||||||
|
||||||||||
export const runtime = "edge" | ||||||||||
|
||||||||||
const route_spec = { | ||||||||||
jsonResponse: z.object({ | ||||||||||
return: z.boolean(), | ||||||||||
}), | ||||||||||
auth: "none", | ||||||||||
} as const | ||||||||||
|
||||||||||
export const GET = withRouteSpecEdge(route_spec)((req) => { | ||||||||||
return req.responseEdge.status(200).json({ return: true }) | ||||||||||
Comment on lines
+13
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe mirroring the interface of non-edge with
Suggested change
would be a bit easier to use (since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { UnauthorizedException, MiddlewareEdge } from "nextlove" | ||
|
||
export const withAuthTokenEdge: MiddlewareEdge<{ | ||
auth: { | ||
authorized_by: "auth_token" | ||
} | ||
}> = (next) => async (req) => { | ||
const authorization = req.headers.get("authorization") | ||
|
||
if (authorization?.split("Bearer ")?.[1] !== "auth_token") { | ||
throw new UnauthorizedException({ | ||
type: "unauthorized", | ||
message: "Unauthorized", | ||
}) | ||
} | ||
|
||
req.auth = { | ||
authorized_by: "auth_token", | ||
} | ||
|
||
return next(req) | ||
} | ||
|
||
export default withAuthTokenEdge |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import test from "ava" | ||
import getTestServer from "tests/fixtures/get-test-server" | ||
import getTestServer from "../fixtures/get-test-server" | ||
|
||
test("GET /health", async (t) => { | ||
test("GET /api/health", async (t) => { | ||
const { axios } = await getTestServer(t) | ||
const res = await axios.get("/health") | ||
const res = await axios.get("/api/health") | ||
|
||
t.truthy(res.data.ok) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not make the redirect work, maybe there is some issue with having /app and /pages simultaneously.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea it's ok to not have /pages and /app simultaneously 👍 we should support app directory but that's non-critical for now