fix: database adapter types

This commit is contained in:
Alessio Gravili
2023-10-12 17:53:35 +02:00
parent ed1d5a60f7
commit cc56da11d6
15 changed files with 37 additions and 27 deletions

View File

@@ -2,24 +2,24 @@ import type { PayloadRequest } from '../../../../../express/types'
import type { RichTextField, Validate } from '../../../../../fields/config/types' import type { RichTextField, Validate } from '../../../../../fields/config/types'
import type { CellComponentProps } from '../../../views/collections/List/Cell/types' import type { CellComponentProps } from '../../../views/collections/List/Cell/types'
export type RichTextFieldProps<AdapterProps = object> = Omit< export type RichTextFieldProps<Value extends object, AdapterProps> = Omit<
RichTextField<AdapterProps>, RichTextField<Value, AdapterProps>,
'type' 'type'
> & { > & {
path?: string path?: string
} }
export type RichTextAdapter<AdapterProps = object> = { export type RichTextAdapter<Value extends object = object, AdapterProps = any> = {
CellComponent: React.FC<CellComponentProps<RichTextField<AdapterProps>>> CellComponent: React.FC<CellComponentProps<RichTextField<Value, AdapterProps>>>
FieldComponent: React.FC<RichTextFieldProps<AdapterProps>> FieldComponent: React.FC<RichTextFieldProps<Value, AdapterProps>>
afterReadPromise?: (data: { afterReadPromise?: (data: {
currentDepth?: number currentDepth?: number
depth: number depth: number
field: RichTextField<AdapterProps> field: RichTextField<Value, AdapterProps>
overrideAccess?: boolean overrideAccess?: boolean
req: PayloadRequest req: PayloadRequest
showHiddenFields: boolean showHiddenFields: boolean
siblingDoc: Record<string, unknown> siblingDoc: Record<string, unknown>
}) => Promise<void> | null }) => Promise<void> | null
validate: Validate<unknown, unknown, RichTextField<AdapterProps>> validate: Validate<Value, Value, unknown, RichTextField<Value, AdapterProps>>
} }

View File

@@ -517,7 +517,7 @@ export type Config = {
*/ */
defaultMaxTextLength?: number defaultMaxTextLength?: number
/** Default richtext editor to use for richText fields */ /** Default richtext editor to use for richText fields */
editor: RichTextAdapter editor: RichTextAdapter<any, any>
/** /**
* Email configuration options. This value is overridden by `email` in Payload.init if passed. * Email configuration options. This value is overridden by `email` in Payload.init if passed.
* *

View File

@@ -398,11 +398,13 @@ export type RelationshipValue =
| ValueWithRelation[] | ValueWithRelation[]
| (number | string) | (number | string)
export type RichTextField<AdapterProps = object> = FieldBase & { type IsAny<T> = 0 extends 1 & T ? true : false
export type RichTextField<Value extends object = any, AdapterProps = any> = FieldBase & {
admin?: Admin admin?: Admin
editor?: RichTextAdapter<AdapterProps> editor?: RichTextAdapter<Value, AdapterProps>
type: 'richText' type: 'richText'
} & AdapterProps } & (IsAny<AdapterProps> extends true ? {} : AdapterProps)
export type ArrayField = FieldBase & { export type ArrayField = FieldBase & {
admin?: Admin & { admin?: Admin & {

View File

@@ -10,7 +10,8 @@ import type { AdapterProps } from '../types'
import { getEnabledNodes } from '../field/lexical/nodes' import { getEnabledNodes } from '../field/lexical/nodes'
export const RichTextCell: React.FC< export const RichTextCell: React.FC<
CellComponentProps<RichTextField<AdapterProps>, SerializedEditorState> & AdapterProps CellComponentProps<RichTextField<SerializedEditorState, AdapterProps>, SerializedEditorState> &
AdapterProps
> = ({ data, editorConfig }) => { > = ({ data, editorConfig }) => {
const [preview, setPreview] = React.useState('Loading...') const [preview, setPreview] = React.useState('Loading...')

View File

@@ -24,7 +24,7 @@ export type AfterReadPromise<T extends SerializedLexicalNode = SerializedLexical
afterReadPromises: Map<string, Array<AfterReadPromise>> afterReadPromises: Map<string, Array<AfterReadPromise>>
currentDepth: number currentDepth: number
depth: number depth: number
field: RichTextField<AdapterProps> field: RichTextField<SerializedEditorState, AdapterProps>
node: T node: T
overrideAccess: boolean overrideAccess: boolean
req: PayloadRequest req: PayloadRequest

View File

@@ -1,3 +1,4 @@
import type { SerializedEditorState } from 'lexical'
import type { EditorConfig as LexicalEditorConfig } from 'lexical/LexicalEditor' import type { EditorConfig as LexicalEditorConfig } from 'lexical/LexicalEditor'
import type { RichTextAdapter } from 'payload/types' import type { RichTextAdapter } from 'payload/types'
@@ -26,8 +27,10 @@ export type LexicalEditorProps = {
lexical?: LexicalEditorConfig lexical?: LexicalEditorConfig
} }
export function lexicalEditor(props?: LexicalEditorProps): RichTextAdapter<AdapterProps> { export function lexicalEditor(
let finalSanitizedEditorConfig: SanitizedEditorConfig = null props?: LexicalEditorProps,
): RichTextAdapter<SerializedEditorState, AdapterProps> {
let finalSanitizedEditorConfig: SanitizedEditorConfig
if (!props || (!props.features && !props.lexical)) { if (!props || (!props.features && !props.lexical)) {
finalSanitizedEditorConfig = cloneDeep(defaultSanitizedEditorConfig) finalSanitizedEditorConfig = cloneDeep(defaultSanitizedEditorConfig)
} else { } else {

View File

@@ -1,3 +1,4 @@
import type { SerializedEditorState } from 'lexical'
import type { PayloadRequest } from 'payload/types' import type { PayloadRequest } from 'payload/types'
import type { Collection, Field, RichTextField } from 'payload/types' import type { Collection, Field, RichTextField } from 'payload/types'
@@ -7,7 +8,7 @@ type Arguments = {
currentDepth?: number currentDepth?: number
data: unknown data: unknown
depth: number depth: number
field: RichTextField<AdapterProps> field: RichTextField<SerializedEditorState, AdapterProps>
key: number | string key: number | string
overrideAccess?: boolean overrideAccess?: boolean
req: PayloadRequest req: PayloadRequest

View File

@@ -4,7 +4,9 @@ import type { PayloadRequest, RichTextAdapter, RichTextField } from 'payload/typ
import type { AfterReadPromise } from '../field/features/types' import type { AfterReadPromise } from '../field/features/types'
import type { AdapterProps } from '../types' import type { AdapterProps } from '../types'
export type Args = Parameters<RichTextAdapter<AdapterProps>['afterReadPromise']>[0] & { export type Args = Parameters<
RichTextAdapter<SerializedEditorState, AdapterProps>['afterReadPromise']
>[0] & {
afterReadPromises: Map<string, Array<AfterReadPromise>> afterReadPromises: Map<string, Array<AfterReadPromise>>
} }
@@ -13,7 +15,7 @@ type RecurseRichTextArgs = {
children: SerializedLexicalNode[] children: SerializedLexicalNode[]
currentDepth: number currentDepth: number
depth: number depth: number
field: RichTextField<AdapterProps> field: RichTextField<SerializedEditorState, AdapterProps>
overrideAccess: boolean overrideAccess: boolean
promises: Promise<void>[] promises: Promise<void>[]
req: PayloadRequest req: PayloadRequest

View File

@@ -1,10 +1,11 @@
import type { SerializedEditorState } from 'lexical'
import type { FieldPermissions } from 'payload/auth' import type { FieldPermissions } from 'payload/auth'
import type { FieldTypes } from 'payload/config' import type { FieldTypes } from 'payload/config'
import type { RichTextFieldProps } from 'payload/types' import type { RichTextFieldProps } from 'payload/types'
import type { SanitizedEditorConfig } from './field/lexical/config/types' import type { SanitizedEditorConfig } from './field/lexical/config/types'
export type FieldProps = RichTextFieldProps<AdapterProps> & { export type FieldProps = RichTextFieldProps<SerializedEditorState, AdapterProps> & {
fieldTypes: FieldTypes fieldTypes: FieldTypes
indexPath: string indexPath: string
path?: string path?: string

View File

@@ -4,7 +4,7 @@ import React from 'react'
import type { AdapterArguments } from '../types' import type { AdapterArguments } from '../types'
const RichTextCell: React.FC<CellComponentProps<RichTextField<AdapterArguments>, any>> = ({ const RichTextCell: React.FC<CellComponentProps<RichTextField<any[], AdapterArguments>, any>> = ({
data, data,
}) => { }) => {
const flattenedText = data?.map((i) => i?.children?.map((c) => c.text)).join(' ') const flattenedText = data?.map((i) => i?.children?.map((c) => c.text)).join(' ')

View File

@@ -7,7 +7,7 @@ type Arguments = {
currentDepth?: number currentDepth?: number
data: unknown data: unknown
depth: number depth: number
field: RichTextField<AdapterArguments> field: RichTextField<any[], AdapterArguments>
key: number | string key: number | string
overrideAccess?: boolean overrideAccess?: boolean
req: PayloadRequest req: PayloadRequest

View File

@@ -5,13 +5,13 @@ import type { AdapterArguments } from '../types'
import { populate } from './populate' import { populate } from './populate'
import { recurseNestedFields } from './recurseNestedFields' import { recurseNestedFields } from './recurseNestedFields'
export type Args = Parameters<RichTextAdapter<AdapterArguments>['afterReadPromise']>[0] export type Args = Parameters<RichTextAdapter<any[], AdapterArguments>['afterReadPromise']>[0]
type RecurseRichTextArgs = { type RecurseRichTextArgs = {
children: unknown[] children: unknown[]
currentDepth: number currentDepth: number
depth: number depth: number
field: RichTextField<AdapterArguments> field: RichTextField<any[], AdapterArguments>
overrideAccess: boolean overrideAccess: boolean
promises: Promise<void>[] promises: Promise<void>[]
req: PayloadRequest req: PayloadRequest

View File

@@ -7,8 +7,8 @@ import { defaultRichTextValue } from './defaultValue'
export const richTextValidate: Validate< export const richTextValidate: Validate<
unknown, unknown,
unknown, unknown,
RichTextField<AdapterArguments>, RichTextField<any[], AdapterArguments>,
RichTextField<AdapterArguments> RichTextField<any[], AdapterArguments>
> = (value, { required, t }) => { > = (value, { required, t }) => {
if (required) { if (required) {
const stringifiedDefaultValue = JSON.stringify(defaultRichTextValue) const stringifiedDefaultValue = JSON.stringify(defaultRichTextValue)

View File

@@ -9,7 +9,7 @@ import { richTextRelationshipPromise } from './data/richTextRelationshipPromise'
import { richTextValidate } from './data/validation' import { richTextValidate } from './data/validation'
import RichTextField from './field' import RichTextField from './field'
export function slateEditor(args: AdapterArguments): RichTextAdapter<AdapterArguments> { export function slateEditor(args: AdapterArguments): RichTextAdapter<any[], AdapterArguments> {
return { return {
CellComponent: withMergedProps({ CellComponent: withMergedProps({
Component: RichTextCell, Component: RichTextCell,

View File

@@ -73,4 +73,4 @@ export type AdapterArguments = {
} }
} }
export type FieldProps = RichTextFieldProps<AdapterArguments> export type FieldProps = RichTextFieldProps<any, AdapterArguments>