Share env vars client-side using @Public decorator (#6627)

* fix: public env vars using decorator

* fix: relocate

* fix: use env.public

* fix: register public env vars across plugins

* fix: test

* fix: tsc

* fix: mark remaining ones as public

* fix: move oidc ones to plugin

* fix: prevent overwrite

* fix: review
This commit is contained in:
Apoorv Mishra
2024-03-09 14:48:59 +05:30
committed by GitHub
parent 0983dd91b6
commit 34e8a64b50
13 changed files with 119 additions and 93 deletions

View File

@@ -18,6 +18,7 @@ import { languages } from "@shared/i18n";
import { CannotUseWithout } from "@server/utils/validators";
import Deprecated from "./models/decorators/Deprecated";
import { getArg } from "./utils/args";
import { Public, PublicEnvironmentRegister } from "./utils/decorators/Public";
export class Environment {
constructor() {
@@ -34,11 +35,21 @@ export class Environment {
}
});
});
PublicEnvironmentRegister.registerEnv(this);
}
/**
* Returns an object consisting of env vars annotated with `@Public` decorator
*/
get public() {
return PublicEnvironmentRegister.getEnv();
}
/**
* The current environment name.
*/
@Public
@IsIn(["development", "production", "staging", "test"])
public ENVIRONMENT = environment.NODE_ENV ?? "production";
@@ -121,13 +132,14 @@ export class Environment {
/**
* The fully qualified, external facing domain name of the server.
*/
@Public
@IsNotEmpty()
@IsUrl({
protocols: ["http", "https"],
require_protocol: true,
require_tld: false,
})
public URL = environment.URL || "";
public URL = (environment.URL ?? "").replace(/\/$/, "");
/**
* If using a Cloudfront/Cloudflare distribution or similar it can be set below.
@@ -135,27 +147,31 @@ export class Environment {
* the hostname defined in CDN_URL. In your CDN configuration the origin server
* should be set to the same as URL.
*/
@Public
@IsOptional()
@IsUrl({
protocols: ["http", "https"],
require_protocol: true,
require_tld: false,
})
public CDN_URL = this.toOptionalString(environment.CDN_URL);
public CDN_URL = this.toOptionalString(
environment.CDN_URL ? environment.CDN_URL.replace(/\/$/, "") : undefined
);
/**
* The fully qualified, external facing domain name of the collaboration
* service, if different (unlikely)
*/
@Public
@IsUrl({
require_tld: false,
require_protocol: true,
protocols: ["http", "https", "ws", "wss"],
})
@IsOptional()
public COLLABORATION_URL = this.toOptionalString(
environment.COLLABORATION_URL
);
public COLLABORATION_URL = (environment.COLLABORATION_URL || this.URL)
.replace(/\/$/, "")
.replace(/^http/, "ws");
/**
* The maximum number of network clients that can be connected to a single
@@ -221,6 +237,7 @@ export class Environment {
* The default interface language. See translate.getoutline.com for a list of
* available language codes and their percentage translated.
*/
@Public
@IsIn(languages)
public DEFAULT_LANGUAGE = environment.DEFAULT_LANGUAGE ?? "en_US";
@@ -270,6 +287,9 @@ export class Environment {
*/
public SMTP_HOST = environment.SMTP_HOST;
@Public
public EMAIL_ENABLED = !!this.SMTP_HOST || this.isDevelopment;
/**
* Optional hostname of the client, used for identifying to the server
* defaults to hostname of the machine.
@@ -325,6 +345,7 @@ export class Environment {
/**
* Sentry DSN for capturing errors and frontend performance.
*/
@Public
@IsUrl()
@IsOptional()
public SENTRY_DSN = this.toOptionalString(environment.SENTRY_DSN);
@@ -332,6 +353,7 @@ export class Environment {
/**
* Sentry tunnel URL for bypassing ad blockers
*/
@Public
@IsUrl()
@IsOptional()
public SENTRY_TUNNEL = this.toOptionalString(environment.SENTRY_TUNNEL);
@@ -344,6 +366,7 @@ export class Environment {
/**
* A Google Analytics tracking ID, supports v3 or v4 properties.
*/
@Public
@IsOptional()
public GOOGLE_ANALYTICS_ID = this.toOptionalString(
environment.GOOGLE_ANALYTICS_ID
@@ -359,44 +382,13 @@ export class Environment {
*/
public DD_SERVICE = environment.DD_SERVICE ?? "outline";
@IsOptional()
public SLACK_CLIENT_ID = this.toOptionalString(
environment.SLACK_CLIENT_ID ?? environment.SLACK_KEY
);
/**
* Injected into the `slack-app-id` header meta tag if provided.
*/
@IsOptional()
@CannotUseWithout("SLACK_CLIENT_ID")
public SLACK_APP_ID = this.toOptionalString(environment.SLACK_APP_ID);
/**
* Disable autoredirect to the OIDC login page if there is only one
* authentication method and that method is OIDC.
*/
@IsOptional()
@IsBoolean()
public OIDC_DISABLE_REDIRECT = this.toOptionalBoolean(
environment.OIDC_DISABLE_REDIRECT
);
/**
* The OIDC logout endpoint.
*/
@IsOptional()
@IsUrl({
require_tld: false,
allow_underscores: true,
})
public OIDC_LOGOUT_URI = this.toOptionalString(environment.OIDC_LOGOUT_URI);
/**
* A string representing the version of the software.
*
* SOURCE_COMMIT is used by Docker Hub
* SOURCE_VERSION is used by Heroku
*/
@Public
public VERSION = this.toOptionalString(
environment.SOURCE_COMMIT || environment.SOURCE_VERSION
);
@@ -477,14 +469,14 @@ export class Environment {
/**
* Optional AWS S3 endpoint URL for file attachments.
*/
@Public
@IsOptional()
public AWS_S3_ACCELERATE_URL = this.toOptionalString(
environment.AWS_S3_ACCELERATE_URL
);
public AWS_S3_ACCELERATE_URL = environment.AWS_S3_ACCELERATE_URL ?? "";
/**
* Optional AWS S3 endpoint URL for file attachments.
*/
@Public
@IsOptional()
public AWS_S3_UPLOAD_BUCKET_URL = environment.AWS_S3_UPLOAD_BUCKET_URL ?? "";
@@ -536,6 +528,7 @@ export class Environment {
/**
* Set max allowed upload size for document imports.
*/
@Public
@IsNumber()
public FILE_STORAGE_IMPORT_MAX_SIZE =
this.toOptionalNumber(environment.FILE_STORAGE_IMPORT_MAX_SIZE) ??
@@ -586,6 +579,7 @@ export class Environment {
/**
* The product name
*/
@Public
public APP_NAME = "Outline";
/**