-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split single huge main.ts to multiple files
comment 相关的逻辑比较耦合,于是就没有继续拆
- Loading branch information
1 parent
fa53d48
commit af73614
Showing
10 changed files
with
1,210 additions
and
1,108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { GitHubMeta, JWTPayload } from "./types"; | ||
import { apiEndpoint } from "./const"; | ||
|
||
export let githubMeta: GitHubMeta; | ||
|
||
export const _fetchGitHubMeta = async () => { | ||
const res = await fetch(`${apiEndpoint}meta/github-app`, { | ||
method: "GET", | ||
}); | ||
|
||
if (!res.ok) { | ||
throw res; | ||
} | ||
|
||
if (!githubMeta) githubMeta = (await res.json()).data; | ||
}; | ||
|
||
export const _handleOAuthToken = () => { | ||
const url = new URL(window.location.href); | ||
const token = url.searchParams.get("oauth_token"); | ||
if (!token) return; | ||
document.cookie = `oauth_token=${token}; path=/; expires=${new Date(JSON.parse(atob(token.split(".")[1])).exp * 1000).toUTCString()}; secure`; | ||
url.searchParams.delete("oauth_token"); | ||
window.history.replaceState(null, "", url.toString()); | ||
}; | ||
|
||
export const _getJWT = () => { | ||
// https://developer.mozilla.org/zh-CN/docs/Web/API/Document/cookie#%E7%A4%BA%E4%BE%8B_2_%E5%BE%97%E5%88%B0%E5%90%8D%E4%B8%BA_test2_%E7%9A%84_cookie | ||
return document.cookie.replace( | ||
/(?:(?:^|.*;\s*)oauth_token\s*\=\s*([^;]*).*$)|^.*$/, | ||
"$1", | ||
); | ||
}; | ||
|
||
export const _decodeJWT = () => { | ||
const jwt = _getJWT(); | ||
if (!jwt) return; | ||
const raw = jwt.split(".")[1]; | ||
|
||
const bytes = Array.from(atob(raw), (char) => char.charCodeAt(0)); | ||
const decodedString = new TextDecoder("utf-8").decode(new Uint8Array(bytes)); | ||
return JSON.parse(decodedString) as JWTPayload; | ||
}; | ||
|
||
export const _logout = () => { | ||
document.cookie = | ||
"oauth_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; secure"; | ||
}; |
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,4 @@ | ||
export let apiEndpoint = "/api"; | ||
export const setApiEndpoint = (endpoint: string) => { | ||
apiEndpoint = endpoint; | ||
}; |
Oops, something went wrong.