From 1f6306ad7875316c5dd4f3d9974dd991604dea9a Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 12 Oct 2024 19:40:24 -0700 Subject: [PATCH] fix node imports and remove exposed mightFailFunction --- docs/content/contents.mdx | 1 - examples/makeMightFailBasic.ts | 20 +++++++++++++++++--- package.json | 11 ++++++----- src/go/Either.ts | 6 +++--- src/go/index.ts | 10 +++++++++- src/go/mightFail.ts | 6 ++++-- src/index.ts | 14 +++++++++++--- src/makeMightFail.ts | 4 ++-- src/mightFail.ts | 14 ++------------ src/utils/mightFailFunction.ts | 16 ++++++++++++++++ tsconfig.json | 1 + 11 files changed, 71 insertions(+), 32 deletions(-) create mode 100644 src/utils/mightFailFunction.ts diff --git a/docs/content/contents.mdx b/docs/content/contents.mdx index 9a701d7..79caf18 100644 --- a/docs/content/contents.mdx +++ b/docs/content/contents.mdx @@ -67,5 +67,4 @@ course: filePath: /jsdocs/functions/Might.mdx slug: /functions/Might type: documentation - --- diff --git a/examples/makeMightFailBasic.ts b/examples/makeMightFailBasic.ts index 006e18f..5586ccd 100644 --- a/examples/makeMightFailBasic.ts +++ b/examples/makeMightFailBasic.ts @@ -1,7 +1,7 @@ -import { makeMightFail } from "../src/makeMightFail" +import { makeMightFail, makeMightFailSync } from "../src/makeMightFail" const resolve = (value: { message: string }) => Promise.resolve(value) -async function main() { +async function asyncMain() { const func = makeMightFail(resolve) const { error, result } = await func({ message: "success" }) if (error) { @@ -11,4 +11,18 @@ async function main() { console.log(result.message) } -main() +asyncMain() + + +function syncMain() { + const parseJSON = makeMightFailSync(JSON.parse) + + const [error, result] = parseJSON("{invalid}") + if (error) { + console.error(error) + return + } + console.log(result.message) +} + +syncMain() \ No newline at end of file diff --git a/package.json b/package.json index d6c122e..abf441c 100644 --- a/package.json +++ b/package.json @@ -2,18 +2,18 @@ "name": "might-fail", "version": "0.6.1", "description": "Return an Either object instead of throwing an exception", - "main": "dist/index.js", + "main": "dist/cjs/index.js", + "module": "dist/index.js", "types": "dist/index.d.ts", + "type": "module", "exports": { ".": { "import": "./dist/index.js", - "require": "./dist/cjs/index.js", - "types": "./dist/index.d.ts" + "require": "./dist/cjs/index.js" }, "./go": { "import": "./dist/go/index.js", - "require": "./dist/cjs/go/index.js", - "types": "./dist/go/index.d.ts" + "require": "./dist/cjs/go/index.js" } }, "scripts": { @@ -55,3 +55,4 @@ "vitest": "^2.1.1" } } + diff --git a/src/go/Either.ts b/src/go/Either.ts index 22dc28b..df89bef 100644 --- a/src/go/Either.ts +++ b/src/go/Either.ts @@ -11,10 +11,10 @@ export type Either = result: undefined } & [undefined, Error]) | ({ - result: T error: undefined + result: T } & [T, undefined]) // Note this array interface is undesireable, we would much rather use just an itterator here but TS doesn't yet support that well enough -// So it has an array interface and proxies array methods. -// See the utils `makeEither` for more information. \ No newline at end of file +// So it has an array interface and proxies array methods. +// See the utils `makeEither` for more information. diff --git a/src/go/index.ts b/src/go/index.ts index 347cd16..4903d77 100644 --- a/src/go/index.ts +++ b/src/go/index.ts @@ -20,4 +20,12 @@ import { mightFail, mightFailSync, Might, Fail } from "./mightFail" import { makeMightFail, makeMightFailSync } from "./makeMightFail" export { Either, mightFail, makeMightFail, mightFailSync, makeMightFailSync, Might, Fail } -export default { mightFail, makeMightFail, mightFailSync, makeMightFailSync, Might, Fail } +const defaultExport = { + mightFail, + makeMightFail, + mightFailSync, + makeMightFailSync, + Might, + Fail + } + export default defaultExport diff --git a/src/go/mightFail.ts b/src/go/mightFail.ts index 79f0df0..08cf079 100644 --- a/src/go/mightFail.ts +++ b/src/go/mightFail.ts @@ -3,9 +3,10 @@ import { type Either } from "./Either" import { createEither } from "../utils/createEither" import { makeProxyHandler } from "../utils/staticMethodsProxy" import { MightFail, MightFailFunction, NotUndefined } from "../utils/utils.type" +import { mightFailFunction as standardMightFailFunction } from "../utils/mightFailFunction" const mightFailFunction: MightFailFunction<"go"> = async function (promise: Promise) { - const { result, error } = await standard.mightFailFunction(promise) + const { result, error } = await standardMightFailFunction(promise) return error ? createEither({ result: undefined, error }, "go") : createEither({ result, error: undefined }, "go") @@ -45,7 +46,7 @@ const mightFailFunction: MightFailFunction<"go"> = async function (promise: P */ export const mightFail: MightFail<"go"> = new Proxy( mightFailFunction, - makeProxyHandler(mightFailFunction), + makeProxyHandler(mightFailFunction) ) as MightFail<"go"> /** @@ -100,3 +101,4 @@ export function Fail(error: unknown): Either { const standardFail = standard.Fail(error) return createEither({ result: undefined, error: standardFail.error }, "go") } + diff --git a/src/index.ts b/src/index.ts index c79fca4..859949e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,14 @@ import { type Either } from "./Either" -import { mightFail, mightFailSync, mightFailFunction, Might, Fail } from "./mightFail" +import { mightFail, mightFailSync, Might, Fail } from "./mightFail" import { makeMightFail, makeMightFailSync } from "./makeMightFail" -export { Either, mightFail, makeMightFail, mightFailSync, makeMightFailSync, mightFailFunction, Might, Fail } -export default { mightFail, makeMightFail, mightFailSync, makeMightFailSync, mightFailFunction, Might, Fail } +export { Either, mightFail, makeMightFail, mightFailSync, makeMightFailSync, Might, Fail } +const defaultExport = { + mightFail, + makeMightFail, + mightFailSync, + makeMightFailSync, + Might, + Fail +} +export default defaultExport diff --git a/src/makeMightFail.ts b/src/makeMightFail.ts index 3c2d4f4..e681171 100644 --- a/src/makeMightFail.ts +++ b/src/makeMightFail.ts @@ -1,5 +1,5 @@ import { type Either } from "./Either" -import { mightFailSync, mightFailFunction } from "./mightFail" +import { mightFail, mightFailSync } from "./mightFail" /** * Utility type that unwraps a Promise type. If T is a Promise, it extracts the type the Promise resolves to, @@ -44,7 +44,7 @@ export function makeMightFail Promise>( ): (...funcArgs: Parameters) => Promise>>> { return async (...args: Parameters) => { const promise = func(...args) - return mightFailFunction(promise) as Promise> + return mightFail(promise) } } diff --git a/src/mightFail.ts b/src/mightFail.ts index 84fe30b..8f18186 100644 --- a/src/mightFail.ts +++ b/src/mightFail.ts @@ -2,19 +2,9 @@ import { type Either } from "./Either" import { makeProxyHandler } from "./utils/staticMethodsProxy" import { handleError } from "./utils/errors" import { createEither } from "./utils/createEither" -import { MightFail, MightFailFunction, NotUndefined } from "./utils/utils.type" +import { MightFail, NotUndefined } from "./utils/utils.type" +import { mightFailFunction } from "./utils/mightFailFunction" -export const mightFailFunction: MightFailFunction<"standard"> = async function ( - promise: Promise -): Promise> { - try { - const result = await promise - return createEither({ result, error: undefined }) - } catch (err) { - const error = handleError(err) - return createEither({ error, result: undefined }) - } -} /** * Wraps a promise in an Either to safely handle both its resolution and rejection. This function diff --git a/src/utils/mightFailFunction.ts b/src/utils/mightFailFunction.ts new file mode 100644 index 0000000..4065706 --- /dev/null +++ b/src/utils/mightFailFunction.ts @@ -0,0 +1,16 @@ +import { type Either } from "../Either" +import { handleError } from "./errors" +import { createEither } from "./createEither" +import { MightFailFunction } from "./utils.type" + +export const mightFailFunction: MightFailFunction<"standard"> = async function ( + promise: Promise +): Promise> { + try { + const result = await promise + return createEither({ result, error: undefined }) + } catch (err) { + const error = handleError(err) + return createEither({ error, result: undefined }) + } +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 27a30c4..5ce09f7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,7 @@ "rootDir": "./src", "strict": true, "esModuleInterop": true, + "allowSyntheticDefaultImports": true, "moduleResolution": "node" }, "include": [