From 514050cb894893801ec5ee4c78248ba62c82b98c Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 27 Oct 2024 12:36:33 -0700 Subject: [PATCH 1/2] Update either type --- src/utils/utils.type.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils/utils.type.ts b/src/utils/utils.type.ts index ed7ca89..6f80d92 100644 --- a/src/utils/utils.type.ts +++ b/src/utils/utils.type.ts @@ -6,13 +6,13 @@ export type EitherMode = "standard" | "go" | "any" export type AnyEither = StandardEither | GoEither export type MightFailFunction = ( - promise: Promise + promise: T ) => Promise< TEitherMode extends "standard" - ? StandardEither + ? StandardEither> : TEitherMode extends "go" - ? GoEither - : AnyEither + ? GoEither> + : AnyEither> > export type PromiseFulfilledResult = { From 2b40a6718d6a740bc5873b48fb14baa7c10d7e6e Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 27 Oct 2024 12:39:00 -0700 Subject: [PATCH 2/2] add example for why updated type is necessary --- examples/mightFailBasic.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/examples/mightFailBasic.ts b/examples/mightFailBasic.ts index 13cdeb1..4c24a50 100644 --- a/examples/mightFailBasic.ts +++ b/examples/mightFailBasic.ts @@ -10,3 +10,31 @@ async function main() { } main() + +function someResponse() { + const responses = [ + { status: 200, data: { id: 1, name: "John Doe" }, json: async () => ({ status: 200, data: { id: 1, name: "John Doe" } }) }, + { status: 404, error: "Not Found", json: async () => ({ status: 404, error: "Not Found" }) }, + { status: 500, error: "Internal Server Error", json: async () => ({ status: 500, error: "Internal Server Error" }) }, + { status: 201, data: { id: 2, name: "Jane Smith", created: true }, json: async () => ({ status: 201, data: { id: 2, name: "Jane Smith", created: true } }) }, + { status: 400, error: "Bad Request", details: ["Invalid email", "Password too short"], json: async () => ({ status: 400, error: "Bad Request", details: ["Invalid email", "Password too short"] }) }, + ]; + + const randomIndex = Math.floor(Math.random() * responses.length); + const randomResponse = responses[randomIndex]; + + return randomResponse; +} + +async function main2() { + const res = someResponse() + const [error, result] = await mightFail(res.json()) + if (error) { + console.error(error) + return + } + console.log(result) +} + +main2() +