node-superfetch
Advanced tools
Comparing version 0.1.1 to 0.1.2
35
index.js
const fetch = require('node-fetch'); | ||
const FormData = require('form-data'); | ||
const querystring = require('querystring'); | ||
const { METHODS } = require('http'); | ||
const { version } = require('./package'); | ||
@@ -73,5 +75,9 @@ class Request { | ||
query(queryOrName, value) { | ||
if (typeof queryOrName === 'object' && !value) this.queryParams = { ...this.queryParams, ...queryOrName }; | ||
else if (typeof queryOrName === 'string' && value) this.queryParams[queryOrName] = value; | ||
else throw new TypeError('The "query" parameter must be either an object or a query field.'); | ||
if (typeof queryOrName === 'object' && !value) { | ||
for (const [param, val] of Object.entries(queryOrName)) this.queryParams[param] = val; | ||
} else if (typeof queryOrName === 'string' && value) { | ||
this.queryParams[queryOrName] = value; | ||
} else { | ||
throw new TypeError('The "query" parameter must be either an object or a query field.'); | ||
} | ||
return this; | ||
@@ -81,9 +87,24 @@ } | ||
set(headersOrName, value) { | ||
if (typeof headersOrName === 'object' && !value) this.headers = { ...this.headers, ...headersOrName }; | ||
else if (typeof headersOrName === 'string' && value) this.headers[headersOrName] = value; | ||
else throw new TypeError('The "headers" parameter must be either an object or a header field.'); | ||
if (typeof headersOrName === 'object' && !value) { | ||
for (const [header, val] of Object.entries(headersOrName)) this.headers[header] = val; | ||
} else if (typeof headersOrName === 'string' && value) { | ||
this.headers[headersOrName] = value; | ||
} else { | ||
throw new TypeError('The "headers" parameter must be either an object or a header field.'); | ||
} | ||
return this; | ||
} | ||
attach(...args) { | ||
if (!this.body || !(this.body instanceof FormData)) this.body = new FormData(); | ||
if (typeof args[0] === 'object') { | ||
for (const [key, val] of Object.entries(args[0])) this.attach(key, val); | ||
} else { | ||
this.body.append(...args); | ||
} | ||
return this; | ||
} | ||
send(body, raw = false) { | ||
if (body instanceof FormData) raw = true; | ||
if (!raw && body !== null && typeof body === 'object') { | ||
@@ -114,2 +135,4 @@ const header = this.headers['content-type']; | ||
Request.version = version; | ||
module.exports = Request; |
{ | ||
"name": "node-superfetch", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A wrapper for node-fetch that makes it appear like superagent.", | ||
@@ -33,2 +33,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"form-data": "^2.3.2", | ||
"node-fetch": "^2.1.2" | ||
@@ -35,0 +36,0 @@ }, |
@@ -26,6 +26,5 @@ # node-superfetch | ||
methods, as well as functions like `query` (sets query parameters), `set` (sets | ||
headers), `send` (for adding POST data and such), and `redirects` (for setting | ||
the allowed number of redirects). `end` is also supported for callbacks. It | ||
_does not_ implement anything related to formdata (but you can probably attach | ||
it like you would with node-fetch). | ||
headers), `send` (for adding POST data and such), `attach` (for sending | ||
FormData), and `redirects` (for setting the allowed number of redirects). `end` | ||
is also supported for callbacks. | ||
@@ -32,0 +31,0 @@ ## Disclaimer for Contributors |
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
7364
122
2
36
+ Addedform-data@^2.3.2
+ 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)