-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
27 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@oofdere/crabrave": minor | ||
--- | ||
|
||
add the Enum<E>() factory function as a better DX alternative to pack<E>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { type Enum, type EnumChecked, pack, match } from "./src/enum"; | ||
export { Enum, type EnumChecked, pack, match } from "./src/enum"; | ||
export { type Option, Some, None } from "./src/option"; | ||
export { type Result, Ok, Err } from "./src/result"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,31 @@ | ||
import { EnumChecked, match, pack, type Enum } from "./enum" | ||
import { Err, Ok, type Result } from "./result" | ||
|
||
// biome-ignore lint/suspicious/noRedeclare: makes sense to redeclare for Err | ||
type Err = { | ||
type FetchErr = { | ||
AbortError: DOMException, | ||
NotAllowedError: DOMException, | ||
TypeError: TypeError, | ||
OtherError: unknown | ||
} | ||
|
||
// biome-ignore lint/suspicious/noRedeclare: <explanation> | ||
type FetchResult = Enum<Result<Response, unknown>> | ||
|
||
async function f(input: string | URL | globalThis.Request, init?: RequestInit): Promise<FetchResult> { | ||
async function f(input: string | URL | globalThis.Request, init?: RequestInit): Promise<Result<Response, FetchErr>> { | ||
try { | ||
return Ok(await fetch(input, init)) | ||
} catch (e) { | ||
if (e instanceof DOMException) { | ||
return Err(['AbortError', e]) | ||
return Err(pack('AbortError', e)) | ||
} | ||
return Err(pack('OtherError', e)) | ||
return Err(pack('OtherError', e)) //=> | ||
|
||
} | ||
|
||
return Err(pack('OtherError', undefined)) | ||
} | ||
|
||
match(await f('i'), { | ||
Ok: (x) => { }, | ||
Err: (x) => { } | ||
match(await f('i'), { //=> | ||
Ok: (x) => x, //=> | ||
Err: (x) => match(x, { | ||
AbortError(x) { //=> | ||
console.log("wtaf") | ||
}, | ||
_: x => x //=> | ||
}) //=> | ||
}) |