Use Strategy Deisgn Pattern to call the right API depending on what type of E-com store the user is connected to (Shopify, Lightspeed, etc.).
The app needs to perform the same actions for e-comm websites depending on which type of E-comm the user is using. For exemple, a user must be able to view his products whether his shop is with Shopify or Lightspeed. Considering the user can be connected to multiple shops, all from different e-comm sources, how can the user's action call the right HTTP request to the right API?
The easy and wrong way to do it is to create an endpoint for each request: GetProductsShopify, GetProductsLightspeed. This doesn't follow clean code principles and doesn't reduce code duplication. We want our code to dynmically decide which type of request is suited for our user.
- Create abstract ShopAPI class
- Create child classes that derive from ShopAPI: ShopifyAPI, LightspeedAPI, etc.
- Add fields and methods. Make sure the childs override the methods with the right HTTP request.
- Spread your routes to make it cleaner later in the server.
/shopifyAPI/getProducts
and/lightspeedAPI/getProducts
Now you can store the different shop types under a Shop array.
Depending on which Shop type is currently selected, it will call the right HTTP request in the overriden method.
Create a routes
folder and add a javascript file for each E-comm API: routes/Shopify.js
, routes/Lightspeed.js
, etc.
const shopifyRoute = require("./routes/Shopify");
const lightspeedRoute = require("./routes/lightspeed");
app.use('/shopify', shopifyRoute);
app.use('/lightspeed', lightspeedRoute);