sync-request-curl
Advanced tools
Comparing version 2.1.4 to 2.1.5
import { HttpVerb, Options, Response } from './types'; | ||
/** | ||
* Performs an HTTP request using cURL with the specified parameters | ||
* Performs an HTTP request using cURL with the specified parameters. | ||
* | ||
* @param {HttpVerb} method - The HTTP method for the request (e.g., 'GET', 'POST'). | ||
* @param {string} url - The URL to make the request to. | ||
* @param {Options} [options={}] - An object to configure the request. | ||
* @returns {Response} - HTTP response consisting of status code, headers, and body. | ||
* @param {HttpVerb} method - The HTTP method for the request (e.g., 'GET', 'POST') | ||
* @param {string} url - The URL to make the request to | ||
* @param {Options} [options={}] - An object to configure the request | ||
* @returns {Response} - HTTP response consisting of status code, headers, and body | ||
*/ | ||
declare const request: (method: HttpVerb, url: string, options?: Options) => Response; | ||
export default request; |
@@ -6,9 +6,28 @@ "use strict"; | ||
/** | ||
* Create a libcurl Easy object with default configurations | ||
* | ||
* @param {HttpVerb} method - The HTTP method (e.g., 'GET', 'POST', 'PUT') | ||
* @param {Options} options - configuration options for the request. | ||
* @returns {Easy} an initialized libcurl Easy object with default options | ||
* ``` | ||
*/ | ||
const createCurlObjectWithDefaults = (method, options) => { | ||
var _a, _b; | ||
const curl = new node_libcurl_1.Easy(); | ||
curl.setOpt(node_libcurl_1.Curl.option.CUSTOMREQUEST, method); | ||
curl.setOpt(node_libcurl_1.Curl.option.TIMEOUT_MS, (_a = options.timeout) !== null && _a !== void 0 ? _a : 0); | ||
curl.setOpt(node_libcurl_1.Curl.option.FOLLOWLOCATION, options.followRedirects === undefined || | ||
options.followRedirects); | ||
curl.setOpt(node_libcurl_1.Curl.option.MAXREDIRS, (_b = options.maxRedirects) !== null && _b !== void 0 ? _b : -1); | ||
curl.setOpt(node_libcurl_1.Curl.option.SSL_VERIFYPEER, !options.insecure); | ||
return curl; | ||
}; | ||
/** | ||
* Handles query string parameters in a URL, modifies the URL if necessary, | ||
* and sets it as the CURLOPT_URL option in the given cURL Easy object. | ||
* | ||
* @param {Easy} curl - The cURL easy handle. | ||
* @param {string} url - The URL to handle query string parameters for. | ||
* @param {Easy} curl - The cURL easy handle | ||
* @param {string} url - The URL to handle query string parameters for | ||
* @param {Object.<string, any>} qs - query string parameters for the request | ||
* @returns {string} The modified URL with the updated query string parameters. | ||
* @returns {string} The modified URL with the updated query string parameters | ||
*/ | ||
@@ -24,4 +43,4 @@ const handleQueryString = (curl, url, qs) => { | ||
* | ||
* @param {Easy} curl - The cURL easy handle. | ||
* @param {string[]} returnedHeaderArray - array for returned header lines. | ||
* @param {Easy} curl - The cURL easy handle | ||
* @param {string[]} returnedHeaderArray - array for returned header lines | ||
*/ | ||
@@ -39,6 +58,6 @@ const handleOutgoingHeaders = (curl, returnedHeaderArray) => { | ||
* | ||
* @param {Easy} curl - The cURL easy handle. | ||
* @param {Options} options - Options for configuring the request. | ||
* @param {{ body: Buffer }} buffer - wrapped buffer for the returned body. | ||
* @param {string[]} httpHeaders - HTTP headers for the request. | ||
* @param {Easy} curl - The cURL easy handle | ||
* @param {Options} options - Options for configuring the request | ||
* @param {{ body: Buffer }} buffer - wrapped buffer for the returned body | ||
* @param {string[]} httpHeaders - HTTP headers for the request | ||
*/ | ||
@@ -62,20 +81,11 @@ const handleBody = (curl, options, buffer, httpHeaders) => { | ||
/** | ||
* Performs an HTTP request using cURL with the specified parameters | ||
* Performs an HTTP request using cURL with the specified parameters. | ||
* | ||
* @param {HttpVerb} method - The HTTP method for the request (e.g., 'GET', 'POST'). | ||
* @param {string} url - The URL to make the request to. | ||
* @param {Options} [options={}] - An object to configure the request. | ||
* @returns {Response} - HTTP response consisting of status code, headers, and body. | ||
* @param {HttpVerb} method - The HTTP method for the request (e.g., 'GET', 'POST') | ||
* @param {string} url - The URL to make the request to | ||
* @param {Options} [options={}] - An object to configure the request | ||
* @returns {Response} - HTTP response consisting of status code, headers, and body | ||
*/ | ||
const request = (method, url, options = {}) => { | ||
var _a, _b; | ||
// Initialing curl object with custom options | ||
const curl = new node_libcurl_1.Easy(); | ||
curl.setOpt(node_libcurl_1.Curl.option.CUSTOMREQUEST, method); | ||
curl.setOpt(node_libcurl_1.Curl.option.TIMEOUT_MS, (_a = options.timeout) !== null && _a !== void 0 ? _a : 0); | ||
curl.setOpt(node_libcurl_1.Curl.option.FOLLOWLOCATION, options.followRedirects === undefined || | ||
options.followRedirects); | ||
curl.setOpt(node_libcurl_1.Curl.option.MAXREDIRS, (_b = options.maxRedirects) !== null && _b !== void 0 ? _b : -1); | ||
curl.setOpt(node_libcurl_1.Curl.option.SSL_VERIFYPEER, !options.insecure); | ||
// Query string parameters | ||
const curl = createCurlObjectWithDefaults(method, options); | ||
handleQueryString(curl, url, options.qs); | ||
@@ -95,10 +105,11 @@ // Headers (both incoming and outgoing) | ||
const code = curl.perform(); | ||
(0, utils_1.checkValidCurlCode)(code, method, url, options); | ||
(0, utils_1.checkValidCurlCode)(code, { method, url, options }); | ||
// Creating return object | ||
const statusCode = curl.getInfo('RESPONSE_CODE').data; | ||
const headers = (0, utils_1.parseReturnedHeaders)(returnedHeaderArray); | ||
const body = bufferWrap.body; | ||
const { body } = bufferWrap; | ||
/** | ||
* Get the body of a response with an optional encoding. | ||
* @throws {Error} if the status code is >= 300. | ||
* | ||
* @throws {Error} if the status code is >= 300 | ||
* @returns {Buffer | string} buffer body by default, string body with encoding | ||
@@ -105,0 +116,0 @@ */ |
@@ -5,2 +5,7 @@ /// <reference types="node" /> | ||
import { HttpVerb, Options } from './types'; | ||
interface RequestInputs { | ||
method: HttpVerb; | ||
url: string; | ||
options: Options; | ||
} | ||
/** | ||
@@ -44,3 +49,3 @@ * Handles query string parameters in a URL by modifying or appending them | ||
*/ | ||
export declare const checkValidCurlCode: (code: CurlCode, method: HttpVerb, url: string, options: Options) => void; | ||
export declare const checkValidCurlCode: (code: CurlCode, requestInputs: RequestInputs) => void; | ||
/** | ||
@@ -54,1 +59,2 @@ * Checks the status code and body of an HTTP response | ||
export declare const checkGetBodyStatus: (statusCode: number, body: Buffer) => void; | ||
export {}; |
@@ -20,3 +20,3 @@ "use strict"; | ||
const urlObj = new URL(url); | ||
Object.entries(qs).forEach(([key, value]) => { | ||
for (const [key, value] of Object.entries(qs)) { | ||
if (Array.isArray(value)) { | ||
@@ -32,3 +32,3 @@ urlObj.searchParams.delete(key); | ||
} | ||
}); | ||
} | ||
urlObj.search = urlObj.searchParams.toString(); | ||
@@ -77,3 +77,3 @@ return urlObj.href; | ||
*/ | ||
const checkValidCurlCode = (code, method, url, options) => { | ||
const checkValidCurlCode = (code, requestInputs) => { | ||
if (code !== node_libcurl_1.CurlCode.CURLE_OK) { | ||
@@ -86,5 +86,5 @@ throw new errors_1.CurlError(code, ` | ||
DEBUG: { | ||
method: "${method}", | ||
url: "${url}", | ||
options: ${JSON.stringify(options)} | ||
method: "${requestInputs.method}", | ||
url: "${requestInputs.url}", | ||
options: ${JSON.stringify(requestInputs.options)} | ||
} | ||
@@ -91,0 +91,0 @@ `); |
import { HttpVerb, Options, Response } from './types'; | ||
/** | ||
* Performs an HTTP request using cURL with the specified parameters | ||
* Performs an HTTP request using cURL with the specified parameters. | ||
* | ||
* @param {HttpVerb} method - The HTTP method for the request (e.g., 'GET', 'POST'). | ||
* @param {string} url - The URL to make the request to. | ||
* @param {Options} [options={}] - An object to configure the request. | ||
* @returns {Response} - HTTP response consisting of status code, headers, and body. | ||
* @param {HttpVerb} method - The HTTP method for the request (e.g., 'GET', 'POST') | ||
* @param {string} url - The URL to make the request to | ||
* @param {Options} [options={}] - An object to configure the request | ||
* @returns {Response} - HTTP response consisting of status code, headers, and body | ||
*/ | ||
declare const request: (method: HttpVerb, url: string, options?: Options) => Response; | ||
export default request; |
import { Curl, Easy } from 'node-libcurl'; | ||
import { checkGetBodyStatus, checkValidCurlCode, handleQs, parseReturnedHeaders, parseIncomingHeaders, } from './utils'; | ||
/** | ||
* Create a libcurl Easy object with default configurations | ||
* | ||
* @param {HttpVerb} method - The HTTP method (e.g., 'GET', 'POST', 'PUT') | ||
* @param {Options} options - configuration options for the request. | ||
* @returns {Easy} an initialized libcurl Easy object with default options | ||
* ``` | ||
*/ | ||
const createCurlObjectWithDefaults = (method, options) => { | ||
var _a, _b; | ||
const curl = new Easy(); | ||
curl.setOpt(Curl.option.CUSTOMREQUEST, method); | ||
curl.setOpt(Curl.option.TIMEOUT_MS, (_a = options.timeout) !== null && _a !== void 0 ? _a : 0); | ||
curl.setOpt(Curl.option.FOLLOWLOCATION, options.followRedirects === undefined || | ||
options.followRedirects); | ||
curl.setOpt(Curl.option.MAXREDIRS, (_b = options.maxRedirects) !== null && _b !== void 0 ? _b : -1); | ||
curl.setOpt(Curl.option.SSL_VERIFYPEER, !options.insecure); | ||
return curl; | ||
}; | ||
/** | ||
* Handles query string parameters in a URL, modifies the URL if necessary, | ||
* and sets it as the CURLOPT_URL option in the given cURL Easy object. | ||
* | ||
* @param {Easy} curl - The cURL easy handle. | ||
* @param {string} url - The URL to handle query string parameters for. | ||
* @param {Easy} curl - The cURL easy handle | ||
* @param {string} url - The URL to handle query string parameters for | ||
* @param {Object.<string, any>} qs - query string parameters for the request | ||
* @returns {string} The modified URL with the updated query string parameters. | ||
* @returns {string} The modified URL with the updated query string parameters | ||
*/ | ||
@@ -21,4 +40,4 @@ const handleQueryString = (curl, url, qs) => { | ||
* | ||
* @param {Easy} curl - The cURL easy handle. | ||
* @param {string[]} returnedHeaderArray - array for returned header lines. | ||
* @param {Easy} curl - The cURL easy handle | ||
* @param {string[]} returnedHeaderArray - array for returned header lines | ||
*/ | ||
@@ -36,6 +55,6 @@ const handleOutgoingHeaders = (curl, returnedHeaderArray) => { | ||
* | ||
* @param {Easy} curl - The cURL easy handle. | ||
* @param {Options} options - Options for configuring the request. | ||
* @param {{ body: Buffer }} buffer - wrapped buffer for the returned body. | ||
* @param {string[]} httpHeaders - HTTP headers for the request. | ||
* @param {Easy} curl - The cURL easy handle | ||
* @param {Options} options - Options for configuring the request | ||
* @param {{ body: Buffer }} buffer - wrapped buffer for the returned body | ||
* @param {string[]} httpHeaders - HTTP headers for the request | ||
*/ | ||
@@ -59,20 +78,11 @@ const handleBody = (curl, options, buffer, httpHeaders) => { | ||
/** | ||
* Performs an HTTP request using cURL with the specified parameters | ||
* Performs an HTTP request using cURL with the specified parameters. | ||
* | ||
* @param {HttpVerb} method - The HTTP method for the request (e.g., 'GET', 'POST'). | ||
* @param {string} url - The URL to make the request to. | ||
* @param {Options} [options={}] - An object to configure the request. | ||
* @returns {Response} - HTTP response consisting of status code, headers, and body. | ||
* @param {HttpVerb} method - The HTTP method for the request (e.g., 'GET', 'POST') | ||
* @param {string} url - The URL to make the request to | ||
* @param {Options} [options={}] - An object to configure the request | ||
* @returns {Response} - HTTP response consisting of status code, headers, and body | ||
*/ | ||
const request = (method, url, options = {}) => { | ||
var _a, _b; | ||
// Initialing curl object with custom options | ||
const curl = new Easy(); | ||
curl.setOpt(Curl.option.CUSTOMREQUEST, method); | ||
curl.setOpt(Curl.option.TIMEOUT_MS, (_a = options.timeout) !== null && _a !== void 0 ? _a : 0); | ||
curl.setOpt(Curl.option.FOLLOWLOCATION, options.followRedirects === undefined || | ||
options.followRedirects); | ||
curl.setOpt(Curl.option.MAXREDIRS, (_b = options.maxRedirects) !== null && _b !== void 0 ? _b : -1); | ||
curl.setOpt(Curl.option.SSL_VERIFYPEER, !options.insecure); | ||
// Query string parameters | ||
const curl = createCurlObjectWithDefaults(method, options); | ||
handleQueryString(curl, url, options.qs); | ||
@@ -92,10 +102,11 @@ // Headers (both incoming and outgoing) | ||
const code = curl.perform(); | ||
checkValidCurlCode(code, method, url, options); | ||
checkValidCurlCode(code, { method, url, options }); | ||
// Creating return object | ||
const statusCode = curl.getInfo('RESPONSE_CODE').data; | ||
const headers = parseReturnedHeaders(returnedHeaderArray); | ||
const body = bufferWrap.body; | ||
const { body } = bufferWrap; | ||
/** | ||
* Get the body of a response with an optional encoding. | ||
* @throws {Error} if the status code is >= 300. | ||
* | ||
* @throws {Error} if the status code is >= 300 | ||
* @returns {Buffer | string} buffer body by default, string body with encoding | ||
@@ -102,0 +113,0 @@ */ |
@@ -5,2 +5,7 @@ /// <reference types="node" /> | ||
import { HttpVerb, Options } from './types'; | ||
interface RequestInputs { | ||
method: HttpVerb; | ||
url: string; | ||
options: Options; | ||
} | ||
/** | ||
@@ -44,3 +49,3 @@ * Handles query string parameters in a URL by modifying or appending them | ||
*/ | ||
export declare const checkValidCurlCode: (code: CurlCode, method: HttpVerb, url: string, options: Options) => void; | ||
export declare const checkValidCurlCode: (code: CurlCode, requestInputs: RequestInputs) => void; | ||
/** | ||
@@ -54,1 +59,2 @@ * Checks the status code and body of an HTTP response | ||
export declare const checkGetBodyStatus: (statusCode: number, body: Buffer) => void; | ||
export {}; |
@@ -17,3 +17,3 @@ import { CurlCode } from 'node-libcurl'; | ||
const urlObj = new URL(url); | ||
Object.entries(qs).forEach(([key, value]) => { | ||
for (const [key, value] of Object.entries(qs)) { | ||
if (Array.isArray(value)) { | ||
@@ -29,3 +29,3 @@ urlObj.searchParams.delete(key); | ||
} | ||
}); | ||
} | ||
urlObj.search = urlObj.searchParams.toString(); | ||
@@ -71,3 +71,3 @@ return urlObj.href; | ||
*/ | ||
export const checkValidCurlCode = (code, method, url, options) => { | ||
export const checkValidCurlCode = (code, requestInputs) => { | ||
if (code !== CurlCode.CURLE_OK) { | ||
@@ -80,5 +80,5 @@ throw new CurlError(code, ` | ||
DEBUG: { | ||
method: "${method}", | ||
url: "${url}", | ||
options: ${JSON.stringify(options)} | ||
method: "${requestInputs.method}", | ||
url: "${requestInputs.url}", | ||
options: ${JSON.stringify(requestInputs.options)} | ||
} | ||
@@ -85,0 +85,0 @@ `); |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "2.1.4", | ||
"version": "2.1.5", | ||
"files": [ | ||
@@ -21,2 +21,3 @@ "dist" | ||
"lint:fix": "eslint --fix './**/*.ts'", | ||
"tsc": "tsc --noEmit", | ||
"build": "rm -rf dist && npm run build:esm && npm run build:cjs", | ||
@@ -23,0 +24,0 @@ "build:esm": "tsc", |
@@ -25,3 +25,3 @@ <div align="center"> | ||
| ||
[![GitHub issues](https://img.shields.io/github/issues/nktnet1/sync-request-curl.svg?style=social)](https://github.com/nktnet1/sync-request-curl) | ||
[![GitHub issues](https://img.shields.io/github/issues/nktnet1/sync-request-curl.svg?style=social)](https://github.com/nktnet1/sync-request-curl/issues) | ||
@@ -36,3 +36,3 @@ [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nktnet1_sync-request-curl&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=nktnet1_sync-request-curl) | ||
| ||
[![GitHub stars](https://img.shields.io/github/stars/nktnet1/sync-request-curl.svg?style=social)](https://github.com/nktnet1/sync-request-curl) | ||
[![GitHub stars](https://img.shields.io/github/stars/nktnet1/sync-request-curl.svg?style=social)](https://github.com/nktnet1/sync-request-curl/stargazers) | ||
@@ -87,2 +87,4 @@ [![Downloads Total](https://badgen.net/npm/dt/sync-request-curl)](https://moiva.io/?npm=sync-request-curl) | ||
Try with [Replit](https://replit.com/@nktnet1/sync-request-curl-example#index.js). | ||
```typescript | ||
@@ -97,4 +99,2 @@ request(method, url, options); | ||
Try with [Replit](https://replit.com/@nktnet1/sync-request-curl-example#index.js). | ||
`GET` request without options | ||
@@ -156,6 +156,2 @@ | ||
See [sync-request](https://www.npmjs.com/package/sync-request) for the original documentation. See the [Errors](#25-errors) section for information on libcurl errors. | ||
Please note that this library only supports a subset of the original features which are summarised below. | ||
### 2.1. Method | ||
@@ -359,3 +355,3 @@ | ||
It is possible to check the curl code as follows: | ||
It is possible to check the cURL code as follows: | ||
@@ -467,6 +463,6 @@ <details closed> | ||
This library was developed mainly to improve performance with sending synchronous requests in NodeJS. | ||
See [sync-request](https://www.npmjs.com/package/sync-request) for the original documentation. Please note that **sync-request-curl** only supports a subset of the original features in sync-request and additional features through leveraging [node-libcurl](https://www.npmjs.com/package/node-libcurl). | ||
It was designed to work with UNIX-like systems for UNSW students enrolled in COMP1531 Software Engineering Fundamentals. | ||
**sync-request-curl** was developed to improve performance with sending synchronous requests in NodeJS. It is also free from the sync-request bug which leaves an orphaned sync-rpc process, resulting in a [leaked handle being detected in Jest](https://github.com/ForbesLindesay/sync-request/issues/129). | ||
It has been tested to be working on Arch & Debian Linux and is compatible with Windows/MacOS. | ||
**sync-request-curl** was designed to work with UNIX-like systems for UNSW students enrolled in [COMP1531 Software Engineering Fundamentals](https://webcms3.cse.unsw.edu.au/COMP1531/23T2/outline). It has been tested on Alpine, Arch, Debian and Ubuntu Linux and is compatible with Windows/MacOS. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
62438
701
462