Comparing version 7.0.1 to 8.0.0
import Promise = require('any-promise'); | ||
import Request from './request'; | ||
import Response from './response'; | ||
import { TextTypes } from './utils'; | ||
export declare type Types = 'document' | 'blob' | 'arraybuffer' | TextTypes | string; | ||
export declare type Types = 'text' | 'document' | 'blob' | 'arraybuffer' | 'json' | string; | ||
export interface Options { | ||
@@ -7,0 +6,0 @@ type?: Types; |
@@ -5,3 +5,2 @@ "use strict"; | ||
var index_1 = require('./plugins/index'); | ||
var utils_1 = require('./utils'); | ||
function createTransport(options) { | ||
@@ -22,3 +21,2 @@ return { | ||
var url = request.url, method = request.method; | ||
var isText = utils_1.textTypes.indexOf(type) > -1; | ||
if (window.location.protocol === 'https:' && /^http\:/.test(url)) { | ||
@@ -34,3 +32,3 @@ return reject(request.error("The request to \"" + url + "\" was blocked", 'EBLOCKED')); | ||
rawHeaders: parseToRawHeaders(xhr.getAllResponseHeaders()), | ||
body: isText ? utils_1.parse(request, xhr.responseText, type) : xhr.response, | ||
body: type === 'text' ? xhr.responseText : xhr.response, | ||
url: xhr.responseURL | ||
@@ -77,3 +75,3 @@ })); | ||
} | ||
if (!isText) { | ||
if (type !== 'text') { | ||
try { | ||
@@ -80,0 +78,0 @@ xhr.responseType = type; |
"use strict"; | ||
var CookieJar = (function () { | ||
function CookieJar() { | ||
throw new TypeError('Cookie jars are only available on node'); | ||
throw new TypeError('Cookie jars are not available in browsers'); | ||
} | ||
@@ -6,0 +6,0 @@ return CookieJar; |
@@ -5,4 +5,3 @@ import { IncomingMessage, ClientRequest } from 'http'; | ||
import Response from './response'; | ||
import { TextTypes } from './utils'; | ||
export declare type Types = 'buffer' | 'array' | 'uint8array' | 'stream' | TextTypes | string; | ||
export declare type Types = 'text' | 'buffer' | 'array' | 'uint8array' | 'stream' | string; | ||
export interface Options { | ||
@@ -9,0 +8,0 @@ type?: Types; |
@@ -12,4 +12,3 @@ "use strict"; | ||
var index_1 = require('./plugins/index'); | ||
var utils_1 = require('./utils'); | ||
var validTypes = ['buffer', 'array', 'uint8array', 'stream'].concat(utils_1.textTypes); | ||
var validTypes = ['text', 'buffer', 'array', 'uint8array', 'stream']; | ||
function createTransport(options) { | ||
@@ -215,3 +214,2 @@ return { | ||
var unzip = options.unzip !== false; | ||
var isText = utils_1.textTypes.indexOf(type) > -1; | ||
var result = new Promise(function (resolve, reject) { | ||
@@ -230,3 +228,3 @@ if (unzip) { | ||
} | ||
var encoding = isText ? 'string' : type; | ||
var encoding = type === 'text' ? 'string' : type; | ||
var concatStream = concat({ encoding: encoding }, resolve); | ||
@@ -236,7 +234,4 @@ stream.on('error', reject); | ||
}); | ||
if (isText) { | ||
return result.then(function (str) { return utils_1.parse(request, str, type); }); | ||
} | ||
return result; | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -7,1 +7,3 @@ import Promise = require('any-promise'); | ||
export declare const stringify: () => (request: Request, next: () => Promise<Response>) => Promise<Response>; | ||
export declare type ParseType = 'json' | 'urlencoded'; | ||
export declare function parse(type: ParseType | ParseType[], strict?: boolean): (request: Request, next: () => Promise<Response>) => Promise<Response>; |
"use strict"; | ||
var Promise = require('any-promise'); | ||
var FormData = require('form-data'); | ||
var arrify = require('arrify'); | ||
var querystring_1 = require('querystring'); | ||
@@ -8,4 +9,5 @@ var index_1 = require('./is-host/index'); | ||
var JSON_MIME_REGEXP = /^application\/(?:[\w!#\$%&\*`\-\.\^~]*\+)?json$/i; | ||
var QUERY_MIME_REGEXP = /^application\/x-www-form-urlencoded$/i; | ||
var URL_ENCODED_MIME_REGEXP = /^application\/x-www-form-urlencoded$/i; | ||
var FORM_MIME_REGEXP = /^multipart\/form-data$/i; | ||
var JSON_PROTECTION_PREFIX = /^\)\]\}',?\n/; | ||
function wrap(value) { | ||
@@ -43,3 +45,3 @@ return function () { return value; }; | ||
} | ||
else if (QUERY_MIME_REGEXP.test(type)) { | ||
else if (URL_ENCODED_MIME_REGEXP.test(type)) { | ||
request.body = querystring_1.stringify(body); | ||
@@ -56,2 +58,49 @@ } | ||
}); | ||
function parse(type, strict) { | ||
var types = arrify(type); | ||
for (var _i = 0, types_1 = types; _i < types_1.length; _i++) { | ||
var type_1 = types_1[_i]; | ||
if (type_1 !== 'json' && type_1 !== 'urlencoded') { | ||
throw new TypeError("Unexpected parse type: " + type_1); | ||
} | ||
} | ||
return function (request, next) { | ||
return next() | ||
.then(function (response) { | ||
var body = response.body; | ||
var responseType = response.type(); | ||
if (typeof body !== 'string') { | ||
throw request.error("Unable to parse non-string response body", 'EPARSE'); | ||
} | ||
if (responseType == null) { | ||
throw request.error("Unable to parse invalid response content type", 'EPARSE'); | ||
} | ||
if (body === '') { | ||
response.body = null; | ||
return response; | ||
} | ||
for (var _i = 0, types_2 = types; _i < types_2.length; _i++) { | ||
var type_2 = types_2[_i]; | ||
if (type_2 === 'json' && JSON_MIME_REGEXP.test(responseType)) { | ||
try { | ||
response.body = JSON.parse(body.replace(JSON_PROTECTION_PREFIX, '')); | ||
} | ||
catch (err) { | ||
throw request.error("Unable to parse response body: " + err.message, 'EPARSE', err); | ||
} | ||
return response; | ||
} | ||
if (type_2 === 'urlencoded' && URL_ENCODED_MIME_REGEXP.test(responseType)) { | ||
response.body = querystring_1.parse(body); | ||
return response; | ||
} | ||
} | ||
if (strict !== false) { | ||
throw request.error("Unhandled response type: " + responseType, 'EPARSE'); | ||
} | ||
return response; | ||
}); | ||
}; | ||
} | ||
exports.parse = parse; | ||
//# sourceMappingURL=common.js.map |
@@ -220,5 +220,5 @@ "use strict"; | ||
method: 'POST', | ||
body: EXAMPLE_BODY, | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
body: EXAMPLE_BODY | ||
}) | ||
.use(popsicle.plugins.parse('json')) | ||
.then(function (res) { | ||
@@ -236,5 +236,5 @@ t.deepEqual(res.body, EXAMPLE_BODY); | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
transport: popsicle.createTransport({ type: 'urlencoded' }) | ||
} | ||
}) | ||
.use(popsicle.plugins.parse('urlencoded')) | ||
.then(function (res) { | ||
@@ -291,5 +291,5 @@ t.deepEqual(res.body, EXAMPLE_BODY); | ||
url: REMOTE_URL + '/echo/query', | ||
query: EXAMPLE_BODY, | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
query: EXAMPLE_BODY | ||
}) | ||
.use(popsicle.plugins.parse('json')) | ||
.then(function (res) { | ||
@@ -302,4 +302,3 @@ t.deepEqual(res.body, EXAMPLE_BODY); | ||
url: REMOTE_URL + '/echo/query?query=true', | ||
query: EXAMPLE_BODY, | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
query: EXAMPLE_BODY | ||
}); | ||
@@ -315,2 +314,3 @@ var query = { | ||
return req | ||
.use(popsicle.plugins.parse('json')) | ||
.then(function (res) { | ||
@@ -326,4 +326,3 @@ if (typeof window === 'undefined') { | ||
url: REMOTE_URL + '/echo/query', | ||
query: 'query=true', | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
query: 'query=true' | ||
}); | ||
@@ -333,2 +332,3 @@ t.equal(req.url, REMOTE_URL + '/echo/query?query=true'); | ||
return req | ||
.use(popsicle.plugins.parse('json')) | ||
.then(function (res) { | ||
@@ -411,4 +411,3 @@ t.deepEqual(res.body, { query: 'true' }); | ||
body: EXAMPLE_BODY, | ||
method: 'POST', | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
method: 'POST' | ||
}); | ||
@@ -423,2 +422,3 @@ t.plan(3); | ||
return req | ||
.use(popsicle.plugins.parse('json')) | ||
.then(function (res) { | ||
@@ -444,6 +444,4 @@ t.deepEqual(res.body, EXAMPLE_BODY); | ||
t.test('parse json responses', function (t) { | ||
return popsicle.request({ | ||
url: REMOTE_URL + '/json', | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
}) | ||
return popsicle.request(REMOTE_URL + '/json') | ||
.use(popsicle.plugins.parse('json')) | ||
.then(function (res) { | ||
@@ -455,6 +453,4 @@ t.equal(res.type(), 'application/json'); | ||
t.test('parse form encoded responses', function (t) { | ||
return popsicle.request({ | ||
url: REMOTE_URL + '/foo', | ||
transport: popsicle.createTransport({ type: 'urlencoded' }) | ||
}) | ||
return popsicle.request(REMOTE_URL + '/foo') | ||
.use(popsicle.plugins.parse('urlencoded')) | ||
.then(function (res) { | ||
@@ -489,5 +485,5 @@ t.equal(res.type(), 'application/x-www-form-urlencoded'); | ||
'Content-Type': 'application/json' | ||
}, | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
} | ||
}) | ||
.use(popsicle.plugins.parse('json')) | ||
.then(function (res) { | ||
@@ -618,7 +614,5 @@ t.equal(res.body, null); | ||
'Content-Type': 'application/json' | ||
}, | ||
transport: popsicle.createTransport({ | ||
type: 'json' | ||
}) | ||
} | ||
}) | ||
.use(popsicle.plugins.parse('json')) | ||
.catch(function (err) { | ||
@@ -630,2 +624,19 @@ t.ok(/Unable to parse response body/i.test(err.message)); | ||
}); | ||
t.test('give a parse error on invalid response type', function (t) { | ||
t.plan(3); | ||
return popsicle.request({ | ||
url: REMOTE_URL + '/echo', | ||
method: 'POST', | ||
body: 'hello world', | ||
headers: { | ||
'Content-Type': 'foo/bar' | ||
} | ||
}) | ||
.use(popsicle.plugins.parse('json')) | ||
.catch(function (err) { | ||
t.equal(err.message, 'Unhandled response type: foo/bar'); | ||
t.equal(err.code, 'EPARSE'); | ||
t.ok(err.popsicle instanceof popsicle.Request); | ||
}); | ||
}); | ||
t.test('give a stringify error on invalid request body', function (t) { | ||
@@ -632,0 +643,0 @@ var obj = {}; |
{ | ||
"name": "popsicle", | ||
"version": "7.0.1", | ||
"version": "8.0.0", | ||
"description": "Simple HTTP requests for node and the browser", | ||
@@ -5,0 +5,0 @@ "main": "dist/common.js", |
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
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
218059
2116
55