since import * as TE from 'fp-ts/TaskEither' ; import { Alt3 } from 'fp-ts/Alt' ; import { apFirst as apFirst_ , apSecond as apSecond_ , Apply3 , apS as apS_ } from 'fp-ts/Apply' ; import { bind as bind_ , Chain3 , chainFirst as chainFirst_ } from 'fp-ts/Chain' ; import { Bifunctor3 } from 'fp-ts/Bifunctor' ; import { identity , Lazy , pipe , Predicate , Refinement } from 'fp-ts/function' ; import { Functor3 , bindTo as bindTo_ } from 'fp-ts/Functor' ; import { Monad3 } from 'fp-ts/Monad' ; import { BodyOpen , Connection , CookieOptions , HeadersOpen , MediaType , ResponseEnded , Status , StatusOpen } from '.' ; import { Task } from 'fp-ts/Task' ; import { IO } from 'fp-ts/IO' ; import { IOEither } from 'fp-ts/IOEither' ; import { Applicative3 } from 'fp-ts/Applicative' ; import { MonadThrow3 } from 'fp-ts/MonadThrow' ; import { MonadTask3 } from 'fp-ts/MonadTask' ; import * as E from 'fp-ts/Either' ; import { Pointed3 } from 'fp-ts/Pointed' ; import { fromPredicate as fromPredicate_ , fromOption as fromOption_ , filterOrElse as filterOrElse_ , chainEitherK as chainEitherK_ , FromEither3 , } from 'fp-ts/FromEither' ; import * as O from 'fp-ts/Option' ; import * as J from 'fp-ts/Json' ; import { FromIO3 , fromIOK as fromIOK_ , chainIOK as chainIOK_ , chainFirstIOK as chainFirstIOK_ } from 'fp-ts/FromIO' ; import { FromTask3 , fromTaskK as fromTaskK_ , chainTaskK as chainTaskK_ , chainFirstTaskK as chainFirstTaskK_ , } from 'fp-ts/FromTask' ; declare module 'fp-ts/HKT' { interface URItoKind3 < R , E , A > { Middleware : Middleware < R , R , E , A > ; } } category since export const URI = 'Middleware' ; category since export type URI = typeof URI ; category since export interface Middleware < I , O , E , A > { ( c ) : TE . TaskEither < E , [ A , Connection < O > ] > ; } category since export function gets < I = StatusOpen , E = never , A = never > ( f ) { return ( c ) => TE . right ( [ f ( c ) , c ] ) ; } category since export function fromConnection < I = StatusOpen , E = never , A = never > ( f ) { return ( c ) => TE . fromEither ( pipe ( f ( c ) , E . map ( ( a ) => [ a , c ] ) ) ) ; } category since export function modifyConnection < I , O , E > ( f ) { return ( c ) => TE . right ( [ undefined , f ( c ) ] ) ; } const _map = ( fa , f ) => ( ci ) => pipe ( fa ( ci ) , TE . map ( ( [ a , co ] ) => [ f ( a ) , co ] ) ) ; const _apPar = ( fab , fa ) => pipe ( fab , ap ( fa ) ) ; const _apSeq = ( fab , fa ) => _chain ( fab , ( f ) => _map ( fa , ( a ) => f ( a ) ) ) ; const _chain = ( ma , f ) => ( c ) => pipe ( ma ( c ) , TE . chain ( ( [ a , c ] ) => f ( a ) ( c ) ) ) ; const _alt = ( fx , f ) => ( c ) => pipe ( fx ( c ) , TE . alt ( ( ) => f ( ) ( c ) ) ) ; const _bimap = ( fea , f , g ) => ( c ) => pipe ( fea ( c ) , TE . bimap ( f , ( [ a , c ] ) => [ g ( a ) , c ] ) ) ; const _mapLeft = ( fea , f ) => ( c ) => pipe ( fea ( c ) , TE . mapLeft ( f ) ) ; category since export const map = < A , B > ( f ) => < I , E > ( fa ) : Middleware < I , I , E , B > => _map ( fa , f ) ; category since export const imap = < A , B > ( f ) => < I , O , E > ( fa ) : Middleware < I , O , E , B > => ( ci ) => pipe ( fa ( ci ) , TE . map ( ( [ a , co ] ) => [ f ( a ) , co ] ) ) ; category since export const bimap = < E , G , A , B > ( f , g ) => < I > ( fa ) : Middleware < I , I , G , B > => _bimap ( fa , f , g ) ; category since export const mapLeft = < E , G > ( f ) => < I , A > ( fa ) : Middleware < I , I , G , A > => _mapLeft ( fa , f ) ; category since export const ap = < I , E , A > ( fa ) => < B > ( fab ) : Middleware < I , I , E , B > => _apSeq ( fab , fa ) ; category since export const apW = ap as any ; category since export const of = right ; category since export function iof < I = StatusOpen , O = StatusOpen , E = never , A = never > ( a ) { return ( c ) => TE . right ( [ a , c as any ] ) ; } category since export const chain = < I , E , A , B > ( f ) => ( ma ) : Middleware < I , I , E , B > => _chain ( ma , f ) ; category since export const chainW = chain as any ; category since export const flattenW = chainW ( identity ) ; category since export const flatten = flattenW ; category since export function ichainW < A , O , Z , E , B > ( f ) { return ( ma ) => ( ci ) => pipe ( ma ( ci ) , TE . chainW ( ( [ a , co ] ) => f ( a ) ( co ) ) ) ; } category since export const ichain = ichainW ; category since export function ichainFirstW < A , O , Z , E , B > ( f ) { return ( ma ) => ( ci ) => pipe ( ma ( ci ) , TE . chainW ( ( [ a , co ] ) => pipe ( f ( a ) ( co ) , TE . map ( ( [ _ , co ] ) => [ a , co ] ) ) ) ) ; } category since export const ichainFirst = ichainFirstW ; category since export const iflattenW = ichainW ( identity ) ; category since export const iflatten = iflattenW ; category since export const alt = < I , E , A > ( that ) => ( fa ) : Middleware < I , I , E , A > => _alt ( fa , that ) ; category since export const altW = alt as any ; since export function evalMiddleware < I , O , E , A > ( ma , c ) { return pipe ( ma ( c ) , TE . map ( ( [ a ] ) => a ) ) ; } since export function execMiddleware < I , O , E , A > ( ma , c ) { return pipe ( ma ( c ) , TE . map ( ( [ , c ] ) => c ) ) ; } category since export const orElseW = < E , I , O , M , B > ( f ) => < A > ( ma ) : Middleware < I , O , M , A | B > => ( c ) => pipe ( ma ( c ) , TE . orElseW ( ( e ) => f ( e ) ( c ) ) ) ; category since export const orElse = orElseW ; category since export function tryCatch < I = StatusOpen , E = never , A = never > ( f , onRejected ) { return fromTaskEither ( TE . tryCatch ( f , onRejected ) ) ; } category since export function fromTaskEither < I = StatusOpen , E = never , A = never > ( fa ) { return ( c ) => pipe ( fa , TE . map ( ( a ) => [ a , c ] ) ) ; } category since export function right < I = StatusOpen , E = never , A = never > ( a ) { return iof ( a ) ; } category since export function left < I = StatusOpen , E = never , A = never > ( e ) { return fromTaskEither ( TE . left ( e ) ) ; } category since export function rightTask < I = StatusOpen , E = never , A = never > ( fa ) { return fromTaskEither ( TE . rightTask ( fa ) ) ; } category since export function leftTask < I = StatusOpen , E = never , A = never > ( te ) { return fromTaskEither ( TE . leftTask ( te ) ) ; } category since export function rightIO < I = StatusOpen , E = never , A = never > ( fa ) { return fromTaskEither ( TE . rightIO ( fa ) ) ; } category since export function leftIO < I = StatusOpen , E = never , A = never > ( fe ) { return fromTaskEither ( TE . leftIO ( fe ) ) ; } category since export const fromEither = < I = StatusOpen , E = never , A = never > ( fa ) : Middleware < I , I , E , A > => fromTaskEither ( TE . fromEither ( fa ) ) ; category since export function fromIOEither < I = StatusOpen , E = never , A = never > ( fa ) { return fromTaskEither ( TE . fromIOEither ( fa ) ) ; } category since export function status < E = never > ( status ) { return modifyConnection ( ( c ) => c . setStatus ( status ) ) ; } category since export function header < E = never > ( name , value ) { return modifyConnection ( ( c ) => c . setHeader ( name , value ) ) ; } category since export function contentType < E = never > ( mediaType ) { return header ( 'Content-Type' , mediaType ) ; } category since export function cookie < E = never > ( name , value , options ) { return modifyConnection ( ( c ) => c . setCookie ( name , value , options ) ) ; } category since export function clearCookie < E = never > ( name , options ) { return modifyConnection ( ( c ) => c . clearCookie ( name , options ) ) ; } const closedHeaders = iof ( undefined ) ; category since export function closeHeaders < E = never > ( ) { return closedHeaders ; } category since export function send < E = never > ( body ) { return modifyConnection ( ( c ) => c . setBody ( body ) ) ; } const ended = modifyConnection ( ( c ) => c . endResponse ( ) ) ; category since export function end < E = never > ( ) { return ended ; } category since export function json < E > ( body , onError ) { return pipe ( fromEither < HeadersOpen , unknown , string > ( J . stringify ( body ) ) , mapLeft ( onError ) , ichain ( ( json ) => pipe ( contentType < E > ( MediaType . applicationJSON ) , ichain ( ( ) => closeHeaders ( ) ) , ichain ( ( ) => send ( json ) ) ) ) ) ; } category since export function redirect < E = never > ( uri ) { return pipe ( status ( Status . Found ) , ichain ( ( ) => header ( 'Location' , uri ) ) ) ; } category since export function pipeStream < E > ( stream ) { return modifyConnection ( ( c ) => c . pipeStream ( stream ) ) ; } const isUnknownRecord = ( u ) : u is Record < string , unknown > => u !== null && typeof u === 'object' ; category since export function decodeParam < E , A > ( name , f ) { return fromConnection ( ( c ) => { const params = c . getParams ( ) ; return f ( isUnknownRecord ( params ) ? params [ name ] : undefined ) ; } ) ; } category since export function decodeParams < E , A > ( f ) { return fromConnection ( ( c ) => f ( c . getParams ( ) ) ) ; } category since export function decodeQuery < E , A > ( f ) { return fromConnection ( ( c ) => f ( c . getQuery ( ) ) ) ; } category since export function decodeBody < E , A > ( f ) { return fromConnection ( ( c ) => f ( c . getBody ( ) ) ) ; } category since export function decodeMethod < E , A > ( f ) { return fromConnection ( ( c ) => f ( c . getMethod ( ) ) ) ; } category since export function decodeHeader < E , A > ( name , f ) { return fromConnection ( ( c ) => f ( c . getHeader ( name ) ) ) ; } category since export const Functor = { URI , map : _map , } ; category since export const Pointed = { URI , of , } ; category since export const ApplyPar = { ... Functor , ap : _apPar , } ; category since export const ApplySeq = { ... Functor , ap : _apSeq , } ; category since export const ApplicativePar = { ... ApplyPar , of , } ; category since export const ApplicativeSeq = { ... ApplySeq , of , } ; category since export const Chain = { ... Functor , ap : _apPar , chain : _chain , } ; category since export const Monad = { ... ApplicativePar , chain : _chain , } ; category since export const MonadThrow = { ... Monad , throwError : left , } ; category since export const Alt = { ... Functor , alt : _alt , } ; category since export const FromEither = { URI , fromEither , } ; category since export const Bifunctor = { URI , bimap : _bimap , mapLeft : _mapLeft , } ; category since export const MonadTask = { ... Monad , fromIO : rightIO , fromTask : rightTask , } ; category since export const apFirst = apFirst_ ( ApplyPar ) ; category since export const apFirstW = apFirst as any ; category since export const apSecond = apSecond_ ( ApplyPar ) ; category since export const apSecondW = apSecond as any ; category since export const chainFirst = chainFirst_ ( Chain ) ; category since export const chainFirstW = chainFirst as any ; category since export const fromPredicate = fromPredicate_ ( FromEither ) ; category since export const filterOrElse = filterOrElse_ ( FromEither , Chain ) ; category since export const filterOrElseW = filterOrElse ; category since export const fromOption = fromOption_ ( FromEither ) ; category since export const chainEitherK = chainEitherK_ ( FromEither , Chain ) ; category since export const chainEitherKW = chainEitherK as any ; category since export const fromIO = rightIO ; category since export const FromIO = { URI , fromIO , } ; category since export const fromIOK = fromIOK_ ( FromIO ) ; category since export const chainIOK = chainIOK_ ( FromIO , Chain ) ; category since export const chainFirstIOK = chainFirstIOK_ ( FromIO , Chain ) ; category since export const fromTask = rightTask ; category since export const FromTask = { ... FromIO , fromTask , } ; category since export const fromTaskK = fromTaskK_ ( FromTask ) ; category since export const chainTaskK = chainTaskK_ ( FromTask , Chain ) ; category since export const chainFirstTaskK = chainFirstTaskK_ ( FromTask , Chain ) ; category since export const chainTaskEitherKW = ( f ) => chainW ( ( a ) => fromTaskEither ( f ( a ) ) ) ; category since export const chainTaskEitherK = chainTaskEitherKW ; category since export const chainFirstTaskEitherKW = ( f ) => chainFirstW ( ( a ) => fromTaskEither ( f ( a ) ) ) ; category since export const chainFirstTaskEitherK = chainFirstTaskEitherKW ; since deprecated export const Do = iof < unknown , unknown , never , { } > ( { } ) ; since export const bindTo = bindTo_ ( Functor ) ; since export const ibindTo = bindTo as any ; since export const bind = bind_ ( Chain ) ; since export const bindW = bind as any ; since export const ibindW = bindW as any ; since export const ibind = ibindW ; since export const apS = apS_ ( ApplyPar ) ; since export const apSW = apS as any ; since export const iapSW = apS as any ; since export const iapS = iapSW ;