Skip to content

Commit

Permalink
Merge pull request #38 from thib3113/master
Browse files Browse the repository at this point in the history
Add a function to clear the config object - Fix #37
  • Loading branch information
anthonygauthier authored Mar 22, 2021
2 parents 73300c4 + 112f2d5 commit ed9ea41
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ typings/
# next.js build output
.next

#jetbrains ide
.idea
8 changes: 8 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
src/test
dist/test
.circleci
.github
codecov.yml
.releaserc
.idea
*.tgz
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,16 @@ axios
console.log(err);
});
```

# Clear a request

```javascript
axios
.post('http://localhost:7500/', { dummy: 'data' })
.then(res => {
res.config.clearCurl();
})
.catch(err => {
console.log(err);
});
```
6 changes: 6 additions & 0 deletions dist/curlirize.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ exports.default = function (instance) {
var curl = new _CurlHelper.CurlHelper(req);
req.curlObject = curl;
req.curlCommand = curl.generateCommand();
req.clearCurl = function () {
delete req.curlObject;
delete req.curlCommand;
delete req.clearCurl;
};
} catch (err) {
// Even if the axios middleware is stopped, no error should occur outside.
console.log(err);
callback(null, err);
} finally {
if (req.curlirize !== false) {
Expand Down
4 changes: 1 addition & 3 deletions dist/lib/CurlHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ var CurlHelper = exports.CurlHelper = function () {
}, {
key: "getBody",
value: function getBody() {
if (typeof this.request.data !== "undefined" && this.request.data !== "" && this.request.data !== null &&
//Object.keys(this.request.data).length &&
this.request.method.toUpperCase() !== "GET") {
if (typeof this.request.data !== "undefined" && this.request.data !== "" && this.request.data !== null && this.request.method.toUpperCase() !== "GET") {
var 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 {
Expand Down
13 changes: 13 additions & 0 deletions dist/test/curlirize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ describe('Testing curlirize', function () {
console.error(err);
});
});
it('should allow to remove curlirize part on a request', function (done) {
_axios2.default.post('http://localhost:7500/', { dummy: 'data' }).then(function (res) {
(0, _expect2.default)(res.status).toBe(200);
(0, _expect2.default)(res.data.hello).toBe('world');
res.config.clearCurl();
(0, _expect2.default)(res.config.curlObject).not.toBeDefined();
(0, _expect2.default)(res.config.curlCommand).not.toBeDefined();
(0, _expect2.default)(res.config.clearCurl).not.toBeDefined();
done();
}).catch(function (err) {
console.error(err);
});
});

it('should return a generated command with XML as data', function (done) {
_axios2.default.post('http://localhost:7500', "<myTestTag></myTestTag>").then(function (res) {
Expand Down
5 changes: 5 additions & 0 deletions src/curlirize.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export default (instance, callback = defaultLogCallback) => {
const curl = new CurlHelper(req);
req.curlObject = curl;
req.curlCommand = curl.generateCommand();
req.clearCurl = () => {
delete req.curlObject;
delete req.curlCommand;
delete req.clearCurl;
}
} catch (err) {
// Even if the axios middleware is stopped, no error should occur outside.
callback(null, err);
Expand Down
31 changes: 23 additions & 8 deletions src/test/curlirize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,29 @@ curlirize(axios);
describe('Testing curlirize', () => {
it('should return a 200 with the value \'world\'', done => {
axios.post('http://localhost:7500/', { dummy: 'data' })
.then(res => {
expect(res.status).toBe(200);
expect(res.data.hello).toBe('world');
done();
})
.catch(err => {
console.error(err);
});
.then(res => {
expect(res.status).toBe(200);
expect(res.data.hello).toBe('world');
done();
})
.catch(err => {
console.error(err);
});
});
it('should allow to remove curlirize part on a request', done => {
axios.post('http://localhost:7500/', { dummy: 'data' })
.then(res => {
expect(res.status).toBe(200);
expect(res.data.hello).toBe('world');
res.config.clearCurl();
expect(res.config.curlObject).not.toBeDefined()
expect(res.config.curlCommand).not.toBeDefined()
expect(res.config.clearCurl).not.toBeDefined()
done();
})
.catch(err => {
console.error(err);
});
});

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

0 comments on commit ed9ea41

Please sign in to comment.