{"id":13495559,"url":"https://github.com/DZakh/rescript-envsafe","last_synced_at":"2025-03-28T16:33:01.173Z","repository":{"id":57688863,"uuid":"492307523","full_name":"DZakh/rescript-envsafe","owner":"DZakh","description":"🔒 Makes sure you don't accidentally deploy apps with missing or invalid environment variables","archived":false,"fork":false,"pushed_at":"2024-07-15T08:49:02.000Z","size":571,"stargazers_count":28,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-28T09:46:30.793Z","etag":null,"topics":["configuration","dotenv","environment","environment-variables","nodejs","rescript"],"latest_commit_sha":null,"homepage":"","language":"ReScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DZakh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-14T19:28:53.000Z","updated_at":"2024-09-02T10:53:28.000Z","dependencies_parsed_at":"2023-01-24T06:46:18.868Z","dependency_job_id":"fff68ade-91a7-4c67-beca-8e0c23e8fb44","html_url":"https://github.com/DZakh/rescript-envsafe","commit_stats":{"total_commits":26,"total_committers":1,"mean_commits":26.0,"dds":0.0,"last_synced_commit":"4937519cd3c80534235af7b78b42f503e9dde3a2"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DZakh%2Frescript-envsafe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DZakh%2Frescript-envsafe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DZakh%2Frescript-envsafe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DZakh%2Frescript-envsafe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DZakh","download_url":"https://codeload.github.com/DZakh/rescript-envsafe/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222395896,"owners_count":16977646,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["configuration","dotenv","environment","environment-variables","nodejs","rescript"],"created_at":"2024-07-31T19:01:35.898Z","updated_at":"2025-03-28T16:33:01.103Z","avatar_url":"https://github.com/DZakh.png","language":"ReScript","funding_links":[],"categories":["ReScript"],"sub_categories":[],"readme":"[![npm](https://img.shields.io/npm/dm/rescript-envsafe)](https://www.npmjs.com/package/rescript-envsafe)\n\n# ReScript `env`safe 🔒\n\nValidate access to environment variables and parse them to the right type. Makes sure you don't accidentally deploy apps with missing or invalid environment variables.\n\n```\n========================================\n❌ Invalid environment variables:\n    API_URL (\"http//example.com/graphql\"): Invalid url\n💨 Missing environment variables:\n    MY_VAR: Disallowed empty string\n    PORT: Missing value\n========================================\n```\n\nHeavily inspired by the great project [envsafe](https://github.com/KATT/envsafe), but designed with care for ReScript users:\n\n- **Always strict** - only access the variables you have defined\n- Built for node.js **and** the browser\n- **Composable** parsers with **[rescript-schema](https://github.com/DZakh/rescript-schema)**\n\n## Basic usage\n\n```rescript\n%%private(let envSafe = EnvSafe.make())\n\nlet nodeEnv = envSafe-\u003eEnvSafe.get(\n  \"NODE_ENV\",\n  S.union([\n    S.literal(#production),\n    S.literal(#development),\n    S.literal(#test),\n  ]),\n  ~devFallback=#development,\n)\nlet port = envSafe-\u003eEnvSafe.get(\"PORT\", S.int-\u003eS.port, ~fallback=3000)\nlet apiUrl = envSafe-\u003eEnvSafe.get(\"API_URL\", S.string-\u003eS.url, ~devFallback=\"https://example.com/graphql\")\nlet auth0ClientId = envSafe-\u003eEnvSafe.get(\"AUTH0_CLIENT_ID\", S.string)\nlet auth0Domain = envSafe-\u003eEnvSafe.get(\"AUTH0_DOMAIN\", S.string)\n\n// 🧠 If you forget to close `envSafe` then invalid vars end up being `undefined` leading to an expected runtime error.\nenvSafe-\u003eEnvSafe.close\n```\n\n## Install\n\n```sh\nnpm install rescript-envsafe rescript-schema\n```\n\nThen add `rescript-envsafe` and `rescript-schema` to `bs-dependencies` in your `rescript.json`:\n\n```diff\n{\n  ...\n+ \"bs-dependencies\": [\"rescript-envsafe\", \"rescript-schema\"],\n+ \"bsc-flags\": [\"-open RescriptSchema\"],\n}\n```\n\n## API Reference\n\n### **`EnvSafe.make`**\n\n`(~env: EnvSafe.env=?) =\u003e EnvSafe.t`\n\n```rescript\n%%private(let envSafe = EnvSafe.make(~env=%raw(\"window.__ENVIRONMENT__\")))\n```\n\nCreates `envSafe` to start working with environment variables. By default it uses `process.env` as a base for plucking the vars, but it can be overridden using the `env` argument.\n\n### **`EnvSafe.get`**\n\n`(EnvSafe.t, string, S.t\u003c'value\u003e, ~allowEmpty: bool=?, ~fallback: 'value=?, ~devFallback: 'value=?, ~input: option\u003cstring\u003e=?) =\u003e 'value`\n\n```rescript\nlet port = envSafe-\u003eEnvSafe.get(\"PORT\", S.int-\u003eS.port, ~fallback=3000)\n```\n\nGets an environment variable from `envSafe` applying coercion and parsing logic of `schema`.\n\n#### Possible options\n\n| Name          | Type          | Description                                                                                                                                                                                                                                                                                                                                                           |\n| ------------- | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `name`        | `string`      | Name of the environment variable                                                                                                                                                                                                                                                                                                                                      |\n| `schema`      | `S.t\u003c'value\u003e` | A schema created with **[rescript-schema](https://github.com/DZakh/rescript-schema)**. It's used for coercion and parsing. For bool schemas coerces `\"0\", \"1\", \"true\", \"false\", \"t\", \"f\"` to boolean values. For int, float and bigint schemas coerces string to number. For other non-string schemas the value is coerced using `JSON.parse` before being validated. |\n| `fallback`    | `'value=?`    | A fallback value when the environment variable is missing.                                                                                                                                                                                                                                                                                                            |\n| `devFallback` | `'value=?`    | A fallback value to use only when `NODE_ENV` is not `production`. This is handy for env vars that are required for production environments, but optional for development and testing.                                                                                                                                                                                 |\n| `input`       | `string=?`    | As some environments don't allow you to dynamically read env vars, we can manually put it in as well. Example: `input=%raw(\"process.env.NEXT_PUBLIC_API_URL\")`.                                                                                                                                                                                                       |\n| `allowEmpty`  | `bool=false`  | Default behavior is `false` which treats empty strings as the value is missing. if explicit empty strings are OK, pass in `true`.                                                                                                                                                                                                                                     |\n\n### **`EnvSafe.close`**\n\n`(EnvSafe.t) =\u003e unit`\n\n```rescript\nenvSafe-\u003eEnvSafe.close\n```\n\nIt makes a readable summary of your issues, `console.error`-log an error, `window.alert()` with information about the missing envrionment variable if you're in the browser, throws an error (will exit the process with a code 1 in node).\n\n\u003e 🧠 If you forget to close `envSafe` then invalid vars end up being `undefined` leading to an expected runtime error.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDZakh%2Frescript-envsafe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FDZakh%2Frescript-envsafe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDZakh%2Frescript-envsafe/lists"}