roccomuso / memorystore

express-session full featured MemoryStore layer without leaks!
MIT License
120 stars 20 forks source link

Types for Typescript usage. #4

Closed cberthou closed 5 years ago

cberthou commented 6 years ago

Hello,

I writted custom types for typescript use of your module, but I'm not completely sure if they are ok. If anyone could check that, that would be nice.

This was made for external module definition. It should be defined as a namespace and exports and imports should be moved outside the namepace, but this works fine as an external definition.

declare module "memorystore" {
  import * as express from "express";
  import * as expressSession from "express-session";

  type memorystore = (session: SessionGenerator) => MemoryStoreConstructable;

  type SessionGenerator = (
    options?: expressSession.SessionOptions
  ) => express.RequestHandler;

  interface MemoryStoreConstructable {
    new (config?: MemoryStoreOptions): MemoryStore;
  }

  class MemoryStore extends expressSession.MemoryStore {
    public touch: (
      sid: string,
      session: Express.Session,
      callback: (err: any) => any
    ) => void;
  }

  interface MemoryStoreOptions {
    checkPeriod?: number;
    max?: number;
    ttl?: (
      options: any,
      session: Express.Session,
      sessionID: string
    ) => number | number;
    dispose?: (key: string, value: any) => void;
    stale?: boolean;
    serializer?: Serializer;
  }

  interface Serializer {
    stringify: (object: any) => string;
    parse: (value: string) => any;
  }

  const fun: memorystore;
  export = fun;
}
ghost commented 6 years ago

You need to ask this on stackoverflow

roccomuso commented 6 years ago

yeah, it'd be better ask there

karlhorky commented 4 years ago

Created a pull request for this: #17

karlhorky commented 4 years ago

Oh wait, sorry, #16 is already open to add types! Will close mine.

@roccomuso any chance of merging that one? I can confirm that the types are working.