@spoosh/plugin-cache 0.1.0-beta.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/README.md +38 -0
- package/dist/index.d.mts +47 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +60 -0
- package/dist/index.mjs +37 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Spoosh
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# @spoosh/plugin-cache
|
|
2
|
+
|
|
3
|
+
Response caching plugin for Spoosh with configurable stale time.
|
|
4
|
+
|
|
5
|
+
**[Documentation](https://spoosh.dev/docs/plugins/cache)** · **Requirements:** TypeScript >= 5.0 · **Peer Dependencies:** `@spoosh/core`
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @spoosh/plugin-cache
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { cachePlugin } from "@spoosh/plugin-cache";
|
|
17
|
+
|
|
18
|
+
const plugins = [
|
|
19
|
+
cachePlugin({ staleTime: 5000 }), // 5 second stale time
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
// Per-query override
|
|
23
|
+
useRead((api) => api.posts.$get(), { staleTime: 10000 });
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Options
|
|
27
|
+
|
|
28
|
+
### Plugin Config
|
|
29
|
+
|
|
30
|
+
| Option | Type | Default | Description |
|
|
31
|
+
| ----------- | -------- | ------- | ---------------------------------- |
|
|
32
|
+
| `staleTime` | `number` | `0` | Default stale time in milliseconds |
|
|
33
|
+
|
|
34
|
+
### Per-Request Options
|
|
35
|
+
|
|
36
|
+
| Option | Type | Description |
|
|
37
|
+
| ----------- | -------- | ------------------------------------ |
|
|
38
|
+
| `staleTime` | `number` | Override stale time for this request |
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { SpooshPlugin } from '@spoosh/core';
|
|
2
|
+
|
|
3
|
+
interface CachePluginConfig {
|
|
4
|
+
/** Default stale time in milliseconds. Data older than this is considered stale. Defaults to 0. */
|
|
5
|
+
staleTime?: number;
|
|
6
|
+
}
|
|
7
|
+
interface CacheReadOptions {
|
|
8
|
+
/** Time in milliseconds before cached data is considered stale. Overrides plugin default. */
|
|
9
|
+
staleTime?: number;
|
|
10
|
+
}
|
|
11
|
+
type CacheWriteOptions = object;
|
|
12
|
+
interface CacheInfiniteReadOptions {
|
|
13
|
+
/** Time in milliseconds before cached data is considered stale. Overrides plugin default. */
|
|
14
|
+
staleTime?: number;
|
|
15
|
+
}
|
|
16
|
+
type CacheReadResult = object;
|
|
17
|
+
type CacheWriteResult = object;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Enables caching for read operations with configurable stale time.
|
|
21
|
+
*
|
|
22
|
+
* Returns cached data immediately if available and not stale,
|
|
23
|
+
* avoiding unnecessary network requests.
|
|
24
|
+
*
|
|
25
|
+
* @param config - Plugin configuration
|
|
26
|
+
*
|
|
27
|
+
* @see {@link https://spoosh.dev/docs/plugins/cache | Cache Plugin Documentation}
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const plugins = [cachePlugin({ staleTime: 5000 })];
|
|
32
|
+
*
|
|
33
|
+
* // Per-query override
|
|
34
|
+
* useRead((api) => api.posts.$get(), {
|
|
35
|
+
* staleTime: 10000, // Override default stale time
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function cachePlugin(config?: CachePluginConfig): SpooshPlugin<{
|
|
40
|
+
readOptions: CacheReadOptions;
|
|
41
|
+
writeOptions: CacheWriteOptions;
|
|
42
|
+
infiniteReadOptions: CacheInfiniteReadOptions;
|
|
43
|
+
readResult: CacheReadResult;
|
|
44
|
+
writeResult: CacheWriteResult;
|
|
45
|
+
}>;
|
|
46
|
+
|
|
47
|
+
export { type CacheInfiniteReadOptions, type CachePluginConfig, type CacheReadOptions, type CacheReadResult, type CacheWriteOptions, type CacheWriteResult, cachePlugin };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { SpooshPlugin } from '@spoosh/core';
|
|
2
|
+
|
|
3
|
+
interface CachePluginConfig {
|
|
4
|
+
/** Default stale time in milliseconds. Data older than this is considered stale. Defaults to 0. */
|
|
5
|
+
staleTime?: number;
|
|
6
|
+
}
|
|
7
|
+
interface CacheReadOptions {
|
|
8
|
+
/** Time in milliseconds before cached data is considered stale. Overrides plugin default. */
|
|
9
|
+
staleTime?: number;
|
|
10
|
+
}
|
|
11
|
+
type CacheWriteOptions = object;
|
|
12
|
+
interface CacheInfiniteReadOptions {
|
|
13
|
+
/** Time in milliseconds before cached data is considered stale. Overrides plugin default. */
|
|
14
|
+
staleTime?: number;
|
|
15
|
+
}
|
|
16
|
+
type CacheReadResult = object;
|
|
17
|
+
type CacheWriteResult = object;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Enables caching for read operations with configurable stale time.
|
|
21
|
+
*
|
|
22
|
+
* Returns cached data immediately if available and not stale,
|
|
23
|
+
* avoiding unnecessary network requests.
|
|
24
|
+
*
|
|
25
|
+
* @param config - Plugin configuration
|
|
26
|
+
*
|
|
27
|
+
* @see {@link https://spoosh.dev/docs/plugins/cache | Cache Plugin Documentation}
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const plugins = [cachePlugin({ staleTime: 5000 })];
|
|
32
|
+
*
|
|
33
|
+
* // Per-query override
|
|
34
|
+
* useRead((api) => api.posts.$get(), {
|
|
35
|
+
* staleTime: 10000, // Override default stale time
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function cachePlugin(config?: CachePluginConfig): SpooshPlugin<{
|
|
40
|
+
readOptions: CacheReadOptions;
|
|
41
|
+
writeOptions: CacheWriteOptions;
|
|
42
|
+
infiniteReadOptions: CacheInfiniteReadOptions;
|
|
43
|
+
readResult: CacheReadResult;
|
|
44
|
+
writeResult: CacheWriteResult;
|
|
45
|
+
}>;
|
|
46
|
+
|
|
47
|
+
export { type CacheInfiniteReadOptions, type CachePluginConfig, type CacheReadOptions, type CacheReadResult, type CacheWriteOptions, type CacheWriteResult, cachePlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
cachePlugin: () => cachePlugin
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
|
|
27
|
+
// src/plugin.ts
|
|
28
|
+
function cachePlugin(config = {}) {
|
|
29
|
+
const { staleTime: defaultStaleTime = 0 } = config;
|
|
30
|
+
return {
|
|
31
|
+
name: "spoosh:cache",
|
|
32
|
+
operations: ["read", "infiniteRead"],
|
|
33
|
+
middleware: async (context, next) => {
|
|
34
|
+
if (!context.forceRefetch) {
|
|
35
|
+
const cached = context.stateManager.getCache(context.queryKey);
|
|
36
|
+
if (cached?.state.data && !cached.stale) {
|
|
37
|
+
const pluginOptions = context.pluginOptions;
|
|
38
|
+
const staleTime = pluginOptions?.staleTime ?? defaultStaleTime;
|
|
39
|
+
const isTimeStale = Date.now() - cached.state.timestamp > staleTime;
|
|
40
|
+
if (!isTimeStale) {
|
|
41
|
+
return { data: cached.state.data, status: 200 };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const response = await next();
|
|
46
|
+
if (response.data !== void 0 && !response.error) {
|
|
47
|
+
context.stateManager.setCache(context.queryKey, {
|
|
48
|
+
state: {
|
|
49
|
+
data: response.data,
|
|
50
|
+
error: void 0,
|
|
51
|
+
timestamp: Date.now()
|
|
52
|
+
},
|
|
53
|
+
tags: context.tags,
|
|
54
|
+
stale: false
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return response;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
2
|
+
function cachePlugin(config = {}) {
|
|
3
|
+
const { staleTime: defaultStaleTime = 0 } = config;
|
|
4
|
+
return {
|
|
5
|
+
name: "spoosh:cache",
|
|
6
|
+
operations: ["read", "infiniteRead"],
|
|
7
|
+
middleware: async (context, next) => {
|
|
8
|
+
if (!context.forceRefetch) {
|
|
9
|
+
const cached = context.stateManager.getCache(context.queryKey);
|
|
10
|
+
if (cached?.state.data && !cached.stale) {
|
|
11
|
+
const pluginOptions = context.pluginOptions;
|
|
12
|
+
const staleTime = pluginOptions?.staleTime ?? defaultStaleTime;
|
|
13
|
+
const isTimeStale = Date.now() - cached.state.timestamp > staleTime;
|
|
14
|
+
if (!isTimeStale) {
|
|
15
|
+
return { data: cached.state.data, status: 200 };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const response = await next();
|
|
20
|
+
if (response.data !== void 0 && !response.error) {
|
|
21
|
+
context.stateManager.setCache(context.queryKey, {
|
|
22
|
+
state: {
|
|
23
|
+
data: response.data,
|
|
24
|
+
error: void 0,
|
|
25
|
+
timestamp: Date.now()
|
|
26
|
+
},
|
|
27
|
+
tags: context.tags,
|
|
28
|
+
stale: false
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return response;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export {
|
|
36
|
+
cachePlugin
|
|
37
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spoosh/plugin-cache",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "Response caching plugin for Spoosh with configurable stale time",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/nxnom/spoosh.git",
|
|
9
|
+
"directory": "packages/plugin-cache"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/nxnom/spoosh/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://spoosh.dev/docs/plugins/cache",
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"spoosh",
|
|
20
|
+
"plugin",
|
|
21
|
+
"cache",
|
|
22
|
+
"api-client",
|
|
23
|
+
"stale-time"
|
|
24
|
+
],
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.mjs",
|
|
32
|
+
"require": "./dist/index.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@spoosh/core": ">=0.1.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@spoosh/core": "0.1.0-beta.0",
|
|
40
|
+
"@spoosh/test-utils": "0.1.0-beta.0"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"dev": "tsup --watch",
|
|
44
|
+
"build": "tsup",
|
|
45
|
+
"typecheck": "tsc --noEmit",
|
|
46
|
+
"lint": "eslint src --max-warnings 0",
|
|
47
|
+
"format": "prettier --write 'src/**/*.ts'"
|
|
48
|
+
}
|
|
49
|
+
}
|