sync-request-curl
Advanced tools
Comparing version 1.3.10 to 1.3.11
@@ -30,2 +30,3 @@ "use strict"; | ||
const request = (method, url, options = {}) => { | ||
// Initialing curl object with custom options | ||
const curl = new node_libcurl_1.Easy(); | ||
@@ -36,15 +37,19 @@ curl.setOpt(node_libcurl_1.Curl.option.CUSTOMREQUEST, method); | ||
curl.setOpt(node_libcurl_1.Curl.option.MAXREDIRS, options.maxRedirects || Number.MAX_SAFE_INTEGER); | ||
// Query string parameters | ||
handleQueryString(curl, url, options.qs); | ||
// Headers (both incoming and outgoing) | ||
const httpHeaders = (0, utils_1.parseIncomingHeaders)(options.headers); | ||
const returnedHeaderArray = []; | ||
handleOutgoingHeaders(curl, returnedHeaderArray); | ||
// Body (and JSON) | ||
const bufferWrap = { body: Buffer.alloc(0) }; | ||
handleBody(curl, options, bufferWrap, httpHeaders); | ||
// Execute request | ||
curl.setOpt(node_libcurl_1.Curl.option.HTTPHEADER, httpHeaders); | ||
const code = curl.perform(); | ||
(0, utils_1.checkValidCurlCode)(code, method, url, options); | ||
url = curl.getInfo('EFFECTIVE_URL').data; | ||
// Creating return object | ||
const statusCode = curl.getInfo('RESPONSE_CODE').data; | ||
const headers = (0, utils_1.parseReturnedHeaders)(returnedHeaderArray); | ||
const body = bufferWrap.body; | ||
const headers = (0, utils_1.parseReturnedHeaders)(returnedHeaderArray); | ||
const getBody = (encoding) => { | ||
@@ -54,2 +59,3 @@ (0, utils_1.checkGetBodyStatus)(statusCode, body); | ||
}; | ||
url = curl.getInfo('EFFECTIVE_URL').data; | ||
curl.close(); | ||
@@ -56,0 +62,0 @@ return { statusCode, headers, body, getBody, url }; |
@@ -18,3 +18,3 @@ /// <reference types="node" /> | ||
} | ||
export type GetBody = <B extends BufferEncoding | undefined>(arg?: B) => B extends BufferEncoding ? string : Buffer; | ||
export type GetBody = <encoding extends BufferEncoding | undefined>(arg?: encoding) => encoding extends BufferEncoding ? string : Buffer; | ||
export interface Response { | ||
@@ -21,0 +21,0 @@ statusCode: number; |
@@ -7,16 +7,15 @@ "use strict"; | ||
const urlObj = new URL(url); | ||
const queryParams = urlObj.searchParams; | ||
Object.entries(qs).forEach(([key, value]) => { | ||
if (Array.isArray(value)) { | ||
queryParams.delete(key); | ||
value.forEach((item, i) => queryParams.append(`${key}[${i}]`, String(item))); | ||
urlObj.searchParams.delete(key); | ||
value.forEach((item, i) => urlObj.searchParams.append(`${key}[${i}]`, String(item))); | ||
} | ||
else if (value === null) { | ||
queryParams.set(key, ''); | ||
urlObj.searchParams.set(key, ''); | ||
} | ||
else if (value !== undefined) { | ||
queryParams.set(key, String(value)); | ||
urlObj.searchParams.set(key, String(value)); | ||
} | ||
}); | ||
urlObj.search = queryParams.toString(); | ||
urlObj.search = urlObj.searchParams.toString(); | ||
return urlObj.href; | ||
@@ -23,0 +22,0 @@ }; |
@@ -28,2 +28,3 @@ import { Curl, Easy } from 'node-libcurl'; | ||
const request = (method, url, options = {}) => { | ||
// Initialing curl object with custom options | ||
const curl = new Easy(); | ||
@@ -34,15 +35,19 @@ curl.setOpt(Curl.option.CUSTOMREQUEST, method); | ||
curl.setOpt(Curl.option.MAXREDIRS, options.maxRedirects || Number.MAX_SAFE_INTEGER); | ||
// Query string parameters | ||
handleQueryString(curl, url, options.qs); | ||
// Headers (both incoming and outgoing) | ||
const httpHeaders = parseIncomingHeaders(options.headers); | ||
const returnedHeaderArray = []; | ||
handleOutgoingHeaders(curl, returnedHeaderArray); | ||
// Body (and JSON) | ||
const bufferWrap = { body: Buffer.alloc(0) }; | ||
handleBody(curl, options, bufferWrap, httpHeaders); | ||
// Execute request | ||
curl.setOpt(Curl.option.HTTPHEADER, httpHeaders); | ||
const code = curl.perform(); | ||
checkValidCurlCode(code, method, url, options); | ||
url = curl.getInfo('EFFECTIVE_URL').data; | ||
// Creating return object | ||
const statusCode = curl.getInfo('RESPONSE_CODE').data; | ||
const headers = parseReturnedHeaders(returnedHeaderArray); | ||
const body = bufferWrap.body; | ||
const headers = parseReturnedHeaders(returnedHeaderArray); | ||
const getBody = (encoding) => { | ||
@@ -52,2 +57,3 @@ checkGetBodyStatus(statusCode, body); | ||
}; | ||
url = curl.getInfo('EFFECTIVE_URL').data; | ||
curl.close(); | ||
@@ -54,0 +60,0 @@ return { statusCode, headers, body, getBody, url }; |
@@ -18,3 +18,3 @@ /// <reference types="node" /> | ||
} | ||
export type GetBody = <B extends BufferEncoding | undefined>(arg?: B) => B extends BufferEncoding ? string : Buffer; | ||
export type GetBody = <encoding extends BufferEncoding | undefined>(arg?: encoding) => encoding extends BufferEncoding ? string : Buffer; | ||
export interface Response { | ||
@@ -21,0 +21,0 @@ statusCode: number; |
import { CurlCode } from 'node-libcurl'; | ||
export const handleQs = (url, qs) => { | ||
const urlObj = new URL(url); | ||
const queryParams = urlObj.searchParams; | ||
Object.entries(qs).forEach(([key, value]) => { | ||
if (Array.isArray(value)) { | ||
queryParams.delete(key); | ||
value.forEach((item, i) => queryParams.append(`${key}[${i}]`, String(item))); | ||
urlObj.searchParams.delete(key); | ||
value.forEach((item, i) => urlObj.searchParams.append(`${key}[${i}]`, String(item))); | ||
} | ||
else if (value === null) { | ||
queryParams.set(key, ''); | ||
urlObj.searchParams.set(key, ''); | ||
} | ||
else if (value !== undefined) { | ||
queryParams.set(key, String(value)); | ||
urlObj.searchParams.set(key, String(value)); | ||
} | ||
}); | ||
urlObj.search = queryParams.toString(); | ||
urlObj.search = urlObj.searchParams.toString(); | ||
return urlObj.href; | ||
@@ -19,0 +18,0 @@ }; |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "1.3.10", | ||
"version": "1.3.11", | ||
"files": [ | ||
@@ -58,3 +58,3 @@ "dist" | ||
"@types/morgan": "^1.9.4", | ||
"@types/node": "^20.4.7", | ||
"@types/node": "^20.4.8", | ||
"@typescript-eslint/eslint-plugin": "^6.2.1", | ||
@@ -61,0 +61,0 @@ "@typescript-eslint/parser": "^6.2.1", |
@@ -184,5 +184,3 @@ # sync-request-curl | ||
headers?: IncomingHttpHeaders; | ||
qs?: { | ||
[key: string]: any; | ||
}; | ||
qs?: { [key: string]: any }; | ||
json?: any; | ||
@@ -189,0 +187,0 @@ timeout?: number; |
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
35472
338
223