Closed xristos3490 closed 1 year ago
Is this safe to assume that we can extend the AbstractRoute and/or the AbstractCartRoute classes?
I would say yes. We would want to enforce this type of consistency for custom routes if we want to take care of any heavy lifting for third-party implementing custom routes (ie registering routes).
The RoutesController and the SchemaController don't include any hooks to add custom endpoints. Does this mean we must implement the endpoint from scratch using the register_rest_route() function?
Definitely, there is a chance to improve the Store API init. I would say yes to that question. In theory, third-party could extend the RoutesController, define their own $routes property, and add an action to 'rest_api_init' calling the register_all_routes() method for that class. In realistic terms, it doesn't seem to be worth it since we're hardcoding versioning and namespacing and without re-declaring the whole class we would be limiting the freedom for that.
IMHO, this would represent a good point to justify refactoring StoreApi, RoutesController, etc, and include proper support for custom endpoints to be added. Also, judging by the unused imports on those files, they look like good candidates for reviewing 🤔
EDIT: I misread the namespace, those are not unused imports, just unnecessary ones.
The above quote:
If any of the following statements are true, choose to extend the Store API
Is referring to the schema extend classes for existing Store API routes, specifically these:
If an extension has unique needs that require a new route, there is nothing to extend. register_rest_route
would be used (on the rest_api_init
hook), and for that, all WordPress Rest API documentation applies:
https://developer.wordpress.org/reference/functions/register_rest_route/
There would be no requirement to extend Store API classes such as AbstractRoute
, in fact that would probably make the task more difficult because of how Schema classes are hooked up.
As for authentication, this is something defined by the route:
For StoreAPI, permission callback is just set to __return_true
(no permission needed). Store API does not cover authentication nor have a recommended way of providing authentication to extensions.
Description
This request originated from an internal discussion (pdqkbG-1iZ-p2) about whether handling/adding extension data on the cart resource is applicable. If not, developers must be able to extend the StoreAPI using custom endpoints that will serve their data to the React application.
For visibility, here's the quote that kick-started this entire discussion:
That said, here are some of my questions when I first started analyzing the work for a new custom endpoint: (there are a lot 😅)
The PHP side:
AbstractRoute
and/or theAbstractCartRoute
classes?RoutesController
and theSchemaController
don't include any hooks to add custom endpoints. Does this mean we must implement the endpoint from scratch using theregister_rest_route()
function?The JS side:
useEffect()
context) in a component enough to showcase the usage? (probably yes)Please keep in mind that I'm unfamiliar with architecting React applications, and these questions might come in as "noob-ie." Still, given that the average WP developer isn't familiar with the process, I believe we need to showcase a simple example from start to finish.
Perhaps we could document on two different resources: