git.iko.soy/rowtype-yoga/purescript-yoga-om-node
on main help sign in
git clone https://git.iko.soy/rowtype-yoga/purescript-yoga-om-node.git

yoga-om-node

Node.js-specific extensions for yoga-om, providing convenient wrappers for file system operations, environment variables, and more.

Installation

spago install yoga-om-core yoga-om-node

What's Included

All operations are wrapped in Om with proper error types.

Usage

Reading Environment Variables

import Yoga.Om.Node as Node

loadConfig :: Om {} (envNotFound :: { variable :: String }) Config
loadConfig = do
  apiKey <- Node.getEnv "API_KEY"
  dbUrl <- Node.getEnv "DATABASE_URL"
  pure { apiKey, dbUrl }

File Operations

import Yoga.Om.Node as Node

processFile :: Om {} (fileReadError :: _, fileWriteError :: _) Unit
processFile = do
  content <- Node.readTextFile "input.txt"
  let processed = String.toUpper content
  Node.writeTextFile "output.txt" processed

Checking File Existence

import Yoga.Om.Node as Node

safeRead :: FilePath -> Om {} _ (Maybe String)
safeRead path = do
  fileExists <- Node.exists path
  if fileExists
    then Just <$> Node.readTextFile path
    else pure Nothing

Error Types

The package defines error type aliases for common failures:

These can be extended with your own error types:

type MyErrors r = Node.FileError + Node.EnvError + 
  ( customError :: String | r )

Testing

Tests are colocated with the implementation in the test/ directory:

# Run tests
cd packages/yoga-om-node && spago test

# Or from workspace root
bun test:node

See test/Test/Node.purs for test examples.

Package Structure

yoga-om-node/
├── src/
│   └── Yoga/
│       └── Om/
│           └── Node.purs       # Node.js extensions
└── test/                        # Tests colocated here!
    └── Test/
        ├── Main.purs           # Test runner
        └── Node.purs           # Node-specific tests

See Also

For more examples, see the tests and main README.