https://github.com/misuken-now/smart-result
very smart result type
https://github.com/misuken-now/smart-result
error-handling result result-type results typescript
Last synced: 9 months ago
JSON representation
very smart result type
- Host: GitHub
- URL: https://github.com/misuken-now/smart-result
- Owner: misuken-now
- License: mit
- Created: 2024-03-27T03:34:24.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-27T11:25:37.000Z (about 2 years ago)
- Last Synced: 2024-08-09T02:58:07.337Z (almost 2 years ago)
- Topics: error-handling, result, result-type, results, typescript
- Language: TypeScript
- Homepage:
- Size: 23.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# smart-result
This type library is a very smart result type available in TypeScript 4.6 or later.
```
import type { Result } from "smart-result";
async function loadData(): Promise> {
try {
const response = await client.get();
return { ok: response.data };
} catch (error) {
if (error instanceof Error) {
return { error: error };
}
return { error: new Error("unknown error") };
}
}
async function example() {
const { ok: data, error } = await loadData();
if (data) {
console.log(data); // data: Data; error: undefined;
} else {
console.error(error); // data: undefined; error: Error;
}
}
```
## Highlight
- π Good coding quality
- π Highly readable
- π Fewer things to remember
- β
Safety design
- π‘ Type only
## Intall
```
npm install -D `smart-result`
```
or
```
yarn add -D `smart-result`
```
## Usage
### Void Pattern
```
import type { Result } from "smart-result";
async function start(): Promise> {
try {
await client.start();
return { ok: true };
} catch (error) {
if (error instanceof Error) {
return { error: error };
}
return { error: new Error("unknown error") };
}
}
async function example() {
// Use "ok" if you only want to know if you succeeded or not
const { ok, error } = await start();
if (ok) {
console.log(ok); // ok: true;
} else {
console.error(error); // ok: undefined; error: Error;
}
}
```
### Return Data Pattern
```
import type { Result } from "smart-result";
async function loadData(): Promise> {
try {
const response = await client.get();
return { ok: response.data };
} catch (error) {
if (error instanceof Error) {
return { error: error };
}
return { error: new Error("unknown error") };
}
}
async function example() {
// Assign a name to "ok" if you want to handle the resulting information
const { ok: data, error } = await loadData();
if (data) {
console.log(data); // data: Data; error: undefined;
} else {
console.error(error); // data: undefined; error: Error;
}
}
```
### Return Value Pattern
```
import type { Result } from "smart-result";
async function loadValue(): Promise> {
try {
const response = await client.get();
// If you want to return a primitive value, wrap it with `{ value: }`.
return { ok: { value: response.data } };
} catch (error) {
if (error instanceof Error) {
return { error: error };
}
return { error: new Error("unknown error") };
}
}
async function example() {
const { ok, error } = await loadValue();
if (ok) {
console.log(ok.value); // ok: { value: number } error: undefined;
} else {
console.error(error); // data: undefined; error: Error;
}
}
```
## API
### `Result, Error>`
Alias for `OkResult | ErrorResult`.
### `OkResult>`
An object with the key `ok` and without the key `error`.
### `ErrorResult>>`
An object with the key `error` and without the key `ok`.
## LICENSE
[@misuken-now/smart-result](https://github.com/misuken-now/smart-result)γ»MIT