index.d.mts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { Store, Options as Options$1, ClientRateLimitInfo, IncrementResponse } from 'express-rate-limit';
  2. /**
  3. * The type of data Redis might return to us.
  4. */
  5. type Data = boolean | number | string;
  6. type RedisReply = Data | Data[];
  7. /**
  8. * The library sends Redis raw commands, so all we need to know are the
  9. * 'raw-command-sending' functions for each redis client.
  10. */
  11. type SendCommandFn = (...args: string[]) => Promise<RedisReply>;
  12. /**
  13. * The configuration options for the store.
  14. */
  15. type Options = {
  16. /**
  17. * The function used to send commands to Redis.
  18. */
  19. readonly sendCommand: SendCommandFn;
  20. /**
  21. * The text to prepend to the key in Redis.
  22. */
  23. readonly prefix?: string;
  24. /**
  25. * Whether to reset the expiry for a particular key whenever its hit count
  26. * changes.
  27. */
  28. readonly resetExpiryOnChange?: boolean;
  29. };
  30. /**
  31. * A `Store` for the `express-rate-limit` package that stores hit counts in
  32. * Redis.
  33. */
  34. declare class RedisStore implements Store {
  35. /**
  36. * The function used to send raw commands to Redis.
  37. */
  38. sendCommand: SendCommandFn;
  39. /**
  40. * The text to prepend to the key in Redis.
  41. */
  42. prefix: string;
  43. /**
  44. * Whether to reset the expiry for a particular key whenever its hit count
  45. * changes.
  46. */
  47. resetExpiryOnChange: boolean;
  48. /**
  49. * Stores the loaded SHA1s of the LUA scripts used for executing the increment
  50. * and get key operations.
  51. */
  52. incrementScriptSha: Promise<string>;
  53. getScriptSha: Promise<string>;
  54. /**
  55. * The number of milliseconds to remember that user's requests.
  56. */
  57. windowMs: number;
  58. /**
  59. * @constructor for `RedisStore`.
  60. *
  61. * @param options {Options} - The configuration options for the store.
  62. */
  63. constructor(options: Options);
  64. /**
  65. * Loads the script used to increment a client's hit count.
  66. */
  67. loadIncrementScript(): Promise<string>;
  68. /**
  69. * Loads the script used to fetch a client's hit count and expiry time.
  70. */
  71. loadGetScript(): Promise<string>;
  72. /**
  73. * Runs the increment command, and retries it if the script is not loaded.
  74. */
  75. retryableIncrement(key: string): Promise<RedisReply>;
  76. /**
  77. * Method to prefix the keys with the given text.
  78. *
  79. * @param key {string} - The key.
  80. *
  81. * @returns {string} - The text + the key.
  82. */
  83. prefixKey(key: string): string;
  84. /**
  85. * Method that actually initializes the store.
  86. *
  87. * @param options {RateLimitConfiguration} - The options used to setup the middleware.
  88. */
  89. init(options: Options$1): void;
  90. /**
  91. * Method to fetch a client's hit count and reset time.
  92. *
  93. * @param key {string} - The identifier for a client.
  94. *
  95. * @returns {ClientRateLimitInfo | undefined} - The number of hits and reset time for that client.
  96. */
  97. get(key: string): Promise<ClientRateLimitInfo | undefined>;
  98. /**
  99. * Method to increment a client's hit counter.
  100. *
  101. * @param key {string} - The identifier for a client
  102. *
  103. * @returns {IncrementResponse} - The number of hits and reset time for that client
  104. */
  105. increment(key: string): Promise<IncrementResponse>;
  106. /**
  107. * Method to decrement a client's hit counter.
  108. *
  109. * @param key {string} - The identifier for a client
  110. */
  111. decrement(key: string): Promise<void>;
  112. /**
  113. * Method to reset a client's hit counter.
  114. *
  115. * @param key {string} - The identifier for a client
  116. */
  117. resetKey(key: string): Promise<void>;
  118. }
  119. export { Options, RedisReply, RedisStore, SendCommandFn, RedisStore as default };