Make WordPress Core

Opened 3 months ago

Last modified 4 weeks ago

#39683 reopened defect (bug)

REST API JS Client: Should enable connecting to multiple endpoints at the same time

Reported by: adamsilverstein Owned by: adamsilverstein
Milestone: 4.8 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch has-unit-tests commit
Focuses: javascript, rest-api Cc:


Originally from @lucasstark: https://github.com/WP-API/client-js/issues/145

If you need to talk to more than one endpoint the way the api is initialized does not make this straight forward.
It seems like it should be possible, since wp.api.endpoints is a collection of each unique endpoint you might initialize, however, the collections and models on each endpoint will be overwritten each time you call wp.api.init, so obviously we need a solid way to initialize more than one endpoint at once.
An example use case would be in a multisite environment you might want to talk to two of the subsites at the same time.
You'd think you could do something like this rough example:

    var sites = [];
        'apiRoot': 'http://example.com/subsite1/wp-json/'
        sites[ 1 ] = endpoint;

        'apiRoot': 'http://example.com/subsite2/wp-json/'
        sites[ 2 ] = endpoint;

But now sites[1].collections is configured with the URL for the second site.

Attachments (2)

39683.diff (3.6 KB) - added by adamsilverstein 3 months ago.
39683.2.diff (5.6 KB) - added by adamsilverstein 6 weeks ago.

Download all attachments as: .zip

Change History (12)

#1 @adamsilverstein
3 months ago

  • Keywords has-patch needs-unit-tests added
  • Milestone changed from Awaiting Review to 4.8

Correct a name collision issue that prevented the client from properly connecting to multiple APIs as it was designed to do.

  • Don't create default model for wp.api.endpoints - this should contain only initialized endpoints
  • Add some expected defaults for versionString and apiRoot
  • Add some missing quotes around properties when searching with findWhere as per coding standards
  • Fix bug: Clean up naming conventions so inner resolvedEndpoint doesn't overwrite outer endpoint
  • Fix: deferred.resolve call - only pass the resolvedEndpoint, not the wp.api global
  • Fix: url property should be a function that dynamically returns the current model route

I'd like to add a QUnit test here to demonstrate/verify the fix by connecting to two known remote apis.

#2 follow-up: @lucasstark
3 months ago

@adamsilverstein Wanted to let you know that this seems to resolve the problems.

#3 in reply to: ↑ 2 @adamsilverstein
3 months ago

Replying to lucasstark:

@adamsilverstein Wanted to let you know that this seems to resolve the problems.

Great! Thank you for testing.

This ticket was mentioned in Slack in #core-restapi by adamsilverstein. View the logs.

3 months ago

#5 @adamsilverstein
6 weeks ago

  • Owner set to adamsilverstein
  • Status changed from new to assigned

#6 @adamsilverstein
6 weeks ago

39683.2.diff adds a unit test to verify multiple endpoints work correctly

#7 @adamsilverstein
4 weeks ago

  • Keywords has-unit-tests commit added; needs-unit-tests removed

#8 @adamsilverstein
4 weeks ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 40364:

REST API: JS Client - Enable connecting to multiple endpoints.

Enable connecting to multiple wp-api endpoints. Calling wp.api.init with a new apiRoot will parse the new endpoint's schema and store a new set of models and collections. A collection of connected endpoints is stored in wp.api.endpoints.

Props lucasstark.
Fixes #39683.

#9 @jnylen0
4 weeks ago

@adamsilverstein should we get this in 4.7.4 as well?

#10 @adamsilverstein
4 weeks ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

@jnylen0 that would make sense if there is still time. reopening for consideration, cc: @swissspidy

Note: See TracTickets for help on using tickets.