From db53184e1d67a0b6428e812af4e42106f5b0693f Mon Sep 17 00:00:00 2001 From: Hunter Tunnicliff Date: Sat, 16 Jul 2022 08:38:09 -0700 Subject: [PATCH] Remove `ExpressMiddleware` --- EXAMPLES.md | 6 ++--- README.md | 64 +++++++++++----------------------------------------- lib/types.ts | 33 +++++++++++---------------- 3 files changed, 29 insertions(+), 74 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index d489175..d7fa6b2 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -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()); @@ -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 diff --git a/README.md b/README.md index 9e803c5..277a29f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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(); @@ -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(); @@ -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 - ): Promise; -} -``` - -#### `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; -} -``` - -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 = ( + req: Request, + res: Response, + next: () => Promise +) => Promise; ``` diff --git a/lib/types.ts b/lib/types.ts index 082e472..fd355ed 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -1,23 +1,16 @@ import type { NextApiRequest, NextApiResponse } from "next"; -export interface NextMiddleware { - ( - req: NextApiRequest, - res: NextApiResponse, - next: () => Promise - ): Promise; -} +export type Middleware = ( + req: Request, + res: Response, + next: () => Promise +) => Promise; -export interface ExpressMiddleware { - ( - req: Request, - res: Response, - next: (error?: any) => void | Promise - ): void; -} - -export type Middleware = NextMiddleware | ExpressMiddleware; - -export interface LabeledMiddleware { - [name: string]: Middleware | Middleware[]; -} +export type LabeledMiddleware< + Request = NextApiRequest, + Response = NextApiResponse +> = { + [name: string]: + | Middleware + | Array>; +};