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

Add tryCatch decoder #637

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/modules/Decoder.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Added in v2.2.7
- [WithRefine](#withrefine)
- [WithUnion](#withunion)
- [WithUnknownContainers](#withunknowncontainers)
- [interop](#interop)
- [tryCatch](#trycatch)
- [model](#model)
- [Decoder (interface)](#decoder-interface)
- [primitives](#primitives)
Expand Down Expand Up @@ -578,6 +580,20 @@ export declare const WithUnknownContainers: S.WithUnknownContainers2C<'io-ts/Dec

Added in v2.2.8

# interop

## tryCatch

Constructs a new `Decoder` from a function that might throw.

**Signature**

```ts
export declare const tryCatch: <I, A>(f: (i: I) => A, id: string) => Decoder<I, A>
```

Added in v2.2.17

# model

## Decoder (interface)
Expand Down
16 changes: 16 additions & 0 deletions docs/modules/TaskDecoder.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ Added in v2.2.7
- [WithRefine](#withrefine)
- [WithUnion](#withunion)
- [WithUnknownContainers](#withunknowncontainers)
- [interop](#interop)
- [tryCatch](#trycatch)
- [model](#model)
- [TaskDecoder (interface)](#taskdecoder-interface)
- [primitives](#primitives)
Expand Down Expand Up @@ -594,6 +596,20 @@ export declare const WithUnknownContainers: S.WithUnknownContainers2C<'io-ts/Tas

Added in v2.2.8

# interop

## tryCatch

Constructs a new `Decoder` from a function that returns a `Promise`.

**Signature**

```ts
export declare const tryCatch: <I, A>(f: (i: I) => Promise<A>, id: string) => TaskDecoder<I, A>
```

Added in v2.2.17

# model

## TaskDecoder (interface)
Expand Down
7 changes: 7 additions & 0 deletions dtslint/ts3.5/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,10 @@ _.readonly(
a: _.string
})
)

//
// tryCatch
//

// $ExpectType Decoder<string, number>
_.tryCatch((a: string) => a.length, 'Length')
7 changes: 7 additions & 0 deletions dtslint/ts3.5/TaskDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ _.readonly(
a: _.string
})
)

//
// tryCatch
//

// $ExpectType TaskDecoder<string, number>
_.tryCatch(async (a: string) => a.length, 'Length')
18 changes: 18 additions & 0 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,24 @@ export const lazy: <I, A>(id: string, f: () => Decoder<I, A>) => Decoder<I, A> =
*/
export const readonly: <I, A>(decoder: Decoder<I, A>) => Decoder<I, Readonly<A>> = identity

// -------------------------------------------------------------------------------------
// interop
// -------------------------------------------------------------------------------------

/**
* Constructs a new `Decoder` from a function that might throw.
*
* @category interop
* @since 2.2.17
*/
export const tryCatch: <I, A>(f: (i: I) => A, id: string) => Decoder<I, A> = (f, id) => ({
decode: (i) =>
E.tryCatch(
() => f(i),
() => error(i, id)
)
})

// -------------------------------------------------------------------------------------
// non-pipeables
// -------------------------------------------------------------------------------------
Expand Down
18 changes: 18 additions & 0 deletions src/TaskDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,24 @@ export const lazy: <I, A>(id: string, f: () => TaskDecoder<I, A>) => TaskDecoder
*/
export const readonly: <I, A>(decoder: TaskDecoder<I, A>) => TaskDecoder<I, Readonly<A>> = identity

// -------------------------------------------------------------------------------------
// interop
// -------------------------------------------------------------------------------------

/**
* Constructs a new `Decoder` from a function that returns a `Promise`.
*
* @category interop
* @since 2.2.17
*/
export const tryCatch: <I, A>(f: (i: I) => Promise<A>, id: string) => TaskDecoder<I, A> = (f, id) => ({
decode: (i) =>
TE.tryCatch(
() => f(i),
() => error(i, id)
)
})

// -------------------------------------------------------------------------------------
// non-pipeables
// -------------------------------------------------------------------------------------
Expand Down
20 changes: 20 additions & 0 deletions test/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,26 @@ describe('Decoder', () => {
})
})

// -------------------------------------------------------------------------------------
// interop
// -------------------------------------------------------------------------------------

describe('tryCatch', () => {
it('should decode when the function returns a value', () => {
const f = (value: number) => value + 1

assert.deepStrictEqual(_.tryCatch(f, 'Number').decode(1), _.success(2))
})

it('should reject when the function throws', () => {
const f = () => {
throw new Error('Some error')
}

assert.deepStrictEqual(_.tryCatch(f, 'Number').decode(1), E.left(FS.of(DE.leaf(1, 'Number'))))
})
})

// -------------------------------------------------------------------------------------
// utils
// -------------------------------------------------------------------------------------
Expand Down
20 changes: 20 additions & 0 deletions test/TaskDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,26 @@ describe('UnknownTaskDecoder', () => {
})
})

// -------------------------------------------------------------------------------------
// interop
// -------------------------------------------------------------------------------------

describe('tryCatch', () => {
it('should decode when the promise resolves with a value', async () => {
const f = async (value: number) => value + 1

assert.deepStrictEqual(await _.tryCatch(f, 'Number').decode(1)(), D.success(2))
})

it('should reject when the promise rejects', async () => {
const f = async () => {
throw new Error('Some error')
}

assert.deepStrictEqual(await _.tryCatch(f, 'Number').decode(1)(), E.left(FS.of(DE.leaf(1, 'Number'))))
})
})

// -------------------------------------------------------------------------------------
// utils
// -------------------------------------------------------------------------------------
Expand Down