Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript: typing for withIronSession handler is wrong #368

Closed
akomm opened this issue Jun 24, 2021 · 7 comments
Closed

typescript: typing for withIronSession handler is wrong #368

akomm opened this issue Jun 24, 2021 · 7 comments

Comments

@akomm
Copy link

akomm commented Jun 24, 2021

withSession's handler is typed to get request as first argument, but when using getServerSideProps the first argument is context with req property. The example ssr-profile ignores the typing and destructures the context, for (type-wise) undefined property req.

@akomm
Copy link
Author

akomm commented Jun 24, 2021

I think the whole typescript typing on withIronSession handler does not make any sense:

export type Handler<Req, Res> = (
  req: Req & { session: Session },
  res: Res,
) => any;
export function withIronSession(
  handler: Handler,
  sessionOptions: SessionOptions,
): (...args: any[]) => Promise<any>;

The implementation is correct though: https://github.com/vvo/next-iron-session/blob/master/lib/index.js#L98-L107

@akomm akomm changed the title typescript example is wrong typescript: typing for withIronSession handler is wrong Jun 24, 2021
@vvo
Copy link
Owner

vvo commented Jun 29, 2021

Hey there @akomm thanks for jumping in, the TypeScript situation of next-iron-session has been tricky since the beginning mostly I guess because of the way it is written (modify req object).

If you have any ideas on how to improve this situation then you can either:

  • create a new type at https://github.com/DefinitelyTyped/DefinitelyTyped (preferred) and experiment with it. As soon as it is working perfectly then we could merge it here directly
  • try to modify/update the current types of this repository (not recommended), but this could get very tricky since multiple people already tried it.

Thanks and good luck!

@ApacheEx
Copy link

ApacheEx commented Jul 31, 2021

Hello,
I faced the same problem when looking on typescript example:

Currently, I've solved it in the following way:

lib/session.ts
image

and when you use getServerSideProps, you should simply add a custom type which stands for SSR from the session.ts:

pages/whatever.ts
image

p.s I tried a few other options (e.g function overloading) and didn't manage to solve the issue.

@Matt319
Copy link

Matt319 commented Aug 5, 2021

@ApacheEx that solution worked for me and solved my problem. Thanks for sharing!

@lindeberg
Copy link

Hello, I faced the same problem when looking on typescript example:

Currently, I've solved it in the following way:

lib/session.ts image

and when you use getServerSideProps, you should simply add a custom type which stands for SSR from the session.ts:

pages/whatever.ts image

p.s I tried a few other options (e.g function overloading) and didn't manage to solve the issue.

import {GetServerSideProps, GetServerSidePropsContext, NextApiRequest, NextApiResponse} from "next";
import {Session, withIronSession} from "next-iron-session";

type NextIronRequest = NextApiRequest & { session: Session };
type ServerSideContext = GetServerSidePropsContext & { req: NextIronRequest };

export type ApiHandler = (req: NextIronRequest, res: NextApiResponse,) => Promise<void>;

export type ServerSideHandler = (context: ServerSideContext,) => ReturnType<GetServerSideProps>;

const withSession = <T extends ApiHandler | ServerSideHandler>(handler: T) =>

For people to copy paste

@vvo vvo closed this as completed in cfa5c80 Nov 4, 2021
@vvo
Copy link
Owner

vvo commented Nov 4, 2021

Hey there, the typings were fully redone as part of the V6. Changelog here: https://github.com/vvo/iron-session/releases/tag/v6.0.0

@vvo
Copy link
Owner

vvo commented Nov 4, 2021

It should be a lot better now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants