-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
54 lines (44 loc) · 1.33 KB
/
utils.ts
File metadata and controls
54 lines (44 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import {
CLIENT_APPS,
CONSUMER_LOGIN_PATH,
NEETO_URL_COMPONENT_REGEX,
NEETO_URL_PREFIX_REGEX,
TLD,
USER_LOGIN_PATH,
} from "./constants.js";
import { Scope } from "./types.js";
export type SearchParams = {
jwt: string;
redirect_uri: string;
client_app_name: string;
};
export const getLoginUri = (
workspace: string,
searchParams: SearchParams,
scope: Scope = "user"
) => {
const protocol =
process.env.NEETO_JWT_ENV === "development" ? "http" : "https";
const params = new URLSearchParams(searchParams).toString();
const path = scope === "consumer" ? CONSUMER_LOGIN_PATH : USER_LOGIN_PATH;
return `${protocol}://${workspace}${getTopLevelDomain()}${path}?${params}`;
};
export const getTopLevelDomain = () => {
const env: string = process.env.NEETO_JWT_ENV || "production";
return TLD[env] || ".com";
};
type App = keyof typeof CLIENT_APPS;
export const getClientAppName = (redirectUri: string) => {
const app = redirectUri.match(NEETO_URL_COMPONENT_REGEX)?.[1];
if (app && app in CLIENT_APPS) {
return CLIENT_APPS[app as App];
}
return "Cal";
};
export const getRedirectUri = (redirectUri: string) => {
const match = redirectUri.match(NEETO_URL_PREFIX_REGEX);
if (match) {
return encodeURI(redirectUri.replace(NEETO_URL_PREFIX_REGEX, ""));
}
return encodeURI(getTopLevelDomain());
};