Comparing version 3.7.0 to 4.0.0
121
lib/phin.js
const {URL} = require('url') | ||
const centra = require('centra') | ||
const axios = require('axios') | ||
const FormData = require('form-data') | ||
@@ -13,11 +14,8 @@ /** | ||
* @property {Object} [headers={}] - Request headers | ||
* @property {Object} [core={}] - Custom core HTTP options | ||
* @property {Object} [axiosOpts={}] - Custom core HTTP options for Axios | ||
* @property {string} [parse=none] - Response parsing. Errors will be given if the response can't be parsed. 'none' returns body as a `Buffer`, 'json' attempts to parse the body as JSON, and 'string' attempts to parse the body as a string | ||
* @property {boolean} [followRedirects=false] - Enable HTTP redirect following | ||
* @property {boolean} [followRedirects=false] - Enable HTTP redirect following. This option exists for compatibility and sets axiosOpts.maxRedirects. Using axiosOpts.maxRedirects is recommended. | ||
* @property {boolean} [compression=false] - Enable compression for request | ||
* @property {boolean} [stream=false] - Enable streaming of response. (Removes body property) | ||
* @property {boolean} [compression=false] - Enable compression for request | ||
* @property {?number} [timeout=null] - Request timeout in milliseconds | ||
* @property {string} [hostname=autodetect] - URL hostname | ||
* @property {Number} [port=autodetect] - URL port | ||
* @property {string} [path=autodetect] - URL path | ||
*/ | ||
@@ -32,60 +30,85 @@ | ||
/** | ||
* Sends an HTTP request | ||
* @param {phinOptions|string} options - phin options object (or string for auto-detection) | ||
* @returns {Promise<http.serverResponse>} - phin-adapted response object | ||
*/ | ||
const phin = async (opts) => { | ||
if (typeof(opts) !== 'string') { | ||
if (!opts.hasOwnProperty('url')) { | ||
throw new Error('Missing url option from options for request method.') | ||
const axiosify = (opts) => { | ||
if (typeof opts === 'string') { | ||
return { | ||
'url': opts | ||
} | ||
} | ||
const req = centra(typeof opts === 'object' ? opts.url : opts, opts.method || 'GET') | ||
let responseType = 'arraybuffer' | ||
if (opts.headers) req.header(opts.headers) | ||
if (opts.stream) req.stream() | ||
if (opts.timeout) req.timeout(opts.timeout) | ||
if (opts.data) req.body(opts.data) | ||
if (opts.form) req.body(opts.form, 'form') | ||
if (opts.compression) req.compress() | ||
if (opts.parse) { | ||
responseType = { | ||
'none': 'arraybuffer', | ||
'json': 'json', | ||
'string': 'text' | ||
}[opts.parse] | ||
} | ||
if (typeof opts.core === 'object') { | ||
Object.keys(opts.core).forEach((optName) => { | ||
req.option(optName, opts.core[optName]) | ||
}) | ||
if (opts.stream) { | ||
responseType = 'stream' | ||
} | ||
const res = await req.send() | ||
if (opts.hostname || opts.port || opts.path) { | ||
throw new Error('use of hostname, port, and path options is not supported') | ||
} | ||
if (res.headers.hasOwnProperty('location') && opts.followRedirects) { | ||
opts.url = (new URL(res.headers['location'], opts.url)).toString() | ||
const axiosOpts = { | ||
url: opts.url, | ||
method: opts.method, | ||
data: opts.data, | ||
headers: opts.headers, | ||
decompress: opts.compression, | ||
timeout: opts.timeout, | ||
responseType: responseType, | ||
transitional: { | ||
silentJSONParsing: false | ||
} | ||
} | ||
return await phin(opts) | ||
if (opts.followRedirects) { | ||
axiosOpts.maxRedirects = 10 | ||
} | ||
if (opts.stream) { | ||
res.stream = res | ||
if (opts.core) { | ||
throw new Error("core options no longer supported. see axiosOpts") | ||
} | ||
return res | ||
if (opts.form) { | ||
axiosOpts.data = new FormData() | ||
Object.keys(opts.form).forEach((fk) => { | ||
axiosOpts.data.append(fk, opts.form[fk]) | ||
}) | ||
} | ||
else { | ||
res.coreRes.body = res.body | ||
if (opts.parse) { | ||
if (opts.parse === 'json') { | ||
res.coreRes.body = await res.json() | ||
return res.coreRes | ||
} | ||
else if (opts.parse === 'string') { | ||
res.coreRes.body = res.coreRes.body.toString() | ||
if (opts.axiosOpts) { | ||
Object.keys(opts.axiosOpts).forEach((ck) => { | ||
axiosOpts[ck] = opts.axiosOpts[ck] | ||
}) | ||
} | ||
return res.coreRes | ||
} | ||
return axiosOpts | ||
} | ||
/** | ||
* Sends an HTTP request | ||
* @param {phinOptions|string} options - phin options object (or string for auto-detection) | ||
* @returns {Promise<http.serverResponse>} - phin-adapted response object | ||
*/ | ||
const phin = async (opts) => { | ||
if (typeof(opts) !== 'string') { | ||
if (!opts.hasOwnProperty('url')) { | ||
throw new Error('Missing url option from options for request method.') | ||
} | ||
return res.coreRes | ||
} | ||
const aopts = axiosify(opts) | ||
const res = await axios(aopts) | ||
res.body = res.data | ||
res.statusCode = res.status | ||
res.stream = res.data | ||
return res | ||
} | ||
@@ -119,2 +142,4 @@ | ||
phin.axiosify = axiosify | ||
module.exports = phin |
{ | ||
"name": "phin", | ||
"version": "3.7.0", | ||
"description": "The ultra-lightweight Node.js HTTP client", | ||
"version": "4.0.0", | ||
"description": "The lightweight Node.js HTTP client", | ||
"main": "lib/phin.js", | ||
@@ -9,4 +9,3 @@ "types": "types.d.ts", | ||
"test": "node ./tests/test.js", | ||
"prepublishOnly": "npm test", | ||
"gendocs": "rm -r docs || true && jsdoc -R README.md -d ./docs lib/phin.js" | ||
"prepublishOnly": "npm test" | ||
}, | ||
@@ -22,3 +21,2 @@ "repository": { | ||
"fetch", | ||
"ajax", | ||
"url", | ||
@@ -33,6 +31,2 @@ "uri" | ||
"homepage": "https://github.com/ethanent/phin", | ||
"devDependencies": { | ||
"jsdoc": "^3.5.5", | ||
"whew": "^1.1.3" | ||
}, | ||
"files": [ | ||
@@ -42,8 +36,6 @@ "lib/phin.js", | ||
], | ||
"engines": { | ||
"node": ">= 8" | ||
}, | ||
"dependencies": { | ||
"centra": "^2.6.0" | ||
"axios": "^1.6.8", | ||
"form-data": "^4.0.0" | ||
} | ||
} |
@@ -7,5 +7,8 @@ <p align="center" style="text-align: center"><img src="https://raw.githubusercontent.com/ethanent/phin/master/media/phin-textIncluded.png" width="250" alt="phin logo"/></p> | ||
[Full documentation](https://ethanent.github.io/phin/global.html) | [GitHub](https://github.com/ethanent/phin) | [NPM](https://www.npmjs.com/package/phin) | ||
## Deprecation warning | ||
Some version of this package are deprecated and should not be used. Ensure you are using a non-deprecated version. | ||
See [#91](https://github.com/ethanent/phin/issues/91) for more information. | ||
## Simple Usage | ||
@@ -16,3 +19,3 @@ | ||
const res = await p('https://ethanent.me') | ||
const res = await p('https://example.com') | ||
@@ -44,3 +47,3 @@ console.log(res.body) | ||
await p({ | ||
url: 'https://ethanent.me', | ||
url: 'https://example.com', | ||
method: 'POST', | ||
@@ -58,3 +61,3 @@ data: { | ||
p('https://ethanent.me', (err, res) => { | ||
p('https://example.com', (err, res) => { | ||
if (!err) console.log(res.body) | ||
@@ -70,3 +73,3 @@ }) | ||
const res = await p({ | ||
'url': 'https://ethanent.me/name', | ||
'url': 'https://example.com/', | ||
'parse': 'json' | ||
@@ -89,3 +92,3 @@ }) | ||
const res = await ppostjson('https://ethanent.me/somejson') | ||
const res = await ppostjson('https://example.com/somejson') | ||
// ^ An options object could also be used here to set other options. | ||
@@ -96,11 +99,11 @@ | ||
### Custom Core HTTP Options | ||
### Custom Axios HTTP Options | ||
Phin allows you to set [core HTTP options](https://nodejs.org/api/http.html#http_http_request_url_options_callback). | ||
Phin allows you to set [Axios HTTP options](https://github.com/axios/axios?tab=readme-ov-file#request-config). | ||
```js | ||
await p({ | ||
'url': 'https://ethanent.me/name', | ||
'core': { | ||
'agent': myAgent // Assuming you'd already created myAgent earlier. | ||
'url': 'https://example.com/name', | ||
'axiosOpts': { | ||
httpAgent: myAgent // Assuming you'd already created myAgent earlier. | ||
} | ||
@@ -110,10 +113,2 @@ }) | ||
## Full Documentation | ||
There's a lot more which can be done with the Phin library. | ||
See [the Phin documentation](https://ethanent.github.io/phin/global.html). | ||
## Phin vs. the Competition | ||
@@ -120,0 +115,0 @@ |
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
0
214
13047
2
122
+ Addedaxios@^1.6.8
+ Addedform-data@^4.0.0
+ Addedasynckit@0.4.0(transitive)
+ Addedaxios@1.7.7(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedform-data@4.0.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedproxy-from-env@1.1.0(transitive)
- Removedcentra@^2.6.0
- Removedcentra@2.7.0(transitive)