brillout / wildcard-api

Functions as API.
MIT License
368 stars 14 forks source link

Namespaced endpoints? #55

Closed gamedevsam closed 4 years ago

gamedevsam commented 4 years ago

Can you have namespaced endpoints with wildcard?

For example:

endpoints.users.create = async function() {...}
endpoints.posts.create = async function() {...}

I know this is a bit semantics since you could have a createUser and createPost endpoints, but as the API surface grows being able to have namespaced routes becomes a valuable tool.

brillout commented 4 years ago

Hi Samuel,

it's currently not possible. Would there be any drawback for your use case to do the following instead?

endpoints.users_create = async function() {...};
endpoints.posts_create = async function() {...};

Note that with Wildcard it's best not to think in terms of CRUD operations. Rather, you can see Wildcard just as a thin secure layer wrapping SQL/ORM queries; image you'd directly use SQL/ORM queries on your frontend. Does that make sense to you?

gamedevsam commented 4 years ago

Yes this is not much of an issue, was more of a curiosity for my own sake. I imagine adding this type of functionality would not be too difficult if someone really wanted it since endpoints is just a proxy which could be modified to handle objects with function keys as well as simple functions.

On a separate note, can you explain why named arrow functions are not allowed in wildcard? Is it for consistency or is there a runtime reason why they are problematic?

brillout commented 4 years ago

Yes and let's keep this open to gauge community interest :).

Because arrow functions don't have a this object.

Let me know if you have any other questions :)

brillout commented 4 years ago

Closing this but if someone wants this let me know. I'm open to implement what Samuel said:

I imagine adding this type of functionality would not be too difficult if someone really wanted it since endpoints is just a proxy which could be modified to handle objects with function keys as well as simple functions.