The input type of the bus
The output type of the bus
Private
constructorInternal
Constructor cannot be called directly. Instead, create should be used.
The name used to identify this bus in errors
An optional Predicate exectuted on the output of the bus
A deserialization function
A serialization function
Contains the inner bus if this bus is a wrapper around other busses
Readonly
deserializeA deserialization function
Readonly
innerContains the inner bus if this bus is a wrapper around other busses
Readonly
nameThe name used to identify this bus in errors
Readonly
predicateAn optional Predicate exectuted on the output of the bus
Readonly
serializeA serialization function
Creates a new bus with the given name and bus, returning the result of the first bus chained into the second bus.
The predicates of the chained busses are preserved, but the newly created bus does not have a predicate of its own.
The other bus' output
The other bus to combine with
The name of the new bus
A new bus "this -> other"
Creates a new bus with the given name and bus, returning the result of the first bus if it succeeds, otherwise the result of the second bus.
On failure, returns the errors of both busses.
import * as io from "@prelude-io/core";
// Creates a bus `ADD | UPDATE | DELETE`
const ActionTypeBus = io
.Literal("ADD")
.else(io.Literal("UPDATE"))
.else(io.Literal("DELETE"));
// Note that the following are functionally the same thing. Pay attention to the brackets:
const busA = io.boolean.else(io.string).else(io.number);
const busB = io.boolean.else(io.string.else(io.number));
The other bus' input
The other bus' output
The other bus to combine with
The name of the new bus
A new bus this | other
Attaches a Predicate to a bus. The predicate is applied on the deserialized value before returning.
Add a predicate to a bus:
import * as io from "@prelude-io/core";
import { Predicate } from "prelude-ts";
const isPositive = Predicate.of((n: number) => n > 0);
const positiveNumber = io.number.if("isPositive", isPositive);
console.log(await positiveNumber.deserialize(1)); // => IORight containing `1`
console.log(await positiveNumber.deserialize("one")); // => IOLeft containing errors
console.log(await positiveNumber.deserialize(-1)); // => IOLeft containing errors for condition "isPositive(number)"
Because this nests the original bus, you can call this function multiple times:
import * as io from "@prelude-io/core";
import { Predicate } from "prelude-ts";
const canDivideBy3 = Predicate.of<number>((input) => input % 3 === 0);
const canDivideBy5 = Predicate.of<number>((input) => input % 5 === 0);
// Creates canDivideBy5(canDivideBy3(number))
io.number.if("canDivideBy3", canDivideBy3).if("canDivideBy5", canDivideBy5);
// Creates canDivideBy15(number)
io.number.if("canDivideBy15", canDivideBy3.and(canDivideBy5));
The name of the predicate. Formatted to be "predicateName(busName)"
The predicate to attach to this bus
A new bus with the given predicate attached to it.
Static
createCreates a new bus with the given name and deserializer.
The input type for the new bus
The output type for the new bus
Name of the bus
deserialize function for this bus
Generated using TypeDoc
A bus is a wrapper around two transformers (serialize and deserialize), with some basic logic included such as chaining or unions.
Busses are executed in parallel, so the order of the errors is not guaranteed (but should not matter). When chaining busses, the deserialized value of the first bus is returned, the others are ignored. This might change in the future.
Transformers should ALWAYS return an IOResult, never throw an error. This is a philosophical choice based on functional programming paradigms. Result objects allow functions to explicitly return errors, and the caller can decide what to do with them. Throwing errors is a side effect, and should be avoided.
Predicates can be attached to ensure validity of the external type, that isn't relevant for the transforming process.
Example
Ensure something is a number:
Example
Add a predicate to a bus: