Skip to content

Latest commit

 

History

History
482 lines (355 loc) · 16.2 KB

api.md

File metadata and controls

482 lines (355 loc) · 16.2 KB

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.stream : ReadableStream

Kind: instance property of RequestBody

requestBody.asBuffer([options]) ⇒ Promise.<Buffer>

Get the body as buffer.

Kind: instance method of 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

Param Type Default
[options] Object
[options.limit] string "1mb"

requestBody.asJSON([options]) ⇒ Promise.<*>

Get the body as JSON object.

Kind: instance method of RequestBody

Param Type Default
[options] Object
[options.limit] string "1mb"

Request

Represent a HTTP request.

Kind: global class

request.httpVersion : string

Kind: instance property of Request

request.method : string

Kind: instance property of Request

request.url : string

Kind: instance property of Request

request.query : string

Kind: instance property of Request

request.headers : Object

Kind: instance property of Request
See: headers

request.body : RequestBody

Kind: instance property of Request

request.parsedUrl : Object

Url object parsed by url.

Kind: instance property of Request

request.parsedQuery : Object

Query object parsed by querystring.

Kind: instance property of 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

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.statusCode : number

Kind: instance property of 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.headers : Object

Kind: instance property of Response

response.body : string | Buffer | ReadableStream

Body of this response.

Kind: instance property of Response

response.body : string | Buffer | ReadableStream

Kind: instance property of 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

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

Param Type
name string

Response.withStatusCode(statusCode, [message]) ⇒ 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

Param Type
statusCode number
[message] string

Response.withBufferBody(bufferBody) ⇒ Response

Build a response with given buffer body. The content-type header will be set as 'application/octet-stream'.

Kind: static method of Response

Param Type
bufferBody Buffer

Response.withStreamBody(streamBody) ⇒ Response

Build a response with given stream body. The content-type header will be set as 'application/octet-stream'.

Kind: static method of Response
Returns: Response - response

Param Type
streamBody ReadableStream

Response.withTextBody(textBody) ⇒ 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
Returns: Response - response

Param
textBody

Response.withJSONBody(jsonBody) ⇒ 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
Returns: Response - response

Param
jsonBody

adapt(handler) ⇒ NodeHTTPHandler

Convert a TH handler into a node HTTP handler.

Kind: global function

Param Type
handler THHandler

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.

Kind: global function

Param Type
downwardHandler DownwardHandler

buildDownwardWrapper~DownwardHandler : function

Kind: inner typedef of buildDownwardWrapper
Prototype: async (request: Request) => Request|Response
See

buildErrorWrapper(errorHandler) ⇒ THWrapper

Build a wrapper which could handle the error logic.

Kind: global function

Param Type
errorHandler ErrorHandler

buildErrorWrapper~ErrorHandler : function

Kind: inner typedef of buildErrorWrapper
Prototype: async (error: Error, request: Request) => Response
See

buildUpwardWrapper(upwardHandler) ⇒ THWrapper

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

Kind: global function

Param Type
upwardHandler UpwardHandler

buildUpwardWrapper~UpwardHandler : function

Kind: inner typedef of buildUpwardWrapper
Prototype: async (response: Response, request: Request) => Response
See

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

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

NodeHTTPHandler : function

Kind: global typedef
Prototype: (req, res) =>
See: Node.js create server

THWrapper : function

Kind: global typedef
Prototype: (handler: THHandler) => THHandler
See: THHandler