next-safe-action
next-safe-action copied to clipboard
Type safe and validated Server Actions in your Next.js project.
next-safe-action is a library that takes full advantage of the latest and greatest Next.js, React and TypeScript features, using validation libraries of your choice, to let you define typesafe Server Actions and execute them inside Client Components.
How does it work?
Like magic. 🪄
https://github.com/TheEdoRan/next-safe-action/assets/1337629/7ebc398e-6c7d-49b2-8d03-e85e17d2ae32
Features
- ✅ Pretty simple
- ✅ End-to-end type safety
- ✅ Context based clients (with middlewares)
- ✅ Input validation using multiple validation libraries
- ✅ Advanced server error handling
- ✅ Optimistic updates
Documentation
Explore the documentation for version 6 of the library on the next-safe-action website. ✨
If you are looking for old versions documentation, check out:
- the README_v3 file for version 3.x.x.
- the README_v2 file for version 2.x.x.
Next.js >= 14 vs 13
Server Actions are now a stable feature in React. For this reason, there were some internal breaking changes to the API, and so a new version of the library is required to prevent backward compatibility issues.
You can continue to use next-safe-action version 4 for your Next.js 13 app.
npm i next-safe-action # for Next.js >= 14
npm i next-safe-action@v4 # for Next.js 13
Migration guide
Version 4.x.x of the library introduced some breaking changes to the API. Please check out the v3 to v4 migration guide on the website for more information.
Version 5.x.x of the library is required for Next.js >= 14 applications. Please check out the v4 to v5 migration guide on the website for more information.
Version 6.x.x of the library lets you use validation libraries other than Zod. Please check out the v5 to v6 migration guide on the website for more information.
Example
You can find a basic working implementation of the library here.
Contributing
If you want to contribute to next-safe-action, the best way is to fork this repository and open a pull request when you're done implementing your feature.
If you found bugs or just want to ask a question, feel free to open an issue.
Contributors
Made with contrib.rocks.
License
next-safe-action is released under the MIT license.