-
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.
- biome format - pack's param is now named `e` instead of `entry` to be more usable in inlay hints - fetch wrapper is now actually functional
- Loading branch information
Showing
4 changed files
with
55 additions
and
38 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
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,36 +1,51 @@ | ||
import { EnumChecked, match, pack, type Enum } from "./enum"; | ||
import { Enum, match, pack } from "./enum"; | ||
import { Err, Ok, type Result } from "./result"; | ||
|
||
type FetchErr = { | ||
AbortError: DOMException; | ||
NotAllowedError: DOMException; | ||
TypeError: TypeError; | ||
OtherError: unknown; | ||
Redirect: Response; | ||
ClientError: Response; | ||
ServerError: Response; | ||
}; | ||
|
||
async function f( | ||
const todo = (x?: unknown) => console.trace("todo!", x); | ||
const FetchErr = Enum<FetchErr>(); | ||
|
||
export async function f( | ||
input: string | URL | globalThis.Request, | ||
init?: RequestInit, | ||
): Promise<Result<Response, FetchErr>> { | ||
try { | ||
return Ok(await fetch(input, init)); | ||
const res = await fetch(input, init); | ||
|
||
if (res.ok) { | ||
return Ok(res); | ||
} | ||
|
||
if (res.status >= 300 && res.status < 400) { | ||
return Err(FetchErr("Redirect", res)); | ||
} | ||
|
||
if (res.status >= 400 && res.status < 500) { | ||
return Err(FetchErr("ClientError", res)); | ||
} | ||
|
||
if (res.status >= 500) { | ||
return Err(FetchErr("ServerError", res)); | ||
} | ||
|
||
return Ok(res); | ||
} catch (e) { | ||
if (e instanceof TypeError) { | ||
return Err(FetchErr("TypeError", e)); | ||
} | ||
|
||
if (e instanceof DOMException) { | ||
return Err(pack("AbortError", e)); | ||
return Err(FetchErr("AbortError", e)); | ||
} | ||
return Err(pack("OtherError", e)); //=> | ||
|
||
throw e; | ||
} | ||
} | ||
|
||
match(await f("i"), { | ||
//=> | ||
Ok: (x) => x, //=> | ||
Err: (x) => | ||
match(x, { | ||
AbortError(x) { | ||
//=> | ||
console.log("wtaf"); | ||
}, | ||
_: (x) => x, //=> | ||
}), //=> | ||
}); |
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,7 +1,7 @@ | ||
enum ColorEnum { | ||
Red, | ||
Green, | ||
Blue, | ||
Red = 0, | ||
Green = 1, | ||
Blue = 2, | ||
} | ||
|
||
const red = ColorEnum.Red; | ||
|