Skip to content

[Feature] Added proxyReqOverride option #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { NextApiResponse, NextApiRequest } from "next";
import httpProxy, { ServerOptions } from "http-proxy";

export interface NextHttpProxyMiddlewareOptions extends ServerOptions {
pathRewrite?: { [key: string]: string };
/** @type {(proxyReq: any, req: any) => boolean} Override proxyReq event handler. If return true, bypass the original event handler. */
proxyReqOverride?: (proxyReq: any, req: any) => boolean;
}

/**
Expand Down Expand Up @@ -42,7 +45,7 @@ const httpProxyMiddleware = async (
httpProxyOptions: NextHttpProxyMiddlewareOptions = {}
): Promise<any> =>
new Promise((resolve, reject) => {
const { pathRewrite } = httpProxyOptions;
const { pathRewrite, proxyReqOverride } = httpProxyOptions;
if (pathRewrite) {
req.url = rewritePath(req.url as string, pathRewrite);
}
Expand All @@ -57,6 +60,7 @@ const httpProxyMiddleware = async (
}
proxy
.once("proxyReq", ((proxyReq: any, req: any): void => {
if (proxyReqOverride && proxyReqOverride(proxyReq, req)) return;
if (hasRequestBodyMethods.indexOf(req.method as string) >= 0 && typeof req.body === "string") {
proxyReq.write(req.body);
proxyReq.end();
Expand Down