@galileopy/phantom-stories
v0.0.8
Published
**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 algebra
Downloads
8
Maintainers
Readme
Phantom Stories
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.
Features
- Resource ADT: Manage async states with four variants:
Data: Successful state with a value.Query: Loading state.Empty: No data state.Failure: Error state with messages.
- Validation ADT: Handle validation with two variants:
Passing: Valid state with a value.Failing: Invalid state with error messages.
- FP Constructs: Supports functors (
map), applicative functors (ap), monads (chain), and semigroups (concat) for declarative transformations. - TypeScript Support: Strong typing with comprehensive TSDoc for type safety and IDE support.
- React Integration: Render ADTs with
ResourceRenderandValidationRendercomponents. - Redux Compatibility: Works with Redux Toolkit and Redux Observables for state management and async flows.
- Open Source: Licensed under GPL-3.0-or-later
Installation
Install the library via npm:
npm install @galileopy/phantom-storiesEnsure you have the peer dependencies installed:
"peerDependencies": {
"react": "^19.0.0",
"react-dom": "^19.0.0"
}Usage
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.
Resource Example
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}
/>
);
}Validation Example
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}
/>
);
}API Documentation
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.
Testing
Run the test suite to verify functionality:
npm run testTo run specific tests:
npm run test:validation # Tests for Validation ADTThe library includes comprehensive tests for:
- Functor, applicative functor, and monad laws (
Resource,Validation). - Semigroup operations (
Validation.concat). - Edge cases (e.g.,
Resource.getDataOr, emptyValidation.Failingmessages). - React component rendering (
ResourceRender,ValidationRender).
Contributing
Contributions are welcome! To contribute:
- Fork the repository:
https://github.com/galileopy/phantom-stories. - Create a feature branch:
git checkout -b feature/your-feature. - Commit changes:
git commit -m "Add your feature". - Push to your fork:
git push origin feature/your-feature. - Open a pull request.
Please follow the coding style (Prettier, ESLint) and include tests for new features. Check the issues for ideas or report bugs.
Development
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- Build:
npm run buildgeneratesbuild/index.jsandbuild/index.es.js. - Tests:
npm run testruns Jest with TypeScript support. - Documentation:
npm run docsgenerates TypeDoc output indocs/, deployed to GitHub Pages.
License
This project is licensed under the GNU General Public License v3.0 or later. See the LICENSE file for details.
Contact
- Author: Galileo Sánchez ([email protected])
- Blog: https://blog.galileopy.com/
- Issues: https://github.com/galileopy/phantom-stories/issues
