Skip to content

Commit 311d79e

Browse files
authored
Merge pull request #1 from danimydev/v0.1.1
V0.1.1
2 parents 397ce9d + 8350d87 commit 311d79e

File tree

13 files changed

+92
-131
lines changed

13 files changed

+92
-131
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
# @danimydev/env
22

3-
A TypeScript-first, runtime-safe environment variable reader. Works in Node.js, Deno, Bun, Browser, and Cloudflare Workers.
3+
A TypeScript-first, runtime-safe environment variable reader. Works in Node.js,
4+
Deno and Bun. If you want you can just copy the source code or install it via
5+
[jsr](https://jsr.io/@danimydev/env).
46

57
## Usage
8+
69
```typescript
7-
import env, { string, number, boolean, object, optional } from "@danimydev/env";
8-
9-
const config = env(
10-
object({
11-
NODE_ENV: string(),
12-
PORT: number(),
13-
DEBUG: optional(boolean())
14-
})
15-
);
16-
17-
console.log(config.NODE_ENV);
18-
console.log(config.PORT);
19-
console.log(config.DEBUG);
10+
import env, { boolean, number, optional, string } from "@danimydev/env";
11+
12+
const config = env({
13+
NODE_ENV: string(), // string,
14+
PORT: number(), // number,
15+
DEBUG: optional(boolean()), // boolean | undefined
16+
});
2017
```
2118

2219
## Highlights
2320

2421
- ✅ TypeScript-first: fully typed environment variable schemas.
25-
- 🌍 Cross-runtime: works in Node.js, Deno, Bun, Browser, Cloudflare Workers.
22+
- 🌍 Cross-runtime: works in Node.js, Deno and Bun.
2623
- 💡 Flexible: supports optional variables.
2724
- ⚡ Lightweight: zero dependencies, minimal overhead.
2825

2926
## License
3027

31-
This project is released into the public domain under [The Unlicense](https://unlicense.org).
28+
This project is released into the public domain under
29+
[The Unlicense](https://unlicense.org).
3230

33-
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software for any purpose.
31+
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
32+
software for any purpose.
3433

35-
If you like the project, feel free to ⭐ [@danimydev/env on GitHub](https://github.com/danimydev/env)!
34+
If you like the project, feel free to ⭐
35+
[@danimydev/env on GitHub](https://github.com/danimydev/env)!

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@danimydev/env",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"license": "MIT",
55
"exports": "./src/mod.ts",
66
"tasks": {

src/boolean.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { StandardSchema } from "./standard-schema.ts";
1+
import type { StandardSchemaV1 } from "./standard.ts";
22

33
/**
44
* A schema representing a boolean value.
55
*
66
* Validates boolean values, numeric 0/1, and string representations
77
* such as `"true"`, `"false"`, `"1"`, `"0"`.
88
*/
9-
export interface BooleanSchema extends StandardSchema<boolean> {
9+
export interface BooleanSchema extends StandardSchemaV1<boolean> {
1010
/** The literal type of the schema */
1111
type: "boolean";
1212

@@ -31,24 +31,31 @@ export interface BooleanSchema extends StandardSchema<boolean> {
3131
export function boolean(message: string = "Expected a boolean"): BooleanSchema {
3232
return {
3333
type: "boolean",
34-
message,
34+
message: message,
3535
"~standard": {
3636
validate(value) {
3737
if (typeof value === "boolean") return { value };
3838

3939
if (typeof value === "string" || value instanceof String) {
4040
const primitive = value.toString();
41+
4142
if (["TRUE", "True", "true", "1"].includes(primitive)) {
4243
return { value: true };
4344
}
45+
4446
if (["FALSE", "False", "false", "0"].includes(primitive)) {
4547
return { value: false };
4648
}
4749
}
4850

4951
if (typeof value === "number") {
50-
if (value === 1) return { value: true };
51-
if (value === 0) return { value: false };
52+
if (value === 1) {
53+
return { value: true };
54+
}
55+
56+
if (value === 0) {
57+
return { value: false };
58+
}
5259
}
5360

5461
return { issues: [{ message }] };

src/env.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
import type { StandardSchema } from "./standard-schema.ts";
2-
import { parse } from "./parse.ts";
1+
import type { StandardSchemaV1 } from "./standard.ts";
2+
import type { OptionalSchema } from "./optional.ts";
3+
import { object } from "./object.ts";
34
import { getEnv, getRuntime } from "./runtime.ts";
5+
import { parse } from "./parse.ts";
46

57
/**
6-
* Reads and validates environment variables based on the provided schema.
8+
* Reads and validates environment variables based on the provided record of schemas.
79
*
810
* Main entry point of the `@danimydev/env` library.
911
*
1012
* ```ts
11-
* import env, { string, number, boolean, object, optional } from "@danimydev/env";
13+
* import env, { string, number, boolean, optional } from "@danimydev/env";
1214
*
13-
* const config = env(
14-
* object({
15+
* const safeEnv = env({
1516
* NODE_ENV: string(),
1617
* PORT: number(),
1718
* DEBUG: optional(boolean()),
18-
* })
19-
* );
19+
* });
2020
* ```
2121
*
2222
* @typeParam TSchema - The schema type.
23-
* @param schema - A `StandardSchema` describing the expected environment shape.
23+
* @param schema - A `StandardSchemaV1` describing the expected environment shape.
2424
* @returns The validated and typed environment object.
2525
* @throws {Error} If any variable fails validation.
2626
*/
27-
export function env<TSchema extends StandardSchema>(
28-
schema: TSchema,
29-
): StandardSchema.InferOutput<TSchema> {
30-
const runtime = getRuntime();
31-
const env = getEnv(runtime);
32-
return parse(schema, env);
27+
export function env<
28+
TShape extends Record<
29+
string,
30+
| StandardSchemaV1<string | number | boolean>
31+
| OptionalSchema<StandardSchemaV1<string | number | boolean>>
32+
>,
33+
>(
34+
shape: TShape,
35+
): { [K in keyof TShape]: StandardSchemaV1.InferOutput<TShape[K]> } {
36+
return parse(object(shape), getEnv(getRuntime()));
3337
}

src/mod.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { object } from "./object.ts";
21
import { string } from "./string.ts";
32
import { number } from "./number.ts";
43
import { boolean } from "./boolean.ts";
54
import { optional } from "./optional.ts";
6-
import { parse } from "./parse.ts";
75
import { env } from "./env.ts";
86

97
/**
@@ -14,6 +12,6 @@ import { env } from "./env.ts";
1412
* - `parse`: Parse and validate arbitrary input against a schema.
1513
* - `string`, `number`, `boolean`, `object`, `optional`: Standard schema constructors.
1614
*/
17-
export { boolean, env, number, object, optional, parse, string };
15+
export { boolean, env, number, optional, string };
1816

1917
export default env;

src/number.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { StandardSchema } from "./standard-schema.ts";
1+
import type { StandardSchemaV1 } from "./standard.ts";
22

33
/**
44
* A schema representing a numeric value.
55
*
66
* Validates that the input is a number or a string representing a number.
77
*/
8-
export interface NumberSchema extends StandardSchema<number> {
8+
export interface NumberSchema extends StandardSchemaV1<number> {
99
/** The literal type of the schema */
1010
type: "number";
1111

src/object.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { StandardSchema } from "./standard-schema.ts";
1+
import type { StandardSchemaV1 } from "./standard.ts";
22
import type { OptionalSchema } from "./optional.ts";
33

44
/**
@@ -9,13 +9,13 @@ import type { OptionalSchema } from "./optional.ts";
99
export interface ObjectSchema<
1010
TShape extends Record<
1111
string,
12-
| StandardSchema<string | number | boolean>
13-
| OptionalSchema<StandardSchema<string | number | boolean>>
12+
| StandardSchemaV1<string | number | boolean>
13+
| OptionalSchema<StandardSchemaV1<string | number | boolean>>
1414
>,
1515
> extends
16-
StandardSchema<
16+
StandardSchemaV1<
1717
Record<string, unknown>,
18-
{ [K in keyof TShape]: StandardSchema.InferOutput<TShape[K]> }
18+
{ [K in keyof TShape]: StandardSchemaV1.InferOutput<TShape[K]> }
1919
> {
2020
/** The literal type of the schema */
2121
type: "object";
@@ -45,8 +45,8 @@ export interface ObjectSchema<
4545
export function object<
4646
TShape extends Record<
4747
string,
48-
| StandardSchema<string | number | boolean>
49-
| OptionalSchema<StandardSchema<string | number | boolean>>
48+
| StandardSchemaV1<string | number | boolean>
49+
| OptionalSchema<StandardSchemaV1<string | number | boolean>>
5050
>,
5151
>(shape: TShape, message = "Expected an object"): ObjectSchema<TShape> {
5252
return {
@@ -62,7 +62,7 @@ export function object<
6262
}
6363

6464
const result: Record<string, unknown> = {};
65-
const issues: StandardSchema.Issue[] = [];
65+
const issues: StandardSchemaV1.Issue[] = [];
6666

6767
for (const key in shape) {
6868
const fieldSchema = shape[key];
@@ -85,15 +85,15 @@ export function object<
8585

8686
return issues.length > 0
8787
? { issues }
88-
: { value: result } as StandardSchema.SuccessResult<
89-
{ [K in keyof TShape]: StandardSchema.InferOutput<TShape[K]> }
88+
: { value: result } as StandardSchemaV1.SuccessResult<
89+
{ [K in keyof TShape]: StandardSchemaV1.InferOutput<TShape[K]> }
9090
>;
9191
},
9292

9393
types: {
9494
input: {} as Record<string, unknown>,
9595
output: {} as {
96-
[K in keyof TShape]: StandardSchema.InferOutput<TShape[K]>;
96+
[K in keyof TShape]: StandardSchemaV1.InferOutput<TShape[K]>;
9797
},
9898
},
9999
},

src/optional.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import type { StandardSchema } from "./standard-schema.ts";
1+
import type { StandardSchemaV1 } from "./standard.ts";
22

33
/**
44
* A schema representing an optional value.
55
*
66
* Wraps another schema and allows `undefined` or `null` values.
77
*/
88
export interface OptionalSchema<
9-
TSchema extends StandardSchema<string | number | boolean>,
10-
> extends StandardSchema<StandardSchema.InferOutput<TSchema> | undefined> {
9+
TSchema extends StandardSchemaV1<string | number | boolean>,
10+
> extends StandardSchemaV1<StandardSchemaV1.InferOutput<TSchema> | undefined> {
1111
/** The literal type of the schema */
1212
type: "optional";
1313

@@ -32,7 +32,7 @@ export interface OptionalSchema<
3232
* @returns An `OptionalSchema` instance.
3333
*/
3434
export function optional<
35-
TSchema extends StandardSchema<string | number | boolean>,
35+
TSchema extends StandardSchemaV1<string | number | boolean>,
3636
>(schema?: TSchema, message = "Value is optional"): OptionalSchema<TSchema> {
3737
return {
3838
type: "optional",
@@ -43,6 +43,7 @@ export function optional<
4343
if (value === undefined || value === null || schema === undefined) {
4444
return { value: undefined };
4545
}
46+
4647
return schema["~standard"].validate(value);
4748
},
4849
},

src/parse.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { StandardSchema } from "./standard-schema.ts";
1+
import type { StandardSchemaV1 } from "./standard.ts";
22

33
/**
44
* Validates and parses an input according to a schema.
@@ -12,15 +12,15 @@ import type { StandardSchema } from "./standard-schema.ts";
1212
* ```
1313
*
1414
* @typeParam TSchema - The schema type.
15-
* @param schema - The `StandardSchema` to validate against.
15+
* @param schema - The `StandardSchemaV1` to validate against.
1616
* @param input - The value to validate.
1717
* @returns The validated value.
1818
* @throws {Error} If validation fails.
1919
*/
20-
export function parse<TSchema extends StandardSchema>(
20+
export function parse<TSchema extends StandardSchemaV1>(
2121
schema: TSchema,
2222
input: unknown,
23-
): StandardSchema.InferOutput<TSchema> {
23+
): StandardSchemaV1.InferOutput<TSchema> {
2424
const result = schema["~standard"].validate(input);
2525

2626
if (result.issues) {

0 commit comments

Comments
 (0)