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:
37
server/utils/decorators/Public.ts
Normal file
37
server/utils/decorators/Public.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import "reflect-metadata";
|
||||
import isUndefined from "lodash/isUndefined";
|
||||
import type { Environment } from "@server/env";
|
||||
|
||||
const key = Symbol("env:public");
|
||||
|
||||
/**
|
||||
* This decorator on an environment variable makes that variable available client-side
|
||||
*/
|
||||
export function Public(target: any, propertyKey: string) {
|
||||
const publicVars: string[] = Reflect.getMetadata(key, target);
|
||||
|
||||
if (!publicVars) {
|
||||
return Reflect.defineMetadata(key, [propertyKey], target);
|
||||
}
|
||||
|
||||
publicVars.push(propertyKey);
|
||||
}
|
||||
|
||||
export class PublicEnvironmentRegister {
|
||||
private static publicEnv: Record<string, any> = {};
|
||||
|
||||
static registerEnv(env: Environment) {
|
||||
process.nextTick(() => {
|
||||
const vars: string[] = Reflect.getMetadata(key, env);
|
||||
(vars ?? []).forEach((key: string) => {
|
||||
if (isUndefined(this.publicEnv[key])) {
|
||||
this.publicEnv[key] = env[key];
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
static getEnv() {
|
||||
return this.publicEnv;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user