Skip to content

Commit 1fd3ca9

Browse files
committed
Set Session type via module augmentation
1 parent 930c48b commit 1fd3ca9

File tree

30 files changed

+184
-146
lines changed

30 files changed

+184
-146
lines changed

.changeset/old-buttons-wave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@keystone-6/core": major
3+
---
4+
5+
The `Session` type is now set by augmenting the `Session` interface in `.keystone/types` instead of being a generic on various types exported by `.keystone/types`

examples/auth-magic-link/keystone.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { config } from '@keystone-6/core'
22
import { statelessSessions } from '@keystone-6/core/session'
33
import { createAuth } from '@keystone-6/auth'
4-
import { type Session, lists, extendGraphqlSchema } from './schema'
4+
import { lists, extendGraphqlSchema } from './schema'
55
import type { TypeInfo } from '.keystone/types'
66

77
// WARNING: this example is for demonstration purposes only
@@ -38,7 +38,7 @@ const { withAuth } = createAuth({
3838
},
3939
})
4040

41-
export default withAuth<TypeInfo<Session>>(
41+
export default withAuth<TypeInfo>(
4242
config<TypeInfo>({
4343
db: {
4444
provider: 'sqlite',

examples/auth-magic-link/schema.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { gWithContext, list } from '@keystone-6/core'
22
import { allowAll, denyAll } from '@keystone-6/core/access'
33
import { password, text, timestamp } from '@keystone-6/core/fields'
4-
import type { Lists, Context } from '.keystone/types'
4+
import type { Lists, Context, Session } from '.keystone/types'
55

66
import { randomBytes } from 'node:crypto'
77

88
const g = gWithContext<Context>()
99
type g<T> = gWithContext.infer<T>
1010

11-
export type Session = {
12-
itemId: string
11+
declare module '.keystone/types' {
12+
interface Session {
13+
itemId: string
14+
listKey: string
15+
}
1316
}
1417

1518
function hasSession({ session }: { session?: Session }) {
@@ -83,7 +86,7 @@ export const lists = {
8386
oneTimeTokenCreatedAt: timestamp({ ...hiddenField }),
8487
},
8588
}),
86-
} satisfies Lists<Session>
89+
} satisfies Lists
8790

8891
// WARNING: this example is for demonstration purposes only
8992
// as with each of our examples, it has not been vetted
@@ -176,7 +179,7 @@ export const extendGraphqlSchema = g.extend(base => {
176179
await context.sessionStrategy.start({
177180
context,
178181
data: {
179-
listkey: 'User',
182+
listKey: 'User',
180183
itemId: userId,
181184
},
182185
})

examples/auth/keystone.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { config } from '@keystone-6/core'
22
import { statelessSessions } from '@keystone-6/core/session'
33
import { createAuth } from '@keystone-6/auth'
4-
import { type Session, lists } from './schema'
4+
import { lists } from './schema'
55
import type { TypeInfo } from '.keystone/types'
66

77
// WARNING: this example is for demonstration purposes only
@@ -47,7 +47,7 @@ const { withAuth } = createAuth({
4747
sessionData: 'isAdmin',
4848
})
4949

50-
export default withAuth<TypeInfo<Session>>(
50+
export default withAuth<TypeInfo>(
5151
config<TypeInfo>({
5252
db: {
5353
provider: 'sqlite',

examples/auth/schema.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { list } from '@keystone-6/core'
22
import { allowAll, denyAll } from '@keystone-6/core/access'
33
import { text, checkbox, password } from '@keystone-6/core/fields'
4-
import type { Lists } from '.keystone/types'
4+
import type { Lists, Session } from '.keystone/types'
55

66
// WARNING: this example is for demonstration purposes only
77
// as with each of our examples, it has not been vetted
88
// or tested for any particular usage
9-
10-
export type Session = {
11-
itemId: string
12-
data: {
13-
isAdmin: boolean
9+
declare module '.keystone/types' {
10+
interface Session {
11+
itemId: string
12+
data: {
13+
isAdmin: boolean
14+
}
1415
}
1516
}
1617

@@ -156,4 +157,4 @@ export const lists = {
156157
}),
157158
},
158159
}),
159-
} satisfies Lists<Session>
160+
} satisfies Lists

examples/custom-output-paths/my-types.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,13 @@ type ResolvedPostUpdateInput = {
135135
publishDate?: import('./node_modules/myprisma').Prisma.PostUpdateInput['publishDate']
136136
}
137137

138+
export interface Session {}
139+
138140
export declare namespace Lists {
139-
export type Post<Session = any> = import('@keystone-6/core/types').ListConfig<Lists.Post.TypeInfo<Session>>
141+
export type Post = import('@keystone-6/core/types').ListConfig<Lists.Post.TypeInfo>
140142
namespace Post {
141143
export type Item = import('./node_modules/myprisma').Post
142-
export type TypeInfo<Session = any> = {
144+
export type TypeInfo = {
143145
key: 'Post'
144146
isSingleton: false
145147
fields: 'id' | 'title' | 'content' | 'publishDate'
@@ -155,25 +157,25 @@ export declare namespace Lists {
155157
create: ResolvedPostCreateInput
156158
update: ResolvedPostUpdateInput
157159
}
158-
all: __TypeInfo<Session>
160+
all: __TypeInfo
159161
}
160162
}
161163
}
162-
export type Context<Session = any> = import('@keystone-6/core/types').KeystoneContext<TypeInfo<Session>>
163-
export type Config<Session = any> = import('@keystone-6/core/types').KeystoneConfig<TypeInfo<Session>>
164+
export type Context = import('@keystone-6/core/types').KeystoneContext<TypeInfo>
165+
export type Config = import('@keystone-6/core/types').KeystoneConfig<TypeInfo>
164166

165-
export type TypeInfo<Session = any> = {
167+
export type TypeInfo = {
166168
lists: {
167-
readonly Post: Lists.Post.TypeInfo<Session>
169+
readonly Post: Lists.Post.TypeInfo
168170
}
169171
prisma: import('./node_modules/myprisma').PrismaClient
170172
session: Session
171173
}
172174

173-
type __TypeInfo<Session = any> = TypeInfo<Session>
175+
type __TypeInfo = TypeInfo
174176

175-
export type Lists<Session = any> = {
176-
[Key in keyof TypeInfo['lists']]?: import('@keystone-6/core/types').ListConfig<TypeInfo<Session>['lists'][Key]>
177+
export type Lists = {
178+
[Key in keyof TypeInfo['lists']]?: import('@keystone-6/core/types').ListConfig<TypeInfo['lists'][Key]>
177179
} & Record<string, import('@keystone-6/core/types').ListConfig<any>>
178180

179181
export {}

examples/custom-session-invalidation/keystone.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { config } from '@keystone-6/core'
22
import { statelessSessions } from '@keystone-6/core/session'
33
import { createAuth } from '@keystone-6/auth'
4-
import { type Session, lists } from './schema'
5-
import type { Config, Context, TypeInfo } from '.keystone/types'
4+
import { lists } from './schema'
5+
import type { Config, Context, TypeInfo, Session } from '.keystone/types'
66

77
// WARNING: this example is for demonstration purposes only
88
// as with each of our examples, it has not been vetted
@@ -32,7 +32,7 @@ const { withAuth } = createAuth({
3232
sessionData: 'passwordChangedAt',
3333
})
3434

35-
function withSessionInvalidation(config: Config<Session>): Config<Session> {
35+
function withSessionInvalidation(config: Config): Config {
3636
const existingSessionStrategy = config.session!
3737

3838
return {

examples/custom-session-invalidation/schema.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import { list } from '@keystone-6/core'
22
import { denyAll, unfiltered } from '@keystone-6/core/access'
33
import { text, password, timestamp } from '@keystone-6/core/fields'
4-
import type { Lists } from '.keystone/types'
4+
import type { Lists, Session } from '.keystone/types'
55

66
// WARNING: this example is for demonstration purposes only
77
// as with each of our examples, it has not been vetted
88
// or tested for any particular usage
99

1010
// needs to be compatible with withAuth
11-
export type Session = {
12-
listKey: string
13-
itemId: string
14-
data: {
15-
passwordChangedAt: string
11+
declare module '.keystone/types' {
12+
interface Session {
13+
listKey: string
14+
itemId: string
15+
data: {
16+
passwordChangedAt: string
17+
}
18+
startedAt: number
1619
}
17-
startedAt: number
1820
}
1921

2022
function hasSession({ session }: { session?: Session }) {
@@ -85,4 +87,4 @@ export const lists = {
8587
},
8688
},
8789
}),
88-
} satisfies Lists<Session>
90+
} satisfies Lists

examples/custom-session-jwt/keystone.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import jwt from 'jsonwebtoken'
22
import { config } from '@keystone-6/core'
3-
import { lists, type Session } from './schema'
4-
import type { Context, TypeInfo } from '.keystone/types'
3+
import { lists } from './schema'
4+
import type { Context, TypeInfo, Session } from '.keystone/types'
55

66
// WARNING: this example is for demonstration purposes only
77
// as with each of our examples, it has not been vetted

examples/custom-session-jwt/schema.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { list } from '@keystone-6/core'
22
import { allowAll, unfiltered, allOperations } from '@keystone-6/core/access'
33
import { checkbox, text } from '@keystone-6/core/fields'
4-
import type { Lists } from '.keystone/types'
4+
import type { Lists, Session } from '.keystone/types'
55

6-
export type Session = {
7-
id: string
8-
admin: boolean
6+
declare module '.keystone/types' {
7+
interface Session {
8+
id: string
9+
admin: boolean
10+
}
911
}
1012

1113
function hasSession({ session }: { session?: Session }) {
@@ -61,4 +63,4 @@ export const lists = {
6163
admin: checkbox(),
6264
},
6365
}),
64-
} satisfies Lists<Session>
66+
} satisfies Lists

0 commit comments

Comments
 (0)