@spoosh/plugin-throttle 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 +39 -0
- package/dist/index.d.mts +38 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +59 -0
- package/dist/index.mjs +36 -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,39 @@
|
|
|
1
|
+
# @spoosh/plugin-throttle
|
|
2
|
+
|
|
3
|
+
Request throttling plugin for Spoosh - limits request frequency.
|
|
4
|
+
|
|
5
|
+
**[Documentation](https://spoosh.dev/docs/plugins/throttle)** · **Requirements:** TypeScript >= 5.0 · **Peer Dependencies:** `@spoosh/core`
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @spoosh/plugin-throttle
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { throttlePlugin } from "@spoosh/plugin-throttle";
|
|
17
|
+
|
|
18
|
+
const plugins = [
|
|
19
|
+
// ...otherPlugins,
|
|
20
|
+
throttlePlugin(), // register at end to block even force fetches
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
// Max 1 request per second - extras return cached data
|
|
24
|
+
const { data } = useRead((api) => api.expensive.$get(), { throttle: 1000 });
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Options
|
|
28
|
+
|
|
29
|
+
### Per-Request Options
|
|
30
|
+
|
|
31
|
+
| Option | Type | Description |
|
|
32
|
+
| ---------- | -------- | -------------------------------------------------------------------- |
|
|
33
|
+
| `throttle` | `number` | Max 1 request per X milliseconds. Extra requests return cached data. |
|
|
34
|
+
|
|
35
|
+
## Notes
|
|
36
|
+
|
|
37
|
+
- Register this plugin at the end of your plugin list, to make sure it block even force fetch requests.
|
|
38
|
+
- Unlike debounce (which delays), throttle immediately returns cached data for extra requests
|
|
39
|
+
- Useful for rate-limiting expensive endpoints
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { SpooshPlugin } from '@spoosh/core';
|
|
2
|
+
|
|
3
|
+
interface ThrottleReadOptions {
|
|
4
|
+
/** Throttle requests to max 1 per X milliseconds. Extras return cached data. */
|
|
5
|
+
throttle?: number;
|
|
6
|
+
}
|
|
7
|
+
type ThrottleInfiniteReadOptions = ThrottleReadOptions;
|
|
8
|
+
type ThrottleWriteOptions = object;
|
|
9
|
+
type ThrottleReadResult = object;
|
|
10
|
+
type ThrottleWriteResult = object;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Enables throttling for read operations.
|
|
14
|
+
*
|
|
15
|
+
* Limits how frequently a query can be executed, returning cached data
|
|
16
|
+
* if the throttle window hasn't elapsed.
|
|
17
|
+
*
|
|
18
|
+
* @see {@link https://spoosh.dev/docs/plugins/throttle | Throttle Plugin Documentation}
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const plugins = [throttlePlugin()];
|
|
23
|
+
*
|
|
24
|
+
* // Throttle to max once per second
|
|
25
|
+
* useRead((api) => api.posts.$get(), {
|
|
26
|
+
* throttle: 1000,
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
declare function throttlePlugin(): SpooshPlugin<{
|
|
31
|
+
readOptions: ThrottleReadOptions;
|
|
32
|
+
writeOptions: ThrottleWriteOptions;
|
|
33
|
+
infiniteReadOptions: ThrottleInfiniteReadOptions;
|
|
34
|
+
readResult: ThrottleReadResult;
|
|
35
|
+
writeResult: ThrottleWriteResult;
|
|
36
|
+
}>;
|
|
37
|
+
|
|
38
|
+
export { type ThrottleInfiniteReadOptions, type ThrottleReadOptions, type ThrottleReadResult, type ThrottleWriteOptions, type ThrottleWriteResult, throttlePlugin };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { SpooshPlugin } from '@spoosh/core';
|
|
2
|
+
|
|
3
|
+
interface ThrottleReadOptions {
|
|
4
|
+
/** Throttle requests to max 1 per X milliseconds. Extras return cached data. */
|
|
5
|
+
throttle?: number;
|
|
6
|
+
}
|
|
7
|
+
type ThrottleInfiniteReadOptions = ThrottleReadOptions;
|
|
8
|
+
type ThrottleWriteOptions = object;
|
|
9
|
+
type ThrottleReadResult = object;
|
|
10
|
+
type ThrottleWriteResult = object;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Enables throttling for read operations.
|
|
14
|
+
*
|
|
15
|
+
* Limits how frequently a query can be executed, returning cached data
|
|
16
|
+
* if the throttle window hasn't elapsed.
|
|
17
|
+
*
|
|
18
|
+
* @see {@link https://spoosh.dev/docs/plugins/throttle | Throttle Plugin Documentation}
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const plugins = [throttlePlugin()];
|
|
23
|
+
*
|
|
24
|
+
* // Throttle to max once per second
|
|
25
|
+
* useRead((api) => api.posts.$get(), {
|
|
26
|
+
* throttle: 1000,
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
declare function throttlePlugin(): SpooshPlugin<{
|
|
31
|
+
readOptions: ThrottleReadOptions;
|
|
32
|
+
writeOptions: ThrottleWriteOptions;
|
|
33
|
+
infiniteReadOptions: ThrottleInfiniteReadOptions;
|
|
34
|
+
readResult: ThrottleReadResult;
|
|
35
|
+
writeResult: ThrottleWriteResult;
|
|
36
|
+
}>;
|
|
37
|
+
|
|
38
|
+
export { type ThrottleInfiniteReadOptions, type ThrottleReadOptions, type ThrottleReadResult, type ThrottleWriteOptions, type ThrottleWriteResult, throttlePlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
throttlePlugin: () => throttlePlugin
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
|
|
27
|
+
// src/plugin.ts
|
|
28
|
+
function throttlePlugin() {
|
|
29
|
+
const lastFetchTime = /* @__PURE__ */ new Map();
|
|
30
|
+
return {
|
|
31
|
+
name: "spoosh:throttle",
|
|
32
|
+
operations: ["read", "infiniteRead"],
|
|
33
|
+
middleware: async (context, next) => {
|
|
34
|
+
const pluginOptions = context.pluginOptions;
|
|
35
|
+
const throttleMs = pluginOptions?.throttle;
|
|
36
|
+
if (!throttleMs || throttleMs <= 0) {
|
|
37
|
+
return next();
|
|
38
|
+
}
|
|
39
|
+
const { queryKey } = context;
|
|
40
|
+
const now = Date.now();
|
|
41
|
+
const lastTime = lastFetchTime.get(queryKey) ?? 0;
|
|
42
|
+
const elapsed = now - lastTime;
|
|
43
|
+
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
|
+
return { data: void 0, status: 0 };
|
|
49
|
+
}
|
|
50
|
+
lastFetchTime.set(queryKey, now);
|
|
51
|
+
return next();
|
|
52
|
+
},
|
|
53
|
+
lifecycle: {
|
|
54
|
+
onUnmount(context) {
|
|
55
|
+
lastFetchTime.delete(context.queryKey);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
2
|
+
function throttlePlugin() {
|
|
3
|
+
const lastFetchTime = /* @__PURE__ */ new Map();
|
|
4
|
+
return {
|
|
5
|
+
name: "spoosh:throttle",
|
|
6
|
+
operations: ["read", "infiniteRead"],
|
|
7
|
+
middleware: async (context, next) => {
|
|
8
|
+
const pluginOptions = context.pluginOptions;
|
|
9
|
+
const throttleMs = pluginOptions?.throttle;
|
|
10
|
+
if (!throttleMs || throttleMs <= 0) {
|
|
11
|
+
return next();
|
|
12
|
+
}
|
|
13
|
+
const { queryKey } = context;
|
|
14
|
+
const now = Date.now();
|
|
15
|
+
const lastTime = lastFetchTime.get(queryKey) ?? 0;
|
|
16
|
+
const elapsed = now - lastTime;
|
|
17
|
+
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
|
+
return { data: void 0, status: 0 };
|
|
23
|
+
}
|
|
24
|
+
lastFetchTime.set(queryKey, now);
|
|
25
|
+
return next();
|
|
26
|
+
},
|
|
27
|
+
lifecycle: {
|
|
28
|
+
onUnmount(context) {
|
|
29
|
+
lastFetchTime.delete(context.queryKey);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
throttlePlugin
|
|
36
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spoosh/plugin-throttle",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "Request throttling plugin for Spoosh - limits request frequency",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/nxnom/spoosh.git",
|
|
9
|
+
"directory": "packages/plugin-throttle"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/nxnom/spoosh/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://spoosh.dev/docs/plugins/throttle",
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"spoosh",
|
|
20
|
+
"plugin",
|
|
21
|
+
"throttle",
|
|
22
|
+
"api-client",
|
|
23
|
+
"rate-limit"
|
|
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
|
+
}
|