From 0e8347d6d3441b048ca0ce875ebca9263bb3fd0b Mon Sep 17 00:00:00 2001 From: Amiram_se Date: Tue, 6 Oct 2026 21:47:18 +0400 Subject: [PATCH] refactor(libs): share the origin env schema `origin` (scheme and host only) lives in @lumina/libs/env, so the apps stop repeating it. Co-Authored-By: Claude Opus 5.5 --- packages/libs/src/env/index.ts | 1 + packages/libs/src/env/schemas.ts | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 packages/libs/src/env/schemas.ts diff --git a/packages/libs/src/env/index.ts b/packages/libs/src/env/index.ts index 07ec9ba..1a947ed 100644 --- a/packages/libs/src/env/index.ts +++ b/packages/libs/src/env/index.ts @@ -1,4 +1,5 @@ export { createClientEnv } from './env.client'; +export { origin } from './schemas'; export type { ClientEnvOptions, EnvSchema, diff --git a/packages/libs/src/env/schemas.ts b/packages/libs/src/env/schemas.ts new file mode 100644 index 0000000..5a8251b --- /dev/null +++ b/packages/libs/src/env/schemas.ts @@ -0,0 +1,23 @@ +import * as v from 'valibot'; + +/** `https://api.lumina.ir`, not `https://api.lumina.ir/` or `https://api.lumina.ir/v1`. */ +function isOrigin(value: string): boolean { + try { + return new URL(value).origin === value; + } catch { + return false; + } +} + +/** + * A scheme and host only, such as `http://gateway:3000`: paths are joined to it, so a trailing + * slash or a path would give `//v1` or `/v1/v1`. + */ +export const origin = v.pipe( + v.string(), + v.url(), + v.check( + isOrigin, + 'Must be an origin such as https://api.example.com, with no path or trailing slash.', + ), +);