Skip to content

Commit

Permalink
fix(rest): httpAgent condition (#4484)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent committed Sep 4, 2024
1 parent 469d624 commit 3d42884
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
16 changes: 16 additions & 0 deletions docs/helpers/REST.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ Type: [object][4]
}
```

```js
{
helpers: {
REST: {
endpoint: 'http://site.com/api',
prettyPrintJson: true,
httpAgent: {
ca: fs.readFileSync(__dirname + '/path/to/ca.pem'),
rejectUnauthorized: false,
keepAlive: true
}
}
}
}
```

## Access From Helpers

Send REST requests by accessing `_executeRequest` method:
Expand Down
24 changes: 22 additions & 2 deletions lib/helper/REST.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ const config = {}
* }
* ```
*
* ```js
* {
* helpers: {
* REST: {
* endpoint: 'http://site.com/api',
* prettyPrintJson: true,
* httpAgent: {
* ca: fs.readFileSync(__dirname + '/path/to/ca.pem'),
* rejectUnauthorized: false,
* keepAlive: true
* }
* }
* }
* }
* ```
*
* ## Access From Helpers
*
* Send REST requests by accessing `_executeRequest` method:
Expand Down Expand Up @@ -101,9 +117,13 @@ class REST extends Helper {

// Create an agent with SSL certificate
if (this.options.httpAgent) {
if (!this.options.httpAgent.key || !this.options.httpAgent.cert)
// if one of those keys is there, all good to go
if (this.options.httpAgent.ca || this.options.httpAgent.key || this.options.httpAgent.cert) {
this.httpsAgent = new Agent(this.options.httpAgent)
} else {
// otherwise, throws an error of httpAgent config
throw Error('Please recheck your httpAgent config!')
this.httpsAgent = new Agent(this.options.httpAgent)
}
}

this.axios = this.httpsAgent ? axios.create({ httpsAgent: this.httpsAgent }) : axios.create()
Expand Down

0 comments on commit 3d42884

Please sign in to comment.