http-to-curl
Advanced tools
Comparing version 1.2.3 to 1.2.4
@@ -1,186 +0,1 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var monkeypatch = _interopDefault(require('monkeypatch')); | ||
var http = _interopDefault(require('http')); | ||
require('buffer'); | ||
var chalk = _interopDefault(require('chalk')); | ||
/** | ||
* | ||
* | ||
* @export | ||
* @param {any} options | ||
* @returns {string} | ||
*/ | ||
function generateMethod(options) { | ||
const method = options.method; | ||
if (!method) return ''; | ||
const type = { | ||
GET: '-X GET', | ||
POST: '-X POST', | ||
PUT: '-X PUT', | ||
PATCH: '-X PATCH', | ||
DELETE: '-X DELETE' | ||
}; | ||
const methodParam = type[method.toUpperCase()]; | ||
return methodParam ? methodParam : ''; | ||
} | ||
/** | ||
* | ||
* | ||
* @export | ||
* @param {any} options | ||
* @returns {string} | ||
*/ | ||
function generateHeader(options) { | ||
const headers = options.headers; | ||
let isEncode = false; | ||
if (!headers) return ''; | ||
let headerParam = ''; | ||
Object.keys(headers).map((val, key) => { | ||
if (val.toLocaleLowerCase() !== 'content-length') { | ||
headerParam += `-H "${val}: ${headers[val]}" `; | ||
} | ||
if (val.toLocaleLowerCase() === 'accept-encoding') { | ||
isEncode = true; | ||
} | ||
}); | ||
return { | ||
params: headerParam.trim(), | ||
isEncode | ||
}; | ||
} | ||
/** | ||
* | ||
* | ||
* @export | ||
* @param {any} [options={}] | ||
* @returns {string} | ||
*/ | ||
function generateUrl(options = {}) { | ||
if (!options) return ''; | ||
const { | ||
protocol = 'http:', | ||
hostname = 'localhost', | ||
pathname = '/' | ||
} = options; | ||
return `"${protocol}//${hostname}${pathname}"`; | ||
} | ||
/** | ||
* | ||
* | ||
* @export | ||
* @param {Object} body | ||
* @returns {string} | ||
*/ | ||
function generateBody(body) { | ||
if (!body) return ''; | ||
return `--data-binary ${JSON.stringify(body)}`; | ||
} | ||
/** | ||
* | ||
* | ||
* @export | ||
* @param {boolean} isEncode | ||
* @return {string} | ||
*/ | ||
function generateCompress(isEncode) { | ||
return isEncode ? '--compressed' : ''; | ||
} | ||
/** | ||
* | ||
* | ||
* @export | ||
* @param {any} options | ||
* @param {string} [body=''] | ||
* @param {any} regex | ||
*/ | ||
function curlGenerator(options, body = '', regex) { | ||
let result = 'curl '; | ||
const headers = generateHeader(options); | ||
const url = generateUrl(options); | ||
if (regex) { | ||
let matchTemp = regex; | ||
if (!Array.isArray(regex)) { | ||
matchTemp = [regex]; | ||
} | ||
const isMatchFilter = matchTemp.filter(value => { | ||
return url.match(value); | ||
}); | ||
if (isMatchFilter.length === 0) { | ||
return ''; | ||
} | ||
} | ||
result += url + ' '; | ||
result += generateMethod(options) + ' '; | ||
result += headers.params + ' '; | ||
result += generateBody(body) + ' '; | ||
result += generateCompress(headers.isEncode); | ||
console.log(`${chalk.black.bgYellow.bold(' http-to-curl ')} | ||
${result} | ||
`); | ||
return result; | ||
} | ||
/** | ||
* | ||
* | ||
* @export | ||
* @param {any} regex | ||
* @param {any} request | ||
* @param {any} options | ||
* @param {any} cb | ||
* @returns | ||
*/ | ||
function requestPatch(regex, request, options, cb) { | ||
const bodyData = []; | ||
const clientReq = request(options, cb); | ||
monkeypatch(clientReq, 'write', (original, chunk, encoding, cb) => { | ||
bodyData.push(chunk); | ||
return original(chunk, encoding, cb); | ||
}); | ||
monkeypatch(clientReq, 'end', (original, data, encoding, cb) => { | ||
let body = ''; | ||
if (data) { | ||
bodyData.push(data); | ||
} | ||
if (bodyData.length > 0) { | ||
body = Buffer.concat(bodyData).toString(); | ||
} | ||
curlGenerator(options, body, regex); | ||
return original(data, encoding, cb); | ||
}); | ||
return clientReq; | ||
} | ||
/** | ||
* | ||
* | ||
* @param {string} [urlRegex=''] | ||
*/ | ||
function httpToCurl(urlRegex = '') { | ||
monkeypatch(http, 'request', (request, options, cb) => { | ||
return requestPatch(urlRegex, request, options, cb); | ||
}); | ||
} | ||
exports.generateMethod = generateMethod; | ||
exports.generateHeader = generateHeader; | ||
exports.generateUrl = generateUrl; | ||
exports.generateBody = generateBody; | ||
exports.generateCompress = generateCompress; | ||
exports.curlGenerator = curlGenerator; | ||
exports.requestPatch = requestPatch; | ||
exports.default = httpToCurl; | ||
"use strict";function _interopDefault(a){return a&&"object"==typeof a&&"default"in a?a.default:a}Object.defineProperty(exports,"__esModule",{value:!0});var monkeypatch=_interopDefault(require("monkeypatch")),http=_interopDefault(require("http"));require("buffer");var chalk=_interopDefault(require("chalk"));function generateMethod(a){const b=a.method;if(!b)return"";const c={GET:"-X GET",POST:"-X POST",PUT:"-X PUT",PATCH:"-X PATCH",DELETE:"-X DELETE"}[b.toUpperCase()];return c||""}function generateHeader(a){const b=a.headers;let c=!1;if(!b)return"";let d="";return Object.keys(b).map((a)=>{"content-length"!==a.toLocaleLowerCase()&&(d+=`-H "${a}: ${b[a]}" `),"accept-encoding"===a.toLocaleLowerCase()&&(c=!0)}),{params:d.trim(),isEncode:c}}function generateUrl(a={}){if(!a)return"";const{protocol:b="http:",hostname:c="localhost",pathname:d="/"}=a;return`"${b}//${c}${d}"`}function generateBody(a){return a?`--data-binary ${JSON.stringify(a)}`:""}function generateCompress(a){return a?"--compressed":""}function curlGenerator(b,c="",d){let e="curl ";const f=generateHeader(b),g=generateUrl(b);if(d){let a=d;if(Array.isArray(d)||(a=[d]),0===a.filter((a)=>g.match(a)).length)return""}return e+=g+" ",e+=generateMethod(b)+" ",e+=f.params+" ",e+=generateBody(c)+" ",e+=generateCompress(f.isEncode),console.log(`${chalk.black.bgYellow.bold(" http-to-curl ")}\n ${e}\n `),e}function requestPatch(b,c,d,e){const f=[],g=c(d,e);return monkeypatch(g,"write",(a,b,c,d)=>(f.push(b),a(b,c,d))),monkeypatch(g,"end",(e,g,h,a)=>{let c="";return g&&f.push(g),0<f.length&&(c=Buffer.concat(f).toString()),curlGenerator(d,c,b),e(g,h,a)}),g}function httpToCurl(a=""){monkeypatch(http,"request",(b,c,d)=>requestPatch(a,b,c,d))}exports.generateMethod=generateMethod,exports.generateHeader=generateHeader,exports.generateUrl=generateUrl,exports.generateBody=generateBody,exports.generateCompress=generateCompress,exports.curlGenerator=curlGenerator,exports.requestPatch=requestPatch,exports.default=httpToCurl; |
{ | ||
"name": "http-to-curl", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "Convert node HTTP request to curl", | ||
@@ -30,3 +30,5 @@ "main": "lib/bundle.js", | ||
"rollup-plugin-babel": "^3.0.4", | ||
"rollup-plugin-node-resolve": "^3.3.0" | ||
"rollup-plugin-babel-minify": "^4.0.0", | ||
"rollup-plugin-node-resolve": "^3.3.0", | ||
"rollup-plugin-uglify": "^3.0.0" | ||
}, | ||
@@ -33,0 +35,0 @@ "repository": { |
@@ -0,3 +1,4 @@ | ||
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php) | ||
# Node js HTTP request to cURL | ||
Tired to manually generate curl from nodejs request for debugging proposes? Need to export nodejs request to REST client (e.g. [Insomnia](https://insomnia.rest/) and [Postman](https://www.getpostman.com/))? http-to-curl come to the rescue!! | ||
Tired to manually generate curl from nodejs request for debugging proposes? Need to export nodejs request to your REST client (e.g. [Insomnia](https://insomnia.rest/) and [Postman](https://www.getpostman.com/))? http-to-curl come to the rescue!! | ||
@@ -15,10 +16,12 @@ | ||
```js | ||
// import httpToCurl on your server entry point of your project or code (e.g. server.js / index.js) | ||
import httpToCurl from 'http-to-curl'; | ||
httpToCurl(); | ||
// Traditional way | ||
const httpToCurl = require('http-to-curl').default; | ||
httpToCurl(); | ||
/* | ||
import your favorite http client (e.g. axios, isomorphic fetch or even vanilla request) all works well with http-to-curl. | ||
// Use your favorite http client to fetch (e.g. axios, isomorphic fetch or even vanilla request) all works well with http-to-curl. | ||
// In this example we are using axios. | ||
In this example we using axios | ||
*/ | ||
import axios from 'axios' | ||
@@ -30,3 +33,3 @@ const options = { | ||
//output | ||
//Output | ||
curl "https://jsonplaceholder.typicode.com/posts/1" -X GET -H "Accept: application/json, text/plain, */*" -H "User-Agent: axios/0.18.0" | ||
@@ -42,3 +45,6 @@ | ||
import httpToCurl from 'http-to-curl'; | ||
httpToCurl([/api/v1/, /api/v3/]); << Only generate match url | ||
//Single url match | ||
httpToCurl(/api/v1/); | ||
//Multiple url match | ||
httpToCurl([/api/v1/, /api/v3/]); | ||
``` | ||
@@ -45,0 +51,0 @@ |
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
51
4883
13
8
1
2