Skip to content

Commit

Permalink
Add docs for custom options
Browse files Browse the repository at this point in the history
Fixes #1794
  • Loading branch information
szmarczak committed Aug 2, 2021
1 parent fee9685 commit 6a44a81
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions documentation/tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,88 @@ const wrappedAgent = new TransformHeadersAgent(agent);

See [an example](examples/uppercase-headers.js).

### Custom options

Got v12 throws when an option does not exist. Therefore passing a top-level option such as:

```js
import got from 'got';

await got('https://example.com', {
foo: 'bar'
});
```

will throw. To prevent this, you need read the option in an `init` hook:

```js
import got from 'got';

const convertFoo = got.extend({
hooks: {
init: [
(rawOptions, options) => {
if ('foo' in rawOptions) {
options.context.foo = rawOptions.foo;
delete rawOptions.foo;
}
}
]
}
});

const instance = got.extend(convertFoo, {
hooks: {
beforeRequest: [
options => {
options.headers.foo = options.context.foo;
}
]
}
});

const {headers} = await instance('https://httpbin.org/anything', {foo: 'bar'}).json();
console.log(headers.Foo); //=> 'bar'
```

Eventually, you may want to create a catch-all instance:

```js
import got from 'got';

const catchAllOptions = got.extend({
hooks: {
init: [
(raw, options) => {
for (const key in raw) {
if (!(key in options)) {
options.context[key] = raw[key];
delete raw[key];
}
}
}
]
}
});

const instance = got.extend(catchAllOptions, {
hooks: {
beforeRequest: [
options => {
// All custom options will be visible under `options.context`
options.headers.foo = options.context.foo;
}
]
}
});

const {headers} = await instance('https://httpbin.org/anything', {foo: 'bar'}).json();
console.log(headers.Foo); //=> 'bar'
```

**Note:**
> - It's a good practice to perform the validation inside the `init` hook. You can safely throw when an option is unknown! Internally, Got uses the [`@sindresorhus/is`](https://github.com/sindresorhus/is) package.
### Electron `net` module is not supported

Got doesn't support the `electron.net` module. It's missing crucial APIs that are available in Node.js.\
Expand Down

0 comments on commit 6a44a81

Please sign in to comment.