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

Cannot create API route containing "pages" in the path #564

Open
mathcrln opened this issue Mar 3, 2023 · 5 comments
Open

Cannot create API route containing "pages" in the path #564

mathcrln opened this issue Mar 3, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@mathcrln
Copy link

mathcrln commented Mar 3, 2023

Hey!

I am trying to create an API route to handle all requests from an admin to modify the pages of the public website. Base requests will be made at '/api/admin/pages', as you can see in the file structure below.

CleanShot 2023-03-02 at 22 01 54

CleanShot 2023-03-02 at 22 04 30

However, when I try this, I come across a 404 Cannot GET error. It's as if next-api-decorators recognizes the path, but that no GET endpoint was defined, which is not the case as you can see above.

CleanShot 2023-03-02 at 22 03 57

After some further testing, I observed that it's specific to the endpoint containing "pages" in the path. Everything else seems to work (tried "p", "pa", "pag", "page" or any other random set of characters) as the route is found and the intended data is returned, but as soon as i put it back to "pages" it returns this error.

Is it a known bug/behaviour?

(I imagine it might be a regex issue to avoid conflicts with the Next.js "pages" directory, or something like that?)

How to reproduce

  1. Create a Next.js app with next-api-decorators
  2. Create an endpoint that would return basic data as shown above "api/admin/pages" (GET /)
  3. See the error
  4. Change the endpoint to anything else
  5. Observe that the expected data is returned

I looked around and didn't find any issue related to this one, so I hope it's not a duplicate. I didn't find any reference to this in the documentation either.

@ggurkal
Copy link
Member

ggurkal commented Mar 5, 2023

Hi @mathcrln

Can't replicate the issue you're having with the steps you've written. Please provide a minimum reproducible repo.

@mathcrln
Copy link
Author

mathcrln commented Mar 5, 2023

Here is a minimum reproducible repo : https://github.com/mathcrln/nad-pages-bug.

While creating the repo, I could observe that it only happens once "path-to-regexp" is installed (@6.2.1), per the documentation for route matching. Before that, there is no issue, as you could observe.

As you will notice from this repo:

  • /admin/api/users is always accessible
  • /admin/api/page is always accessible
  • /admin/api/pages is not accessible if "path-to-regexp" is installed

When trying to access /admin/api/pages, error {"statusCode":404,"message":"Cannot GET /api/admin/pages","error":"Not Found"} is returned, proving that next-api-decorators recognizes it (instead of throwing a normal next.js 404) but somehow cannot find an existing GET endpoint for it.

Same thing happens for api/**/pages, hence no matter the level of that directory, as long as it contains "pages".

Please let me know if you have any more question.

PS: I'm unfamiliar with the codebase of next-api-decorators, it seems like this code could be related?

directoryPath.split('/pages')[1].replace(/(\[[0-9a-zA-Z-]+\])/, '([0-9a-zA-Z-]+)') +

@ggurkal
Copy link
Member

ggurkal commented Mar 13, 2023

Hi @mathcrln

Sounds very likely that the part you're referencing is the problem. I'll take a look when I have some time.

@ggurkal ggurkal added the bug Something isn't working label Mar 13, 2023
@mathcrln
Copy link
Author

Sure, no worries! I wanted to open a Pull Request but encountered some issues with the tests.
In general, do you accept/look for contributions?

@ggurkal
Copy link
Member

ggurkal commented Mar 18, 2023

Hi @mathcrln

PRs are always welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants