@vite-hub/env 0.0.0 → 0.0.1

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
@@ -1,3 +1,68 @@
1
1
  # @vite-hub/env
2
2
 
3
- Reserved package name for ViteHub.
3
+ <p>
4
+ <a href="https://vitehub.dev"><img alt="ViteHub" src="https://img.shields.io/badge/ViteHub-vitehub.dev-646cff?style=flat-square"></a>
5
+ <img alt="TypeScript" src="https://img.shields.io/badge/TypeScript-ready-3178c6?style=flat-square">
6
+ <img alt="Vite" src="https://img.shields.io/badge/Vite-public%20env-646cff?style=flat-square">
7
+ <img alt="Nitro" src="https://img.shields.io/badge/Nitro-server%20env-00dc82?style=flat-square">
8
+ </p>
9
+
10
+ `@vite-hub/env` declares environment values once, then generates typed public and server env access.
11
+
12
+ ## Install
13
+
14
+ ```sh
15
+ pnpm add @vite-hub/env
16
+ ```
17
+
18
+ ## Minimal API
19
+
20
+ ```ts
21
+ // vite.config.ts
22
+ import { env, envVite } from "@vite-hub/env/vite"
23
+ import { defineConfig } from "vite"
24
+
25
+ export default defineConfig({
26
+ plugins: [envVite({ prefix: "VITEHUB_" })],
27
+ env: {
28
+ public: {
29
+ appName: env({ default: "ViteHub App", mode: "build" }),
30
+ },
31
+ },
32
+ })
33
+ ```
34
+
35
+ ```ts
36
+ // nitro.config.ts
37
+ import { env, envNitro } from "@vite-hub/env/nitro"
38
+ import { defineNitroConfig } from "nitro/config"
39
+
40
+ export default defineNitroConfig({
41
+ modules: [envNitro()],
42
+ env: {
43
+ auth: {
44
+ token: env({ secret: true }),
45
+ },
46
+ },
47
+ })
48
+ ```
49
+
50
+ ```ts
51
+ // server/api/config.get.ts
52
+ import { useServerEnv } from "#vitehub/env/server"
53
+ import { defineEventHandler } from "h3"
54
+
55
+ export default defineEventHandler((event) => {
56
+ const env = useServerEnv(event)
57
+
58
+ return {
59
+ hasAuthToken: Boolean(env.auth.token.unseal()),
60
+ }
61
+ })
62
+ ```
63
+
64
+ ## Vite and Nitro
65
+
66
+ Vite handles public/build env and generates `#vitehub/env/public`. Nitro handles server/runtime env, secret values, and `#vitehub/env/server`. Cloudflare secrets can be marked as required during Nitro setup.
67
+
68
+ Learn more at [vitehub.dev](https://vitehub.dev).
@@ -0,0 +1,17 @@
1
+ import { d as EnvSourceResolver, f as EnvVariableDeclaration, l as EnvSource, p as EnvVariableOptions } from "./types-DnbiD9KN.js";
2
+
3
+ //#region src/core/declarations.d.ts
4
+ interface EnvNamespace {
5
+ (options?: EnvVariableOptions): EnvVariableDeclaration;
6
+ custom: (label: string, resolver: EnvSourceResolver) => EnvSource;
7
+ gitBranch: () => EnvSource;
8
+ gitCommit: (options?: {
9
+ short?: boolean;
10
+ }) => EnvSource;
11
+ packageJson: (path: string) => EnvSource;
12
+ source: (name: string | string[]) => EnvSource;
13
+ variable: (options?: EnvVariableOptions) => EnvVariableDeclaration;
14
+ }
15
+ declare const env: EnvNamespace;
16
+ //#endregion
17
+ export { env as t };
@@ -0,0 +1,111 @@
1
+ //#region src/core/declarations.ts
2
+ const defaultStringSchema = {
3
+ __vitehubDefaultRuntimeSchema: getDefaultStringSchemaToken(),
4
+ safeParse(input) {
5
+ return typeof input === "string" ? {
6
+ data: input,
7
+ success: true
8
+ } : {
9
+ error: /* @__PURE__ */ new Error("Expected string"),
10
+ success: false
11
+ };
12
+ }
13
+ };
14
+ const defaultStringSchemaProperty = "__vitehubDefaultRuntimeSchema";
15
+ const defaultStringSchemaToken = defaultStringSchema.__vitehubDefaultRuntimeSchema;
16
+ const defaultStringSchemas = /* @__PURE__ */ new WeakMap();
17
+ const defaultStringSchemaParsers = /* @__PURE__ */ new WeakSet();
18
+ defaultStringSchemaParsers.add(defaultStringSchema.safeParse);
19
+ function source(name) {
20
+ const names = Array.isArray(name) ? name : [name];
21
+ if (!names.length || names.some((value) => typeof value !== "string" || !value.trim())) throw new TypeError("env.source() requires one or more non-empty env variable names.");
22
+ const normalized = names.map((value) => value.trim());
23
+ return {
24
+ kind: "env",
25
+ label: `env:${normalized.join("|")}`,
26
+ name: normalized[0],
27
+ ...normalized.length > 1 ? { names: normalized } : {},
28
+ serializable: true
29
+ };
30
+ }
31
+ function custom(label, resolver) {
32
+ return {
33
+ kind: "custom",
34
+ label,
35
+ resolver,
36
+ serializable: false
37
+ };
38
+ }
39
+ function gitBranch() {
40
+ return {
41
+ kind: "git-branch",
42
+ label: "git:branch",
43
+ serializable: true
44
+ };
45
+ }
46
+ function gitCommit(options = {}) {
47
+ return {
48
+ kind: "git-commit",
49
+ label: "git:commit",
50
+ serializable: true,
51
+ short: options.short
52
+ };
53
+ }
54
+ function packageJson(path) {
55
+ return {
56
+ kind: "package-json",
57
+ label: `package.json:${path}`,
58
+ path,
59
+ serializable: true
60
+ };
61
+ }
62
+ function variable(options = {}) {
63
+ if (typeof options !== "object" || options === null || Array.isArray(options)) throw new TypeError("env() only accepts a single options object.");
64
+ if (options.optional && typeof options.required !== "undefined") throw new TypeError("env() cannot use both optional and required.");
65
+ const required = options.optional ? false : options.required ?? true;
66
+ const source = typeof options.source === "function" ? custom("custom", options.source) : options.source;
67
+ const schema = options.schema ?? defaultStringSchema;
68
+ const declaration = {
69
+ default: options.default,
70
+ kind: "env-variable",
71
+ mode: options.mode ?? "runtime",
72
+ required,
73
+ schema,
74
+ secret: options.secret ?? false,
75
+ source,
76
+ type: options.type
77
+ };
78
+ if (typeof options.schema === "undefined") {
79
+ defaultStringSchemas.set(declaration, defaultStringSchema);
80
+ Object.defineProperty(declaration, defaultStringSchemaProperty, {
81
+ enumerable: true,
82
+ value: defaultStringSchemaToken
83
+ });
84
+ }
85
+ return declaration;
86
+ }
87
+ const env = Object.assign(variable, {
88
+ custom,
89
+ gitBranch,
90
+ gitCommit,
91
+ packageJson,
92
+ source,
93
+ variable
94
+ });
95
+ function isDefaultStringEnvVariable(declaration) {
96
+ return defaultStringSchemas.get(declaration) === declaration.schema || declaration[defaultStringSchemaProperty] === defaultStringSchemaToken && typeof declaration.schema === "object" && declaration.schema !== null && declaration.schema[defaultStringSchemaProperty] === defaultStringSchemaToken && hasOnlyDefaultStringSchemaKeys(declaration.schema);
97
+ }
98
+ function getDefaultStringSchemaToken() {
99
+ const tokenKey = Symbol.for("vitehub.env.defaultRuntimeSchemaToken");
100
+ const globalScope = globalThis;
101
+ globalScope[tokenKey] ??= `string:${Math.random().toString(36).slice(2)}`;
102
+ return globalScope[tokenKey];
103
+ }
104
+ function hasOnlyDefaultStringSchemaKeys(schema) {
105
+ if ("~standard" in schema || "parse" in schema) return false;
106
+ const keys = Reflect.ownKeys(schema);
107
+ const safeParse = Object.getOwnPropertyDescriptor(schema, "safeParse");
108
+ return keys.length === 2 && keys.includes(defaultStringSchemaProperty) && keys.includes("safeParse") && typeof safeParse?.value === "function" && defaultStringSchemaParsers.has(safeParse.value);
109
+ }
110
+ //#endregion
111
+ export { env as n, isDefaultStringEnvVariable as r, defaultStringSchema as t };
@@ -0,0 +1,6 @@
1
+ import { _ as ServerEnv, a as EnvMode, c as EnvRuntimeRegistry, d as EnvSourceResolver, f as EnvVariableDeclaration, g as PublicEnv, h as EnvViteUserConfig, i as EnvIntegrationOptions, l as EnvSource, m as EnvViteConfigOptions, n as EnvDiagnosticEntry, o as EnvNitroConfigOptions, p as EnvVariableOptions, r as EnvDiagnostics, s as EnvNitroUserConfig, t as EnvConfigOptions, u as EnvSourceContext } from "./types-DnbiD9KN.js";
2
+ import { t as env } from "./declarations-DIq2gG-8.js";
3
+ import { openWorkflowEnv } from "./presets.js";
4
+ import { StandardSchemaV1, parseSchema } from "./schema.js";
5
+ import { SecretEnv } from "./secret.js";
6
+ export { type EnvConfigOptions, type EnvDiagnosticEntry, type EnvDiagnostics, type EnvIntegrationOptions, type EnvMode, type EnvNitroConfigOptions, type EnvNitroUserConfig, type EnvRuntimeRegistry, type EnvSource, type EnvSourceContext, type EnvSourceResolver, type EnvVariableDeclaration, type EnvVariableOptions, type EnvViteConfigOptions, type EnvViteUserConfig, type PublicEnv, SecretEnv, type ServerEnv, type StandardSchemaV1, env, openWorkflowEnv, parseSchema };
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import { n as env } from "./declarations-DgJVnQyx.js";
2
+ import { openWorkflowEnv } from "./presets.js";
3
+ import { parseSchema } from "./schema.js";
4
+ import { SecretEnv } from "./secret.js";
5
+ export { SecretEnv, env, openWorkflowEnv, parseSchema };