sync-request-curl
Advanced tools
Comparing version 1.3.3 to 1.3.4
@@ -10,9 +10,2 @@ "use strict"; | ||
}; | ||
const handleIncomingHeaders = (curl, headers) => { | ||
return headers | ||
? Object.entries(headers) | ||
.filter(([_, value]) => value !== undefined) | ||
.map(([key, value]) => value === '' ? `${key};` : `${key}: ${value}`) | ||
: []; | ||
}; | ||
const handleOutgoingHeaders = (curl, returnedHeaderArray) => { | ||
@@ -37,29 +30,2 @@ curl.setOpt(node_libcurl_1.Curl.option.HEADERFUNCTION, (headerLine) => { | ||
}; | ||
const checkValidCurlCode = (code, method, url, options) => { | ||
if (code !== node_libcurl_1.CurlCode.CURLE_OK) { | ||
throw new Error(` | ||
Curl request failed with code ${code} | ||
Please look up libcurl error code! | ||
- https://curl.se/libcurl/c/libcurl-errors.html | ||
DEBUG: { | ||
method: "${method}", | ||
url: "${url}", | ||
options: ${JSON.stringify(options)} | ||
} | ||
`); | ||
} | ||
}; | ||
const checkGetBodyStatus = (statusCode, body) => { | ||
if (statusCode >= 300) { | ||
throw new Error(` | ||
Server responded with status code ${statusCode} | ||
Body: ${body.toString()} | ||
Use 'res.body' instead of 'res.getBody()' to not have any errors thrown. | ||
The status code (in this case, ${statusCode}) can be checked manually with res.statusCode. | ||
`); | ||
} | ||
}; | ||
const request = (method, url, options = {}) => { | ||
@@ -72,3 +38,3 @@ const curl = new node_libcurl_1.Easy(); | ||
handleQueryString(curl, url, options.qs); | ||
const httpHeaders = handleIncomingHeaders(curl, options.headers); | ||
const httpHeaders = (0, utils_1.parseIncomingHeaders)(options.headers); | ||
const returnedHeaderArray = []; | ||
@@ -80,9 +46,9 @@ handleOutgoingHeaders(curl, returnedHeaderArray); | ||
const code = curl.perform(); | ||
checkValidCurlCode(code, method, url, options); | ||
(0, utils_1.checkValidCurlCode)(code, method, url, options); | ||
url = curl.getInfo('EFFECTIVE_URL').data; | ||
const statusCode = curl.getInfo('RESPONSE_CODE').data; | ||
const body = bufferWrap.body; | ||
const headers = (0, utils_1.parseHeaders)(returnedHeaderArray); | ||
const headers = (0, utils_1.parseReturnedHeaders)(returnedHeaderArray); | ||
const getBody = (encoding) => { | ||
checkGetBodyStatus(statusCode, body); | ||
(0, utils_1.checkGetBodyStatus)(statusCode, body); | ||
return typeof encoding === 'string' ? body.toString(encoding) : body; | ||
@@ -89,0 +55,0 @@ }; |
@@ -0,5 +1,11 @@ | ||
/// <reference types="node" /> | ||
import { IncomingHttpHeaders } from 'http'; | ||
import { CurlCode } from 'node-libcurl'; | ||
import { HttpVerb, Options } from './types'; | ||
export declare const handleQs: (url: string, qs: { | ||
[key: string]: any; | ||
}) => string; | ||
export declare const parseHeaders: (headerLines: string[]) => IncomingHttpHeaders; | ||
export declare const parseIncomingHeaders: (headers?: IncomingHttpHeaders) => string[]; | ||
export declare const parseReturnedHeaders: (headerLines: string[]) => IncomingHttpHeaders; | ||
export declare const checkValidCurlCode: (code: CurlCode, method: HttpVerb, url: string, options: Options) => void; | ||
export declare const checkGetBodyStatus: (statusCode: number, body: Buffer) => void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parseHeaders = exports.handleQs = void 0; | ||
exports.checkGetBodyStatus = exports.checkValidCurlCode = exports.parseReturnedHeaders = exports.parseIncomingHeaders = exports.handleQs = void 0; | ||
const node_libcurl_1 = require("node-libcurl"); | ||
const handleQs = (url, qs) => { | ||
@@ -20,3 +21,11 @@ const urlObj = new URL(url); | ||
exports.handleQs = handleQs; | ||
const parseHeaders = (headerLines) => { | ||
const parseIncomingHeaders = (headers) => { | ||
return headers | ||
? Object.entries(headers) | ||
.filter(([_, value]) => value !== undefined) | ||
.map(([key, value]) => value === '' ? `${key};` : `${key}: ${value}`) | ||
: []; | ||
}; | ||
exports.parseIncomingHeaders = parseIncomingHeaders; | ||
const parseReturnedHeaders = (headerLines) => { | ||
return headerLines.reduce((acc, header) => { | ||
@@ -30,3 +39,32 @@ const [name, ...values] = header.split(':'); | ||
}; | ||
exports.parseHeaders = parseHeaders; | ||
exports.parseReturnedHeaders = parseReturnedHeaders; | ||
const checkValidCurlCode = (code, method, url, options) => { | ||
if (code !== node_libcurl_1.CurlCode.CURLE_OK) { | ||
throw new Error(` | ||
Curl request failed with code ${code} | ||
Please look up libcurl error code! | ||
- https://curl.se/libcurl/c/libcurl-errors.html | ||
DEBUG: { | ||
method: "${method}", | ||
url: "${url}", | ||
options: ${JSON.stringify(options)} | ||
} | ||
`); | ||
} | ||
}; | ||
exports.checkValidCurlCode = checkValidCurlCode; | ||
const checkGetBodyStatus = (statusCode, body) => { | ||
if (statusCode >= 300) { | ||
throw new Error(` | ||
Server responded with status code ${statusCode} | ||
Body: ${body.toString()} | ||
Use 'res.body' instead of 'res.getBody()' to not have any errors thrown. | ||
The status code (in this case, ${statusCode}) can be checked manually with res.statusCode. | ||
`); | ||
} | ||
}; | ||
exports.checkGetBodyStatus = checkGetBodyStatus; | ||
//# sourceMappingURL=utils.js.map |
@@ -1,3 +0,3 @@ | ||
import { Curl, CurlCode, Easy } from 'node-libcurl'; | ||
import { handleQs, parseHeaders } from './utils'; | ||
import { Curl, Easy } from 'node-libcurl'; | ||
import { checkGetBodyStatus, checkValidCurlCode, handleQs, parseReturnedHeaders, parseIncomingHeaders } from './utils'; | ||
const handleQueryString = (curl, url, qs) => { | ||
@@ -8,9 +8,2 @@ url = qs && Object.keys(qs).length ? handleQs(url, qs) : url; | ||
}; | ||
const handleIncomingHeaders = (curl, headers) => { | ||
return headers | ||
? Object.entries(headers) | ||
.filter(([_, value]) => value !== undefined) | ||
.map(([key, value]) => value === '' ? `${key};` : `${key}: ${value}`) | ||
: []; | ||
}; | ||
const handleOutgoingHeaders = (curl, returnedHeaderArray) => { | ||
@@ -35,29 +28,2 @@ curl.setOpt(Curl.option.HEADERFUNCTION, (headerLine) => { | ||
}; | ||
const checkValidCurlCode = (code, method, url, options) => { | ||
if (code !== CurlCode.CURLE_OK) { | ||
throw new Error(` | ||
Curl request failed with code ${code} | ||
Please look up libcurl error code! | ||
- https://curl.se/libcurl/c/libcurl-errors.html | ||
DEBUG: { | ||
method: "${method}", | ||
url: "${url}", | ||
options: ${JSON.stringify(options)} | ||
} | ||
`); | ||
} | ||
}; | ||
const checkGetBodyStatus = (statusCode, body) => { | ||
if (statusCode >= 300) { | ||
throw new Error(` | ||
Server responded with status code ${statusCode} | ||
Body: ${body.toString()} | ||
Use 'res.body' instead of 'res.getBody()' to not have any errors thrown. | ||
The status code (in this case, ${statusCode}) can be checked manually with res.statusCode. | ||
`); | ||
} | ||
}; | ||
const request = (method, url, options = {}) => { | ||
@@ -70,3 +36,3 @@ const curl = new Easy(); | ||
handleQueryString(curl, url, options.qs); | ||
const httpHeaders = handleIncomingHeaders(curl, options.headers); | ||
const httpHeaders = parseIncomingHeaders(options.headers); | ||
const returnedHeaderArray = []; | ||
@@ -82,3 +48,3 @@ handleOutgoingHeaders(curl, returnedHeaderArray); | ||
const body = bufferWrap.body; | ||
const headers = parseHeaders(returnedHeaderArray); | ||
const headers = parseReturnedHeaders(returnedHeaderArray); | ||
const getBody = (encoding) => { | ||
@@ -85,0 +51,0 @@ checkGetBodyStatus(statusCode, body); |
@@ -0,5 +1,11 @@ | ||
/// <reference types="node" /> | ||
import { IncomingHttpHeaders } from 'http'; | ||
import { CurlCode } from 'node-libcurl'; | ||
import { HttpVerb, Options } from './types'; | ||
export declare const handleQs: (url: string, qs: { | ||
[key: string]: any; | ||
}) => string; | ||
export declare const parseHeaders: (headerLines: string[]) => IncomingHttpHeaders; | ||
export declare const parseIncomingHeaders: (headers?: IncomingHttpHeaders) => string[]; | ||
export declare const parseReturnedHeaders: (headerLines: string[]) => IncomingHttpHeaders; | ||
export declare const checkValidCurlCode: (code: CurlCode, method: HttpVerb, url: string, options: Options) => void; | ||
export declare const checkGetBodyStatus: (statusCode: number, body: Buffer) => void; |
@@ -0,1 +1,2 @@ | ||
import { CurlCode } from 'node-libcurl'; | ||
export const handleQs = (url, qs) => { | ||
@@ -16,3 +17,10 @@ const urlObj = new URL(url); | ||
}; | ||
export const parseHeaders = (headerLines) => { | ||
export const parseIncomingHeaders = (headers) => { | ||
return headers | ||
? Object.entries(headers) | ||
.filter(([_, value]) => value !== undefined) | ||
.map(([key, value]) => value === '' ? `${key};` : `${key}: ${value}`) | ||
: []; | ||
}; | ||
export const parseReturnedHeaders = (headerLines) => { | ||
return headerLines.reduce((acc, header) => { | ||
@@ -26,2 +34,29 @@ const [name, ...values] = header.split(':'); | ||
}; | ||
export const checkValidCurlCode = (code, method, url, options) => { | ||
if (code !== CurlCode.CURLE_OK) { | ||
throw new Error(` | ||
Curl request failed with code ${code} | ||
Please look up libcurl error code! | ||
- https://curl.se/libcurl/c/libcurl-errors.html | ||
DEBUG: { | ||
method: "${method}", | ||
url: "${url}", | ||
options: ${JSON.stringify(options)} | ||
} | ||
`); | ||
} | ||
}; | ||
export const checkGetBodyStatus = (statusCode, body) => { | ||
if (statusCode >= 300) { | ||
throw new Error(` | ||
Server responded with status code ${statusCode} | ||
Body: ${body.toString()} | ||
Use 'res.body' instead of 'res.getBody()' to not have any errors thrown. | ||
The status code (in this case, ${statusCode}) can be checked manually with res.statusCode. | ||
`); | ||
} | ||
}; | ||
//# sourceMappingURL=utils.js.map |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"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
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
34111
322