|
2 | 2 |
|
3 | 3 | > Make calling REST APIs easier by creating niche-specific `got` instances.
|
4 | 4 |
|
5 |
| -#### got.create(settings) |
6 |
| - |
7 |
| -Example: [gh-got](https://github.com/sindresorhus/gh-got/blob/master/index.js) |
8 |
| - |
9 |
| -Configures a new `got` instance with the provided settings. You can access the resolved options with the `.defaults` property on the instance. |
10 |
| - |
11 |
| -**Note:** In contrast to [`got.extend()`](../readme.md#gotextendinstances), this method has no defaults. |
12 |
| - |
13 |
| -##### [options](readme.md#options) |
14 |
| - |
15 |
| -To inherit from the parent, set it to `got.defaults.options` or use [`got.mergeOptions(defaults.options, options)`](../readme.md#gotmergeoptionsparentoptions-newoptions).<br> |
16 |
| -**Note:** Avoid using [object spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals) as it doesn't work recursively. |
17 |
| - |
18 |
| -##### mutableDefaults |
19 |
| - |
20 |
| -Type: `boolean`<br> |
21 |
| -Default: `false` |
22 |
| - |
23 |
| -States if the defaults are mutable. It can be useful when you need to [update headers over time](readme.md#hooksafterresponse), for example, update an access token when it expires. |
24 |
| - |
25 |
| -##### handlers |
26 |
| - |
27 |
| -Type: `Function[]`<br> |
28 |
| -Default: `[]` |
29 |
| - |
30 |
| -An array of functions. You execute them directly by calling `got()`. They are some sort of "global hooks" - these functions are called first. The last handler (*it's hidden*) is either [`asPromise`](../source/as-promise.ts) or [`asStream`](../source/as-stream.ts), depending on the `options.stream` property. |
31 |
| - |
32 |
| -To inherit from the parent, set it as `got.defaults.handlers`.<br> |
33 |
| -To use the default handler, just omit specifying this. |
34 |
| - |
35 |
| -Each handler takes two arguments: |
36 |
| - |
37 |
| -###### [options](readme.md#options) |
38 |
| - |
39 |
| -**Note:** These options are [normalized](source/normalize-arguments.js). |
40 |
| - |
41 |
| -###### next() |
42 |
| - |
43 |
| -Returns a `Promise` or a `Stream` depending on [`options.stream`](readme.md#stream). |
44 |
| - |
45 |
| -```js |
46 |
| -const settings = { |
47 |
| - handlers: [ |
48 |
| - (options, next) => { |
49 |
| - if (options.stream) { |
50 |
| - // It's a Stream, so we can perform stream-specific actions on it |
51 |
| - return next(options) |
52 |
| - .on('request', request => { |
53 |
| - setTimeout(() => { |
54 |
| - request.abort(); |
55 |
| - }, 50); |
56 |
| - }); |
57 |
| - } |
58 |
| - |
59 |
| - // It's a Promise |
60 |
| - return next(options); |
61 |
| - } |
62 |
| - ], |
63 |
| - options: got.mergeOptions(got.defaults.options, { |
64 |
| - responseType: 'json' |
65 |
| - }) |
66 |
| -}; |
67 |
| - |
68 |
| -const jsonGot = got.create(settings); |
69 |
| -``` |
70 |
| - |
71 |
| -Sometimes you don't need to use `got.create(defaults)`. You should go for `got.extend(options)` if you don't want to overwrite the defaults: |
72 |
| - |
73 |
| -```js |
74 |
| -const settings = { |
75 |
| - handler: got.defaults.handler, |
76 |
| - options: got.mergeOptions(got.defaults.options, { |
77 |
| - headers: { |
78 |
| - unicorn: 'rainbow' |
79 |
| - } |
80 |
| - }) |
81 |
| -}; |
82 |
| - |
83 |
| -const unicorn = got.create(settings); |
84 |
| - |
85 |
| -// Same as: |
86 |
| -const unicorn = got.extend({headers: {unicorn: 'rainbow'}}); |
87 |
| -``` |
88 |
| - |
89 |
| -**Note:** Handlers can be asynchronous. The recommended approach is: |
90 |
| - |
91 |
| -```js |
92 |
| -const handler = (options, next) => { |
93 |
| - if (options.stream) { |
94 |
| - // It's a Stream |
95 |
| - return next(options); |
96 |
| - } |
97 |
| - |
98 |
| - // It's a Promise |
99 |
| - return (async () => { |
100 |
| - try { |
101 |
| - const response = await next(options); |
102 |
| - |
103 |
| - response.yourOwnProperty = true; |
104 |
| - |
105 |
| - return response; |
106 |
| - } catch (error) { |
107 |
| - // Every error will be replaced by this one. |
108 |
| - // Before you receive any error here, |
109 |
| - // it will be passed to the `beforeError` hooks first. |
110 |
| - |
111 |
| - // Note: this one won't be passed to `beforeError` hook. It's final. |
112 |
| - throw new Error('Your very own error.'); |
113 |
| - } |
114 |
| - })(); |
115 |
| -}; |
116 |
| -``` |
117 |
| - |
118 | 5 | ### Merging instances
|
119 | 6 |
|
120 | 7 | Got supports composing multiple instances together. This is very powerful. You can create a client that limits download speed and then compose it with an instance that signs a request. It's like plugins without any of the plugin mess. You just create instances and then compose them together.
|
|
0 commit comments