ayiin 2.0.40 → 2.0.41

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/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
+ import Redis from "ioredis";
1
2
  import Methods from "./methods";
2
- export type { DurationInput, FloodwaitPlaceholder, RateLimitStore, RateLimiterOptions, } from "./types";
3
+ export type { DurationInput, RateLimitStore, RateLimiterOptions, } from "./types";
3
4
  export default class Ayiin extends Methods {
4
5
  version: string;
5
- constructor();
6
+ cache: Redis | undefined;
7
+ constructor(databaseUrl?: string);
6
8
  }
package/dist/index.js CHANGED
@@ -3,12 +3,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const ioredis_1 = __importDefault(require("ioredis"));
6
7
  const package_json_1 = __importDefault(require("../package.json"));
7
8
  const methods_1 = __importDefault(require("./methods"));
8
9
  class Ayiin extends methods_1.default {
9
10
  version = package_json_1.default.version;
10
- constructor() {
11
+ cache;
12
+ constructor(databaseUrl) {
11
13
  super();
14
+ if (databaseUrl) {
15
+ this.cache = new ioredis_1.default(databaseUrl);
16
+ }
17
+ else {
18
+ this.cache = undefined;
19
+ }
12
20
  }
13
21
  }
14
22
  exports.default = Ayiin;
@@ -3,5 +3,4 @@ import { RateLimiterOptions } from "../types";
3
3
  import Tools from "./tools";
4
4
  export declare class Limiter extends Tools {
5
5
  createRateLimit: (options: RateLimiterOptions) => RequestHandler;
6
- private formatMessage;
7
6
  }
@@ -57,14 +57,15 @@ class Limiter extends tools_1.default {
57
57
  const blockedUntil = new Date(blockedUntilStr).getTime();
58
58
  const remainingMs = blockedUntil - now;
59
59
  if (remainingMs > 0) {
60
- const msg = options.messageFormat
61
- ? this.formatMessage(options.messageFormat, {
60
+ const msg = typeof options.messageFormat === "function"
61
+ ? options.messageFormat({
62
62
  api: options.keyPrefix,
63
63
  remainMs: remainingMs,
64
64
  remain: this.formatRemainingTime(remainingMs),
65
65
  limit: options.limit,
66
66
  })
67
- : `[FloodWait] - Silakan coba lagi dalam ${this.formatRemainingTime(remainingMs)}.`;
67
+ : (options.messageFormat ??
68
+ `[FloodWait] - Silakan coba lagi dalam ${this.formatRemainingTime(remainingMs)}.`);
68
69
  return res.status(429).json({ success: false, error: msg });
69
70
  }
70
71
  }
@@ -92,28 +93,20 @@ class Limiter extends tools_1.default {
92
93
  }
93
94
  const untilDate = new Date(now + floodwaitMs).toISOString();
94
95
  await store.set(floodKey, untilDate, floodwaitMs);
95
- const msg = options.messageFormat
96
- ? this.formatMessage(options.messageFormat, {
96
+ const msg = typeof options.messageFormat === "function"
97
+ ? options.messageFormat({
97
98
  api: options.keyPrefix,
98
99
  remainMs: floodwaitMs,
99
100
  remain: this.formatRemainingTime(floodwaitMs),
100
101
  level,
101
102
  limit: options.limit,
102
103
  })
103
- : `[FloodWait] - Terlalu banyak permintaan. Silakan coba lagi dalam ${this.formatRemainingTime(floodwaitMs)}.`;
104
+ : (options.messageFormat ??
105
+ `[FloodWait] - Terlalu banyak permintaan. Silakan coba lagi dalam ${this.formatRemainingTime(floodwaitMs)}.`);
104
106
  return res.status(429).json({ success: false, error: msg });
105
107
  }
106
108
  next();
107
109
  };
108
110
  };
109
- // internal formatter
110
- formatMessage(template, ctx) {
111
- return template
112
- .replace("%(api)s", ctx.api)
113
- .replace("%(remainMs)s", ctx.remainMs.toString())
114
- .replace("%(remain)s", ctx.remain)
115
- .replace("%(level)s", ctx.level?.toString() ?? "1")
116
- .replace("%(limit)s", ctx.limit?.toString() ?? "");
117
- }
118
111
  }
119
112
  exports.Limiter = Limiter;
package/dist/types.d.ts CHANGED
@@ -18,6 +18,11 @@ export interface RateLimiterOptions {
18
18
  identifyUser?: (req: Request, res: Response) => string | Response<any, Record<string, any>>;
19
19
  store?: RateLimitStore;
20
20
  refreshAt?: DurationInput;
21
- messageFormat?: string extends FloodwaitPlaceholder ? string : never;
21
+ messageFormat?: string | ((ctx: {
22
+ api: string;
23
+ remainMs: number;
24
+ remain: string;
25
+ level?: number;
26
+ limit?: number;
27
+ }) => string);
22
28
  }
23
- export type FloodwaitPlaceholder = "%(api)s" | "%(remainMs)s" | "%(remain)s" | "%(level)s" | "%(limit)s";
package/package.json CHANGED
@@ -1,30 +1,33 @@
1
1
  {
2
2
  "name": "ayiin",
3
- "version": "2.0.40",
4
- "description": "Library Pribadi Yang Gak Ada Isinya",
5
- "main": "./dist/index.js",
6
- "types": "./dist/index.d.ts",
7
- "bin": {
8
- "axd": "./dist/bin/cli.js"
9
- },
10
- "scripts": {
11
- "build": "tsc"
3
+ "version": "2.0.41",
4
+ "author": {
5
+ "name": "AyiinXd",
6
+ "url": "https://github.com/AyiinXd"
12
7
  },
13
8
  "repository": {
14
9
  "type": "git",
15
10
  "url": "git+https://github.com/AyiinXd/ayiin.git"
16
11
  },
17
- "author": {
18
- "name": "AyiinXd",
19
- "url": "https://github.com/AyiinXd"
12
+ "main": "./dist/index.js",
13
+ "devDependencies": {
14
+ "@types/express": "^5.0.6",
15
+ "@types/node": "^26.3.0",
16
+ "typescript": "^7"
17
+ },
18
+ "bin": {
19
+ "axd": "./dist/bin/cli.js"
20
20
  },
21
+ "description": "Library Pribadi Yang Gak Ada Isinya",
21
22
  "keywords": [
22
23
  "ayiin"
23
24
  ],
24
25
  "license": "GPL-3.0-only",
25
- "devDependencies": {
26
- "@types/express": "^5.0.6",
27
- "@types/node": "^26.3.0",
28
- "typescript": "^7"
26
+ "scripts": {
27
+ "build": "tsc"
28
+ },
29
+ "types": "./dist/index.d.ts",
30
+ "dependencies": {
31
+ "ioredis": "^6.0.0"
29
32
  }
30
33
  }