Skip to content

Commit

Permalink
refactor(web): config (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaWaite committed Jul 14, 2023
1 parent b1875e6 commit 2ebdf5f
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 133 deletions.
1 change: 0 additions & 1 deletion web/src/main.tsx
Expand Up @@ -13,7 +13,6 @@ window.React = React;
window.ReactDOM = ReactDOM;

loadConfig().finally(async () => {
await import("./services/config/aws.config");
const element = document.getElementById("root");
if (!element) throw new Error("root element is not found");

Expand Down
30 changes: 0 additions & 30 deletions web/src/services/config/aws.config.ts

This file was deleted.

47 changes: 47 additions & 0 deletions web/src/services/config/aws.ts
@@ -0,0 +1,47 @@
import { Amplify } from "aws-amplify";

import { Config } from ".";

export type CognitoParams = {
region?: string;
userPoolId?: string;
userPoolWebClientId?: string;
oauthScope?: string;
oauthDomain?: string;
oauthRedirectSignIn?: string;
oauthRedirectSignOut?: string;
oauthResponseType?: string;
};

export const configureCognito = (config: Config) => {
const cognitoConfig = config.cognito;
const authProvider = config.authProvider;

if (cognitoConfig && authProvider === "cognito") {
const cognitoRegion = cognitoConfig?.region;
const cognitoUserPoolId = cognitoConfig?.userPoolId;
const cognitoUserPoolWebClientId = cognitoConfig?.userPoolWebClientId;
const cognitoOauthScope = cognitoConfig?.oauthScope?.split(", ");
const cognitoOauthDomain = cognitoConfig?.oauthDomain;
const cognitoOauthRedirectSignIn = cognitoConfig?.oauthRedirectSignIn;
const cognitoOauthRedirectSignOut = cognitoConfig?.oauthRedirectSignOut;
const cognitoOauthResponseType = cognitoConfig?.oauthResponseType;

const config = {
Auth: {
region: cognitoRegion,
userPoolId: cognitoUserPoolId,
userPoolWebClientId: cognitoUserPoolWebClientId,
oauth: {
scope: cognitoOauthScope,
domain: cognitoOauthDomain,
redirectSignIn: cognitoOauthRedirectSignIn,
redirectSignOut: cognitoOauthRedirectSignOut,
responseType: cognitoOauthResponseType,
},
},
};

Amplify.configure(config);
}
};
2 changes: 1 addition & 1 deletion web/src/services/config/config.test.ts
@@ -1,6 +1,6 @@
import { expect, test } from "vitest";

import { convertPasswordPolicy } from ".";
import { convertPasswordPolicy } from "./passwordPolicy";

test("convert password policy to regular expression", () => {
const passwordPolicy = {
Expand Down
51 changes: 51 additions & 0 deletions web/src/services/config/defaultConfig.ts
@@ -0,0 +1,51 @@
import { Config } from ".";

export const defaultConfig: Config = {
api: "/api",
plugins: "/plugins",
published: window.origin + "/p/{}/",
auth0Audience: "http://localhost:8080",
auth0Domain: "http://localhost:8080",
auth0ClientId: "reearth-authsrv-client-default",
authProvider: "auth0",
policy: {
modalTitle: {
en: "Re:Earth Cloud",
ja: "Re:Earth Cloud",
},
modalDescription: {
en: "This is your currently subscribed to plan. If this plan stops meeting your needs, Re:Earth has other plans available. ",
ja: "日本語版にほなsdflkjlksdjf",
},
url: {
en: "https://reearth.io/service/cloud",
ja: "https://reearth.io/ja/service/cloud",
},
limitNotifications: {
asset: {
en: "Your workspace has reached its plan's limit for assets. Please check reearth.io/service/cloud for details.",
ja: "",
},
createProject: {
en: "Your workspace has reached its plan's limit for projects. Please check reearth.io/service/cloud for details.",
ja: "",
},
dataset: {
en: "Your workspace has reached its plan's limit for dataset. Please check reearth.io/service/cloud for details.",
ja: "",
},
member: {
en: "Your workspace has reached its plan's limit for new members. Please check reearth.io/service/cloud for details.",
ja: "",
},
layer: {
en: "Your workspace has reached its plan's limit for layers. Please check reearth.io/service/cloud for details.",
ja: "",
},
publishProject: {
en: "Your workspace has reached its plan's limit for publishing projects. Please check reearth.io/service/cloud for details.",
ja: "",
},
},
},
};
111 changes: 10 additions & 101 deletions web/src/services/config/index.ts
@@ -1,6 +1,9 @@
import { Viewer } from "cesium";

import { Extensions, loadExtensions } from "./extensions";
import { configureCognito, type CognitoParams } from "./aws";
import { defaultConfig } from "./defaultConfig";
import { type Extensions, loadExtensions } from "./extensions";
import { type PasswordPolicy, convertPasswordPolicy } from "./passwordPolicy";

export type Config = {
version?: string;
Expand All @@ -11,14 +14,7 @@ export type Config = {
auth0Domain?: string;
auth0Audience?: string;
authProvider?: string;
cognitoRegion?: string;
cognitoUserPoolId?: string;
cognitoUserPoolWebClientId?: string;
cognitoOauthScope?: string;
cognitoOauthDomain?: string;
cognitoOauthRedirectSignIn?: string;
cognitoOauthRedirectSignOut?: string;
cognitoOauthResponseType?: string;
cognito?: CognitoParams;
googleApiKey?: string;
googleClientId?: string;
sentryDsn?: string;
Expand All @@ -29,14 +25,7 @@ export type Config = {
logoUrl?: string;
background?: string;
};
passwordPolicy?: {
tooShort?: RegExp;
tooLong?: RegExp;
whitespace?: RegExp;
lowSecurity?: RegExp;
medSecurity?: RegExp;
highSecurity?: RegExp;
};
passwordPolicy?: PasswordPolicy;
ip?: string;
policy?: {
modalTitle: Record<string, string> | string;
Expand Down Expand Up @@ -66,90 +55,6 @@ declare global {
}
}

export const defaultConfig: Config = {
api: "/api",
plugins: "/plugins",
published: window.origin + "/p/{}/",
auth0Audience: "http://localhost:8080",
auth0Domain: "http://localhost:8080",
auth0ClientId: "reearth-authsrv-client-default",
authProvider: "auth0",
policy: {
modalTitle: {
en: "Re:Earth Cloud",
ja: "Re:Earth Cloud",
},
modalDescription: {
en: "This is your currently subscribed to plan. If this plan stops meeting your needs, Re:Earth has other plans available. ",
ja: "日本語版にほなsdflkjlksdjf",
},
url: {
en: "https://reearth.io/service/cloud",
ja: "https://reearth.io/ja/service/cloud",
},
limitNotifications: {
asset: {
en: "Your workspace has reached its plan's limit for assets. Please check reearth.io/service/cloud for details.",
ja: "",
},
createProject: {
en: "Your workspace has reached its plan's limit for projects. Please check reearth.io/service/cloud for details.",
ja: "",
},
dataset: {
en: "Your workspace has reached its plan's limit for dataset. Please check reearth.io/service/cloud for details.",
ja: "",
},
member: {
en: "Your workspace has reached its plan's limit for new members. Please check reearth.io/service/cloud for details.",
ja: "",
},
layer: {
en: "Your workspace has reached its plan's limit for layers. Please check reearth.io/service/cloud for details.",
ja: "",
},
publishProject: {
en: "Your workspace has reached its plan's limit for publishing projects. Please check reearth.io/service/cloud for details.",
ja: "",
},
},
},
};

export function convertPasswordPolicy(passwordPolicy?: {
[key: string]: string;
}): { [key: string]: RegExp | undefined } | undefined {
if (!passwordPolicy) return;
return Object.fromEntries(
Object.entries(passwordPolicy)
.map(([k, v]) => {
if (typeof v !== "string") return [k, undefined];
try {
return [k, new RegExp(v)];
} catch {
return [k, undefined];
}
})
.filter(i => !!i[1]),
);
}

// IIFE
// function importExternal(url: string) {
// return new Promise((res, rej) => {
// const script = document.createElement("script");
// script.src = url;
// script.async = true;
// script.onload = () => {
// if (!window.REEARTH_CONFIG) return;
// res(window.REEARTH_CONFIG.extensions);
// };
// script.onerror = rej;

// document.body.appendChild(script);
// });
// }

export default async function loadConfig() {
if (window.REEARTH_CONFIG) return;
window.REEARTH_CONFIG = defaultConfig;
Expand All @@ -158,6 +63,10 @@ export default async function loadConfig() {
...(await (await fetch("/reearth_config.json")).json()),
};

if (config?.cognito) {
configureCognito(config);
}

if (config?.passwordPolicy) {
config.passwordPolicy = convertPasswordPolicy(
config.passwordPolicy as { [key: string]: string },
Expand Down
26 changes: 26 additions & 0 deletions web/src/services/config/passwordPolicy.ts
@@ -0,0 +1,26 @@
export type PasswordPolicy = {
tooShort?: RegExp;
tooLong?: RegExp;
whitespace?: RegExp;
lowSecurity?: RegExp;
medSecurity?: RegExp;
highSecurity?: RegExp;
};

export function convertPasswordPolicy(passwordPolicy?: {
[key: string]: string;
}): { [key: string]: RegExp | undefined } | undefined {
if (!passwordPolicy) return;
return Object.fromEntries(
Object.entries(passwordPolicy)
.map(([k, v]) => {
if (typeof v !== "string") return [k, undefined];
try {
return [k, new RegExp(v)];
} catch {
return [k, undefined];
}
})
.filter(i => !!i[1]),
);
}

0 comments on commit 2ebdf5f

Please sign in to comment.