servie v4.3.3
Standard, framework-agnostic HTTP interfaces for JavaScript servers and clients.
Installation
npm install servie --saveUsage
throwbackCompose middleware functions into a single functionpopsicleHTTP request library for node.js and browsersservie-lambdaServie transport layer for AWS Lambdaservie-httpServie transport layer for node.js HTTPget-bodyGeneral body parser for forms, JSON and textservie-corsCORS middleware for Servieservie-routeRouting middleware for Servieservie-mountMount Servie middleware on a path prefixservie-compat-httpMimic node.js HTTP using Servieservie-redirectCreate response objects for redirectionservie-cookie-storeAPI for managing client-side cookiesservie-errorhandlerStandard error handler for transport layersservie-finalhandlerStandard final handler for transport layershttp-errorsCreate HTTP errorsboomHTTP-friendly error objectsconsolidateTemplate rendering
import { Body, Request, Response, Headers, AbortController } from "servie";Servie is a universal package, meaning node.js and browsers are supported without needing configuration. This means the primary endpoint requires some
domtypes in TypeScript. When in a node.js, or browser, only environment, prefer importingservie/dist/{node,browser}instead.
Body
Base HTTP class shared between
RequestandResponse.
new Body(body, headers);Body
Supported body types, depending on environment:
- Node.js -
string | Buffer | ArrayBuffer | Readable | null | undefined - Browser -
string | ArrayBuffer | ReadableStream | null | undefined
Properties and Methods
bodyUsedBoolean whether the body is already consumedtext()Returns the body as aPromise<string>json()Returns the body withJSON.parseasPromise<any>arrayBuffer()Returns the body as aPromise<ArrayBuffer>clone()Clones an unconsumed bodydestroy()Consumes and destroys the body
Request
HTTP class for modelling a
Request, extendsBody.
new Request(input [, init]);Input
A URL string or another Request instance to clone from.
Init
method?HTTP request method.body?Any support body types.signal?A signal from anAbortController.headers?A map, list of tuples, orHeaderinstance to initialize from.trailer?A promise resolve to a support header initialization types (above).
Properties and Methods
urlRequested url stringmethodRequested method stringsignalSignal event emitterheadersAHeadersinstancetrailerAPromise<Headers>instanceclone()Clones the request into a new instance
Response
HTTP class for modelling a
Response, extendsBody.
new Response([body [, init]]);Body
One of the support body types (above).
Init
status?The numeric HTTP response status codestatusText?The HTTP response status text
Properties and Methods
statusThe numeric HTTP response status codestatusTextThe HTTP response status textokBoolean indicates successful response (statusbetween 200 and 299)headersAHeadersinstancetrailerAPromise<Headers>instanceclone()Clones the response into a new instance
Headers
Map representation of HTTP headers.
new Headers([init]);Init
Initialize headers from Iterable<HeaderTuple>, a HeadersObject or an existing Headers instance.
Methods
set(name: string, value: string | string[]): voidSet a HTTP header by overriding case-insensitive headers of the same nameappend(name: string, value: string | string[]): voidAppend a HTTP headerget(name: string): string | undefinedRetrieve a case-insensitive HTTP headergetAll(name: string): string[]Retrieve a list of matching case-insensitive HTTP headershas(name: string): booleanCheck if a case-insensitive header is already setdelete(name: string): voidDelete a case-insensitive headerasObject(): HeadersObjectReturn the lower-cased headers as a plain objectextend(obj: HeadersInit): thisExtends the current headers with an objectkeys()Iterable of the available header namesvalues()Iterable of header valuesentries()Iterable of headers as[key, value]clear()Clears the headers instanceclone()Clones theHeadersinstance
AbortController
Simple controller for aborting a
Requestinstance.
new AbortController();Properties and Methods
signalASignalinstance to pass to aRequestabort()Used to abort any listening requests through thesignal
Signal
Tiny event emitter for communicating during a request.
Methods
abortedBoolean indicating whether the request is abortedon(type, fn)Attach an event listener to an event typeoff(type, fn)Remove an event listener from an event typeeach(fn)Attach an event listener for all eventsnone(fn)Remove a global event listeneremit(type, ...args)Emit an event to all listeners
Standard Events
abortThe request has been abortedrequestBytesEmitted on request progress with current bytesrequestEndedThe request has endedrequestStartedThe request has been startedresponseBytesEmitted on response progress with current bytesresponseEndedThe response has endedresponseStartedThe response has started
Plugins can emit new types of events.
Implementation
If you're building the transports for Servie, there are some life cycle events you need to be aware of:
- Listen to the
errorevent onsignalfor errors - Listen to the
abortevent onsignalto destroy the connection - Resolve
trailerpromise and append to HTTP request or response - There are some existing built-in type-safe events in
SignalEventsyou can support
JavaScript
This module is designed for ES2017 environments and published with TypeScript definitions on NPM.
License
Apache 2.0
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago