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

Add Route for Next App Directory #156

Merged
merged 6 commits into from
Oct 27, 2023
Merged

Add Route for Next App Directory #156

merged 6 commits into from
Oct 27, 2023

Conversation

martinmiglio
Copy link
Contributor

@martinmiglio martinmiglio commented Aug 18, 2023

This PR adds an export for a route handler to be used in the latest NextJS App router.

The changes include abstracting out the functionality of the original page router and updating the handler to have a type of type NextAppRouteHandler = (req: NextRequest) => Promise<Response>; to match the described syntax of the App Router's Route Handlers.
With this change of type, the way responses are made is changed, using NextReponse.json() and NextResponse.error()

To implement this route in the App router, something like the following would be used:

// app/api/s3-upload/route.ts

import { AppAPIRoute, sanitizeKey } from "next-s3-upload";

export const POST = AppAPIRoute.configure({
  key(req, filename) {
    return `images/${sanitizeKey(filename)}`;
  },
});

Relates to #154

@vercel
Copy link

vercel bot commented Aug 18, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-s3-upload ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 9, 2023 7:36pm

@ryanto
Copy link
Owner

ryanto commented Aug 29, 2023

Awesome thank you for this PR and getting it added! I'm a little hesitant to duplicating the code because we have a lot of tests that cover various uploads and we'd need to also duplicate those. I agree that an abstraction it the right approach, do you want to take a shot at doing that? Happy to answer any questions or help!

@martinmiglio
Copy link
Contributor Author

I've reduced most of the code duplication, extracted it out into utils/route-builder.ts.
Still need some work done to make the route options maintain the existing API as well as update the tests for these changes.
Let me know what you think so far @ryanto

@ryanto
Copy link
Owner

ryanto commented Aug 29, 2023

Oh awesome, it's looking good! Thanks for doing this, I know that part of the code can be narly with all the config/options everywhere!

👍 Yup I think we should support the existing API so people upgrading don't have to touch their existing pages/api/s3-upload.ts files. Looks like your code will support that though.

Let me know when you want me to take a look and test it. I can take care of adding the automated tests... since I want to create a new Next 13 test app and use it there.

Thanks again for adding this, really appreciate it!

@martinmiglio
Copy link
Contributor Author

Everything looks ready for review, let me know if you want me to write/update any tests or docs.

@martinmiglio martinmiglio marked this pull request as ready for review August 29, 2023 20:27
@devandnow
Copy link

Everything looks ready for review, let me know if you want me to write/update any tests or docs.

Happy to help out testing as well - we have an app being ported to app-router at the mo.

@martinmiglio
Copy link
Contributor Author

@devandnow I would love some more testing on this!

Also just cleaned up the error responses, not sure if I like the console.error's but I don't want errors to go by quietly.

@devandnow
Copy link

devandnow commented Sep 15, 2023

Also just cleaned up the error responses, not sure if I like the console.error's but I don't want errors to go by quietly.

Agree - consolling on the api route will be super helpful whilst everyone settles into the app-router methods. I'll setup a sandbox and will report back :)

@devandnow
Copy link

This all works for me - I tested on a /pages and this new method and didn't notice any major differences so I 'think' all good to go

@devandnow
Copy link

@ryanto be great if you have some time to approve

@thomasmorice
Copy link

@martinmiglio cheers for that, I need it as well 👍

@ryanto ryanto merged commit dfc3bff into ryanto:master Oct 27, 2023
1 of 2 checks passed
@ryanto
Copy link
Owner

ryanto commented Oct 28, 2023

Ok next-s3-upload should now be usable with the app router in version 0.3.4.

Here's all you need:

// app/api/s3-upload/route.js
export { POST } from "next-s3-upload/route";

Huge thanks to @martinmiglio for making this happen!

If anyone tries this let me know how it goes and if you run into any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants