Compare commits
No commits in common. "a6edb3505b49b164a07b31c641ccf51c1da3c121" and "7b38353a0234b73e44709e819754a961caf9d375" have entirely different histories.
a6edb3505b
...
7b38353a02
2
.github/workflows/publish.yml
vendored
2
.github/workflows/publish.yml
vendored
|
@ -16,4 +16,4 @@ jobs:
|
|||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Publish package
|
||||
run: npx jsr publish --allow-slow-types
|
||||
run: npx jsr publish
|
||||
|
|
23
grip.ts
23
grip.ts
|
@ -9,9 +9,6 @@ interface Status {
|
|||
of(cls: any): boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Error result
|
||||
*/
|
||||
export class Err extends Error {
|
||||
Ok() {
|
||||
return false;
|
||||
|
@ -39,9 +36,6 @@ export class Err extends Error {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Successful result
|
||||
*/
|
||||
export class Ok {
|
||||
Ok() {
|
||||
return true;
|
||||
|
@ -154,27 +148,14 @@ type Unwrap<T> =
|
|||
? U
|
||||
: T;
|
||||
|
||||
type SafeResult<T> =
|
||||
export type SafeResult<T> =
|
||||
T extends Promise<any>
|
||||
? Promise<Result<Unwrap<T>>>
|
||||
: T extends () => Promise<any>
|
||||
? Promise<Result<Unwrap<T>>>
|
||||
: Result<Unwrap<T>>;
|
||||
|
||||
/**
|
||||
* Grip wraps functions, promises or generators and returns it as a result.
|
||||
* The result can be handled as an object { value, status }, or as a tuple [value, object].
|
||||
* The result and status interfaces have the methods `ok(), fail(), of(Error)` to check the status:
|
||||
*
|
||||
* ```javascript
|
||||
* const json = grip(response.body.joson())
|
||||
* if (json.of(SyntaxError)) {
|
||||
* // handle parse error
|
||||
* }
|
||||
* // handle json.value
|
||||
* ```
|
||||
*/
|
||||
export function grip<T>(action: T): SafeResult<T> {
|
||||
export function grip<T>(action: T) {
|
||||
if (action instanceof Promise) {
|
||||
return promise<T>(action) as SafeResult<T>;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue