@dnpr/make-request
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -9,3 +9,15 @@ 'use strict' | ||
/** Wrappers for makeRequest(). */ | ||
/** | ||
* @typedef Response | ||
* @property {number} statusCode | ||
* @property {Buffer} responseBuffer | ||
*/ | ||
/** | ||
* Make HTTP request. | ||
* @param {Object} options Options for NodeJS http.request(). | ||
* @param {string | Buffer} payload Request body. | ||
* @returns {Promise.<Response>} Response object containing statusCode and | ||
* responseBuffer. | ||
*/ | ||
function makeHTTPRequest(options, payload) { | ||
@@ -15,2 +27,9 @@ return makeRequest(options, payload, false) | ||
/** | ||
* Make HTTPS request. | ||
* @param {Object} options Options for NodeJS https.request(). | ||
* @param {string | Buffer} payload Request body. | ||
* @returns {Promise.<Response>} Response object containing statusCode and | ||
* responseBuffer. | ||
*/ | ||
function makeHTTPSRequest(options, payload) { | ||
@@ -21,14 +40,9 @@ return makeRequest(options, payload, true) | ||
/** | ||
* @typedef Response | ||
* @property {number} statusCode | ||
* @property {Buffer} responseBuffer | ||
* Make a HTTP or HTTPS request. | ||
* @param {Object} options Options for NodeJS http/https.request(). | ||
* @param {string | Buffer} payload Request body. | ||
* @param {boolean} isHttps Whether this is a HTTPS request. | ||
* @returns {Promise.<Response>} Response object containing statusCode and | ||
* responseBuffer. | ||
*/ | ||
/** | ||
* Send a http request and get the response. | ||
* @param {*} options - Options passed to http/https.request(). | ||
* @param {*} payload - Request body. | ||
* @param {boolean} isHttps - Whether to use HTTPS. | ||
* @returns {Promise.<Response>} Response object. | ||
*/ | ||
function makeRequest(options, payload, isHttps) { | ||
@@ -63,3 +77,4 @@ | ||
* Collect response data. | ||
* @param {http.ServerResponse | https.ServerResponse} res - A Node.js HTTP/HTTPS response object. | ||
* @param {http.ServerResponse | https.ServerResponse} res - A Node.js | ||
* HTTP/HTTPS response object. | ||
* @param {Function} callback - Callback function. | ||
@@ -70,4 +85,4 @@ */ | ||
/** | ||
* zlib accepts Buffer, not string, so we need to collect chucks in an array, | ||
* and use Buffer.concat() to combine them. | ||
* zlib accepts Buffer, not string, so we need to collect chucks in an | ||
* array, and use Buffer.concat() to combine them. | ||
*/ | ||
@@ -74,0 +89,0 @@ let data = [] |
{ | ||
"name": "@dnpr/make-request", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A simple HTTP/HTTPS agent for Node.js.", | ||
@@ -14,7 +14,15 @@ "main": "lib/index.js", | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/dnpr/make-request.git" | ||
}, | ||
"author": "dragonman225", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/dnpr/make-request/issues" | ||
}, | ||
"homepage": "https://github.com/dnpr/make-request", | ||
"devDependencies": { | ||
"eslint": "^6.0.1" | ||
"eslint": "^6.6.0" | ||
} | ||
} |
# make-request | ||
> A simple HTTP/HTTPS agent for Node.js. | ||
A simple HTTP / HTTPS agent for Node.js. | ||
@@ -14,3 +14,3 @@ ## Documentation | ||
```bash | ||
npm i @dnpr/make-request | ||
npm install @dnpr/make-request | ||
``` | ||
@@ -21,3 +21,35 @@ | ||
```javascript | ||
const { makeHTTPSRequest, makeHTTPRequest } = require('@dnpr/make-request') | ||
const { makeHTTPSRequest } = require('@dnpr/make-request') | ||
const BASE_URL = 'httpbin.org' | ||
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36' | ||
const headers = { | ||
'accept': '*/*', | ||
'accept-encoding': 'gzip, deflate, br', | ||
'accept-language': 'en-US,en;q=0.9', | ||
'user-agent': USER_AGENT | ||
} | ||
/** The same as options in NodeJS https.request() */ | ||
const httpsAgentOptions = { | ||
hostname: BASE_URL, | ||
port: 443, | ||
path: '/get', | ||
method: 'GET', | ||
headers | ||
} | ||
async function main() { | ||
try { | ||
let res = await makeHTTPSRequest(httpsAgentOptions) | ||
let resParsed = { | ||
statusCode: res.statusCode, | ||
data: JSON.parse(res.responseBuffer) | ||
} | ||
console.log(resParsed) | ||
} catch { | ||
console.error(error) | ||
} | ||
} | ||
``` | ||
@@ -27,15 +59,13 @@ | ||
Clone the repository. | ||
Clone the repository and setup. | ||
```bash | ||
git clone https://github.com/dnpr/make-request.git | ||
npm install # or any package manager | ||
``` | ||
This project uses `pnpm` as package manager. | ||
Run tests. | ||
```bash | ||
npm i -g pnpm | ||
pnpm install | ||
``` | ||
However, you can still use `npm install`. | ||
npm test | ||
``` |
const { makeHTTPSRequest, makeHTTPRequest } = require('../lib') | ||
const BASE_URL = 'httpbin.org' | ||
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36' | ||
const headers = { | ||
'accept': '*/*', | ||
'accept-encoding': 'gzip, deflate, br', | ||
'accept-language': 'en-US,en;q=0.9', | ||
'user-agent': USER_AGENT | ||
} | ||
const httpsAgentOptions = { | ||
hostname: 'httpbin.org', | ||
hostname: BASE_URL, | ||
port: 443, | ||
path: '/get', | ||
method: 'GET', | ||
headers: { | ||
'accept': '*/*', | ||
'accept-encoding': 'gzip, deflate, br', | ||
'accept-language': 'en-US,en;q=0.9', | ||
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36' | ||
} | ||
headers | ||
} | ||
const httpAgentOptions = { | ||
hostname: 'httpbin.org', | ||
hostname: BASE_URL, | ||
port: 80, | ||
path: '/get', | ||
method: 'GET', | ||
headers: { | ||
'accept': '*/*', | ||
'accept-encoding': 'gzip, deflate, br', | ||
'accept-language': 'en-US,en;q=0.9', | ||
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36' | ||
} | ||
headers | ||
} | ||
main() | ||
async function main() { | ||
@@ -51,2 +49,4 @@ try { | ||
} | ||
} | ||
} | ||
main() |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
178
0
1
68
0
7290
6