Astro API Endpoint Middleware RFC proposal. #355
Replies: 5 comments 5 replies
-
I would love to have first-class support for this! I also think this might be prototypable in userland, which is really exciting. 🎉 Take a look at how our |
Beta Was this translation helpful? Give feedback.
-
Cool API idea, this has come up a lot so I think we definitely should start working on something for middleware. Couple of things that we talked about on the call:
|
Beta Was this translation helpful? Give feedback.
-
Thank you everyone for your comments and prompt feedback @natemoo-re @matthewp. To address the questions brought up above, I have composed a list of some initial solutions. Initial implementation.One way I see the implementation of the Middleware RFC for Astro Pages and Endpoints, would be to have separate files for middleware, such as // /pages/api/index.middleware.ts
import { APIContext } from "astro";
export const get = (ctx: APIContext) => { ... };
export const post = (ctx: APIContext) => { ... }; In the best case scenario, if one does not really care about the HTTP method used while sending a request to a specific route, and wants to apply the middleware to all the requests regardless of the method, they could just export a named // /pages/api/index.middleware.ts
import { APIContext } from "astro";
export const all = (ctx: APIContext) => { ... }; Alternative implementation.Suppose we have defined a custom middleware with import { mwrap } from "astro";
export const withAuth = mwrap(async (ctx) => { ... });
/// ... The way Then in the import { withAuth, withJsonPayload } from "<wrapped-middleware-definition-path>";
export const all = [withAuth];
export const post = [withJsonPayload]; Middleware state persistence.One of the most important things in this RFC which would need implementation would be exposing a state API from the For example, if one would want to have a Regarding the second question, I have not really considered using provider-specific middleware implementations such as CloudFlare Middleware Functions, nor do I have any experience in using them. |
Beta Was this translation helpful? Give feedback.
-
Having middle-wares is really a nice idea, although there should be some config which should allow us to define which middle-ware goes where.
We need to have a global config or something where we could define a matching pattern for the routes and based on that we could add middlewares. Because if go with the @michaelgrigoryan25 approach |
Beta Was this translation helpful? Give feedback.
-
The middleware files will be applied to all the nested routes. So, for example, if you have a middleware file in the
Having a global config like in Next.js would not be really useful in my opinion, because after all Astro is a an MPA not SPA. In case of Next.js only SPAs are intended and they define all the logic in a single middleware file, instead of organizing them in subroutes. I feel that having a single/global middleware file will not be scalable and will be hard to maintain. |
Beta Was this translation helpful? Give feedback.
-
Hello everyone, I hope you are all doing well and safe. I'd like to begin by thanking everyone involved with Astro for your hard work and dedication; you guys have done an excellent job!
Currently, if we want authentication around our desired routes, we must create a separate function:
and call it from our endpoint the following way:
We're currently doing this with multiple middleware handlers, so one handles authentication, another handles parsing the JSON payload from the request body, and so on.
and in our endpoints we define it the following way:
This works. However, as you can imagine, this can become overly complex when used with more middleware handlers that contain much more code. One of my ideas is to have a function
mwrap()
(or something similar) which will take the middleware logic, wrap it internally (perhaps with some metadata, so that Astro can treat it as a middleware), and store the state returned from the middleware in a separatestate
key on the requestctx
. Or in case we want to short-circuit the request, return aResponse
.When creating middleware, we will define our function the following way:
Finally, in our endpoint file, we would use our middleware the following way:
Just so everyone is aware, I am completely willing to work on this RFC. However, more feedback in this discussion section and approval from the maintainers would have been ideal. I'd appreciate if you left some of your comments or suggestions about this proposal below.
Beta Was this translation helpful? Give feedback.
All reactions