Skip to content

Commit

Permalink
Prefer lifting functions
Browse files Browse the repository at this point in the history
Refs #388
  • Loading branch information
thewilkybarkid committed Oct 24, 2022
1 parent 22baca6 commit 9aaf742
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 29 deletions.
16 changes: 14 additions & 2 deletions src/write-review/write-review-add-author.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as O from 'fp-ts/Option'
import { Reader } from 'fp-ts/Reader'
import { constUndefined, flow, pipe } from 'fp-ts/function'
import { Status, StatusOpen } from 'hyper-ts'
import * as M from 'hyper-ts/lib/Middleware'
import * as RM from 'hyper-ts/lib/ReaderMiddleware'
import * as D from 'io-ts/Decoder'
import { Orcid, parse } from 'orcid-id-ts'
Expand Down Expand Up @@ -39,7 +40,10 @@ export const writeReviewAddAuthor = flow(
({ canAddAuthors }) => canAddAuthors,
() => 'not-found',
),
RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))),
RM.bindW(
'form',
RM.fromReaderTaskK(({ user }) => getForm(user.orcid, preprint.doi)),
),
RM.apSW('method', RM.fromMiddleware(getMethod)),
RM.ichainW(state =>
match(state)
Expand All @@ -50,7 +54,7 @@ export const writeReviewAddAuthor = flow(
RM.orElseW(error =>
match(error)
.with('not-found', () => notFound)
.otherwise(() => RM.fromMiddleware(seeOther(format(writeReviewMatch.formatter, { doi: preprint.doi })))),
.otherwise(fromMiddlewareK(() => seeOther(format(writeReviewMatch.formatter, { doi: preprint.doi })))),
),
),
),
Expand Down Expand Up @@ -246,6 +250,14 @@ function addAuthorForm(preprint: Preprint, otherAuthors: boolean, form: AddAutho
})
}

// https://github.com/DenisFrezzato/hyper-ts/pull/83
const fromMiddlewareK =
<R, A extends ReadonlyArray<unknown>, B, I, O, E>(
f: (...a: A) => M.Middleware<I, O, E, B>,
): ((...a: A) => RM.ReaderMiddleware<R, I, O, E, B>) =>
(...a) =>
RM.fromMiddleware(f(...a))

// https://github.com/DenisFrezzato/hyper-ts/pull/85
function fromReaderK<R, A extends ReadonlyArray<unknown>, B, I = StatusOpen, E = never>(
f: (...a: A) => Reader<R, B>,
Expand Down
5 changes: 4 additions & 1 deletion src/write-review/write-review-add-authors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export const writeReviewAddAuthors = flow(
'canAddAuthors',
fromReaderK(({ user }) => canAddAuthors(user)),
),
RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))),
RM.bindW(
'form',
RM.fromReaderTaskK(({ user }) => getForm(user.orcid, preprint.doi)),
),
RM.apSW('method', RM.fromMiddleware(getMethod)),
RM.ichainW(state =>
match(state)
Expand Down
5 changes: 4 additions & 1 deletion src/write-review/write-review-authors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export const writeReviewAuthors = flow(
pipe(
RM.right({ preprint }),
RM.apS('user', getUserFromSession()),
RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))),
RM.bindW(
'form',
RM.fromReaderTaskK(({ user }) => getForm(user.orcid, preprint.doi)),
),
RM.apSW('method', RM.fromMiddleware(getMethod)),
RM.ichainW(state => match(state).with({ method: 'POST' }, handleAuthorsForm).otherwise(showAuthorsForm)),
RM.orElseMiddlewareK(() => seeOther(format(writeReviewMatch.formatter, { doi: preprint.doi }))),
Expand Down
37 changes: 27 additions & 10 deletions src/write-review/write-review-change-author.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { format } from 'fp-ts-routing'
import * as E from 'fp-ts/Either'
import { fromOption as fromOption_ } from 'fp-ts/FromEither'
import * as O from 'fp-ts/Option'
import { Reader } from 'fp-ts/Reader'
import * as RA from 'fp-ts/ReadonlyArray'
import { Lazy, constUndefined, flow, pipe } from 'fp-ts/function'
import { Status, StatusOpen } from 'hyper-ts'
import * as M from 'hyper-ts/lib/Middleware'
import * as RM from 'hyper-ts/lib/ReaderMiddleware'
import * as D from 'io-ts/Decoder'
import { Orcid, parse } from 'orcid-id-ts'
Expand Down Expand Up @@ -38,11 +38,15 @@ export const writeReviewChangeAuthor = (doi: PreprintId['doi'], index: number) =
({ canAddAuthors }) => canAddAuthors,
() => 'not-found',
),
RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))),
RM.bindW('author', ({ form }) =>
fromOption(() => 'not-found')(
pipe(
O.fromNullable(form.otherAuthors),
RM.bindW(
'form',
RM.fromReaderTaskK(({ user }) => getForm(user.orcid, preprint.doi)),
),
RM.bindW(
'author',
fromOptionK(() => 'not-found')(
flow(
O.fromNullableK(({ form }) => form.otherAuthors),
O.chain(RA.lookup(index)),
O.let('index', () => index),
),
Expand All @@ -58,7 +62,7 @@ export const writeReviewChangeAuthor = (doi: PreprintId['doi'], index: number) =
RM.orElseW(error =>
match(error)
.with('not-found', () => notFound)
.otherwise(() => RM.fromMiddleware(seeOther(format(writeReviewMatch.formatter, { doi: preprint.doi })))),
.otherwise(fromMiddlewareK(() => seeOther(format(writeReviewMatch.formatter, { doi: preprint.doi })))),
),
),
),
Expand Down Expand Up @@ -277,13 +281,26 @@ function changeAuthorForm(preprint: Preprint, author: Author, form: ChangeAuthor
})
}

// https://github.com/DenisFrezzato/hyper-ts/pull/83
const fromMiddlewareK =
<R, A extends ReadonlyArray<unknown>, B, I, O, E>(
f: (...a: A) => M.Middleware<I, O, E, B>,
): ((...a: A) => RM.ReaderMiddleware<R, I, O, E, B>) =>
(...a) =>
RM.fromMiddleware(f(...a))

// https://github.com/DenisFrezzato/hyper-ts/pull/85
function fromReaderK<R, A extends ReadonlyArray<unknown>, B, I = StatusOpen, E = never>(
f: (...a: A) => Reader<R, B>,
): (...a: A) => RM.ReaderMiddleware<R, I, I, E, B> {
return (...a) => RM.rightReader(f(...a))
}

// https://github.com/DenisFrezzato/hyper-ts/pull/89
const fromOption: <E>(onNone: Lazy<E>) => <R, I, A>(ma: O.Option<A>) => RM.ReaderMiddleware<R, I, I, E, A> =
fromOption_(RM.FromEither)
// https://github.com/DenisFrezzato/hyper-ts/pull/88
function fromOptionK<E>(
onNone: Lazy<E>,
): <A extends ReadonlyArray<unknown>, B>(
f: (...a: A) => O.Option<B>,
) => <R, I = StatusOpen>(...a: A) => RM.ReaderMiddleware<R, I, I, E, B> {
return f => fromMiddlewareK((...a) => M.fromOption(onNone)(f(...a)))
}
5 changes: 4 additions & 1 deletion src/write-review/write-review-competing-interests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export const writeReviewCompetingInterests = flow(
pipe(
RM.right({ preprint }),
RM.apS('user', getUserFromSession()),
RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))),
RM.bindW(
'form',
RM.fromReaderTaskK(({ user }) => getForm(user.orcid, preprint.doi)),
),
RM.apSW('method', RM.fromMiddleware(getMethod)),
RM.ichainW(state =>
match(state).with({ method: 'POST' }, handleCompetingInterestsForm).otherwise(showCompetingInterestsForm),
Expand Down
5 changes: 4 additions & 1 deletion src/write-review/write-review-conduct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export const writeReviewConduct = flow(
pipe(
RM.right({ preprint }),
RM.apS('user', getUserFromSession()),
RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))),
RM.bindW(
'form',
RM.fromReaderTaskK(({ user }) => getForm(user.orcid, preprint.doi)),
),
RM.apSW('method', RM.fromMiddleware(getMethod)),
RM.ichainW(state =>
match(state).with({ method: 'POST' }, handleCodeOfConductForm).otherwise(showCodeOfConductForm),
Expand Down
5 changes: 4 additions & 1 deletion src/write-review/write-review-persona.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export const writeReviewPersona = flow(
pipe(
RM.right({ preprint }),
RM.apS('user', getUserFromSession()),
RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))),
RM.bindW(
'form',
RM.fromReaderTaskK(({ user }) => getForm(user.orcid, preprint.doi)),
),
RM.apSW('method', RM.fromMiddleware(getMethod)),
RM.ichainW(state => match(state).with({ method: 'POST' }, handlePersonaForm).otherwise(showPersonaForm)),
RM.orElseMiddlewareK(() => seeOther(format(writeReviewMatch.formatter, { doi: preprint.doi }))),
Expand Down
5 changes: 4 additions & 1 deletion src/write-review/write-review-post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export const writeReviewPost = flow(
'canAddAuthors',
fromReaderK(({ user }) => canAddAuthors(user)),
),
RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))),
RM.bindW(
'form',
RM.fromReaderTaskK(({ user }) => getForm(user.orcid, preprint.doi)),
),
RM.apSW('method', RM.fromMiddleware(getMethod)),
RM.ichainW(state =>
match(state)
Expand Down
28 changes: 18 additions & 10 deletions src/write-review/write-review-remove-author.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { format } from 'fp-ts-routing'
import * as E from 'fp-ts/Either'
import { fromOption as fromOption_ } from 'fp-ts/FromEither'
import * as O from 'fp-ts/Option'
import { Reader } from 'fp-ts/Reader'
import * as RA from 'fp-ts/ReadonlyArray'
Expand Down Expand Up @@ -43,11 +42,15 @@ export const writeReviewRemoveAuthor = (doi: PreprintId['doi'], index: number) =
({ canAddAuthors }) => canAddAuthors,
() => 'not-found',
),
RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))),
RM.bindW('author', ({ form }) =>
fromOption(() => 'not-found')(
pipe(
O.fromNullable(form.otherAuthors),
RM.bindW(
'form',
RM.fromReaderTaskK(({ user }) => getForm(user.orcid, preprint.doi)),
),
RM.bindW(
'author',
fromOptionK(() => 'not-found')(
flow(
O.fromNullableK(({ form }) => form.otherAuthors),
O.chain(RA.lookup(index)),
O.let('index', () => index),
),
Expand All @@ -63,7 +66,7 @@ export const writeReviewRemoveAuthor = (doi: PreprintId['doi'], index: number) =
RM.orElseW(error =>
match(error)
.with('not-found', () => notFound)
.otherwise(() => RM.fromMiddleware(seeOther(format(writeReviewMatch.formatter, { doi: preprint.doi })))),
.otherwise(fromMiddlewareK(() => seeOther(format(writeReviewMatch.formatter, { doi: preprint.doi })))),
),
),
),
Expand Down Expand Up @@ -273,6 +276,11 @@ function fromReaderK<R, A extends ReadonlyArray<unknown>, B, I = StatusOpen, E =
return (...a) => RM.rightReader(f(...a))
}

// https://github.com/DenisFrezzato/hyper-ts/pull/89
const fromOption: <E>(onNone: Lazy<E>) => <R, I, A>(ma: O.Option<A>) => RM.ReaderMiddleware<R, I, I, E, A> =
fromOption_(RM.FromEither)
// https://github.com/DenisFrezzato/hyper-ts/pull/88
function fromOptionK<E>(
onNone: Lazy<E>,
): <A extends ReadonlyArray<unknown>, B>(
f: (...a: A) => O.Option<B>,
) => <R, I = StatusOpen>(...a: A) => RM.ReaderMiddleware<R, I, I, E, B> {
return f => fromMiddlewareK((...a) => M.fromOption(onNone)(f(...a)))
}
5 changes: 4 additions & 1 deletion src/write-review/write-review-review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export const writeReviewReview = flow(
pipe(
RM.right({ preprint }),
RM.apS('user', getUserFromSession()),
RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))),
RM.bindW(
'form',
RM.fromReaderTaskK(({ user }) => getForm(user.orcid, preprint.doi)),
),
RM.apSW('method', RM.fromMiddleware(getMethod)),
RM.ichainW(state => match(state).with({ method: 'POST' }, handleReviewForm).otherwise(showReviewForm)),
RM.orElseMiddlewareK(() => seeOther(format(writeReviewMatch.formatter, { doi: preprint.doi }))),
Expand Down

0 comments on commit 9aaf742

Please sign in to comment.