Skip to content

Commit

Permalink
fix node imports and remove exposed mightFailFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
meech-ward committed Oct 13, 2024
1 parent 6a0b148 commit 1f6306a
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 32 deletions.
1 change: 0 additions & 1 deletion docs/content/contents.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,4 @@ course:
filePath: /jsdocs/functions/Might.mdx
slug: /functions/Might
type: documentation

---
20 changes: 17 additions & 3 deletions examples/makeMightFailBasic.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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()
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -55,3 +55,4 @@
"vitest": "^2.1.1"
}
}

6 changes: 3 additions & 3 deletions src/go/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export type Either<T> =
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.
// So it has an array interface and proxies array methods.
// See the utils `makeEither` for more information.
10 changes: 9 additions & 1 deletion src/go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions src/go/mightFail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T>(promise: Promise<T>) {
const { result, error } = await standard.mightFailFunction(promise)
const { result, error } = await standardMightFailFunction(promise)
return error
? createEither<T, "go">({ result: undefined, error }, "go")
: createEither<T, "go">({ result, error: undefined }, "go")
Expand Down Expand Up @@ -45,7 +46,7 @@ const mightFailFunction: MightFailFunction<"go"> = async function <T>(promise: P
*/
export const mightFail: MightFail<"go"> = new Proxy(
mightFailFunction,
makeProxyHandler(mightFailFunction),
makeProxyHandler(mightFailFunction)
) as MightFail<"go">

/**
Expand Down Expand Up @@ -100,3 +101,4 @@ export function Fail(error: unknown): Either<undefined> {
const standardFail = standard.Fail(error)
return createEither<undefined, "go">({ result: undefined, error: standardFail.error }, "go")
}

14 changes: 11 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions src/makeMightFail.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -44,7 +44,7 @@ export function makeMightFail<T extends (...args: any[]) => Promise<any>>(
): (...funcArgs: Parameters<T>) => Promise<Either<UnwrapPromise<ReturnType<T>>>> {
return async (...args: Parameters<T>) => {
const promise = func(...args)
return mightFailFunction(promise) as Promise<Either<any>>
return mightFail(promise)
}
}

Expand Down
14 changes: 2 additions & 12 deletions src/mightFail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T>(
promise: Promise<T>
): Promise<Either<T>> {
try {
const result = await promise
return createEither<T>({ result, error: undefined })
} catch (err) {
const error = handleError(err)
return createEither<T>({ error, result: undefined })
}
}

/**
* Wraps a promise in an Either to safely handle both its resolution and rejection. This function
Expand Down
16 changes: 16 additions & 0 deletions src/utils/mightFailFunction.ts
Original file line number Diff line number Diff line change
@@ -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 <T>(
promise: Promise<T>
): Promise<Either<T>> {
try {
const result = await promise
return createEither<T>({ result, error: undefined })
} catch (err) {
const error = handleError(err)
return createEither<T>({ error, result: undefined })
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "node"
},
"include": [
Expand Down

0 comments on commit 1f6306a

Please sign in to comment.