bigcommerce / storefront-data-hooks

Hooks for React Storefront UI Components
MIT License
166 stars 36 forks source link

API Handler Operations are unuseable #64

Closed brendan-hall closed 3 years ago

brendan-hall commented 3 years ago

In the function returned by createApiHandler any operations passed in are overridden by the original handlers defined for that api route. This makes it impossible to override behaviour cleanly.


Example:

Set up the following for /api/bigcommerce/cart

import cart from "@bigcommerce/storefront-data-hooks/api/cart/index";

export default cart({
  operations: {
    getCart: () => {
      console.log("This code is never run");
      throw new Error();
    },
  },
});

Expected Outcome: Any requests for current cart state (GET /api/bigcommerce/cart) return a 500 with no data, or other failure state. Actual Outcome: Requests continue to succeed as though no changes have been made.


I believe the solution is to swap the merging order here: https://github.com/bigcommerce/storefront-data-hooks/blob/136a7e3e10502296cd8d90ff419fcc3b020eb5af/src/api/utils/create-api-handler.ts#L51

jorgemasta commented 3 years ago

Hey @brendan-hall, thanks for creating the ticket and sorry for the delay. Do you use this package with NextJS or with another framework?

We plan to make this package more agnostic in the near future, making it more extensible.

Any particular recommendation?

jorgemasta commented 3 years ago

Solved in #67