0.0.0 • Published 4 years ago

simple-hyper-ts v0.0.0

Weekly downloads
1
License
ISC
Repository
-
Last release
4 years ago

hippo

a simplified fork of hyper-ts.

Hello world

import * as H from 'hippo'
import { createServer } from 'http'
import { toRequestListener } from 'hippo/lib/http'
import { pipe } from 'fp-ts/lib/pipeable'
import { log } from 'fp-ts/lib/Console'

const hello = pipe(
  H.status(H.Status.OK),
  H.chain(() => H.send(Buffer.from('Hello, world!')))
)

pipe(createServer(hello), server => server.listen(8000, log(':8000')))

Sending a JSON

const hello = pipe(
  H.status(H.Status.OK),
  H.chain(() => H.json({ a: 1 }, () => 'error'))
)

Core API

Connection

A Connection models the entirety of a connection between the HTTP server and the user agent, both request and response.

interface Connection {
  readonly getRequest: () => IncomingMessage
  readonly getHeader: (name: string) => unknown
  readonly getMethod: () => string
  readonly getOriginalUrl: () => string
  readonly setStatus: (this: Connection, status: Status) => Connection
  readonly setHeader: (this: Connection, name: string, value: string) => Connection
  readonly endResponse: (this: Connection) => Connection
  readonly sendResponse: (this: Connection, body: Buffer) => Connection
}

Middleware

A middleware is a monadic action transforming one Connection to another Connection. It operates in the TaskEither monad.

interface Middleware<E, A> {
  (c: Connection): TaskEither<E, [A, Connection]>
}

Documentation