Skip to content

Commit

Permalink
Add type annotations for examples (#9012)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Cousens <[email protected]>
  • Loading branch information
dcousens and dcousens authored Feb 8, 2024
1 parent f4d8b6b commit cb6ea03
Show file tree
Hide file tree
Showing 72 changed files with 258 additions and 234 deletions.
6 changes: 3 additions & 3 deletions docs/pages/docs/walkthroughs/lesson-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const { withAuth } = createAuth({
});
let sessionSecret = '-- DEV COOKIE SECRET; CHANGE ME --';
let sessionMaxAge = 60 * 60 * 24 * 30; // 30 days
let sessionMaxAge = 60 * 60 * 24; // 24 hours
const session = statelessSessions({
maxAge: sessionMaxAge,
Expand Down Expand Up @@ -240,7 +240,7 @@ const { withAuth } = createAuth({
});
let sessionSecret = '-- DEV COOKIE SECRET; CHANGE ME --';
let sessionMaxAge = 60 * 60 * 24 * 30; // 30 days
let sessionMaxAge = 60 * 60 * 24; // 24 hours
const session = statelessSessions({
maxAge: sessionMaxAge,
Expand Down Expand Up @@ -272,7 +272,7 @@ const { withAuth } = createAuth({
});

let sessionSecret = '-- DEV COOKIE SECRET; CHANGE ME --';
let sessionMaxAge = 60 * 60 * 24 * 30; // 30 days
let sessionMaxAge = 60 * 60 * 24; // 24 hours

const session = statelessSessions({
maxAge: sessionMaxAge,
Expand Down
4 changes: 3 additions & 1 deletion examples/assets-local/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { list } from '@keystone-6/core'
import { allowAll } from '@keystone-6/core/access'
import { text, image, file } from '@keystone-6/core/fields'

import type { Lists } from '.keystone/types'

export const lists = {
Post: list({
access: allowAll,
Expand All @@ -12,4 +14,4 @@ export const lists = {
attachment: file({ storage: 'my_files' }),
},
}),
}
} satisfies Lists
4 changes: 3 additions & 1 deletion examples/assets-s3/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { list } from '@keystone-6/core'
import { allowAll } from '@keystone-6/core/access'
import { text, image, file } from '@keystone-6/core/fields'

import type { Lists } from '.keystone/types'

export const lists = {
Post: list({
access: allowAll,
Expand All @@ -12,4 +14,4 @@ export const lists = {
attachment: file({ storage: 'my_files' }),
},
}),
}
} satisfies Lists
15 changes: 8 additions & 7 deletions examples/auth/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { config } from '@keystone-6/core'
import { statelessSessions } from '@keystone-6/core/session'
import { createAuth } from '@keystone-6/auth'
import { fixPrismaPath } from '../example-utils'
import { lists } from './schema'
import { type Session, lists } from './schema'
import { type TypeInfo } from '.keystone/types'

// WARNING: this example is for demonstration purposes only
// as with each of our examples, it has not been vetted
Expand All @@ -13,8 +14,8 @@ const sessionSecret = '-- DEV COOKIE SECRET; CHANGE ME --'

// statelessSessions uses cookies for session tracking
// these cookies have an expiry, in seconds
// we use an expiry of 30 days for this example
const sessionMaxAge = 60 * 60 * 24 * 30
// we use an expiry of one hour for this example
const sessionMaxAge = 60 * 60

// withAuth is a function we can use to wrap our base configuration
const { withAuth } = createAuth({
Expand Down Expand Up @@ -47,8 +48,8 @@ const { withAuth } = createAuth({
sessionData: 'isAdmin',
})

export default withAuth(
config({
export default withAuth<TypeInfo<Session>>(
config<TypeInfo>({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',
Expand All @@ -59,8 +60,8 @@ export default withAuth(
lists,
ui: {
// only admins can view the AdminUI
isAccessAllowed: ({ session }) => {
return session?.data?.isAdmin ?? false
isAccessAllowed: (context) => {
return context.session?.data?.isAdmin ?? false
},
},
// you can find out more at https://keystonejs.com/docs/apis/session#session-api
Expand Down
6 changes: 3 additions & 3 deletions examples/auth/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Lists } from '.keystone/types'
// as with each of our examples, it has not been vetted
// or tested for any particular usage

type Session = {
export type Session = {
itemId: string
data: {
isAdmin: boolean
Expand Down Expand Up @@ -55,7 +55,7 @@ function isAdmin ({ session }: { session?: Session }) {
return false
}

export const lists: Lists<Session> = {
export const lists = {
User: list({
access: {
operation: {
Expand Down Expand Up @@ -153,4 +153,4 @@ export const lists: Lists<Session> = {
}),
},
}),
}
} satisfies Lists<Session>
4 changes: 3 additions & 1 deletion examples/cloudinary/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { allowAll } from '@keystone-6/core/access'
import { text } from '@keystone-6/core/fields'
import { cloudinaryImage } from '@keystone-6/cloudinary'

import type { Lists } from '.keystone/types'

export const lists = {
Post: list({
access: allowAll,
Expand All @@ -19,4 +21,4 @@ export const lists = {
}),
},
}),
}
} satisfies Lists
4 changes: 3 additions & 1 deletion examples/custom-admin-ui-logo/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { allowAll } from '@keystone-6/core/access'
import { checkbox, relationship, text, timestamp } from '@keystone-6/core/fields'
import { select } from '@keystone-6/core/fields'

import type { Lists } from '.keystone/types'

export const lists = {
Task: list({
access: allowAll,
Expand All @@ -28,4 +30,4 @@ export const lists = {
tasks: relationship({ ref: 'Task.assignedTo', many: true }),
},
}),
}
} satisfies Lists
4 changes: 3 additions & 1 deletion examples/custom-admin-ui-navigation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { allowAll } from '@keystone-6/core/access'
import { checkbox, relationship, text, timestamp } from '@keystone-6/core/fields'
import { select } from '@keystone-6/core/fields'

import type { Lists } from '.keystone/types'

export const lists = {
Task: list({
access: allowAll,
Expand All @@ -28,4 +30,4 @@ export const lists = {
tasks: relationship({ ref: 'Task.assignedTo', many: true }),
},
}),
}
} satisfies Lists
4 changes: 3 additions & 1 deletion examples/custom-admin-ui-pages/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { allowAll } from '@keystone-6/core/access'
import { checkbox, relationship, text, timestamp } from '@keystone-6/core/fields'
import { select } from '@keystone-6/core/fields'

import type { Lists } from '.keystone/types'

export const lists = {
Task: list({
access: allowAll,
Expand All @@ -28,4 +30,4 @@ export const lists = {
tasks: relationship({ ref: 'Task.assignedTo', many: true }),
},
}),
}
} satisfies Lists
4 changes: 3 additions & 1 deletion examples/custom-field-view/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { allowAll } from '@keystone-6/core/access'
import { checkbox, relationship, text, timestamp } from '@keystone-6/core/fields'
import { json, select } from '@keystone-6/core/fields'

import type { Lists } from '.keystone/types'

export const lists = {
Task: list({
access: allowAll,
Expand Down Expand Up @@ -37,4 +39,4 @@ export const lists = {
tasks: relationship({ ref: 'Task.assignedTo', many: true }),
},
}),
}
} satisfies Lists
4 changes: 2 additions & 2 deletions examples/custom-field/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { feedback } from './4-conditional-field'

import type { Lists } from '.keystone/types'

export const lists: Lists = {
export const lists = {
Post: list({
access: allowAll,
fields: {
Expand Down Expand Up @@ -109,4 +109,4 @@ export const lists: Lists = {
},
},
}),
}
} satisfies Lists
4 changes: 2 additions & 2 deletions examples/custom-id/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function makeCustomIdentifier (listKey: string) {
return `${listKey.toUpperCase()}_${createId()}`
}

export const lists: Lists = {
export const lists = {
Task: list({
access: allowAll,
db: {
Expand Down Expand Up @@ -48,4 +48,4 @@ export const lists: Lists = {
},
},
}),
}
} satisfies Lists
4 changes: 2 additions & 2 deletions examples/custom-output-paths/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { allowAll } from '@keystone-6/core/access'
import { text, timestamp } from '@keystone-6/core/fields'
import type { Lists } from './my-types'

export const lists: Lists = {
export const lists = {
Post: list({
access: allowAll,
fields: {
Expand Down Expand Up @@ -34,4 +34,4 @@ export const lists: Lists = {
},
},
}),
}
} satisfies Lists
4 changes: 2 additions & 2 deletions examples/custom-session-invalidation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function isSameUserFilter ({ session }: { session?: Session }) {
}
}

export const lists: Lists = {
export const lists = {
User: list({
access: {
operation: hasSession,
Expand Down Expand Up @@ -85,4 +85,4 @@ export const lists: Lists = {
},
},
}),
}
} satisfies Lists<Session>
4 changes: 2 additions & 2 deletions examples/custom-session-jwt/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function isAdminOrOnlySameUser ({ session }: { session?: Session }) {
}
}

export const lists: Lists = {
export const lists = {
Post: list({
access: {
operation: {
Expand Down Expand Up @@ -61,4 +61,4 @@ export const lists: Lists = {
admin: checkbox(),
},
}),
}
} satisfies Lists<Session>
4 changes: 2 additions & 2 deletions examples/custom-session-next-auth/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function hasSession ({ session }: { session?: Session }) {
return Boolean(session)
}

export const lists: Lists<Session> = {
export const lists = {
Post: list({
// WARNING - for this example, anyone can that can login can create, query, update and delete anything
// -- anyone with an account on the auth provider you are using can login
Expand Down Expand Up @@ -43,4 +43,4 @@ export const lists: Lists<Session> = {
posts: relationship({ ref: 'Post.author', many: true }),
},
}),
}
} satisfies Lists<Session>
4 changes: 2 additions & 2 deletions examples/custom-session-redis/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function isSameUserFilter ({ session }: { session?: Session }) {
}
}

export const lists: Lists = {
export const lists = {
User: list({
access: {
operation: hasSession,
Expand Down Expand Up @@ -62,4 +62,4 @@ export const lists: Lists = {
}),
},
}),
}
} satisfies Lists<Session>
4 changes: 2 additions & 2 deletions examples/custom-session/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function isAdminOrOnlySameUser ({ session }: { session?: Session }) {
}
}

export const lists: Lists<Session> = {
export const lists = {
Post: list({
access: {
operation: {
Expand Down Expand Up @@ -63,4 +63,4 @@ export const lists: Lists<Session> = {
admin: checkbox(),
},
}),
}
} satisfies Lists<Session>
4 changes: 2 additions & 2 deletions examples/default-values/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { select } from '@keystone-6/core/fields'
import { allowAll } from '@keystone-6/core/access'
import type { Lists } from '.keystone/types'

export const lists: Lists = {
export const lists = {
Task: list({
access: allowAll,
fields: {
Expand Down Expand Up @@ -82,4 +82,4 @@ export const lists: Lists = {
tasks: relationship({ ref: 'Task.assignedTo', many: true }),
},
}),
}
} satisfies Lists
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { list } from '@keystone-6/core'
import { allowAll } from '@keystone-6/core/access'
import { relationship, text, timestamp } from '@keystone-6/core/fields'
import { document } from '@keystone-6/fields-document'
import type { KeystoneConfig } from '@keystone-6/core/types'
import { componentBlocks } from './component-blocks'
import { type TypeInfo } from '.keystone/types'

export const lists: KeystoneConfig<TypeInfo>['lists'] = {
import type { Lists } from '.keystone/types'

export const lists = {
User: list({
access: allowAll,
fields: {
Expand Down Expand Up @@ -39,4 +39,4 @@ export const lists: KeystoneConfig<TypeInfo>['lists'] = {
author: relationship({ ref: 'User.posts', many: false }),
},
}),
}
} satisfies Lists
4 changes: 3 additions & 1 deletion examples/document-field/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { select, relationship, text, timestamp } from '@keystone-6/core/fields'
import { document } from '@keystone-6/fields-document'
import { allowAll } from '@keystone-6/core/access'

import type { Lists } from '.keystone/types'

export const lists = {
Post: list({
access: allowAll,
Expand Down Expand Up @@ -62,4 +64,4 @@ export const lists = {
}),
},
}),
}
} satisfies Lists
5 changes: 3 additions & 2 deletions examples/extend-express-app/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { list } from '@keystone-6/core'
import { allowAll } from '@keystone-6/core/access'
import { checkbox, relationship, text, timestamp } from '@keystone-6/core/fields'
import { select } from '@keystone-6/core/fields'

import { type Lists } from '.keystone/types'

export const lists: Lists = {
export const lists = {
Task: list({
access: allowAll,
fields: {
Expand All @@ -29,4 +30,4 @@ export const lists: Lists = {
tasks: relationship({ ref: 'Task.assignedTo', many: true }),
},
}),
}
} satisfies Lists
Loading

0 comments on commit cb6ea03

Please sign in to comment.