web-request
Simplifies making web requests with TypeScript async/await
This package makes it easier to perform web requests using TypeScript and async/await. It wraps the popular request package, extending it with an interface that facilitates async/await and strong-typing.
Examples
Get web-page content as a string...
var result = await WebRequest;console;
Get JSON data...
var url = 'http://query.yahooapis.com/v1/public/yql?q=select+*+from+yahoo.finance.quotes+where+symbol+IN+(%22YHOO%22,%22AAPL%22)&format=json&env=http://datatables.org/alltables.env';var data = await WebRequestjson<any>url;for var quote of dataqueryresultsquote console;
Get JSON data with a strongly typed result...
interface QuoteResult query: results: quote: Array< Symbol: string; Bid: string; DaysHigh: string; DaysLow: string; Volume: string; > var url = 'http://query.yahooapis.com/v1/public/yql?q=select+*+from+yahoo.finance.quotes+where+symbol+IN+(%22YHOO%22,%22AAPL%22)&format=json&env=http://datatables.org/alltables.env';var data = await WebRequestjson<QuoteResult>url;for var quote of dataqueryresultsquote console;
Perform a series of REST operations, one-by-one...
// Transfer all orders from customer #123 to customer #321 and then delete customer #123...var orders = await WebRequestjson<Order>'http://www.example.com/customers/123/orders';// Change status of all orders to backorder...for var order of orders orderstatus = "backorder";await WebRequest;await WebRequest;// Flag order #98765 as shipped...await WebRequest;
Getting Started
Make sure you're running Node v4 and TypeScript 1.7 or higher...
$ node -v
v4.2.6
$ npm install -g typescript tsd
$ tsc -v
Version 1.7.5
Install the web-request package and the typings definitions for Node.js...
$ npm install web-request
$ tsd install node
Write some code...
; { var result = await WebRequest; console;};
Save the above to a file (index.ts), build and run it!
$ tsc index.ts typings/node/node.d.ts --target es6 --module commonjs
$ node index.js
<!doctype html><html ...
Response Errors as Exceptions
The throwResponseError option will cause any response with a 400 or 500 level status to throw an exception. This option is disabled by default.
Throw an exception for a specific request.
await WebRequest;
Throw an exception for any request that results in an error response.
WebRequest;
Interface
: Promise<Response<string>>;function post(uri: string, options?: RequestOptions, content?: any): Promise<Response<string>>;function put(uri: string, options?: RequestOptions, content?: any): Promise<Response<string>>;function patch(uri: string, options?: RequestOptions, content?: any): Promise<Response<string>>;function head(uri: string, options?: RequestOptions): Promise<Response<void>>;function delete(uri: string, options?: RequestOptions): Promise<Response<string>>;function json<T>(uri: string, options?: RequestOptions): Promise<T>;function create<T>(uri: string, options?: RequestOptions, content?: any): Promise<Response<T>>;function stream(uri: string, options?: RequestOptions, content?: any): Promise<Response<void>>;function defaults(options: RequestOptions): void;function debug(value?: boolean): boolean; interface Request<T> extends request.Request { options: RequestOptions; response: Promise<Response<T>>;} <T> request: Request<T>; message: httpIncomingMessage; get : string; get : T; get : number; get : string; get : Cookie; get : Headers; get : string; get : Date; get : string; get : string; get : number; get : string; get : Uri;
Note the following interfaces are as defined by request...
More Examples
Setting defaults that apply for all requests is supported...
WebRequest;// now we can make requests without having to specify the root every time...var orders = await WebRequestjson<Order>'/customers/123/orders';await WebRequest;await WebRequest;
To make a request that requires authentication...
await WebRequest;
To make a request with custom headers...
await WebRequest;
To enable cookies, set jar to true or specify a custom cookie jar...
var response = await WebRequest;console;
Use the stream method to request a large resource efficiently with negligible memory impact...
var request = WebRequest; // 4.3Mbvar w = fs;request; // pipe content directly to a filevar response = await requestresponse; // wait for web-request to completeawait w; // wait for file-write to complete
Stream a file up to a server...
var request = WebRequest;fs;await requestresponse;
Stream a file from one server to another...
var request1 = WebRequest;var request2 = WebRequest;request1;await Promiseallrequest1response request2response;