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

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Jan 27, 2018
1 parent f41be40 commit f85d9d6
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 56 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Snekfetch is a fast, efficient, and user-friendly library for making HTTP requests.

The API was inspired by superagent, however it is much smaller and faster.
In fact, in browser, it is a mere 4.8kb.
In fact, in browser, it is a mere 4.4kb.

Documentation is available at https://snekfetch.js.org/

Expand Down Expand Up @@ -38,7 +38,7 @@ Available for browser as UMD from [unpkg][unpkg-link]
[npm]: https://npmjs.org/package/snekfetch
[large-badge]: https://nodei.co/npm/snekfetch.png?downloads=true&downloadRank=true&stars=true
[stats-link]: https://nodei.co/npm/snekfetch/
[version-badge]: http://versionbadg.es/devsnek/snekfetch.svg
[version-badge]: https://versionbadge.now.sh/snekfetch.svg
[download-badge]: https://img.shields.io/npm/dt/snekfetch.svg?maxAge=3600
[build-badge]: https://api.travis-ci.org/devsnek/snekfetch.svg?branch=master
[build-link]: https://travis-ci.org/devsnek/snekfetch
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "snekfetch",
"version": "3.6.1",
"version": "3.6.2",
"main": "index.js",
"module": "esm.mjs",
"unpkg": "browser.js",
Expand Down
23 changes: 1 addition & 22 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const browser = typeof window !== 'undefined';
const querystring = require('querystring');
const Package = require('../package');
const transport = browser ? require('./browser') : require('./node');

/**
Expand Down Expand Up @@ -48,7 +47,6 @@ class Snekfetch extends transport.Extension {
* @returns {Snekfetch} This request
*/
query(name, value) {
this._checkModify();
if (!this.request.query)
this.request.query = {};
if (name !== null && typeof name === 'object') {
Expand All @@ -68,7 +66,6 @@ class Snekfetch extends transport.Extension {
* @returns {Snekfetch} This request
*/
set(name, value) {
this._checkModify();
if (name !== null && typeof name === 'object') {
for (const key of Object.keys(name))
this.set(key, name[key]);
Expand All @@ -87,8 +84,7 @@ class Snekfetch extends transport.Extension {
* @returns {Snekfetch} This request
*/
attach(...args) {
this._checkModify();
const form = this._getFormData();
const form = this.data instanceof transport.FormData ? this.data : this.data = new transport.FormData();
if (typeof args[0] === 'object') {
for (const [k, v] of Object.entries(args[0]))
this.attach(k, v);
Expand All @@ -105,7 +101,6 @@ class Snekfetch extends transport.Extension {
* @returns {Snekfetch} This request
*/
send(data) {
this._checkModify();
if (data instanceof transport.FormData || transport.shouldSendRaw(data)) {
this.data = data;
} else if (data !== null && typeof data === 'object') {
Expand Down Expand Up @@ -219,18 +214,9 @@ class Snekfetch extends transport.Extension {
);
}

_getFormData() {
if (!(this.data instanceof transport.FormData))
this.data = new transport.FormData();

return this.data;
}

_finalizeRequest() {
if (!this.request)
return;
if (!this.request.getHeader('user-agent'))
this.set('User-Agent', `snekfetch/${Snekfetch.version} (${Package.homepage})`);

if (this.request.method !== 'HEAD')
this.set('Accept-Encoding', 'gzip, deflate');
Expand All @@ -242,15 +228,8 @@ class Snekfetch extends transport.Extension {
this.request.path = `${path}?${this.options.qs.stringify(this.request.query)}${query ? `&${query}` : ''}`;
}
}

_checkModify() {
if (this.response)
throw new Error('Cannot modify request after it has been sent!');
}
}

Snekfetch.version = Package.version;

/**
* Create a ((THIS)) request
* @dynamic this.METHODS
Expand Down
15 changes: 8 additions & 7 deletions src/qs_mock.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
exports.parse = (str) => {
const parsed = {};
for (const [k, v] of new Window.URLSearchParams(str).entries())
parsed[k] = v;
return parsed;
exports = {
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(),
};

exports.stringify = (obj) => new window.URLSearchParams(obj).toString();
4 changes: 3 additions & 1 deletion test/interop.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ function makeProxy(fetch) {
}

exports.Snekfetch = makeProxy(require('../'));
exports.SnekfetchSync = makeProxy(require('../sync'));
try {
exports.SnekfetchSync = makeProxy(require('../sync'));
} catch (err) {} // eslint-disable-line no-empty

exports.TestRoot = global.HTTP_VERSION === 2 ?
'https://nghttp2.org/httpbin' :
Expand Down
4 changes: 2 additions & 2 deletions test/node/sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const { SnekfetchSync, TestRoot } = require('../interop');

test('sync get', () => {
test('sync get', SnekfetchSync && (() => {
const res = SnekfetchSync.get(`${TestRoot}/get`).end();
expect(res.body).not.toBeUndefined();
});
}));
13 changes: 0 additions & 13 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,6 @@ module.exports = {
plugins: [
new UglifyJSPlugin(),
],
module: {
rules: [
{
test: require.resolve('./package.json'),
use: {
loader: 'json-filter-loader',
options: {
used: ['version', 'homepage'],
},
},
},
],
},
resolve: {
alias: {
querystring: require.resolve('./src/qs_mock'),
Expand Down
8 changes: 0 additions & 8 deletions webpack.supplemental.js

This file was deleted.

0 comments on commit f85d9d6

Please sign in to comment.