@akinon/pz-masterpass-rest 2.0.56-rc.0 → 2.0.56

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,64 +0,0 @@
1
- const DEFAULT_TIMEOUT_MS = 10000;
2
- const POLL_INTERVAL_MS = 100;
3
-
4
- interface MasterpassUtils {
5
- decodeJwt: (token: string) => any;
6
- [key: string]: any;
7
- }
8
-
9
- // `window.Utils` is attached by the Masterpass REST SDK and is undefined until
10
- // that script finishes loading, so it is resolved through this guarded getter
11
- // rather than accessed directly.
12
- const getMasterpassUtils = (): MasterpassUtils | undefined =>
13
- (window as unknown as { Utils?: MasterpassUtils }).Utils;
14
-
15
- // The Masterpass REST SDK (`/masterpass-javascript-sdk-web.min.js`) attaches
16
- // both `window.Masterpass` and `window.Utils` when it finishes loading. That
17
- // script is loaded asynchronously (see `useMasterpassScript`), so on slow
18
- // networks the `/orders/masterpass-token` response can arrive before the SDK
19
- // is ready. Calling `window.Utils.decodeJwt(...)` at that point crashes the
20
- // checkout with `TypeError: Cannot read properties of undefined (reading
21
- // 'decodeJwt')` (ZERO-4362 — same class of bug already fixed in the non-rest
22
- // `@akinon/pz-masterpass` package).
23
- //
24
- // This helper polls until the SDK has exposed `window.Utils.decodeJwt`, so
25
- // callers can await readiness before decoding instead of crashing.
26
- export const waitForUtilsReady = (
27
- timeoutMs: number = DEFAULT_TIMEOUT_MS
28
- ): Promise<boolean> =>
29
- new Promise((resolve) => {
30
- const start = Date.now();
31
-
32
- const check = () => {
33
- if (typeof getMasterpassUtils()?.decodeJwt === 'function') {
34
- resolve(true);
35
- return;
36
- }
37
-
38
- if (Date.now() - start >= timeoutMs) {
39
- resolve(false);
40
- return;
41
- }
42
-
43
- setTimeout(check, POLL_INTERVAL_MS);
44
- };
45
-
46
- check();
47
- });
48
-
49
- // Guarded `decodeJwt` wrapper. Returns `undefined` (instead of throwing) when
50
- // the SDK is not yet available, so every callsite degrades gracefully.
51
- export const decodeMasterpassToken = <T = any>(
52
- token: string
53
- ): T | undefined => {
54
- const utils = getMasterpassUtils();
55
-
56
- if (typeof utils?.decodeJwt !== 'function') {
57
- console.error(
58
- '[pz-masterpass-rest] window.Utils.decodeJwt is not available'
59
- );
60
- return undefined;
61
- }
62
-
63
- return utils.decodeJwt(token);
64
- };