Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Commit

Permalink
clean up mocks and do some golf
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Feb 12, 2018
1 parent 6b3f822 commit eb86f67
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 32 deletions.
1 change: 0 additions & 1 deletion index.js

This file was deleted.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "snekfetch",
"version": "3.6.4",
"main": "index.js",
"main": "src/index.js",
"module": "esm.mjs",
"unpkg": "browser.js",
"jsdelivr": "browser.js",
Expand Down Expand Up @@ -45,7 +45,6 @@
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*.js",
"!src/qs_mock.js",
"!src/node/mimeOfBuffer.js",
"!src/node/transports/http2.js"
],
Expand Down
9 changes: 9 additions & 0 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ module.exports = {
STATUS_CODES: {},
Extension: Object,
FormData: window.FormData,
querystring: {
parse: (str) => {
const parsed = {};
for (const [k, v] of new window.URLSearchParams(str).entries())
parsed[k] = v;
return parsed;
},
stringify: (obj) => new window.URLSearchParams(obj).toString(),
},
};
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const browser = typeof window !== 'undefined';
const querystring = require('querystring');
const transport = browser ? require('./browser') : require('./node');
const transport = require(typeof window !== 'undefined' ? './browser' : './node');

/**
* Snekfetch
Expand Down Expand Up @@ -32,7 +30,11 @@ class Snekfetch extends transport.Extension {
*/
constructor(method, url, opts = {}) {
super();
this.options = Object.assign({ version: 1, qs: querystring, followRedirects: true }, opts);
this.options = Object.assign({
version: 1,
qs: transport.querystring,
followRedirects: true,
}, opts);
this.request = transport.buildRequest.call(this, method, url, opts);
if (opts.headers)
this.set(opts.headers);
Expand Down
1 change: 1 addition & 0 deletions src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,5 @@ module.exports = {
STATUS_CODES: http.STATUS_CODES,
FormData,
Extension: Stream.Readable,
querystring: require('querystring'),
};
9 changes: 0 additions & 9 deletions src/qs_mock.js

This file was deleted.

11 changes: 0 additions & 11 deletions test/browser/qs_mock.test.js

This file was deleted.

5 changes: 0 additions & 5 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@ module.exports = {
plugins: [
new UglifyJSPlugin(),
],
resolve: {
alias: {
querystring: require.resolve('./src/qs_mock'),
},
},
};

0 comments on commit eb86f67

Please sign in to comment.