https://github.com/pokatomnik/deno-environment
Friendly wrapper for Deno env
https://github.com/pokatomnik/deno-environment
deno env environment-variables typescript
Last synced: about 1 month ago
JSON representation
Friendly wrapper for Deno env
- Host: GitHub
- URL: https://github.com/pokatomnik/deno-environment
- Owner: pokatomnik
- License: mit
- Created: 2021-03-23T15:28:13.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-03-23T21:52:33.000Z (over 5 years ago)
- Last Synced: 2026-02-16T18:10:00.738Z (4 months ago)
- Topics: deno, env, environment-variables, typescript
- Language: TypeScript
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# deno-environment
[Deno](https://deno.land/) package for environment variables management.
Currently, Deno exposes environment variables through `Deno.env`. This package is for converting environment variables to expected types.
## Exports are:
### `StringValue`
Typescript [string type](https://www.typescriptlang.org/docs/handbook/basic-types.html#string). It checks if the environment value exists and does nothing with It.
### `NumberValue`
Typescript [number type](https://www.typescriptlang.org/docs/handbook/basic-types.html#number). Converts env value to number.
### `IntValue`
Integer type. Converts the environment variable to a number as well as `NumberValue`, but checks if the environment variable can be converted to `integer`.
### `BooleanValue`
Typescript [boolean type](https://www.typescriptlang.org/docs/handbook/basic-types.html#boolean). Converts the environment variable to a `boolean` from string `'true'` or `'false'`.
### `EnvValue`
Is a generic abstract class that must be extended in this way:
```typescript
interface MyType {
title: string;
tome: number;
isRead: boolean;
}
class MyType extends EnvValue {
protected mapper(rawValue: string): MyType {
return JSON.parse(rawValue);
}
}
```
So the raw env value could be `'{"title": "Harry Potter", "tome": 1, "isRead": false}'`. And the Environment converter can use JSON parser to do all the work.
## Examples
Let's imagine we have the environment variable `port` with value `8080`. We could access that in this way:
```typescript
const port = Number(Deno.env.get('port'));
```
But what if this environment variable is missing? We should check It first:
```typescript
const portString = Deno.env.get('port');
const portNumber = Number(portString);
if (portNumber === undefined) {
throw new Error('port number is missing');
}
if (Number.isNaN(portNumber)) {
throw new Error('Incorrect port variable');
}
```
This routine could be easily done with this:
```typescript
const portNumber = new IntValue('port').get();
```
## Please, note
> If a particular variable is not convertible, `.get()` method throws the exception.
## Importing the module
> `mod.ts` is an entrypoint, other files should not be used.
## Tests
`deno test --allow-env`