Skip to content

Commit 12f9bd2

Browse files
Feat: Add support for custom APIs @W-15585881@ (#402)
* add custom API helper * add unit tests * lint * update docs * update README * move custom api param type from core to commerce-sdk * update unit test * update unit tests and add flag for transforming request body * update property name * update README example with enableTransformBody example * update example in README * revert template changes * update customApiParameters type * consume new commerce-sdk-core
1 parent bece071 commit 12f9bd2

File tree

9 files changed

+572
-10
lines changed

9 files changed

+572
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ only use JavaScript, or if you use TypeScript but only import the client classes
1010
then your usage **will not change**. You will likely only need to make changes if
1111
you import the type definitions directly.
1212

13+
## v2.16.0-dev
14+
15+
#### Enchancements
16+
17+
- Add helper function `customApiHelper.callCustomEndpoint` to call [Custom APIs](https://developer.salesforce.com/docs/commerce/commerce-api/guide/custom-apis.html) - [#402](https://github.com/SalesforceCommerceCloud/commerce-sdk/pull/402)
18+
1319
## v2.15.0
1420

1521
#### API Changes

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,91 @@ const searchResults = await searchClient.productSearch({
229229

230230
Invalid query parameters that are not a part of the API and do not follow the `c_` custom query parameter convention will be filtered from the request and a warning will be displayed.
231231

232+
### Custom APIs
233+
234+
The SDK supports calling [custom APIs](https://developer.salesforce.com/docs/commerce/commerce-api/guide/custom-apis.html) with a helper function, `customApiHelper.callCustomEndpoint()`.
235+
236+
Example usage:
237+
238+
```javascript
239+
import * as CommerceSdk from "commerce-sdk";
240+
const { helpers } = CommerceSdk;
241+
242+
// client configuration parameters
243+
const clientConfigExample = {
244+
parameters: {
245+
clientId: '<your-client-id>',
246+
organizationId: '<your-org-id>',
247+
shortCode: '<your-short-code>',
248+
siteId: '<your-site-id>',
249+
},
250+
// If not provided, it'll use the default production URI:
251+
// 'https://{shortCode}.api.commercecloud.salesforce.com/custom/{apiName}/{apiVersion}'
252+
// path parameters should be wrapped in curly braces like the default production URI
253+
baseUri: '<your-base-uri>'
254+
};
255+
256+
const access_token = '<INSERT_ACCESS_TOKEN_HERE>'
257+
258+
// Required params: apiName, endpointPath, shortCode, organizaitonId
259+
// Required path params can be passed into:
260+
// options.customApiPathParameters or clientConfig.parameters
261+
// customApiPathParameters will take priority for duplicate values
262+
const customApiArgs = {
263+
apiName: 'loyalty-info',
264+
apiVersion: 'v1', // defaults to v1 if not provided
265+
endpointPath: 'customers'
266+
}
267+
268+
const getResponse = await helpers.callCustomEndpoint({
269+
options: {
270+
// http operation is defaulted to 'GET' if not provided
271+
method: 'GET',
272+
parameters: {
273+
queryParameter: 'queryParameter1',
274+
},
275+
headers: {
276+
// Content-Type is defaulted to application/json if not provided
277+
'Content-type': 'application/json',
278+
authorization: `Bearer ${access_token}`
279+
},
280+
customApiPathParameters: customApiArgs,
281+
},
282+
clientConfig: clientConfigExample,
283+
// Flag to retrieve raw response or data from helper function
284+
rawResponse: false
285+
})
286+
287+
const postResponse = await customApiHelper.callCustomEndpoint({
288+
options: {
289+
method: 'POST',
290+
headers: {
291+
authorization: `Bearer ${access_token}`
292+
},
293+
customApiPathParameters: {
294+
apiVersion: 'v1',
295+
endpointPath: 'greeting',
296+
apiName: 'e2e-tests',
297+
},
298+
// When this flag is set to true, the request body will be automatically
299+
// formatted in the expected format set by the 'Content-type' headers
300+
// 'application/json' or 'application/x-www-form-urlencoded'
301+
enableTransformBody: true,
302+
303+
// object can be passed since we have enableTransformBody set to true
304+
body: { data: 'data' }
305+
// if enableTransformBody is not set to true,
306+
// we have to ensure the request body is correctly formatted
307+
// body: JSON.stringify({ data: 'data' })
308+
},
309+
clientConfig: clientConfigExample,
310+
rawResponse: false
311+
})
312+
313+
console.log('get response: ', getResponse)
314+
console.log('post response: ', postResponse)
315+
```
316+
232317
## Caching
233318

234319
The SDK currently supports two types of caches - In-memory and Redis. Both the implementations respect [standard cache headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control). To use another type of cache, write your own implementation of the [CacheManager](https://github.com/SalesforceCommerceCloud/commerce-sdk-core/tree/main/src/base/cacheManager.ts). See the [default cache manager](https://github.com/SalesforceCommerceCloud/commerce-sdk-core/tree/main/src/base/cacheManagerKeyv.ts) to design your implementation.

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
]
4444
},
4545
"dependencies": {
46-
"@commerce-apps/core": "^1.6.1",
46+
"@commerce-apps/core": "^1.7.0",
4747
"nanoid": "^3.3.4",
4848
"retry": "^0.13.1",
4949
"tslib": "^2.4.1"

src/static/helperTemplates/index.ts.hbs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77

8+
import { getShopperToken } from "./shopperCustomer"
9+
import { callCustomEndpoint } from "./customApi";
10+
export const helpers = {
11+
callCustomEndpoint,
12+
getShopperToken
13+
}
814
export * as slasHelpers from "./slas";
9-
export * as helpers from "./shopperCustomer";

src/static/helpers/config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
* Copyright (c) 2022, salesforce.com, inc.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
export const CUSTOM_API_DEFAULT_BASE_URI =
8+
"https://{shortCode}.api.commercecloud.salesforce.com/custom/{apiName}/{apiVersion}";

0 commit comments

Comments
 (0)