npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-make-app-proxy

v1.0.2

Published

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).

Downloads

34

Readme

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