composable-functions
Advanced tools
Comparing version 1.0.0-beta-20240523-2 to 1.0.0-beta-20240523-3
{ | ||
"name": "composable-functions", | ||
"version": "1.0.0-beta-20240523-2", | ||
"version": "1.0.0-beta-20240523-3", | ||
"description": "Types and functions to make composition easy and safe", | ||
@@ -5,0 +5,0 @@ "author": "Seasoned", |
@@ -163,2 +163,25 @@ <p align="center"> | ||
You can throw anything derived from `Error`. Check [this documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#custom_error_types) on how to define your custom errors. The library will also wrap anything that does not extends `Error`, just to keep compatibility with code-bases that throw strings or objects. | ||
```typescript | ||
import { composable } from 'composable-functions' | ||
class NotFoundError extends Error { | ||
constructor(message) { | ||
super(message); | ||
this.name = 'NotFoundError'; | ||
} | ||
} | ||
const getUser = composable((userId: string, users: Array<string>) => { | ||
// ^? Composable<(userId: string, users: Array<string>) => string> | ||
const result = users.find(({id}) => id === userId) | ||
if(result == undefined) throw new NotFoundError(`userId ${userId} was not found`) | ||
return result | ||
}) | ||
``` | ||
The library defines a few custom errors out of the box but these will be more important later on, whend dealing with external input and schemas. | ||
See [the errors module](./src/errors.ts) for more details. | ||
### Catching | ||
@@ -170,4 +193,3 @@ You can catch an error in a `Composable`, using `catchFailure` which is similar to `map` but will run whenever the first composable fails: | ||
const getUser = composable((id: string) => fetchUser(id)) | ||
// ^? Composable<(id: string) => User> | ||
// assuming we have the definitions from the previous example | ||
const getOptionalUser = catchFailure(getUser, (errors, id) => { | ||
@@ -177,3 +199,3 @@ console.log(`Failed to fetch user with id ${id}`, errors) | ||
}) | ||
// ^? Composable<(id: string) => User | null> | ||
// ^? Composable<(id: string) => string | null> | ||
``` | ||
@@ -180,0 +202,0 @@ |
153564
42
274