SalesforceCommerceCloud / commerce-sdk-isomorphic

Browser & Node.js JavaScript client for B2C Commerce API
https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/
BSD 3-Clause "New" or "Revised" License
43 stars 21 forks source link

Feat: Add support for custom query parameters @W-13974911@ #139

Closed joeluong-sfcc closed 9 months ago

joeluong-sfcc commented 10 months ago

This PR adds support for custom query parameters as SCAPI now supports SCAPI hooks: https://developer.salesforce.com/docs/commerce/commerce-api/references/about-commerce-api/about.html#08302023

Query params that begin with c_ are considered custom query parameters and are passed onto the underlying SCAPI call. Any other query params that aren't a part of the SCAPI endpoint spec will be filtered out and not passed.

This PR also adds node 18 & 20 to the CI

Testing this PR takes a bit of set up:

  1. git clone git@github.com:SalesforceCommerceCloud/commerce-sdk-isomorphic.git

  2. git checkout ju/support-custom-params

  3. yarn install

  4. modify templates/operations.ts.hbs to add a console.log() to check query params:

    // operations.ts.hbs
    ...
      console.log('URL', url.toString())
      const response = await fetch(url.toString(), requestOptions);
      if (rawResponse) {
        return response;
    ...
  5. yarn run renderTemplates && yarn run build:lib

  6. Create a new node project in a separate directory:

    mkdir testCustomParams
    cd testCustomParams
    npm init -y
    touch index.js
  7. modify package.json in testCustomParams directory to point commerce-sdk-isomorphic to local directory in dependencies section and add "type": "module":

  "type": "module",
  "dependencies": {
    "commerce-sdk-isomorphic": "<insert_path_to_local_commerce-sdk-isomorphic>"
  }
  1. npm install
  2. Add test code to index.js and replace client creds
import pkg from 'commerce-sdk-isomorphic';
const { helpers, ShopperLogin, ShopperProducts } = pkg;

// demo client credentials, if you have access to your own please replace them below.
const CLIENT_ID = "<CLIENT_ID>";
const ORG_ID = "<ORG_ID>";
const SHORT_CODE = "<SHORT_CODE>";
const SITE_ID = "<SITE_ID>";

// must be registered in SLAS. On server, redirectURI is never called
const redirectURI = "http://localhost:3000/callback";

// client configuration parameters
const clientConfig = {
  parameters: {
    clientId: CLIENT_ID,
    organizationId: ORG_ID,
    shortCode: SHORT_CODE,
    siteId: SITE_ID,
  },
};

const slasClient = new ShopperLogin(clientConfig);

// GUEST LOGIN
const guestTokenResponse = await helpers
  .loginGuestUser(slasClient, { redirectURI })
  .then((guestTokenResponse) => {
    console.log("Guest Token Response: ", guestTokenResponse);
    return guestTokenResponse;
  })
  .catch((error) =>
    console.log("Error fetching token for guest login: ", error)
  );

const shopperProducts = new ShopperProducts({
    ...clientConfig,
    headers: {authorization: `Bearer ${guestTokenResponse.access_token}`}
});

const productsResult = await shopperProducts.getProducts({
    parameters: {
        ids: "25720044M,25686395M", 
        expand: ["promotions", "images", "prices"],
        c_customParamOne: '1',
        c_customParamTwo: 2,
        c_customParamThree: 3,
        invalidParam: 'hello',
    }
})

console.log("productsResult: ", productsResult)
  1. node index.js > out.txt

  2. Observe out.txt and that the custom query params are included in the fetch call and invalid query params are excluded from the call:

    URL https://<short_code>.api.commercecloud.salesforce.com/product/shopper-products/v1/organizations/<org_id>/products?ids=25720044M%2C25686395M&expand=promotions%2Cimages%2Cprices&siteId=RefArch&c_customParamOne=1&c_customParamTwo=2&c_customParamThree=3