Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Migrate REST API usage to the Store API #2282

Merged
merged 18 commits into from
Apr 24, 2020
Merged

Conversation

mikejolley
Copy link
Member

Upon reviewing the REST API overrides in blocks, to see what needed to be ported to the REST API itself, I found that most endpoints simply needed to expand access capabilities to authors/non-admin users.

As well as expanding access rights, some of the endpoints were then needing to shape data to hide private things, or return data in a specific opinionated format. This didn't seem like a good solution to me, so I worked through each endpoint to see if the Store API could be used instead. Store API is public, so there should be no auth issues in doing this.

I had success with the existing Store endpoints (products, attributes) so decided to port over some of the other endpoints too, creating new Store API endpoints for tags, categories, reviews all following similar practices and only allowing READ access to public data.

This PR contains that work :)

  • Introduces endpoints for tags, categories, reviews
  • Expands some endpoints with missing data and params
  • Removes all REST API overrides
  • Refactors some HOCS which used Rest API
  • Switches all APIFetches to Store API

How to test the changes in this Pull Request:

  1. Test a variety of blocks including reviews by x, featured product, products by attribute/tag/category.
  2. Confirm all products block works
  3. Double check cart/checkout is still functional. I didn't touch too much there but did move some code around.

@mikejolley mikejolley requested a review from a team as a code owner April 23, 2020 15:39
@mikejolley mikejolley requested review from senadir and removed request for a team April 23, 2020 15:39
@senadir
Copy link
Member

senadir commented Apr 23, 2020

Conflict on initial commit 😆

@mikejolley
Copy link
Member Author

@senadir Not marked for review yet, fixing tests.

@mikejolley mikejolley force-pushed the update/rest-api-overrides branch from 1d6d251 to be3eadd Compare April 23, 2020 16:19
@mikejolley mikejolley self-assigned this Apr 23, 2020
@mikejolley
Copy link
Member Author

@senadir I'm going to do some more testing tomorrow and I had to comment out the tests for with-attributes - I redid the HOC with hooks and it isn't compatible with existing tests. Marking for review though.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 23, 2020

Size Change: -12.7 kB (0%)

Total Size: 1.71 MB

Filename Size Change
build/active-filters-frontend.js 7.51 kB -25 B (0%)
build/active-filters.js 8.28 kB -32 B (0%)
build/all-products-frontend.js 17.8 kB -26 B (0%)
build/all-products.js 17.9 kB -51 B (0%)
build/all-reviews-legacy.js 10.8 kB +7 B (0%)
build/all-reviews.js 11 kB -14 B (0%)
build/attribute-filter-frontend.js 17 kB -25 B (0%)
build/attribute-filter.js 11.8 kB -54 B (0%)
build/blocks-legacy.js 2.92 kB -1 B
build/blocks.js 2.92 kB -1 B
build/cart-frontend.js 112 kB -20 B (0%)
build/cart.js 32.2 kB -61 B (0%)
build/checkout-frontend.js 116 kB -34 B (0%)
build/checkout.js 34.8 kB -71 B (0%)
build/editor-rtl.css 13.6 kB +57 B (0%)
build/editor.css 13.6 kB +58 B (0%)
build/featured-category-legacy.js 146 kB +104 B (0%)
build/featured-category.js 146 kB -67 B (0%)
build/featured-product-legacy.js 9.41 kB -59 B (0%)
build/featured-product.js 9.71 kB -65 B (0%)
build/handpicked-products-legacy.js 7.26 kB -52 B (0%)
build/handpicked-products.js 7.55 kB -40 B (0%)
build/price-filter-frontend.js 14.1 kB -36 B (0%)
build/price-filter.js 10.3 kB -44 B (0%)
build/product-best-sellers-legacy.js 7.35 kB -62 B (0%)
build/product-best-sellers.js 7.62 kB -67 B (0%)
build/product-categories-legacy.js 3.2 kB +4 B (0%)
build/product-categories.js 3.19 kB +2 B (0%)
build/product-category-legacy.js 8.26 kB -66 B (0%)
build/product-category.js 8.53 kB -64 B (0%)
build/product-new-legacy.js 7.51 kB -64 B (0%)
build/product-new.js 7.78 kB -72 B (0%)
build/product-on-sale-legacy.js 7.86 kB -65 B (0%)
build/product-on-sale.js 8.19 kB -62 B (0%)
build/product-search-legacy.js 3.6 kB -43 B (1%)
build/product-search.js 3.83 kB -50 B (1%)
build/product-tag-legacy.js 6.44 kB -45 B (0%)
build/product-tag.js 6.7 kB -56 B (0%)
build/product-top-rated-legacy.js 7.49 kB -62 B (0%)
build/product-top-rated.js 7.75 kB -68 B (0%)
build/products-by-attribute-legacy.js 8.21 kB -269 B (3%)
build/products-by-attribute.js 8.47 kB -277 B (3%)
build/reviews-by-category-legacy.js 12.8 kB +71 B (0%)
build/reviews-by-category.js 13 kB -50 B (0%)
build/reviews-by-product-legacy.js 14.2 kB -10 B (0%)
build/reviews-by-product.js 14.5 kB -40 B (0%)
build/reviews-frontend-legacy.js 8.41 kB +3 B (0%)
build/reviews-frontend.js 9.1 kB +8 B (0%)
build/style-legacy-rtl.css 5.37 kB -6 B (0%)
build/style-legacy.css 5.38 kB -7 B (0%)
build/style-rtl.css 15.4 kB +363 B (2%)
build/style.css 15.4 kB +356 B (2%)
build/vendors-legacy.js 280 kB +11 B (0%)
build/vendors.js 366 kB -6 B (0%)
build/wc-payment-method-cheque.js 0 B -783 B (0%)
build/wc-payment-method-stripe.js 0 B -10.7 kB (0%)
ℹ️ View Unchanged
Filename Size Change
build/block-error-boundary-legacy.js 774 B 0 B
build/block-error-boundary.js 774 B 0 B
build/custom-select-control-style-legacy.js 782 B 0 B
build/custom-select-control-style.js 783 B 0 B
build/editor-legacy-rtl.css 12.6 kB 0 B
build/editor-legacy.css 12.6 kB 0 B
build/panel-style-legacy.js 774 B 0 B
build/panel-style.js 773 B 0 B
build/product-list-style-legacy.js 776 B 0 B
build/snackbar-notice-style-legacy.js 778 B 0 B
build/snackbar-notice-style.js 779 B 0 B
build/spinner-style-legacy.js 772 B 0 B
build/spinner-style.js 772 B 0 B
build/vendors-style-legacy-rtl.css 1.65 kB 0 B
build/vendors-style-legacy.css 1.65 kB 0 B
build/vendors-style-legacy.js 105 B 0 B
build/vendors-style-rtl.css 1.65 kB 0 B
build/vendors-style.css 1.65 kB 0 B
build/vendors-style.js 105 B 0 B
build/wc-blocks-data.js 7.01 kB 0 B
build/wc-blocks-registry.js 1.58 kB 0 B
build/wc-settings.js 2.14 kB 0 B

compressed-size-action

Copy link
Member

@senadir senadir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, this was a huge refactor, wonderful job Mike!
I spotted a small regression that I think is easy to fix, once it's fixed feel free to merge.

@@ -24,23 +20,21 @@ const getProductsRequests = ( {
queryArgs = [],
} ) => {
const defaultArgs = {
per_page: IS_LARGE_CATALOG ? 100 : -1,
per_page: IS_LARGE_CATALOG ? 100 : 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn’t 0 return no product?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. I noticed some functions treat 0 as "unlimited" and adopted that instead of -1 which also makes validation easier.

/**
* ProductCategories class.
*/
class ProductCategories extends AbstractTermsRoute {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is not implementing the count property right.

in Master:
Screen Shot 2020-04-24 at 12 53 14 PM

In this branch:
Screen Shot 2020-04-24 at 12 32 33 PM

@@ -31,17 +31,36 @@ public function prepare_objects_query( $request ) {
'paged' => $request['page'],
'post__in' => $request['include'],
'post__not_in' => $request['exclude'],
'posts_per_page' => $request['per_page'],
'posts_per_page' => $request['per_page'] ? $request['per_page'] : -1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that if per_page is set to 0, this will evaluate to -1, but why does that? wouldn't make sense to let the client use the same logic as WP?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's easier to check ! per_page then set to -1 than handle 0 and -1 separately. The schema validates from 0 and has a message than leaving blank, or using 0, maps to unlimited.

Some functions in WP use 0 instead of -1 - I think the terms query object.

@mikejolley
Copy link
Member Author

Good catch @senadir I am using the count from WP, but some counts enhance to include children terms. Will revise.

@mikejolley mikejolley merged commit 5a195cf into master Apr 24, 2020
@mikejolley mikejolley deleted the update/rest-api-overrides branch April 24, 2020 13:36
@nerrad nerrad mentioned this pull request May 25, 2020
17 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants