
@foomo/next-proxy-middleware
A Next.js middleware for proxying requests to external services with advanced configuration options.
Installation
npm install @foomo/next-proxy-middleware
Usage
Import the middleware and configuration type in your Next.js middleware file:
import {
createProxyMiddleware,
DevProxyConfig,
} from "@foomo/next-proxy-middleware";
Configuration
The DevProxyConfig
type defines the following options:
export type DevProxyConfig = {
debug?: boolean;
disable?: boolean;
remoteUrl: string | ((request: NextRequest) => string);
allowResponseCompression?: boolean;
overrideHostHeader?: boolean;
overrideCookieDomain?: false | string;
basicAuth?: {
authHeader: string;
};
cfTokenAuth?: {
clientId: string;
clientSecret: string;
};
};
Example
Here's an example of how to use the middleware in your middleware.ts
file:
import {
createProxyMiddleware,
DevProxyConfig,
} from "@foomo/next-proxy-middleware";
const proxyConfig: DevProxyConfig = {
remoteUrl: "https://api.example.com",
basicAuth: {
authHeader: "Basic abc123==",
},
overrideCookieDomain: "example.com",
};
export function middleware(request: NextRequest) {
if (request.nextUrl.pathname.match("^/(api|webhooks)/")) {
return proxyMiddleware(request);
}
return request;
}
export const config = {
matcher: ["/api/:path*"],
};
const proxyMiddleware = createProxyMiddleware(proxyConfig);
Contributing
Contributions are welcome! Please fork the repository and submit a pull request.
License
Distributed under MIT License, please see license file within the code for more details.
Made with ♥ foomo by bestbytes