Next.js Discord

Discord Forum

Exporting a deeply nested series of routes

Unanswered
Large garden bumble bee posted this in #help-forum
Open in Discord
Avatar
Large garden bumble beeOP
Hello, I am exporting a client which will be used by a bunch of Next.js apps. I want to distribute a "proxy" over an SDK I have, which may be deeply nested. It would look like this:
./src/app/api
├── film
│   ├── [id]
│   │   └── route.ts
│   └── route.ts
└── review
    ├── [id]
    │   └── route.ts
    └── route.ts

These are basically set up to use my SDK and extract values from the NextRequest. As an example, let's say I want to look at app/api/films
export async function GET(request: NextRequest) {
  const limit = request.nextUrl.searchParams.get('limit');
  const offset = request.nextUrl.searchParams.get('offset');

  const data = await client.films.findMany({ limit, offset });
  return NextResponse.json(data);
}

I want to have this as a package where people can simply import it and somehow use it in a way where they can set up an API key. As an example, maybe like this:
const routeHandlers = createRouteHandlers(process.env.FILM_DB_API_KEY);

export routeHandlers;

How would this be done? Can it be done?

1 Reply