Template
1
0

feat: modular domain driven boilerplate

This commit is contained in:
2025-09-22 01:29:55 +02:00
parent 2433f59d1a
commit 9be3230c84
160 changed files with 2468 additions and 1525 deletions

10
platform/config/dotenv.ts Normal file
View File

@@ -0,0 +1,10 @@
import { load } from "@std/dotenv";
const env = await load();
/**
* TODO ...
*/
export function getDotEnvVariable(key: string): string {
return env[key] ?? Deno.env.get(key);
}

View File

@@ -0,0 +1,51 @@
import { load } from "@std/dotenv";
import { z, type ZodType } from "zod";
import { InvalidEnvironmentKeyError } from "./errors.ts";
import { getServiceEnvironment, type ServiceEnvironment } from "./service.ts";
const env = await load();
/**
* TODO ...
*/
export function getEnvironmentVariable<TType extends ZodType>({
key,
type,
envFallback,
fallback,
}: {
key: string;
type: TType;
envFallback?: EnvironmentFallback;
fallback?: string;
}): z.infer<TType> {
const serviceEnv = getServiceEnvironment();
const providedValue = env[key] ?? Deno.env.get(key);
const fallbackValue = typeof envFallback === "object" ? (envFallback[serviceEnv] ?? fallback) : fallback;
const toBeUsed = providedValue ?? fallbackValue;
try {
if (typeof toBeUsed === "string" && (toBeUsed.trim().startsWith("{") || toBeUsed.trim().startsWith("["))) {
return type.parse(JSON.parse(toBeUsed));
}
return type.parse(toBeUsed);
} catch (error) {
throw new InvalidEnvironmentKeyError(key, {
cause: error,
});
}
}
/*
|--------------------------------------------------------------------------------
| Types
|--------------------------------------------------------------------------------
*/
type EnvironmentFallback = Partial<Record<ServiceEnvironment, string>> & {
testing?: string;
local?: string;
stg?: string;
demo?: string;
prod?: string;
};

22
platform/config/errors.ts Normal file
View File

@@ -0,0 +1,22 @@
import { SERVICE_ENV } from "./service.ts";
export class InvalidServiceEnvironmentError extends Error {
readonly code = "INVALID_SERVICE_ENVIRONMENT";
constructor(value: string) {
super(
`@platform/config requested invalid service environment, expected '${SERVICE_ENV.join(", ")}' got '${value}'.`,
);
}
}
export class InvalidEnvironmentKeyError extends Error {
readonly code = "INVALID_ENVIRONMENT_KEY";
constructor(
key: string,
readonly details: unknown,
) {
super(`@platform/config invalid environment key '${key}' provided.`);
}
}

View File

@@ -0,0 +1,10 @@
{
"name": "@platform/config",
"version": "0.0.0",
"private": true,
"type": "module",
"dependencies": {
"@std/dotenv": "npm:@jsr/std__dotenv@0.225.5",
"zod": "4.1.11"
}
}

View File

@@ -0,0 +1,19 @@
import { getDotEnvVariable } from "./dotenv.ts";
export const SERVICE_ENV = ["testing", "local", "stg", "demo", "prod"] as const;
/**
* TODO ...
*/
export function getServiceEnvironment(): ServiceEnvironment {
const value = getDotEnvVariable("SERVICE_ENV");
if (value === undefined) {
return "local";
}
if ((SERVICE_ENV as unknown as string[]).includes(value) === false) {
throw new Error(`Config Exception: Invalid env ${value} provided`);
}
return value as ServiceEnvironment;
}
export type ServiceEnvironment = (typeof SERVICE_ENV)[number];