## Classes
HTTPError

Represent a HTTP error with status code and message.

RequestBody

Represent the body (payload or data) of a HTTP request. You can handle it via the stream field or fetch and use it as buffer, text or json via the provided helpers

Request

Represent a HTTP request.

Response

Represent a HTTP response. By default, a response is just like a plain object with some default values. You can create a response and set it on your own. However, it's usually convenient and better to use the static builders to build a proper initial response and make any needed adjustment to it.

## Functions
adapt(handler)NodeHTTPHandler

Convert a TH handler into a node HTTP handler.

buildDownwardWrapper(downwardHandler)THWrapper

Build a wrapper which could handle the downward logic. Generally it maps request (context), but if it returns a response, the flow will return right here, and the handlers below will be ignored.

buildErrorWrapper(errorHandler)THWrapper

Build a wrapper which could handle the error logic.

buildUpwardWrapper(upwardHandler)THWrapper

Build a wrapper which could handle the upward logic. Generally it maps response.

compose(...wrappers)function

Compose wrappers

listen(...args)function

Create a http server and listen for connections.

handleErrors([options])THWrapper

Provided default error handler wrapper. Generate response from the error and return it. If it's not a http client error, then log it.

## Typedefs
THHandler : function
NodeHTTPHandler : function
THWrapper : function
## HTTPError Represent a HTTP error with status code and message. **Kind**: global class ### new HTTPError([statusCode], [message], [originalError]) | Param | Type | | --- | --- | | [statusCode] | number | | [message] | string | | [originalError] | Error | ## RequestBody Represent the body (payload or data) of a HTTP request. You can handle it via the stream field or fetch and use it as buffer, text or json via the provided helpers **Kind**: global class * [RequestBody](#RequestBody) * [.stream](#RequestBody+stream) : ReadableStream * [.asBuffer([options])](#RequestBody+asBuffer) ⇒ Promise.<Buffer> * [.asText([options])](#RequestBody+asText) ⇒ Promise.<string> * [.asJSON([options])](#RequestBody+asJSON) ⇒ Promise.<\*> ### requestBody.stream : ReadableStream **Kind**: instance property of [RequestBody](#RequestBody) ### requestBody.asBuffer([options]) ⇒ Promise.<Buffer> Get the body as buffer. **Kind**: instance method of [RequestBody](#RequestBody) | Param | Type | Default | | --- | --- | --- | | [options] | Object | | | [options.limit] | string | "1mb" | ### requestBody.asText([options]) ⇒ Promise.<string> Get the body as string. **Kind**: instance method of [RequestBody](#RequestBody) | Param | Type | Default | | --- | --- | --- | | [options] | Object | | | [options.limit] | string | "1mb" | ### requestBody.asJSON([options]) ⇒ Promise.<\*> Get the body as JSON object. **Kind**: instance method of [RequestBody](#RequestBody) | Param | Type | Default | | --- | --- | --- | | [options] | Object | | | [options.limit] | string | "1mb" | ## Request Represent a HTTP request. **Kind**: global class * [Request](#Request) * [.httpVersion](#Request+httpVersion) : string * [.method](#Request+method) : string * [.url](#Request+url) : string * [.query](#Request+query) : string * [.headers](#Request+headers) : Object * [.body](#Request+body) : [RequestBody](#RequestBody) * [.parsedUrl](#Request+parsedUrl) : Object * [.parsedQuery](#Request+parsedQuery) : Object ### request.httpVersion : string **Kind**: instance property of [Request](#Request) ### request.method : string **Kind**: instance property of [Request](#Request) ### request.url : string **Kind**: instance property of [Request](#Request) ### request.query : string **Kind**: instance property of [Request](#Request) ### request.headers : Object **Kind**: instance property of [Request](#Request) **See**: [headers](https://nodejs.org/docs/latest-v8.x/api/http.html#http_message_headers) ### request.body : [RequestBody](#RequestBody) **Kind**: instance property of [Request](#Request) ### request.parsedUrl : Object Url object parsed by [url](https://nodejs.org/docs/latest-v8.x/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost). **Kind**: instance property of [Request](#Request) ### request.parsedQuery : Object Query object parsed by [querystring](https://nodejs.org/docs/latest-v8.x/api/querystring.html#querystring_querystring_parse_str_sep_eq_options). **Kind**: instance property of [Request](#Request) ## Response Represent a HTTP response. By default, a response is just like a plain object with some default values. You can create a response and set it on your own. However, it's usually convenient and better to use the static builders to build a proper initial response and make any needed adjustment to it. **Kind**: global class * [Response](#Response) * [new Response()](#new_Response_new) * _instance_ * [.statusCode](#Response+statusCode) : number * [.statusCode](#Response+statusCode) : number * [.headers](#Response+headers) : Object * [.headers](#Response+headers) : Object * [.body](#Response+body) : string \| Buffer \| ReadableStream * [.body](#Response+body) : string \| Buffer \| ReadableStream * [.setHeader(name, value)](#Response+setHeader) * [.getHeader(name)](#Response+getHeader) ⇒ string \| Array.<string> * _static_ * [.withStatusCode(statusCode, [message])](#Response.withStatusCode) ⇒ [Response](#Response) * [.withBufferBody(bufferBody)](#Response.withBufferBody) ⇒ [Response](#Response) * [.withStreamBody(streamBody)](#Response.withStreamBody) ⇒ [Response](#Response) * [.withTextBody(textBody)](#Response.withTextBody) ⇒ [Response](#Response) * [.withJSONBody(jsonBody)](#Response.withJSONBody) ⇒ [Response](#Response) ### new Response() Create a new response and set the default fields ### response.statusCode : number Status code of this response. The range is [100, 600) **Kind**: instance property of [Response](#Response) ### response.statusCode : number **Kind**: instance property of [Response](#Response) ### response.headers : Object Headers of this response. Generally you should use `setHeader` to avoid duplicate headers, but if you know what you are doing, you can use this setter to update headers more efficiently. **Kind**: instance property of [Response](#Response) ### response.headers : Object **Kind**: instance property of [Response](#Response) ### response.body : string \| Buffer \| ReadableStream Body of this response. **Kind**: instance property of [Response](#Response) ### response.body : string \| Buffer \| ReadableStream **Kind**: instance property of [Response](#Response) ### response.setHeader(name, value) Set the header value of given header name. The name will be converted to lowercase to avoid duplication. **Kind**: instance method of [Response](#Response) | Param | Type | | --- | --- | | name | string | | value | string \| Array.<string> | ### response.getHeader(name) ⇒ string \| Array.<string> Return the header value of given header name. **Kind**: instance method of [Response](#Response) | Param | Type | | --- | --- | | name | string | ### Response.withStatusCode(statusCode, [message]) ⇒ [Response](#Response) Build a response with given status code. The body will be set as the corresponding status text if not provided **Kind**: static method of [Response](#Response) | Param | Type | | --- | --- | | statusCode | number | | [message] | string | ### Response.withBufferBody(bufferBody) ⇒ [Response](#Response) Build a response with given buffer body. The content-type header will be set as 'application/octet-stream'. **Kind**: static method of [Response](#Response) | Param | Type | | --- | --- | | bufferBody | Buffer | ### Response.withStreamBody(streamBody) ⇒ [Response](#Response) Build a response with given stream body. The content-type header will be set as 'application/octet-stream'. **Kind**: static method of [Response](#Response) **Returns**: [Response](#Response) - response | Param | Type | | --- | --- | | streamBody | ReadableStream | ### Response.withTextBody(textBody) ⇒ [Response](#Response) Build a response with given text body. The body could be of any type, but it will be converted into string. The content-type header will be set as 'text/plain; charset=utf-8'. **Kind**: static method of [Response](#Response) **Returns**: [Response](#Response) - response | Param | | --- | | textBody | ### Response.withJSONBody(jsonBody) ⇒ [Response](#Response) Build a response with given JSON body. The body could be of any type, but it will be converted into JSON string. The content-type header will be set as 'application/json; charset=utf-8'. **Kind**: static method of [Response](#Response) **Returns**: [Response](#Response) - response | Param | | --- | | jsonBody | ## adapt(handler) ⇒ [NodeHTTPHandler](#NodeHTTPHandler) Convert a TH handler into a node HTTP handler. **Kind**: global function | Param | Type | | --- | --- | | handler | [THHandler](#THHandler) | ## buildDownwardWrapper(downwardHandler) ⇒ [THWrapper](#THWrapper) Build a wrapper which could handle the downward logic. Generally it maps request (context), but if it returns a response, the flow will return right here, and the handlers below will be ignored. **Kind**: global function | Param | Type | | --- | --- | | downwardHandler | [DownwardHandler](#buildDownwardWrapper..DownwardHandler) | ### buildDownwardWrapper~DownwardHandler : function **Kind**: inner typedef of [buildDownwardWrapper](#buildDownwardWrapper) **Prototype**: `async (request: Request) => Request|Response` **See** - [Request](#Request) - [Response](#Response) ## buildErrorWrapper(errorHandler) ⇒ [THWrapper](#THWrapper) Build a wrapper which could handle the error logic. **Kind**: global function | Param | Type | | --- | --- | | errorHandler | [ErrorHandler](#buildErrorWrapper..ErrorHandler) | ### buildErrorWrapper~ErrorHandler : function **Kind**: inner typedef of [buildErrorWrapper](#buildErrorWrapper) **Prototype**: `async (error: Error, request: Request) => Response` **See** - [Request](#Request) - [Response](#Response) ## buildUpwardWrapper(upwardHandler) ⇒ [THWrapper](#THWrapper) Build a wrapper which could handle the upward logic. Generally it maps response. **Kind**: global function | Param | Type | | --- | --- | | upwardHandler | [UpwardHandler](#buildUpwardWrapper..UpwardHandler) | ### buildUpwardWrapper~UpwardHandler : function **Kind**: inner typedef of [buildUpwardWrapper](#buildUpwardWrapper) **Prototype**: `async (response: Response, request: Request) => Response` **See** - [Request](#Request) - [Response](#Response) ## compose(...wrappers) ⇒ function Compose wrappers **Kind**: global function | Param | Type | | --- | --- | | ...wrappers | function | ## listen(...args) ⇒ function Create a http server and listen for connections. **Kind**: global function | Param | Description | | --- | --- | | ...args | the same with that of server.listen of node http package | ## handleErrors([options]) ⇒ [THWrapper](#THWrapper) Provided default error handler wrapper. Generate response from the error and return it. If it's not a http client error, then log it. **Kind**: global function | Param | Type | Default | | --- | --- | --- | | [options] | Object | | | [options.logError] | function | console.error | ## THHandler : function **Kind**: global typedef **Prototype**: `async (request: Request) => Response` **See** - [Request](#Request) - [Response](#Response) ## NodeHTTPHandler : function **Kind**: global typedef **Prototype**: `(req, res) =>` **See**: [Node.js create server](https://nodejs.org/docs/latest-v8.x/api/http.html#http_http_createserver_requestlistener) ## THWrapper : function **Kind**: global typedef **Prototype**: `(handler: THHandler) => THHandler` **See**: [THHandler](#THHandler)