@spoosh/plugin-throttle 0.1.1 → 0.1.3
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 +4 -4
- package/dist/index.d.mts +7 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +4 -12
- package/dist/index.mjs +4 -12
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -13,14 +13,14 @@ npm install @spoosh/plugin-throttle
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
|
+
import { Spoosh } from "@spoosh/core";
|
|
16
17
|
import { throttlePlugin } from "@spoosh/plugin-throttle";
|
|
17
18
|
|
|
18
|
-
const
|
|
19
|
+
const client = new Spoosh<ApiSchema, Error>("/api").use([
|
|
19
20
|
// ...otherPlugins,
|
|
20
|
-
throttlePlugin(),
|
|
21
|
-
]
|
|
21
|
+
throttlePlugin(),
|
|
22
|
+
]);
|
|
22
23
|
|
|
23
|
-
// Max 1 request per second - extras return cached data
|
|
24
24
|
const { data } = useRead((api) => api.expensive.$get(), { throttle: 1000 });
|
|
25
25
|
```
|
|
26
26
|
|
package/dist/index.d.mts
CHANGED
|
@@ -19,7 +19,13 @@ type ThrottleWriteResult = object;
|
|
|
19
19
|
*
|
|
20
20
|
* @example
|
|
21
21
|
* ```ts
|
|
22
|
-
*
|
|
22
|
+
* import { Spoosh } from "@spoosh/core";
|
|
23
|
+
*
|
|
24
|
+
* const client = new Spoosh<ApiSchema, Error>("/api")
|
|
25
|
+
* .use([
|
|
26
|
+
* // ... other plugins
|
|
27
|
+
* throttlePlugin(),
|
|
28
|
+
* ]);
|
|
23
29
|
*
|
|
24
30
|
* // Throttle to max once per second
|
|
25
31
|
* useRead((api) => api.posts.$get(), {
|
package/dist/index.d.ts
CHANGED
|
@@ -19,7 +19,13 @@ type ThrottleWriteResult = object;
|
|
|
19
19
|
*
|
|
20
20
|
* @example
|
|
21
21
|
* ```ts
|
|
22
|
-
*
|
|
22
|
+
* import { Spoosh } from "@spoosh/core";
|
|
23
|
+
*
|
|
24
|
+
* const client = new Spoosh<ApiSchema, Error>("/api")
|
|
25
|
+
* .use([
|
|
26
|
+
* // ... other plugins
|
|
27
|
+
* throttlePlugin(),
|
|
28
|
+
* ]);
|
|
23
29
|
*
|
|
24
30
|
* // Throttle to max once per second
|
|
25
31
|
* useRead((api) => api.posts.$get(), {
|
package/dist/index.js
CHANGED
|
@@ -36,24 +36,16 @@ function throttlePlugin() {
|
|
|
36
36
|
if (!throttleMs || throttleMs <= 0) {
|
|
37
37
|
return next();
|
|
38
38
|
}
|
|
39
|
-
const {
|
|
39
|
+
const { path, method } = context;
|
|
40
|
+
const stableKey = `${path.join("/")}:${method}`;
|
|
40
41
|
const now = Date.now();
|
|
41
|
-
const lastTime = lastFetchTime.get(
|
|
42
|
+
const lastTime = lastFetchTime.get(stableKey) ?? 0;
|
|
42
43
|
const elapsed = now - lastTime;
|
|
43
44
|
if (elapsed < throttleMs) {
|
|
44
|
-
const cached = context.stateManager.getCache(queryKey);
|
|
45
|
-
if (cached?.state?.data !== void 0) {
|
|
46
|
-
return { data: cached.state.data, status: 200 };
|
|
47
|
-
}
|
|
48
45
|
return { data: void 0, status: 0 };
|
|
49
46
|
}
|
|
50
|
-
lastFetchTime.set(
|
|
47
|
+
lastFetchTime.set(stableKey, now);
|
|
51
48
|
return next();
|
|
52
|
-
},
|
|
53
|
-
lifecycle: {
|
|
54
|
-
onUnmount(context) {
|
|
55
|
-
lastFetchTime.delete(context.queryKey);
|
|
56
|
-
}
|
|
57
49
|
}
|
|
58
50
|
};
|
|
59
51
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -10,24 +10,16 @@ function throttlePlugin() {
|
|
|
10
10
|
if (!throttleMs || throttleMs <= 0) {
|
|
11
11
|
return next();
|
|
12
12
|
}
|
|
13
|
-
const {
|
|
13
|
+
const { path, method } = context;
|
|
14
|
+
const stableKey = `${path.join("/")}:${method}`;
|
|
14
15
|
const now = Date.now();
|
|
15
|
-
const lastTime = lastFetchTime.get(
|
|
16
|
+
const lastTime = lastFetchTime.get(stableKey) ?? 0;
|
|
16
17
|
const elapsed = now - lastTime;
|
|
17
18
|
if (elapsed < throttleMs) {
|
|
18
|
-
const cached = context.stateManager.getCache(queryKey);
|
|
19
|
-
if (cached?.state?.data !== void 0) {
|
|
20
|
-
return { data: cached.state.data, status: 200 };
|
|
21
|
-
}
|
|
22
19
|
return { data: void 0, status: 0 };
|
|
23
20
|
}
|
|
24
|
-
lastFetchTime.set(
|
|
21
|
+
lastFetchTime.set(stableKey, now);
|
|
25
22
|
return next();
|
|
26
|
-
},
|
|
27
|
-
lifecycle: {
|
|
28
|
-
onUnmount(context) {
|
|
29
|
-
lastFetchTime.delete(context.queryKey);
|
|
30
|
-
}
|
|
31
23
|
}
|
|
32
24
|
};
|
|
33
25
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spoosh/plugin-throttle",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Request throttling plugin for Spoosh - limits request frequency",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@spoosh/core": ">=0.
|
|
36
|
+
"@spoosh/core": ">=0.3.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@spoosh/core": "0.
|
|
40
|
-
"@spoosh/test-utils": "0.1.
|
|
39
|
+
"@spoosh/core": "0.3.0",
|
|
40
|
+
"@spoosh/test-utils": "0.1.3"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"dev": "tsup --watch",
|