-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #855 from supertokens/webjs-uri-support
Added support for dynamic web-js library URI
- Loading branch information
Showing
13 changed files
with
237 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { API_URL } from "../constants"; | ||
import * as httpNetworking from "../httpNetworking"; | ||
|
||
const URL = API_URL + "/frontend/web-js"; | ||
const VERSION = 0; | ||
|
||
export default async function getURI(): Promise<{ | ||
uri: { | ||
dateprovider: string; | ||
emailpassword: string; | ||
emailverification: string; | ||
multifactorauth: string; | ||
multitenancy: string; | ||
passwordless: string; | ||
session: string; | ||
supertokens: string; | ||
thirdparty: string; | ||
totp: string; | ||
userroles: string; | ||
website: string; | ||
}; | ||
}> { | ||
let options: httpNetworking.GETRequestConfig = { | ||
timeout: 50000, | ||
}; | ||
|
||
let response = (await httpNetworking.simpleGETRequest(URL, options, VERSION)) | ||
.data; | ||
return response; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import React, { PropsWithChildren } from "react"; | ||
import { recursiveMap } from "../utils"; | ||
import getURI from "../api/webJs"; | ||
import { MOCK_ENABLED } from "../constants"; | ||
|
||
type Uri = { | ||
dateprovider: string; | ||
emailpassword: string; | ||
emailverification: string; | ||
multifactorauth: string; | ||
multitenancy: string; | ||
passwordless: string; | ||
session: string; | ||
supertokens: string; | ||
thirdparty: string; | ||
totp: string; | ||
userroles: string; | ||
website: string; | ||
}; | ||
|
||
type State = { | ||
uri: Uri | undefined; | ||
}; | ||
export default class WebJsInjector extends React.PureComponent< | ||
PropsWithChildren<{}>, | ||
State | ||
> { | ||
isUnmounting = false; | ||
|
||
constructor(props: PropsWithChildren<{}>) { | ||
super(props); | ||
this.state = { | ||
uri: undefined, | ||
}; | ||
} | ||
|
||
replaceUrisByKeys(value: string) { | ||
if (!this.state.uri) { | ||
// replace all webjs mentions with empty string; | ||
return value.replace(/\^\{jsdeliver_webjs_[^}]+\}/g, ""); | ||
} | ||
|
||
const uri = this.state.uri; | ||
return Object.keys(uri).reduce((acc, key) => { | ||
acc = acc.replace( | ||
new RegExp(`\\^\\{jsdeliver_webjs_${key}\\}`, "g"), | ||
uri[key as keyof State["uri"]] | ||
); | ||
return acc; | ||
}, value); | ||
} | ||
|
||
render() { | ||
let result = recursiveMap(this.props.children, (c: any) => { | ||
if (typeof c === "string") { | ||
c = this.replaceUrisByKeys(c); | ||
} | ||
return c; | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
async componentDidMount() { | ||
if (typeof window != "undefined") { | ||
if (MOCK_ENABLED) { | ||
if (this.isUnmounting) { | ||
return; | ||
} | ||
this.setState((oldState) => { | ||
return { | ||
...oldState, | ||
uri: { | ||
dateprovider: | ||
"https://cdn.jsdelivr.net/gh/supertokens/[email protected]/bundle/dateprovider.test.js", | ||
emailpassword: | ||
"https://cdn.jsdelivr.net/gh/supertokens/[email protected]/bundle/emailpassword.test.js", | ||
emailverification: | ||
"https://cdn.jsdelivr.net/gh/supertokens/[email protected]/bundle/emailverification.test.js", | ||
multifactorauth: | ||
"https://cdn.jsdelivr.net/gh/supertokens/[email protected]/bundle/multifactorauth.test.js", | ||
multitenancy: | ||
"https://cdn.jsdelivr.net/gh/supertokens/[email protected]/bundle/multitenancy.test.js", | ||
passwordless: | ||
"https://cdn.jsdelivr.net/gh/supertokens/[email protected]/bundle/passwordless.test.js", | ||
session: | ||
"https://cdn.jsdelivr.net/gh/supertokens/[email protected]/bundle/session.test.js", | ||
supertokens: | ||
"https://cdn.jsdelivr.net/gh/supertokens/[email protected]/bundle/supertokens.test.js", | ||
thirdparty: | ||
"https://cdn.jsdelivr.net/gh/supertokens/[email protected]/bundle/thirdparty.test.js", | ||
totp: "https://cdn.jsdelivr.net/gh/supertokens/[email protected]/bundle/totp.test.js", | ||
userroles: | ||
"https://cdn.jsdelivr.net/gh/supertokens/[email protected]/bundle/userroles.test.js", | ||
website: | ||
"https://cdn.jsdelivr.net/gh/supertokens/[email protected]/bundle/website.test.js", | ||
}, | ||
}; | ||
}); | ||
} else { | ||
try { | ||
let uri = await getURI(); | ||
if (this.isUnmounting) { | ||
return; | ||
} | ||
this.setState((oldState) => { | ||
return { | ||
...oldState, | ||
uri: uri.uri, | ||
}; | ||
}); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
this.isUnmounting = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.