@@ -3,24 +3,26 @@ import { recursiveMap } from "../utils";
3
3
import getURI from "../api/webJs" ;
4
4
import { MOCK_ENABLED } from "../constants" ;
5
5
6
- type Uri = {
7
- dateprovider : string ;
8
- emailpassword : string ;
9
- emailverification : string ;
10
- multifactorauth : string ;
11
- multitenancy : string ;
12
- passwordless : string ;
13
- session : string ;
14
- supertokens : string ;
15
- thirdparty : string ;
16
- totp : string ;
17
- userroles : string ;
18
- website : string ;
19
- } ;
6
+ type Uri = Record < string , string > ;
20
7
21
8
type State = {
22
9
uri : Uri | undefined ;
23
10
} ;
11
+
12
+ function matchAll ( pattern : RegExp , haystack : string ) {
13
+ const regex = new RegExp ( pattern , "g" ) ;
14
+ const matches : any [ ] = [ ] ;
15
+
16
+ const match_result = haystack . match ( regex ) ;
17
+
18
+ for ( const index in match_result ) {
19
+ const item = match_result [ index as unknown as number ] ;
20
+ matches [ index as unknown as number ] = item . match ( new RegExp ( pattern ) ) ;
21
+ }
22
+
23
+ return matches ;
24
+ }
25
+
24
26
export default class WebJsInjector extends React . PureComponent <
25
27
PropsWithChildren < { } > ,
26
28
State
@@ -40,14 +42,22 @@ export default class WebJsInjector extends React.PureComponent<
40
42
return value . replace ( / \^ \{ j s d e l i v e r _ w e b j s _ [ ^ } ] + \} / g, "" ) ;
41
43
}
42
44
43
- const uri = this . state . uri ;
44
- return Object . keys ( uri ) . reduce ( ( acc , key ) => {
45
- acc = acc . replace (
45
+ // get all the keys from the mentions
46
+ const keys = matchAll ( / \^ \{ j s d e l i v e r _ w e b j s _ ( [ ^ } ] + ) \} / , value ) . map (
47
+ ( match ) => {
48
+ return match [ 1 ] ;
49
+ }
50
+ ) ;
51
+
52
+ // replace all the mentions with the corresponding uri
53
+ keys . forEach ( ( key ) => {
54
+ value = value . replace (
46
55
new RegExp ( `\\^\\{jsdeliver_webjs_${ key } \\}` , "g" ) ,
47
- uri [ key as keyof State [ "uri" ] ]
56
+ this . state . uri ?. [ key ] || ""
48
57
) ;
49
- return acc ;
50
- } , value ) ;
58
+ } ) ;
59
+
60
+ return value ;
51
61
}
52
62
53
63
render ( ) {
@@ -63,38 +73,26 @@ export default class WebJsInjector extends React.PureComponent<
63
73
64
74
async componentDidMount ( ) {
65
75
if ( typeof window != "undefined" ) {
66
- if ( MOCK_ENABLED ) {
76
+ if ( MOCK_ENABLED || window . location . hostname === "test.supertokens.com" ) {
67
77
if ( this . isUnmounting ) {
68
78
return ;
69
79
}
80
+
81
+ const proxy = new Proxy (
82
+ { } ,
83
+ {
84
+ get ( target , name , receiver ) {
85
+ return `https://cdn.jsdelivr.net/gh/supertokens/[email protected] /bundle/${ String (
86
+ name
87
+ ) } .test.js`;
88
+ } ,
89
+ }
90
+ ) ;
91
+
70
92
this . setState ( ( oldState ) => {
71
93
return {
72
94
...oldState ,
73
- uri : {
74
- dateprovider :
75
- "https://cdn.jsdelivr.net/gh/supertokens/[email protected] /bundle/dateprovider.test.js" ,
76
- emailpassword :
77
- "https://cdn.jsdelivr.net/gh/supertokens/[email protected] /bundle/emailpassword.test.js" ,
78
- emailverification :
79
- "https://cdn.jsdelivr.net/gh/supertokens/[email protected] /bundle/emailverification.test.js" ,
80
- multifactorauth :
81
- "https://cdn.jsdelivr.net/gh/supertokens/[email protected] /bundle/multifactorauth.test.js" ,
82
- multitenancy :
83
- "https://cdn.jsdelivr.net/gh/supertokens/[email protected] /bundle/multitenancy.test.js" ,
84
- passwordless :
85
- "https://cdn.jsdelivr.net/gh/supertokens/[email protected] /bundle/passwordless.test.js" ,
86
- session :
87
- "https://cdn.jsdelivr.net/gh/supertokens/[email protected] /bundle/session.test.js" ,
88
- supertokens :
89
- "https://cdn.jsdelivr.net/gh/supertokens/[email protected] /bundle/supertokens.test.js" ,
90
- thirdparty :
91
- "https://cdn.jsdelivr.net/gh/supertokens/[email protected] /bundle/thirdparty.test.js" ,
92
- totp :
"https://cdn.jsdelivr.net/gh/supertokens/[email protected] /bundle/totp.test.js" ,
93
- userroles :
94
- "https://cdn.jsdelivr.net/gh/supertokens/[email protected] /bundle/userroles.test.js" ,
95
- website :
96
- "https://cdn.jsdelivr.net/gh/supertokens/[email protected] /bundle/website.test.js" ,
97
- } ,
95
+ uri : proxy ,
98
96
} ;
99
97
} ) ;
100
98
} else {
0 commit comments