Comparing version
{ | ||
"name": "ts-res", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "TypeScript Result", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/Slava-Ini/ts-result", |
@@ -0,1 +1,3 @@ | ||
[](https://www.npmjs.com/package/ts-res) | ||
# ts-res | ||
@@ -14,5 +16,5 @@ | ||
- Using npm - `npm install ts-result` | ||
- Using yarn - `yarn add ts-result` | ||
- Using pnpm - `pnpm add ts-result` | ||
- Using npm - `npm install ts-res` | ||
- Using yarn - `yarn add ts-res` | ||
- Using pnpm - `pnpm add ts-res` | ||
@@ -73,3 +75,3 @@ ## Result Type | ||
- `else(callback: (error: E) => T)` - returns an unwrapped result or executes callback that returns back-up value which can be based on provided error | ||
- `and(callback: (result: T) => void)` - handles a result in a callback while ignoring an error, doesn't return anything | ||
- `and(callback: (result: T) => Result<T, E>)` - handles a result in a callback while ignoring an error, returns the result allowing for chaining | ||
@@ -128,3 +130,3 @@ ```ts | ||
return Err("Invalide status code"); | ||
return Err("Invalid status code"); | ||
} | ||
@@ -194,1 +196,31 @@ | ||
``` | ||
- Use `and()` for handling http response data and chaining it with another method | ||
```ts | ||
type User = { | ||
name: string; | ||
age: number; | ||
}; | ||
async function fetchUserData(): Promise<Result<User, Error>> { | ||
const response = await fetch("https://api.example.com/user"); | ||
if (!response.ok) { | ||
return Err(new Error("Failed to fetch user data")); | ||
} | ||
const data = await response.json(); | ||
return Ok(data); | ||
} | ||
const result = await fetchUserData(); | ||
result | ||
.and((user) => { | ||
console.log(user); | ||
}) | ||
.else((error) => { | ||
console.error(error); | ||
}); | ||
``` |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
223
16.75%6760
-60.07%3
-75%0
-100%1
Infinity%2
Infinity%