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 { CellComponentProps } from '../../../views/collections/List/Cell/types'
export type RichTextFieldProps<AdapterProps = object> = Omit<
RichTextField<AdapterProps>,
export type RichTextFieldProps<Value extends object, AdapterProps> = Omit<
RichTextField<Value, AdapterProps>,
'type'
> & {
path?: string
}
export type RichTextAdapter<AdapterProps = object> = {
CellComponent: React.FC<CellComponentProps<RichTextField<AdapterProps>>>
FieldComponent: React.FC<RichTextFieldProps<AdapterProps>>
export type RichTextAdapter<Value extends object = object, AdapterProps = any> = {
CellComponent: React.FC<CellComponentProps<RichTextField<Value, AdapterProps>>>
FieldComponent: React.FC<RichTextFieldProps<Value, AdapterProps>>
afterReadPromise?: (data: {
currentDepth?: number
depth: number
field: RichTextField<AdapterProps>
field: RichTextField<Value, AdapterProps>
overrideAccess?: boolean
req: PayloadRequest
showHiddenFields: boolean
siblingDoc: Record<string, unknown>
}) => 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
/** 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.
*

View File

@@ -398,11 +398,13 @@ export type RelationshipValue =
| ValueWithRelation[]
| (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
editor?: RichTextAdapter<AdapterProps>
editor?: RichTextAdapter<Value, AdapterProps>
type: 'richText'
} & AdapterProps
} & (IsAny<AdapterProps> extends true ? {} : AdapterProps)
export type ArrayField = FieldBase & {
admin?: Admin & {

View File

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

View File

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

View File

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

View File

@@ -1,3 +1,4 @@
import type { SerializedEditorState } from 'lexical'
import type { PayloadRequest } from 'payload/types'
import type { Collection, Field, RichTextField } from 'payload/types'
@@ -7,7 +8,7 @@ type Arguments = {
currentDepth?: number
data: unknown
depth: number
field: RichTextField<AdapterProps>
field: RichTextField<SerializedEditorState, AdapterProps>
key: number | string
overrideAccess?: boolean
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 { 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>>
}
@@ -13,7 +15,7 @@ type RecurseRichTextArgs = {
children: SerializedLexicalNode[]
currentDepth: number
depth: number
field: RichTextField<AdapterProps>
field: RichTextField<SerializedEditorState, AdapterProps>
overrideAccess: boolean
promises: Promise<void>[]
req: PayloadRequest

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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