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
Allow defining custom API routes (http) #268
Comments
There are couple approaches to consider here:
4 is probalby the strongest solution, 2 is a quick way out, while 1 and 2 are probably useful in their own regard but don't address the problem directly / completely. |
Regarding middleware -> we talked a lot about it in some other capacities, e.g. Permissions, so I will also link to that #584 . @shayneczyzewski was big part of those previous conversations so also might have something to say here. |
Here is request from a Waspeteer that is related to this:
|
I think we all agree our main choices are escape hatch into Express (super easy, but lose lots of info), or we provide Wasp syntax. The Wasp syntax could take many forms, but one quick one that may be nice is if we just provided them a way to say something like: post { path: "/foo/<id:integer>", to: import { sayHi } from "@server/actions.js" } So we would let them specify the HTTP verb as a keyword, and in the path we can also encode some type info. This may also dovetail into the top level data schema issue #642 |
One more idea: What if we would allow them to define Operations with custom API? Think about it as a hybrid between custom API routes and normal Operations -> Custom Operation. So what Custom Operation let's you do is define its behaviour in more details, including what its REST API looks like. I guess you would implement a piece of logic on backend that handles a request and then returns Oooo this sounds exciting to me! Not only do we get custom API but we get custom Operations! Maybe this turns out to be silly if we dig a bit deeper, or unfeasible, but it sounds like it could be potentially doable and pretty cool to me! |
Interesting idea. I wonder how much impact this would have on the TS stuff @sodic is doing, but this is definitely one way to go about it and it does feel a bit more aligned with our conceptual framework. One thing I wonder if this could support though is websockets? Maybe that is just another verb though, so users can define |
That is interesting! I was always imagining we will have a special abstraction for sockets, due to their "live" nature. I am having a bit hard time now imagining how this would work in the context of Operations, but I am sure it can in some way I just don't have enough knowledge at the moment. Interesting! |
Related issue: #983 -> exposing |
I gave it a bit of thought @Martinsos, and tried to play off your idea about adding a route notion to Operations. What if it looked like something as simple as:
The You still invoke it in the same way: const { data: task, isFetching, error } = useQuery(getTask, { id: taskId, show: "everything" }) In fact, we can now do a better job of warning the user if they do not supply everything. This now just becomes a URL like You now get some extra things like this in your query, as mentioned: export const getTask: GetTask<Task> = async ({ id, show }, context) => {
// can use context.req or context.res
} Users can then do things like Possible types:
Maybe we can also define some notion of optionality with a Possible verbs:
Then, anything else more complex requires using |
Looks pretty cool @shayneczyzewski ! Ok, so basically we allow them to define custom HTTP route, with mapping from it to the Are these two things orthogonal? We could expose If so, should we always give them By giving them control over How much flexibility can they achieve with this approach you suggested? Instead of us being smart and implementing this small language/DSL for routes that maps from route to args and vice versa, we could just let them provide two functions: one that receives route as a string and produces JS object, and vice versa. So basically they could define those mappings on their own, via two functions. But I do like the idea that we do this for them, that is quite nice indeed, just wanted to think through this. We could add Yup, we should also have |
Had a quick chat with @shayneczyzewski , we concluded that we could think about this in following steps:
It is good to remind ourselves that main use case for custom routes is for callbacks and some custom stuff, not for RPC between client and frontend -> for RPC we have operations. I think we certainly need to do (1) and I do think we should also go for (3) after it, it is much nicer than (1). I think (2) is also attractive, but we should evaluate a bit how useful it is, and we should check with @sodic how it plays with adding types to Operations, because there will be some interplay there almost certainly. |
@Martinsos @shayneczyzewski I'll reply to the conclusions outlined in the last comment.
I agree this makes sense as the first step.
Can't say I agree with this one. I think we should either:
Exposing
Me neither 😄. One more reason to stick with the "dumb"
This makes a lot of sense to me. I even like that you can't use it with
Yes, Exactly!
Types shouldn't be an issue here, but I'd definitely punt on (2) for now. |
Thanks @Martinsos and @sodic. I appreciate the discussion and thoughts. I agree with the next steps. So the plan will be:
Thanks again! 🚀 |
@shayneczyzewski sounds like a plan! @sodic although we agree about punting on (2) for now, I wanted to follow up on couple things, for the sake of further discussion we might do on this topic:
|
Right now dev can define operations (actions, queries) which are then consumed from client via RPC that works via http.
However, they can't define custom http API roues at the moment! They might need this, so we should offer them support for this.
This basically means they should be able to drop one level of abstraction below the operations and directly define http routes (in Express right now).
The text was updated successfully, but these errors were encountered: