You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)._
4
6
5
7
## Getting Started
6
8
7
9
### Requirements
8
10
9
11
- 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).
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
82
52
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.
87
54
88
55
```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
-
consthttps=require('https');
56
+
consthttps=require("https");
96
57
97
58
constconfig= {
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
-
},
106
59
fetchOptions: {
107
-
timeout:2000, //request times out after 2 seconds
108
-
agent:newhttps.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:newhttps.agent({ keepAlive:true }),
112
64
},
113
65
};
114
66
```
115
67
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/).
119
69
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.
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.
128
74
129
75
### Custom Query Parameters
130
76
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_`:
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.
143
89
144
90
### Custom APIs
145
91
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`:
// options.customApiPathParameters or clientConfig.parameters
170
-
constcustomApiArgs= {
171
-
apiName:'loyalty-info',
172
-
apiVersion:'v1', // defaults to v1 if not provided
173
-
endpointPath:'customers'
174
-
}
114
+
constcustomApiPathParameters= {
115
+
apiName:"loyalty-info",
116
+
apiVersion:"v1", // defaults to v1 if not provided
117
+
endpointPath:"customers",
118
+
};
175
119
176
-
constaccessToken='<INSERT ACCESS TOKEN HERE>';
120
+
constaccessToken="<INSERT ACCESS TOKEN HERE>";
177
121
178
-
let getResponse =awaithelpers.callCustomEndpoint({
122
+
awaithelpers.callCustomEndpoint({
179
123
options: {
180
-
method:'GET',
124
+
method:"GET",
181
125
parameters: {
182
-
queryParameter:'queryParameter1',
126
+
queryParameter:"queryParameter1",
183
127
},
184
128
headers: {
185
129
// 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}`,
188
132
},
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 =awaithelpers.callCustomEndpoint({
133
+
customApiPathParameters,
134
+
},
135
+
clientConfig,
136
+
});
137
+
138
+
awaithelpers.callCustomEndpoint({
197
139
options: {
198
-
method:'POST',
140
+
method:"POST",
199
141
parameters: {
200
-
queryParameter:'queryParameter1',
142
+
queryParameter:"queryParameter1",
201
143
},
202
144
headers: {
203
-
authorization:`Bearer ${access_token}`
145
+
authorization:`Bearer ${accessToken}`,
204
146
},
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
+
});
215
152
```
216
153
217
154
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).
218
155
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
-
221
156
## License Information
222
157
223
158
The Commerce SDK Isomorphic is licensed under BSD-3-Clause license. See the [license](./LICENSE.txt) for details.
0 commit comments