SalesforceCommerceCloud / commerce-sdk

https://salesforcecommercecloud.github.io/commerce-sdk/
BSD 3-Clause "New" or "Revised" License
149 stars 48 forks source link

Feat: Add support for custom query params @W-14391914@ #396

Closed joeluong-sfcc closed 10 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.

This PR also:

To test this PR, you can build a local version of the commerce-sdk and use that as a package for manual testing:

  1. git clone -b feat/support-custom-params git@github.com:SalesforceCommerceCloud/commerce-sdk.git
  2. cd commerce-sdk
  3. npm install --legacy-peer-deps
  4. Add a console.log to templates/operations.ts.hbs to see query params in console output when testing
    ...
      console.log('queryParams: ', queryParams)
      return StaticClient.{{method}}({
        client: this,
        rawResponse: rawResponse,
    ...
  5. npm run build
  6. Create new testing directory:
    mkdir testCustomParams
    cd testCustomParams
    npm init -y
    touch index.js
  7. modify package.json in testCustomParams directory to point commerce-sdk to local directory in dependencies section and add "type": "module"
    "type": "module",
    "dependencies": {
    "commerce-sdk": "<insert_path_to_local_commerce-sdk>"
    }
  8. npm install
  9. Add test code to index.js and replace client creds
    
    import * as CommerceSdk from "commerce-sdk";
    const { slasHelpers, Customer, Product } = CommerceSdk;

// demo client credentials, if you have access to your own please replace them below. const CLIENT_ID = ""; const ORG_ID = ""; const SHORT_CODE = ""; const 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 Customer.ShopperLogin(clientConfig);

// GUEST LOGIN const guestTokenResponse = await slasHelpers .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 Product.ShopperProducts({ ...clientConfig, headers: {authorization: Bearer ${guestTokenResponse.access_token}} });

const productsResult = await shopperProducts.getProducts({ parameters: { ids: "25720044M,25686395M", expand: ["promotions", "images", "prices"], invalid_query: 'invalidQueryParameter', c_customQuery: 'validQueryParameter' } })

console.log("productsResult: ", productsResult)


10. `node index.js > out.txt`
11. Observe out.txt and that the custom query params are included and invalid query params are excluded from the call
alexvuong commented 10 months ago

Manual test as instructed in the PR. Confirming the code works as expected. I left a few non-blocking comments @joeluong-sfcc