@joint-ops/hitlimit-types 1.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 JointOps
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,103 @@
1
+ # @joint-ops/hitlimit-types
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@joint-ops/hitlimit-types.svg)](https://www.npmjs.com/package/@joint-ops/hitlimit-types)
4
+ [![GitHub](https://img.shields.io/github/license/JointOps/hitlimit-monorepo)](https://github.com/JointOps/hitlimit-monorepo)
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.0+-blue.svg)](https://www.typescriptlang.org/)
6
+
7
+ > TypeScript type definitions for hitlimit rate limiting libraries
8
+
9
+ This package contains shared TypeScript interfaces and types used by [@joint-ops/hitlimit](https://www.npmjs.com/package/@joint-ops/hitlimit) and [@joint-ops/hitlimit-bun](https://www.npmjs.com/package/@joint-ops/hitlimit-bun).
10
+
11
+ ## Installation
12
+
13
+ This package is automatically installed as a dependency of `hitlimit` and `hitlimit-bun`. You don't need to install it directly.
14
+
15
+ ```bash
16
+ # Install hitlimit (includes types automatically)
17
+ npm install @joint-ops/hitlimit
18
+
19
+ # Or for Bun
20
+ bun add @joint-ops/hitlimit-bun
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ Types are re-exported from the main packages:
26
+
27
+ ```typescript
28
+ import type {
29
+ HitLimitConfig,
30
+ RateLimitResult,
31
+ Store,
32
+ TierConfig
33
+ } from '@joint-ops/hitlimit'
34
+
35
+ // Or from hitlimit-bun
36
+ import type { HitLimitConfig, Store } from '@joint-ops/hitlimit-bun'
37
+ ```
38
+
39
+ ## Available Types
40
+
41
+ ### Core Types
42
+
43
+ ```typescript
44
+ // Main configuration
45
+ interface HitLimitConfig {
46
+ limit?: number
47
+ window?: string | number
48
+ key?: (req: Request) => string | Promise<string>
49
+ tiers?: Record<string, TierConfig>
50
+ tier?: (req: Request) => string | Promise<string>
51
+ response?: RateLimitResponse | ((info: RateLimitInfo) => RateLimitResponse)
52
+ headers?: HeadersConfig
53
+ store?: Store
54
+ skip?: (req: Request) => boolean | Promise<boolean>
55
+ onStoreError?: (error: Error, req: Request) => 'allow' | 'deny'
56
+ }
57
+
58
+ // Store interface for custom backends
59
+ interface Store {
60
+ increment(key: string, window: number): Promise<RateLimitResult>
61
+ decrement?(key: string): Promise<void>
62
+ reset?(key: string): Promise<void>
63
+ close?(): Promise<void>
64
+ }
65
+
66
+ // Rate limit result
67
+ interface RateLimitResult {
68
+ allowed: boolean
69
+ limit: number
70
+ remaining: number
71
+ resetAt: number
72
+ }
73
+
74
+ // Tier configuration
75
+ interface TierConfig {
76
+ limit: number
77
+ window?: string | number
78
+ }
79
+ ```
80
+
81
+ ### Logger Types
82
+
83
+ ```typescript
84
+ interface Logger {
85
+ debug(message: string, meta?: Record<string, unknown>): void
86
+ info(message: string, meta?: Record<string, unknown>): void
87
+ warn(message: string, meta?: Record<string, unknown>): void
88
+ error(message: string, meta?: Record<string, unknown>): void
89
+ }
90
+ ```
91
+
92
+ ## Related Packages
93
+
94
+ - [@joint-ops/hitlimit](https://www.npmjs.com/package/@joint-ops/hitlimit) - Rate limiting for Node.js (Express, NestJS)
95
+ - [@joint-ops/hitlimit-bun](https://www.npmjs.com/package/@joint-ops/hitlimit-bun) - Rate limiting for Bun (Bun.serve, Elysia)
96
+
97
+ ## License
98
+
99
+ MIT
100
+
101
+ ## Keywords
102
+
103
+ rate limit types, typescript rate limit, rate limiter types, hitlimit, api types, middleware types, typescript definitions
@@ -0,0 +1,87 @@
1
+ export interface HitLimitOptions<TRequest = any> {
2
+ limit?: number;
3
+ window?: string | number;
4
+ key?: KeyGenerator<TRequest>;
5
+ tiers?: Record<string, TierConfig>;
6
+ tier?: TierResolver<TRequest>;
7
+ response?: ResponseConfig | ResponseFormatter;
8
+ headers?: HeadersConfig;
9
+ store?: HitLimitStore;
10
+ onStoreError?: StoreErrorHandler<TRequest>;
11
+ skip?: SkipFunction<TRequest>;
12
+ logger?: HitLimitLogger;
13
+ }
14
+ export interface TierConfig {
15
+ limit: number;
16
+ window?: string | number;
17
+ }
18
+ export interface HitLimitInfo {
19
+ limit: number;
20
+ remaining: number;
21
+ resetIn: number;
22
+ resetAt: number;
23
+ key: string;
24
+ tier?: string;
25
+ }
26
+ export interface HitLimitResult {
27
+ allowed: boolean;
28
+ info: HitLimitInfo;
29
+ headers: Record<string, string>;
30
+ body: Record<string, any>;
31
+ }
32
+ export interface HitLimitStore {
33
+ hit(key: string, windowMs: number, limit: number): Promise<StoreResult> | StoreResult;
34
+ reset(key: string): Promise<void> | void;
35
+ shutdown?(): Promise<void> | void;
36
+ }
37
+ export interface StoreResult {
38
+ count: number;
39
+ resetAt: number;
40
+ }
41
+ export interface HitLimitLogger {
42
+ debug(message: string, meta?: Record<string, any>): void;
43
+ info(message: string, meta?: Record<string, any>): void;
44
+ warn(message: string, meta?: Record<string, any>): void;
45
+ error(message: string, meta?: Record<string, any>): void;
46
+ }
47
+ export declare class HitLimitError extends Error {
48
+ readonly code: ErrorCode;
49
+ constructor(message: string, code: ErrorCode);
50
+ }
51
+ export declare class StoreError extends HitLimitError {
52
+ readonly cause?: Error | undefined;
53
+ constructor(message: string, cause?: Error | undefined);
54
+ }
55
+ export declare class ConfigError extends HitLimitError {
56
+ constructor(message: string);
57
+ }
58
+ export type ErrorCode = 'STORE_ERROR' | 'STORE_TIMEOUT' | 'CONFIG_ERROR' | 'KEY_ERROR' | 'TIER_ERROR';
59
+ export interface HeadersConfig {
60
+ standard?: boolean;
61
+ legacy?: boolean;
62
+ retryAfter?: boolean;
63
+ }
64
+ export interface ResolvedConfig<TRequest = any> {
65
+ limit: number;
66
+ windowMs: number;
67
+ key: KeyGenerator<TRequest>;
68
+ tiers?: Record<string, TierConfig>;
69
+ tier?: TierResolver<TRequest>;
70
+ response: ResponseConfig | ResponseFormatter;
71
+ headers: Required<HeadersConfig>;
72
+ store: HitLimitStore;
73
+ onStoreError: StoreErrorHandler<TRequest>;
74
+ skip?: SkipFunction<TRequest>;
75
+ logger?: HitLimitLogger;
76
+ }
77
+ export type KeyGenerator<TRequest = any> = (req: TRequest) => string | Promise<string>;
78
+ export type TierResolver<TRequest = any> = (req: TRequest) => string | Promise<string>;
79
+ export type SkipFunction<TRequest = any> = (req: TRequest) => boolean | Promise<boolean>;
80
+ export type StoreErrorHandler<TRequest = any> = (error: Error, req: TRequest) => 'allow' | 'deny' | Promise<'allow' | 'deny'>;
81
+ export type ResponseFormatter = (info: HitLimitInfo) => Record<string, any>;
82
+ export type ResponseConfig = Record<string, any>;
83
+ export declare const DEFAULT_LIMIT = 100;
84
+ export declare const DEFAULT_WINDOW = "1m";
85
+ export declare const DEFAULT_WINDOW_MS = 60000;
86
+ export declare const DEFAULT_MESSAGE = "Whoa there! Rate limit exceeded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,GAAG,EAAE,QAAQ,KAAK,OAAO,GAAG,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,MAAM,CAAC,CAAA;AAC7H,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,YAAY,KAAK,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAC3E,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAEhD,eAAO,MAAM,aAAa,MAAM,CAAA;AAChC,eAAO,MAAM,cAAc,OAAO,CAAA;AAClC,eAAO,MAAM,iBAAiB,QAAQ,CAAA;AACtC,eAAO,MAAM,eAAe,qCAAqC,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,27 @@
1
+ export class HitLimitError extends Error {
2
+ code;
3
+ constructor(message, code) {
4
+ super(message);
5
+ this.code = code;
6
+ this.name = 'HitLimitError';
7
+ }
8
+ }
9
+ export class StoreError extends HitLimitError {
10
+ cause;
11
+ constructor(message, cause) {
12
+ super(message, 'STORE_ERROR');
13
+ this.cause = cause;
14
+ this.name = 'StoreError';
15
+ }
16
+ }
17
+ export class ConfigError extends HitLimitError {
18
+ constructor(message) {
19
+ super(message, 'CONFIG_ERROR');
20
+ this.name = 'ConfigError';
21
+ }
22
+ }
23
+ export const DEFAULT_LIMIT = 100;
24
+ export const DEFAULT_WINDOW = '1m';
25
+ export const DEFAULT_WINDOW_MS = 60000;
26
+ export const DEFAULT_MESSAGE = 'Whoa there! Rate limit exceeded.';
27
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAqDA,MAAM,OAAO,aAAc,SAAQ,KAAK;IACO;IAA7C,YAAY,OAAe,EAAkB,IAAe;QAC1D,KAAK,CAAC,OAAO,CAAC,CAAA;QAD6B,SAAI,GAAJ,IAAI,CAAW;QAE1D,IAAI,CAAC,IAAI,GAAG,eAAe,CAAA;IAC7B,CAAC;CACF;AAED,MAAM,OAAO,UAAW,SAAQ,aAAa;IACE;IAA7C,YAAY,OAAe,EAAkB,KAAa;QACxD,KAAK,CAAC,OAAO,EAAE,aAAa,CAAC,CAAA;QADc,UAAK,GAAL,KAAK,CAAQ;QAExD,IAAI,CAAC,IAAI,GAAG,YAAY,CAAA;IAC1B,CAAC;CACF;AAED,MAAM,OAAO,WAAY,SAAQ,aAAa;IAC5C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,EAAE,cAAc,CAAC,CAAA;QAC9B,IAAI,CAAC,IAAI,GAAG,aAAa,CAAA;IAC3B,CAAC;CACF;AAoCD,MAAM,CAAC,MAAM,aAAa,GAAG,GAAG,CAAA;AAChC,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,CAAA;AAClC,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,CAAA;AACtC,MAAM,CAAC,MAAM,eAAe,GAAG,kCAAkC,CAAA"}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@joint-ops/hitlimit-types",
3
+ "version": "1.0.0",
4
+ "description": "Type definitions for hitlimit rate limiting library",
5
+ "author": {
6
+ "name": "Shayan M Hussain",
7
+ "email": "shayanhussain48@gmail.com",
8
+ "url": "https://github.com/ShayanHussainSB"
9
+ },
10
+ "license": "MIT",
11
+ "homepage": "https://hitlimit.dev",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/JointOps/hitlimit-monorepo",
15
+ "directory": "packages/types"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/JointOps/hitlimit-monorepo/issues"
19
+ },
20
+ "keywords": [
21
+ "rate-limit",
22
+ "rate-limiter",
23
+ "types",
24
+ "typescript",
25
+ "type-definitions",
26
+ "hitlimit",
27
+ "rate-limit-types",
28
+ "api-types",
29
+ "middleware-types"
30
+ ],
31
+ "type": "module",
32
+ "main": "./dist/index.js",
33
+ "module": "./dist/index.js",
34
+ "types": "./dist/index.d.ts",
35
+ "exports": {
36
+ ".": {
37
+ "types": "./dist/index.d.ts",
38
+ "import": "./dist/index.js"
39
+ }
40
+ },
41
+ "files": [
42
+ "dist"
43
+ ],
44
+ "sideEffects": false,
45
+ "scripts": {
46
+ "build": "tsc",
47
+ "clean": "rm -rf dist"
48
+ },
49
+ "devDependencies": {
50
+ "typescript": "^5.3.0"
51
+ }
52
+ }