Skip to content

Commit

Permalink
improvement(request plugin): add resolveWithFullResponse option. Supp…
Browse files Browse the repository at this point in the history
…ort sending data with FormData
  • Loading branch information
Masquerade-Circus committed Jan 25, 2020
1 parent d5c3fba commit f3df252
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dist/valyrian.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/valyrian.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"clean-css": "^4.2.1",
"cssnano": "^4.1.10",
"favicons": "^5.5.0",
"form-data": "^3.0.0",
"node-fetch": "^2.6.0",
"parse5": "^5.1.1",
"purgecss": "1.4.2"
Expand Down
3 changes: 3 additions & 0 deletions plugins/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ let cssnano = require('cssnano');
let CleanCSS = require('clean-css');
let Purgecss = require('purgecss');
let fetch = require('node-fetch');
let FormData = require('form-data');

let {Document, parseHtml} = require('./utils/dom');
let treeAdapter = require('./utils/tree-adapter');
let requestPlugin = require('./request').default;

global.fetch = fetch;
global.FormData = FormData;
global.document = new Document();


let errorHandler = (resolve, reject) => (err) => {
if (err) {
return reject(err);
Expand Down
53 changes: 37 additions & 16 deletions plugins/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,46 @@ let plugin = function (v) {

async function request(method, url, data, options = {}) {
let opts = Object.assign(
{
method: method.toLowerCase(),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
},
request.options,
options
),
type = opts.headers.Accept;
{
method: method.toLowerCase(),
headers: {},
resolveWithFullResponse: false
},
request.options,
options
);

if (!opts.headers.Accept) {
opts.headers.Accept = 'application/json';
}

let acceptType = opts.headers.Accept;
let contentType = opts.headers['Content-Type'] || opts.headers['content-type'] || '';

if (opts.methods.indexOf(method) === -1) {
throw new Error('Method not allowed');
}

if (data !== undefined) {
if (data) {
if (opts.method === 'get' && typeof data === 'object') {
url += '?' + serialize(data);
}

if (opts.method !== 'get') {
opts.body = JSON.stringify(data);
if (/json/gi.test(contentType)) {
opts.body = JSON.stringify(data);
} else {
let formData;
if (data instanceof FormData) {
formData = data;
} else {
formData = new FormData();
for (let i in data) {
formData.append(i, data[i]);
}
}
opts.body = formData;
}
}
}

Expand All @@ -60,11 +77,15 @@ let plugin = function (v) {
throw err;
}

if (/text/gi.test(type)) {
return response.text();
if (opts.resolveWithFullResponse) {
return response;
}

if (/text/gi.test(acceptType)) {
response.text();
}

if (/json/gi.test(type)) {
if (/json/gi.test(acceptType)) {
return response.json();
}

Expand Down
27 changes: 27 additions & 0 deletions test/request_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ describe('Request', () => {
title: 'foo',
body: 'bar',
userId: 1
}, {
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
});
expect(res).toEqual({
userId: 1,
Expand All @@ -39,6 +43,10 @@ describe('Request', () => {
title: 'foo',
body: 'bar',
userId: 1
}, {
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
});

expect(res).toEqual({
Expand All @@ -52,12 +60,20 @@ describe('Request', () => {
it('should patch', async () => {
let res = await v.request.patch('https://jsonplaceholder.typicode.com/posts/1', {
body: 'bar'
}, {
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
});
expect(res).toEqual({
userId: 1,
id: 1,
title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
body: 'bar'
}, {
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
});
});

Expand All @@ -73,4 +89,15 @@ describe('Request', () => {
expect(res).toEqual(expect.any(Array));
expect(res.length).toEqual(10);
});

it('should resolve with full response', async () => {
let res = await v.request.get('https://jsonplaceholder.typicode.com/posts/1', null, {resolveWithFullResponse: true});
expect(res).toEqual(expect.objectContaining({
body: expect.any(Object),
url: 'https://jsonplaceholder.typicode.com/posts/1',
status: 200,
statusText: 'OK',
headers: expect.any(Object)
}));
});
});
11 changes: 10 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2652,7 +2652,7 @@ colors@^1.3.3, colors@^1.4.0:
resolved "https://registry.yarnpkg.com/colors/-/colors-1.4.0.tgz#c50491479d4c1bdaed2c9ced32cf7c7dc2360f78"
integrity sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==

combined-stream@^1.0.6, combined-stream@~1.0.6:
combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
Expand Down Expand Up @@ -4260,6 +4260,15 @@ form-data@2.5.1:
combined-stream "^1.0.6"
mime-types "^2.1.12"

form-data@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.0.tgz#31b7e39c85f1355b7139ee0c647cf0de7f83c682"
integrity sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
Expand Down

0 comments on commit f3df252

Please sign in to comment.