sync-request-curl
Advanced tools
Comparing version
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.request = void 0; | ||
// const request = require('sync-curl'); | ||
const node_libcurl_1 = require("node-libcurl"); | ||
@@ -14,8 +13,6 @@ const utils_1 = require("./utils"); | ||
// ==========================// | ||
if (options.qs && Object.keys(options.qs).length > 0) { | ||
curl.setOpt(node_libcurl_1.Curl.option.URL, `${url}?${(0, utils_1.generateQueryString)(options.qs)}`); | ||
} | ||
else { | ||
curl.setOpt(node_libcurl_1.Curl.option.URL, url); | ||
} | ||
const queryString = options.qs && Object.keys(options.qs).length > 0 | ||
? `?${(0, utils_1.generateQueryString)(options.qs)}` | ||
: ''; | ||
curl.setOpt(node_libcurl_1.Curl.option.URL, `${url}${queryString}`); | ||
// ======================================================================= // | ||
@@ -25,7 +22,6 @@ // Handle headers | ||
// Incoming headers | ||
const httpHeaders = []; | ||
if (options.headers) { | ||
Object.entries(options.headers).forEach(([key, value]) => httpHeaders.push(`${key}: ${value}`)); | ||
} | ||
// Response headers | ||
const httpHeaders = options.headers | ||
? Object.entries(options.headers).map(([key, value]) => `${key}: ${value}`) | ||
: []; | ||
// Outgoing headers | ||
const returnedHeaderArray = []; | ||
@@ -56,5 +52,2 @@ curl.setOpt(node_libcurl_1.Curl.option.HEADERFUNCTION, (headerLine) => { | ||
const code = curl.perform(); | ||
// ======================================================================= // | ||
// Error handling | ||
// ==========================// | ||
if (code !== node_libcurl_1.CurlCode.CURLE_OK) { | ||
@@ -73,2 +66,5 @@ throw new Error(` | ||
} | ||
// ======================================================================= // | ||
// Finalising return | ||
// ==========================// | ||
const statusCode = curl.getInfo('RESPONSE_CODE').data; | ||
@@ -78,5 +74,2 @@ if (typeof statusCode !== 'number') { | ||
} | ||
// ======================================================================= // | ||
// Finalising return | ||
// ==========================// | ||
const headers = (0, utils_1.parseHeaders)(returnedHeaderArray); | ||
@@ -96,9 +89,6 @@ const getBody = (encoding) => { | ||
}; | ||
// ======================================================================= // | ||
// Finish | ||
// ==========================// | ||
curl.close(); | ||
return { statusCode, body, getBody, headers }; | ||
return { statusCode, headers, body, getBody }; | ||
}; | ||
exports.request = request; | ||
//# sourceMappingURL=request.js.map |
@@ -1,2 +0,1 @@ | ||
// const request = require('sync-curl'); | ||
import { Curl, CurlCode, Easy } from 'node-libcurl'; | ||
@@ -11,8 +10,6 @@ import { generateQueryString, parseHeaders } from './utils'; | ||
// ==========================// | ||
if (options.qs && Object.keys(options.qs).length > 0) { | ||
curl.setOpt(Curl.option.URL, `${url}?${generateQueryString(options.qs)}`); | ||
} | ||
else { | ||
curl.setOpt(Curl.option.URL, url); | ||
} | ||
const queryString = options.qs && Object.keys(options.qs).length > 0 | ||
? `?${generateQueryString(options.qs)}` | ||
: ''; | ||
curl.setOpt(Curl.option.URL, `${url}${queryString}`); | ||
// ======================================================================= // | ||
@@ -22,7 +19,6 @@ // Handle headers | ||
// Incoming headers | ||
const httpHeaders = []; | ||
if (options.headers) { | ||
Object.entries(options.headers).forEach(([key, value]) => httpHeaders.push(`${key}: ${value}`)); | ||
} | ||
// Response headers | ||
const httpHeaders = options.headers | ||
? Object.entries(options.headers).map(([key, value]) => `${key}: ${value}`) | ||
: []; | ||
// Outgoing headers | ||
const returnedHeaderArray = []; | ||
@@ -53,5 +49,2 @@ curl.setOpt(Curl.option.HEADERFUNCTION, (headerLine) => { | ||
const code = curl.perform(); | ||
// ======================================================================= // | ||
// Error handling | ||
// ==========================// | ||
if (code !== CurlCode.CURLE_OK) { | ||
@@ -70,2 +63,5 @@ throw new Error(` | ||
} | ||
// ======================================================================= // | ||
// Finalising return | ||
// ==========================// | ||
const statusCode = curl.getInfo('RESPONSE_CODE').data; | ||
@@ -75,5 +71,2 @@ if (typeof statusCode !== 'number') { | ||
} | ||
// ======================================================================= // | ||
// Finalising return | ||
// ==========================// | ||
const headers = parseHeaders(returnedHeaderArray); | ||
@@ -93,8 +86,5 @@ const getBody = (encoding) => { | ||
}; | ||
// ======================================================================= // | ||
// Finish | ||
// ==========================// | ||
curl.close(); | ||
return { statusCode, body, getBody, headers }; | ||
return { statusCode, headers, body, getBody }; | ||
}; | ||
//# sourceMappingURL=request.js.map |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"files": [ | ||
@@ -10,0 +10,0 @@ "dist" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
23426
-4.13%264
-7.04%