Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sync-request-curl

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sync-request-curl - npm Package Compare versions

Comparing version 2.1.3 to 2.1.4

8

dist/cjs/request.d.ts
import { HttpVerb, Options, Response } from './types';
/**
* 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.
*/
declare const request: (method: HttpVerb, url: string, options?: Options) => Response;
export default request;

47

dist/cjs/request.js

@@ -5,2 +5,11 @@ "use strict";

const utils_1 = require("./utils");
/**
* 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 {Object.<string, any>} qs - query string parameters for the request
* @returns {string} The modified URL with the updated query string parameters.
*/
const handleQueryString = (curl, url, qs) => {

@@ -11,2 +20,9 @@ url = qs && Object.keys(qs).length ? (0, utils_1.handleQs)(url, qs) : url;

};
/**
* Sets up a callback function for the cURL Easy object to handle returned
* headers and populate the input array with header lines.
*
* @param {Easy} curl - The cURL easy handle.
* @param {string[]} returnedHeaderArray - array for returned header lines.
*/
const handleOutgoingHeaders = (curl, returnedHeaderArray) => {

@@ -18,2 +34,12 @@ curl.setOpt(node_libcurl_1.Curl.option.HEADERFUNCTION, (headerLine) => {

};
/**
* Prepares the request body and headers for a cURL request based on provided
* options. Also sets up a callback function for the cURL Easy object to handle
* returned body and populates the input buffet.
*
* @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.
*/
const handleBody = (curl, options, buffer, httpHeaders) => {

@@ -35,9 +61,19 @@ let payload = '';

};
/**
* 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.
*/
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, options.timeout || 0);
curl.setOpt(node_libcurl_1.Curl.option.FOLLOWLOCATION, options.followRedirects === undefined || options.followRedirects);
curl.setOpt(node_libcurl_1.Curl.option.MAXREDIRS, options.maxRedirects || -1);
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);

@@ -64,2 +100,7 @@ // Query string parameters

const body = bufferWrap.body;
/**
* Get the body of a response with an optional encoding.
* @throws {Error} if the status code is >= 300.
* @returns {Buffer | string} buffer body by default, string body with encoding
*/
const getBody = (encoding) => {

@@ -66,0 +107,0 @@ (0, utils_1.checkGetBodyStatus)(statusCode, body);

4

dist/cjs/types.d.ts

@@ -7,5 +7,3 @@ /// <reference types="node" />

export type BufferEncoding = 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex';
export interface SetEasyOptionCallback {
(curl: Easy, curlOption: CurlOption): void;
}
export type SetEasyOptionCallback = (curl: Easy, curlOption: CurlOption) => void;
export interface Options {

@@ -12,0 +10,0 @@ headers?: IncomingHttpHeaders;

@@ -5,8 +5,48 @@ /// <reference types="node" />

import { HttpVerb, Options } from './types';
/**
* Handles query string parameters in a URL by modifying or appending them
* based on the provided object.
*
* Arrays of primitives, e.g. { quizIds: [1,2,3] }, will be of the form:
* https://google.com.au/?quizIds%5B0%5D=0&quizIds%5B1%5D=1&quizIds%5B2%5D=2
* i.e. https://www.google.com.au/?quizIds[0]=0&quizIds[1]=1&quizIds[2]=2
*
* @param {string} url - The URL to handle query string parameters for.
* @param {Object.<string, any>} qs - query string parameters to modify or append.
* @returns {string} The modified URL with the updated query string parameters.
*/
export declare const handleQs: (url: string, qs: {
[key: string]: any;
}) => string;
/**
* Parses incoming HTTP headers to an array of formatted strings.
*
* @param {IncomingHttpHeaders} headers - The header object to parse.
* @returns {string[]} An array of formatted header strings.
*/
export declare const parseIncomingHeaders: (headers?: IncomingHttpHeaders) => string[];
/**
* Parses an array of header lines as IncomingHttpHeaders.
*
* @param {string[]} headerLines - An array of header lines to parse.
* @returns {IncomingHttpHeaders} An object containing parsed headers.
*/
export declare const parseReturnedHeaders: (headerLines: string[]) => IncomingHttpHeaders;
/**
* Checks if a CurlCode is valid and throws a CurlError if it's not.
*
* @param {CurlCode} code - The CurlCode to check.
* @param {HttpVerb} method - The HTTP method used in the request.
* @param {string} url - The URL of the request.
* @param {Options} options - The options used in the request.
* @throws {CurlError} if the CurlCode is not CURLE_OK.
*/
export declare const checkValidCurlCode: (code: CurlCode, method: HttpVerb, url: string, options: Options) => void;
/**
* Checks the status code and body of an HTTP response
*
* @param {number} statusCode - The status code of the HTTP response.
* @param {Buffer} body - The body of the HTTP response.
* @throws {Error} if the status code is >= 300.
*/
export declare const checkGetBodyStatus: (statusCode: number, body: Buffer) => void;

@@ -6,2 +6,14 @@ "use strict";

const errors_1 = require("./errors");
/**
* Handles query string parameters in a URL by modifying or appending them
* based on the provided object.
*
* Arrays of primitives, e.g. { quizIds: [1,2,3] }, will be of the form:
* https://google.com.au/?quizIds%5B0%5D=0&quizIds%5B1%5D=1&quizIds%5B2%5D=2
* i.e. https://www.google.com.au/?quizIds[0]=0&quizIds[1]=1&quizIds[2]=2
*
* @param {string} url - The URL to handle query string parameters for.
* @param {Object.<string, any>} qs - query string parameters to modify or append.
* @returns {string} The modified URL with the updated query string parameters.
*/
const handleQs = (url, qs) => {

@@ -25,2 +37,8 @@ const urlObj = new URL(url);

exports.handleQs = handleQs;
/**
* Parses incoming HTTP headers to an array of formatted strings.
*
* @param {IncomingHttpHeaders} headers - The header object to parse.
* @returns {string[]} An array of formatted header strings.
*/
const parseIncomingHeaders = (headers) => {

@@ -34,2 +52,8 @@ return headers

exports.parseIncomingHeaders = parseIncomingHeaders;
/**
* Parses an array of header lines as IncomingHttpHeaders.
*
* @param {string[]} headerLines - An array of header lines to parse.
* @returns {IncomingHttpHeaders} An object containing parsed headers.
*/
const parseReturnedHeaders = (headerLines) => {

@@ -45,2 +69,11 @@ return headerLines.reduce((acc, header) => {

exports.parseReturnedHeaders = parseReturnedHeaders;
/**
* Checks if a CurlCode is valid and throws a CurlError if it's not.
*
* @param {CurlCode} code - The CurlCode to check.
* @param {HttpVerb} method - The HTTP method used in the request.
* @param {string} url - The URL of the request.
* @param {Options} options - The options used in the request.
* @throws {CurlError} if the CurlCode is not CURLE_OK.
*/
const checkValidCurlCode = (code, method, url, options) => {

@@ -62,2 +95,9 @@ if (code !== node_libcurl_1.CurlCode.CURLE_OK) {

exports.checkValidCurlCode = checkValidCurlCode;
/**
* Checks the status code and body of an HTTP response
*
* @param {number} statusCode - The status code of the HTTP response.
* @param {Buffer} body - The body of the HTTP response.
* @throws {Error} if the status code is >= 300.
*/
const checkGetBodyStatus = (statusCode, body) => {

@@ -64,0 +104,0 @@ if (statusCode >= 300) {

import { HttpVerb, Options, Response } from './types';
/**
* 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.
*/
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';
import { checkGetBodyStatus, checkValidCurlCode, handleQs, parseReturnedHeaders, parseIncomingHeaders, } from './utils';
/**
* 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 {Object.<string, any>} qs - query string parameters for the request
* @returns {string} The modified URL with the updated query string parameters.
*/
const handleQueryString = (curl, url, qs) => {

@@ -8,2 +17,9 @@ url = qs && Object.keys(qs).length ? handleQs(url, qs) : url;

};
/**
* Sets up a callback function for the cURL Easy object to handle returned
* headers and populate the input array with header lines.
*
* @param {Easy} curl - The cURL easy handle.
* @param {string[]} returnedHeaderArray - array for returned header lines.
*/
const handleOutgoingHeaders = (curl, returnedHeaderArray) => {

@@ -15,2 +31,12 @@ curl.setOpt(Curl.option.HEADERFUNCTION, (headerLine) => {

};
/**
* Prepares the request body and headers for a cURL request based on provided
* options. Also sets up a callback function for the cURL Easy object to handle
* returned body and populates the input buffet.
*
* @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.
*/
const handleBody = (curl, options, buffer, httpHeaders) => {

@@ -32,9 +58,19 @@ let payload = '';

};
/**
* 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.
*/
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, options.timeout || 0);
curl.setOpt(Curl.option.FOLLOWLOCATION, options.followRedirects === undefined || options.followRedirects);
curl.setOpt(Curl.option.MAXREDIRS, options.maxRedirects || -1);
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);

@@ -61,2 +97,7 @@ // Query string parameters

const body = bufferWrap.body;
/**
* Get the body of a response with an optional encoding.
* @throws {Error} if the status code is >= 300.
* @returns {Buffer | string} buffer body by default, string body with encoding
*/
const getBody = (encoding) => {

@@ -63,0 +104,0 @@ checkGetBodyStatus(statusCode, body);

@@ -7,5 +7,3 @@ /// <reference types="node" />

export type BufferEncoding = 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex';
export interface SetEasyOptionCallback {
(curl: Easy, curlOption: CurlOption): void;
}
export type SetEasyOptionCallback = (curl: Easy, curlOption: CurlOption) => void;
export interface Options {

@@ -12,0 +10,0 @@ headers?: IncomingHttpHeaders;

@@ -5,8 +5,48 @@ /// <reference types="node" />

import { HttpVerb, Options } from './types';
/**
* Handles query string parameters in a URL by modifying or appending them
* based on the provided object.
*
* Arrays of primitives, e.g. { quizIds: [1,2,3] }, will be of the form:
* https://google.com.au/?quizIds%5B0%5D=0&quizIds%5B1%5D=1&quizIds%5B2%5D=2
* i.e. https://www.google.com.au/?quizIds[0]=0&quizIds[1]=1&quizIds[2]=2
*
* @param {string} url - The URL to handle query string parameters for.
* @param {Object.<string, any>} qs - query string parameters to modify or append.
* @returns {string} The modified URL with the updated query string parameters.
*/
export declare const handleQs: (url: string, qs: {
[key: string]: any;
}) => string;
/**
* Parses incoming HTTP headers to an array of formatted strings.
*
* @param {IncomingHttpHeaders} headers - The header object to parse.
* @returns {string[]} An array of formatted header strings.
*/
export declare const parseIncomingHeaders: (headers?: IncomingHttpHeaders) => string[];
/**
* Parses an array of header lines as IncomingHttpHeaders.
*
* @param {string[]} headerLines - An array of header lines to parse.
* @returns {IncomingHttpHeaders} An object containing parsed headers.
*/
export declare const parseReturnedHeaders: (headerLines: string[]) => IncomingHttpHeaders;
/**
* Checks if a CurlCode is valid and throws a CurlError if it's not.
*
* @param {CurlCode} code - The CurlCode to check.
* @param {HttpVerb} method - The HTTP method used in the request.
* @param {string} url - The URL of the request.
* @param {Options} options - The options used in the request.
* @throws {CurlError} if the CurlCode is not CURLE_OK.
*/
export declare const checkValidCurlCode: (code: CurlCode, method: HttpVerb, url: string, options: Options) => void;
/**
* Checks the status code and body of an HTTP response
*
* @param {number} statusCode - The status code of the HTTP response.
* @param {Buffer} body - The body of the HTTP response.
* @throws {Error} if the status code is >= 300.
*/
export declare const checkGetBodyStatus: (statusCode: number, body: Buffer) => void;
import { CurlCode } from 'node-libcurl';
import { CurlError } from './errors';
/**
* Handles query string parameters in a URL by modifying or appending them
* based on the provided object.
*
* Arrays of primitives, e.g. { quizIds: [1,2,3] }, will be of the form:
* https://google.com.au/?quizIds%5B0%5D=0&quizIds%5B1%5D=1&quizIds%5B2%5D=2
* i.e. https://www.google.com.au/?quizIds[0]=0&quizIds[1]=1&quizIds[2]=2
*
* @param {string} url - The URL to handle query string parameters for.
* @param {Object.<string, any>} qs - query string parameters to modify or append.
* @returns {string} The modified URL with the updated query string parameters.
*/
export const handleQs = (url, qs) => {

@@ -20,2 +32,8 @@ const urlObj = new URL(url);

};
/**
* Parses incoming HTTP headers to an array of formatted strings.
*
* @param {IncomingHttpHeaders} headers - The header object to parse.
* @returns {string[]} An array of formatted header strings.
*/
export const parseIncomingHeaders = (headers) => {

@@ -28,2 +46,8 @@ return headers

};
/**
* Parses an array of header lines as IncomingHttpHeaders.
*
* @param {string[]} headerLines - An array of header lines to parse.
* @returns {IncomingHttpHeaders} An object containing parsed headers.
*/
export const parseReturnedHeaders = (headerLines) => {

@@ -38,2 +62,11 @@ return headerLines.reduce((acc, header) => {

};
/**
* Checks if a CurlCode is valid and throws a CurlError if it's not.
*
* @param {CurlCode} code - The CurlCode to check.
* @param {HttpVerb} method - The HTTP method used in the request.
* @param {string} url - The URL of the request.
* @param {Options} options - The options used in the request.
* @throws {CurlError} if the CurlCode is not CURLE_OK.
*/
export const checkValidCurlCode = (code, method, url, options) => {

@@ -54,2 +87,9 @@ if (code !== CurlCode.CURLE_OK) {

};
/**
* Checks the status code and body of an HTTP response
*
* @param {number} statusCode - The status code of the HTTP response.
* @param {Buffer} body - The body of the HTTP response.
* @throws {Error} if the status code is >= 300.
*/
export const checkGetBodyStatus = (statusCode, body) => {

@@ -56,0 +96,0 @@ if (statusCode >= 300) {

@@ -7,3 +7,3 @@ {

},
"version": "2.1.3",
"version": "2.1.4",
"files": [

@@ -56,14 +56,14 @@ "dist"

"devDependencies": {
"@types/express": "^4.17.17",
"@types/express": "^4.17.18",
"@types/jest": "^29.5.5",
"@types/morgan": "^1.9.5",
"@types/node": "^20.6.2",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"eslint": "^8.49.0",
"eslint-plugin-jest": "^27.4.0",
"@types/morgan": "^1.9.6",
"@types/node": "^20.8.3",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"eslint": "^8.51.0",
"eslint-plugin-jest": "^27.4.2",
"express": "^4.18.2",
"http-errors": "^2.0.0",
"jest": "^29.7.0",
"jest-dev-server": "^9.0.0",
"jest-dev-server": "^9.0.1",
"morgan": "^1.10.0",

@@ -70,0 +70,0 @@ "ts-jest": "^29.1.1",

@@ -25,2 +25,12 @@ <div align="center">

&nbsp;
[![GitHub issues](https://img.shields.io/github/issues/nktnet1/sync-request-curl.svg?style=social)](https://github.com/nktnet1/sync-request-curl)
[![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)
&nbsp;
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/f222c2e572fc41b7b45c3591c3575a9d)](https://app.codacy.com/gh/nktnet1/sync-request-curl/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
&nbsp;
[![DeepSource](https://app.deepsource.com/gh/nktnet1/sync-request-curl.svg/?label=active+issues&show_trend=true&token=OTP6tE2be4X1kvxZRsxRh25e)](https://app.deepsource.com/gh/nktnet1/sync-request-curl/)
&nbsp;
[![codebeat badge](https://codebeat.co/badges/8bdb4562-0492-4c1c-8b02-e69c94373d60)](https://codebeat.co/projects/github-com-nktnet1-sync-request-curl-main)
&nbsp;
[![GitHub stars](https://img.shields.io/github/stars/nktnet1/sync-request-curl.svg?style=social)](https://github.com/nktnet1/sync-request-curl)

@@ -54,12 +64,12 @@

- [2. Usage](#2-usage)
- [2.1. Method](#21-method)
- [2.2. URL](#22-url)
- [2.3. Options](#23-options)
- [2.4. Response](#24-response)
- [2.5. Errors](#25-Errors)
- [2.1. Method](#21-method)
- [2.2. URL](#22-url)
- [2.3. Options](#23-options)
- [2.4. Response](#24-response)
- [2.5. Errors](#25-Errors)
- [3. License](#3-license)
- [4. Compatibility](#4-compatibility)
- [4.1. Windows](#41-windows)
- [4.2. MacOS](#42-macos)
- [4.3. Linux](#43-linux)
- [4.1. Windows](#41-windows)
- [4.2. MacOS](#42-macos)
- [4.3. Linux](#43-linux)
- [5. Caveats](#5-caveats)

@@ -66,0 +76,0 @@

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc