-
Notifications
You must be signed in to change notification settings - Fork 191
Add wp.registerRoute() method to define custom endpoints #176
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
Conversation
|
@jasonphillips @timmyc @elyobo @joehoyle @rmccue I'd love your feedback on this! |
|
To clarify the intent of this function: |
|
I think I like this API more than "auto detect by query endpoint" Can you still pass an "api description" object to some endpoint to force the "auto-build" behavior? |
Do I understand correctly that the |
|
@jasonphillips correct, I do not plan for that to go anywhere, because it is still an integral component of maintaining this library in concert with the API itself. In fact the goal is to have it be even easier.
@gnarf The goal is to have three interfaces for querying an arbitrary endpoint; I'd love your thoughts on whether this is over-extending, but here's the plan:
I see these as being interrelated and being all three useful in their own way, but the purpose of this thread is to hold up that assertion for critique! Input welcome. |
|
Got a positive review in slack from @joehoyle, and have not heard any negative feedback, so we're going to keep moving forward! |
This supersedes both #144 and #158 by providing a method that developers can use to arbitrarily support any endpoint. --------------------------------------- Support for Custom Post Types is provided via the `.registerRoute` method. This method returns a handler function which can be assigned to your site instance as a method, and takes the [same namespace and route string arguments as `rest_register_route`](http://v2.wp-api.org/extending/adding/#bare-basics): ```js var site = new WP({ endpoint: 'http://www.yoursite.com/wp-json' }); site.myCustomResource = site.registerRoute( 'myplugin/v1', '/author/(?P<id>)' ); site.myCustomResource().id( 17 ); // => myplugin/v1/author/17 ``` The string `(?P<id>)` indicates that a level of the route for this resource is a dynamic property named ID. By default, properties identified in this fashion will not have any inherent validation. This is designed to give developers the flexibility to pass in anything, with the caveat that only valid IDs will be accepted on the WordPress end. You might notice that in the example from the official WP-API documentation, a pattern is specified with a different format: this is a [regular expression](http://www.regular-expressions.info/tutorial.html) designed to validate the values that may be used for this capture group. ```js var site = new WP({ endpoint: 'http://www.yoursite.com/wp-json' }); site.myCustomResource = site.registerRoute( 'myplugin/v1', '/author/(?P<id>\\d+)' ); site.myCustomResource().id( 7 ); // => myplugin/v1/author/7 site.myCustomResource().id( 'foo' ); // => Error: Invalid path component: foo does not match (?P<a>\d+) ``` Adding the regular expression pattern (as a string) enabled validation for this component. In this case, the `\\d+` will cause only _numeric_ values to be accepted. **NOTE THE DOUBLE-SLASHES** in the route definition here, however: `'/author/(?P<id>\\d+)'` This is a JavaScript string, where `\` _must_ be written as `\\` to be parsed properly. A single backslash will break the route's validation. Each named group in the route will be converted into a named setter method on the route handler, as in `.id()` in the example above: that name is taken from the `<id>` in the route string. The route string `'pages/(?P<parentPage>[\d]+)/revisions/(?P<id>[\d]+)'` would create the setters `.parentPage()` and `id()`, permitting any permutation of the provided URL to be created. To permit custom parameter support methods on custom endpoints, a configuration object may be passed to the `registerRoute` method with a `mixins` property defining any functions to add: ```js site.handler = site.registerRoute( 'myplugin/v1', 'collection/(?P<id>)', { mixins: { myParam: function( val ) { return this.param( 'my_param', val ); } } }); ``` This permits a developer to extend an endpoint with arbitrary parameters in the same manner as is done for the automatically-generated built-in route handlers. Auto-discovery of all available routes will be supported in the near future, as will re-utilizing existing mixins (like `.search()`) on custom routes. ---- Thank you to @jasonphillips, @joehoyle, @gnarf, @elyobo, @timmyc Closes #158, closes #144 (superseded by this PR)
0d6634f to
0b4fbe8
Compare
This supersedes both #144 and #158 by providing a method that developers can use to arbitrarily support any endpoint.
Support for Custom Post Types is provided via the
.registerRoutemethod. This method returns a handler function which can be assigned to your site instance as a method, and takes the same namespace and route string arguments asrest_register_route:The string
(?P<id>)indicates that a level of the route for this resource is a dynamic property named ID. By default, properties identified in this fashion will not have any inherent validation. This is designed to give developers the flexibility to pass in anything, with the caveat that only valid IDs will be accepted on the WordPress end.You might notice that in the example from the official WP-API documentation, a pattern is specified with a different format: this is a regular expression designed to validate the values that may be used for this capture group.
Adding the regular expression pattern (as a string) enabled validation for this component. In this case, the
\\d+will cause only numeric values to be accepted.NOTE THE DOUBLE-SLASHES in the route definition here, however:
'/author/(?P<id>\\d+)'This is a JavaScript string, where\must be written as\\to be parsed properly. A single backslash will break the route's validation.Each named group in the route will be converted into a named setter method on the route handler, as in
.id()in the example above: that name is taken from the<id>in the route string.The route string
'pages/(?P<parentPage>[\d]+)/revisions/(?P<id>[\d]+)'would create the setters.parentPage()andid(), permitting any permutation of the provided URL to be created.To permit custom parameter support methods on custom endpoints, a configuration object may be passed to the
registerRoutemethod with amixinsproperty defining any functions to add:This permits a developer to extend an endpoint with arbitrary parameters in the same manner as is done for the automatically-generated built-in route handlers.
Auto-discovery of all available routes will be supported in the near future, as will re-utilizing existing mixins (like
.search()) on custom routes.