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 APIs @W-15585881@ #402

Closed joeluong-sfcc closed 6 months ago

joeluong-sfcc commented 6 months ago

This PR adds support for custom APIs by exposing a helper function called callCustomEndpoint that allows users to call their custom API following common SDK patterns.

There is a companion PR in the commerce-sdk-core package that will need to be released first then consumed by this PR in order to pass the CI checks. The companion PR: https://github.com/SalesforceCommerceCloud/commerce-sdk-core/pull/110

Some features of the callCustomEndpoint helper:

  1. Defaults apiVersion to v1 if not provided
  2. Defaults content-type header to application/json if not provided
  3. options.body can be passed as an object and will be automatically formatted to match Content-Type through commerce-sdk-core

Below is a sample script I used for testing the callCustomEndpoint helper function. Reach out to me for instructions on how to run this with the proper credentials:


import * as CommerceSdk from "commerce-sdk";
const { helpers } = CommerceSdk;

// client configuration parameters
const clientConfig = {
  parameters: {
    clientId: '<your_client_id>',
    siteId: '<your_site_id>',
    organizationId: '<your_org_id>',
    shortCode: '<your_short_code>',
  },
  // baseUri: 'https://{shortCode}.alternativeBaseUri.com/custom/{apiName}/{apiVersion}'
};

const access_token = '<insert_access_token_here>'

let response = await helpers.callCustomEndpoint({ options: {
    method: 'PUT',
    headers: {
        'Content-type': 'application/json',
        authorization: `Bearer ${access_token}`
    },
    customApiPathParameters: {
      apiVersion: 'v1',
      endpointPath: 'greeting',
      apiName: 'e2e-tests',
    },
    enableTransformBody: true,
    body: { message: 'testing' }
}, clientConfig, rawResponse: false})

console.log('RESPONSE: ', response)