http-to-curl
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -1,1 +0,1 @@ | ||
"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].replace(/(\\|")/g,"\\$1")}" `),"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; | ||
"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")),https=_interopDefault(require("https"));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].replace(/(\\|")/g,"\\$1")}" `),"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,d,e,f,g){const h=[],a=d(e,f);return monkeypatch(a,"write",(a,b,c,d)=>(h.push(b),a(b,c,d))),monkeypatch(a,"end",(a,d,f,c)=>{let i="";return d&&h.push(d),0<h.length&&(i=Buffer.concat(h).toString()),g(curlGenerator(e,i,b)),a(d,f,c)}),a}function httpToCurl(a){monkeyPatchHttp(http,a),monkeyPatchHttp(https,a)}function monkeyPatchHttp(a,{filter:b="",customCallback:c=()=>{}}){monkeypatch(a,"request",(a,d,e)=>requestPatch(b,a,d,e,c))}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.3.0", | ||
"version": "1.4.0", | ||
"description": "Convert node HTTP request to curl", | ||
@@ -5,0 +5,0 @@ "main": "lib/bundle.js", |
@@ -1,2 +0,1 @@ | ||
[![npm version](https://badge.fury.io/js/http-to-curl.svg)](https://badge.fury.io/js/http-to-curl) | ||
@@ -10,4 +9,4 @@ [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php) | ||
## Installation ⚙️ | ||
## Installation ⚙️ | ||
```sh | ||
@@ -17,6 +16,6 @@ yarn add http-to-curl | ||
npm install http-to-curl --save | ||
``` | ||
## Usage 📚 | ||
```js | ||
@@ -42,19 +41,39 @@ // import httpToCurl on your server entry point of your project or code (e.g. server.js / index.js) | ||
curl "https://jsonplaceholder.typicode.com/posts/1" -X GET -H "Accept: application/json, text/plain, */*" -H "User-Agent: axios/0.18.0" | ||
``` | ||
``` | ||
It will listen all your nodejs http request and generate curl for each request. Its support all method (GET, POST, PUT, DELETE & PATCH) | ||
## Filtering ✅ | ||
`nodejs-http-to-curl` support filtering url using regex and string. Only match url will be generated. | ||
# Options | ||
## 1. Filtering ✅ | ||
`http-to-curl` support filtering url using regex and string. Only match url will be generated. | ||
```js | ||
import httpToCurl from 'http-to-curl'; | ||
//Single url match | ||
httpToCurl(/api/v1/); | ||
const options = { | ||
filter: /api/v1/ | ||
} | ||
httpToCurl(options); | ||
//Multiple url match | ||
httpToCurl([/api/v1/, /api/v3/]); | ||
const options = { | ||
filter: [/api/v1/, /api/v3/] | ||
} | ||
httpToCurl(options); | ||
``` | ||
## 2. Custom callback ⚙️ | ||
```js | ||
const options = { | ||
customCallback: function(curlString) { | ||
console.log('hey this is the custom callback', curlString); | ||
}, | ||
}; | ||
httpToCurl(options); | ||
``` | ||
## Contributing | ||
We'd ❤️ to have your helping hand on http-to-curl! Feel free to PR's, add issues or give feedback! Happy Hacking!! 😎 |
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
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
5504
9
77
3