From 15a87fac5a4babebc4065144c93ddb9868648f65 Mon Sep 17 00:00:00 2001 From: Randall Schmidt Date: Fri, 17 Apr 2020 16:56:05 -0400 Subject: [PATCH] write readme --- README.md | 46 +++++++++++++++++++++++++++++++++++++++++++++- index.js | 2 +- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 45ead78..25fbac0 100644 --- a/README.md +++ b/README.md @@ -1 +1,45 @@ -# node-fetch-cache \ No newline at end of file +# node-fetch-cache + +node-fetch with caching to a directory on disk. + +The first request will result in an HTTP request and any subsequent requests with the same arguments and body function (text, json, buffer, or textConverted) will read the response body from the cache on disk. + +## Usage + +Use it the same way you would use fetch. + +```js +const fetch = require('node-fetch-cache')('./path/to/cache/dir'); + +fetch('http://google.com') + .then(response => response.text()) + .then(text => console.log(text)); +``` + +## API + +Note that this is not the full fetch API. Headers and some other things are not accessible. + +### async fetch(resource [, init]) + +Same calling conventions as [browser fetch](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch). + +Returns a **CachedResponse**. + +### async CachedResponse.text() + +Returns the body as a string. + +### async CachedResponse.json() + +Returns the body as a JavaScript object, parsed from JSON. + +### async CachedResponse.buffer() + +Returns the body as a Buffer. + +### async CachedResponse.textConverted() + +Identical to CachedResponse.text(), except instead of always converting to UTF-8, encoding sniffing will be performed and text converted to UTF-8, if possible. + +(This API requires an optional dependency on [npm package encoding](https://www.npmjs.com/package/encoding), which you need to install manually. \ No newline at end of file diff --git a/index.js b/index.js index 9f96b88..6b8be58 100644 --- a/index.js +++ b/index.js @@ -55,7 +55,7 @@ function createFetch(cacheDirPath) { return async (...args) => { if (!madeDir) { try { - await fs.promises.mkdir(cacheDirPath); + await fs.promises.mkdir(cacheDirPath, { recursive: true }); } catch (err) { // Ignore. }