-
Notifications
You must be signed in to change notification settings - Fork 6
Migrate basic examples to be JS based #457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cfd9a53
feat: Add a new `with-typescript` example and convert the `basic` exa…
yamcodes 1e3c3ec
[autofix.ci] apply automated fixes
autofix-ci[bot] 76b7871
feat: Add a Bun + TypeScript example and refine the existing Bun exam…
yamcodes 154b69b
refactor: Consolidate and convert examples to TypeScript, renaming `w…
yamcodes e93ed84
chore: Rename example package names and update video demo StackBlitz …
yamcodes 846882f
docs: update example READMEs to clarify JavaScript usage and remove T…
yamcodes c3dd8f5
Update apps/playgrounds/node/index.js
yamcodes 53578c7
Update examples/basic-js/README.md
yamcodes 3e02aea
docs: clarify pnpm rule exception for examples directory.
yamcodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import arkenv, { type } from "arkenv"; | ||
|
|
||
| const Env = type({ | ||
| HOST: "string.host", | ||
| PORT: "number.port", | ||
| NODE_ENV: "'development' | 'production' | 'test' = 'development'", | ||
| ALLOWED_ORIGINS: type("string[]").default(() => []), | ||
| DEBUG: "boolean = true", | ||
| }); | ||
|
|
||
| const env = arkenv(Env, process.env); | ||
|
|
||
| // Automatically validate and parse process.env | ||
| // Values are validated and parsed at runtime! | ||
| const host = env.HOST; | ||
| const port = env.PORT; | ||
| const nodeEnv = env.NODE_ENV; | ||
| const allowedOrigins = env.ALLOWED_ORIGINS; | ||
| const debug = env.DEBUG; | ||
| console.log({ | ||
| host, | ||
| port, | ||
| nodeEnv, | ||
| allowedOrigins, | ||
| debug, | ||
| }); | ||
|
|
||
| export default env; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| HOST=localhost | ||
| PORT=3000 | ||
| NODE_ENV=development |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # ArkEnv basic example (plain JavaScript) | ||
|
|
||
| This example shows how to use ArkEnv in a basic Node.js application. | ||
|
|
||
|
|
||
| ## What's inside? | ||
|
|
||
| The example demonstrates: | ||
| - Setting up environment variables with ArkEnv | ||
| - Using default values | ||
| - Runtime-validated environment configuration | ||
|
|
||
| ## Getting started | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| Make sure you have [Node.js](https://nodejs.org) installed. We recommend using [nvm](https://github.com/nvm-sh/nvm) to install it. | ||
|
|
||
| ### Quickstart | ||
|
|
||
| 1. #### Install dependencies | ||
| ```bash | ||
| npm install | ||
| ``` | ||
|
|
||
| 2. #### Start the development server with hot reloading enabled | ||
| ```bash | ||
| npm run dev | ||
| ``` | ||
| :white_check_mark: You will see the environment variables printed in the console. | ||
|
|
||
| ### Adding environment variables | ||
|
|
||
| With the development server running (if it isn't - just run `npm run dev`), let's see how to add a new environment variable. For this example, we'll add a new environment variable called `MY_ENV_VAR`. | ||
|
|
||
| 1. #### Define the new environment variable in the schema as a _required_ string | ||
| ```javascript | ||
| // index.js | ||
| const env = arkenv({ | ||
| // other definitions... | ||
| MY_ENV_VAR: "string" | ||
| }); | ||
| ``` | ||
|
|
||
| 2. #### Notice the development server will show an error | ||
| ```bash | ||
| ArkEnvError: Errors found while validating environment variables | ||
| MY_ENV_VAR must be a string (was missing) | ||
| ``` | ||
| This is **good**! It means the environment variable is required and the type is enforced. Let's see how to fix it. For this example, we will define the environment variable [with a `.env` file](https://arkenv.js.org/docs/arkenv/how-to/load-environment-variables#using-env-files). | ||
|
|
||
| 3. #### Copy the `.env.example` file to `.env` | ||
|
|
||
| To keep the development server running, run this command in a new terminal window: | ||
| ```bash | ||
| cp .env.example .env | ||
| ``` | ||
|
|
||
| 4. #### Add a new environment variable to the `.env` file | ||
| ```bash | ||
| echo "MY_ENV_VAR=new_value" >> .env | ||
| ``` | ||
|
|
||
| Notice the development server will once again print the existing environment variables. | ||
|
|
||
| 5. #### Add the following line to the `index.js` file | ||
| ```javascript | ||
| console.log(env.MY_ENV_VAR); | ||
| ``` | ||
| You will see the new environment variable printed in the console. | ||
|
|
||
| **Congratulations!** :tada: You've just added a new environment variable and printed its value. | ||
|
|
||
| ### Next steps | ||
|
|
||
| - [ArkEnv docs](https://arkenv.js.org/docs/arkenv) | ||
| - [ArkType docs](https://arktype.io/) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "name": "arkenv-example-basic-js", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "start": "node --env-file .env index.js", | ||
| "dev": "node --watch --env-file .env index.js", | ||
| "dev:example": "node --watch --env-file .env.example index.js", | ||
| "clean": "rimraf dist node_modules" | ||
| }, | ||
| "dependencies": { | ||
| "arkenv": "^0.7.5", | ||
| "arktype": "^2.1.22" | ||
| }, | ||
| "packageManager": "npm@11.6.4", | ||
| "engines": { | ||
| "node": "24" | ||
| }, | ||
| "stackblitz": { | ||
| "startCommand": "npm install && npm run dev:example" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ESNext", | ||
| "module": "ESNext", | ||
| "moduleResolution": "Bundler", | ||
| "esModuleInterop": true, | ||
| "strict": true, | ||
| "skipLibCheck": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "outDir": "./dist" | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.