rate-limit-mongo icon indicating copy to clipboard operation
rate-limit-mongo copied to clipboard

Typescript declaration

Open lucasltv opened this issue 4 years ago • 4 comments

Is there any effort to develop the @types/rate-limit-mongo module?

lucasltv avatar Jan 23 '21 13:01 lucasltv

I don't know about anything like that, please let me know if you find something 😃

okv avatar Jan 24 '21 17:01 okv

I would like to make this, but my problem is: time... Soon... I guess.

lucasltv avatar Jan 26 '21 20:01 lucasltv

@lucasltv For now, I was able to use it by creating a sample declaration like so:

  1. Create a index.d.ts in @types/rate-limit-mongo directory inside your repository, with the following content
    /* eslint-disable @typescript-eslint/no-explicit-any */
    declare module 'rate-limit-mongo' {
      export interface MongoStoreOptions {
        uri: string;
        collectionName?: string;
        expireTimeMs?: number;
        resetExpireDateOnChange?: boolean;
        errorHandler?: (err: Error) => void;
        createTtlIndex?: boolean;
      }
      declare class MongoStore {
        constructor(options: MongoStoreOptions);
    
        incr: (key: string, callback: (...args: any[]) => void) => void;
    
        decrement: (key: string, callback?: (...args: any[]) => void) => void;
    
        resetKey: (key: string, callback?: (...args: any[]) => void) => void;
    
        resetAll: () => void;
      }
      export default MongoStore;
    }
    
  2. Add the types directory to your tsconfig.json:
    {
       "compilerOptions": {
          // other options
          "typeRoots": ["./@types", "node_modules/@types"],
       }
    }
    

Note: You can further add more options to MongoStoreOptions if you are using them. For e.g. I didn't add host because I was using uri.

Hope this helps!

gtmsingh avatar Jun 06 '21 18:06 gtmsingh

I used the following so I wouldn't have to muck with tsconfig.json

At the TOP (It's gotta be the top, I spent way too long on this because I had it in the body) of the file where you are importing rate-limit-mongo

/// <reference path="../custom_types/rate-limit-mongo/index.d.ts" />

where custom_types is an arbitrary folder name

stevester94 avatar Apr 13 '24 16:04 stevester94