@spoosh/plugin-cache 0.1.4 → 0.2.1

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 CHANGED
@@ -21,7 +21,7 @@ const client = new Spoosh<ApiSchema, Error>("/api").use([
21
21
  ]);
22
22
 
23
23
  // Per-query override
24
- useRead((api) => api.posts.$get(), { staleTime: 10000 });
24
+ useRead((api) => api("posts").GET(), { staleTime: 10000 });
25
25
  ```
26
26
 
27
27
  ## Options
@@ -37,3 +37,22 @@ useRead((api) => api.posts.$get(), { staleTime: 10000 });
37
37
  | Option | Type | Description |
38
38
  | ----------- | -------- | ------------------------------------ |
39
39
  | `staleTime` | `number` | Override stale time for this request |
40
+
41
+ ## Instance API
42
+
43
+ The plugin exposes a `clearCache` function for manually clearing all cached data:
44
+
45
+ ```typescript
46
+ import { createReactSpoosh } from "@spoosh/react";
47
+
48
+ const { useRead, clearCache } = createReactSpoosh(client);
49
+
50
+ // Clear all cached data (e.g., on logout or user switch)
51
+ function handleLogout() {
52
+ clearCache();
53
+ }
54
+ ```
55
+
56
+ | Method | Description |
57
+ | ------------ | -------------------------------------------------------- |
58
+ | `clearCache` | Clears all cached data. Useful for logout/user switching |
package/dist/index.d.mts CHANGED
@@ -4,6 +4,10 @@ interface CachePluginConfig {
4
4
  /** Default stale time in milliseconds. Data older than this is considered stale. Defaults to 0. */
5
5
  staleTime?: number;
6
6
  }
7
+ interface CacheInstanceApi {
8
+ /** Clear all cached data. Useful for logout or user switching scenarios. */
9
+ clearCache: () => void;
10
+ }
7
11
  interface CacheReadOptions {
8
12
  /** Time in milliseconds before cached data is considered stale. Overrides plugin default. */
9
13
  staleTime?: number;
@@ -37,7 +41,7 @@ type CacheWriteResult = object;
37
41
  * ]);
38
42
  *
39
43
  * // Per-query override
40
- * useRead((api) => api.posts.$get(), {
44
+ * useRead((api) => api("posts").GET(), {
41
45
  * staleTime: 10000,
42
46
  * });
43
47
  * ```
@@ -48,6 +52,7 @@ declare function cachePlugin(config?: CachePluginConfig): SpooshPlugin<{
48
52
  infiniteReadOptions: CacheInfiniteReadOptions;
49
53
  readResult: CacheReadResult;
50
54
  writeResult: CacheWriteResult;
55
+ instanceApi: CacheInstanceApi;
51
56
  }>;
52
57
 
53
- export { type CacheInfiniteReadOptions, type CachePluginConfig, type CacheReadOptions, type CacheReadResult, type CacheWriteOptions, type CacheWriteResult, cachePlugin };
58
+ export { type CacheInfiniteReadOptions, type CacheInstanceApi, type CachePluginConfig, type CacheReadOptions, type CacheReadResult, type CacheWriteOptions, type CacheWriteResult, cachePlugin };
package/dist/index.d.ts CHANGED
@@ -4,6 +4,10 @@ interface CachePluginConfig {
4
4
  /** Default stale time in milliseconds. Data older than this is considered stale. Defaults to 0. */
5
5
  staleTime?: number;
6
6
  }
7
+ interface CacheInstanceApi {
8
+ /** Clear all cached data. Useful for logout or user switching scenarios. */
9
+ clearCache: () => void;
10
+ }
7
11
  interface CacheReadOptions {
8
12
  /** Time in milliseconds before cached data is considered stale. Overrides plugin default. */
9
13
  staleTime?: number;
@@ -37,7 +41,7 @@ type CacheWriteResult = object;
37
41
  * ]);
38
42
  *
39
43
  * // Per-query override
40
- * useRead((api) => api.posts.$get(), {
44
+ * useRead((api) => api("posts").GET(), {
41
45
  * staleTime: 10000,
42
46
  * });
43
47
  * ```
@@ -48,6 +52,7 @@ declare function cachePlugin(config?: CachePluginConfig): SpooshPlugin<{
48
52
  infiniteReadOptions: CacheInfiniteReadOptions;
49
53
  readResult: CacheReadResult;
50
54
  writeResult: CacheWriteResult;
55
+ instanceApi: CacheInstanceApi;
51
56
  }>;
52
57
 
53
- export { type CacheInfiniteReadOptions, type CachePluginConfig, type CacheReadOptions, type CacheReadResult, type CacheWriteOptions, type CacheWriteResult, cachePlugin };
58
+ export { type CacheInfiniteReadOptions, type CacheInstanceApi, type CachePluginConfig, type CacheReadOptions, type CacheReadResult, type CacheWriteOptions, type CacheWriteResult, cachePlugin };
package/dist/index.js CHANGED
@@ -55,6 +55,13 @@ function cachePlugin(config = {}) {
55
55
  });
56
56
  }
57
57
  return response;
58
+ },
59
+ instanceApi(context) {
60
+ const { stateManager } = context;
61
+ const clearCache = () => {
62
+ stateManager.clear();
63
+ };
64
+ return { clearCache };
58
65
  }
59
66
  };
60
67
  }
package/dist/index.mjs CHANGED
@@ -29,6 +29,13 @@ function cachePlugin(config = {}) {
29
29
  });
30
30
  }
31
31
  return response;
32
+ },
33
+ instanceApi(context) {
34
+ const { stateManager } = context;
35
+ const clearCache = () => {
36
+ stateManager.clear();
37
+ };
38
+ return { clearCache };
32
39
  }
33
40
  };
34
41
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spoosh/plugin-cache",
3
- "version": "0.1.4",
3
+ "version": "0.2.1",
4
4
  "description": "Response caching plugin for Spoosh with configurable stale time",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -36,7 +36,7 @@
36
36
  "@spoosh/core": ">=0.4.0"
37
37
  },
38
38
  "devDependencies": {
39
- "@spoosh/core": "0.4.2",
39
+ "@spoosh/core": "0.6.0",
40
40
  "@spoosh/test-utils": "0.1.5"
41
41
  },
42
42
  "scripts": {