Phantom Stories is a TypeScript library for managing asynchronous data states and validation using functional programming (FP) principles. Built for seamless integration with Redux Toolkit, Redux Observables, and React, it provides algebraic data types (ADTs) like Resource
and Validation
to handle complex state transitions and form validation declaratively. Whether you're fetching data, managing loading states, or validating user input, Phantom Stories brings FP elegance to your JavaScript applications.
Data
: Successful state with a value.Query
: Loading state.Empty
: No data state.Failure
: Error state with messages.Passing
: Valid state with a value.Failing
: Invalid state with error messages.map
), applicative functors (ap
), monads (chain
), and semigroups (concat
) for declarative transformations.ResourceRender
and ValidationRender
components.Install the library via npm:
npm install @galileopy/phantom-stories
Ensure you have the peer dependencies installed:
"peerDependencies": {
"react": "^19.0.0",
"react-dom": "^19.0.0"
}
Phantom Stories provides two main ADTs: Resource
for async state management and Validation
for data validation. Below are quick examples, followed by a showcase of a settings page.
Manage async data states in a Redux/React application:
import { Resource, ResourceRender } from '@galileopy/phantom-stories';
import React from 'react';
// Define components for each Resource state
const DataComponent: React.FC = ({ value }) => <p>Data: {value}</p>;
const QueryComponent: React.FC = () => <p>Loading...</p>;
const EmptyComponent: React.FC = () => <p>No data</p>;
const FailureComponent: React.FC = ({ messages }) => <p>Error: {messages.join(', ')}</p>;
// Create a Resource instance
const userResource = Resource.Data({ id: '123', name: 'Jane' }, { endpoint: '/users' });
// Transform data with map
const upperCaseName = userResource.map(user => ({
...user,
name: user.name.toUpperCase(),
}));
// Render in React
function App() {
return (
<ResourceRender
resource={upperCaseName}
Data={DataComponent}
Query={QueryComponent}
Empty={EmptyComponent}
Failure={FailureComponent}
/>
);
}
Validate form input with the Validation
ADT:
import { Validation, ValidationRender } from '@galileopy/phantom-stories';
import React from 'react';
// Define components for Validation states
const PassingComponent: React.FC = ({ value }) => <p>Valid: {value}</p>;
const FailingComponent: React.FC = ({ messages }) => <ul>{messages.map(msg => <li key={msg}>{msg}</li>)}</ul>;
// Validate a password
const passwordValidation = password =>
password.length >= 8
? Validation.Passing(password)
: Validation.Failing(['Password must be at least 8 characters']);
// Render in React
function Form() {
const password = 'secret';
return (
<ValidationRender
validation={passwordValidation(password)}
Passing={PassingComponent}
Failing={FailingComponent}
/>
);
}
Explore the full API documentation at https://galileopy.github.io/phantom-stories/. It includes detailed TSDoc for Resource
, Validation
, ResourceRender
, and ValidationRender
, with examples and type information.
Run the test suite to verify functionality:
npm run test
To run specific tests:
npm run test:validation # Tests for Validation ADT
The library includes comprehensive tests for:
Resource
, Validation
).Validation.concat
).Resource.getDataOr
, empty Validation.Failing
messages).ResourceRender
, ValidationRender
).Contributions are welcome! To contribute:
https://github.com/galileopy/phantom-stories
.git checkout -b feature/your-feature
.git commit -m "Add your feature"
.git push origin feature/your-feature
.Please follow the coding style (Prettier, ESLint) and include tests for new features. Check the issues for ideas or report bugs.
To set up the project locally:
git clone https://github.com/galileopy/phantom-stories.git
cd phantom-stories
npm install
npm run build
npm run test
npm run docs
npm run build
generates build/index.js
and build/index.es.js
.npm run test
runs Jest with TypeScript support.npm run docs
generates TypeDoc output in docs/
, deployed to GitHub Pages.This project is licensed under the GNU General Public License v3.0 or later. See the LICENSE file for details.