Skip to content

Commit d00617b

Browse files
author
Josh Calder
committed
Add support for typed sessions
1 parent de0905c commit d00617b

File tree

5 files changed

+15
-7
lines changed

5 files changed

+15
-7
lines changed

examples/roles/keystone.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { config } from '@keystone-6/core';
22
import { statelessSessions } from '@keystone-6/auth/session';
33
import { createAuth } from '@keystone-6/auth';
44
import { lists } from './schema';
5-
65
import { isSignedIn } from './access';
76

7+
import { Session } from './types';
8+
import { TypeInfo } from '.keystone/types';
9+
810
const sessionSecret = '-- DEV COOKIE SECRET; CHANGE ME --';
911
const sessionMaxAge = 60 * 60 * 24 * 30; // 30 days
1012
const sessionConfig = {
@@ -51,7 +53,7 @@ const { withAuth } = createAuth({
5153
sessionStrategy: statelessSessions(sessionConfig),
5254
});
5355

54-
export default withAuth(
56+
export default withAuth<TypeInfo<Session>>(
5557
config({
5658
db: {
5759
provider: 'sqlite',

packages/core/src/lib/schema-type-printer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,12 @@ export function printGeneratedTypes(
228228
'}',
229229
`export type Context = import('@keystone-6/core/types').KeystoneContext<TypeInfo>;`,
230230
'',
231-
'export type TypeInfo = {',
231+
'export type TypeInfo<Session = any> = {',
232232
` lists: {`,
233233
...listsTypeInfo,
234234
` };`,
235235
` prisma: import('.prisma/client').PrismaClient;`,
236+
` session: Session;`,
236237
`};`,
237238
``,
238239
// we need to reference the `TypeInfo` above in another type that is also called `TypeInfo`

packages/core/src/types/context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type KeystoneContext<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystone
1414
graphql: KeystoneGraphQLAPI;
1515
sudo: () => KeystoneContext<TypeInfo>;
1616
exitSudo: () => KeystoneContext<TypeInfo>;
17-
withSession: (session: any) => KeystoneContext<TypeInfo>;
17+
withSession: (session: TypeInfo['session']) => KeystoneContext<TypeInfo>;
1818
withRequest: (req: IncomingMessage, res?: ServerResponse) => Promise<KeystoneContext<TypeInfo>>;
1919
prisma: TypeInfo['prisma'];
2020
files: FilesContext;
@@ -27,8 +27,8 @@ export type KeystoneContext<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystone
2727
*/
2828
initialisedLists: Record<string, InitialisedList>;
2929
};
30-
getSession?: (args: { context: KeystoneContext }) => Promise<unknown | undefined>;
31-
session?: any;
30+
getSession?: (args: { context: KeystoneContext }) => Promise<TypeInfo['session'] | undefined>;
31+
session?: TypeInfo['session'];
3232
};
3333

3434
// List item API

packages/core/src/types/type-info.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@ export type BaseListTypeInfo = {
2828
export type KeystoneContextFromListTypeInfo<ListTypeInfo extends BaseListTypeInfo> =
2929
KeystoneContext<ListTypeInfo['all']>;
3030

31-
export type BaseKeystoneTypeInfo = { lists: Record<string, BaseListTypeInfo>; prisma: any };
31+
export type BaseKeystoneTypeInfo = {
32+
lists: Record<string, BaseListTypeInfo>;
33+
prisma: any;
34+
session: any;
35+
};

packages/core/src/types/type-tests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const someContext: KeystoneContext<{
88
ListOrSingleton: BaseListTypeInfo;
99
};
1010
prisma: any;
11+
session: any;
1112
}> = undefined!;
1213

1314
someContext.query.Singleton.findOne({});

0 commit comments

Comments
 (0)