Skip to content

Commit

Permalink
Add fromMiddlewareK to ReaderMiddleware (#83)
Browse files Browse the repository at this point in the history
* Add fromMiddlewareK to ReaderMiddleware

* Revert accidental merge change
  • Loading branch information
thewilkybarkid authored Sep 6, 2023
1 parent 03e2687 commit 8e179c1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
13 changes: 13 additions & 0 deletions docs/modules/ReaderMiddleware.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Added in v0.6.3
- [flattenW](#flattenw)
- [fromEitherK](#fromeitherk)
- [fromIOK](#fromiok)
- [fromMiddlewareK](#frommiddlewarek)
- [fromOptionK](#fromoptionk)
- [fromReaderEitherK](#fromreadereitherk)
- [fromReaderK](#fromreaderk)
Expand Down Expand Up @@ -1011,6 +1012,18 @@ export declare const fromIOK: <A, B>(f: (...a: A) => IO<B>) => <S, R, E>(...a: A

Added in v0.7.0

## fromMiddlewareK

**Signature**

```ts
export declare const fromMiddlewareK: <R, A extends readonly unknown[], B, I, O, E>(
f: (...a: A) => M.Middleware<I, O, E, B>
) => (...a: A) => ReaderMiddleware<R, I, O, E, B>
```

Added in v0.7.9

## fromOptionK

**Signature**
Expand Down
10 changes: 10 additions & 0 deletions dtslint/ts3.5/ReaderMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ pipe(
_.ichainFirstW(() => middleware3) // $ExpectError
)

//
// fromMiddlewareK
//

// $ExpectType (a: boolean, b: number) => ReaderMiddleware<unknown, "one", "two", number, string>
_.fromMiddlewareK((a: boolean, b: number) => middleware5)

// $ExpectType ReaderMiddleware<R1, "one", "two", number, string>
pipe(middleware1, _.ichain(_.fromMiddlewareK((a: boolean) => middleware5)))

//
// rightReaderTask
//
Expand Down
30 changes: 21 additions & 9 deletions src/ReaderMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,27 +830,39 @@ export const chainMiddlewareKW =

/**
* @category combinators
* @since 0.6.3
* @since 0.7.9
*/
export const chainMiddlewareK: <I, E, A, B>(
f: (a: A) => M.Middleware<I, I, E, B>
) => <R>(ma: ReaderMiddleware<R, I, I, E, A>) => ReaderMiddleware<R, I, I, E, B> = chainMiddlewareKW
export const fromMiddlewareK =
<R, A extends ReadonlyArray<unknown>, B, I, O, E>(
f: (...a: A) => M.Middleware<I, O, E, B>
): ((...a: A) => ReaderMiddleware<R, I, O, E, B>) =>
(...a) =>
fromMiddleware(f(...a))

/**
* @category combinators
* @since 0.6.5
*/
export const ichainMiddlewareKW =
<R, A, O, Z, E, B>(f: (a: A) => M.Middleware<O, Z, E, B>) =>
<I, D>(ma: ReaderMiddleware<R, I, O, D, A>): ReaderMiddleware<R, I, Z, D | E, B> =>
pipe(ma, ichainW(fromMiddlewareK(f)))

/**
* @category combinators
* @since 0.6.3
*/
export const ichainMiddlewareK: <R, A, O, Z, E, B>(
f: (a: A) => M.Middleware<O, Z, E, B>
) => <I>(ma: ReaderMiddleware<R, I, O, E, A>) => ReaderMiddleware<R, I, Z, E, B> = chainMiddlewareK as any
) => <I>(ma: ReaderMiddleware<R, I, O, E, A>) => ReaderMiddleware<R, I, Z, E, B> = ichainMiddlewareKW

/**
* @category combinators
* @since 0.6.5
* @since 0.6.3
*/
export const ichainMiddlewareKW: <R, A, O, Z, E, B>(
f: (a: A) => M.Middleware<O, Z, E, B>
) => <I, D>(ma: ReaderMiddleware<R, I, O, D, A>) => ReaderMiddleware<R, I, Z, D | E, B> = chainMiddlewareK as any
export const chainMiddlewareK: <I, E, A, B>(
f: (a: A) => M.Middleware<I, I, E, B>
) => <R>(ma: ReaderMiddleware<R, I, I, E, A>) => ReaderMiddleware<R, I, I, E, B> = chainMiddlewareKW

/**
* @category combinators
Expand Down
16 changes: 16 additions & 0 deletions test/ReaderMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,22 @@ describe('ReaderMiddleware', () => {
return assertSuccess(m1, r, c, 3, [])
})

describe('fromMiddlewareK', () => {
test('with a left', () => {
const m2 = (value: string) => M.left(value.length)
const m1 = _.fromMiddlewareK(m2)
const c = new MockConnection<H.StatusOpen>(new MockRequest())
return assertFailure(m1('foo'), undefined, c, 3)
})

test('with a right', () => {
const m2 = (value: string) => M.right(value.length)
const m1 = _.fromMiddlewareK(m2)
const c = new MockConnection<H.StatusOpen>(new MockRequest())
return assertSuccess(m1('foo'), undefined, c, 3, [])
})
})

it('chainMiddlewareK', () => {
const m1 = pipe(
_.right('foo'),
Expand Down

0 comments on commit 8e179c1

Please sign in to comment.