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

fix(useSession): add types for data property #346

Merged
merged 1 commit into from
Mar 6, 2023
Merged

Conversation

maou-shonen
Copy link
Contributor

@maou-shonen maou-shonen commented Feb 27, 2023

Referring to the example in #315

const session = await useSession<{ ctr: number }>(event, { password: sessionPassword });
await session.update((data) => ({ ctr: Number(data.ctr || 0) + 2 }));
session.data.ctr // type hints is any

However, getSession().data contains the correct hints, so this should be a bug.

@binajmen
Copy link

binajmen commented Mar 4, 2023

I can confirm I noticed the same behaviour.

import { H3Event, useSession, SessionConfig } from "h3";

const sessionConfig: SessionConfig = {
  cookie: {
    // domain?: string
    // encode?: (value: string) => string;
    // expires?: Date
    httpOnly: true,
    // maxAge?: number
    path: "/",
    sameSite: "lax",
    secure: true,
  },
  // crypto?: Crypto;
  // maxAge?: number;
  name: "__auth",
  password: "s3cr3t",
  // seal?: SealOptions;
  // sessionHeader?: false | string;
};

export type AuthSession = {
  id: string;
  email: string;
};

export async function useAuthSession(event: H3Event) {
  const session = await useSession<AuthSession>(event, sessionConfig);
  console.log(session.data)
  //                   ^? data: SessionDataT [= Record<string, any>]
  return session;
}

Playground Link: Provided

@pi0 pi0 changed the title fix: useSession().data lacks type hints fix(useSession): add types for data property Mar 6, 2023
Copy link
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@pi0 pi0 merged commit ade2773 into unjs:main Mar 6, 2023
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

Successfully merging this pull request may close these issues.

3 participants