Skip to content

Commit 0e953c2

Browse files
author
zoedsoupe
committed
refactor: remove tidewaveMiddleware in favor of explicit setup
1 parent e065130 commit 0e953c2

3 files changed

Lines changed: 8 additions & 69 deletions

File tree

README.md

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,37 +84,18 @@ export const config = {
8484
**Middleware** - Then create (or modify) `middleware.ts` with:
8585

8686
```typescript
87-
import { tidewaveMiddleware } from 'tidewave/next-js';
88-
89-
export const middleware = tidewaveMiddleware();
90-
91-
export const config = {
92-
matcher: '/tidewave/(.*)',
93-
};
94-
```
95-
96-
In case you already have an existing `middleware.ts`:
97-
98-
```typescript
99-
import { tidewaveMiddleware } from 'tidewave/next-js';
100-
101-
const withTidewave = tidewaveMiddleware();
102-
10387
export function middleware(req: NextRequest): Promise<NextResponse> {
104-
// This will return a unchanged NextResponse.next()
105-
// if path doesn't match with `/tidewave`
106-
// if does match it will rewrite to `/api/tidewave` instead
107-
withTidewave(req, (req: NextRequest) => {
108-
// your own logic
109-
return NextResponse.json({ message: 'hello, world!' });
110-
});
88+
if (req.nextUrl.pathname.startsWith('/tidewave')) {
89+
return NextResponse.rewrite(new URL(`/api${pathname}`, req.url));
90+
}
91+
92+
// here you could add your own logic or different middlewares
93+
// while changing the config.matcher to a string[]
94+
return NextResponse.next();
11195
}
11296

11397
export const config = {
114-
matcher: [
115-
'/tidewave/(.*)', // tidewave specific matcher
116-
'/your/route/', // your specific matcher
117-
],
98+
matcher: '/tidewave/(.*)',
11899
};
119100
```
120101

src/next-js.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { NextApiRequest, NextApiResponse } from 'next';
2-
import { NextResponse, type NextRequest } from 'next/server';
32
import { checkSecurity, HANDLERS, methodNotAllowed, type Request, type Response } from './http';
43
import bodyParser from 'body-parser';
54
import type { TidewaveConfig } from './core';
@@ -12,7 +11,6 @@ const DEFAULT_CONFIG: TidewaveConfig = {
1211
};
1312

1413
type NextJsHandler = (_req: NextApiRequest, _res: NextApiResponse) => Promise<void>;
15-
type NextJsMiddleware = (_req: NextRequest) => Promise<NextResponse>;
1614

1715
type NextHandler = () => ValueOrPromise<unknown>;
1816

@@ -83,30 +81,3 @@ export async function tidewaveHandler(
8381
return res.status(404).json({ message: `Route not found: ${req.method} ${req.url}` });
8482
};
8583
}
86-
87-
export function tidewaveMiddleware(): NextJsMiddleware {
88-
const env = process.env.NODE_ENV;
89-
90-
if (!(env === 'development')) {
91-
throw Error(
92-
`[Tidewave] tidewave is designed to only work on development environment, got: ${env}`,
93-
);
94-
}
95-
return async function middleware(
96-
req: NextRequest,
97-
next?: NextJsMiddleware,
98-
): Promise<NextResponse> {
99-
const { pathname } = req.nextUrl;
100-
101-
if (!isTidewaveRoute(pathname) && next) return next(req);
102-
if (!isTidewaveRoute(pathname)) return NextResponse.next();
103-
104-
// since next.js v12, middleware doesn't
105-
// accept relative URLs
106-
return NextResponse.rewrite(new URL(`/api${pathname}`, req.url));
107-
};
108-
}
109-
110-
export function isTidewaveRoute(pathname: string): boolean {
111-
return pathname.startsWith('/tidewave');
112-
}

test/next-js.test.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)