Skip to content

Commit 3853532

Browse files
Simplify README (#163)
* Update README.md * Update README.md Simplify custom API example --------- Co-authored-by: Joel Uong <[email protected]>
1 parent 0dcac57 commit 3853532

File tree

1 file changed

+51
-116
lines changed

1 file changed

+51
-116
lines changed

README.md

+51-116
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# commerce-sdk-isomorphic
22

3-
The Salesforce Commerce SDK (Isomorphic) allows easy interaction with the B2C Commerce platform’s Shopper APIs on the Node.js runtime and works both in browsers and Node applications. For a Node-based SDK that can access the Admin APIs in addition to the Shopper APIs, see the main [Commerce SDK](https://github.com/SalesforceCommerceCloud/commerce-sdk).
3+
This SDK provides a Browser & Node.js JavaScript client for calling [B2C Commerce Shopper APIs](https://developer.salesforce.com/docs/commerce/commerce-api/overview).
4+
5+
_For a Node.js only SDK that can also access Admin APIs checkout [Commerce SDK](https://github.com/SalesforceCommerceCloud/commerce-sdk)._
46

57
## Getting Started
68

79
### Requirements
810

911
- Node `^12.x`, `^14.x`, `^16.x`, `^18.x`
10-
12+
- The SDK requires B2C Commerce API (SCAPI) to be configured. For more info see [Getting started with SCAPI](https://developer.salesforce.com/docs/commerce/commerce-api/guide/get-started.html).
1113

1214
### Installation
1315

@@ -17,55 +19,25 @@ npm install commerce-sdk-isomorphic
1719

1820
### Usage
1921

20-
> **Note:** These are required parameters.
21-
22-
| Parameter | Description |
23-
| -------------- | :------------------------------------------------------------------------- |
24-
| clientId | ID of the client account created with Salesforce Commerce. |
25-
| organizationId | The unique identifier for your Salesforce identity. |
26-
| shortCode | Region-specific merchant ID. |
27-
| siteId | Name of the site to access data from, for example, RefArch or SiteGenesis. |
28-
29-
30-
### Configure the Isomorphic SDK
31-
32-
3322
```javascript
34-
/**
35-
* Configure required parameters
36-
*
37-
* To learn more about the parameters please refer to https://developer.salesforce.com/docs/commerce/commerce-api/guide/get-started.html
38-
*/
3923
import {helpers, ShopperLogin, ShopperSearch} from 'commerce-sdk-isomorphic';
4024

41-
// Create a configuration to use when creating API clients
4225
const config = {
43-
proxy: 'https://localhost:3000', // Routes API calls through a proxy when set
44-
headers: {},
26+
// SCAPI does not support CORS, so client side requests must use a reverse proxy.
27+
proxy: 'https://localhost:3000',
4528
parameters: {
4629
clientId: '<your-client-id>',
4730
organizationId: '<your-org-id>',
4831
shortCode: '<your-short-code>',
4932
siteId: '<your-site-id>',
5033
},
51-
throwOnBadResponse: true,
5234
};
5335

54-
const shopperLogin = new ShopperLogin(config);
55-
// Execute Public Client OAuth with PKCE to acquire guest tokens
56-
const {access_token, refresh_token} = await helpers.loginGuestUser(
57-
shopperLogin,
58-
{redirectURI: `${config.proxy}/callback`} // Callback URL must be configured in SLAS Admin
36+
const {access_token} = await helpers.loginGuestUser(
37+
new ShopperLogin(config),
38+
{redirectURI: `${config.proxy}/callback`}
5939
);
6040

61-
// Execute Private Client OAuth with PKCE to acquire guest tokens
62-
// ***WARNING*** Be cautious about using this function in the browser as you may end up exposing your client secret
63-
// only use it when you know your slas client secret is secured
64-
// const {access_token, refresh_token} = await helpers.loginGuestUserPrivate(
65-
// shopperLogin,
66-
// {}, {clientSecret: '<your-slas-client-secret>'}
67-
// );
68-
6941
const shopperSearch = new ShopperSearch({
7042
...config,
7143
headers: {authorization: `Bearer ${access_token}`},
@@ -76,59 +48,33 @@ const searchResult = await shopperSearch.productSearch({
7648
});
7749
```
7850

79-
#### CORS
80-
81-
The Salesforce Commerce API (SCAPI) does not support CORS, so a proxy must be used to be able to use the SDK.
51+
#### Fetch Options
8252

83-
### Advanced Options
84-
85-
Commerce SDK Isomorphic supports Fetch API options for [node-fetch](https://github.com/node-fetch/node-fetch/1#api) on server and [whatwg-fetch](https://github.github.io/fetch/) on browser with a simple configuration.
86-
This sample code shows how to configure HTTP timeout and agent options.
53+
You can configure how the SDK makes requests using the `fetchOptions` parameter. It is passed to [node-fetch](https://github.com/node-fetch/node-fetch/1#api) on the server and [whatwg-fetch](https://github.github.io/fetch/) on browser.
8754

8855
```javascript
89-
/**
90-
* Configure advanced timeout and agent parameters
91-
*
92-
* To learn more about the parameters please refer to the [Salesforce Developer Center](https://developer.salesforce.com/docs/commerce/commerce-api).
93-
*/
94-
// Create a configuration to use when creating API clients
95-
const https = require('https');
56+
const https = require("https");
9657

9758
const config = {
98-
proxy: 'https://localhost:3000',
99-
headers: {},
100-
parameters: {
101-
clientId: '<your-client-id>',
102-
organizationId: '<your-org-id>',
103-
shortCode: '<your-short-code>',
104-
siteId: '<your-site-id>',
105-
},
10659
fetchOptions: {
107-
timeout: 2000, //request times out after 2 seconds
108-
agent: new https.agent({
109-
// a custom http agent
110-
keepAlive: true,
111-
}),
60+
// By default, requests made using the SDK do not include cookies.
61+
credentials: "include",
62+
timeout: 2000,
63+
agent: new https.agent({ keepAlive: true }),
11264
},
11365
};
11466
```
11567

116-
### Additional Config Settings
117-
118-
_headers:_ A collection of key/value string pairs representing additional headers to include with API requests.
68+
For more info, refer to the [documentation site](https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/).
11969

120-
_throwOnBadResponse:_ Default value is false. When set to true, the SDK throws an Error on responses with statuses that are not 2xx or 304.
70+
#### Additional Config Settings
12171

122-
### Public/Private Client Shopper Login (SLAS) helpers
123-
124-
A collection of helper functions are available in this SDK to simplify [Public/Private Client Shopper Login OAuth flows](https://developer.salesforce.com/docs/commerce/commerce-api/references?meta=shopper-login:Summary). See sample code above for guest login.
125-
126-
**⚠️ WARNING ⚠️**
127-
Users should be extremely cautious about using the SLAS private client helper functions in the browser as it can expose your client secret. Ensure that your client secret is secured before running the function client side.
72+
* `headers`: Headers to include with API requests.
73+
* `throwOnBadResponse`: When `true`, the SDK throws an `Error` on responses whose status is not 2xx or 304.
12874

12975
### Custom Query Parameters
13076

131-
With the introduction of [hooks for Commerce APIs](https://developer.salesforce.com/docs/commerce/commerce-api/guide/extensibility_via_hooks.html), customers can pass custom query parameters through the SDK to be used in their custom hook. Custom query parameters must begin with `c_`:
77+
You can pass custom query parameters through the SDK to be used in [B2C Commerce API Hooks](https://developer.salesforce.com/docs/commerce/commerce-api/guide/extensibility_via_hooks.html). Custom query parameters must begin with `c_`:
13278

13379
```javascript
13480
const searchResult = await shopperSearch.productSearch({
@@ -139,19 +85,17 @@ const searchResult = await shopperSearch.productSearch({
13985
});
14086
```
14187

142-
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.
88+
Invalid query parameters that are not a part of the API and do not follow the `c_` custom query parameter convention are filtered from the request with a warning.
14389

14490
### Custom APIs
14591

146-
The SDK supports calling [custom APIs](https://developer.salesforce.com/docs/commerce/commerce-api/guide/custom-apis.html) with a helper function, `callCustomEndpoint`.
147-
148-
Example usage:
92+
The SDK supports calling [B2C Commerce Custom APIs](https://developer.salesforce.com/docs/commerce/commerce-api/guide/custom-apis.html) with a helper function, `callCustomEndpoint`:
14993

15094
```javascript
151-
import pkg from 'commerce-sdk-isomorphic';
95+
import pkg from "commerce-sdk-isomorphic";
15296
const { helpers } = pkg;
15397

154-
const clientConfigExample = {
98+
const clientConfig = {
15599
parameters: {
156100
clientId: "<your-client-id>",
157101
organizationId: "<your-org-id>",
@@ -161,63 +105,54 @@ const clientConfigExample = {
161105
// If not provided, it'll use the default production URI:
162106
// 'https://{shortCode}.api.commercecloud.salesforce.com/custom/{apiName}/{apiVersion}'
163107
// path parameters should be wrapped in curly braces like the default production URI
164-
baseUri: "<your-base-uri>"
108+
baseUri: "<your-base-uri>",
165109
};
166110

167111
// Required params: apiName, endpointPath, shortCode, organizaitonId
168112
// Required path params can be passed into:
169113
// options.customApiPathParameters or clientConfig.parameters
170-
const customApiArgs = {
171-
apiName: 'loyalty-info',
172-
apiVersion: 'v1', // defaults to v1 if not provided
173-
endpointPath: 'customers'
174-
}
114+
const customApiPathParameters = {
115+
apiName: "loyalty-info",
116+
apiVersion: "v1", // defaults to v1 if not provided
117+
endpointPath: "customers",
118+
};
175119

176-
const accessToken = '<INSERT ACCESS TOKEN HERE>';
120+
const accessToken = "<INSERT ACCESS TOKEN HERE>";
177121

178-
let getResponse = await helpers.callCustomEndpoint({
122+
await helpers.callCustomEndpoint({
179123
options: {
180-
method: 'GET',
124+
method: "GET",
181125
parameters: {
182-
queryParameter: 'queryParameter1',
126+
queryParameter: "queryParameter1",
183127
},
184128
headers: {
185129
// Content-Type is defaulted to application/json if not provided
186-
'Content-Type': 'application/json',
187-
authorization: `Bearer ${access_token}`
130+
"Content-Type": "application/json",
131+
authorization: `Bearer ${accessToken}`,
188132
},
189-
customApiPathParameters: customApiArgs
190-
},
191-
clientConfig: clientConfigExample,
192-
// Flag to retrieve raw response or data from helper function
193-
rawResponse: false,
194-
})
195-
196-
let postResponse = await helpers.callCustomEndpoint({
133+
customApiPathParameters,
134+
},
135+
clientConfig,
136+
});
137+
138+
await helpers.callCustomEndpoint({
197139
options: {
198-
method: 'POST',
140+
method: "POST",
199141
parameters: {
200-
queryParameter: 'queryParameter1',
142+
queryParameter: "queryParameter1",
201143
},
202144
headers: {
203-
authorization: `Bearer ${access_token}`
145+
authorization: `Bearer ${accessToken}`,
204146
},
205-
customApiPathParameters: customApiArgs,
206-
body: JSON.stringify({ data: 'data' })
207-
},
208-
clientConfig: clientConfigExample,
209-
// Flag to retrieve raw response or data from helper function
210-
rawResponse: false,
211-
})
212-
213-
console.log('get response: ', getResponse)
214-
console.log('post response: ', postResponse)
147+
customApiPathParameters,
148+
body: JSON.stringify({ data: "data" }),
149+
},
150+
clientConfig,
151+
});
215152
```
216153

217154
For more documentation about this helper function, please refer to the [commerce-sdk-isomorphic docs](https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/modules/helpers.html).
218155

219-
For more information about custom APIs, please refer to the [Salesforce Developer Docs](https://developer.salesforce.com/docs/commerce/commerce-api/guide/custom-apis.html)
220-
221156
## License Information
222157

223158
The Commerce SDK Isomorphic is licensed under BSD-3-Clause license. See the [license](./LICENSE.txt) for details.

0 commit comments

Comments
 (0)