@blaze-cache/axios 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/dist/adapter.d.ts +34 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +130 -0
- package/dist/adapter.js.map +1 -0
- package/dist/cache-control.d.ts +18 -0
- package/dist/cache-control.d.ts.map +1 -0
- package/dist/cache-control.js +29 -0
- package/dist/cache-control.js.map +1 -0
- package/dist/headers.d.ts +19 -0
- package/dist/headers.d.ts.map +1 -0
- package/dist/headers.js +39 -0
- package/dist/headers.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/key.d.ts +12 -0
- package/dist/key.d.ts.map +1 -0
- package/dist/key.js +27 -0
- package/dist/key.js.map +1 -0
- package/dist/types.d.ts +81 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +50 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Blaze Cache
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AxiosAdapter, AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import type { BlazeAxiosCacheOptions } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Wraps an axios adapter with a Blaze Cache layer.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { blazeAdapter } from '@blaze-cache/axios';
|
|
8
|
+
*
|
|
9
|
+
* const api = axios.create({ adapter: blazeAdapter({ cache }) });
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* For an existing instance prefer {@link installBlazeCache}, which guarantees the wrapper
|
|
13
|
+
* captures the *previous* adapter instead of itself.
|
|
14
|
+
*/
|
|
15
|
+
export declare function blazeAdapter(options: BlazeAxiosCacheOptions): AxiosAdapter;
|
|
16
|
+
/**
|
|
17
|
+
* Installs the Blaze Cache adapter on an axios instance.
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { installBlazeCache } from '@blaze-cache/axios';
|
|
21
|
+
*
|
|
22
|
+
* installBlazeCache(api, { cache }); // GET responses stay fresh for 60s
|
|
23
|
+
* installBlazeCache(api, { cache, ttlMs: 5_000 });
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* The adapter currently in `instance.defaults` is captured **before** it is replaced, so the
|
|
27
|
+
* wrapper always delegates to a real network adapter and can never recurse into itself.
|
|
28
|
+
*/
|
|
29
|
+
export declare function installBlazeCache(instance: AxiosInstance, options: InstallBlazeCacheOptions): void;
|
|
30
|
+
/** {@link BlazeAxiosCacheOptions} where `adapter` may also be an axios adapter *name*. */
|
|
31
|
+
export type InstallBlazeCacheOptions = Omit<BlazeAxiosCacheOptions, 'adapter'> & {
|
|
32
|
+
adapter?: AxiosRequestConfig['adapter'];
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,kBAAkB,EAGnB,MAAM,OAAO,CAAC;AAKf,OAAO,KAAK,EAGV,sBAAsB,EAEvB,MAAM,YAAY,CAAC;AAWpB;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,sBAAsB,GAAG,YAAY,CAkD1E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,aAAa,EACvB,OAAO,EAAE,wBAAwB,GAChC,IAAI,CAMN;AAED,0FAA0F;AAC1F,MAAM,MAAM,wBAAwB,GAAG,IAAI,CAAC,sBAAsB,EAAE,SAAS,CAAC,GAAG;IAC/E,OAAO,CAAC,EAAE,kBAAkB,CAAC,SAAS,CAAC,CAAC;CACzC,CAAC"}
|
package/dist/adapter.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { AxiosError, getAdapter } from 'axios';
|
|
2
|
+
import { parseCacheControl } from './cache-control.js';
|
|
3
|
+
import { defaultCacheKey } from './key.js';
|
|
4
|
+
import { readHeader, toAxiosHeaders, toPlainHeaders } from './headers.js';
|
|
5
|
+
/** Freshness window when neither the option nor `Cache-Control` says otherwise. */
|
|
6
|
+
const DEFAULT_TTL_MS = 60_000;
|
|
7
|
+
/** Headers whose values split the cache unless the caller says otherwise. */
|
|
8
|
+
const DEFAULT_VARY_HEADERS = ['authorization'];
|
|
9
|
+
const HIT = 'HIT';
|
|
10
|
+
const MISS = 'MISS';
|
|
11
|
+
/**
|
|
12
|
+
* Wraps an axios adapter with a Blaze Cache layer.
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { blazeAdapter } from '@blaze-cache/axios';
|
|
16
|
+
*
|
|
17
|
+
* const api = axios.create({ adapter: blazeAdapter({ cache }) });
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* For an existing instance prefer {@link installBlazeCache}, which guarantees the wrapper
|
|
21
|
+
* captures the *previous* adapter instead of itself.
|
|
22
|
+
*/
|
|
23
|
+
export function blazeAdapter(options) {
|
|
24
|
+
const cache = options.cache;
|
|
25
|
+
const varyHeaders = options.varyHeaders ?? DEFAULT_VARY_HEADERS;
|
|
26
|
+
const markerHeader = options.markerHeader === undefined ? 'x-blaze-cache' : options.markerHeader;
|
|
27
|
+
const network = options.adapter ?? getAdapter(['xhr', 'http', 'fetch']);
|
|
28
|
+
const keyOf = (config) => options.key ? options.key(config) : defaultCacheKey(config, varyHeaders);
|
|
29
|
+
const shouldCache = options.shouldCache ?? defaultShouldCache;
|
|
30
|
+
return async function blazeCacheAdapter(config) {
|
|
31
|
+
const requestControl = parseCacheControl(readHeader(config.headers, 'Cache-Control'));
|
|
32
|
+
// `no-store`: neither read nor write. `no-cache`: may store, but must not serve blindly.
|
|
33
|
+
const skipWrite = requestControl.noStore;
|
|
34
|
+
const skipRead = requestControl.noStore || requestControl.noCache;
|
|
35
|
+
const key = keyOf(config);
|
|
36
|
+
if (!skipRead) {
|
|
37
|
+
const record = cache.get(key);
|
|
38
|
+
if (record) {
|
|
39
|
+
return settle(cachedResponse(record, config, markerHeader), config);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const response = await network(config);
|
|
43
|
+
if (markerHeader) {
|
|
44
|
+
response.headers = toAxiosHeaders(response.headers).set(markerHeader, MISS);
|
|
45
|
+
}
|
|
46
|
+
if (!skipWrite && shouldCache(response, config)) {
|
|
47
|
+
const control = parseCacheControl(readHeader(response.headers, 'Cache-Control'));
|
|
48
|
+
const ttlMs = control.maxAgeMs ?? resolveTtl(options.ttlMs, response, config);
|
|
49
|
+
// `max-age=0` (or a non-positive TTL) means "do not keep this".
|
|
50
|
+
if (!(control.noStore || control.noCache) && ttlMs > 0) {
|
|
51
|
+
const method = (config.method ?? 'get').toLowerCase();
|
|
52
|
+
const record = {
|
|
53
|
+
status: response.status,
|
|
54
|
+
statusText: response.statusText,
|
|
55
|
+
headers: toPlainHeaders(response.headers),
|
|
56
|
+
// Raw on purpose: axios runs `transformResponse` on replay, exactly as it would
|
|
57
|
+
// for a fresh network response.
|
|
58
|
+
data: response.data,
|
|
59
|
+
storedAt: Date.now(),
|
|
60
|
+
};
|
|
61
|
+
const tags = options.tags?.(response, config) ?? ['axios', `${method} ${config.url ?? ''}`];
|
|
62
|
+
cache.set(key, record, { ttlMs, tags });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return response;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Installs the Blaze Cache adapter on an axios instance.
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { installBlazeCache } from '@blaze-cache/axios';
|
|
73
|
+
*
|
|
74
|
+
* installBlazeCache(api, { cache }); // GET responses stay fresh for 60s
|
|
75
|
+
* installBlazeCache(api, { cache, ttlMs: 5_000 });
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* The adapter currently in `instance.defaults` is captured **before** it is replaced, so the
|
|
79
|
+
* wrapper always delegates to a real network adapter and can never recurse into itself.
|
|
80
|
+
*/
|
|
81
|
+
export function installBlazeCache(instance, options) {
|
|
82
|
+
const { adapter, ...rest } = options;
|
|
83
|
+
instance.defaults.adapter = blazeAdapter({
|
|
84
|
+
...rest,
|
|
85
|
+
adapter: adapter === undefined ? resolveAdapter(instance.defaults.adapter) : resolveAdapter(adapter),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function resolveAdapter(adapter) {
|
|
89
|
+
if (typeof adapter === 'function')
|
|
90
|
+
return adapter;
|
|
91
|
+
return getAdapter(adapter ?? ['xhr', 'http', 'fetch']);
|
|
92
|
+
}
|
|
93
|
+
function defaultShouldCache(response, config) {
|
|
94
|
+
const method = (config.method ?? 'get').toLowerCase();
|
|
95
|
+
return method === 'get' && response.status >= 200 && response.status < 300;
|
|
96
|
+
}
|
|
97
|
+
function resolveTtl(ttl, response, config) {
|
|
98
|
+
if (typeof ttl === 'function')
|
|
99
|
+
return ttl(response, config);
|
|
100
|
+
return ttl ?? DEFAULT_TTL_MS;
|
|
101
|
+
}
|
|
102
|
+
/** Rebuilds an `AxiosResponse` from a stored record, without touching hit/miss statistics. */
|
|
103
|
+
function cachedResponse(record, config, markerHeader) {
|
|
104
|
+
const headers = toAxiosHeaders(record.headers);
|
|
105
|
+
if (markerHeader)
|
|
106
|
+
headers.set(markerHeader, HIT);
|
|
107
|
+
return {
|
|
108
|
+
data: record.data,
|
|
109
|
+
status: record.status,
|
|
110
|
+
statusText: record.statusText,
|
|
111
|
+
headers,
|
|
112
|
+
config,
|
|
113
|
+
request: undefined,
|
|
114
|
+
fromBlazeCache: true,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Applies the request's `validateStatus` to a replayed response.
|
|
119
|
+
*
|
|
120
|
+
* A cached `404` must reject exactly like a network `404` would, even if the record was
|
|
121
|
+
* written under a more permissive `validateStatus`.
|
|
122
|
+
*/
|
|
123
|
+
function settle(response, config) {
|
|
124
|
+
const validateStatus = config.validateStatus;
|
|
125
|
+
if (!validateStatus || validateStatus(response.status)) {
|
|
126
|
+
return Promise.resolve(response);
|
|
127
|
+
}
|
|
128
|
+
return Promise.reject(new AxiosError(`Request failed with status code ${response.status}`, response.status >= 500 ? AxiosError.ERR_BAD_RESPONSE : AxiosError.ERR_BAD_REQUEST, config, undefined, response));
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAS/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAQ1E,mFAAmF;AACnF,MAAM,cAAc,GAAG,MAAM,CAAC;AAE9B,6EAA6E;AAC7E,MAAM,oBAAoB,GAAsB,CAAC,eAAe,CAAC,CAAC;AAElE,MAAM,GAAG,GAAG,KAAK,CAAC;AAClB,MAAM,IAAI,GAAG,MAAM,CAAC;AAEpB;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,YAAY,CAAC,OAA+B;IAC1D,MAAM,KAAK,GAAwB,OAAO,CAAC,KAAK,CAAC;IACjD,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,oBAAoB,CAAC;IAChE,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC;IACjG,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,UAAU,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACxE,MAAM,KAAK,GAAG,CAAC,MAAkC,EAAU,EAAE,CAC3D,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IAC3E,MAAM,W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OAAO,CAAC,QAAQ,CAAC,CAAC;IACnC,CAAC;IACD,OAAO,OAAO,CAAC,MAAM,CACnB,IAAI,UAAU,CACZ,mCAAmC,QAAQ,CAAC,MAAM,EAAE,EACpD,QAAQ,CAAC,MAAM,IAAI,GAAG,CAAC,CAAC,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC,CAAC,UAAU,CAAC,eAAe,EACjF,MAAM,EACN,SAAS,EACT,QAAQ,CACT,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** What `Cache-Control` says about a request or a response. */
|
|
2
|
+
export interface CacheControl {
|
|
3
|
+
/** `no-store` — neither read nor write the cache. */
|
|
4
|
+
noStore: boolean;
|
|
5
|
+
/** `no-cache` — must not be served without revalidating. */
|
|
6
|
+
noCache: boolean;
|
|
7
|
+
/** `max-age=<seconds>`, converted to milliseconds. */
|
|
8
|
+
maxAgeMs: number | undefined;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Minimal `Cache-Control` parser — only the directives a client-side cache must honour.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* parseCacheControl('max-age=30, public').maxAgeMs; // 30_000
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseCacheControl(value: string | undefined | null): CacheControl;
|
|
18
|
+
//# sourceMappingURL=cache-control.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache-control.d.ts","sourceRoot":"","sources":["../src/cache-control.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,MAAM,WAAW,YAAY;IAC3B,qDAAqD;IACrD,OAAO,EAAE,OAAO,CAAC;IACjB,4DAA4D;IAC5D,OAAO,EAAE,OAAO,CAAC;IACjB,sDAAsD;IACtD,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;CAC9B;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,YAAY,CAkBhF"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal `Cache-Control` parser — only the directives a client-side cache must honour.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* parseCacheControl('max-age=30, public').maxAgeMs; // 30_000
|
|
6
|
+
* ```
|
|
7
|
+
*/
|
|
8
|
+
export function parseCacheControl(value) {
|
|
9
|
+
const result = { noStore: false, noCache: false, maxAgeMs: undefined };
|
|
10
|
+
if (!value)
|
|
11
|
+
return result;
|
|
12
|
+
for (const rawDirective of value.split(',')) {
|
|
13
|
+
const directive = rawDirective.trim().toLowerCase();
|
|
14
|
+
if (directive === 'no-store') {
|
|
15
|
+
result.noStore = true;
|
|
16
|
+
}
|
|
17
|
+
else if (directive === 'no-cache') {
|
|
18
|
+
result.noCache = true;
|
|
19
|
+
}
|
|
20
|
+
else if (directive.startsWith('max-age=')) {
|
|
21
|
+
const seconds = Number(directive.slice('max-age='.length));
|
|
22
|
+
if (Number.isFinite(seconds) && seconds >= 0) {
|
|
23
|
+
result.maxAgeMs = seconds * 1000;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=cache-control.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache-control.js","sourceRoot":"","sources":["../src/cache-control.ts"],"names":[],"mappings":"AAUA;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAgC;IAChE,MAAM,MAAM,GAAiB,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC;IACrF,IAAI,CAAC,KAAK;QAAE,OAAO,MAAM,CAAC;IAE1B,KAAK,MAAM,YAAY,IAAI,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QAC5C,MAAM,SAAS,GAAG,YAAY,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;QACpD,IAAI,SAAS,KAAK,UAAU,EAAE,CAAC;YAC7B,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC;QACxB,CAAC;aAAM,IAAI,SAAS,KAAK,UAAU,EAAE,CAAC;YACpC,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC;QACxB,CAAC;aAAM,IAAI,SAAS,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;YAC5C,MAAM,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;YAC3D,IAAI,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,OAAO,IAAI,CAAC,EAAE,CAAC;gBAC7C,MAAM,CAAC,QAAQ,GAAG,OAAO,GAAG,IAAI,CAAC;YACnC,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { AxiosHeaders } from 'axios';
|
|
2
|
+
/**
|
|
3
|
+
* Case-insensitive header lookup that works with both `AxiosHeaders` and plain objects.
|
|
4
|
+
*
|
|
5
|
+
* Adapters see `config.headers` as an `AxiosHeaders` instance, while stored responses are
|
|
6
|
+
* plain objects — this normalises over both.
|
|
7
|
+
*/
|
|
8
|
+
export declare function readHeader(source: unknown, name: string): string | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Builds an `AxiosHeaders` from whatever axios handed us.
|
|
11
|
+
*
|
|
12
|
+
* The `AxiosHeaders` constructor types are narrower than what adapters actually receive
|
|
13
|
+
* (`AxiosResponseHeaders` is a union that does not overlap cleanly), so the cast lives here,
|
|
14
|
+
* in one documented place, instead of at every call site.
|
|
15
|
+
*/
|
|
16
|
+
export declare function toAxiosHeaders(headers: unknown): AxiosHeaders;
|
|
17
|
+
/** Copies headers into a plain object suitable for storage. */
|
|
18
|
+
export declare function toPlainHeaders(headers: unknown): Record<string, unknown>;
|
|
19
|
+
//# sourceMappingURL=headers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"headers.d.ts","sourceRoot":"","sources":["../src/headers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAErC;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAgB5E;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,YAAY,CAG7D;AAED,+DAA+D;AAC/D,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAExE"}
|
package/dist/headers.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AxiosHeaders } from 'axios';
|
|
2
|
+
/**
|
|
3
|
+
* Case-insensitive header lookup that works with both `AxiosHeaders` and plain objects.
|
|
4
|
+
*
|
|
5
|
+
* Adapters see `config.headers` as an `AxiosHeaders` instance, while stored responses are
|
|
6
|
+
* plain objects — this normalises over both.
|
|
7
|
+
*/
|
|
8
|
+
export function readHeader(source, name) {
|
|
9
|
+
if (source === null || source === undefined)
|
|
10
|
+
return undefined;
|
|
11
|
+
if (source instanceof AxiosHeaders) {
|
|
12
|
+
const value = source.get(name);
|
|
13
|
+
return value === null || value === undefined ? undefined : String(value);
|
|
14
|
+
}
|
|
15
|
+
if (typeof source.get === 'function') {
|
|
16
|
+
const value = source.get(name);
|
|
17
|
+
return value === null || value === undefined ? undefined : String(value);
|
|
18
|
+
}
|
|
19
|
+
const record = source;
|
|
20
|
+
const value = record[name] ?? record[name.toLowerCase()];
|
|
21
|
+
return value === null || value === undefined ? undefined : String(value);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Builds an `AxiosHeaders` from whatever axios handed us.
|
|
25
|
+
*
|
|
26
|
+
* The `AxiosHeaders` constructor types are narrower than what adapters actually receive
|
|
27
|
+
* (`AxiosResponseHeaders` is a union that does not overlap cleanly), so the cast lives here,
|
|
28
|
+
* in one documented place, instead of at every call site.
|
|
29
|
+
*/
|
|
30
|
+
export function toAxiosHeaders(headers) {
|
|
31
|
+
if (headers instanceof AxiosHeaders)
|
|
32
|
+
return headers;
|
|
33
|
+
return AxiosHeaders.from(headers);
|
|
34
|
+
}
|
|
35
|
+
/** Copies headers into a plain object suitable for storage. */
|
|
36
|
+
export function toPlainHeaders(headers) {
|
|
37
|
+
return { ...toAxiosHeaders(headers) };
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=headers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"headers.js","sourceRoot":"","sources":["../src/headers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAErC;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAC,MAAe,EAAE,IAAY;IACtD,IAAI,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAE9D,IAAI,MAAM,YAAY,YAAY,EAAE,CAAC;QACnC,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC/B,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC3E,CAAC;IAED,IAAI,OAAQ,MAA4B,CAAC,GAAG,KAAK,UAAU,EAAE,CAAC;QAC5D,MAAM,KAAK,GAAI,MAA6C,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACvE,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC3E,CAAC;IAED,MAAM,MAAM,GAAG,MAAiC,CAAC;IACjD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;IACzD,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC3E,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,OAAgB;IAC7C,IAAI,OAAO,YAAY,YAAY;QAAE,OAAO,OAAO,CAAC;IACpD,OAAO,YAAY,CAAC,IAAI,CAAC,OAAiC,CAAC,CAAC;AAC9D,CAAC;AAED,+DAA+D;AAC/D,MAAM,UAAU,cAAc,CAAC,OAAgB;IAC7C,OAAO,EAAE,GAAG,cAAc,CAAC,OAAO,CAAC,EAAE,CAAC;AACxC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Axios adapter backed by Blaze Cache.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* import { createCache } from '@blaze-cache/core';
|
|
6
|
+
* import { installBlazeCache } from '@blaze-cache/axios';
|
|
7
|
+
*
|
|
8
|
+
* const cache = await createCache({ persistence: { namespace: 'api' } });
|
|
9
|
+
* installBlazeCache(api, { cache, ttlMs: 30_000 });
|
|
10
|
+
*
|
|
11
|
+
* const response = await api.get('/users/42');
|
|
12
|
+
* response.fromBlazeCache; // true on a hit
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @packageDocumentation
|
|
16
|
+
*/
|
|
17
|
+
export { blazeAdapter, installBlazeCache } from './adapter.js';
|
|
18
|
+
export type { InstallBlazeCacheOptions } from './adapter.js';
|
|
19
|
+
export { parseCacheControl } from './cache-control.js';
|
|
20
|
+
export type { CacheControl } from './cache-control.js';
|
|
21
|
+
export { defaultCacheKey, fnv1a } from './key.js';
|
|
22
|
+
export type { AxiosCacheRecord, BlazeAxiosCacheLike, BlazeAxiosCacheOptions, CachedAxiosResponse, } from './types.js';
|
|
23
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC/D,YAAY,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAE7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAElD,YAAY,EACV,gBAAgB,EAChB,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Axios adapter backed by Blaze Cache.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* import { createCache } from '@blaze-cache/core';
|
|
6
|
+
* import { installBlazeCache } from '@blaze-cache/axios';
|
|
7
|
+
*
|
|
8
|
+
* const cache = await createCache({ persistence: { namespace: 'api' } });
|
|
9
|
+
* installBlazeCache(api, { cache, ttlMs: 30_000 });
|
|
10
|
+
*
|
|
11
|
+
* const response = await api.get('/users/42');
|
|
12
|
+
* response.fromBlazeCache; // true on a hit
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @packageDocumentation
|
|
16
|
+
*/
|
|
17
|
+
export { blazeAdapter, installBlazeCache } from './adapter.js';
|
|
18
|
+
export { parseCacheControl } from './cache-control.js';
|
|
19
|
+
export { defaultCacheKey, fnv1a } from './key.js';
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAG/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAGvD,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC"}
|
package/dist/key.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { InternalAxiosRequestConfig } from 'axios';
|
|
2
|
+
/**
|
|
3
|
+
* FNV-1a, 32-bit.
|
|
4
|
+
*
|
|
5
|
+
* Folds sensitive material (bearer tokens, query parameters) into a short, stable token. It
|
|
6
|
+
* is not a security primitive — the point is that a persisted cache key never contains a
|
|
7
|
+
* secret in readable form.
|
|
8
|
+
*/
|
|
9
|
+
export declare function fnv1a(input: string): string;
|
|
10
|
+
/** Default cache key: method + URL, plus a hash of the params and the `vary` headers. */
|
|
11
|
+
export declare function defaultCacheKey(config: InternalAxiosRequestConfig, varyHeaders: readonly string[]): string;
|
|
12
|
+
//# sourceMappingURL=key.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key.d.ts","sourceRoot":"","sources":["../src/key.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,OAAO,CAAC;AAGxD;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAO3C;AAED,yFAAyF;AACzF,wBAAgB,eAAe,CAC7B,MAAM,EAAE,0BAA0B,EAClC,WAAW,EAAE,SAAS,MAAM,EAAE,GAC7B,MAAM,CASR"}
|
package/dist/key.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { safeStableStringify } from '@blaze-cache/core';
|
|
2
|
+
import { readHeader } from './headers.js';
|
|
3
|
+
/**
|
|
4
|
+
* FNV-1a, 32-bit.
|
|
5
|
+
*
|
|
6
|
+
* Folds sensitive material (bearer tokens, query parameters) into a short, stable token. It
|
|
7
|
+
* is not a security primitive — the point is that a persisted cache key never contains a
|
|
8
|
+
* secret in readable form.
|
|
9
|
+
*/
|
|
10
|
+
export function fnv1a(input) {
|
|
11
|
+
let hash = 0x811c9dc5;
|
|
12
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
13
|
+
hash ^= input.charCodeAt(index);
|
|
14
|
+
hash = Math.imul(hash, 0x01000193) >>> 0;
|
|
15
|
+
}
|
|
16
|
+
return hash.toString(36);
|
|
17
|
+
}
|
|
18
|
+
/** Default cache key: method + URL, plus a hash of the params and the `vary` headers. */
|
|
19
|
+
export function defaultCacheKey(config, varyHeaders) {
|
|
20
|
+
const method = (config.method ?? 'get').toLowerCase();
|
|
21
|
+
const url = `${config.baseURL ?? ''}${config.url ?? ''}`;
|
|
22
|
+
// `params` is stringified canonically, so `?a=1&b=2` and `?b=2&a=1` share one key.
|
|
23
|
+
const params = safeStableStringify(config.params) ?? 'undefined';
|
|
24
|
+
const vary = varyHeaders.map((header) => readHeader(config.headers, header) ?? '').join(' ');
|
|
25
|
+
return `${method} ${url}?${fnv1a(`${params} ${vary}`)}`;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=key.js.map
|
package/dist/key.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key.js","sourceRoot":"","sources":["../src/key.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C;;;;;;GAMG;AACH,MAAM,UAAU,KAAK,CAAC,KAAa;IACjC,IAAI,IAAI,GAAG,UAAU,CAAC;IACtB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;QACrD,IAAI,IAAI,KAAK,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QAChC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC;IAC3C,CAAC;IACD,OAAO,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AAC3B,CAAC;AAED,yFAAyF;AACzF,MAAM,UAAU,eAAe,CAC7B,MAAkC,EAClC,WAA8B;IAE9B,MAAM,MAAM,GAAG,CAAC,MAAM,CAAC,MAAM,IAAI,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;IACtD,MAAM,GAAG,GAAG,GAAG,MAAM,CAAC,OAAO,IAAI,EAAE,GAAG,MAAM,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC;IAEzD,mFAAmF;IACnF,MAAM,MAAM,GAAG,mBAAmB,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,WAAW,CAAC;IACjE,MAAM,IAAI,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAE7F,OAAO,GAAG,MAAM,IAAI,GAAG,IAAI,KAAK,CAAC,GAAG,MAAM,IAAI,IAAI,EAAE,CAAC,EAAE,CAAC;AAC1D,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { AxiosAdapter, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
|
2
|
+
/**
|
|
3
|
+
* What the adapter stores in Blaze Cache for one response.
|
|
4
|
+
*
|
|
5
|
+
* `data` is the **raw** payload returned by the underlying adapter — before axios runs
|
|
6
|
+
* `transformResponse`. Replaying it therefore goes through exactly the same transformation
|
|
7
|
+
* pipeline as a network response, so a hit is indistinguishable from a miss.
|
|
8
|
+
*/
|
|
9
|
+
export interface AxiosCacheRecord {
|
|
10
|
+
status: number;
|
|
11
|
+
statusText: string;
|
|
12
|
+
/** Normalised (lower-cased) response headers. */
|
|
13
|
+
headers: Record<string, unknown>;
|
|
14
|
+
/** Raw, untransformed response body. */
|
|
15
|
+
data: unknown;
|
|
16
|
+
/** `Date.now()` when the response was stored. */
|
|
17
|
+
storedAt: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* An {@link AxiosResponse} served by the cache.
|
|
21
|
+
*
|
|
22
|
+
* The extra field lets you branch without sniffing headers; the `x-blaze-cache` response
|
|
23
|
+
* header carries the same information for the network layer.
|
|
24
|
+
*/
|
|
25
|
+
export interface CachedAxiosResponse<T = unknown, D = unknown> extends AxiosResponse<T, D> {
|
|
26
|
+
/** `true` when the response came from cache instead of the network. */
|
|
27
|
+
fromBlazeCache: boolean;
|
|
28
|
+
}
|
|
29
|
+
/** Options for {@link blazeAdapter}. */
|
|
30
|
+
export interface BlazeAxiosCacheOptions {
|
|
31
|
+
/** Cache instance (or any {@link BlazeCacheLike} implementation). */
|
|
32
|
+
cache: BlazeAxiosCacheLike;
|
|
33
|
+
/**
|
|
34
|
+
* Adapter performing the real network call.
|
|
35
|
+
*
|
|
36
|
+
* `installBlazeCache` fills this in from the instance's current defaults *before*
|
|
37
|
+
* replacing them, which is what prevents the adapter from calling itself.
|
|
38
|
+
*/
|
|
39
|
+
adapter?: AxiosAdapter;
|
|
40
|
+
/**
|
|
41
|
+
* Freshness window for a cached response, in milliseconds.
|
|
42
|
+
*
|
|
43
|
+
* Ignored when the response carries `Cache-Control: max-age`, which wins.
|
|
44
|
+
* Default: `60_000`. A value `<= 0` means "do not store".
|
|
45
|
+
*/
|
|
46
|
+
ttlMs?: number | ((response: AxiosResponse, config: InternalAxiosRequestConfig) => number);
|
|
47
|
+
/**
|
|
48
|
+
* Builds the cache key. Receives the final (post-interceptor) config.
|
|
49
|
+
*
|
|
50
|
+
* The default hashes the method, URL, params and the {@link varyHeaders} values, keeping
|
|
51
|
+
* secrets such as bearer tokens out of stored keys.
|
|
52
|
+
*/
|
|
53
|
+
key?: (config: InternalAxiosRequestConfig) => string;
|
|
54
|
+
/**
|
|
55
|
+
* Decide whether a response is worth storing.
|
|
56
|
+
*
|
|
57
|
+
* Default: `GET` requests answering `2xx` without a `Cache-Control: no-store`/`no-cache`.
|
|
58
|
+
*/
|
|
59
|
+
shouldCache?: (response: AxiosResponse, config: InternalAxiosRequestConfig) => boolean;
|
|
60
|
+
/** Tags attached to stored responses. Default: `['axios', '<METHOD> <url>']`. */
|
|
61
|
+
tags?: (response: AxiosResponse, config: InternalAxiosRequestConfig) => string[];
|
|
62
|
+
/**
|
|
63
|
+
* Request headers whose values participate in the cache key.
|
|
64
|
+
*
|
|
65
|
+
* Default `['Authorization']`: a personalized response must never be served to a different
|
|
66
|
+
* user just because the URL matched. Values are hashed, never stored verbatim.
|
|
67
|
+
*/
|
|
68
|
+
varyHeaders?: readonly string[];
|
|
69
|
+
/** Response header set to `HIT`/`MISS`. Default `'x-blaze-cache'`; pass `false` to disable. */
|
|
70
|
+
markerHeader?: string | false;
|
|
71
|
+
}
|
|
72
|
+
/** The slice of a Blaze Cache the adapter needs — deliberately structural. */
|
|
73
|
+
export interface BlazeAxiosCacheLike {
|
|
74
|
+
get(key: string): AxiosCacheRecord | undefined;
|
|
75
|
+
set(key: string, value: AxiosCacheRecord, options?: {
|
|
76
|
+
ttlMs?: number;
|
|
77
|
+
tags?: readonly string[];
|
|
78
|
+
}): void;
|
|
79
|
+
}
|
|
80
|
+
export type { AxiosAdapter, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig };
|
|
81
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],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C/F,YAAY,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;CAC/B;AAED,8EAA8E;AAC9E,MAAM,WAAW,mBAAmB;IAClC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS,CAAC;IAC/C,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,gBAAgB,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE,GAAG,IAAI,CAAC;CACzG;AAED,YAAY,EAAE,YAAY,EAAE,kBAAkB,EAAE,aAAa,EAAE,0BAA0B,EAAE,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blaze-cache/axios",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Cache HTTP responses in axios via a WebAssembly cache — TTLs, tags, Cache-Control support and IndexedDB persistence",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"axios",
|
|
7
|
+
"cache",
|
|
8
|
+
"caching",
|
|
9
|
+
"http",
|
|
10
|
+
"adapter",
|
|
11
|
+
"interceptor",
|
|
12
|
+
"webassembly",
|
|
13
|
+
"wasm",
|
|
14
|
+
"ttl",
|
|
15
|
+
"persistence"
|
|
16
|
+
],
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/blaze-cache/blaze-cache.git",
|
|
21
|
+
"directory": "packages/axios"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"type": "module",
|
|
28
|
+
"main": "dist/index.js",
|
|
29
|
+
"types": "dist/index.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"import": "./dist/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@blaze-cache/core": "0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"axios": "^1.7.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"axios": "^1.11.0",
|
|
47
|
+
"typescript": "^5.9.3"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsc -p tsconfig.json",
|
|
51
|
+
"typecheck": "tsc -p tsconfig.typecheck.json"
|
|
52
|
+
}
|
|
6
53
|
}
|
package/README.md
DELETED