Servie provides standard, framework-agnostic HTTP interfaces for servers and clients.
Installation
npm install servie --save
Usage
Common
Base HTTP class for common request and response logic.
import { Common } from 'servie'
Options
headers?
HTTP headers (Headers | HeadersObject | string[]
)trailers?
HTTP trailers (Headers | HeadersObject | string[]
)events?
An event emitter object (EventEmitter
)body?
Allowed HTTP bodies (string | Buffer | Readable | object
)
Properties
events
The request/response event emitterheaders
The submitted headers as a Headers
instancetrailers
The submitted trailers as a Headers
instancebody
The submitted body payloadbodyUsed
A boolean indicating where the body was readtype
A shorthand property for reading and writing the Content-Type
headerlength
A shorthand property for reading and writing Content-Length
as a numberstarted
Boolean indicating if a request/response has startedfinished
Boolean indicating if a request/response has finishedbytesTransferred
The number of bytes sent in the HTTP request/response
Methods
buffer(maxBufferSize): Promise<Buffer>
Read the body into a Buffer
objecttext(maxBufferSize): Promise<string>
Read the body as a string
stream(): Readable
Read the body as a Readable
streamsetTimeout(ms): void
Set a timeout on the request or response to be marked as finishedclearTimeout(ms): void
Clear a previous timeout
Events
headers
Emitted when the headers
object becomes availabletrailers
Emitted when the trailers
object becomes availablestarted
Emitted when the request/response has startedfinished
Emitted when the request/respone has finishedprogress
Emitted when the bytesTransferred
properties is incremented
Request
HTTP class for encapsulating a Request
, extends Common
.
import { Request } from 'servie'
Options
const request = new Request({
url: '/',
method: 'GET'
})
Extends Common
options.
url
The HTTP request url (string
)method?
The HTTP request method (string
, default: GET
)connection?
Connection information ({ remoteAddress?: string, remotePort?: number, localAddress?: string, localPort?: number, encrypted?: boolean }
)
Properties
url
The HTTP request url (string
)method
The HTTP request method upper-cased (string
)Url
The HTTP request url as a read-only parsed object (object
)connection
Connection information ({}
)
Methods
abort(): boolean
Emit an abort eventerror(message, code, status?, original?): HttpError
Create a HTTP error instance
Events
abort
Emitted when the request is aborted and MUST be handled by transporterror
Emitted when an out-of-band error occurs (e.g. abort or timeout) and MUST be handled by the transport
Response
HTTP class for encapsulating a Response
, extends Common
.
import { Response } from 'servie'
Options
const response = new Response(request, {})
Extends Common
options.
status?
The HTTP response status code (number
)statusText?
The HTTP response status message (string
)
Properties
status?
The HTTP response status code (number
)statusText?
The HTTP response status message (string
)
Used by Common
for Request
and Response
objects.
Options
Take a single parameter with the headers in object, array or Headers
format.
Properties
raw
The raw HTTP headers list (string[]
)
Methods
object(obj?: HeadersObject | null): HeadersObject | void
A getter/setter method for reading the headers as a lower-cased object (like node.js)set(name: string, value: string | string[]): this
Set a HTTP header by overriding case-insensitive headers of the same nameappend(name: string, value: string | string[]): this
Append a HTTP headerget(name: string): string | undefined
Retrieve a case-insensitive HTTP headergetAll(name: string): string[]
Retrieve a list of matching case-insensitive HTTP headershas(name: string): boolean
Check if a case-insensitive header is already setdelete(name: string): this
Delete a case-insensitive header
HttpError
Internally and externally triggered HTTP errors.
Properties
code
A unique error code (string
)status
A HTTP status code (number
)request
The Request
instance that triggered the error (Request
)message
Standard error message (string
)cause
Specified when the HTTP error was triggered by an underlying error
JavaScript
This module is designed for ES5 environments, but also requires Promise
to be available.
TypeScript
This project is written using TypeScript and publishes the definitions directly to NPM.
License
MIT