@kisaragi-hiu/cached-fetch 0.1.1 → 0.2.0
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/index.d.ts +20 -0
- package/index.js +12 -6
- package/package.json +2 -2
package/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A cache designed to cache some text.
|
|
3
|
+
*/
|
|
4
|
+
export declare function cached(
|
|
5
|
+
/**
|
|
6
|
+
* The key of the value.
|
|
7
|
+
* Subsequent calls with the same key will return the cached value.
|
|
8
|
+
*/
|
|
9
|
+
key: string,
|
|
10
|
+
/**
|
|
11
|
+
* A fetcher function, called when the value isn't cached.
|
|
12
|
+
*
|
|
13
|
+
* This can return a string or a Promise<string>.
|
|
14
|
+
*
|
|
15
|
+
* This can also return a Promise<{ text: () => string | Promise<string> }>.
|
|
16
|
+
* This means a Response or zx's ProcessOutput can be used directly.
|
|
17
|
+
*/
|
|
18
|
+
fetcher: () => string | Promise<string> | Promise<{
|
|
19
|
+
text: () => string | Promise<string>;
|
|
20
|
+
}>): Promise<string>;
|
package/index.js
CHANGED
|
@@ -14,16 +14,22 @@ key,
|
|
|
14
14
|
/**
|
|
15
15
|
* A fetcher function, called when the value isn't cached.
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
17
|
+
* This can return a string or a Promise<string>.
|
|
18
|
+
*
|
|
19
|
+
* This can also return a Promise<{ text: () => string | Promise<string> }>.
|
|
20
|
+
* This means a Response or zx's ProcessOutput can be used directly.
|
|
20
21
|
*/
|
|
21
22
|
fetcher) {
|
|
22
23
|
const cacheFile = join(tmpdir(), key);
|
|
23
24
|
const cacheHit = existsSync(cacheFile);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
let text;
|
|
26
|
+
if (cacheHit) {
|
|
27
|
+
text = readFileSync(cacheFile, { encoding: "utf-8" });
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
const result = await fetcher();
|
|
31
|
+
text = typeof result === "string" ? result : await result.text();
|
|
32
|
+
}
|
|
27
33
|
if (!cacheHit) {
|
|
28
34
|
mkdirSync(dirname(cacheFile), { recursive: true });
|
|
29
35
|
writeFileSync(cacheFile, text, { encoding: "utf-8" });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kisaragi-hiu/cached-fetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A cache in OS tmpdir useful for fetch()",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"homepage": "https://github.com/kisaragi-hiu/cached-fetch",
|
|
15
15
|
"author": "Kisaragi Hiu <mail@kisaragi-hiu.com>",
|
|
16
16
|
"license": "MIT",
|
|
17
|
-
"files": ["index.js"],
|
|
17
|
+
"files": ["index.js", "index.d.ts"],
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@typescript/native-preview": "^7.0.0-dev.20250822.1"
|
|
20
20
|
},
|