|
| 1 | +# @wuespace/envar |
| 2 | + |
| 3 | +[](https://jsr.io/@wuespace) |
| 4 | +[](https://jsr.io/@wuespace/envar) |
| 5 | +[](https://jsr.io/@wuespace/envar) |
| 6 | +[](https://github.com/wuespace/envar/actions/workflows/deno-ci.yml) |
| 7 | +[](https://github.com/wuespace/envar/actions/workflows/publish-jsr.yml) |
| 8 | + |
| 9 | +🚀 **Envar** makes it easy to add configurability to your Deno applications. |
| 10 | +It supports loading configuration from environment variables as well as |
| 11 | +specifying default values. |
| 12 | +It even supports configuration values from files specified by environment |
| 13 | +variables to provide first-class support for secrets and the like in your |
| 14 | +Docker Swarm or Kubernetes deployments. |
| 15 | + |
| 16 | +## 📦 Usage |
| 17 | + |
| 18 | +You can use Envar in your Deno application by importing it from the |
| 19 | +`jsr:@wuespace/envar` module. |
| 20 | + |
| 21 | +```tsx |
| 22 | +// Import the initVariable function from the module |
| 23 | +import { initVariable, EnvNotSetError } from "jsr:@wuespace/envar"; |
| 24 | +import { z } from "npm:zod"; |
| 25 | + |
| 26 | +// Initialize the application variables |
| 27 | +await initVariable("PORT", z.string().match(/^[0-9]{1,5}$/), "8080"); |
| 28 | +await initVariable("SECRET", z.string().min(32)); |
| 29 | +// At this point, we can rest assured that we have valid values for |
| 30 | +// PORT and SECRET. Otherwise, the promises would have been rejected. |
| 31 | + |
| 32 | +// Access the variables like you normally would. |
| 33 | +// Everything's synchronous now, so it's quite easy. |
| 34 | +console.log(Deno.env.get("PORT")); |
| 35 | +console.log(Deno.env.get("SECRET")); |
| 36 | + |
| 37 | +// For type safety, you'll need to check if it's undefined: |
| 38 | +const port = Deno.env.get("PORT"); |
| 39 | +if (port == undefined) { |
| 40 | + // Automatically generate a nice error message for the user |
| 41 | + throw new EnvNotSetError("PORT"); |
| 42 | +} |
| 43 | + |
| 44 | +// Alternatively, you can also use process.env in Deno 2.0+ |
| 45 | +console.log(process.env.PORT); |
| 46 | +``` |
| 47 | + |
| 48 | +## 👥 Authors |
| 49 | + |
| 50 | +This package was created and is maintained by [WüSpace e. V.](https://github.com/wuespace) |
| 51 | + |
| 52 | +Its primary author is [Zuri Klaschka](https://github.com/pklaschka). |
| 53 | + |
| 54 | +## 📄 License |
| 55 | + |
| 56 | +This package is licensed under the MIT license. See the [LICENSE](LICENSE) file for more information. |
0 commit comments