-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Framework: Use a single way to call APIs: wp.apiRequest #5253
Conversation
blocks/library/embed/index.js
Outdated
setAttributes( { type, providerNameSlug } ); | ||
} else { | ||
this.setState( { error: true } ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This breaks the embed blocks here.
With fetch
, we get the error response from oembed back here as json, so this is where we deal with errors. With wp.apiRequest
, we don't. So if anyone puts in a URL that we can't embed, we never get out of the fetching
state and report the error to the user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch @notnownikki It should be fixed now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 You beat me to the fix :) works as expected now!
This is beautiful. I had no idea it was considered reasonable to not use |
I like how you unified the way all API calls are handled. In particular, I love the fact that there is one way to trigger all calls. However, I'm not sold on the idea of leaving the details on how paths are created for the endpoints. This should be an implementation detail in my opinion. I would prefer having: this.suggestionsRequest = wp.api.makeRequest( 'posts', {
search: value,
perPage: 20,
orderBy: 'relevance',
} );
const makeRequest = ( endpointName, options ) => {
const endpointMappers = applyFilters( 'api/endpointMappers', {
posts: ( { search, perPage, orderBy } ) = ( {
path: `/wp/v2/posts?${ stringify( {
search,
per_page: perPage,
orderby: orderBy,
} ) }`,
},
} );
// I assumed that the base path is prepended if not provided
wp.apiRequest( endpointMappers[ endpointName ]( options ) );
}; over: this.suggestionsRequest = wp.apiRequest( {
path: `/wp/v2/posts?${ stringify( {
search: value,
per_page: 20,
orderby: 'relevance',
} ) }`,
} ); It would be definitely more work because you would have to define a separate mapping function for every single API endpoint. However, we could expose this configuration and make it extensible. This would allow overriding those mapping functions and give more control over how all those requests are handled. I might be wrong in here because I'm not familiar with the code updated in this PR. So treat it as my own opinion. In fact, this PR is perfectly fine to be merged as it is once it is properly tested with someone who knows all the places we should look at. |
@gziolo Actually, I'm totally on board with your idea, in fact, I think that should be the next step. This is more "in-between" approach unifying API calls without rewriting everything. In your proposal, we'll have to specify all the models properly (independently from the WP models):
This means a huge impact on the code base of Gutenberg because we're abstracting the backend entirely, which is good because it means offline support, WP agnostic but requires way more work. The unifying using An adventurous implementation can still achieve the exact same thing by mapping the WP paths instead. |
I'd get behind that too. I'm trying to fix a situation where I need some abstraction between the API call and what the block does with it, so, happy to help out if we go this direction. |
Just wanted to repeat that I'm more than happy to see it merged as it is :D |
I wouldn't describe it better in terms of overriding paths based on regular expressions. I agree it's totally doable 👍 |
aaf075e
to
07c449b
Compare
@youknowriad can |
This PR refactors API calls in Gutenberg to always use
wp.apiRequest
.In addition to consistency, this makes it easier for third-party Gutenberg USAGE to use it without a WordPress backend and just provide an alternative implementation to
wp.apiRequest
Testing instructions