Skip to content

Commit 3afaa80

Browse files
committed
feat: add simple example of a nextjs project integrating the middleware, update eslint to ignore examples folder
1 parent 541182e commit 3afaa80

File tree

15 files changed

+6025
-2
lines changed

15 files changed

+6025
-2
lines changed

eslint.config.mjs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ export default tseslint.config(
77
eslint.configs.recommended,
88
tseslint.configs.recommended,
99
{
10-
ignores: [".husky/", "coverage/", "dist/", "docs/", "eslint.config.mjs"],
11-
//files: ["src/**/*.ts"],
10+
ignores: [
11+
".husky/",
12+
"coverage/",
13+
"dist/",
14+
"docs/",
15+
"examples/",
16+
"eslint.config.mjs",
17+
],
1218
},
1319
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["next/core-web-vitals", "next/typescript"]
3+
}

examples/simple-example/.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.*
7+
.yarn/*
8+
!.yarn/patches
9+
!.yarn/plugins
10+
!.yarn/releases
11+
!.yarn/versions
12+
13+
# testing
14+
/coverage
15+
16+
# next.js
17+
/.next/
18+
/out/
19+
20+
# production
21+
/build
22+
23+
# misc
24+
.DS_Store
25+
*.pem
26+
27+
# debug
28+
npm-debug.log*
29+
yarn-debug.log*
30+
yarn-error.log*
31+
32+
# env files (can opt-in for committing if needed)
33+
.env*
34+
35+
# vercel
36+
.vercel
37+
38+
# typescript
39+
*.tsbuildinfo
40+
next-env.d.ts

examples/simple-example/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# About
2+
3+
This is an example project using Next.js's app router and the `@qacomet/next-api-middleware` library giving a simple demonstration of the middleware together with basic API routes. There are two examples given, one using a class-based handler and another using a file-based handler.
4+
5+
The main relevant files are
6+
7+
```sh
8+
app/middleware/index.ts
9+
app/dashboard/route.ts
10+
app/dashboard/[userId]/route.ts
11+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { middleware } from "@/app/middleware";
2+
import { NextRequest, NextResponse } from "next/server";
3+
import { type NextRouteHandlerContext } from "@qacomet/next-api-middleware";
4+
5+
const getHandler = async function (_request: NextRequest, context: NextRouteHandlerContext) {
6+
const params = await context!.params;
7+
console.log("getHandler")
8+
return new NextResponse(`params: ${JSON.stringify(params)}`);
9+
}
10+
11+
export const { GET } = middleware.routes({ GET: getHandler }).build();
12+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { middleware } from "@/app/middleware";
2+
import { NextResponse } from "next/server";
3+
4+
class Handlers {
5+
data: string
6+
constructor () {
7+
this.data = "Handlers data";
8+
this.GET = this.GET.bind(this);
9+
}
10+
11+
async GET () {
12+
console.log("Instance<Handlers>.GET", this.data);
13+
return new NextResponse(`data: ${this.data}`);
14+
}
15+
}
16+
17+
export const { GET } = middleware.routes(Handlers).build();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
export default function RootLayout({
3+
children,
4+
}: Readonly<{
5+
children: React.ReactNode;
6+
}>) {
7+
return (
8+
<html lang="en">
9+
<body>
10+
{children}
11+
</body>
12+
</html>
13+
);
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { RouteMiddleware, type MiddlewareNextFunction } from "@qacomet/next-api-middleware";
2+
import { NextRequest } from "next/server";
3+
4+
export const middleware = RouteMiddleware.from({
5+
name: 'TestMiddleware',
6+
middleware: async function (_req: NextRequest, next: MiddlewareNextFunction) {
7+
console.log("before");
8+
const resp = await next();
9+
console.log("after");
10+
return resp;
11+
}
12+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
export default function Home() {
3+
return (
4+
<div style={{padding: '32px', display: 'flex', flexDirection: 'column', gap: '8px'}}>
5+
<div>Check out <a href="/dashboard">/dashboard/</a></div>
6+
<div>Check out <a href="/dashboard/1234/">/dashboard/:userId/</a></div>
7+
</div>
8+
)
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { NextConfig } from "next";
2+
3+
const nextConfig: NextConfig = {
4+
/* config options here */
5+
};
6+
7+
export default nextConfig;

0 commit comments

Comments
 (0)