From 47ac1810bedcd2b1e34ef4de2e6162d14717d2c2 Mon Sep 17 00:00:00 2001 From: Tibet Tornaci Date: Thu, 15 Aug 2024 23:32:19 -0700 Subject: [PATCH] a few minor touchups - 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 --- coins.ts | 12 ++++++------ src/enum.ts | 22 ++++++++++++---------- src/fetch.ts | 53 +++++++++++++++++++++++++++++++++------------------- vanilla.ts | 6 +++--- 4 files changed, 55 insertions(+), 38 deletions(-) diff --git a/coins.ts b/coins.ts index f5aa4d3..2bfbb81 100644 --- a/coins.ts +++ b/coins.ts @@ -1,12 +1,12 @@ import { Enum, match } from "./index"; type Coins = { - Penny: 1, - Nickel: 5, - Dime: 10, - Quarter: 25, -} -const Coins = Enum() + Penny: 1; + Nickel: 5; + Dime: 10; + Quarter: 25; +}; +const Coins = Enum(); // you have to specify a value, but the type checker won't let you enter anything other than the preset value const penny = Coins("Penny", 1); diff --git a/src/enum.ts b/src/enum.ts index 28c245e..cbc1a28 100644 --- a/src/enum.ts +++ b/src/enum.ts @@ -5,22 +5,24 @@ export type Enum = { [K in keyof E]: [K, E[K]] }[keyof E]; // "-?" removes any optional parameters, making all cases required export type Arms = | { - [K in keyof E]-?: (x: E[K]) => unknown; - } + [K in keyof E]-?: (x: E[K]) => unknown; + } | ({ - [L in keyof E]?: (x: E[L]) => unknown; - } & { - _: (x: Enum[1]) => unknown; - }); + [L in keyof E]?: (x: E[L]) => unknown; + } & { + _: (x: Enum[1]) => unknown; + }); // this is a generic way to create enum instances, but the Enum() factory function is preferred -export const pack = (...entry: Enum) => entry as E; +export const pack = (...e: Enum) => e as E; export const Enum = () => - (...entry: Enum) => - entry as unknown as E; + (...e: Enum) => + e as unknown as E; export const match = >(pattern: E, arms: Fn) => // biome-ignore lint/suspicious/noExplicitAny: return type is handled externally - (((arms as any)[(pattern as any[])[0]] as any) || (arms as any)._)((pattern as any)[1] as unknown,) as ReturnType>; + (((arms as any)[(pattern as any[])[0]] as any) || (arms as any)._)( + (pattern as any)[1] as unknown, + ) as ReturnType>; diff --git a/src/fetch.ts b/src/fetch.ts index f2879e5..528ebe3 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -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(); + +export async function f( input: string | URL | globalThis.Request, init?: RequestInit, ): Promise> { 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, //=> - }), //=> -}); diff --git a/vanilla.ts b/vanilla.ts index e45053b..a25ea15 100644 --- a/vanilla.ts +++ b/vanilla.ts @@ -1,7 +1,7 @@ enum ColorEnum { - Red, - Green, - Blue, + Red = 0, + Green = 1, + Blue = 2, } const red = ColorEnum.Red;