Skip to content
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

withAPIData does not work with custom endpoints #3209

Closed
2 tasks
donnapep opened this issue Oct 27, 2017 · 4 comments
Closed
2 tasks

withAPIData does not work with custom endpoints #3209

donnapep opened this issue Oct 27, 2017 · 4 comments
Labels
Core REST API Task Task for Core REST API efforts

Comments

@donnapep
Copy link
Contributor

donnapep commented Oct 27, 2017

Issue Overview

withAPIData does not work with custom endpoints.

Steps to Reproduce (for bugs)

  1. Install and activate Sensei (you’ll need the update/block-support​ branch)
  2. Download Sensei Courses (you'll need the update/use-custom-endpoint branch). cd into the directory and run npm install and npm run build. Upload the plugin to your site and activate it.
  3. Create a course by going to Courses > Add New. Give your course a title and description.
  4. Create a new page.
  5. Add the Courses block.
  6. Watch the Network tab in Chrome DevTools. Notice that there is no request being sent to /sensei/v1/courses, although there is a request to /wp/v2/course-category.

Chrome 61.0.3163.100 on MacOS 10.12.6 and WordPress 4.9-beta4-42028, Gutenberg 1.5.2.

Expected Behavior

I expect a request to be made to the custom endpoint and results to be returned.

Current Behavior

No API data is returned. In fact, the request is not even sent.

Possible Solution

When using a path set to /sensei/v1/courses, the getRoute function returns false, which in turn causes the applyMapping function to return early.

In other words, it seems like there is currently a requirement for a custom endpoint to start with /wp/v2.

Todos

  • Tests
  • Documentation
@mtias mtias added the Core REST API Task Task for Core REST API efforts label Oct 31, 2017
@ockham
Copy link
Contributor

ockham commented Nov 9, 2017

When using a path set to /sensei/v1/courses, the getRoute function returns false, which in turn causes the applyMapping function to return early.

In other words, it seems like there is currently a requirement for a custom endpoint to start with /wp/v2.

This seems to come from the window.wpApiSettings global (thru

this.schema = context.getAPISchema();
and
...wpApiSettings,
among others), which includes a schema attr that looks like this for me:

image

This really doesn't include any /sensei routes.

/cc @aduth

@youknowriad
Copy link
Contributor

I guess that's probably the reason we need the init call for our custom namespace here https://github.com/WordPress/gutenberg/pull/3377/files#diff-620130744b6b73b822a3df609671d930R776

So that might be the way to fix this for custom endpoints.

@aduth
Copy link
Member

aduth commented Nov 9, 2017

Hmm, ideally we'd not have to initialize each namespace separately and could instead load in a single schema for all endpoints of the REST API.

Might be that we drop /wp/v2 to / here? Untested theory:

// Localize the wp-api settings and schema.
$schema_response = rest_do_request( new WP_REST_Request( 'GET', '/wp/v2' ) );
if ( ! $schema_response->is_error() ) {
wp_add_inline_script( 'wp-api', sprintf(
'wpApiSettings.cacheSchema = true; wpApiSettings.schema = %s;',
wp_json_encode( $schema_response->get_data() )
), 'before' );
}

@greatislander
Copy link
Contributor

Related: #3462

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core REST API Task Task for Core REST API efforts
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants