@stytch/react 17.0.0 → 18.1.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.
@@ -0,0 +1,27 @@
1
+ import { Dispatch, SetStateAction } from "react";
2
+ declare const noProviderError: (item: string, provider?: string) => string;
3
+ declare const providerMustBeUniqueError = "You cannot render a <StytchProvider> inside another <StytchProvider>.";
4
+ declare const B2BProviderMustBeUniqueError = "You cannot render a <StytchB2BProvider> inside another <StytchB2BProvider>.";
5
+ declare const noSSRError = "The @stytch/react library is not meant for use with serverside environments like NextJS.\nUse the @stytch/nextjs library instead - \nnpm remove @stytch/react && npm install @stytch/nextjs\n";
6
+ declare const noHeadlessClientError = "Tried to create a Stytch Login UI element using the Stytch Headless SDK.\nYou must use the UI SDK to use UI elements.\nPlease make sure you are importing from @stytch/vanilla-js and not from the @stytch/vanilla-js/headless.";
7
+ // useState can cause memory leaks if it is set after the component unmounted. For example, if it is
8
+ // set after `await`, or in a `then`, `catch`, or `finally`, or in a setTimout/setInterval.
9
+ declare const useAsyncState: <T>(initialState: T | (() => T)) => [
10
+ T,
11
+ Dispatch<SetStateAction<T>>
12
+ ];
13
+ /**
14
+ * Returns a version of `newValue` whose properties that are deeply equal to
15
+ * those in `oldValue` are replaced with those from `oldValue`. This provides a
16
+ * limited form of "structural sharing" that provides a stable reference for
17
+ * unchanged slices of the object.
18
+ *
19
+ * If `oldValue` and `newValue` are referentially equal, the same value is
20
+ * returned.
21
+ *
22
+ * @param oldValue The old value
23
+ * @param newValue The new value
24
+ */
25
+ declare const mergeWithStableProps: <T extends Record<string, unknown>, U extends Record<string, unknown> = T>(oldValue: U, newValue: T) => T;
26
+ declare function invariant(cond: any, message: string): asserts cond;
27
+ export { noProviderError, providerMustBeUniqueError, B2BProviderMustBeUniqueError, noSSRError, noHeadlessClientError, useAsyncState, mergeWithStableProps, invariant };
@@ -0,0 +1,88 @@
1
+ import { useRef, useState, useEffect, useCallback } from 'react';
2
+
3
+ const createDeepEqual = ({ KEYS_TO_EXCLUDE = [] } = {}) => {
4
+ // If comparing functions, this may need some work. Not sure the
5
+ // best path for this: compare instance (what it currently does),
6
+ // stringify and compare, etc.
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ const deepEqual = (a, b) => {
9
+ // Ensures type is the same
10
+ if (typeof a !== typeof b)
11
+ return false;
12
+ // arrays, null, and objects all have type 'object'
13
+ if (a === null || b === null)
14
+ return a === b;
15
+ if (typeof a === 'object') {
16
+ if (Object.keys(a).length !== Object.keys(b).length || Object.keys(a).some((k) => !(k in b)))
17
+ return false;
18
+ return Object.entries(a)
19
+ .filter(([k]) => !KEYS_TO_EXCLUDE.includes(k))
20
+ .every(([k, v]) => deepEqual(v, b[k]));
21
+ }
22
+ // boolean, string, number, undefined
23
+ return a === b;
24
+ };
25
+ return deepEqual;
26
+ };
27
+
28
+ const deepEqual = createDeepEqual();
29
+ /**
30
+ * Returns a version of `newValue` whose properties that are deeply equal to
31
+ * those in `oldValue` are replaced with those from `oldValue`. This provides a
32
+ * limited form of "structural sharing" that provides a stable reference for
33
+ * unchanged slices of the object.
34
+ *
35
+ * If `oldValue` and `newValue` are referentially equal, the same value is
36
+ * returned.
37
+ *
38
+ * @param oldValue The old value
39
+ * @param newValue The new value
40
+ */
41
+ const mergeWithStableProps = (oldValue, newValue) => {
42
+ // If the values are already referentially the same, just return the new value
43
+ if (oldValue === newValue) {
44
+ return newValue;
45
+ }
46
+ return Object.keys(oldValue).reduce((acc, key) => {
47
+ if (key in newValue && deepEqual(oldValue[key], newValue[key])) {
48
+ acc[key] = oldValue[key];
49
+ }
50
+ return acc;
51
+ }, Object.assign({}, newValue));
52
+ };
53
+
54
+ // useState can cause memory leaks if it is set after the component unmounted. For example, if it is
55
+ // set after `await`, or in a `then`, `catch`, or `finally`, or in a setTimout/setInterval.
56
+ const useAsyncState = (initialState) => {
57
+ const isMounted = useRef(true);
58
+ const [state, setState] = useState(initialState);
59
+ useEffect(() => {
60
+ isMounted.current = true;
61
+ return () => {
62
+ isMounted.current = false;
63
+ };
64
+ }, []);
65
+ const setStateAction = useCallback((newState) => {
66
+ isMounted.current && setState(newState);
67
+ }, []);
68
+ return [state, setStateAction];
69
+ };
70
+
71
+ const noProviderError = (item, provider = 'StytchProvider') => `${item} can only be used inside <${provider}>.`;
72
+ const providerMustBeUniqueError = 'You cannot render a <StytchProvider> inside another <StytchProvider>.';
73
+ const B2BProviderMustBeUniqueError = 'You cannot render a <StytchB2BProvider> inside another <StytchB2BProvider>.';
74
+ const noSSRError = `The @stytch/react library is not meant for use with serverside environments like NextJS.
75
+ Use the @stytch/nextjs library instead -
76
+ npm remove @stytch/react && npm install @stytch/nextjs
77
+ `;
78
+ const noHeadlessClientError = `Tried to create a Stytch Login UI element using the Stytch Headless SDK.
79
+ You must use the UI SDK to use UI elements.
80
+ Please make sure you are importing from @stytch/vanilla-js and not from the @stytch/vanilla-js/headless.`;
81
+
82
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
83
+ function invariant(cond, message) {
84
+ if (!cond)
85
+ throw new Error(message);
86
+ }
87
+
88
+ export { B2BProviderMustBeUniqueError as B, noSSRError as a, noHeadlessClientError as b, invariant as i, mergeWithStableProps as m, noProviderError as n, providerMustBeUniqueError as p, useAsyncState as u };
@@ -0,0 +1,27 @@
1
+ import { Dispatch, SetStateAction } from "react";
2
+ declare const noProviderError: (item: string, provider?: string) => string;
3
+ declare const providerMustBeUniqueError = "You cannot render a <StytchProvider> inside another <StytchProvider>.";
4
+ declare const B2BProviderMustBeUniqueError = "You cannot render a <StytchB2BProvider> inside another <StytchB2BProvider>.";
5
+ declare const noSSRError = "The @stytch/react library is not meant for use with serverside environments like NextJS.\nUse the @stytch/nextjs library instead - \nnpm remove @stytch/react && npm install @stytch/nextjs\n";
6
+ declare const noHeadlessClientError = "Tried to create a Stytch Login UI element using the Stytch Headless SDK.\nYou must use the UI SDK to use UI elements.\nPlease make sure you are importing from @stytch/vanilla-js and not from the @stytch/vanilla-js/headless.";
7
+ // useState can cause memory leaks if it is set after the component unmounted. For example, if it is
8
+ // set after `await`, or in a `then`, `catch`, or `finally`, or in a setTimout/setInterval.
9
+ declare const useAsyncState: <T>(initialState: T | (() => T)) => [
10
+ T,
11
+ Dispatch<SetStateAction<T>>
12
+ ];
13
+ /**
14
+ * Returns a version of `newValue` whose properties that are deeply equal to
15
+ * those in `oldValue` are replaced with those from `oldValue`. This provides a
16
+ * limited form of "structural sharing" that provides a stable reference for
17
+ * unchanged slices of the object.
18
+ *
19
+ * If `oldValue` and `newValue` are referentially equal, the same value is
20
+ * returned.
21
+ *
22
+ * @param oldValue The old value
23
+ * @param newValue The new value
24
+ */
25
+ declare const mergeWithStableProps: <T extends Record<string, unknown>, U extends Record<string, unknown> = T>(oldValue: U, newValue: T) => T;
26
+ declare function invariant(cond: any, message: string): asserts cond;
27
+ export { noProviderError, providerMustBeUniqueError, B2BProviderMustBeUniqueError, noSSRError, noHeadlessClientError, useAsyncState, mergeWithStableProps, invariant };
@@ -0,0 +1,97 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+
5
+ const createDeepEqual = ({ KEYS_TO_EXCLUDE = [] } = {}) => {
6
+ // If comparing functions, this may need some work. Not sure the
7
+ // best path for this: compare instance (what it currently does),
8
+ // stringify and compare, etc.
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ const deepEqual = (a, b) => {
11
+ // Ensures type is the same
12
+ if (typeof a !== typeof b)
13
+ return false;
14
+ // arrays, null, and objects all have type 'object'
15
+ if (a === null || b === null)
16
+ return a === b;
17
+ if (typeof a === 'object') {
18
+ if (Object.keys(a).length !== Object.keys(b).length || Object.keys(a).some((k) => !(k in b)))
19
+ return false;
20
+ return Object.entries(a)
21
+ .filter(([k]) => !KEYS_TO_EXCLUDE.includes(k))
22
+ .every(([k, v]) => deepEqual(v, b[k]));
23
+ }
24
+ // boolean, string, number, undefined
25
+ return a === b;
26
+ };
27
+ return deepEqual;
28
+ };
29
+
30
+ const deepEqual = createDeepEqual();
31
+ /**
32
+ * Returns a version of `newValue` whose properties that are deeply equal to
33
+ * those in `oldValue` are replaced with those from `oldValue`. This provides a
34
+ * limited form of "structural sharing" that provides a stable reference for
35
+ * unchanged slices of the object.
36
+ *
37
+ * If `oldValue` and `newValue` are referentially equal, the same value is
38
+ * returned.
39
+ *
40
+ * @param oldValue The old value
41
+ * @param newValue The new value
42
+ */
43
+ const mergeWithStableProps = (oldValue, newValue) => {
44
+ // If the values are already referentially the same, just return the new value
45
+ if (oldValue === newValue) {
46
+ return newValue;
47
+ }
48
+ return Object.keys(oldValue).reduce((acc, key) => {
49
+ if (key in newValue && deepEqual(oldValue[key], newValue[key])) {
50
+ acc[key] = oldValue[key];
51
+ }
52
+ return acc;
53
+ }, Object.assign({}, newValue));
54
+ };
55
+
56
+ // useState can cause memory leaks if it is set after the component unmounted. For example, if it is
57
+ // set after `await`, or in a `then`, `catch`, or `finally`, or in a setTimout/setInterval.
58
+ const useAsyncState = (initialState) => {
59
+ const isMounted = React.useRef(true);
60
+ const [state, setState] = React.useState(initialState);
61
+ React.useEffect(() => {
62
+ isMounted.current = true;
63
+ return () => {
64
+ isMounted.current = false;
65
+ };
66
+ }, []);
67
+ const setStateAction = React.useCallback((newState) => {
68
+ isMounted.current && setState(newState);
69
+ }, []);
70
+ return [state, setStateAction];
71
+ };
72
+
73
+ const noProviderError = (item, provider = 'StytchProvider') => `${item} can only be used inside <${provider}>.`;
74
+ const providerMustBeUniqueError = 'You cannot render a <StytchProvider> inside another <StytchProvider>.';
75
+ const B2BProviderMustBeUniqueError = 'You cannot render a <StytchB2BProvider> inside another <StytchB2BProvider>.';
76
+ const noSSRError = `The @stytch/react library is not meant for use with serverside environments like NextJS.
77
+ Use the @stytch/nextjs library instead -
78
+ npm remove @stytch/react && npm install @stytch/nextjs
79
+ `;
80
+ const noHeadlessClientError = `Tried to create a Stytch Login UI element using the Stytch Headless SDK.
81
+ You must use the UI SDK to use UI elements.
82
+ Please make sure you are importing from @stytch/vanilla-js and not from the @stytch/vanilla-js/headless.`;
83
+
84
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
85
+ function invariant(cond, message) {
86
+ if (!cond)
87
+ throw new Error(message);
88
+ }
89
+
90
+ exports.B2BProviderMustBeUniqueError = B2BProviderMustBeUniqueError;
91
+ exports.invariant = invariant;
92
+ exports.mergeWithStableProps = mergeWithStableProps;
93
+ exports.noHeadlessClientError = noHeadlessClientError;
94
+ exports.noProviderError = noProviderError;
95
+ exports.noSSRError = noSSRError;
96
+ exports.providerMustBeUniqueError = providerMustBeUniqueError;
97
+ exports.useAsyncState = useAsyncState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stytch/react",
3
- "version": "17.0.0",
3
+ "version": "18.1.0",
4
4
  "description": "Stytch's official React Library",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -34,7 +34,7 @@
34
34
  "devDependencies": {
35
35
  "@babel/runtime": "7.18.6",
36
36
  "@stytch/js-utils": "0.0.0",
37
- "@stytch/vanilla-js": "4.9.0",
37
+ "@stytch/vanilla-js": "4.13.0",
38
38
  "@testing-library/react": "14.0.0",
39
39
  "eslint-config-custom": "0.0.1",
40
40
  "react": "18.2.0",
@@ -45,7 +45,7 @@
45
45
  "typescript": "5.3.3"
46
46
  },
47
47
  "peerDependencies": {
48
- "@stytch/vanilla-js": "^4.9.0",
48
+ "@stytch/vanilla-js": "^4.13.0",
49
49
  "react": ">= 17.0.2",
50
50
  "react-dom": ">= 17.0.2"
51
51
  }