@salesforce/commerce-sdk-react 3.1.0 → 3.1.1-preview.1

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.
@@ -1,185 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.createTrustedAgentPopup = void 0;
7
- var _react = require("react");
8
- var _reactQuery = require("@tanstack/react-query");
9
- var _useAuthContext = _interopRequireDefault(require("./useAuthContext"));
10
- var _utils = require("../utils");
11
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
- 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
- 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
- * Copyright (c) 2024, salesforce.com, inc.
15
- * All rights reserved.
16
- * SPDX-License-Identifier: BSD-3-Clause
17
- * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
18
- */
19
- let popup;
20
- let intervalId;
21
- const getCodeAndStateValueFromPopup = popup => {
22
- let code = null;
23
- let state = null;
24
- try {
25
- var _popup$location;
26
- const url = new URL((popup === null || popup === void 0 ? void 0 : (_popup$location = popup.location) === null || _popup$location === void 0 ? void 0 : _popup$location.toString()) || 'http://localhost');
27
- code = url.searchParams.get('code');
28
- state = url.searchParams.get('state');
29
- } catch (e) {
30
- /* here to catch invalid URL or crossdomain popup access errors */
31
- }
32
- return {
33
- code,
34
- state
35
- };
36
- };
37
- const createTrustedAgentPopup = exports.createTrustedAgentPopup = /*#__PURE__*/function () {
38
- var _ref = _asyncToGenerator(function* (url, isRefresh = false, timeoutMinutes = 5, refreshTimeoutFocusMinutes = 1) {
39
- var _window, _window$open;
40
- // if a popup already exists, close it
41
- if (popup) {
42
- var _popup;
43
- (_popup = popup) === null || _popup === void 0 ? void 0 : _popup.close();
44
- }
45
-
46
- // if a timer already exists, clear it
47
- if (intervalId) {
48
- clearTimeout(intervalId);
49
- }
50
-
51
- // create our popup
52
- popup = ((_window = window) === null || _window === void 0 ? void 0 : (_window$open = _window.open) === null || _window$open === void 0 ? void 0 : _window$open.call(_window, url, 'accountManagerPopup', 'popup=true,width=800,height=800,scrollbars=false,status=false,location=false,menubar=false,toolbar=false')) || null;
53
-
54
- // if this is intended to be a behind the
55
- // scenes refresh call, make sure our main
56
- // window stays focused
57
- if (isRefresh) {
58
- var _window2, _window2$focus;
59
- (_window2 = window) === null || _window2 === void 0 ? void 0 : (_window2$focus = _window2.focus) === null || _window2$focus === void 0 ? void 0 : _window2$focus.call(_window2);
60
- }
61
- const startTime = Date.now();
62
- return new Promise((resolve, reject) => {
63
- const checkPopupState = () => {
64
- var _popup3;
65
- const popupCouldntInitialize = !popup;
66
- if (popupCouldntInitialize) {
67
- clearTimeout(intervalId);
68
- return reject("Popup couldn't initialize. Check your popup blocker.");
69
- }
70
-
71
- // success state
72
- const {
73
- code,
74
- state
75
- } = getCodeAndStateValueFromPopup(popup);
76
- if (code && state) {
77
- var _popup2;
78
- clearTimeout(intervalId);
79
- (_popup2 = popup) === null || _popup2 === void 0 ? void 0 : _popup2.close();
80
- return resolve({
81
- code,
82
- state
83
- });
84
- }
85
- const popupClosedWithoutAuthenticating = (_popup3 = popup) === null || _popup3 === void 0 ? void 0 : _popup3.closed;
86
- if (popupClosedWithoutAuthenticating) {
87
- clearTimeout(intervalId);
88
- return reject('Popup closed without authenticating.');
89
- }
90
- const popupTimeoutOccurred = Math.floor(Date.now() - startTime) > timeoutMinutes * 1000 * 60;
91
- if (popupTimeoutOccurred) {
92
- var _popup4;
93
- clearTimeout(intervalId);
94
- (_popup4 = popup) === null || _popup4 === void 0 ? void 0 : _popup4.close();
95
- return reject(`Popup timed out after ${timeoutMinutes} minutes.`);
96
- }
97
-
98
- // if our refresh flow is stuck, focus the popup window
99
- const popupRefreshTimeoutOccurred = Math.floor(Date.now() - startTime) > refreshTimeoutFocusMinutes * 1000 * 60;
100
- if (isRefresh && popupRefreshTimeoutOccurred) {
101
- var _popup5;
102
- (_popup5 = popup) === null || _popup5 === void 0 ? void 0 : _popup5.focus();
103
- }
104
- };
105
- checkPopupState();
106
- intervalId = setInterval(checkPopupState, 1000);
107
- });
108
- });
109
- return function createTrustedAgentPopup(_x) {
110
- return _ref.apply(this, arguments);
111
- };
112
- }();
113
-
114
- /**
115
- * A hook to return trusted agent state.
116
- *
117
- * @group Helpers
118
- * @category Shopper Authentication
119
- * @experimental
120
- *
121
- */
122
- const useTrustedAgent = () => {
123
- const auth = (0, _useAuthContext.default)();
124
- const [isAgent, setIsAgent] = (0, _react.useState)(false);
125
- const [agentId, setAgentId] = (0, _react.useState)('');
126
- const [loginId, setLoginId] = (0, _react.useState)('');
127
- const authorizeTrustedAgent = (0, _reactQuery.useMutation)(auth.authorizeTrustedAgent.bind(auth));
128
- const loginTrustedAgent = (0, _reactQuery.useMutation)(auth.loginTrustedAgent.bind(auth));
129
- const logoutTrustedAgent = (0, _reactQuery.useMutation)(auth.logout.bind(auth));
130
- const login = (0, _react.useCallback)( /*#__PURE__*/function () {
131
- var _ref2 = _asyncToGenerator(function* (loginId, usid, refresh = false) {
132
- if (!(0, _utils.onClient)()) {
133
- throw new Error('Something went wrong, this client side method is invoked on the server.');
134
- }
135
- const {
136
- url,
137
- codeVerifier
138
- } = yield authorizeTrustedAgent.mutateAsync({
139
- loginId
140
- });
141
- const {
142
- code,
143
- state
144
- } = yield createTrustedAgentPopup(url, refresh);
145
- return yield loginTrustedAgent.mutateAsync({
146
- loginId,
147
- code,
148
- codeVerifier,
149
- state,
150
- usid
151
- });
152
- });
153
- return function (_x2, _x3) {
154
- return _ref2.apply(this, arguments);
155
- };
156
- }(), [auth]);
157
- const logout = (0, _react.useCallback)( /*#__PURE__*/_asyncToGenerator(function* () {
158
- return yield logoutTrustedAgent.mutateAsync();
159
- }), [auth]);
160
- (0, _react.useEffect)(() => {
161
- auth.registerTrustedAgentRefreshHandler(login);
162
- }, [auth]);
163
- (0, _react.useEffect)(() => {
164
- try {
165
- const {
166
- isAgent,
167
- agentId,
168
- loginId
169
- } = auth.parseSlasJWT(auth.get('access_token'));
170
- setIsAgent(isAgent);
171
- setAgentId(agentId || '');
172
- setLoginId(loginId);
173
- } catch (e) {
174
- /* here to catch invalid jwt errors */
175
- }
176
- }, [auth.get('access_token')]);
177
- return {
178
- isAgent,
179
- agentId,
180
- loginId,
181
- login,
182
- logout
183
- };
184
- };
185
- var _default = exports.default = useTrustedAgent;