@kisaragi-hiu/cached-fetch 0.3.0 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -29
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -17,34 +17,29 @@ const text = await cached(
|
|
|
17
17
|
|
|
18
18
|
This persists the text response in `os.tmpdir` and should stay around until the next reboot.
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
```typescript
|
|
23
|
-
/**
|
|
24
|
-
* The key of the value.
|
|
25
|
-
* Subsequent calls with the same key will return the cached value.
|
|
26
|
-
*/
|
|
27
|
-
key: string,
|
|
28
|
-
/**
|
|
29
|
-
* A fetcher function, called when the value isn't cached.
|
|
30
|
-
*
|
|
31
|
-
* Should return a promise, which resolves to an object whose text key is a
|
|
32
|
-
* Promise<string> --- usually a Response, but the ProcessPromise from the
|
|
33
|
-
* zx library are also supported.
|
|
34
|
-
*/
|
|
35
|
-
fetcher: () => Promise<{ text: () => Promise<string> }>
|
|
36
|
-
```
|
|
20
|
+
Signature: `cached(key, fetcher)`
|
|
37
21
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
This also works:
|
|
41
|
-
|
|
42
|
-
```typescript
|
|
43
|
-
import { $ } from "zx";
|
|
44
|
-
import { cached } from "@kisaragi-hiu/cached-fetch";
|
|
22
|
+
Arguments:
|
|
45
23
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
() =>
|
|
49
|
-
|
|
50
|
-
|
|
24
|
+
- `key`: the key of the value. Subsequent calls with the same key will return the cached value.
|
|
25
|
+
- `fetcher`: a function doing work that should be cached. This should return any of these:
|
|
26
|
+
- `Promise<{text: () => string | Promise<string>}`, a promise of an object whose “text” property is a function returning a string or a promise of a string.
|
|
27
|
+
|
|
28
|
+
This covers both a `Promise<Response>` from `fetch`, and also `ProcessPromise` from `zx`. So this works:
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
const value = await cached("key1", () => fetch("https://example.com"))
|
|
32
|
+
|
|
33
|
+
import {$} from "zx"
|
|
34
|
+
const output = await cached("key2", () => $`sleep 1 && echo "slow process demo"`)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
- a `Promise<string>`
|
|
38
|
+
|
|
39
|
+
- a string. In this case the whole call is sync.
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
const output = cached("key4", () =>
|
|
43
|
+
spawnSync("ls", { stdio: "pipe", encoding: "utf-8" }).stdout,
|
|
44
|
+
);
|
|
45
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kisaragi-hiu/cached-fetch",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "A cache in OS tmpdir useful for fetch()",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
"author": "Kisaragi Hiu <mail@kisaragi-hiu.com>",
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"files": ["index.js", "index.d.ts"],
|
|
18
|
+
"main": "index.js",
|
|
19
|
+
"types": "index.d.ts",
|
|
18
20
|
"devDependencies": {
|
|
19
21
|
"@typescript/native-preview": "^7.0.0-dev.20250822.1"
|
|
20
22
|
},
|