fetchache 0.1.4 → 0.1.5-alpha-20230320124321-e6b82e7
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 +38 -0
- package/{index.js → cjs/index.js} +8 -12
- package/cjs/package.json +1 -0
- package/{index.mjs → esm/index.js} +1 -4
- package/package.json +19 -12
- package/typings/index.d.cts +26 -0
- package/{index.d.ts → typings/index.d.ts} +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Fetchache
|
|
2
|
+
|
|
3
|
+
A fetch wrapper that allows you to respect HTTP caching strategies on non-browser environments with
|
|
4
|
+
a key-value cache implementation. It follows the [HTTP Caching](https://tools.ietf.org/html/rfc7234)
|
|
5
|
+
and [Conditional Requests](https://tools.ietf.org/html/rfc7232) standards.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
yarn add fetchache
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { fetchFactory } from 'fetchache'
|
|
17
|
+
import { fetch, Response } from 'some-fetch-impl'
|
|
18
|
+
|
|
19
|
+
// We recommend using `@whatwg-node/fetch`
|
|
20
|
+
|
|
21
|
+
const someCacheImpl = {
|
|
22
|
+
get: async key => {
|
|
23
|
+
// Get the cached value from your cache implementation
|
|
24
|
+
},
|
|
25
|
+
set: async (key, value) => {
|
|
26
|
+
// Set the cached value to your cache implementation
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const fetchWithCache = fetchFactory({
|
|
31
|
+
fetch,
|
|
32
|
+
Response,
|
|
33
|
+
cache
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
// Then you can use it like a normal fetch
|
|
37
|
+
const response = await fetchWithCache('https://example.com')
|
|
38
|
+
```
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const CachePolicy = _interopDefault(require('http-cache-semantics'));
|
|
8
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchFactory = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const http_cache_semantics_1 = tslib_1.__importDefault(require("http-cache-semantics"));
|
|
9
6
|
function fetchFactory({ fetch, Response, cache }) {
|
|
10
7
|
return async (input, init, ...rest) => {
|
|
11
8
|
let url;
|
|
@@ -28,11 +25,11 @@ function fetchFactory({ fetch, Response, cache }) {
|
|
|
28
25
|
const policyRequest = policyRequestFrom(url, method, headers);
|
|
29
26
|
if (!entry) {
|
|
30
27
|
const response = await fetch(input, init, ...rest);
|
|
31
|
-
const policy = new
|
|
28
|
+
const policy = new http_cache_semantics_1.default(policyRequest, policyResponseFrom(response));
|
|
32
29
|
return storeResponseAndReturnClone(cache, response, policy, cacheKey);
|
|
33
30
|
}
|
|
34
31
|
const { policy: policyRaw, bytes } = typeof entry === 'string' ? JSON.parse(entry) : entry;
|
|
35
|
-
const policy =
|
|
32
|
+
const policy = http_cache_semantics_1.default.fromObject(policyRaw);
|
|
36
33
|
// Remove url from the policy, because otherwise it would never match a request with a custom cache key
|
|
37
34
|
policy._url = undefined;
|
|
38
35
|
const bodyInit = new Uint8Array(bytes);
|
|
@@ -90,6 +87,7 @@ function fetchFactory({ fetch, Response, cache }) {
|
|
|
90
87
|
return new Response(uint8array, response);
|
|
91
88
|
}
|
|
92
89
|
}
|
|
90
|
+
exports.fetchFactory = fetchFactory;
|
|
93
91
|
function canBeRevalidated(response) {
|
|
94
92
|
return response.headers.has('ETag');
|
|
95
93
|
}
|
|
@@ -120,5 +118,3 @@ function headersToObject(headers) {
|
|
|
120
118
|
}
|
|
121
119
|
return object;
|
|
122
120
|
}
|
|
123
|
-
|
|
124
|
-
exports.fetchFactory = fetchFactory;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import CachePolicy from 'http-cache-semantics';
|
|
2
|
-
|
|
3
|
-
function fetchFactory({ fetch, Response, cache }) {
|
|
2
|
+
export function fetchFactory({ fetch, Response, cache }) {
|
|
4
3
|
return async (input, init, ...rest) => {
|
|
5
4
|
let url;
|
|
6
5
|
let method = 'GET';
|
|
@@ -114,5 +113,3 @@ function headersToObject(headers) {
|
|
|
114
113
|
}
|
|
115
114
|
return object;
|
|
116
115
|
}
|
|
117
|
-
|
|
118
|
-
export { fetchFactory };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fetchache",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5-alpha-20230320124321-e6b82e7",
|
|
4
4
|
"description": "Cross Platform Fetch Wrapper with Key Value Cache support",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"dependencies": {
|
|
@@ -14,21 +14,28 @@
|
|
|
14
14
|
},
|
|
15
15
|
"author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
|
|
16
16
|
"license": "MIT",
|
|
17
|
-
"main": "index.js",
|
|
18
|
-
"module": "index.
|
|
19
|
-
"typings": "index.d.ts",
|
|
17
|
+
"main": "cjs/index.js",
|
|
18
|
+
"module": "esm/index.js",
|
|
19
|
+
"typings": "typings/index.d.ts",
|
|
20
20
|
"typescript": {
|
|
21
|
-
"definition": "index.d.ts"
|
|
21
|
+
"definition": "typings/index.d.ts"
|
|
22
22
|
},
|
|
23
|
+
"type": "module",
|
|
23
24
|
"exports": {
|
|
24
25
|
".": {
|
|
25
|
-
"require":
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
|
|
26
|
+
"require": {
|
|
27
|
+
"types": "./typings/index.d.cts",
|
|
28
|
+
"default": "./cjs/index.js"
|
|
29
|
+
},
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./typings/index.d.ts",
|
|
32
|
+
"default": "./esm/index.js"
|
|
33
|
+
},
|
|
34
|
+
"default": {
|
|
35
|
+
"types": "./typings/index.d.ts",
|
|
36
|
+
"default": "./esm/index.js"
|
|
37
|
+
}
|
|
31
38
|
},
|
|
32
39
|
"./package.json": "./package.json"
|
|
33
40
|
}
|
|
34
|
-
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import CachePolicy from 'http-cache-semantics';
|
|
2
|
+
export interface FetchacheCacheEntry {
|
|
3
|
+
policy: CachePolicy.CachePolicyObject;
|
|
4
|
+
body: string;
|
|
5
|
+
}
|
|
6
|
+
type FetchFn = (input: URL | RequestInfo, init: RequestInit, ...rest: any[]) => Promise<Response>;
|
|
7
|
+
export interface FetchacheOptions {
|
|
8
|
+
fetch: FetchFn;
|
|
9
|
+
Request: typeof Request;
|
|
10
|
+
Response: typeof Response;
|
|
11
|
+
cache: KeyValueCache<FetchacheCacheEntry>;
|
|
12
|
+
}
|
|
13
|
+
export declare function fetchFactory({ fetch, Response, cache }: FetchacheOptions): FetchFn;
|
|
14
|
+
export interface KeyValueCacheSetOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Specified in **seconds**, the time-to-live (TTL) value limits the lifespan
|
|
17
|
+
* of the data being stored in the cache.
|
|
18
|
+
*/
|
|
19
|
+
ttl?: number | null;
|
|
20
|
+
}
|
|
21
|
+
export interface KeyValueCache<V = any> {
|
|
22
|
+
get(key: string): Promise<V | undefined>;
|
|
23
|
+
set(key: string, value: V, options?: KeyValueCacheSetOptions): Promise<void>;
|
|
24
|
+
delete(key: string): Promise<boolean | void>;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
|
@@ -3,7 +3,7 @@ export interface FetchacheCacheEntry {
|
|
|
3
3
|
policy: CachePolicy.CachePolicyObject;
|
|
4
4
|
body: string;
|
|
5
5
|
}
|
|
6
|
-
|
|
6
|
+
type FetchFn = (input: URL | RequestInfo, init: RequestInit, ...rest: any[]) => Promise<Response>;
|
|
7
7
|
export interface FetchacheOptions {
|
|
8
8
|
fetch: FetchFn;
|
|
9
9
|
Request: typeof Request;
|