import { IncomingMessage , ServerResponse } from "http" ; const createHttpListener = < C , R > ( buildContext , handler , mapErrors ) => ( req , res ) => Promise . resolve ( ) . then ( ( ) => buildContext ( req ) ) . then ( context => handler ( context , req ) ) . then ( result => result != null ? [ 200 , result ] : [ 404 , { error : 'Not Found' } ] ) . catch ( ( err ) => mapErrors ( err ) ?? [ 500 , { error : err . message } ] ) . then ( ( [ code , data ] ) => { res . statusCode = code ; res . end ( JSON . stringify ( data , null , 2 ) ) ; } ) ; export default createHttpListener ;