Merge pull request #6186 from joeskeen/request-promise

Add full 'request' api to 'request-promise'
This commit is contained in:
Masahiro Wakame
2015-11-05 23:08:26 +09:00
4 changed files with 976 additions and 80 deletions

View File

@@ -1,17 +1,464 @@
/// <reference path="request-promise.d.ts" />
import rp = require('request-promise');
import nodeRequest = require('request');
rp('http://www.google.com')
.then(console.dir)
.catch(console.error);
var options: rp.Options = {
var options: nodeRequest.Options = {
uri : 'http://posttestserver.com/post.php',
method : 'POST'
method : 'POST',
json: true,
body: { some: 'payload' }
};
rp(options)
.then(console.dir)
.catch(console.error);
// --> Displays length of response from server after post
// Get full response after DELETE
options = {
method: 'DELETE',
uri: 'http://my-server/path/to/resource/1234'
};
rp(options)
.then(function (response: http.IncomingMessage) {
console.log("DELETE succeeded with status %d", response.statusCode);
})
.catch(console.error);
//The following examples from https://github.com/request/request
import fs = require('fs');
import http = require('http');
var request = rp;
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body); // Show the HTML for the Google homepage.
}
});
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'));
fs.createReadStream('file.json').pipe(request.put('http://mysite.com/obj.json'));
request.get('http://google.com/img.png').pipe(request.put('http://mysite.com/img.png'));
request
.get('http://google.com/img.png')
.on('response', function(response: any) {
console.log(response.statusCode); // 200
console.log(response.headers['content-type']); // 'image/png'
})
.pipe(request.put('http://mysite.com/img.png'));
request
.get('http://mysite.com/doodle.png')
.on('error', function(err: any) {
console.log(err);
})
.pipe(fs.createWriteStream('doodle.png'));
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
if (req.method === 'PUT') {
req.pipe(request.put('http://mysite.com/doodle.png'));
} else if (req.method === 'GET' || req.method === 'HEAD') {
request.get('http://mysite.com/doodle.png').pipe(resp);
}
}
});
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
var x = request('http://mysite.com/doodle.png');
req.pipe(x);
x.pipe(resp);
}
});
var resp: http.ServerResponse;
var req: nodeRequest.Request;
req.pipe(request('http://mysite.com/doodle.png')).pipe(resp);
var r = request;
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
r.get('http://google.com/doodle.png').pipe(resp);
}
});
request.post('http://service.com/upload', {form:{key:'value'}});
// or
request.post('http://service.com/upload').form({key:'value'});
// or
request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ /* ... */ });
var data = {
// Pass a simple key-value pair
my_field: 'my_value',
// Pass data via Buffers
my_buffer: new Buffer([1, 2, 3]),
// Pass data via Streams
my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
// Pass multiple values /w an Array
attachments: [
fs.createReadStream(__dirname + '/attachment1.jpg'),
fs.createReadStream(__dirname + '/attachment2.jpg')
],
// Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
// Use case: for some types of streams, you'll need to provide "file"-related information manually.
// See the `form-data` README for more information about options: https://github.com/felixge/node-form-data
custom_file: {
value: fs.createReadStream('/dev/urandom'),
options: {
filename: 'topsecret.jpg',
contentType: 'image/jpg'
}
}
};
request.post({url:'http://service.com/upload', formData: data}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
var requestMultipart = request.post('http://service.com/upload', function optionalCallback(err, httpResponse, body) {});
var form = requestMultipart.form();
form.append('my_field', 'my_value');
form.append('my_buffer', new Buffer([1, 2, 3]));
form.append('custom_file', fs.createReadStream(__dirname + '/unicycle.jpg'), {filename: 'unicycle.jpg'});
request({
method: 'PUT',
preambleCRLF: true,
postambleCRLF: true,
uri: 'http://service.com/upload',
multipart: {
chunked: false,
data: [
{
'content-type': 'application/json',
body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
},
{ body: 'I am an attachment' }
]
}
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
console.log('Upload successful! Server responded with:', body);
});
request({
method: 'PUT',
preambleCRLF: true,
postambleCRLF: true,
uri: 'http://service.com/upload',
multipart: [
{
'content-type': 'application/json',
body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
},
{ body: 'I am an attachment' },
{ body: fs.createReadStream('image.png') }
]
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
console.log('Upload successful! Server responded with:', body);
});
request.get('http://some.server.com/').auth('username', 'password', false);
// or
request.get('http://some.server.com/', {
'auth': {
'user': 'username',
'pass': 'password',
'sendImmediately': false
}
});
// or
request.get('http://some.server.com/').auth(null, null, true, 'bearerToken');
// or
request.get('http://some.server.com/', {
'auth': {
'bearer': 'bearerToken'
}
});
var username = 'username',
password = 'password',
url = 'http://' + username + ':' + password + '@some.server.com';
request({url: url}, function (error, response, body) {
// Do more stuff with 'body' here
});
options = {
url: 'https://api.github.com/repos/request/request',
headers: {
'User-Agent': 'request'
}
};
function callback(error: any, response: http.IncomingMessage, body: string) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info.stargazers_count + " Stars");
console.log(info.forks_count + " Forks");
}
}
request(options, callback);
// OAuth1.0 - 3-legged server side flow (Twitter example)
// step 1
import qs = require('querystring');
const CONSUMER_KEY = 'key';
const CONSUMER_SECRET = 'secret';
var oauth =
{ callback: 'http://mysite.com/callback/'
, consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
}
, url = 'https://api.twitter.com/oauth/request_token'
;
request.post({url:url, oauth:oauth}, function (e, r, body) {
// Ideally, you would take the body in the response
// and construct a URL that a user clicks on (like a sign in button).
// The verifier is only available in the response after a user has
// verified with twitter that they are authorizing your app.
// step 2
var req_data = qs.parse(body);
var uri = 'https://api.twitter.com/oauth/authenticate'
+ '?' + qs.stringify({oauth_token: req_data.oauth_token});
// redirect the user to the authorize uri
// step 3
// after the user is redirected back to your server
var auth_data: any = qs.parse(body)
, oauth =
{ consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
, token: auth_data.oauth_token
, token_secret: req_data.oauth_token_secret
, verifier: auth_data.oauth_verifier
}
, url = 'https://api.twitter.com/oauth/access_token'
;
request.post({url:url, oauth:oauth}, function (e, r, body) {
// ready to make signed requests on behalf of the user
var perm_data: any = qs.parse(body);
var oauth =
{ consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
, token: perm_data.oauth_token
, token_secret: perm_data.oauth_token_secret
};
var url = 'https://api.twitter.com/1.1/users/show.json';
var query = {
screen_name: perm_data.screen_name,
user_id: perm_data.user_id
};
request.get({url:url, oauth:oauth, qs:query, json:true}, function (e, r, user) {
console.log(user);
});
});
});
var path = require('path')
, certFile = path.resolve(__dirname, 'ssl/client.crt')
, keyFile = path.resolve(__dirname, 'ssl/client.key')
, caFile = path.resolve(__dirname, 'ssl/ca.cert.pem');
options = {
url: 'https://api.some-server.com/',
cert: fs.readFileSync(certFile),
key: fs.readFileSync(keyFile),
passphrase: 'password',
ca: fs.readFileSync(caFile)
};
request.get(options);
var path = require('path')
, certFile = path.resolve(__dirname, 'ssl/client.crt')
, keyFile = path.resolve(__dirname, 'ssl/client.key');
options = {
url: 'https://api.some-server.com/',
agentOptions: {
cert: fs.readFileSync(certFile),
key: fs.readFileSync(keyFile),
// Or use `pfx` property replacing `cert` and `key` when using private key, certificate and CA certs in PFX or PKCS12 format:
// pfx: fs.readFileSync(pfxFilePath),
passphrase: 'password',
securityOptions: 'SSL_OP_NO_SSLv3'
}
};
request.get(options);
request.get({
url: 'https://api.some-server.com/',
agentOptions: {
secureProtocol: 'SSLv3_method'
}
});
request.get({
url: 'https://api.some-server.com/',
agentOptions: {
ca: fs.readFileSync('ca.cert.pem')
}
});
request({
// will be ignored
method: 'GET',
uri: 'http://www.google.com',
// HTTP Archive Request Object
har: {
url: 'http://www.mockbin.com/har',
method: 'POST',
headers: [
{
name: 'content-type',
value: 'application/x-www-form-urlencoded'
}
],
postData: {
mimeType: 'application/x-www-form-urlencoded',
params: [
{
name: 'foo',
value: 'bar'
},
{
name: 'hello',
value: 'world'
}
]
}
}
});
//requests using baseRequest() will set the 'x-token' header
var baseRequest = request.defaults({
headers: {'x-token': 'my-token'}
});
//requests using specialRequest() will include the 'x-token' header set in
//baseRequest and will also include the 'special' header
var specialRequest = baseRequest.defaults({
headers: {special: 'special value'}
});
request.put(url);
request.patch(url);
request.post(url);
request.head(url);
request.del(url);
request.get(url);
request.cookie('key1=value1');
request.jar();
request.debug = true;
request.get('http://10.255.255.1', {timeout: 1500}, function(err) {
console.log(err.code === 'ETIMEDOUT');
// Set to `true` if the timeout was a connection timeout, `false` or
// `undefined` otherwise.
console.log(err.connect === true);
process.exit(0);
});
var rand = Math.floor(Math.random()*100000000).toString();
request(
{ method: 'PUT'
, uri: 'http://mikeal.iriscouch.com/testjs/' + rand
, multipart:
[ { 'content-type': 'application/json'
, body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
}
, { body: 'I am an attachment' }
]
}
, function (error, response, body) {
if(response.statusCode == 201){
console.log('document saved as: http://mikeal.iriscouch.com/testjs/'+ rand)
} else {
console.log('error: '+ response.statusCode)
console.log(body)
}
}
);
request(
{ method: 'GET'
, uri: 'http://www.google.com'
, gzip: true
}
, function (error, response, body) {
// body is the decompressed response body
console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
console.log('the decoded data is: ' + body)
}
).on('data', function(data: any) {
// decompressed data as it is received
console.log('decoded chunk: ' + data)
})
.on('response', function(response: http.IncomingMessage) {
// unmodified http.IncomingMessage object
response.on('data', function(data: any[]) {
// compressed data as it is received
console.log('received ' + data.length + ' bytes of compressed data')
})
});
var requestWithJar = request.defaults({jar: true})
requestWithJar('http://www.google.com', function () {
requestWithJar('http://images.google.com');
});
var j = request.jar()
requestWithJar = request.defaults({jar:j})
requestWithJar('http://www.google.com', function () {
requestWithJar('http://images.google.com');
});
var j = request.jar();
var cookie = request.cookie('key1=value1');
var url = 'http://www.google.com';
j.setCookie(cookie, url);
request({url: url, jar: j}, function () {
request('http://images.google.com');
});
//TODO: add definitions for tough-cookie-filestore
//var FileCookieStore = require('tough-cookie-filestore');
// NOTE - currently the 'cookies.json' file must already exist!
//var j = request.jar(new FileCookieStore('cookies.json'));
requestWithJar = request.defaults({ jar : j })
request('http://www.google.com', function() {
request('http://images.google.com');
});
var j = request.jar()
request({url: 'http://www.google.com', jar: j}, function () {
var cookie_string = j.getCookieString(url); // "key1=value1; key2=value2; ..."
var cookies = j.getCookies(url);
// [{key: 'key1', value: 'value1', domain: "www.google.com", ...}, ...]
});

View File

@@ -1,32 +1,30 @@
// Type definitions for request-promise v0.4.2
// Project: https://www.npmjs.com/package/request-promise
// Definitions by: Christopher Glantschnig <https://github.com/cglantschnig/>
// Definitions by: Christopher Glantschnig <https://github.com/cglantschnig/>, Joe Skeen <http://github.com/joeskeen>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
// Change [0]: 2015/08/20 - Aya Morisawa <https://github.com/AyaMorisawa>
/// <reference path="../node/node.d.ts" />
/// <reference path="../form-data/form-data.d.ts" />
/// <reference path="../request/request.d.ts" />
/// <reference path="../bluebird/bluebird.d.ts" />
declare module 'request-promise' {
import request = require('request');
import stream = require('stream');
import http = require('http');
import FormData = require('form-data');
export = RequestPromiseAPI;
function RequestPromiseAPI(options: RequestPromiseAPI.Options): Promise<any>;
function RequestPromiseAPI(uri: string): Promise<request.Request>;
module RequestPromiseAPI {
interface AdditionalOptions {
simple?: boolean;
transform?: (body: any, response: http.IncomingMessage) => any;
resolveWithFullResponse?: boolean;
}
export type Options = AdditionalOptions & request.Options;
interface RequestPromise extends request.Request {
then(onFulfilled: Function, onRejected?: Function): Promise<any>;
catch(onRejected: Function): Promise<any>;
finally(onFinished: Function): Promise<any>;
promise(): Promise<any>;
}
interface RequestPromiseOptions extends request.OptionalOptions {
simple?: boolean;
transform?: (body: any, response: http.IncomingMessage) => any;
resolveWithFullResponse?: boolean;
}
var requestPromise: request.RequestAPI<RequestPromise, RequestPromiseOptions>;
export = requestPromise;
}

View File

@@ -4,6 +4,7 @@ import request = require('request');
import http = require('http');
import stream = require('stream');
import formData = require('form-data');
import fs = require('fs');
var value: any;
var str: string;
@@ -20,7 +21,7 @@ var headers: {[key: string]: string};
var agent: http.Agent;
var write: stream.Writable;
var req: request.Request;
var form: formData.FormData;
var form1: formData.FormData;
var bodyArr: request.RequestPart[] = [{
body: value
@@ -32,7 +33,7 @@ var bodyArr: request.RequestPart[] = [{
// --- --- --- --- --- --- --- --- --- --- --- ---
str = req.toJSON();
obj = req.toJSON();
var cookieValue: request.CookieValue;
str = cookieValue.name;
@@ -125,8 +126,6 @@ req.destroy();
// --- --- --- --- --- --- --- --- --- --- --- ---
var callback: (error: any, response: any, body: any) => void;
value = request.initParams;
req = request(uri);
@@ -136,13 +135,6 @@ req = request(uri, callback);
req = request(options);
req = request(options, callback);
req = request.request(uri);
req = request.request(uri, options);
req = request.request(uri, options, callback);
req = request.request(uri, callback);
req = request.request(options);
req = request.request(options, callback);
req = request.get(uri);
req = request.get(uri, options);
req = request.get(uri, options, callback);
@@ -204,3 +196,428 @@ request
// check response
})
.pipe(request.put('http://another.com/another.png'));
//The following examples from https://github.com/request/request
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body); // Show the HTML for the Google homepage.
}
});
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'));
fs.createReadStream('file.json').pipe(request.put('http://mysite.com/obj.json'));
request.get('http://google.com/img.png').pipe(request.put('http://mysite.com/img.png'));
request
.get('http://google.com/img.png')
.on('response', function(response: any) {
console.log(response.statusCode); // 200
console.log(response.headers['content-type']); // 'image/png'
})
.pipe(request.put('http://mysite.com/img.png'));
request
.get('http://mysite.com/doodle.png')
.on('error', function(err: any) {
console.log(err);
})
.pipe(fs.createWriteStream('doodle.png'));
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
if (req.method === 'PUT') {
req.pipe(request.put('http://mysite.com/doodle.png'));
} else if (req.method === 'GET' || req.method === 'HEAD') {
request.get('http://mysite.com/doodle.png').pipe(resp);
}
}
});
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
var x = request('http://mysite.com/doodle.png');
req.pipe(x);
x.pipe(resp);
}
});
var resp: http.ServerResponse;
req.pipe(request('http://mysite.com/doodle.png')).pipe(resp);
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
r.get('http://google.com/doodle.png').pipe(resp);
}
});
request.post('http://service.com/upload', {form:{key:'value'}});
// or
request.post('http://service.com/upload').form({key:'value'});
// or
request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ /* ... */ });
var data = {
// Pass a simple key-value pair
my_field: 'my_value',
// Pass data via Buffers
my_buffer: new Buffer([1, 2, 3]),
// Pass data via Streams
my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
// Pass multiple values /w an Array
attachments: [
fs.createReadStream(__dirname + '/attachment1.jpg'),
fs.createReadStream(__dirname + '/attachment2.jpg')
],
// Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
// Use case: for some types of streams, you'll need to provide "file"-related information manually.
// See the `form-data` README for more information about options: https://github.com/felixge/node-form-data
custom_file: {
value: fs.createReadStream('/dev/urandom'),
options: {
filename: 'topsecret.jpg',
contentType: 'image/jpg'
}
}
};
request.post({url:'http://service.com/upload', formData: data}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
var requestMultipart = request.post('http://service.com/upload', function optionalCallback(err, httpResponse, body) {});
var form = requestMultipart.form();
form.append('my_field', 'my_value');
form.append('my_buffer', new Buffer([1, 2, 3]));
form.append('custom_file', fs.createReadStream(__dirname + '/unicycle.jpg'), {filename: 'unicycle.jpg'});
request({
method: 'PUT',
preambleCRLF: true,
postambleCRLF: true,
uri: 'http://service.com/upload',
multipart: {
chunked: false,
data: [
{
'content-type': 'application/json',
body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
},
{ body: 'I am an attachment' }
]
}
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
console.log('Upload successful! Server responded with:', body);
});
request({
method: 'PUT',
preambleCRLF: true,
postambleCRLF: true,
uri: 'http://service.com/upload',
multipart: [
{
'content-type': 'application/json',
body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
},
{ body: 'I am an attachment' },
{ body: fs.createReadStream('image.png') }
]
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
console.log('Upload successful! Server responded with:', body);
});
request.get('http://some.server.com/').auth('username', 'password', false);
// or
request.get('http://some.server.com/', {
'auth': {
'user': 'username',
'pass': 'password',
'sendImmediately': false
}
});
// or
request.get('http://some.server.com/').auth(null, null, true, 'bearerToken');
// or
request.get('http://some.server.com/', {
'auth': {
'bearer': 'bearerToken'
}
});
var username = 'username',
password = 'password',
url = 'http://' + username + ':' + password + '@some.server.com';
request({url: url}, function (error, response, body) {
// Do more stuff with 'body' here
});
options = {
url: 'https://api.github.com/repos/request/request',
headers: {
'User-Agent': 'request'
}
};
function callback(error: any, response: http.IncomingMessage, body: string) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info.stargazers_count + " Stars");
console.log(info.forks_count + " Forks");
}
}
request(options, callback);
// OAuth1.0 - 3-legged server side flow (Twitter example)
// step 1
import qs = require('querystring');
const CONSUMER_KEY = 'key';
const CONSUMER_SECRET = 'secret';
oauth =
{ callback: 'http://mysite.com/callback/'
, consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
}
, url = 'https://api.twitter.com/oauth/request_token'
;
request.post({url:url, oauth:oauth}, function (e, r, body) {
// Ideally, you would take the body in the response
// and construct a URL that a user clicks on (like a sign in button).
// The verifier is only available in the response after a user has
// verified with twitter that they are authorizing your app.
// step 2
var req_data = qs.parse(body);
var uri = 'https://api.twitter.com/oauth/authenticate'
+ '?' + qs.stringify({oauth_token: req_data.oauth_token});
// redirect the user to the authorize uri
// step 3
// after the user is redirected back to your server
var auth_data: any = qs.parse(body)
, oauth =
{ consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
, token: auth_data.oauth_token
, token_secret: req_data.oauth_token_secret
, verifier: auth_data.oauth_verifier
}
, url = 'https://api.twitter.com/oauth/access_token'
;
request.post({url:url, oauth:oauth}, function (e, r, body) {
// ready to make signed requests on behalf of the user
var perm_data: any = qs.parse(body);
var oauth =
{ consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
, token: perm_data.oauth_token
, token_secret: perm_data.oauth_token_secret
};
var url = 'https://api.twitter.com/1.1/users/show.json';
var query = {
screen_name: perm_data.screen_name,
user_id: perm_data.user_id
};
request.get({url:url, oauth:oauth, qs:query, json:true}, function (e, r, user) {
console.log(user);
});
});
});
var path = require('path')
, certFile = path.resolve(__dirname, 'ssl/client.crt')
, keyFile = path.resolve(__dirname, 'ssl/client.key')
, caFile = path.resolve(__dirname, 'ssl/ca.cert.pem');
options = {
url: 'https://api.some-server.com/',
cert: fs.readFileSync(certFile),
key: fs.readFileSync(keyFile),
passphrase: 'password',
ca: fs.readFileSync(caFile)
};
request.get(options);
var path = require('path')
, certFile = path.resolve(__dirname, 'ssl/client.crt')
, keyFile = path.resolve(__dirname, 'ssl/client.key');
options = {
url: 'https://api.some-server.com/',
agentOptions: {
cert: fs.readFileSync(certFile),
key: fs.readFileSync(keyFile),
// Or use `pfx` property replacing `cert` and `key` when using private key, certificate and CA certs in PFX or PKCS12 format:
// pfx: fs.readFileSync(pfxFilePath),
passphrase: 'password',
securityOptions: 'SSL_OP_NO_SSLv3'
}
};
request.get(options);
request.get({
url: 'https://api.some-server.com/',
agentOptions: {
secureProtocol: 'SSLv3_method'
}
});
request.get({
url: 'https://api.some-server.com/',
agentOptions: {
ca: fs.readFileSync('ca.cert.pem')
}
});
request({
// will be ignored
method: 'GET',
uri: 'http://www.google.com',
// HTTP Archive Request Object
har: {
url: 'http://www.mockbin.com/har',
method: 'POST',
headers: [
{
name: 'content-type',
value: 'application/x-www-form-urlencoded'
}
],
postData: {
mimeType: 'application/x-www-form-urlencoded',
params: [
{
name: 'foo',
value: 'bar'
},
{
name: 'hello',
value: 'world'
}
]
}
}
});
//requests using baseRequest() will set the 'x-token' header
var baseRequest = request.defaults({
headers: {'x-token': 'my-token'}
});
//requests using specialRequest() will include the 'x-token' header set in
//baseRequest and will also include the 'special' header
var specialRequest = baseRequest.defaults({
headers: {special: 'special value'}
});
request.put(url);
request.patch(url);
request.post(url);
request.head(url);
request.del(url);
request.get(url);
request.cookie('key1=value1');
request.jar();
request.debug = true;
request.get('http://10.255.255.1', {timeout: 1500}, function(err) {
console.log(err.code === 'ETIMEDOUT');
// Set to `true` if the timeout was a connection timeout, `false` or
// `undefined` otherwise.
console.log(err.connect === true);
process.exit(0);
});
var rand = Math.floor(Math.random()*100000000).toString();
request(
{ method: 'PUT'
, uri: 'http://mikeal.iriscouch.com/testjs/' + rand
, multipart:
[ { 'content-type': 'application/json'
, body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
}
, { body: 'I am an attachment' }
]
}
, function (error, response, body) {
if(response.statusCode == 201){
console.log('document saved as: http://mikeal.iriscouch.com/testjs/'+ rand)
} else {
console.log('error: '+ response.statusCode)
console.log(body)
}
}
);
request(
{ method: 'GET'
, uri: 'http://www.google.com'
, gzip: true
}
, function (error, response, body) {
// body is the decompressed response body
console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
console.log('the decoded data is: ' + body)
}
).on('data', function(data: any) {
// decompressed data as it is received
console.log('decoded chunk: ' + data)
})
.on('response', function(response: http.IncomingMessage) {
// unmodified http.IncomingMessage object
response.on('data', function(data: any[]) {
// compressed data as it is received
console.log('received ' + data.length + ' bytes of compressed data')
})
});
var requestWithJar = request.defaults({jar: true})
requestWithJar('http://www.google.com', function () {
requestWithJar('http://images.google.com');
});
var j = request.jar()
requestWithJar = request.defaults({jar:j})
requestWithJar('http://www.google.com', function () {
requestWithJar('http://images.google.com');
});
var j = request.jar();
cookie = request.cookie('key1=value1');
var url = 'http://www.google.com';
j.setCookie(cookie, url);
request({url: url, jar: j}, function () {
request('http://images.google.com');
});
//TODO: add definitions for tough-cookie-filestore
//var FileCookieStore = require('tough-cookie-filestore');
// NOTE - currently the 'cookies.json' file must already exist!
//var j = request.jar(new FileCookieStore('cookies.json'));
requestWithJar = request.defaults({ jar : j })
request('http://www.google.com', function() {
request('http://images.google.com');
});
var j = request.jar()
request({url: 'http://www.google.com', jar: j}, function () {
var cookie_string = j.getCookieString(url); // "key1=value1; key2=value2; ..."
var cookies = j.getCookies(url);
// [{key: 'key1', value: 'value1', domain: "www.google.com", ...}, ...]
});

132
request/request.d.ts vendored
View File

@@ -1,6 +1,6 @@
// Type definitions for request
// Project: https://github.com/mikeal/request
// Definitions by: Carlos Ballesteros Velasco <https://github.com/soywiz>, bonnici <https://github.com/bonnici>, Bart van der Schoor <https://github.com/Bartvds>
// Definitions by: Carlos Ballesteros Velasco <https://github.com/soywiz>, bonnici <https://github.com/bonnici>, Bart van der Schoor <https://github.com/Bartvds>, Joe Skeen <http://github.com/joeskeen>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
// Imported from: https://github.com/soywiz/typescript-node-definitions/d.ts
@@ -13,57 +13,54 @@ declare module 'request' {
import http = require('http');
import FormData = require('form-data');
import url = require('url');
import fs = require('fs');
export = RequestAPI;
namespace request {
export interface RequestAPI<TRequest extends Request, TOptions extends OptionalOptions> {
defaults(options: TOptions): RequestAPI<TRequest, TOptions>;
(uri: string, options?: TOptions, callback?: RequestCallback): TRequest;
(uri: string, callback?: RequestCallback): TRequest;
(options?: RequiredOptions & TOptions, callback?: RequestCallback): TRequest;
function RequestAPI(uri: string, options?: RequestAPI.Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): RequestAPI.Request;
function RequestAPI(uri: string, callback?: (error: any, response: http.IncomingMessage, body: any) => void): RequestAPI.Request;
function RequestAPI(options: RequestAPI.Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): RequestAPI.Request;
get(uri: string, options?: TOptions, callback?: RequestCallback): TRequest;
get(uri: string, callback?: RequestCallback): TRequest;
get(options: RequiredOptions & TOptions, callback?: RequestCallback): TRequest;
module RequestAPI {
export function defaults(options: Options): typeof RequestAPI;
post(uri: string, options?: TOptions, callback?: RequestCallback): TRequest;
post(uri: string, callback?: RequestCallback): TRequest;
post(options: RequiredOptions & TOptions, callback?: RequestCallback): TRequest;
export function request(uri: string, options?: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function request(uri: string, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function request(options?: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
put(uri: string, options?: TOptions, callback?: RequestCallback): TRequest;
put(uri: string, callback?: RequestCallback): TRequest;
put(options: RequiredOptions & TOptions, callback?: RequestCallback): TRequest;
export function get(uri: string, options?: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function get(uri: string, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function get(options: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
head(uri: string, options?: TOptions, callback?: RequestCallback): TRequest;
head(uri: string, callback?: RequestCallback): TRequest;
head(options: RequiredOptions & TOptions, callback?: RequestCallback): TRequest;
export function post(uri: string, options?: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function post(uri: string, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function post(options: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
patch(uri: string, options?: TOptions, callback?: RequestCallback): TRequest;
patch(uri: string, callback?: RequestCallback): TRequest;
patch(options: RequiredOptions & TOptions, callback?: RequestCallback): TRequest;
export function put(uri: string, options?: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function put(uri: string, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function put(options: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
del(uri: string, options?: TOptions, callback?: RequestCallback): TRequest;
del(uri: string, callback?: RequestCallback): TRequest;
del(options: RequiredOptions & TOptions, callback?: RequestCallback): TRequest;
export function head(uri: string, options?: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function head(uri: string, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function head(options: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
forever(agentOptions: any, optionsArg: any): TRequest;
jar(): CookieJar;
cookie(str: string): Cookie;
export function patch(uri: string, options?: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function patch(uri: string, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function patch(options: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
initParams: any;
debug: boolean;
}
export function del(uri: string, options?: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function del(uri: string, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
export function del(options: Options, callback?: (error: any, response: http.IncomingMessage, body: any) => void): Request;
interface UriOptions {
uri: string;
}
export function forever(agentOptions: any, optionsArg: any): Request;
export function jar(): CookieJar;
export function cookie(str: string): Cookie;
export var initParams: any;
interface UriOptions {
uri: string;
}
interface UrlOptions {
url: string;
}
interface UrlOptions {
url: string;
}
interface OptionalOptions {
callback?: (error: any, response: http.IncomingMessage, body: any) => void;
@@ -73,10 +70,10 @@ declare module 'request' {
auth?: AuthOptions;
oauth?: OAuthOptions;
aws?: AWSOptions;
hawk ?: HawkOptions;
hawk?: HawkOptions;
qs?: any;
json?: any;
multipart?: RequestPart[];
multipart?: RequestPart[] | Multipart;
agentOptions?: any;
agentClass?: any;
forever?: any;
@@ -85,7 +82,7 @@ declare module 'request' {
method?: string;
headers?: Headers;
body?: any;
followRedirect?: boolean|((response: http.IncomingMessage) => boolean);
followRedirect?: boolean | ((response: http.IncomingMessage) => boolean);
followAllRedirects?: boolean;
maxRedirects?: number;
encoding?: string;
@@ -94,9 +91,44 @@ declare module 'request' {
proxy?: any;
strictSSL?: boolean;
gzip?: boolean;
preambleCRLF?: boolean;
postambleCRLF?: boolean;
key?: Buffer;
cert?: Buffer;
passphrase?: string;
ca?: Buffer;
har?: HttpArchiveRequest;
}
export type Options = (UriOptions|UrlOptions)&OptionalOptions;
export type RequiredOptions = UriOptions | UrlOptions;
export type Options = RequiredOptions & OptionalOptions;
export interface RequestCallback {
(error: any, response: http.IncomingMessage, body: any): void;
}
export interface HttpArchiveRequest {
url?: string;
method?: string;
headers?: NameValuePair[];
postData?: {
mimeType?: string;
params?: NameValuePair[];
}
}
export interface NameValuePair {
name: string;
value: string;
}
export interface Multipart {
chunked?: boolean;
data?: {
'content-type'?: string,
body: string
}[];
}
export interface RequestPart {
headers?: Headers;
@@ -137,7 +169,7 @@ declare module 'request' {
resume(): void;
abort(): void;
destroy(): void;
toJSON(): string;
toJSON(): Object;
}
export interface Headers {
@@ -172,9 +204,9 @@ declare module 'request' {
}
export interface CookieJar {
setCookie(cookie: Cookie, uri: string|url.Url, options?: any): void
getCookieString(uri: string|url.Url): string
getCookies(uri: string|url.Url): Cookie[]
setCookie(cookie: Cookie, uri: string | url.Url, options?: any): void
getCookieString(uri: string | url.Url): string
getCookies(uri: string | url.Url): Cookie[]
}
export interface CookieValue {
@@ -191,4 +223,6 @@ declare module 'request' {
toString(): string;
}
}
var request: request.RequestAPI<request.Request, request.OptionalOptions>;
export = request;
}