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

@@ -12,6 +12,7 @@ import {
Default,
DataType,
} from "sequelize-typescript";
import env from "@server/env";
import Team from "./Team";
import User from "./User";
import Fix from "./decorators/Fix";
@@ -48,7 +49,7 @@ class NotificationSetting extends Model {
get unsubscribeUrl() {
const token = NotificationSetting.getUnsubscribeToken(this.userId);
return `${process.env.URL}/api/notificationSettings.unsubscribe?token=${token}&id=${this.id}`;
return `${env.URL}/api/notificationSettings.unsubscribe?token=${token}&id=${this.id}`;
}
get unsubscribeToken() {
@@ -73,7 +74,7 @@ class NotificationSetting extends Model {
static getUnsubscribeToken = (userId: string) => {
const hash = crypto.createHash("sha256");
hash.update(`${userId}-${process.env.SECRET_KEY}`);
hash.update(`${userId}-${env.SECRET_KEY}`);
return hash.digest("hex");
};
}

View File

@@ -117,7 +117,7 @@ class Team extends ParanoidModel {
*/
get emailSigninEnabled(): boolean {
return (
this.guestSignin && (!!env.SMTP_HOST || env.NODE_ENV === "development")
this.guestSignin && (!!env.SMTP_HOST || env.ENVIRONMENT === "development")
);
}
@@ -126,11 +126,11 @@ class Team extends ParanoidModel {
return `https://${this.domain}`;
}
if (!this.subdomain || process.env.SUBDOMAINS_ENABLED !== "true") {
return process.env.URL;
if (!this.subdomain || !env.SUBDOMAINS_ENABLED) {
return env.URL;
}
const url = new URL(process.env.URL || "");
const url = new URL(env.URL);
url.host = `${this.subdomain}.${stripSubdomain(url.host)}`;
return url.href.replace(/\/$/, "");
}

View File

@@ -23,8 +23,8 @@ import {
import { v4 as uuidv4 } from "uuid";
import { languages } from "@shared/i18n";
import { stringToColor } from "@shared/utils/color";
import env from "@server/env";
import Logger from "@server/logging/logger";
import { DEFAULT_AVATAR_HOST } from "@server/utils/avatars";
import { publicS3Endpoint, uploadToS3FromUrl } from "@server/utils/s3";
import { ValidationError } from "../errors";
import ApiKey from "./ApiKey";
@@ -137,7 +137,7 @@ class User extends ParanoidModel {
@Column(DataType.JSONB)
flags: { [key in UserFlag]?: number } | null;
@Default(process.env.DEFAULT_LANGUAGE)
@Default(env.DEFAULT_LANGUAGE)
@IsIn([languages])
@Column
language: string;
@@ -156,7 +156,7 @@ class User extends ParanoidModel {
.createHash("md5")
.update(this.email || "")
.digest("hex");
return `${DEFAULT_AVATAR_HOST}/avatar/${hash}/${initial}.png?c=${color}`;
return `${env.DEFAULT_AVATAR_HOST}/avatar/${hash}/${initial}.png?c=${color}`;
}
set avatarUrl(value: string | null) {
@@ -439,7 +439,7 @@ class User extends ParanoidModel {
avatarUrl &&
!avatarUrl.startsWith("/api") &&
!avatarUrl.startsWith(endpoint) &&
!avatarUrl.startsWith(DEFAULT_AVATAR_HOST)
!avatarUrl.startsWith(env.DEFAULT_AVATAR_HOST)
) {
try {
const newUrl = await uploadToS3FromUrl(

View File

@@ -0,0 +1,14 @@
/* eslint-disable @typescript-eslint/ban-types */
const Deprecated = (message?: string) => (
target: Object,
propertyKey: string
) => {
if (process.env[propertyKey]) {
console.warn(
`The environment variable ${propertyKey} is deprecated and will be removed in a future release. ${message}`
);
}
};
export default Deprecated;