Skip to content

Commit caba53b

Browse files
author
zoey
authored
refactor: remove tidewaveMiddleware in favor of explicit setup (#26)
1 parent e065130 commit caba53b

3 files changed

Lines changed: 10 additions & 67 deletions

File tree

README.md

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

8686
```typescript
87-
import { tidewaveMiddleware } from 'tidewave/next-js';
87+
import { NextRequest, NextResponse } from 'next/server';
8888

89-
export const middleware = tidewaveMiddleware();
89+
export function middleware(req: NextRequest): NextResponse {
90+
const { pathname } = req.nextUrl;
9091

91-
export const config = {
92-
matcher: '/tidewave/(.*)',
93-
};
94-
```
92+
if (pathname.startsWith('/tidewave')) {
93+
return NextResponse.rewrite(new URL(`/api${pathname}`, req.url));
94+
}
9595

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-
103-
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-
});
96+
// here you could add your own logic or different middlewares
97+
// while changing the config.matcher to a string[]
98+
return NextResponse.next();
11199
}
112100

113101
export const config = {
114-
matcher: [
115-
'/tidewave/(.*)', // tidewave specific matcher
116-
'/your/route/', // your specific matcher
117-
],
102+
matcher: '/tidewave/(.*)',
118103
};
119104
```
120105

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)