RlxJS provides error handling functions inspired by rust like Some, None, Ok, Err.
Released under the MIT License. See the LICENSE file for further details.
In shell execute
npm install rlx-js
Or update package.json
to include a dependency on
"dependencies": {
"rlx-js": "0.2.x"
}
import { Some, None } from 'rlx-js';
const getArg = () => process.argv.length > 2 ? Some(process.argv[2]) : None();
const message = getArg().mapOrElse(() => "Missing Argument", msg => `Echo: ${msg}`);
console.log(message);
import { Ok, Err, FlattenResult } from 'rlx-js';
const fetchData = async (url) => {
const res = await fetch(url);
if (res.ok) {
return Ok(res);
}
return Err(`Invalid status ${res.status}`);
};
const validateContentType = (res) => {
const contentType = res.headers.get('content-type');
if (contentType.includes('application/json')) {
return Ok(res);
}
return Err(`Invalid content type ${contentType}`);
};
const loadText = async (res) => {
const text = await res.text();
return text;
};
const text = await FlattenResult(fetchData('...'))
.andThen(validateContentType)
.map(loadText)
.unwrapOrElse(err => err);
console.log(text);