Skip to content

Commit

Permalink
Return ResolvedKeystoneConfig instead of KeystoneConfig (#9189)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Cousens <413395+dcousens@users.noreply.github.com>
Co-authored-by: Daniel Cousens <dcousens@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 21, 2025
1 parent a42f956 commit a2ee52e
Show file tree
Hide file tree
Showing 55 changed files with 593 additions and 623 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-balloons-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@keystone-6/core": major
---

Update the `config` function to default any missing values to what is used internally; compatible with the internal type `ResolvedKeystoneConfig`
5 changes: 5 additions & 0 deletions .changeset/shaggy-beds-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@keystone-6/auth": minor
---

Change the type of `withAuth`'s first parameter to be a `ResolvedKeystoneConfig`, rather than `KeystoneConfig`
4 changes: 2 additions & 2 deletions examples/assets-local/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import bytes from 'bytes'
export default config({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',
url: process.env.DATABASE_URL ?? 'file:./keystone-example.db',

// WARNING: this is only needed for our monorepo examples, dont do this
prismaClientPath: 'node_modules/myprisma',
},
lists,
server: {
maxFileSize: bytes('40Mb')
maxFileSize: bytes('40Mb')!
},
storage: {
my_images: {
Expand Down
4 changes: 2 additions & 2 deletions examples/assets-s3/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ const {
export default config({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',
url: process.env.DATABASE_URL ?? 'file:./keystone-example.db',

// WARNING: this is only needed for our monorepo examples, dont do this
prismaClientPath: 'node_modules/myprisma',
},
lists,
server: {
maxFileSize: bytes('8Mb')
maxFileSize: bytes('8Mb')!
},
storage: {
my_images: {
Expand Down
9 changes: 6 additions & 3 deletions examples/custom-session-invalidation/keystone.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { config } from '@keystone-6/core'
import { statelessSessions } from '@keystone-6/core/session'
import { createAuth } from '@keystone-6/auth'
import { lists, type Session } from './schema'
import {
type Session,
lists,
} from './schema'
import type { Config, Context, TypeInfo } from '.keystone/types'

// WARNING: this example is for demonstration purposes only
Expand Down Expand Up @@ -32,13 +35,13 @@ const { withAuth } = createAuth({
sessionData: 'passwordChangedAt',
})

function withSessionInvalidation (config: Config<Session>) {
function withSessionInvalidation (config: Config<Session>): Config<Session> {
const existingSessionStrategy = config.session!

return {
...config,
session: {
...config.session,
...existingSessionStrategy,
async get ({ context }: { context: Context }): Promise<Session | undefined> {
const session = await existingSessionStrategy.get({ context })
if (!session) return
Expand Down
4 changes: 2 additions & 2 deletions examples/custom-session-passport/schema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { denyAll, allOperations } from '@keystone-6/core/access'
import { list } from '@keystone-6/core'
import { text, relationship } from '@keystone-6/core/fields'
import { type Lists } from '.keystone/types'
import type { Lists } from '.keystone/types'

import { type Session } from './auth'
import type { Session } from './auth'

// WARNING: this example is for demonstration purposes only
// as with each of our examples, it has not been vetted
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { config } from '@keystone-6/core'
import type { KeystoneConfig } from '@keystone-6/core/types'
import type { KeystoneConfigPre } from '@keystone-6/core/types'
import { seedDatabase } from './src/seed'
import { lists } from './src/schema'
import { type Context, type TypeInfo } from '.keystone/types'
import type { Context, TypeInfo } from '.keystone/types'

const db: KeystoneConfig<TypeInfo>['db'] = {
const db: KeystoneConfigPre<TypeInfo>['db'] = {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./database.db',
async onConnect (context: Context) {
Expand Down
5 changes: 2 additions & 3 deletions examples/extend-graphql-schema-nexus/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ export function extendGraphqlSchema (baseSchema: GraphQLSchema) {
},

// Typescript output settings, probably something you might commit in dev
// TODO: remove false ??, it is not part of the example, but we are having monorepo issues
// eslint-disable-next-line no-constant-binary-expression
shouldGenerateArtifacts: false ?? process.env.NODE_ENV !== 'production',
// TODO: remove false ??
shouldGenerateArtifacts: false, // ?? process.env.NODE_ENV !== 'production',
outputs: {
typegen: path.join(process.cwd(), 'nexus-types.ts'),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { config } from '@keystone-6/core'
import type { KeystoneConfig } from '@keystone-6/core/types'
import type { KeystoneConfigPre } from '@keystone-6/core/types'
import { seedDatabase } from './src/seed'
import { lists } from './src/schema'
import { type Context, type TypeInfo } from '.keystone/types'
import type { Context, TypeInfo } from '.keystone/types'

const db: KeystoneConfig<TypeInfo>['db'] = {
const db: KeystoneConfigPre<TypeInfo>['db'] = {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./database.db',
async onConnect (context: Context) {
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {
AdminFileToWrite,
BaseListTypeInfo,
KeystoneConfig,
KeystoneContext,
SessionStrategy,
BaseKeystoneTypeInfo,
KeystoneConfig,
} from '@keystone-6/core/types'
import { password, timestamp } from '@keystone-6/core/fields'

Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/admin-ui/system/generateAdminUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { promisify } from 'node:util'
import fs from 'node:fs/promises'
import fse from 'fs-extra'
import resolve from 'resolve'
import { type GraphQLSchema } from 'graphql'
import type { GraphQLSchema } from 'graphql'
import { type Entry, walk as _walk } from '@nodelib/fs.walk'
import {
type AdminFileToWrite,
type __ResolvedKeystoneConfig
import type {
AdminFileToWrite,
KeystoneConfig
} from '../../types'
import { writeAdminFiles } from '../templates'
import { type AdminMetaRootVal } from '../../lib/create-admin-meta'
import type { AdminMetaRootVal } from '../../lib/create-admin-meta'

const walk = promisify(_walk)

Expand Down Expand Up @@ -66,7 +66,7 @@ export async function writeAdminFile (file: AdminFileToWrite, projectAdminPath:
const pageExtensions = new Set(['.js', '.jsx', '.ts', '.tsx'])

export async function generateAdminUI (
config: __ResolvedKeystoneConfig,
config: KeystoneConfig,
graphQLSchema: GraphQLSchema,
adminMeta: AdminMetaRootVal,
projectAdminPath: string,
Expand Down
11 changes: 5 additions & 6 deletions packages/core/src/admin-ui/templates/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as Path from 'path'
import { type GraphQLSchema } from 'graphql'
import {
type __ResolvedKeystoneConfig
} from '../../types'
import { type AdminMetaRootVal } from '../../lib/create-admin-meta'
import type { GraphQLSchema } from 'graphql'
import type { KeystoneConfig } from '../../types'
import type { AdminMetaRootVal } from '../../lib/create-admin-meta'
import { appTemplate } from './app'
import { homeTemplate } from './home'
import { listTemplate } from './list'
Expand All @@ -14,7 +12,8 @@ import { nextConfigTemplate } from './next-config'

const pkgDir = Path.dirname(require.resolve('@keystone-6/core/package.json'))

export function writeAdminFiles (config: __ResolvedKeystoneConfig,
export function writeAdminFiles (
config: KeystoneConfig,
graphQLSchema: GraphQLSchema,
adminMeta: AdminMetaRootVal,
configFileExists: boolean
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/artifacts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { type ChildProcess } from 'node:child_process'
import type { ChildProcess } from 'node:child_process'

import { printSchema } from 'graphql'
import { getGenerators, formatSchema } from '@prisma/internals'
import { ExitError } from './scripts/utils'
import { type __ResolvedKeystoneConfig } from './types'
import { initialiseLists } from './lib/core/initialise-lists'
import {
type System,
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
type BaseKeystoneTypeInfo,
type KeystoneConfig,
type KeystoneContext
import type {
BaseKeystoneTypeInfo,
KeystoneConfig,
KeystoneContext
} from './types'
import { createSystem } from './lib/createSystem'

Expand All @@ -12,4 +12,4 @@ export function getContext<TypeInfo extends BaseKeystoneTypeInfo> (
const system = createSystem(config)
const { context } = system.getKeystone(PrismaModule)
return context
}
}
7 changes: 4 additions & 3 deletions packages/core/src/fields/types/relationship/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,14 @@ export type RelationshipFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
} & (OneDbConfig | ManyDbConfig) &
(SelectDisplayConfig | CardsDisplayConfig | CountDisplayConfig)

export function relationship <ListTypeInfo extends BaseListTypeInfo>({
export function relationship <ListTypeInfo extends BaseListTypeInfo> ({
ref,
...config
}: RelationshipFieldConfig<ListTypeInfo>): FieldTypeFunc<ListTypeInfo> {
const { many = false } = config
const [foreignListKey, foreignFieldKey] = ref.split('.')

return ({ fieldKey, listKey, lists }) => {
const { many = false } = config
const [foreignListKey, foreignFieldKey] = ref.split('.')
const foreignList = lists[foreignListKey]
if (!foreignList) throw new Error(`${listKey}.${fieldKey} points to ${ref}, but ${ref} doesn't exist`)

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/lib/assets/createFilesContext.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { randomBytes } from 'node:crypto'

import {
type FilesContext,
type __ResolvedKeystoneConfig,
import type {
FilesContext,
KeystoneConfig,
} from '../../types'
import { localFileAssetsAPI } from './local'
import { s3FileAssetsAPI } from './s3'
Expand All @@ -21,7 +21,7 @@ function defaultTransformName (path: string) {
return `${urlSafeName}-${id}`
}

export function createFilesContext (config: __ResolvedKeystoneConfig): FilesContext {
export function createFilesContext (config: KeystoneConfig): FilesContext {
const adaptersMap = new Map<string, FileAdapter>()

for (const [storageKey, storageConfig] of Object.entries(config.storage || {})) {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/lib/assets/createImagesContext.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { randomBytes } from 'node:crypto'
import imageSize from 'image-size'

import {
type ImagesContext,
type __ResolvedKeystoneConfig,
import type {
ImagesContext,
KeystoneConfig,
} from '../../types'
import type { ImageAdapter } from './types'
import { localImageAssetsAPI } from './local'
Expand Down Expand Up @@ -33,7 +33,7 @@ async function getImageMetadataFromBuffer (buffer: Buffer) {
return { width, height, filesize: buffer.length, extension }
}

export function createImagesContext (config: __ResolvedKeystoneConfig): ImagesContext {
export function createImagesContext (config: KeystoneConfig): ImagesContext {
const imageAssetsAPIs = new Map<string, ImageAdapter>()
for (const [storageKey, storageConfig] of Object.entries(config.storage || {})) {
if (storageConfig.type === 'image') {
Expand Down
16 changes: 8 additions & 8 deletions packages/core/src/lib/context/createContext.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {
type IncomingMessage,
type ServerResponse
import type {
IncomingMessage,
ServerResponse
} from 'http'
import {
type ExecutionResult,
type GraphQLSchema,
graphql,
print
} from 'graphql'
import {
type KeystoneContext,
type KeystoneGraphQLAPI,
type __ResolvedKeystoneConfig,
import type {
KeystoneContext,
KeystoneGraphQLAPI,
KeystoneConfig,
} from '../../types'

import { type InitialisedList } from '../core/initialise-lists'
Expand All @@ -27,7 +27,7 @@ export function createContext ({
prismaClient,
prismaTypes
}: {
config: __ResolvedKeystoneConfig
config: KeystoneConfig
lists: Record<string, InitialisedList>
graphQLSchema: GraphQLSchema
graphQLSchemaSudo: GraphQLSchema
Expand Down
Loading

0 comments on commit a2ee52e

Please sign in to comment.