commerce-sdk-isomorphic 3.1.1 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -3
- package/lib/index.cjs.d.ts +8224 -7325
- package/lib/index.cjs.js +1 -1
- package/lib/index.esm.d.ts +8224 -7325
- package/lib/index.esm.js +1 -1
- package/package.json +6 -6
- package/CHANGELOG.md +0 -340
package/README.md
CHANGED
|
@@ -21,6 +21,12 @@ In practice, we recommend:
|
|
|
21
21
|
- For customers using the SLAS helpers with a public client, it is recommended to upgrade to at least `v1.8.0` of the `commerce-sdk-isomorphic`.
|
|
22
22
|
- For customers using the SLAS helpers with a private client, it is recommended to upgrade to `v3.0.0` of the `commerce-sdk-isomorphic`.
|
|
23
23
|
|
|
24
|
+
## :warning: Planned SDK Changes :warning:
|
|
25
|
+
|
|
26
|
+
### Encoding path parameters
|
|
27
|
+
|
|
28
|
+
In the next major version release, the SDK will encode special characters (UTF-8 based on SCAPI guidelines) in path parameters by default. Please see the [Encoding special characters](#encoding-special-characters) section for more details.
|
|
29
|
+
|
|
24
30
|
## Getting Started
|
|
25
31
|
|
|
26
32
|
### Requirements
|
|
@@ -84,12 +90,37 @@ const config = {
|
|
|
84
90
|
|
|
85
91
|
For more info, refer to the [documentation site](https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/).
|
|
86
92
|
|
|
93
|
+
#### `throwOnBadResponse`
|
|
94
|
+
|
|
95
|
+
When `true`, the SDK throws an `Error` on responses whose status is not 2xx or 304. By default, the value of this flag is `false` for backwards compatibility. Below is an example for accessing the error object via `e.response.json()`.
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
const config = {
|
|
99
|
+
throwOnBadResponse: true
|
|
100
|
+
// rest of the config object...
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const shopperSearch = new ShopperSearch({
|
|
104
|
+
...config
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// in an async function
|
|
108
|
+
try {
|
|
109
|
+
const searchResult = await shopperSearch.productSearch({
|
|
110
|
+
parameters: { q: "shirt" },
|
|
111
|
+
});
|
|
112
|
+
} catch (e) {
|
|
113
|
+
const error = await e.response.json();
|
|
114
|
+
console.log(error);
|
|
115
|
+
// error is the JSON object - {error: ",,,"}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
87
119
|
#### Additional Config Settings
|
|
88
120
|
|
|
89
121
|
* `headers`: Headers to include with API requests.
|
|
90
|
-
* `throwOnBadResponse`: When `true`, the SDK throws an `Error` on responses whose status is not 2xx or 304.
|
|
91
122
|
|
|
92
|
-
|
|
123
|
+
#### Custom Query Parameters
|
|
93
124
|
|
|
94
125
|
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_`:
|
|
95
126
|
|
|
@@ -104,7 +135,7 @@ const searchResult = await shopperSearch.productSearch({
|
|
|
104
135
|
|
|
105
136
|
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.
|
|
106
137
|
|
|
107
|
-
|
|
138
|
+
#### Custom APIs
|
|
108
139
|
|
|
109
140
|
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`:
|
|
110
141
|
|
|
@@ -170,6 +201,67 @@ await helpers.callCustomEndpoint({
|
|
|
170
201
|
|
|
171
202
|
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).
|
|
172
203
|
|
|
204
|
+
#### Encoding special characters
|
|
205
|
+
|
|
206
|
+
The SDK currently single encodes special characters for query parameters in UTF-8 format based on SCAPI guidelines. However, the SDK does NOT encode path parameters, and will require the developer to encode any path parameters with special characters.
|
|
207
|
+
|
|
208
|
+
Additionally, SCAPI has special characters that should be double encoded, specifically `%` and `,`:
|
|
209
|
+
- `%` should always be double encoded
|
|
210
|
+
- `,` should be double encoded when used as part of an ID/parameter string, and single encoded when used to differentiate items in a list
|
|
211
|
+
|
|
212
|
+
There is a helper function called `encodeSCAPISpecialCharacters` that can be utilized to single encode the SCAPI special characters and no other special characters.
|
|
213
|
+
|
|
214
|
+
Here's an example where the `getCategory/getCategories` endpoints are called with a `categoryID` with special characters:
|
|
215
|
+
```javascript
|
|
216
|
+
import pkg from "commerce-sdk-isomorphic";
|
|
217
|
+
const { helpers, ShopperProducts } = pkg;
|
|
218
|
+
|
|
219
|
+
const clientConfig = {
|
|
220
|
+
parameters: {
|
|
221
|
+
clientId: "<your-client-id>",
|
|
222
|
+
organizationId: "<your-org-id>",
|
|
223
|
+
shortCode: "<your-short-code>",
|
|
224
|
+
siteId: "<your-site-id>",
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const shopperProducts = new ShopperProducts({
|
|
229
|
+
...clientConfig,
|
|
230
|
+
headers: {authorization: `Bearer <insert_access_token>`}
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
const categoryId = "SpecialCharacter,%$^@&$;()!123Category";
|
|
234
|
+
// "SpecialCharacter%2C%25$^@&$;()!123Category"
|
|
235
|
+
const scapiSpecialEncodedId = helpers.encodeSCAPISpecialCharacters(categoryId);
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
// id is a path parameter for API call:
|
|
239
|
+
// <base-url>/product/shopper-products/v1/organizations/{organizationId}/categories/{id}
|
|
240
|
+
const categoryResult = await shopperProducts.getCategory({
|
|
241
|
+
parameters: {
|
|
242
|
+
// Path parameters are NOT encoded by the SDK, so we have to single encode special characters
|
|
243
|
+
// and the SCAPI special characters will end up double encoded
|
|
244
|
+
id: encodeURIComponent(scapiSpecialEncodedId),
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
console.log("categoryResult: ", categoryResult);
|
|
249
|
+
|
|
250
|
+
// `ids` are a query parameter and comma delimited to separate category IDs
|
|
251
|
+
const categoriesResult = await shopperProducts.getCategories({
|
|
252
|
+
parameters: {
|
|
253
|
+
// No need to use `encodeURIComponent` as query parameters are single encoded by the SDK
|
|
254
|
+
// So the SCAPI special characters will end up double encoded as well
|
|
255
|
+
// Commas that separate items in a list will end up single encoded
|
|
256
|
+
ids: `${scapiSpecialEncodedId},${scapiSpecialEncodedId}`,
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
console.log("categoriesResult: ", categoriesResult);
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
**NOTE: In the next major version release, path parameters will be single encoded by default**
|
|
264
|
+
|
|
173
265
|
## License Information
|
|
174
266
|
|
|
175
267
|
The Commerce SDK Isomorphic is licensed under BSD-3-Clause license. See the [license](./LICENSE.txt) for details.
|