Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow map and mapError in DFs to have async mapper function #134

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/domain-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function sequence<Fns extends DomainFunction[]>(
*/
function map<O, R>(
dfn: DomainFunction<O>,
mapper: (element: O) => R,
mapper: (element: O) => R | Promise<R>,
): DomainFunction<R> {
return ((input, environment) =>
dfResultFromcomposable(
Expand Down Expand Up @@ -327,16 +327,16 @@ function fromSuccess<T extends DomainFunction>(
*/
function mapError<O>(
dfn: DomainFunction<O>,
mapper: (element: ErrorData) => ErrorData,
mapper: (element: ErrorData) => ErrorData | Promise<ErrorData>,
): DomainFunction<O> {
return async (input, environment) => {
return (async (input, environment) => {
const result = await dfn(input, environment)
if (result.success) return result

return safeResult(() => {
throw new ResultError({ ...mapper(result) })
return safeResult(async () => {
throw new ResultError({ ...(await mapper(result)) })
})
}
}) as DomainFunction<O>
}

type TraceData<T> = {
Expand Down Expand Up @@ -394,4 +394,3 @@ export {
sequence,
trace,
}

29 changes: 27 additions & 2 deletions src/map-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('mapError', () => {
({
errors: [{ message: 'New Error Message' }],
inputErrors: [{ message: 'New Input Error Message' }],
} as ErrorData)
}) as ErrorData

const c = mapError(a, b)
type _R = Expect<Equal<typeof c, DomainFunction<number>>>
Expand All @@ -39,7 +39,32 @@ describe('mapError', () => {
path: [],
},
],
} as ErrorData)
}) as ErrorData

const c = mapError(a, b)
type _R = Expect<Equal<typeof c, DomainFunction<number>>>

assertEquals(await c({ invalidInput: '1' }), {
success: false,
errors: [{ message: 'Number of errors: 0' }],
environmentErrors: [],
inputErrors: [{ message: 'Number of input errors: 1', path: [] }],
})
})

it('returns a domain function function that will apply an async function over the error of the first one', async () => {
const a = mdf(z.object({ id: z.number() }))(({ id }) => id + 1)
const b = (result: ErrorData) =>
Promise.resolve({
errors: [{ message: 'Number of errors: ' + result.errors.length }],
environmentErrors: [],
inputErrors: [
{
message: 'Number of input errors: ' + result.inputErrors.length,
path: [],
},
],
}) as Promise<ErrorData>

const c = mapError(a, b)
type _R = Expect<Equal<typeof c, DomainFunction<number>>>
Expand Down
16 changes: 16 additions & 0 deletions src/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ describe('map', () => {
})
})

it('returns a domain function function that will apply an async function over the results of the first one', async () => {
const a = mdf(z.object({ id: z.number() }))(({ id }) => id + 1)
const b = (id: number) => Promise.resolve(id + 1)

const c = map(a, b)
type _R = Expect<Equal<typeof c, DomainFunction<number>>>

assertEquals(await c({ id: 1 }), {
success: true,
data: 3,
errors: [],
inputErrors: [],
environmentErrors: [],
})
})

it('returns the error when the domain function fails', async () => {
const firstInputParser = z.object({ id: z.number() })
const a = mdf(firstInputParser)(({ id }) => id + 1)
Expand Down
Loading