Skip to content

Commit 0c7aeba

Browse files
Merge pull request #858 from supertokens/web-js-uri-mocks-for-test
Web-JS URI mocks for test docs
2 parents 1d66e2a + aab570b commit 0c7aeba

File tree

2 files changed

+45
-60
lines changed

2 files changed

+45
-60
lines changed

v2/src/components/api/webJs.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,7 @@ const URL = API_URL + "/frontend/web-js";
55
const VERSION = 0;
66

77
export default async function getURI(): Promise<{
8-
uri: {
9-
dateprovider: string;
10-
emailpassword: string;
11-
emailverification: string;
12-
multifactorauth: string;
13-
multitenancy: string;
14-
passwordless: string;
15-
session: string;
16-
supertokens: string;
17-
thirdparty: string;
18-
totp: string;
19-
userroles: string;
20-
website: string;
21-
};
8+
uri: Record<string, string>;
229
}> {
2310
let options: httpNetworking.GETRequestConfig = {
2411
timeout: 50000,

v2/src/components/webJsInjector/index.tsx

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,26 @@ import { recursiveMap } from "../utils";
33
import getURI from "../api/webJs";
44
import { MOCK_ENABLED } from "../constants";
55

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>;
207

218
type State = {
229
uri: Uri | undefined;
2310
};
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+
2426
export default class WebJsInjector extends React.PureComponent<
2527
PropsWithChildren<{}>,
2628
State
@@ -40,14 +42,22 @@ export default class WebJsInjector extends React.PureComponent<
4042
return value.replace(/\^\{jsdeliver_webjs_[^}]+\}/g, "");
4143
}
4244

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(/\^\{jsdeliver_webjs_([^}]+)\}/, 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(
4655
new RegExp(`\\^\\{jsdeliver_webjs_${key}\\}`, "g"),
47-
uri[key as keyof State["uri"]]
56+
this.state.uri?.[key] || ""
4857
);
49-
return acc;
50-
}, value);
58+
});
59+
60+
return value;
5161
}
5262

5363
render() {
@@ -63,38 +73,26 @@ export default class WebJsInjector extends React.PureComponent<
6373

6474
async componentDidMount() {
6575
if (typeof window != "undefined") {
66-
if (MOCK_ENABLED) {
76+
if (MOCK_ENABLED || window.location.hostname === "test.supertokens.com") {
6777
if (this.isUnmounting) {
6878
return;
6979
}
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+
7092
this.setState((oldState) => {
7193
return {
7294
...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,
9896
};
9997
});
10098
} else {

0 commit comments

Comments
 (0)