@astralarium/repo-env 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,3 +2,19 @@
2
2
 
3
3
  Validated Wrangler bindings shared by workers.
4
4
  Apps spread `coreEnvironment(config)` into their environment.
5
+
6
+ ```ts
7
+ export const appSiteOrigin = createIsomorphicFn()
8
+ .server(() => {
9
+ let requestOrigin: string | undefined;
10
+ try {
11
+ requestOrigin = getRequestUrl().origin;
12
+ } catch {
13
+ /* Outside a request: prerender, startup. */
14
+ }
15
+ return siteOrigin(publicWebConfig(env), requestOrigin);
16
+ })
17
+ .client(() => window.location.origin);
18
+
19
+ const sql = postgres(databaseConnectionString(env)); // DB_APP Hyperdrive
20
+ ```
package/dist/index.d.ts CHANGED
@@ -47,5 +47,18 @@ export declare function publicWebConfig(env: PublicWebEnvironment): {
47
47
  hostnames: string[];
48
48
  };
49
49
  };
50
+ /** Origin the site answers on: `requestOrigin` when it is one of the local
51
+ * origins, else the configured site origin. */
52
+ export declare function siteOrigin(config: ReturnType<typeof publicWebConfig>, requestOrigin?: string): string;
50
53
  /** Validated process environment every worker shares. */
51
54
  export declare function coreEnvironment(config: CoreConfig): CoreEnvironment;
55
+ /** Bindings carrying the app database's Hyperdrive. */
56
+ export interface DatabaseEnvironment {
57
+ /** App database Hyperdrive; unbound during static prerender. */
58
+ DB_APP?: {
59
+ connectionString: string;
60
+ };
61
+ }
62
+ /** The `DB_APP` Hyperdrive connection string. Throws while the binding is
63
+ * absent (static prerender). */
64
+ export declare function databaseConnectionString(env: DatabaseEnvironment): string;
package/dist/index.js CHANGED
@@ -50,6 +50,13 @@ export function publicWebConfig(env) {
50
50
  localOrigins: parseLocalOrigins(env.LOCAL_ORIGINS),
51
51
  };
52
52
  }
53
+ /** Origin the site answers on: `requestOrigin` when it is one of the local
54
+ * origins, else the configured site origin. */
55
+ export function siteOrigin(config, requestOrigin) {
56
+ return requestOrigin && config.localOrigins.origins.includes(requestOrigin)
57
+ ? requestOrigin
58
+ : config.siteOrigin;
59
+ }
53
60
  /** Validated process environment every worker shares. */
54
61
  export function coreEnvironment(config) {
55
62
  if (config.authSecret.length < 32)
@@ -71,3 +78,10 @@ export function coreEnvironment(config) {
71
78
  LOCAL_ORIGINS: parseLocalOrigins(config.localOrigins?.join(",")).origins.join(","),
72
79
  };
73
80
  }
81
+ /** The `DB_APP` Hyperdrive connection string. Throws while the binding is
82
+ * absent (static prerender). */
83
+ export function databaseConnectionString(env) {
84
+ if (!env.DB_APP)
85
+ throw new Error("DB_APP is unavailable: database access requires runtime bindings");
86
+ return env.DB_APP.connectionString;
87
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astralarium/repo-env",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "license": "MIT",
5
5
  "homepage": "https://github.com/astralarium/repo/tree/main/packages/repo-env#readme",
6
6
  "repository": {
@@ -20,16 +20,16 @@
20
20
  "access": "public"
21
21
  },
22
22
  "dependencies": {
23
- "@astralarium/repo-telemetry": "^0.1.0"
23
+ "@astralarium/repo-telemetry": "^0.2.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/node": "^24.2.0",
27
27
  "eslint": "^10.2.1",
28
28
  "typescript": "^6.0.3",
29
29
  "vitest": "^5.0.3",
30
- "@astralarium/repo-config-typescript": "^0.1.0",
31
- "@astralarium/repo-config-eslint": "^0.1.0",
32
- "@astralarium/repo-package-files": "^0.1.0"
30
+ "@astralarium/repo-package-files": "^0.1.0",
31
+ "@astralarium/repo-config-eslint": "^0.3.0",
32
+ "@astralarium/repo-config-typescript": "^0.1.0"
33
33
  },
34
34
  "engines": {
35
35
  "node": ">=24.2"