You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+68Lines changed: 68 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,7 @@ Created because [`request`](https://github.com/request/request) is bloated *(sev
19
19
20
20
-[Promise & stream API](#api)
21
21
-[Request cancelation](#aborting-the-request)
22
+
-[RFC compliant caching](#cache-adapters)
22
23
-[Follows redirects](#followredirect)
23
24
-[Retries on network failure](#retries)
24
25
-[Progress events](#onuploadprogress-progress)
@@ -69,6 +70,10 @@ It's a `GET` request by default, but can be changed in `options`.
69
70
70
71
Returns a Promise for a `response` object with a `body` property, a `url` property with the request URL or the final URL after redirects, and a `requestUrl` property with the original request URL.
71
72
73
+
The response object will normally be a [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage), however if returned from the cache it will be a [responselike object](https://github.com/lukechilds/responselike) which behaves in the same way.
74
+
75
+
The response will also have a `fromCache` property set with a boolean value.
76
+
72
77
##### url
73
78
74
79
Type: `string``Object`
@@ -170,6 +175,13 @@ Decompress the response automatically.
170
175
171
176
If this is disabled, a compressed response is returned as a `Buffer`. This may be useful if you want to handle decompression yourself or stream the raw compressed data.
172
177
178
+
###### cache
179
+
180
+
Type: `Object`<br>
181
+
Default: `false`
182
+
183
+
[Cache adapter instance](#cache-adapters) for storing cached data.
In Promise mode, the `response` is attached to the error.
255
267
268
+
#### got.CacheError
269
+
270
+
When a cache method fails, for example if the database goes down, or there's a filesystem error.
271
+
256
272
#### got.RequestError
257
273
258
274
When a request fails. Contains a `code` property with error class code, like `ECONNREFUSED`.
@@ -316,6 +332,58 @@ request.catch(err => {
316
332
request.cancel();
317
333
```
318
334
335
+
<aname="cache-adapters"></a>
336
+
## Cache
337
+
338
+
You can use the JavaScript `Map` type as an in memory cache:
339
+
340
+
```js
341
+
constgot=require('got');
342
+
constmap=newMap();
343
+
344
+
(async () => {
345
+
let response =awaitgot('todomvc.com', {cache: map});
346
+
console.log(response.fromCache);
347
+
//=> false
348
+
349
+
response =awaitgot('todomvc.com', {cache: map});
350
+
console.log(response.fromCache);
351
+
//=> true
352
+
})();
353
+
```
354
+
355
+
Got uses [Keyv](https://github.com/lukechilds/keyv) internally to support a wide range of storage adapters. For something more scalable you could use an [official Keyv storage adapter](https://github.com/lukechilds/keyv#official-storage-adapters):
View the [Keyv docs](https://github.com/lukechilds/keyv) for more information on how to use storage adapters.
0 commit comments