Skip to content

Commit

Permalink
Remove ExpressMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
htunnicliff committed Jul 16, 2022
1 parent 6b879a0 commit db53184
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 74 deletions.
6 changes: 3 additions & 3 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
Middleware that does work before a request:

```ts
import type { NextMiddleware } from "next-api-middleware";
import type { Middleware } from "next-api-middleware";

export const addRequestUUID: NextMiddleware = async (req, res, next) => {
export const addRequestUUID: Middleware = async (req, res, next) => {
// Set a custom header
res.setHeader("X-Response-ID", uuid());

Expand All @@ -34,7 +34,7 @@ export const addRequestTiming = async (req, res, next) => {
Middleware that catches errors that occur in remaining middleware _and_ the request:

```ts
export const logErrorsWithACME: NextMiddleware = async (req, res, next) => {
export const logErrorsWithACME: Middleware = async (req, res, next) => {
try {
// Catch any errors that are thrown in remaining
// middleware and the API route handler
Expand Down
64 changes: 13 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,18 @@ This library attempts to provide minimal, clean, composable middleware patterns
- [Advanced](#advanced)
- [Debugging](#debugging)
- [Middleware Factories](#middleware-factories)
- [Middleware Types](#middleware-types)
- [`NextMiddleware`](#nextmiddleware-preferred)
- [`ExpressMiddleware`](#expressmiddleware-compatibility)
- [Middleware Signature](#middleware-types)

## Quick Start

```ts
import { label, NextMiddleware } from "next-api-middleware";
import { label, Middleware } from "next-api-middleware";
import * as Sentry from "@sentry/nextjs";
import nanoid from "nanoid";

// 1 – Create middleware functions

const captureErrors: NextMiddleware = async (req, res, next) => {
const captureErrors: Middleware = async (req, res, next) => {
try {
// Catch any errors that are thrown in remaining
// middleware and the API route handler
Expand All @@ -62,7 +60,7 @@ const captureErrors: NextMiddleware = async (req, res, next) => {
}
};

const addRequestId: NextMiddleware = async (req, res, next) => {
const addRequestId: Middleware = async (req, res, next) => {
// Let remaining middleware and API route execute
await next();

Expand Down Expand Up @@ -293,11 +291,11 @@ Since `use` and `label` accept values that evaluate to middleware functions, thi
Here's an example of a factory that generates a middleware function to only allow requests with a given HTTP method:

```ts
import { NextMiddleware } from "next-api-middleware";
import { Middleware } from "next-api-middleware";

const httpMethod = (
allowedHttpMethod: "GET" | "POST" | "PATCH"
): NextMiddleware => {
): Middleware => {
return async function (req, res, next) {
if (req.method === allowedHttpMethod || req.method == "OPTIONS") {
await next();
Expand All @@ -311,50 +309,14 @@ const httpMethod = (
export const postRequestsOnlyMiddleware = httpMethod("POST");
```

### Middleware Types
### Middleware Signature

`next-api-middleware` supports two middleware signatures, `NextMiddleware` and `ExpressMiddleware`.

#### `NextMiddleware`

`NextMiddleware` is inspired by the asyncronous middleware style popularized by Koa.js. Prefer this style when writing your own middleware.
`Middleware` is inspired by the asyncronous middleware style popularized by Koa.js.

```ts
interface NextMiddleware {
(
req: NextApiRequest,
res: NextApiResponse,
next: () => Promise<void>
): Promise<void>;
}
```

#### `ExpressMiddleware` (compatibility)

`ExpressMiddleware` roughly matches the signature used by Express/Connect style middleware. This type can be used when importing third-party libraries such as `cors`.

```ts
interface ExpressMiddleware<
Request = IncomingMessage,
Response = ServerResponse
> {
(
req: Request,
res: Response,
next: (error?: any) => void | Promise<void>
): void;
}
```

An example using `cors`:

```ts
import { use } from "next-api-middleware";
import cors from "cors";

export const withMiddleware = use(
// Asserting express/connect middleware as an `ExpressMiddleware` interface
// can resolve type conflicts by libraries that provide their own types.
cors() as ExpressMiddleware
);
type Middleware<Request = NextApiRequest, Response = NextApiResponse> = (
req: Request,
res: Response,
next: () => Promise<void>
) => Promise<void>;
```
33 changes: 13 additions & 20 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import type { NextApiRequest, NextApiResponse } from "next";

export interface NextMiddleware {
(
req: NextApiRequest,
res: NextApiResponse,
next: () => Promise<void>
): Promise<void>;
}
export type Middleware<Request = NextApiRequest, Response = NextApiResponse> = (
req: Request,
res: Response,
next: () => Promise<void>
) => Promise<void>;

export interface ExpressMiddleware<Request = any, Response = any> {
(
req: Request,
res: Response,
next: (error?: any) => void | Promise<void>
): void;
}

export type Middleware = NextMiddleware | ExpressMiddleware;

export interface LabeledMiddleware {
[name: string]: Middleware | Middleware[];
}
export type LabeledMiddleware<
Request = NextApiRequest,
Response = NextApiResponse
> = {
[name: string]:
| Middleware<Request, Response>
| Array<Middleware<Request, Response>>;
};

0 comments on commit db53184

Please sign in to comment.