axios-curlirize
Advanced tools
Comparing version 1.4.0 to 2.0.0
{ | ||
"name": "axios-curlirize", | ||
"version": "1.4.0", | ||
"version": "2.0.0", | ||
"description": "Axios third-party module to print all axios requests as curl commands in the console. This repository is forked from axios-curlirize <https://www.npmjs.com/package/axios-curlirize> and supports use with Node JS without having to enable ES6 imports. This repo was inspired by an issue <https://github.com/delirius325/axios-curlirize/issues/20> raised on the origional repository <https://github.com/delirius325/axios-curlirize>. Special thanks to Anthony Gauthier <https://github.com/delirius325>, the author of axios-curlirize", | ||
@@ -46,4 +46,5 @@ "main": "src/main.js", | ||
"express": "^4.17.1", | ||
"mocha": "^9.0.1" | ||
"mocha": "^9.0.1", | ||
"qs": "^6.9.4" | ||
} | ||
} |
@@ -60,3 +60,9 @@ export class CurlHelper { | ||
if (this.request.baseURL) { | ||
return this.request.baseURL + "/" + this.request.url; | ||
let baseUrl = this.request.baseURL | ||
let url = this.request.url | ||
let finalUrl = baseUrl + "/" + url | ||
return finalUrl | ||
.replace(/\/{2,}/g, '/') | ||
.replace("http:/", "http://") | ||
.replace("https:/", "https://") | ||
} | ||
@@ -67,4 +73,10 @@ return this.request.url; | ||
getQueryString() { | ||
let params = "", | ||
i = 0; | ||
if (this.request.paramsSerializer) { | ||
const params = this.request.paramsSerializer(this.request.params) | ||
if (!params || params.length === 0) return '' | ||
if (params.startsWith('?')) return params | ||
return `?${params}` | ||
} | ||
let params = "" | ||
let i = 0 | ||
@@ -88,3 +100,2 @@ for(let param in this.request.params) { | ||
if (this.getQueryString() !== "") { | ||
url = url.charAt(url.length - 1) === "/" ? url.substr(0, url.length - 1) : url; | ||
url += this.getQueryString(); | ||
@@ -97,3 +108,3 @@ } | ||
generateCommand() { | ||
return `curl ${this.getMethod()} ${this.getHeaders()} ${this.getBody()} "${this.getBuiltURL()}"` | ||
return `curl ${this.getMethod()} "${this.getBuiltURL()}" ${this.getHeaders()} ${this.getBody()}` | ||
.trim() | ||
@@ -100,0 +111,0 @@ .replace(/\s{2,}/g, " "); |
@@ -5,3 +5,3 @@ import expect from "expect"; | ||
import { CurlHelper } from "../src/lib/CurlHelper.js"; | ||
import qs from 'qs' | ||
import app from "./devapp.js"; | ||
@@ -55,3 +55,3 @@ | ||
expect(res.config.curlCommand).toBeDefined(); | ||
expect(res.config.curlCommand).toBe(`curl -X POST -H "Content-Type:application/x-www-form-urlencoded" --data '{"dummy":"data"}' "http://localhost:7500/"`); | ||
expect(res.config.curlCommand).toBe(`curl -X POST "http://localhost:7500/" -H "Content-Type:application/x-www-form-urlencoded" --data '{"dummy":"data"}'`); | ||
done(); | ||
@@ -68,3 +68,3 @@ }) | ||
expect(res.config.curlCommand).toBeDefined(); | ||
expect(res.config.curlCommand).toBe(`curl -X POST -H "Content-Type:application/x-www-form-urlencoded" "http://localhost:7500/"`); | ||
expect(res.config.curlCommand).toBe(`curl -X POST "http://localhost:7500/" -H "Content-Type:application/x-www-form-urlencoded"`); | ||
done(); | ||
@@ -81,3 +81,3 @@ }) | ||
expect(res.config.curlCommand).toBeDefined(); | ||
expect(res.config.curlCommand).toBe("curl -X POST -H \"Content-Type:application/x-www-form-urlencoded\" -H \"Authorization:Bearer 123\" -H \"testHeader:Testing\" \"http://localhost:7500/\""); | ||
expect(res.config.curlCommand).toBe('curl -X POST "http://localhost:7500/" -H "Content-Type:application/x-www-form-urlencoded" -H "Authorization:Bearer 123" -H "testHeader:Testing"'); | ||
done(); | ||
@@ -94,3 +94,3 @@ }) | ||
expect(res.config.curlCommand).toBeDefined(); | ||
expect(res.config.curlCommand).toBe("curl -X POST -H \"Content-Type:application/x-www-form-urlencoded\" \"http://localhost:7500?test=1\""); | ||
expect(res.config.curlCommand).toBe('curl -X POST "http://localhost:7500/?test=1" -H "Content-Type:application/x-www-form-urlencoded"'); | ||
done(); | ||
@@ -102,2 +102,65 @@ }) | ||
}); | ||
it("should return the generated command with a queryString specified in the URL with paramsSerializer", (done) => { | ||
const api = axios.create({ | ||
paramsSerializer: (params) => { | ||
return qs.stringify(params) | ||
} | ||
}) | ||
curlirize(api) | ||
api.post("http://localhost:7500/", null, {params: {test: 1, text: 'sim'}}) | ||
.then((res) => { | ||
expect(res.config.curlCommand).toBeDefined(); | ||
expect(res.config.curlCommand).toBe('curl -X POST "http://localhost:7500/?test=1&text=sim" -H "Content-Type:application/x-www-form-urlencoded"'); | ||
done(); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
}); | ||
}); | ||
it("do not add ? if params is empty", (done) => { | ||
axios.post("http://localhost:7500/", null) | ||
.then((res) => { | ||
expect(res.config.curlCommand).toBeDefined(); | ||
expect(res.config.curlCommand).toBe('curl -X POST "http://localhost:7500/" -H "Content-Type:application/x-www-form-urlencoded"'); | ||
done(); | ||
}) | ||
.catch((err) => { | ||
console.log('--', err) | ||
console.error(err); | ||
}); | ||
}); | ||
it("do not cut end slash", (done) => { | ||
const api = axios.create({ | ||
baseURL: 'http://localhost:7500', | ||
}) | ||
curlirize(api) | ||
api.post("api/", null, {params: {test: 1, text: 'sim'}}) | ||
.then((res) => { | ||
expect(res.config.curlCommand).toBeDefined(); | ||
expect(res.config.curlCommand).toBe('curl -X POST "http://localhost:7500/api/?test=1&text=sim" -H "Content-Type:application/x-www-form-urlencoded"'); | ||
done(); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
}); | ||
}); | ||
it("cut middle slash", (done) => { | ||
const api = axios.create({ | ||
baseURL: 'http://localhost:7500/', | ||
}) | ||
curlirize(api) | ||
api.post("/api/", null, {params: {test: 1, text: 'sim'}}) | ||
.then((res) => { | ||
expect(res.config.curlCommand).toBeDefined(); | ||
expect(res.config.curlCommand).toBe('curl -X POST "http://localhost:7500/api/?test=1&text=sim" -H "Content-Type:application/x-www-form-urlencoded"'); | ||
done(); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
}); | ||
}); | ||
}); | ||
@@ -119,3 +182,3 @@ | ||
data: { dummy: "data" }, | ||
params: { testParam: "test1", testParamTwo: "test2"} | ||
params: { testParam: "test1", testParamTwo: "test2"} | ||
}; | ||
@@ -208,2 +271,2 @@ const curl = new CurlHelper(fakeConfig); | ||
}); | ||
}); | ||
}); |
@@ -13,2 +13,6 @@ import express from "express"; | ||
app.post("/api", (req, res) => { | ||
res.send({ hello: "world" }); | ||
}); | ||
app.listen(7500, () => { | ||
@@ -18,2 +22,2 @@ console.info("Express dev server listening on port 7500"); | ||
export default app; | ||
export default app; |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
31061
10
387
0
0
6