node-superfetch
Advanced tools
Comparing version 0.1.5 to 0.1.6
16
index.js
const fetch = require('node-fetch'); | ||
const FormData = require('form-data'); | ||
const querystring = require('querystring'); | ||
const { URL } = require('url'); | ||
const { METHODS } = require('http'); | ||
@@ -10,6 +10,5 @@ const { version } = require('./package'); | ||
if (!options.url) throw new Error('The "url" option is required.'); | ||
this.url = options.url; | ||
this.url = new URL(options.url); | ||
this.method = options.method ? options.method.toUpperCase() : 'GET'; | ||
if (!METHODS.includes(this.method)) throw new Error(`The method "${this.method}" is not supported.`); | ||
this.queryParams = options.query || {}; | ||
this.headers = options.headers || {}; | ||
@@ -21,4 +20,3 @@ this.body = options.body || null; | ||
async _request() { | ||
const queryParams = querystring.stringify(this.queryParams); | ||
const response = await fetch(`${this.url}${queryParams ? `?${queryParams}` : ''}`, { | ||
const response = await fetch(this.url.toString(), { | ||
method: this.method, | ||
@@ -78,6 +76,6 @@ headers: this.headers, | ||
query(queryOrName, value) { | ||
if (typeof queryOrName === 'object' && !value) { | ||
for (const [param, val] of Object.entries(queryOrName)) this.queryParams[param] = val; | ||
if (typeof queryOrName === 'object') { | ||
for (const [param, val] of Object.entries(queryOrName)) this.url.searchParams.append(param, val); | ||
} else if (typeof queryOrName === 'string' && value) { | ||
this.queryParams[queryOrName] = value; | ||
this.url.searchParams.append(queryOrName, value); | ||
} else { | ||
@@ -90,3 +88,3 @@ throw new TypeError('The "query" parameter must be either an object or a query field.'); | ||
set(headersOrName, value) { | ||
if (typeof headersOrName === 'object' && !value) { | ||
if (typeof headersOrName === 'object') { | ||
for (const [header, val] of Object.entries(headersOrName)) this.headers[header] = val; | ||
@@ -93,0 +91,0 @@ } else if (typeof headersOrName === 'string' && value) { |
{ | ||
"name": "node-superfetch", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "A wrapper for node-fetch that makes it appear like superagent.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
6890
121