Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
"type": "node",
"request": "launch",
"runtimeExecutable": "bun",
"runtimeArgs": ["--inspect-wait", "run", "test"],
"runtimeArgs": [
"--inspect-wait",
"run",
"test",
"config-builder.test.ts"
],
"cwd": "${workspaceFolder}/packages/config",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
Expand Down
46 changes: 36 additions & 10 deletions packages/config/src/config-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,39 @@ import { ObjectSource } from "./sources/object";

const builders = [
{ ConfigBuilder: ClientModule.ConfigBuilder, mode: "Client" },
{
ConfigBuilder: ServerModule.ConfigBuilder,
mode: "Server",
},
// {
// ConfigBuilder: ServerModule.ConfigBuilder,
// mode: "Server",
// },
];

describe.each(builders)(
"[Shared Features] ConfigBuilder ($mode)",
({ ConfigBuilder }) => {
it.only("foo", () => {
expect(
new ConfigBuilder({
validate: (finalConfig) => finalConfig,
runtimeEnv: {
PORT_2: 8888,
},
})
.addSource(
new ObjectSource({
test: true,
port: "${PORT}",
alternativePort: "${self.port::-3001}",
alternativePort2: "${ALTERNATIVE_PORT::self.randomPort::PORT_2}",
}),
)
.build(),
).toEqual({
port: undefined,
alternativePort: "3001",
alternativePort2: "8888",
});
});

it("should build config correctly (complex case)", () => {
const config = new ConfigBuilder({
validate: (finalConfig) => finalConfig,
Expand All @@ -36,10 +60,11 @@ describe.each(builders)(
.addSource(
new ObjectSource({
server: {
port: "${PORT}",
test: "${self.security.jwtSecret}",
port: "${PORT::-8080}",
hostname: "${HOSTNAME::-localhost}",
protocol: "${PROTOCOL::SCHEME::-http}",
baseUrl: "${BASE_URL::self.server.hostname}:${PORT::-8080}",
baseUrl: "${BASE_URL::self.server.hostname}:${self.server.port}",
endpoints: [
{
name: "health",
Expand All @@ -56,9 +81,9 @@ describe.each(builders)(
database: {
host: "${DB_HOST::PRIMARY_DB_HOST::SECONDARY_DB_HOST::-db.local}",
port: "${DB_PORT::-5432}",
name: "${DB_NAME::-appdb}",
user: "${DB_USER::-appuser}",
password: "${DB_PASS::-secret}",
name: "${DB_NAME::-appdb}",
pool: {
min: 2,
max: "${DB_POOL_MAX::-10}",
Expand Down Expand Up @@ -101,7 +126,7 @@ describe.each(builders)(
},
},
selfReference: {
port: "${PORT}",
port: "${self.server.port}",
hostname: "${HOSTNAME::-localhost}",
url: "${self.selfReference.hostname}:${self.selfReference.port}",
},
Expand All @@ -111,19 +136,20 @@ describe.each(builders)(

expect(config).toEqual({
server: {
test: "changeme",
port: "3000",
protocol: "https",
hostname: "app.example.com",
protocol: "https",
baseUrl: "app.example.com:3000",
endpoints: [
{ name: "health", path: "/health", enabled: true },
{ name: "metrics", path: "/metrics", enabled: "true" },
],
},
database: {
host: "primary.db.example.com",
port: "5432",
user: "appuser",
host: "primary.db.example.com",
password: "supersecret",
name: "appdb",
pool: {
Expand Down
145 changes: 36 additions & 109 deletions packages/config/src/sources/source.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { get } from "es-toolkit/compat";
import { flattenObject } from "es-toolkit";
import { get, set } from "es-toolkit/compat";
import type {
ClientConfigBuilderOptions,
Prettify,
RuntimeEnvValue,
ServerConfigBuilderOptions,
UnknownArray,
UnknownRecord,
} from "../types";
import { extractSlotsFromExpression, hasSlot, type Slot } from "../utils/slot";

const UNDEFINED_MARKER = "___UNDEFINED_MARKER___" as const;
import {
type ExtractedSlotReturn,
extractSlotsFromExpression,
hasSelfReference,
hasSlot,
type SelfReferenceSlot,
} from "../utils/slot";

type ObjectPath = string;
type FlattenObjectValue = string | boolean | number;
type FlattenedObject = Record<ObjectPath, FlattenObjectValue>;

export abstract class Source<T = Record<string, unknown>> {
/**
Expand All @@ -21,122 +29,41 @@ export abstract class Source<T = Record<string, unknown>> {
abstract loadSource(loadSourceOptions: LoadSourceOptions): Prettify<T>;

maybeReplaceSlots<T>(options: MaybeReplaceSlotsOptions<T>) {
// debugger;
const initialObject = options.transform(options.contentString);

/**
* If there's no slot, we don't need to do anything
*/
if (!hasSlot(options.contentString, options.slotPrefix)) {
return initialObject;
}

const slots = this.#extractSlots(
initialObject as UnknownRecord,
options.slotPrefix,
);

let updatedContentString = options.contentString;

for (const slot of slots) {
let envVarValue: RuntimeEnvValue;

for (const reference of slot.references) {
if (reference.type === "env_var") {
envVarValue = options.runtimeEnv[reference.envVar];
}

if (reference.type === "self_reference") {
const partialObj = options.transform(updatedContentString);

envVarValue = get(
partialObj,
reference.propertyPath,
) as RuntimeEnvValue;
}

if (envVarValue !== null && envVarValue !== undefined) {
// If we found a value for the env var, we can stop looking
break;
}
}

if (!envVarValue && slot.fallbackValue) {
envVarValue = slot.fallbackValue;
}

updatedContentString = updatedContentString.replaceAll(
slot.slotMatch,
envVarValue !== null && envVarValue !== undefined
? String(envVarValue)
: UNDEFINED_MARKER,
);
}

const partialConfig = this.#cleanUndefinedMarkers(
options.transform(updatedContentString),
);

return partialConfig;
}

#extractSlots(
value: UnknownRecord | UnknownArray,
slotPrefix: string,
): Slot[] {
const result: Slot[] = [];

if (Array.isArray(value)) {
for (const item of value) {
result.push(...this.#extractSlots(item as UnknownRecord, slotPrefix));
}
} else if (typeof value === "string") {
result.push(...extractSlotsFromExpression(value, slotPrefix));
} else if (value && typeof value === "object") {
for (const [_, v] of Object.entries(value)) {
if (typeof v === "string") {
result.push(...extractSlotsFromExpression(v, slotPrefix));
} else {
result.push(...this.#extractSlots(v as UnknownRecord, slotPrefix));
}
}
}

return result;
}

#cleanUndefinedMarkers<T = unknown>(value: T): any {
if (value === UNDEFINED_MARKER) {
return undefined;
}

if (typeof value === "string" && value.includes(UNDEFINED_MARKER)) {
// If it's mixed content with undefined slots, return undefined
return undefined;
}

if (Array.isArray(value)) {
const newList: any[] = [];

for (const item of value) {
const cleanedItem = this.#cleanUndefinedMarkers(item);
if (cleanedItem !== undefined) {
newList.push(cleanedItem);
}
const flattenedObject = flattenObject(initialObject as UnknownRecord);

const propsWithoutSlots: FlattenedObject = {};
const propsWithSlots: FlattenedObject = {};
const slotsObjs: Map<string, ExtractedSlotReturn> = new Map();

for (const [key, value] of Object.entries(flattenedObject) as [
string,
FlattenObjectValue,
][]) {
if (!hasSlot(value.toString(), options.slotPrefix)) {
propsWithoutSlots[key] = value;
} else {
propsWithSlots[key] = value;

slotsObjs.set(
key,
extractSlotsFromExpression(value, options.slotPrefix),
);
}

return newList;
}

if (value && typeof value === "object") {
const result: UnknownRecord = {};

for (const [oKey, oValue] of Object.entries(value)) {
result[oKey] = this.#cleanUndefinedMarkers(oValue);
}

return result;
}
const flattenedStr = JSON.stringify(propsWithSlots);

return value;
console.log(flattenedStr);
}
}

Expand Down
102 changes: 0 additions & 102 deletions packages/config/src/utils/slot.test.ts

This file was deleted.

Loading
Loading