https://github.com/fredericoo/fx-nano
https://github.com/fredericoo/fx-nano
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/fredericoo/fx-nano
- Owner: fredericoo
- License: mit
- Created: 2025-06-25T08:33:05.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-06-30T21:09:04.000Z (12 months ago)
- Last Synced: 2025-07-24T20:42:06.833Z (11 months ago)
- Language: TypeScript
- Size: 96.7 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# fx-nano
hyper lightweight (300bytes gzip) typesafe error library loosely inspired by golang and effect
## incremental adoption
no need to rewrite your whole app. you can start with smaller and simpler functions at the edges of your app just fine
## true error type-safety
even the most robust libraries don’t actually return type-safe errors because they fail to consider javascript’s goblin nature
e.g.:
```ts
const getPokemonSpriteImage = async (name: string): string => {
const res = await fetch(`https://pokeapi.co/api/v2/${name}`);
const json = await res.json();
if (typeof json.sprites.front_default === 'string') return json.sprites.front_default;
throw new Error("Missing sprite");
}
```
this function seems harmless and straightforward. it could still fail in many ways:
- requester has no connection (e.g.: "Fetch failed")
- response is not json (e.g.: "Unexpected token '<' at position 0")
- `json.sprites` does not exist
- `json.sprites.front_default` does not exist
you can make it type-safe by first returning `fx.ok` or `fx.fail`:
```ts
import { fx } from "fx-nano";
const getPokemonSpriteImage = async (name: string): Result<"Missing sprite", string> => {
const res = await fetch(`https://pokeapi.co/api/v2/${name}`);
const json = await res.json();
if (typeof json.sprites.front_default === 'string') return fx.ok(json.sprites.front_default);
return fx.fail("Missing sprite");
}
```
and then when you need to use it, wrap it in `fx.runPromise`:
```ts
import { fx } from "fx-nano";
const getPokemonSpriteImage = async (name: string) => {
const res = await fetch(`https://pokeapi.co/api/v2/${name}`);
const json = await res.json();
if (typeof json.sprites.front_default === 'string') return fx.ok(json.sprites.front_default);
return fx.fail("Missing sprite");
}
// in your app
const main = () => {
const [error, sprite] = fx.runPromise(getPokemonSprite('ditto'));
}
```
## Installation
```bash
npm add fx-nano
```