@salesforce/commerce-sdk-react 3.3.0-dev → 3.3.0-extensibility-preview.4

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/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
+ ## v3.3.0-extensibility-preview.4 (Feb 12, 2025)
2
+ - Add `ServerContext` type for `useServerContext` hook [#2239](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2239)
3
+
1
4
  ## v3.3.0-dev (Feb 18, 2025)
2
5
  - Invalidate cache instead of removing cache when triggering logout [#2323](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2323)
3
6
  - Fix dependencies vulnerabilities [#2338](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2338)
7
+ - Allow custom parameters/body to be passed to SLAS authorize/authenticate calls via commerce-sdk-react auth helpers [#2358](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2358)
4
8
 
5
9
  ## v3.2.1 (Mar 05, 2025)
6
10
  - Update PWA-Kit SDKs to v3.9.1 [#2301](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2301)
package/auth/index.d.ts CHANGED
@@ -24,6 +24,16 @@ type LoginIDPUserParams = Parameters<Helpers['loginIDPUser']>[2];
24
24
  type AuthorizePasswordlessParams = Parameters<Helpers['authorizePasswordless']>[2];
25
25
  type LoginPasswordlessParams = Parameters<Helpers['getPasswordLessAccessToken']>[2];
26
26
  type LoginRegisteredUserB2CCredentials = Parameters<Helpers['loginRegisteredUserB2C']>[1];
27
+ /**
28
+ * This is a temporary type until we can make a breaking change and modify the signature for
29
+ * loginRegisteredUserB2C so that it takes in a body rather than just credentials
30
+ *
31
+ */
32
+ type LoginRegisteredUserCredentialsWithCustomParams = LoginRegisteredUserB2CCredentials & {
33
+ options?: {
34
+ body: helpers.CustomRequestBody;
35
+ };
36
+ };
27
37
  /**
28
38
  * The extended field is not from api response, we manually store the auth type,
29
39
  * so we don't need to make another API call when we already have the data.
@@ -199,7 +209,7 @@ declare class Auth {
199
209
  * A wrapper method for commerce-sdk-isomorphic helper: loginGuestUser.
200
210
  *
201
211
  */
202
- loginGuestUser(): Promise<ShopperLoginTypes.TokenResponse>;
212
+ loginGuestUser(parameters?: helpers.CustomQueryParameters): Promise<ShopperLoginTypes.TokenResponse>;
203
213
  /**
204
214
  * This is a wrapper method for ShopperCustomer API registerCustomer endpoint.
205
215
  *
@@ -302,8 +312,14 @@ declare class Auth {
302
312
  /**
303
313
  * A wrapper method for commerce-sdk-isomorphic helper: loginRegisteredUserB2C.
304
314
  *
315
+ * Note: This uses the type LoginRegisteredUserCredentialsWithCustomParams rather than LoginRegisteredUserB2CCredentials
316
+ * as a workaround to allow custom parameters through because the login.mutateAsync hook will only pass through a single
317
+ * 'body' argument into this function.
318
+ *
319
+ * In the next major version release, we should modify this method so that it's input is a body containing credentials,
320
+ * similar to the input for the register function.
305
321
  */
306
- loginRegisteredUserB2C(credentials: LoginRegisteredUserB2CCredentials): Promise<helpers.TokenResponse>;
322
+ loginRegisteredUserB2C(credentials: LoginRegisteredUserCredentialsWithCustomParams): Promise<helpers.TokenResponse>;
307
323
  /**
308
324
  * Trusted agent authorization
309
325
  *
package/auth/index.js CHANGED
@@ -9,6 +9,9 @@ var _jwtDecode = require("jwt-decode");
9
9
  var _storage = require("./storage");
10
10
  var _utils = require("../utils");
11
11
  var _constant = require("../constant");
12
+ const _excluded = ["customer", "password"];
13
+ function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
14
+ function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
12
15
  function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
13
16
  function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
14
17
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
@@ -21,6 +24,12 @@ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e =
21
24
  * SPDX-License-Identifier: BSD-3-Clause
22
25
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
23
26
  */
27
+ /**
28
+ * This is a temporary type until we can make a breaking change and modify the signature for
29
+ * loginRegisteredUserB2C so that it takes in a body rather than just credentials
30
+ *
31
+ */
32
+
24
33
  /**
25
34
  * The extended field is not from api response, we manually store the auth type,
26
35
  * so we don't need to make another API call when we already have the data.
@@ -692,7 +701,7 @@ class Auth {
692
701
  * A wrapper method for commerce-sdk-isomorphic helper: loginGuestUser.
693
702
  *
694
703
  */
695
- loginGuestUser() {
704
+ loginGuestUser(parameters) {
696
705
  var _this6 = this;
697
706
  return _asyncToGenerator(function* () {
698
707
  if (_this6.clientSecret && (0, _utils.onClient)() && _this6.clientSecret !== _constant.SLAS_SECRET_PLACEHOLDER) {
@@ -710,12 +719,12 @@ class Auth {
710
719
  }), {
711
720
  clientSecret: _this6.clientSecret
712
721
  }];
713
- const guestPublicArgs = [_this6.client, _objectSpread({
722
+ const guestPublicArgs = [_this6.client, _objectSpread(_objectSpread({
714
723
  redirectURI: _this6.redirectURI,
715
724
  dnt: dntPref
716
725
  }, usid && {
717
726
  usid
718
- })];
727
+ }), parameters)];
719
728
  const callback = _this6.clientSecret ? () => _commerceSdkIsomorphic.helpers.loginGuestUserPrivate(...guestPrivateArgs) : () => _commerceSdkIsomorphic.helpers.loginGuestUser(...guestPublicArgs);
720
729
  try {
721
730
  return yield _this6.queueRequest(callback, isGuest);
@@ -740,11 +749,14 @@ class Auth {
740
749
  var _this7 = this;
741
750
  return _asyncToGenerator(function* () {
742
751
  const {
743
- customer: {
744
- login
745
- },
746
- password
747
- } = body;
752
+ customer,
753
+ password
754
+ } = body,
755
+ parameters = _objectWithoutProperties(body, _excluded);
756
+ const {
757
+ login
758
+ } = customer;
759
+ const customParameters = (0, _utils.extractCustomParameters)(parameters);
748
760
 
749
761
  // login is optional field from isomorphic library
750
762
  // type CustomerRegistration
@@ -752,15 +764,24 @@ class Auth {
752
764
  if (!login) {
753
765
  throw new Error('Customer registration is missing login field.');
754
766
  }
767
+
768
+ // The registerCustomer endpoint currently does not support custom parameters
769
+ // so we make sure not to send any custom params here
755
770
  const res = yield _this7.shopperCustomersClient.registerCustomer({
756
771
  headers: {
757
772
  authorization: `Bearer ${_this7.get('access_token')}`
758
773
  },
759
- body
774
+ body: {
775
+ customer,
776
+ password
777
+ }
760
778
  });
761
779
  yield _this7.loginRegisteredUserB2C({
762
780
  username: login,
763
- password
781
+ password,
782
+ options: {
783
+ body: customParameters
784
+ }
764
785
  });
765
786
  return res;
766
787
  })();
@@ -769,6 +790,12 @@ class Auth {
769
790
  /**
770
791
  * A wrapper method for commerce-sdk-isomorphic helper: loginRegisteredUserB2C.
771
792
  *
793
+ * Note: This uses the type LoginRegisteredUserCredentialsWithCustomParams rather than LoginRegisteredUserB2CCredentials
794
+ * as a workaround to allow custom parameters through because the login.mutateAsync hook will only pass through a single
795
+ * 'body' argument into this function.
796
+ *
797
+ * In the next major version release, we should modify this method so that it's input is a body containing credentials,
798
+ * similar to the input for the register function.
772
799
  */
773
800
  loginRegisteredUserB2C(credentials) {
774
801
  var _this8 = this;
@@ -782,14 +809,16 @@ class Auth {
782
809
  includeDefaults: true
783
810
  });
784
811
  const isGuest = false;
785
- const token = yield _commerceSdkIsomorphic.helpers.loginRegisteredUserB2C(_this8.client, _objectSpread(_objectSpread({}, credentials), {}, {
812
+ const token = yield _commerceSdkIsomorphic.helpers.loginRegisteredUserB2C(_this8.client, {
813
+ username: credentials.username,
814
+ password: credentials.password,
786
815
  clientSecret: _this8.clientSecret
787
- }), _objectSpread({
816
+ }, _objectSpread({
788
817
  redirectURI,
789
818
  dnt: dntPref
790
819
  }, usid && {
791
820
  usid
792
- }));
821
+ }), credentials.options);
793
822
  _this8.handleTokenResponse(token, isGuest);
794
823
  if ((0, _utils.onClient)()) {
795
824
  void _this8.clearECOMSession();
@@ -956,15 +985,16 @@ class Auth {
956
985
  return _asyncToGenerator(function* () {
957
986
  const redirectURI = parameters.redirectURI || _this14.redirectURI;
958
987
  const usid = _this14.get('usid');
988
+ const customParameters = (0, _utils.extractCustomParameters)(parameters);
959
989
  const {
960
990
  url,
961
991
  codeVerifier
962
- } = yield _commerceSdkIsomorphic.helpers.authorizeIDP(_this14.client, _objectSpread({
992
+ } = yield _commerceSdkIsomorphic.helpers.authorizeIDP(_this14.client, _objectSpread(_objectSpread({
963
993
  redirectURI,
964
994
  hint: parameters.hint
965
995
  }, usid && {
966
996
  usid
967
- }), _this14.isPrivate);
997
+ }), customParameters), _this14.isPrivate);
968
998
  if ((0, _utils.onClient)()) {
969
999
  window.location.assign(url);
970
1000
  } else {
package/hooks/types.d.ts CHANGED
@@ -144,5 +144,10 @@ export type TMutationVariables = {
144
144
  [key: string]: string;
145
145
  };
146
146
  } | void;
147
+ export type ServerContext = {
148
+ req: {
149
+ originalUrl?: string;
150
+ };
151
+ };
147
152
  export {};
148
153
  //# sourceMappingURL=types.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/commerce-sdk-react",
3
- "version": "3.3.0-dev",
3
+ "version": "3.3.0-extensibility-preview.4",
4
4
  "description": "A library that provides react hooks for fetching data from Commerce Cloud",
5
5
  "homepage": "https://github.com/SalesforceCommerceCloud/pwa-kit/tree/develop/packages/ecom-react-hooks#readme",
6
6
  "bugs": {
@@ -40,12 +40,12 @@
40
40
  "version": "node ./scripts/version.js"
41
41
  },
42
42
  "dependencies": {
43
- "commerce-sdk-isomorphic": "^3.2.0",
43
+ "commerce-sdk-isomorphic": "^3.3.0",
44
44
  "js-cookie": "^3.0.1",
45
45
  "jwt-decode": "^4.0.0"
46
46
  },
47
47
  "devDependencies": {
48
- "@salesforce/pwa-kit-dev": "3.10.0-dev",
48
+ "@salesforce/pwa-kit-dev": "4.0.0-extensibility-preview.4",
49
49
  "@tanstack/react-query": "^4.28.0",
50
50
  "@testing-library/jest-dom": "^5.16.5",
51
51
  "@testing-library/react": "^14.0.0",
@@ -60,7 +60,7 @@
60
60
  "@types/react-helmet": "~6.1.6",
61
61
  "@types/react-router-dom": "~5.3.3",
62
62
  "cross-env": "^5.2.1",
63
- "internal-lib-build": "3.10.0-dev",
63
+ "internal-lib-build": "4.0.0-extensibility-preview.4",
64
64
  "jsonwebtoken": "^9.0.0",
65
65
  "nock": "^13.3.0",
66
66
  "nodemon": "^2.0.22",
@@ -90,5 +90,5 @@
90
90
  "publishConfig": {
91
91
  "directory": "dist"
92
92
  },
93
- "gitHead": "d86dc7bea3fe03a0f475ecf1a0ab878374f07026"
93
+ "gitHead": "5ad3b1f0e598cfe0fbdec5b1212b4dabc0a8733c"
94
94
  }
package/provider.d.ts CHANGED
@@ -20,6 +20,7 @@ export interface CommerceApiProviderProps extends ApiClientConfigParams {
20
20
  passwordlessLoginCallbackURI?: string;
21
21
  refreshTokenRegisteredCookieTTL?: number;
22
22
  refreshTokenGuestCookieTTL?: number;
23
+ OCAPISessionsURL?: string;
23
24
  }
24
25
  /**
25
26
  * @internal
package/utils.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { CookieAttributes } from 'js-cookie';
2
+ import { helpers } from 'commerce-sdk-isomorphic';
2
3
  /** Utility to determine if you are on the browser (client) or not. */
3
4
  export declare const onClient: () => boolean;
4
5
  /** Detects whether the storefront is running in an iframe. */
@@ -58,4 +59,15 @@ export declare function isAbsoluteUrl(url: string): boolean;
58
59
  * that uses `Buffer` to perform the Base64 encoding.
59
60
  */
60
61
  export declare const stringToBase64: typeof btoa;
62
+ /**
63
+ * Extracts custom parameters from a set of SCAPI parameters
64
+ *
65
+ * Custom parameters are identified by the 'c_' prefix before their key
66
+ *
67
+ * @param parameters object containing all parameters for a SCAPI / SLAS call
68
+ * @returns new object containing only custom parameters
69
+ */
70
+ export declare const extractCustomParameters: (parameters: {
71
+ [key: string]: string | number | boolean | string[] | number[];
72
+ } | null) => helpers.CustomQueryParameters | helpers.CustomRequestBody;
61
73
  //# sourceMappingURL=utils.d.ts.map
package/utils.js CHANGED
@@ -7,7 +7,7 @@ exports.DEVELOPMENT_ORIGIN = void 0;
7
7
  exports.detectCookiesAvailable = detectCookiesAvailable;
8
8
  exports.detectInIframe = void 0;
9
9
  exports.detectLocalStorageAvailable = detectLocalStorageAvailable;
10
- exports.getParentOrigin = exports.getDefaultCookieAttributes = exports.getCookieSameSiteAttribute = void 0;
10
+ exports.getParentOrigin = exports.getDefaultCookieAttributes = exports.getCookieSameSiteAttribute = exports.extractCustomParameters = void 0;
11
11
  exports.isAbsoluteUrl = isAbsoluteUrl;
12
12
  exports.stringToBase64 = exports.onClient = exports.isOriginTrusted = void 0;
13
13
  var _jsCookie = _interopRequireDefault(require("js-cookie"));
@@ -154,4 +154,20 @@ function isAbsoluteUrl(url) {
154
154
  * - In a non-browser environment (like Node.js), a fallback is provided
155
155
  * that uses `Buffer` to perform the Base64 encoding.
156
156
  */
157
- const stringToBase64 = exports.stringToBase64 = typeof window === 'object' && typeof window.document === 'object' ? btoa : unencoded => Buffer.from(unencoded).toString('base64');
157
+ const stringToBase64 = exports.stringToBase64 = typeof window === 'object' && typeof window.document === 'object' ? btoa : unencoded => Buffer.from(unencoded).toString('base64');
158
+
159
+ /**
160
+ * Extracts custom parameters from a set of SCAPI parameters
161
+ *
162
+ * Custom parameters are identified by the 'c_' prefix before their key
163
+ *
164
+ * @param parameters object containing all parameters for a SCAPI / SLAS call
165
+ * @returns new object containing only custom parameters
166
+ */
167
+ const extractCustomParameters = parameters => {
168
+ if (typeof parameters !== 'object' || parameters === null) {
169
+ throw new Error('Invalid input. Expecting an object as an input.');
170
+ }
171
+ return Object.fromEntries(Object.entries(parameters).filter(([key]) => key.startsWith('c_')));
172
+ };
173
+ exports.extractCustomParameters = extractCustomParameters;