1.0.2 • Published 4 months ago

next-make-app-proxy v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

next-make-app-proxy GitHub license npm version

Create a NextJS App Router Route Handler that forwards your requests to a separate backend server, with the option to inject contextual information to your requests (e.g. your users' access token).

Features

  1. Zero configurations needed, and highly configurable.
  2. Zero dependencies.
  3. Protect your backend using your NextJS sessions. Integrate with libraries like @auth0/nextjs-auth0.
  4. Inject things (e.g. access tokens, API keys, whatever) into the request header that you'll be sending to your backend.

Why would you create this? Don't we already have rewrites?

Because you can't inject headers to the requests being sent. I wanted to inject the user's access token that is stored in the Auth0 SDK to all downstream requests, so my Go API can validate my user's authentication.

Getting Started

Installation

NextJS is only included as a peer dependency, so make sure you've installed it beforehand.

# for yarn
yarn add next next-make-app-proxy

# for npm
npm i next next-make-app-proxy

Usage

Basic

// app/api/my-api/route.ts
import { makeProxyHandler } from 'next-make-app-proxy'

// generic GET handler
export const GET = makeProxyHandler('https://yourapi.com/my-api')

// forwards your request body to your backend
export const POST = makeProxyHandler('https://yourapi.com/my-api', {
  withReqBody: true,
})

With path params

// app/api/my-api/[id]/route.ts
import { makeProxyHandler } from 'next-make-app-proxy'

// path params will be automatically forwarded - example usage: curl localhost:3000/api/my-api/123
export const GET = makeProxyHandler('https://yourapi.com/my-api/:id')

Advanced Usage

Customising your app's default proxy handler (bonus: with Auth0 SDK example)

// some/path/to/your/default/proxy/handler.ts
import { getCommonHeaders } from '@/server/utils/headers' // internal lib
import { withApiAuthRequired } from '@auth0/nextjs-auth0' // version ^3.5.0
import { proxy } from 'next-make-app-proxy'

const appProxy = proxy.withOpts({
  async headers(): Promise<Record<string, string>> {
    // this injects the user's access token to the Authorization header of all of my proxied requests
    return await getCommonHeaders()
  },
  // this actually works - wrap the resulting Route Handler with whatever HOC (Higher Order Component) you want
  hocWrappers: [withApiAuthRequired],
})

// it's important to bind it, otherwise it won't be able to retrieve your default opts
export const makeProxyHandler = appProxy.makeProxyHandler.bind(appProxy)

// app/api/my-api/route.ts
import { makeProxyHandler } from 'some/path/to/your/default/proxy/handler.ts' // import it

// use it
export const GET = makeProxyHandler('https://yourapi.com/my-api/:id')

// you can also override each individual option
export const POST = makeProxyHandler('https://yourapi.com/my-api', {
  withReqBody: true,
  hocWrappers: [],
})

Contributing? Need a Feature?

Just chuck me a PR or an Issue in the repo and I'll review it. No need for templates (for now).

New Issue

1.0.2

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago

0.1.9

4 months ago

0.1.8

4 months ago

0.1.7

4 months ago

0.1.6

4 months ago

0.1.5

4 months ago

0.1.4

4 months ago

0.1.3

4 months ago

0.1.2

4 months ago

0.1.1

4 months ago

0.1.0

4 months ago