hoangvvo/next-session

connectMongo type check from example documentation

albertpeiro opened this issue · 6 comments

Hi I'm doing this:

import Session, {
  session,
  promisifyStore,
  Store,
  MemoryStore,
} from "next-session";
import connectMongo from "connect-mongo";

const MongoStore = connectMongo({ Store, MemoryStore });

declare module "next" {
  interface NextApiRequest {
    session: any;
  }
}

export default function (req, res, next) {
  const mongoStore = new MongoStore({
    mongooseConnection: req.mongooseConnection, // see how we use req.dbClient from the previous step
    stringify: true,
    secret: "squirrel",
  });
  return session({
    store: promisifyStore(mongoStore),
  })(req, res, next);
}

and I'm getting this type error:
Screenshot 2020-05-19 at 22 11 04

I'm using:

    "connect-mongo": "^3.2.0",
    "mongoose": "^5.9.14",
    "next": "9.4.1",
    "next-connect": "^0.6.6",
    "next-session": "^3.1.0",

However the basic session functionality seems to work - but haven't tested extensively.
What can I do to fix this?

Yeah, experiencing the same issue. Also, would be nice if this project supported TypeScript types as well.

Thanks for the issue. I am aware of this and intending to rewrite next-session with TypeScript really soon.

I take a look in source code of connect-mongo and see that the factory function does expect connect.Store (connect is the argument connectMongo factory function)

https://github.com/jdesboeufs/connect-mongo/blob/master/src/index.js#L57

The way its type was written is kinda not compatible type-wise. After all, connect-mongo is written for express-session.

What the factory function expects is session from express-session, which is a function.

However, what it really uses is the Store and MemoryStore properties which are attached to that function. https://github.com/expressjs/session/blob/master/index.js#L45-L47. We kinda "emulate" by manually passing in the two properties. But still, what we pass in is an object, not a function like the type expects.

I think it is safe to @ts-ignore it.

Try out the TypeScript rewrite in v3.2.1 and let me know if it works.

connectMongo should be safe to @ts-ignore

@EtienneK @albertpeiro Use v3.2.3 and the new expressSession named export should resolve the TS error! Feel free to reopen if this still occurs. See this

@hoangvvo it does! It's working now using expressSession
Thank you very much for looking into this and fixing it so quickly.
Feel free to contact me if you need any help or collaboration.