then-request
Advanced tools
Comparing version 3.2.0 to 4.0.0
@@ -0,3 +1,13 @@ | ||
/// <reference types="node" /> | ||
import { HttpVerb } from 'http-basic/lib/HttpVerb'; | ||
import { Headers } from 'http-basic/lib/Headers'; | ||
import GenericResponse = require('http-response-object'); | ||
import { Options } from './Options'; | ||
import { ResponsePromise } from './ResponsePromise'; | ||
import { RequestFn } from './RequestFn'; | ||
declare type Response = GenericResponse<Buffer | string>; | ||
export { HttpVerb, Headers, Options, ResponsePromise, Response }; | ||
declare const fd: any; | ||
export { fd as FormData }; | ||
declare const _default: RequestFn; | ||
export = _default; | ||
export default _default; |
@@ -10,5 +10,7 @@ 'use strict'; | ||
}; | ||
var Response = require("http-response-object"); | ||
exports.__esModule = true; | ||
var GenericResponse = require("http-response-object"); | ||
var Promise = require("promise"); | ||
var ResponsePromise_1 = require("./ResponsePromise"); | ||
exports.ResponsePromise = ResponsePromise_1.ResponsePromise; | ||
var handle_qs_1 = require("./handle-qs"); | ||
@@ -82,2 +84,5 @@ function request(method, url, options) { | ||
} | ||
if (options.form) { | ||
options.body = options.form; | ||
} | ||
if (options.timeout) { | ||
@@ -103,3 +108,3 @@ xhr.timeout = options.timeout; | ||
}); | ||
var res = new Response(xhr.status, headers, xhr.responseText, url); | ||
var res = new GenericResponse(xhr.status, headers, xhr.responseText, url); | ||
resolve(res); | ||
@@ -117,2 +122,7 @@ } | ||
} | ||
var fd = FormData; | ||
exports.FormData = fd; | ||
exports["default"] = request; | ||
module.exports = request; | ||
module.exports["default"] = request; | ||
module.exports.FormData = fd; |
@@ -0,3 +1,13 @@ | ||
/// <reference types="node" /> | ||
import { HttpVerb } from 'http-basic/lib/HttpVerb'; | ||
import GenericResponse = require('http-response-object'); | ||
import { Headers } from 'http-basic/lib/Headers'; | ||
import { Options } from './Options'; | ||
import { ResponsePromise } from './ResponsePromise'; | ||
import { RequestFn } from './RequestFn'; | ||
import FormData = require('form-data'); | ||
declare type Response = GenericResponse<Buffer | string>; | ||
export { HttpVerb, Headers, Options, ResponsePromise, Response }; | ||
export { FormData }; | ||
declare const _default: RequestFn; | ||
export = _default; | ||
export default _default; |
161
lib/index.js
'use strict'; | ||
var Response = require("http-response-object"); | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
exports.__esModule = true; | ||
var GenericResponse = require("http-response-object"); | ||
var Promise = require("promise"); | ||
var concat = require("concat-stream"); | ||
var ResponsePromise_1 = require("./ResponsePromise"); | ||
exports.ResponsePromise = ResponsePromise_1.ResponsePromise; | ||
var handle_qs_1 = require("./handle-qs"); | ||
var _basicRequest = require("http-basic"); | ||
var FormData = require("form-data"); | ||
exports.FormData = FormData; | ||
var caseless = require('caseless'); | ||
var basicRequest = _basicRequest; | ||
var BufferBody = (function () { | ||
function BufferBody(body, extraHeaders) { | ||
this._body = body; | ||
this._headers = extraHeaders; | ||
} | ||
BufferBody.prototype.getHeaders = function () { | ||
return Promise.resolve(__assign({ 'content-length': '' + this._body.length }, this._headers)); | ||
}; | ||
BufferBody.prototype.pipe = function (stream) { | ||
stream.end(this._body); | ||
}; | ||
return BufferBody; | ||
}()); | ||
var FormBody = (function () { | ||
function FormBody(body) { | ||
this._body = body; | ||
} | ||
FormBody.prototype.getHeaders = function () { | ||
var _this = this; | ||
var headers = this._body.getHeaders(); | ||
return new Promise(function (resolve, reject) { | ||
var gotLength = false; | ||
_this._body.getLength(function (err, length) { | ||
if (gotLength) | ||
return; | ||
gotLength = true; | ||
if (err) { | ||
return reject(typeof err == 'string' | ||
? new Error(err) | ||
: err); | ||
} | ||
headers['content-length'] = '' + length; | ||
resolve(headers); | ||
}); | ||
}); | ||
}; | ||
FormBody.prototype.pipe = function (stream) { | ||
this._body.pipe(stream); | ||
}; | ||
return FormBody; | ||
}()); | ||
function handleBody(options) { | ||
if (options.form) { | ||
return new FormBody(options.form); | ||
} | ||
var extraHeaders = {}; | ||
var body = options.body; | ||
if (options.json) { | ||
extraHeaders['content-type'] = 'application/json'; | ||
body = JSON.stringify(options.json); | ||
} | ||
if (typeof body === 'string') { | ||
body = Buffer.from(body); | ||
} | ||
if (!body) { | ||
body = new Buffer(0); | ||
} | ||
if (!Buffer.isBuffer(body)) { | ||
throw new TypeError('body should be a Buffer or a String'); | ||
} | ||
return new BufferBody(body, extraHeaders); | ||
} | ||
function request(method, url, options) { | ||
if (options === void 0) { options = {}; } | ||
return ResponsePromise_1["default"](new Promise(function (resolve, reject) { | ||
@@ -32,46 +107,54 @@ // check types of arguments | ||
} | ||
// handle json body | ||
if (options.json) { | ||
options.body = JSON.stringify(options.json); | ||
headers.set('Content-Type', 'application/json'); | ||
var duplex = !(method === 'GET' || method === 'DELETE' || method === 'HEAD'); | ||
if (duplex) { | ||
var body_1 = handleBody(options); | ||
body_1.getHeaders().then(function (bodyHeaders) { | ||
Object.keys(bodyHeaders).forEach(function (key) { | ||
if (!headers.has(key)) { | ||
headers.set(key, bodyHeaders[key]); | ||
} | ||
}); | ||
ready(body_1); | ||
})["catch"](reject); | ||
} | ||
var body = options.body ? options.body : new Buffer(0); | ||
if (typeof body === 'string') | ||
body = new Buffer(body); | ||
if (!Buffer.isBuffer(body)) { | ||
throw new TypeError('body should be a Buffer or a String'); | ||
else if (options.body) { | ||
throw new Error('You cannot pass a body to a ' + method + ' request.'); | ||
} | ||
if (!headers.has('Content-Length')) { | ||
headers.set('Content-Length', body.length); | ||
else { | ||
ready(); | ||
} | ||
var req = basicRequest(method, url, { | ||
allowRedirectHeaders: options.allowRedirectHeaders, | ||
headers: options.headers, | ||
followRedirects: options.followRedirects !== false, | ||
maxRedirects: options.maxRedirects, | ||
gzip: options.gzip !== false, | ||
cache: options.cache, | ||
agent: options.agent, | ||
timeout: options.timeout, | ||
socketTimeout: options.socketTimeout, | ||
retry: options.retry, | ||
retryDelay: options.retryDelay, | ||
maxRetries: options.maxRetries, | ||
isMatch: options.isMatch, | ||
isExpired: options.isExpired, | ||
canCache: options.canCache | ||
}, function (err, res) { | ||
if (err) | ||
return reject(err); | ||
res.body.on('error', reject); | ||
res.body.pipe(concat(function (body) { | ||
resolve(new Response(res.statusCode, res.headers, Array.isArray(body) ? new Buffer(0) : body, res.url)); | ||
})); | ||
}); | ||
if (req) { | ||
req.end(body); | ||
function ready(body) { | ||
var req = basicRequest(method, url, { | ||
allowRedirectHeaders: options.allowRedirectHeaders, | ||
headers: options.headers, | ||
followRedirects: options.followRedirects !== false, | ||
maxRedirects: options.maxRedirects, | ||
gzip: options.gzip !== false, | ||
cache: options.cache, | ||
agent: options.agent, | ||
timeout: options.timeout, | ||
socketTimeout: options.socketTimeout, | ||
retry: options.retry, | ||
retryDelay: options.retryDelay, | ||
maxRetries: options.maxRetries, | ||
isMatch: options.isMatch, | ||
isExpired: options.isExpired, | ||
canCache: options.canCache | ||
}, function (err, res) { | ||
if (err) | ||
return reject(err); | ||
res.body.on('error', reject); | ||
res.body.pipe(concat(function (body) { | ||
resolve(new GenericResponse(res.statusCode, res.headers, Array.isArray(body) ? new Buffer(0) : body, res.url)); | ||
})); | ||
}); | ||
if (req && body) { | ||
body.pipe(req); | ||
} | ||
} | ||
})); | ||
} | ||
request._setBasicRequest = function (_basicRequest) { return basicRequest = _basicRequest; }; | ||
exports["default"] = request; | ||
module.exports = request; | ||
module.exports["default"] = request; | ||
module.exports.FormData = FormData; |
/// <reference types="node" /> | ||
/// <reference types="form-data" /> | ||
import { Agent } from 'http'; | ||
@@ -7,2 +8,3 @@ import { Headers } from 'http-basic/lib/Headers'; | ||
import { CachedResponse } from 'http-basic/lib/CachedResponse'; | ||
import FormData = require('form-data'); | ||
interface Options { | ||
@@ -28,4 +30,5 @@ allowRedirectHeaders?: string[]; | ||
json?: any; | ||
form?: FormData; | ||
body?: string | Buffer; | ||
} | ||
export { Options }; |
@@ -27,1 +27,2 @@ "use strict"; | ||
exports["default"] = toResponsePromise; | ||
exports.ResponsePromise = undefined; |
{ | ||
"name": "then-request", | ||
"version": "3.2.0", | ||
"version": "4.0.0", | ||
"description": "A request library that returns promises, inspired by request", | ||
@@ -14,2 +14,3 @@ "keywords": [], | ||
"@types/concat-stream": "^1.6.0", | ||
"@types/form-data": "0.0.33", | ||
"@types/node": "^8.0.0", | ||
@@ -19,2 +20,3 @@ "@types/qs": "^6.2.31", | ||
"concat-stream": "^1.6.0", | ||
"form-data": "^2.2.0", | ||
"http-basic": "^5.0.3", | ||
@@ -27,2 +29,3 @@ "http-response-object": "^2.0.3", | ||
"browserify": "^14.4.0", | ||
"busboy": "^0.2.14", | ||
"exorcist": "^0.4.0", | ||
@@ -34,2 +37,3 @@ "flowgen2": "^2.0.0-alpha.12", | ||
"mkdirp": "^0.5.1", | ||
"multiparty": "^4.1.3", | ||
"rimraf": "^2.6.1", | ||
@@ -36,0 +40,0 @@ "testit": "^2.1.3", |
# then-request | ||
A request library that returns promises, inspired by request | ||
A request library that returns promises and supports both browsers and node.js | ||
@@ -17,3 +17,3 @@ [![Build Status](https://img.shields.io/travis/then/then-request/master.svg)](https://travis-ci.org/then/then-request) | ||
e.g. | ||
The following examples all work on both client and server. | ||
@@ -26,4 +26,40 @@ ```js | ||
}); | ||
request('POST', 'http://example.com/json-api', {json: {some: 'values'}}).getBody('utf8').then(JSON.parse).done(function (res) { | ||
console.log(res); | ||
}); | ||
var FormData = request.FormData; | ||
var data = new FormData(); | ||
data.append('some', 'values'); | ||
request('POST', 'http://example.com/form-api', {form: data}).done(function (res) { | ||
console.log(res.getBody()); | ||
}); | ||
``` | ||
Or with ES6 | ||
```js | ||
import request, {FormData} from 'then-request'; | ||
request('GET', 'http://example.com').done((res) => { | ||
console.log(res.getBody()); | ||
}); | ||
request('POST', 'http://example.com/json-api', {json: {some: 'values'}}).getBody('utf8').then(JSON.parse).done((res) => { | ||
console.log(res); | ||
}); | ||
var FormData = request.FormData; | ||
var data = new FormData(); | ||
data.append('some', 'values'); | ||
request('POST', 'http://example.com/form-api', {form: data}).done((res) => { | ||
console.log(res.getBody()); | ||
}); | ||
``` | ||
**Method:** | ||
@@ -43,2 +79,3 @@ | ||
- `json` - sets `body` but to JSON representation of value and adds `Content-type: application/json`. Does not have any affect on how the response is treated. | ||
- `form` - You can pass a `FormData` instance to the `form` option, this will manage all the appropriate headers for you. Does not have any affect on how the response is treated. | ||
- `cache` - only used in node.js (browsers already have their own caches) Can be `'memory'`, `'file'` or your own custom implementaton (see https://github.com/ForbesLindesay/http-basic#implementing-a-cache). | ||
@@ -56,5 +93,5 @@ - `followRedirects` - defaults to `true` but can be explicitly set to `false` on node.js to prevent then-request following redirects automatically. | ||
**Callback / Returns:** | ||
**Returns:** | ||
If a callback is provided it is called with `err` and `res`. If no callback is provided, a [Promise](https://www.promisejs.org/) is returned that eventually resolves to `res`. The resulting Promise also has an additional `.getBody(encoding?)` method that is equivallent to calling `.then(function (res) { return res.getBody(encoding?); })`. | ||
A [Promise](https://www.promisejs.org/) is returned that eventually resolves to the `Response`. The resulting Promise also has an additional `.getBody(encoding?)` method that is equivallent to calling `.then(function (res) { return res.getBody(encoding?); })`. | ||
@@ -85,4 +122,14 @@ ### Response | ||
### FormData | ||
```js | ||
var FormData = require('then-request').FormData; | ||
``` | ||
Form data either exposes the node.js module, [form-data](https://www.npmjs.com/package/form-data), or the builtin browser object [FormData](https://developer.mozilla.org/en/docs/Web/API/FormData), as appropriate. | ||
They have broadly the same API, with the exception that form-data handles node.js streams and Buffers, while FormData handles the browser's `File` Objects. | ||
## License | ||
MIT |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28738
407
131
11
12
+ Added@types/form-data@0.0.33
+ Addedform-data@^2.2.0
+ Added@types/form-data@0.0.33(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedform-data@2.5.1(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)