Skip to content

Commit

Permalink
feat: add gotOptions constructor options
Browse files Browse the repository at this point in the history
This allows the user to pass optional options through to the `got` instance. For instance, users can now connect via proxies.

Here is an example using [`caw`](https://github.com/kevva/caw), as further described in [sindresorhus/got#435](sindresorhus/got#435 (comment)) and originally brought up in [sindresorhus/got#79](sindresorhus/got#79).

```js
import Cloudflare from 'cloudflare':
import caw from 'caw';

const cf = new Cloudflare({
  email: '[email protected]',
  key: '<your API key here>',
  gotOptions: {
    agent: caw({ protocol: 'https' }) // Cloudflare API is HTTPS only
  }
});
```
  • Loading branch information
buschtoens committed Dec 22, 2017
1 parent c3e1c83 commit 061c9a4
Show file tree
Hide file tree
Showing 5 changed files with 3,309 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ the [My Account][my-account] page in the Cloudflare dashboard.
```javascript
var cf = require('cloudflare')({
email: '[email protected]',
key: 'your Cloudflare API key'
key: 'your Cloudflare API key',
gotOptions: {} // options passed to `got`, like a HTTP agent for proxying
});
```

Expand Down Expand Up @@ -71,4 +72,4 @@ async function getZoneStatus(id) {
* `purgeCache(zoneId, params)`
* user
* `read()`
* `edit(params)`
* `edit(params)`
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ var resources = {
};

var Cloudflare = auto(prototypal({
constructor: function (auth) {
constructor: function (options) {
var client = new Client({
email: auth && auth.email,
key: auth && auth.key
email: options && options.email,
key: options && options.key,
gotOptions: options && options.gotOptions
});

Object.defineProperty(this, '_client', {
Expand Down
2 changes: 1 addition & 1 deletion lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = prototypal({
constructor: function (options) {
this.email = options.email;
this.key = options.key;
this.getter = new Getter(options);
this.getter = new Getter(options && options.gotOptions);
},
request: function (requestMethod, requestPath, data, opts) {
var uri = 'https://api.cloudflare.com/client/v4/' + requestPath;
Expand Down
5 changes: 4 additions & 1 deletion lib/Getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ var assign = require('object-assign');
var got = require('got');

module.exports = prototypal({
constructor: function (options) {
this.options = options || {};
},
got: function (uri, options) {
options = assign({}, options);
options = assign({}, this.options, options);

return got(uri, options);
}
Expand Down
Loading

0 comments on commit 061c9a4

Please sign in to comment.