@infinum/nextjs-api-dev-proxy
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

@infinum/nextjs-api-dev-proxy

Development API proxy for Next.js pages. It uses http-proxy-middleware under the hood.

Installation

yarn add @infinum/nextjs-api-dev-proxy
# or
npm i @infinum/nextjs-api-dev-proxy

Usage

// pages/api/[[...slug]].ts
import { createProxy } from '@/utils/proxy/proxy';

export default createProxy(process.env.API_URL);

export const config = {
	api: {
		bodyParser: false, // enable POST requests
		externalResolver: true, // hide warning message
	},
};

Multi env

// pages/api/[[...slug]].ts
import { createProxy } from '@/utils/proxy/proxy';

let apiUrl: string;
switch (process.env.API_PROXY) {
	case 'production':
		apiUrl = 'http://production.api.com';
		break;
	case 'uat':
		apiUrl = 'http://uat.api.com';
		break;
	case 'dev':
	default:
		apiUrl = 'http://dev.api.com';
}

export default createProxy(apiUrl);

export const config = {
	api: {
		bodyParser: false, // enable POST requests
		externalResolver: true, // hide warning message
	},
};

Custom config

// pages/api/some/path/[[...slug]].ts
import { createProxy } from '@/utils/proxy/proxy';

export default createProxy(process.env.API_URL, {
	pathRewrite: {
		'^/api/some/path': '/',
	},
	logLevel: NODE_ENV === 'production' ? 'silent' : 'debug',
});

export const config = {
	api: {
		bodyParser: false, // enable POST requests
		externalResolver: true, // hide warning message
	},
};

API

options

The options extend the http-proxy-middleware options.

Name Type Default Description
disable boolean | ((req: NextApiRequest) => boolean) process.env.NODE_ENV !== 'development'' Disable proxy.

Contribution

Yes please! See the contributing guidelines for details.

Licence

This project is licensed under the terms of the MIT license.

Credits

nextjsApiDevProxy is maintained and sponsored by Infinum.

/@infinum/nextjs-api-dev-proxy/

    Package Sidebar

    Install

    npm i @infinum/nextjs-api-dev-proxy

    Weekly Downloads

    5

    Version

    1.1.1

    License

    MIT

    Unpacked Size

    395 kB

    Total Files

    6

    Last publish

    Collaborators

    • safo6m
    • fvoska
    • darkokukovec
    • infinumcom