arkenv 0.3.0 → 0.4.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
@@ -1,7 +1,90 @@
1
- # `arkenv`
2
-
3
- Hello, developers! Welcome to the `arkenv` package codebase. This directory contains the core implementation of ArkEnv, providing typesafe environment variable parsing and validation for Node.js.
4
-
5
- For more information, please refer to the [README](../../README.md) in the monorepo root.
6
-
7
- Happy coding!
1
+ <p align="center">
2
+ <a href="https://arkenv.js.org">
3
+ <img alt="arkenv - Typesafe Environment Variables" src="https://og.tailgraph.com/og?titleFontFamily=JetBrains+Mono&textFontFamily=Inter&title=ArkEnv&titleTailwind=text-[%23e9eef9]%20font-bold%20relative%20decoration-%5Brgb(180,215,255)%5D%20decoration-wavy%20decoration-[5px]%20underline%20underline-offset-[16px]%20text-5xl%20mb-8&text=Typesafe%20environment%20variables%20powered%20by%20ArkType&textTailwind=text-[%238b9dc1]%20text-3xl&bgTailwind=bg-gradient-to-b%20from-[%23061a3a]%20to-black" width="645px">
4
+ </a>
5
+ <br />
6
+ <a href="https://github.com/yamcodes/arkenv/actions/workflows/tests.yml?query=branch%3Amain"><img alt="Tests Status" src="https://github.com/yamcodes/arkenv/actions/workflows/tests.yml/badge.svg?event=push&branch=main"></a>
7
+ <a href="https://bundlephobia.com/package/arkenv"><img alt="Bundle Size" src="https://img.shields.io/bundlephobia/min/arkenv?label=bundle%20size&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMzcgMTU3IiB3aWR0aD0iMTM3IiBoZWlnaHQ9IjE1NyI+PHJlY3QgZmlsbD0iI2ZmZiIgeD0iNjIuMzEiIHk9IjEwMi4zMSIgd2lkdGg9IjE2Ljc0IiBoZWlnaHQ9IjE1Ljk0IiByeD0iNy45NyIvPjxyZWN0IGZpbGw9IiNmZmYiIHg9IjYyLjMxIiB5PSI4Ni4zNyIgd2lkdGg9IjE2Ljc0IiBoZWlnaHQ9IjMxLjg4IiByeD0iOC4zNyIvPjxlbGxpcHNlIGZpbGw9IiNmZmYiIGN4PSI0NC4zNCIgY3k9IjU0LjIyIiByeD0iNy4xMyIgcnk9IjguNDUiLz48ZWxsaXBzZSBmaWxsPSIjZmZmIiBjeD0iOTMuNzUiIGN5PSI1NC4yMiIgcng9IjcuMTMiIHJ5PSI4LjQ1Ii8+PHBhdGggZmlsbD0iI2ZmZiIgZD0iTTExNC4xIDExNy44NGMxLjItMS4wMiAxLjc0LTEuOTYgMi40OC0zLjU2bDE5LjMtNDIuOTJjLTIuMDItMjcuMS0zLjQ0LTQwLjctMy40NC00MC43NyAwLTIuNy0yLjE0LTQuOC00Ljc4LTQuOC0yLjYgMC00LjczIDIuMS00Ljc4IDQuNzBsLTMuMDUgMzcuNy0xNC43NiA0Mi4xYy0uNDQuOC0uNyAxLjgtLjcgMi44IDAgLjgzLjIgMS42NC41IDIuNGwxMC40MyA0MC4xMiAxMS41NS0zLjEtMTIuNzQtMzQuOHoiLz48cGF0aCBmaWxsPSIjZmZmIiBkPSJNMjMuMjcgMTE3Ljg0Yy0xLjItMS4wMi0xLjczLTEuOTYtMi40Ny0zLjU2TDEuNSA3MS4zNmMyLjAyLTI3LjEgMy40My00MC43IDMuNDMtNDAuNzcgMC0yLjcgMi4xNC00LjggNC44LTQuOCAyLjYgMCA0LjcyIDIuMSA0Ljc3IDQuN2wzLjA1IDM3LjcgMTQuNzUgNDIuMmMuNDUuOC43IDEuOC43IDIuOCAwIC44LS4xOCAxLjYtLjUgMi40bC0xMC40IDQwLjEtMTEuNTUtMy4xIDEyLjc0LTM0Ljh6Ii8+PHBhdGggZmlsbD0iI2ZmZiIgZD0iTTk0LjI2IDkxLjIzYzEyLjItNy41NCAyMC4yNS0yMC4zOCAyMC4yNS0zNC45NCAwLTMuOS0uNS03LjYtMS41LTExLjFDMTEyLjggMjEgOTMuMiAxLjUgNjguOTggMS41UzI1IDIxLjAyIDI0Ljg3IDQ1LjJjLTEuMDUgMy41Mi0xLjYgNy4yMy0xLjYgMTEuMDUgMCAxNi41NCAxMC40MyAzMC45IDI1LjYgMzcuNzItLjEgMS40LS4xIDIuODItLjEgNC4yNiAwIDIzLjIyIDEwLjIyIDQyLjA0IDIyLjkgNDIuMDQgMTIuNjUgMCAyMi45Mi0xOC44IDIyLjkyLTQyLjAzIDAtMi40LS4yLTQuOC0uNC03LjF6Ii8+PC9zdmc+"></a>
8
+ <a href="https://www.npmjs.com/package/arkenv?activeTab=versions"><img alt="Total Downloads" src="https://badgen.net/npm/dt/arkenv?icon=npm&color=blue&label=downloads"></a>
9
+ <a href="https://www.npmjs.com/package/arkenv?activeTab=dependencies"><img alt="Dependency Count" src="https://badgen.net/bundlephobia/dependency-count/arkenv?icon=npm&color=blue&label=dependencies"></a>
10
+ <a href="https://arktype.io/"><img alt="Powered By ArkType" src="https://badgen.net/static/powered by/ArkType"></a>
11
+ </p>
12
+
13
+ ## Installation
14
+
15
+ <details open>
16
+ <summary>npm</summary>
17
+
18
+ ```sh
19
+ npm install arkenv arktype
20
+ ```
21
+ </details>
22
+
23
+ <details>
24
+ <summary>pnpm</summary>
25
+
26
+ ```sh
27
+ pnpm add arkenv arktype
28
+ ```
29
+ </details>
30
+
31
+ <details>
32
+ <summary>Yarn</summary>
33
+
34
+ ```sh
35
+ yarn add arkenv arktype
36
+ ```
37
+ </details>
38
+
39
+ <details>
40
+ <summary>Bun</summary>
41
+
42
+ ```sh
43
+ bun add arkenv arktype
44
+ ```
45
+ </details>
46
+
47
+ ## Quickstart
48
+
49
+ > [!TIP]
50
+ > Find more examples in the [examples](https://github.com/yamcodes/arkenv/tree/main/examples) directory.
51
+
52
+ ```ts
53
+ import { createEnv } from 'arkenv';
54
+
55
+ const env = createEnv({
56
+ HOST: "string.host", // valid IP address or localhost
57
+ PORT: "number.port", // valid port number (0-65535)
58
+ NODE_ENV: "'development' | 'production' | 'test'",
59
+ });
60
+
61
+ // Automatically validate and parse process.env
62
+ // TypeScript knows the ✨exact✨ types!
63
+ console.log(env.HOST); // (property) HOST: string
64
+ console.log(env.PORT); // (property) PORT: number
65
+ console.log(env.NODE_ENV); // (property) NODE_ENV: "development" | "production" | "test"
66
+ ```
67
+
68
+ ## Features
69
+
70
+ - 🔒 **Typesafe**: Full TypeScript support with inferred types
71
+ - 🚀 **Runtime validation**: Catch missing or invalid environment variables early
72
+ - 💪 **Powered by ArkType**: Leverage ArkType's powerful type system
73
+ - 🪶 **Lightweight**: Only a single dependency ([5.6 kB](https://bundlephobia.com/package/chalk@5.4.1))
74
+ - ⚡ **Fast**: Optimized for performance with minimal overhead
75
+
76
+ ## Documentation
77
+
78
+ For detailed documentation and examples, please visit our [documentation site](https://arkenv.js.org/docs).
79
+
80
+ ## Plugins
81
+
82
+ - [@arkenv/vite-plugin](https://github.com/yamcodes/arkenv/tree/main/packages/vite-plugin): [Vite](https://vite.dev/) plugin to validate environment variables at build time
83
+
84
+ ## Contributing
85
+
86
+ Contributions are welcome! Please see [CONTRIBUTING.md](https://github.com/yamcodes/arkenv/blob/main/CONTRIBUTING.md) for more information.
87
+
88
+ ## Thanks / Inspiration
89
+
90
+ Find projects and people who helped or inspired the creation of ArkEnv in [THANKS.md](https://github.com/yamcodes/arkenv/blob/main/THANKS.md). Thank you 🙏
package/dist/index.cjs CHANGED
@@ -31,18 +31,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
33
  createEnv: () => createEnv,
34
- default: () => index_default,
35
- host: () => host,
36
- port: () => port
34
+ default: () => index_default
37
35
  });
38
36
  module.exports = __toCommonJS(index_exports);
39
37
 
40
- // src/define-env.ts
41
- var define_env_exports = {};
42
- __export(define_env_exports, {
38
+ // src/create-env.ts
39
+ var create_env_exports = {};
40
+ __export(create_env_exports, {
43
41
  createEnv: () => createEnv
44
42
  });
45
- var import_arktype = require("arktype");
43
+ var import_arktype3 = require("arktype");
46
44
 
47
45
  // src/errors.ts
48
46
  var import_chalk = __toESM(require("chalk"), 1);
@@ -75,30 +73,12 @@ ${indent(formatErrors(errors))}
75
73
  }
76
74
  };
77
75
 
78
- // src/define-env.ts
79
- var createEnv = (def, env = process.env) => {
80
- const schema = (0, import_arktype.type)(def);
81
- const requiredEnvKeys = Object.keys(
82
- def
83
- );
84
- const filteredEnvVars = Object.fromEntries(
85
- Object.entries(env).filter(([key]) => requiredEnvKeys.includes(key))
86
- );
87
- const validatedEnv = schema(filteredEnvVars);
88
- if (validatedEnv instanceof import_arktype.type.errors) {
89
- throw new ArkEnvError(validatedEnv);
90
- }
91
- return validatedEnv;
92
- };
76
+ // src/scope.ts
77
+ var import_arktype2 = require("arktype");
93
78
 
94
79
  // src/types.ts
95
- var types_exports = {};
96
- __export(types_exports, {
97
- host: () => host,
98
- port: () => port
99
- });
100
- var import_arktype2 = require("arktype");
101
- var port = (0, import_arktype2.type)("string", "=>", (data, ctx) => {
80
+ var import_arktype = require("arktype");
81
+ var port = (0, import_arktype.type)("string", "=>", (data, ctx) => {
102
82
  const asNumber = Number.parseInt(data);
103
83
  const isInteger = Number.isInteger(asNumber);
104
84
  const isBetween = 0 <= asNumber && asNumber <= 65535;
@@ -107,13 +87,33 @@ var port = (0, import_arktype2.type)("string", "=>", (data, ctx) => {
107
87
  }
108
88
  return asNumber;
109
89
  });
110
- var host = (0, import_arktype2.type)("string.ip | 'localhost'");
90
+ var host = (0, import_arktype.type)("string.ip | 'localhost'");
91
+
92
+ // src/scope.ts
93
+ var $ = (0, import_arktype2.scope)({
94
+ string: import_arktype2.type.module({
95
+ ...import_arktype2.type.keywords.string,
96
+ host
97
+ }),
98
+ number: import_arktype2.type.module({
99
+ ...import_arktype2.type.keywords.number,
100
+ port
101
+ })
102
+ });
103
+
104
+ // src/create-env.ts
105
+ function createEnv(def, env = process.env) {
106
+ const schema = $.type.raw(def);
107
+ const validatedEnv = schema(env);
108
+ if (validatedEnv instanceof import_arktype3.type.errors) {
109
+ throw new ArkEnvError(validatedEnv);
110
+ }
111
+ return validatedEnv;
112
+ }
111
113
 
112
114
  // src/index.ts
113
- var index_default = { ...define_env_exports, ...types_exports };
115
+ var index_default = { ...create_env_exports };
114
116
  // Annotate the CommonJS export names for ESM import in node:
115
117
  0 && (module.exports = {
116
- createEnv,
117
- host,
118
- port
118
+ createEnv
119
119
  });
package/dist/index.d.cts CHANGED
@@ -1,34 +1,102 @@
1
1
  import * as arktype from 'arktype';
2
2
  import { type, distill } from 'arktype';
3
- import * as arktype_internal_methods_string_ts from 'arktype/internal/methods/string.ts';
4
- import * as arktype_internal_methods_morph_ts from 'arktype/internal/methods/morph.ts';
5
3
  import * as arktype_internal_attributes_ts from 'arktype/internal/attributes.ts';
4
+ import * as arktype_internal_keywords_string_ts from 'arktype/internal/keywords/string.ts';
6
5
 
7
- type UserEnvironment = Record<string, string | undefined>;
8
- type EnvSchema<T extends Record<string, string | undefined> = Record<string, string | undefined>> = type.validate<T>;
9
6
  /**
10
- * Define an environment variable schema and validate it against a given environment (defaults to `process.env`)
11
- * @param def - The environment variable schema
12
- * @param env - The environment variables to validate, defaults to `process.env`
13
- * @returns The validated environment variable schema
7
+ * The root scope for the ArkEnv library, containing extensions to the ArkType scopes with ArkEnv-specific types like `string.host` and `number.port`.
14
8
  */
15
- declare const createEnv: <const T extends Record<string, string | undefined>>(def: EnvSchema<T>, env?: UserEnvironment) => distill.Out<type.infer<T>>;
9
+ declare const $: arktype.Scope<{
10
+ string: arktype.Submodule<{
11
+ root: string;
12
+ " arkInferred": string;
13
+ trim: arktype.Submodule<arktype_internal_keywords_string_ts.trim.$ & {
14
+ " arkInferred": (In: string) => arktype_internal_attributes_ts.To<string>;
15
+ }>;
16
+ normalize: arktype.Submodule<arktype_internal_keywords_string_ts.normalize.$ & {
17
+ " arkInferred": (In: string) => arktype_internal_attributes_ts.To<string>;
18
+ }>;
19
+ alpha: string;
20
+ alphanumeric: string;
21
+ hex: string;
22
+ base64: arktype.Submodule<{
23
+ root: string;
24
+ url: string;
25
+ } & {
26
+ " arkInferred": string;
27
+ }>;
28
+ capitalize: arktype.Submodule<arktype_internal_keywords_string_ts.capitalize.$ & {
29
+ " arkInferred": (In: string) => arktype_internal_attributes_ts.To<string>;
30
+ }>;
31
+ creditCard: string;
32
+ date: arktype.Submodule<arktype_internal_keywords_string_ts.stringDate.$ & {
33
+ " arkInferred": string;
34
+ }>;
35
+ digits: string;
36
+ email: string;
37
+ integer: arktype.Submodule<arktype_internal_keywords_string_ts.stringInteger.$ & {
38
+ " arkInferred": string;
39
+ }>;
40
+ ip: arktype.Submodule<arktype_internal_keywords_string_ts.ip.$ & {
41
+ " arkInferred": string;
42
+ }>;
43
+ json: arktype.Submodule<arktype_internal_keywords_string_ts.stringJson.$ & {
44
+ " arkInferred": string;
45
+ }>;
46
+ lower: arktype.Submodule<arktype_internal_keywords_string_ts.lower.$ & {
47
+ " arkInferred": (In: string) => arktype_internal_attributes_ts.To<string>;
48
+ }>;
49
+ numeric: arktype.Submodule<arktype_internal_keywords_string_ts.stringNumeric.$ & {
50
+ " arkInferred": string;
51
+ }>;
52
+ regex: string;
53
+ semver: string;
54
+ upper: arktype.Submodule<{
55
+ root: (In: string) => arktype_internal_attributes_ts.To<string>;
56
+ preformatted: string;
57
+ } & {
58
+ " arkInferred": (In: string) => arktype_internal_attributes_ts.To<string>;
59
+ }>;
60
+ url: arktype.Submodule<arktype_internal_keywords_string_ts.url.$ & {
61
+ " arkInferred": string;
62
+ }>;
63
+ uuid: arktype.Submodule<arktype_internal_keywords_string_ts.uuid.$ & {
64
+ " arkInferred": string;
65
+ }>;
66
+ host: string;
67
+ }>;
68
+ number: arktype.Submodule<{
69
+ NaN: number;
70
+ Infinity: number;
71
+ root: number;
72
+ " arkInferred": number;
73
+ integer: number;
74
+ epoch: number;
75
+ safe: number;
76
+ NegativeInfinity: number;
77
+ port: (In: string) => arktype.Out<number>;
78
+ }>;
79
+ }>;
16
80
 
81
+ type RuntimeEnvironment = Record<string, string | undefined>;
82
+ type EnvSchema<def, $ = {}> = type.validate<def, $>;
17
83
  /**
18
- * A `string` that can be parsed into a number between 0 and 65535
19
- */
20
- declare const port: arktype_internal_methods_morph_ts.MorphType<(In: string) => arktype_internal_attributes_ts.Out<number>, {}>;
21
- /**
22
- * An IP address or `"localhost"`
84
+ * Create an environment variables object from a schema and an environment
85
+ * @param def - The environment variable schema
86
+ * @param env - The environment variables to validate, defaults to `process.env`
87
+ * @returns The validated environment variable schema
88
+ * @throws An error if the environment variables are invalid. See {@link ArkEnvError}
23
89
  */
24
- declare const host: arktype_internal_methods_string_ts.StringType<string, {}>;
90
+ declare function createEnv<const T extends Record<string, string | undefined>>(def: EnvSchema<T, (typeof $)["t"]>, env?: RuntimeEnvironment): distill.Out<type.infer<T, (typeof $)["t"]>>;
91
+ declare function createEnv<const T extends Record<string, string | undefined>>(def: EnvSchema<T>, env?: RuntimeEnvironment): distill.Out<type.infer<T>>;
25
92
 
26
93
  declare const _default: {
27
- port: arktype_internal_methods_morph_ts.MorphType<(In: string) => arktype_internal_attributes_ts.Out<number>, {}>;
28
- host: arktype_internal_methods_string_ts.StringType<string, {}>;
29
- createEnv: <const T extends Record<string, string | undefined>>(def: EnvSchema<T>, env?: {
94
+ createEnv<const T extends Record<string, string | undefined>>(def: EnvSchema<T, typeof $["t"]>, env?: {
95
+ [x: string]: string | undefined;
96
+ }): arktype.distill.Out<arktype.type.infer<T, typeof $["t"]>>;
97
+ createEnv<const T extends Record<string, string | undefined>>(def: EnvSchema<T>, env?: {
30
98
  [x: string]: string | undefined;
31
- }) => arktype.distill.Out<arktype.type.infer<T>>;
99
+ }): arktype.distill.Out<arktype.type.infer<T>>;
32
100
  };
33
101
 
34
- export { type EnvSchema, createEnv, _default as default, host, port };
102
+ export { type EnvSchema, createEnv, _default as default };
package/dist/index.d.ts CHANGED
@@ -1,34 +1,102 @@
1
1
  import * as arktype from 'arktype';
2
2
  import { type, distill } from 'arktype';
3
- import * as arktype_internal_methods_string_ts from 'arktype/internal/methods/string.ts';
4
- import * as arktype_internal_methods_morph_ts from 'arktype/internal/methods/morph.ts';
5
3
  import * as arktype_internal_attributes_ts from 'arktype/internal/attributes.ts';
4
+ import * as arktype_internal_keywords_string_ts from 'arktype/internal/keywords/string.ts';
6
5
 
7
- type UserEnvironment = Record<string, string | undefined>;
8
- type EnvSchema<T extends Record<string, string | undefined> = Record<string, string | undefined>> = type.validate<T>;
9
6
  /**
10
- * Define an environment variable schema and validate it against a given environment (defaults to `process.env`)
11
- * @param def - The environment variable schema
12
- * @param env - The environment variables to validate, defaults to `process.env`
13
- * @returns The validated environment variable schema
7
+ * The root scope for the ArkEnv library, containing extensions to the ArkType scopes with ArkEnv-specific types like `string.host` and `number.port`.
14
8
  */
15
- declare const createEnv: <const T extends Record<string, string | undefined>>(def: EnvSchema<T>, env?: UserEnvironment) => distill.Out<type.infer<T>>;
9
+ declare const $: arktype.Scope<{
10
+ string: arktype.Submodule<{
11
+ root: string;
12
+ " arkInferred": string;
13
+ trim: arktype.Submodule<arktype_internal_keywords_string_ts.trim.$ & {
14
+ " arkInferred": (In: string) => arktype_internal_attributes_ts.To<string>;
15
+ }>;
16
+ normalize: arktype.Submodule<arktype_internal_keywords_string_ts.normalize.$ & {
17
+ " arkInferred": (In: string) => arktype_internal_attributes_ts.To<string>;
18
+ }>;
19
+ alpha: string;
20
+ alphanumeric: string;
21
+ hex: string;
22
+ base64: arktype.Submodule<{
23
+ root: string;
24
+ url: string;
25
+ } & {
26
+ " arkInferred": string;
27
+ }>;
28
+ capitalize: arktype.Submodule<arktype_internal_keywords_string_ts.capitalize.$ & {
29
+ " arkInferred": (In: string) => arktype_internal_attributes_ts.To<string>;
30
+ }>;
31
+ creditCard: string;
32
+ date: arktype.Submodule<arktype_internal_keywords_string_ts.stringDate.$ & {
33
+ " arkInferred": string;
34
+ }>;
35
+ digits: string;
36
+ email: string;
37
+ integer: arktype.Submodule<arktype_internal_keywords_string_ts.stringInteger.$ & {
38
+ " arkInferred": string;
39
+ }>;
40
+ ip: arktype.Submodule<arktype_internal_keywords_string_ts.ip.$ & {
41
+ " arkInferred": string;
42
+ }>;
43
+ json: arktype.Submodule<arktype_internal_keywords_string_ts.stringJson.$ & {
44
+ " arkInferred": string;
45
+ }>;
46
+ lower: arktype.Submodule<arktype_internal_keywords_string_ts.lower.$ & {
47
+ " arkInferred": (In: string) => arktype_internal_attributes_ts.To<string>;
48
+ }>;
49
+ numeric: arktype.Submodule<arktype_internal_keywords_string_ts.stringNumeric.$ & {
50
+ " arkInferred": string;
51
+ }>;
52
+ regex: string;
53
+ semver: string;
54
+ upper: arktype.Submodule<{
55
+ root: (In: string) => arktype_internal_attributes_ts.To<string>;
56
+ preformatted: string;
57
+ } & {
58
+ " arkInferred": (In: string) => arktype_internal_attributes_ts.To<string>;
59
+ }>;
60
+ url: arktype.Submodule<arktype_internal_keywords_string_ts.url.$ & {
61
+ " arkInferred": string;
62
+ }>;
63
+ uuid: arktype.Submodule<arktype_internal_keywords_string_ts.uuid.$ & {
64
+ " arkInferred": string;
65
+ }>;
66
+ host: string;
67
+ }>;
68
+ number: arktype.Submodule<{
69
+ NaN: number;
70
+ Infinity: number;
71
+ root: number;
72
+ " arkInferred": number;
73
+ integer: number;
74
+ epoch: number;
75
+ safe: number;
76
+ NegativeInfinity: number;
77
+ port: (In: string) => arktype.Out<number>;
78
+ }>;
79
+ }>;
16
80
 
81
+ type RuntimeEnvironment = Record<string, string | undefined>;
82
+ type EnvSchema<def, $ = {}> = type.validate<def, $>;
17
83
  /**
18
- * A `string` that can be parsed into a number between 0 and 65535
19
- */
20
- declare const port: arktype_internal_methods_morph_ts.MorphType<(In: string) => arktype_internal_attributes_ts.Out<number>, {}>;
21
- /**
22
- * An IP address or `"localhost"`
84
+ * Create an environment variables object from a schema and an environment
85
+ * @param def - The environment variable schema
86
+ * @param env - The environment variables to validate, defaults to `process.env`
87
+ * @returns The validated environment variable schema
88
+ * @throws An error if the environment variables are invalid. See {@link ArkEnvError}
23
89
  */
24
- declare const host: arktype_internal_methods_string_ts.StringType<string, {}>;
90
+ declare function createEnv<const T extends Record<string, string | undefined>>(def: EnvSchema<T, (typeof $)["t"]>, env?: RuntimeEnvironment): distill.Out<type.infer<T, (typeof $)["t"]>>;
91
+ declare function createEnv<const T extends Record<string, string | undefined>>(def: EnvSchema<T>, env?: RuntimeEnvironment): distill.Out<type.infer<T>>;
25
92
 
26
93
  declare const _default: {
27
- port: arktype_internal_methods_morph_ts.MorphType<(In: string) => arktype_internal_attributes_ts.Out<number>, {}>;
28
- host: arktype_internal_methods_string_ts.StringType<string, {}>;
29
- createEnv: <const T extends Record<string, string | undefined>>(def: EnvSchema<T>, env?: {
94
+ createEnv<const T extends Record<string, string | undefined>>(def: EnvSchema<T, typeof $["t"]>, env?: {
95
+ [x: string]: string | undefined;
96
+ }): arktype.distill.Out<arktype.type.infer<T, typeof $["t"]>>;
97
+ createEnv<const T extends Record<string, string | undefined>>(def: EnvSchema<T>, env?: {
30
98
  [x: string]: string | undefined;
31
- }) => arktype.distill.Out<arktype.type.infer<T>>;
99
+ }): arktype.distill.Out<arktype.type.infer<T>>;
32
100
  };
33
101
 
34
- export { type EnvSchema, createEnv, _default as default, host, port };
102
+ export { type EnvSchema, createEnv, _default as default };
package/dist/index.js CHANGED
@@ -4,12 +4,12 @@ var __export = (target, all) => {
4
4
  __defProp(target, name, { get: all[name], enumerable: true });
5
5
  };
6
6
 
7
- // src/define-env.ts
8
- var define_env_exports = {};
9
- __export(define_env_exports, {
7
+ // src/create-env.ts
8
+ var create_env_exports = {};
9
+ __export(create_env_exports, {
10
10
  createEnv: () => createEnv
11
11
  });
12
- import { type } from "arktype";
12
+ import { type as type3 } from "arktype";
13
13
 
14
14
  // src/errors.ts
15
15
  import chalk from "chalk";
@@ -42,30 +42,12 @@ ${indent(formatErrors(errors))}
42
42
  }
43
43
  };
44
44
 
45
- // src/define-env.ts
46
- var createEnv = (def, env = process.env) => {
47
- const schema = type(def);
48
- const requiredEnvKeys = Object.keys(
49
- def
50
- );
51
- const filteredEnvVars = Object.fromEntries(
52
- Object.entries(env).filter(([key]) => requiredEnvKeys.includes(key))
53
- );
54
- const validatedEnv = schema(filteredEnvVars);
55
- if (validatedEnv instanceof type.errors) {
56
- throw new ArkEnvError(validatedEnv);
57
- }
58
- return validatedEnv;
59
- };
45
+ // src/scope.ts
46
+ import { scope, type as type2 } from "arktype";
60
47
 
61
48
  // src/types.ts
62
- var types_exports = {};
63
- __export(types_exports, {
64
- host: () => host,
65
- port: () => port
66
- });
67
- import { type as type2 } from "arktype";
68
- var port = type2("string", "=>", (data, ctx) => {
49
+ import { type } from "arktype";
50
+ var port = type("string", "=>", (data, ctx) => {
69
51
  const asNumber = Number.parseInt(data);
70
52
  const isInteger = Number.isInteger(asNumber);
71
53
  const isBetween = 0 <= asNumber && asNumber <= 65535;
@@ -74,13 +56,33 @@ var port = type2("string", "=>", (data, ctx) => {
74
56
  }
75
57
  return asNumber;
76
58
  });
77
- var host = type2("string.ip | 'localhost'");
59
+ var host = type("string.ip | 'localhost'");
60
+
61
+ // src/scope.ts
62
+ var $ = scope({
63
+ string: type2.module({
64
+ ...type2.keywords.string,
65
+ host
66
+ }),
67
+ number: type2.module({
68
+ ...type2.keywords.number,
69
+ port
70
+ })
71
+ });
72
+
73
+ // src/create-env.ts
74
+ function createEnv(def, env = process.env) {
75
+ const schema = $.type.raw(def);
76
+ const validatedEnv = schema(env);
77
+ if (validatedEnv instanceof type3.errors) {
78
+ throw new ArkEnvError(validatedEnv);
79
+ }
80
+ return validatedEnv;
81
+ }
78
82
 
79
83
  // src/index.ts
80
- var index_default = { ...define_env_exports, ...types_exports };
84
+ var index_default = { ...create_env_exports };
81
85
  export {
82
86
  createEnv,
83
- index_default as default,
84
- host,
85
- port
87
+ index_default as default
86
88
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "arkenv",
3
3
  "type": "module",
4
- "version": "0.3.0",
4
+ "version": "0.4.0",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -37,7 +37,7 @@
37
37
  "typescript": "^5.9.2"
38
38
  },
39
39
  "peerDependencies": {
40
- "arktype": "^2.0.2"
40
+ "arktype": "^2.1.22"
41
41
  },
42
42
  "dependencies": {
43
43
  "chalk": "^5.6.0"
@@ -47,6 +47,7 @@
47
47
  "test:once": "pnpm test",
48
48
  "typecheck": "tsc --noEmit",
49
49
  "clean": "rimraf dist node_modules",
50
- "test": "pnpm -w test --project arkenv"
50
+ "test": "pnpm -w test --project arkenv",
51
+ "fix": "pnpm -w run fix"
51
52
  }
52
53
  }