Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows custom route.ts exports with the "_" prefix #55157

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from

Conversation

theo-lubert
Copy link

@theo-lubert theo-lubert commented Sep 9, 2023

What?

App Router only allows HTTP methods and Route Segment Config in route.ts files.
This PR lets us export our own custom keys, without altering the supported keys behavior

  • allow all exports
  • only allow custom exports prefixed with _, like _customConfig, _test, ...

Why?

Some cases require to export some info: data, configs, types for the client, etc...
Next.js currently forces us to create separate files, that often implement everything in exactly the same way, and then export the HTTP methods in the route.ts like so:

export { OPTIONS, HEAD, GET, POST, PUT, PATCH, DELETE } from './server'
// server.ts might also export `_customConfig` for example

This can lead to more code duplication and boilerpate

How?

By modifying type definition here (webpack/plugins/next-types-plugin/index.ts#L57), we:

  • allow all exports
  • only allow prefixed exports with "_", like _customConfig, _test, ...

Allowing everything would give us more control, but it will prevent TS from catching typos like export const Get = ... (instead of GET). I believe that's the actual intent behind the current limitation, and I think _ prefixes are a good compromise

Fixes #55140

Copy link

@orca-security-us orca-security-us bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Secrets high 0   medium 1   low 0   info 0 View in Orca

@theo-lubert
Copy link
Author

theo-lubert commented Sep 15, 2023

@ijjk I can't edit labels myself. This PR is not about documentation (though I added some documentation to reflect the code change). It is about changing Next.js route.ts type definition to allow custom exports. Which is not currently possible.

Also the discussion (#55140) hasn't been picked up by anybody, do you know if I could mention someone who could be interested in having a look at my proposal ?

Copy link

@orca-security-us orca-security-us bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca

@theo-lubert theo-lubert marked this pull request as ready for review January 3, 2024 18:16
@theo-lubert theo-lubert requested review from jh3y and delbaoliveira and removed request for a team January 3, 2024 18:16
@balazsorban44 balazsorban44 added Documentation Related to Next.js' official documentation. and removed area: documentation labels Apr 17, 2024
@jens-ox
Copy link

jens-ox commented Sep 9, 2024

Are there any updates on this? I'm trying to colocate OpenAPI docs with the route implementation and this would be really handy.

@ijjk
Copy link
Member

ijjk commented Sep 10, 2024

Allow CI Workflow Run

  • approve CI run for commit: f17b419

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@theo-lubert
Copy link
Author

Are there any updates on this? I'm trying to colocate OpenAPI docs with the route implementation and this would be really handy.

@jens-ox Nothing new unfortunately, it's a bit sad to let small PRs like this one fade away. It has been a year.
Colocation in API routes seems pretty unharmful and could enable a lot of nice features (especially around type inference imo). I wonder if there is more to it, or if it is simply a lack of visibility.

I rebased the PR to have a clean commit that can be merged into the current canary branch, let's see if it can make it to Next.js 15
@leerob you looked at it at least once a while back, maybe you can give us some feedback ? that would be great

@samcx
Copy link
Member

samcx commented Sep 10, 2024

@theo-lubert

Some cases require to export some info: data, configs, types for the client, etc...

Wouldn't you want these /lib type of logic in a separate directory?

could enable a lot of nice features (especially around type inference imo).

Is it possible to elaborate on this (versus having like a separate types.ts file)?

Next.js currently forces us to create separate files

Is the issue because the thought is you can't export multiple methods in a Route Handler?

@theo-lubert
Copy link
Author

@samcx

You could build a custom wrapper in /lib. But you wouldn't write the actual route specific logic outside of the route.ts file.

That wrapper could expose:

  • inferred types from the methods
  • callable local js functions, for use from other routes, RSC, jobs, etc
  • OpenAPI data (as @jens-ox mentionned)
  • metadata
  • ... probably a lot more

There is currently no way to do that from route.ts, where the actual info is.

const apiRoute = customCRUD({ model: UserModel })
// or maybe
const apiRoute = customCRUD({
    create: {
        input: z.object({
            title: z.string(),
        }),
        handler: ({ title }) => return { title }
    },
})

// This works as intended
export { GET, POST } = apiRoute

// This is not allowed, even though `apiRoute` is what contains the actual info, inside `route.ts`
export { _createInputType, _openAPISchema, /* etc */ } = apiRoute
  • An other example for this is a lib I started working on last year (@trestjs/react-query) that exposes reactQuery hooks based on the types inferred form the routes. There is no supported path to make that work in the current Next.js state

@theo-lubert
Copy link
Author

theo-lubert commented Sep 20, 2024

Hi @samcx @leerob what can we do to make this happen ? or at least move in any direction ? A year is a long time. It really is a small change. Thx a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Related to Next.js' official documentation. type: next
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants