Skip to content

Commit 87cf0f1

Browse files
committed
ignore file to npm + add way to clear the config object
1 parent 73300c4 commit 87cf0f1

9 files changed

+75
-12
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ typings/
6161
# next.js build output
6262
.next
6363

64+
#jetbrains ide
65+
.idea

.npmignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
src/test
2+
dist/test
3+
.circleci
4+
.github
5+
codecov.yml
6+
.releaserc
7+
.idea
8+
*.tgz

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,18 @@ axios
9292
console.log(err);
9393
});
9494
```
95+
96+
# Clear a request
97+
98+
```javascript
99+
axios
100+
.post('http://localhost:7500/', { dummy: 'data' }, {
101+
curlirize: false
102+
})
103+
.then(res => {
104+
res.config.clearCurl();
105+
})
106+
.catch(err => {
107+
console.log(err);
108+
});
109+
```

dist/curlirize.js

+6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ exports.default = function (instance) {
2424
var curl = new _CurlHelper.CurlHelper(req);
2525
req.curlObject = curl;
2626
req.curlCommand = curl.generateCommand();
27+
req.clearCurl = function () {
28+
delete req.curlObject;
29+
delete req.curlCommand;
30+
delete req.clearCurl;
31+
};
2732
} catch (err) {
2833
// Even if the axios middleware is stopped, no error should occur outside.
34+
console.log(err);
2935
callback(null, err);
3036
} finally {
3137
if (req.curlirize !== false) {

dist/lib/CurlHelper.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ var CurlHelper = exports.CurlHelper = function () {
5050
}, {
5151
key: "getBody",
5252
value: function getBody() {
53-
if (typeof this.request.data !== "undefined" && this.request.data !== "" && this.request.data !== null &&
54-
//Object.keys(this.request.data).length &&
55-
this.request.method.toUpperCase() !== "GET") {
53+
if (typeof this.request.data !== "undefined" && this.request.data !== "" && this.request.data !== null && this.request.method.toUpperCase() !== "GET") {
5654
var data = _typeof(this.request.data) === "object" || Object.prototype.toString.call(this.request.data) === "[object Array]" ? JSON.stringify(this.request.data) : this.request.data;
5755
return ("--data '" + data + "'").trim();
5856
} else {

dist/test/curlirize.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ describe('Testing curlirize', function () {
3030
console.error(err);
3131
});
3232
});
33+
it('should allow to remove curlirize part on a request', function (done) {
34+
_axios2.default.post('http://localhost:7500/', { dummy: 'data' }).then(function (res) {
35+
(0, _expect2.default)(res.status).toBe(200);
36+
(0, _expect2.default)(res.data.hello).toBe('world');
37+
res.config.clearCurl();
38+
(0, _expect2.default)(res.config.curlObject).not.toBeDefined();
39+
(0, _expect2.default)(res.config.curlCommand).not.toBeDefined();
40+
(0, _expect2.default)(res.config.clearCurl).not.toBeDefined();
41+
done();
42+
}).catch(function (err) {
43+
console.error(err);
44+
});
45+
});
3346

3447
it('should return a generated command with XML as data', function (done) {
3548
_axios2.default.post('http://localhost:7500', "<myTestTag></myTestTag>").then(function (res) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "./node_modules/.bin/babel ./src --experimental --source-maps-inline -d ./dist && export NODE_ENV=test || SET \"NODE_ENV=test\" && mocha --exit dist/test/*.test.js",
8-
"prepare": "if [ -e ./node_modules/.bin/babel ]; then ./node_modules/.bin/babel ./src --experimental --source-maps-inline -d ./dist; fi"
8+
"prepare": "babel ./src --experimental --source-maps-inline -d ./dist"
99
},
1010
"repository": {
1111
"type": "git",

src/curlirize.js

+6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ export default (instance, callback = defaultLogCallback) => {
1515
const curl = new CurlHelper(req);
1616
req.curlObject = curl;
1717
req.curlCommand = curl.generateCommand();
18+
req.clearCurl = () => {
19+
delete req.curlObject;
20+
delete req.curlCommand;
21+
delete req.clearCurl;
22+
}
1823
} catch (err) {
1924
// Even if the axios middleware is stopped, no error should occur outside.
25+
console.log(err);
2026
callback(null, err);
2127
} finally {
2228
if (req.curlirize !== false) {

src/test/curlirize.test.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,29 @@ curlirize(axios);
1010
describe('Testing curlirize', () => {
1111
it('should return a 200 with the value \'world\'', done => {
1212
axios.post('http://localhost:7500/', { dummy: 'data' })
13-
.then(res => {
14-
expect(res.status).toBe(200);
15-
expect(res.data.hello).toBe('world');
16-
done();
17-
})
18-
.catch(err => {
19-
console.error(err);
20-
});
13+
.then(res => {
14+
expect(res.status).toBe(200);
15+
expect(res.data.hello).toBe('world');
16+
done();
17+
})
18+
.catch(err => {
19+
console.error(err);
20+
});
21+
});
22+
it('should allow to remove curlirize part on a request', done => {
23+
axios.post('http://localhost:7500/', { dummy: 'data' })
24+
.then(res => {
25+
expect(res.status).toBe(200);
26+
expect(res.data.hello).toBe('world');
27+
res.config.clearCurl();
28+
expect(res.config.curlObject).not.toBeDefined()
29+
expect(res.config.curlCommand).not.toBeDefined()
30+
expect(res.config.clearCurl).not.toBeDefined()
31+
done();
32+
})
33+
.catch(err => {
34+
console.error(err);
35+
});
2136
});
2237

2338
it('should return a generated command with XML as data', done => {

0 commit comments

Comments
 (0)