Skip to content

Commit

Permalink
return resolved Config
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamsi committed Jul 25, 2024
1 parent 2849031 commit f12a5b8
Show file tree
Hide file tree
Showing 25 changed files with 65 additions and 67 deletions.
8 changes: 4 additions & 4 deletions 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,
ResolvedKeystoneConfig,
} from '@keystone-6/core/types'
import { password, timestamp } from '@keystone-6/core/fields'

Expand Down Expand Up @@ -134,7 +134,7 @@ export function createAuth<ListTypeInfo extends BaseListTypeInfo> ({
})

function throwIfInvalidConfig<TypeInfo extends BaseKeystoneTypeInfo> (
config: KeystoneConfig<TypeInfo>
config: ResolvedKeystoneConfig<TypeInfo>
) {
if (!(listKey in config.lists)) {
throw new Error(`withAuth cannot find the list "${listKey}"`)
Expand Down Expand Up @@ -247,8 +247,8 @@ export function createAuth<ListTypeInfo extends BaseListTypeInfo> ({
* Automatically extends your configuration with a prescriptive implementation.
*/
function withAuth<TypeInfo extends BaseKeystoneTypeInfo> (
config: KeystoneConfig<TypeInfo>
): KeystoneConfig<TypeInfo> {
config: ResolvedKeystoneConfig<TypeInfo>
): ResolvedKeystoneConfig<TypeInfo> {
throwIfInvalidConfig(config)
let { ui } = config
if (!ui?.isDisabled) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/admin-ui/system/generateAdminUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { type GraphQLSchema } from 'graphql'
import { type Entry, walk as _walk } from '@nodelib/fs.walk'
import {
type AdminFileToWrite,
type __ResolvedKeystoneConfig
type ResolvedKeystoneConfig
} from '../../types'
import { writeAdminFiles } from '../templates'
import { type AdminMetaRootVal } from '../../lib/create-admin-meta'
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: ResolvedKeystoneConfig,
graphQLSchema: GraphQLSchema,
adminMeta: AdminMetaRootVal,
projectAdminPath: string,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/admin-ui/templates/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Path from 'path'
import { type GraphQLSchema } from 'graphql'
import {
type __ResolvedKeystoneConfig
type ResolvedKeystoneConfig
} from '../../types'
import { type AdminMetaRootVal } from '../../lib/create-admin-meta'
import { appTemplate } from './app'
Expand All @@ -14,7 +14,7 @@ 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: ResolvedKeystoneConfig,
graphQLSchema: GraphQLSchema,
adminMeta: AdminMetaRootVal,
configFileExists: boolean
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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
4 changes: 2 additions & 2 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
type BaseKeystoneTypeInfo,
type KeystoneConfig,
type ResolvedKeystoneConfig,
type KeystoneContext
} from './types'
import { createSystem } from './lib/createSystem'

export function getContext<TypeInfo extends BaseKeystoneTypeInfo> (
config: KeystoneConfig<TypeInfo>,
config: ResolvedKeystoneConfig<TypeInfo>,
PrismaModule: unknown
): KeystoneContext<TypeInfo> {
const system = createSystem(config)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/assets/createFilesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { randomBytes } from 'node:crypto'

import {
type FilesContext,
type __ResolvedKeystoneConfig,
type ResolvedKeystoneConfig,
} 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: ResolvedKeystoneConfig): FilesContext {
const adaptersMap = new Map<string, FileAdapter>()

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

import {
type ImagesContext,
type __ResolvedKeystoneConfig,
type ResolvedKeystoneConfig,
} 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: ResolvedKeystoneConfig): ImagesContext {
const imageAssetsAPIs = new Map<string, ImageAdapter>()
for (const [storageKey, storageConfig] of Object.entries(config.storage || {})) {
if (storageConfig.type === 'image') {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/context/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import {
type KeystoneContext,
type KeystoneGraphQLAPI,
type __ResolvedKeystoneConfig,
type ResolvedKeystoneConfig,
} from '../../types'

import { type InitialisedList } from '../core/initialise-lists'
Expand All @@ -27,7 +27,7 @@ export function createContext ({
prismaClient,
prismaTypes
}: {
config: __ResolvedKeystoneConfig
config: ResolvedKeystoneConfig
lists: Record<string, InitialisedList>
graphQLSchema: GraphQLSchema
graphQLSchemaSudo: GraphQLSchema
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/lib/core/initialise-lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type GraphQLTypesForList,
type ListGraphQLTypes,
type ListHooks,
type __ResolvedKeystoneConfig,
type ResolvedKeystoneConfig,
type MaybePromise,
type NextFieldType,
type FieldTypeFunc,
Expand Down Expand Up @@ -143,7 +143,7 @@ function throwIfNotAFilter (x: unknown, listKey: string, fieldKey: string) {
)
}

type ListConfigType = __ResolvedKeystoneConfig['lists'][string]
type ListConfigType = ResolvedKeystoneConfig['lists'][string]
type FieldConfigType = ReturnType<FieldTypeFunc<any>>
type PartiallyInitialisedList1 = { graphql: { isEnabled: IsEnabled } }
type PartiallyInitialisedList2 = Omit<InitialisedList, 'lists' | 'resolvedDbFields'>
Expand Down Expand Up @@ -351,7 +351,7 @@ function parseFieldHooks (
}

function getListsWithInitialisedFields (
{ storage: configStorage, lists: listsConfig, db: { provider } }: __ResolvedKeystoneConfig,
{ storage: configStorage, lists: listsConfig, db: { provider } }: ResolvedKeystoneConfig,
listGraphqlTypes: Record<string, ListGraphQLTypes>,
intermediateLists: Record<string, PartiallyInitialisedList1>
) {
Expand Down Expand Up @@ -569,7 +569,7 @@ function graphqlForOutputField (field: InitialisedField) {
}

function getListGraphqlTypes (
listsConfig: __ResolvedKeystoneConfig['lists'],
listsConfig: ResolvedKeystoneConfig['lists'],
lists: Record<string, InitialisedList>,
intermediateLists: Record<string, { graphql: { isEnabled: IsEnabled } }>
): Record<string, ListGraphQLTypes> {
Expand Down Expand Up @@ -834,7 +834,7 @@ function getListGraphqlTypes (
* 5. Handle relationships - ensure correct linking between two sides of all relationships (including one-sided relationships)
* 6.
*/
export function initialiseLists (config: __ResolvedKeystoneConfig): Record<string, InitialisedList> {
export function initialiseLists (config: ResolvedKeystoneConfig): Record<string, InitialisedList> {
const listsConfig = config.lists

let intermediateLists
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/lib/core/prisma-schema-printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
} from '../../types'
import { type ResolvedDBField } from './resolve-relationships'
import { type InitialisedList } from './initialise-lists'
import {
type __ResolvedKeystoneConfig
} from '../../types'
import { type ResolvedKeystoneConfig } from '../../types'
import { areArraysEqual, getDBFieldKeyForFieldOnMultiField } from './utils'

const modifiers = {
Expand Down Expand Up @@ -183,7 +181,7 @@ function assertDbFieldIsValidForIdField (
}

export function printPrismaSchema (
config: __ResolvedKeystoneConfig,
config: ResolvedKeystoneConfig,
lists: Record<string, InitialisedList>,
) {
const {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/create-admin-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
MaybeItemFunction,
MaybePromise,
MaybeSessionFunction,
__ResolvedKeystoneConfig,
ResolvedKeystoneConfig,
} from '../types'
import type { FilterOrderArgs } from '../types/config/fields'

Expand Down Expand Up @@ -82,7 +82,7 @@ export type AdminMetaRootVal = {
}

export function createAdminMeta (
config: __ResolvedKeystoneConfig,
config: ResolvedKeystoneConfig,
initialisedLists: Record<string, InitialisedList>
) {
const { lists } = config
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/createAdminUIMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import type express from 'express'
import type next from 'next'
import {
type KeystoneContext,
type __ResolvedKeystoneConfig,
type ResolvedKeystoneConfig,
} from '../types'
import { pkgDir } from '../pkg-dir'

const adminErrorHTMLFilepath = path.join(pkgDir, 'static', 'admin-error.html')

export function createAdminUIMiddlewareWithNextApp (
config: __ResolvedKeystoneConfig,
config: ResolvedKeystoneConfig,
commonContext: KeystoneContext,
nextApp: ReturnType<typeof next>
) {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/lib/createExpressServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.js'
import {
type KeystoneContext,
type __ResolvedKeystoneConfig,
type ResolvedKeystoneConfig,
} from '../types'

/*
Expand All @@ -24,7 +24,7 @@ The Admin UI takes a while to build for dev, and is created separately
so the CLI can bring up the dev server early to handle GraphQL requests.
*/

function formatError (graphqlConfig: __ResolvedKeystoneConfig['graphql']) {
function formatError (graphqlConfig: ResolvedKeystoneConfig['graphql']) {
return (formattedError: GraphQLFormattedError, error: unknown) => {
let debug = graphqlConfig.debug
if (debug === undefined) {
Expand All @@ -46,7 +46,7 @@ function formatError (graphqlConfig: __ResolvedKeystoneConfig['graphql']) {
}

export async function createExpressServer (
config: Pick<__ResolvedKeystoneConfig, 'graphql' | 'server' | 'storage'>,
config: Pick<ResolvedKeystoneConfig, 'graphql' | 'server' | 'storage'>,
context: KeystoneContext
): Promise<{
expressServer: express.Express
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/createGraphQLSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type GraphQLNamedType, GraphQLSchema } from 'graphql'

import { graphql } from '../types/schema'
import {
type __ResolvedKeystoneConfig
type ResolvedKeystoneConfig
} from '../types'
import { KeystoneMeta } from './admin-meta-resolver'
import type { AdminMetaRootVal } from './create-admin-meta'
Expand Down Expand Up @@ -97,7 +97,7 @@ function collectTypes (
}

export function createGraphQLSchema (
config: __ResolvedKeystoneConfig,
config: ResolvedKeystoneConfig,
lists: Record<string, InitialisedList>,
adminMeta: AdminMetaRootVal | null,
sudo: boolean
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/lib/createSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { randomBytes } from 'node:crypto'
import {
type KeystoneConfig,
type FieldData,
type __ResolvedKeystoneConfig
type ResolvedKeystoneConfig
} from '../types'
import { GraphQLError } from 'graphql'

Expand All @@ -26,7 +26,7 @@ function posixify (s: string) {
return s.split(path.sep).join('/')
}

export function getSystemPaths (cwd: string, config: KeystoneConfig | __ResolvedKeystoneConfig) {
export function getSystemPaths (cwd: string, config: KeystoneConfig | ResolvedKeystoneConfig) {
const prismaClientPath = config.db.prismaClientPath === '@prisma/client'
? null
: config.db.prismaClientPath
Expand Down Expand Up @@ -64,7 +64,7 @@ export function getSystemPaths (cwd: string, config: KeystoneConfig | __Resolved
}
}

function getSudoGraphQLSchema (config: __ResolvedKeystoneConfig) {
function getSudoGraphQLSchema (config: ResolvedKeystoneConfig) {
// This function creates a GraphQLSchema based on a modified version of the provided config.
// The modifications are:
// * All list level access control is disabled
Expand All @@ -76,7 +76,7 @@ function getSudoGraphQLSchema (config: __ResolvedKeystoneConfig) {
// operations that can be run.
//
// The resulting schema is used as the GraphQL schema when calling `context.sudo()`.
const transformedConfig: __ResolvedKeystoneConfig = {
const transformedConfig: ResolvedKeystoneConfig = {
...config,
ui: {
...config.ui,
Expand Down Expand Up @@ -183,7 +183,7 @@ function injectNewDefaults (prismaClient: unknown, lists: Record<string, Initial
return prismaClient
}

function formatUrl (provider: __ResolvedKeystoneConfig['db']['provider'], url: string) {
function formatUrl (provider: ResolvedKeystoneConfig['db']['provider'], url: string) {
if (url.startsWith('file:')) {
const parsed = new URL(url)
if (provider === 'sqlite' && !parsed.searchParams.get('connection_limit')) {
Expand All @@ -200,8 +200,8 @@ function formatUrl (provider: __ResolvedKeystoneConfig['db']['provider'], url: s
return url
}

export function createSystem (config_: KeystoneConfig) {
const config = resolveDefaults(config_)
export function createSystem (config_: KeystoneConfig | ResolvedKeystoneConfig) {
const config = resolveDefaults(config_ as KeystoneConfig)
const lists = initialiseLists(config)
const adminMeta = createAdminMeta(config, lists)
const graphQLSchema = createGraphQLSchema(config, lists, adminMeta, false)
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/lib/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type IdFieldConfig,
type KeystoneConfig,
type KeystoneContext,
type __ResolvedKeystoneConfig,
type ResolvedKeystoneConfig,
} from '../types'
import {
idFieldType
Expand Down Expand Up @@ -73,10 +73,10 @@ function defaultIsAccessAllowed ({ session, sessionStrategy }: KeystoneContext)
return session !== undefined
}

async function noop () {}
export async function noop () {}
function identity<T> (x: T) { return x }

export function resolveDefaults <TypeInfo extends BaseKeystoneTypeInfo> (config: KeystoneConfig<TypeInfo>): __ResolvedKeystoneConfig<TypeInfo> {
export function resolveDefaults<TypeInfo extends BaseKeystoneTypeInfo> (config: KeystoneConfig<TypeInfo>): ResolvedKeystoneConfig<TypeInfo> {
if (!['postgresql', 'sqlite', 'mysql'].includes(config.db.provider)) {
throw new TypeError(`"db.provider" only supports "sqlite", "postgresql" or "mysql"`)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/typescript-schema-printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export function printGeneratedTypes (
})(),
'}',
`export type Context<Session = any> = import('@keystone-6/core/types').KeystoneContext<TypeInfo<Session>>`,
`export type Config<Session = any> = import('@keystone-6/core/types').KeystoneConfig<TypeInfo<Session>>`,
`export type Config<Session = any> = import('@keystone-6/core/types').ResolvedKeystoneConfig<TypeInfo<Session>>`,
'',
'export type TypeInfo<Session = any> = {',
` lists: {`,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolveDefaults } from './lib/defaults'
import {
type BaseFields,
type BaseKeystoneTypeInfo,
Expand All @@ -7,7 +8,7 @@ import {
} from './types'

export function config<TypeInfo extends BaseKeystoneTypeInfo> (config: KeystoneConfig<TypeInfo>) {
return config
return resolveDefaults(config)
}

let i = 0
Expand Down
Loading

0 comments on commit f12a5b8

Please sign in to comment.