chore: Centralize env parsing, validation, defaults, and deprecation notices (#3487)

* chore: Centralize env parsing, defaults, deprecation

* wip

* test

* test

* tsc

* docs, more validation

* fix: Allow empty REDIS_URL (defaults to localhost)

* test

* fix: SLACK_MESSAGE_ACTIONS not bool

* fix: Add SMTP port validation
This commit is contained in:
Tom Moor
2022-05-19 08:05:11 -07:00
committed by GitHub
parent 51001cfac1
commit 3c002f82cc
66 changed files with 783 additions and 341 deletions

View File

@@ -4,9 +4,8 @@ import en_US from "./locales/en_US/translation.json";
import pt_PT from "./locales/pt_PT/translation.json";
import { initI18n } from ".";
describe("i18n process.env is unset", () => {
describe("i18n env is unset", () => {
beforeEach(() => {
delete process.env.DEFAULT_LANGUAGE;
initI18n()
.addResources("en-US", "translation", en_US)
.addResources("de-DE", "translation", de_DE)
@@ -26,10 +25,9 @@ describe("i18n process.env is unset", () => {
expect(i18n.t("Saving")).toBe("A guardar");
});
});
describe("i18n process.env is en-US", () => {
describe("i18n env is en-US", () => {
beforeEach(() => {
process.env.DEFAULT_LANGUAGE = "en-US";
initI18n()
initI18n("en-US")
.addResources("en-US", "translation", en_US)
.addResources("de-DE", "translation", de_DE)
.addResources("pt-PT", "translation", pt_PT);
@@ -48,10 +46,10 @@ describe("i18n process.env is en-US", () => {
expect(i18n.t("Saving")).toBe("A guardar");
});
});
describe("i18n process.env is de-DE", () => {
describe("i18n env is de-DE", () => {
beforeEach(() => {
process.env.DEFAULT_LANGUAGE = "de-DE";
initI18n()
initI18n("de-DE")
.addResources("en-US", "translation", en_US)
.addResources("de-DE", "translation", de_DE)
.addResources("pt-PT", "translation", pt_PT);
@@ -70,10 +68,10 @@ describe("i18n process.env is de-DE", () => {
expect(i18n.t("Saving")).toBe("A guardar");
});
});
describe("i18n process.env is pt-PT", () => {
describe("i18n env is pt-PT", () => {
beforeEach(() => {
process.env.DEFAULT_LANGUAGE = "pt-PT";
initI18n()
initI18n("pt-PT")
.addResources("en-US", "translation", en_US)
.addResources("de-DE", "translation", de_DE)
.addResources("pt-PT", "translation", pt_PT);

View File

@@ -79,10 +79,8 @@ const underscoreToDash = (text: string) => text.replace("_", "-");
const dashToUnderscore = (text: string) => text.replace("-", "_");
export const initI18n = () => {
const lng = underscoreToDash(
"DEFAULT_LANGUAGE" in process.env ? process.env.DEFAULT_LANGUAGE! : "en_US"
);
export const initI18n = (defaultLanguage = "en_US") => {
const lng = underscoreToDash(defaultLanguage);
i18n
.use(backend)
.use(initReactI18next)
@@ -104,7 +102,6 @@ export const initI18n = () => {
fallbackLng: lng,
supportedLngs: languages.map(underscoreToDash),
// Uncomment when debugging translation framework, otherwise it's noisy
// debug: process.env.NODE_ENV === "development",
keySeparator: false,
});
return i18n;

View File

@@ -8,15 +8,16 @@ export type PublicEnv = {
COLLABORATION_URL: string;
AWS_S3_UPLOAD_BUCKET_URL: string;
AWS_S3_ACCELERATE_URL: string;
DEPLOYMENT: "hosted" | "";
ENVIRONMENT: "production" | "development";
DEPLOYMENT: string | undefined;
ENVIRONMENT: string;
SENTRY_DSN: string | undefined;
TEAM_LOGO: string | undefined;
SLACK_KEY: string | undefined;
SLACK_CLIENT_ID: string | undefined;
SLACK_APP_ID: string | undefined;
MAXIMUM_IMPORT_SIZE: number;
SUBDOMAINS_ENABLED: boolean;
EMAIL_ENABLED: boolean;
DEFAULT_LANGUAGE: string;
GOOGLE_ANALYTICS_ID: string | undefined;
RELEASE: string | undefined;
};

View File

@@ -1,3 +1,5 @@
import env from "../env";
export function slackAuth(
state: string,
scopes: string[] = [
@@ -6,9 +8,8 @@ export function slackAuth(
"identity.avatar",
"identity.team",
],
// @ts-expect-error ts-migrate(2322) FIXME: Type 'string | undefined' is not assignable to typ... Remove this comment to see the full error message
clientId: string = process.env.SLACK_KEY,
redirectUri = `${process.env.URL}/auth/slack.callback`
clientId: string,
redirectUri = `${env.URL}/auth/slack.callback`
): string {
const baseUrl = "https://slack.com/oauth/authorize";
const params = {
@@ -48,7 +49,7 @@ export function changelogUrl(): string {
}
export function signin(service = "slack"): string {
return `${process.env.URL}/auth/${service}`;
return `${env.URL}/auth/${service}`;
}
export const SLUG_URL_REGEX = /^(?:[0-9a-zA-Z-_~]*-)?([a-zA-Z0-9]{10,15})$/;