axios-curlirize
Advanced tools
Comparing version 1.2.3 to 1.3.0
{ | ||
"name": "axios-curlirize", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"description": "Axios third-party module to print all axios requests as curl commands in the console.", | ||
@@ -20,6 +20,8 @@ "main": "index.js", | ||
"author": "delirius325", | ||
"contributors": [{ | ||
"name" : "Justin \"jayceekay\"", | ||
"url" : "https://github.com/jayceekay" | ||
}, { | ||
"contributors": [ | ||
{ | ||
"name": "Justin \"jayceekay\"", | ||
"url": "https://github.com/jayceekay" | ||
}, | ||
{ | ||
"name": "Peter \"binggg\" Zhao", | ||
@@ -34,4 +36,3 @@ "url": "https://github.com/binggg" | ||
"homepage": "https://github.com/delirius325/axios-curlirize#readme", | ||
"dependencies": { | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
@@ -38,0 +39,0 @@ "axios": ">=0.19.0", |
@@ -8,12 +8,16 @@ export class CurlHelper { | ||
let headers = this.request.headers, | ||
curlHeaders = ''; | ||
curlHeaders = ""; | ||
// get the headers concerning the appropriate method (defined in the global axios instance) | ||
if (headers.hasOwnProperty('common')) { | ||
if (headers.hasOwnProperty("common")) { | ||
headers = this.request.headers[this.request.method]; | ||
} | ||
// add any custom headers (defined upon calling methods like .get(), .post(), etc.) | ||
for(let property in this.request.headers) { | ||
if(!['common', 'delete', 'get', 'head', 'patch', 'post', 'put'].includes(property)) { | ||
// add any custom headers (defined upon calling methods like .get(), .post(), etc.) | ||
for (let property in this.request.headers) { | ||
if ( | ||
!["common", "delete", "get", "head", "patch", "post", "put"].includes( | ||
property | ||
) | ||
) { | ||
headers[property] = this.request.headers[property]; | ||
@@ -25,3 +29,3 @@ } | ||
let header = `${property}:${headers[property]}`; | ||
curlHeaders = `${curlHeaders} -H "${header}"` | ||
curlHeaders = `${curlHeaders} -H "${header}"`; | ||
} | ||
@@ -37,7 +41,16 @@ | ||
getBody() { | ||
if((typeof this.request.data !== 'undefined') && this.request.data !== '' && Object.keys(this.request.data).length && this.request.method.toUpperCase() !== 'GET') { | ||
let data = (typeof this.request.data === 'object' || Object.prototype.toString.call(this.request.data) === '[object Array]') ? JSON.stringify(this.request.data) : this.request.data; | ||
if ( | ||
typeof this.request.data !== "undefined" && | ||
this.request.data !== "" && | ||
Object.keys(this.request.data).length && | ||
this.request.method.toUpperCase() !== "GET" | ||
) { | ||
let data = | ||
typeof this.request.data === "object" || | ||
Object.prototype.toString.call(this.request.data) === "[object Array]" | ||
? JSON.stringify(this.request.data) | ||
: this.request.data; | ||
return `--data '${data}'`.trim(); | ||
} else { | ||
return ''; | ||
return ""; | ||
} | ||
@@ -47,8 +60,36 @@ } | ||
getUrl() { | ||
return this.request.url.trim(); | ||
return this.request.url | ||
} | ||
getQueryString() { | ||
let params = "", | ||
i = 0; | ||
for (let param in this.request.params) { | ||
if (i != 0) { | ||
params += `&${param}=${this.request.params[param]}`; | ||
} else { | ||
params = `?${param}=${this.request.params[param]}`; | ||
} | ||
i++; | ||
} | ||
return params; | ||
} | ||
getBuiltURL() { | ||
let url = this.getUrl(); | ||
if (this.getQueryString() != "") { | ||
url = url.substr(0, url.length - 1) + this.getQueryString(); | ||
} | ||
return url.trim(); | ||
} | ||
generateCommand() { | ||
return `curl ${this.getMethod()} ${this.getHeaders()} ${this.getBody()} "${this.getUrl()}"`.trim().replace(/\s{2,}/g, ' '); | ||
return `curl ${this.getMethod()} ${this.getHeaders()} ${this.getBody()} "${this.getBuiltURL()}"` | ||
.trim() | ||
.replace(/\s{2,}/g, " "); | ||
} | ||
} |
@@ -70,2 +70,14 @@ import expect from 'expect'; | ||
}); | ||
it('should return the generated command with a queryString specified in the URL', done => { | ||
axios.post('http://localhost:7500/', {}, {params: {test: 1}}) | ||
.then(res => { | ||
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"'); | ||
done(); | ||
}) | ||
.catch(err => { | ||
console.error(err); | ||
}); | ||
}); | ||
}); | ||
@@ -86,3 +98,4 @@ | ||
url: 'http://localhost:7500/', | ||
data: { dummy: 'data' } | ||
data: { dummy: 'data' }, | ||
params: { testParam: 'test1', testParam_two: "test2"} | ||
} | ||
@@ -170,2 +183,7 @@ const curl = new CurlHelper(fakeConfig); | ||
}); | ||
it('should return the queryString of the request', done => { | ||
expect(curl.getQueryString()).toBe("?testParam=test1&testParam_two=test2"); | ||
done(); | ||
}); | ||
}); |
15946
283