further types

This commit is contained in:
James
2020-11-30 14:16:55 -05:00
parent e2d9cd1eed
commit 98b62df792
30 changed files with 197 additions and 93 deletions

View File

@@ -44,10 +44,12 @@ const buildStateFromSchema = async (fieldSchema: FieldSchema[], fullData: Data =
if (field.type === 'array' || field.type === 'blocks') {
if (Array.isArray(initialData?.[field.name])) {
const rows = initialData[field.name] as Data[];
if (field.type === 'array') {
return {
...state,
...initialData[field.name].reduce((rowState, row, i) => ({
...rows.reduce((rowState, row, i) => ({
...rowState,
...iterateFields(field.fields, row, `${path}${field.name}.${i}.`),
}), {}),
@@ -57,7 +59,7 @@ const buildStateFromSchema = async (fieldSchema: FieldSchema[], fullData: Data =
if (field.type === 'blocks') {
return {
...state,
...initialData[field.name].reduce((rowState, row, i) => {
...rows.reduce((rowState, row, i) => {
const block = field.blocks.find((blockType) => blockType.slug === row.blockType);
const rowPath = `${path}${field.name}.${i}.`;
@@ -84,7 +86,7 @@ const buildStateFromSchema = async (fieldSchema: FieldSchema[], fullData: Data =
}
// Handle non-array-based nested fields (group, etc)
if (field.fields) {
if (Array.isArray(field.fields)) {
return {
...state,
...iterateFields(field.fields, initialData?.[field.name], `${path}${field.name}.`),

View File

@@ -74,6 +74,7 @@ export interface IncomingAuthType {
maxLoginAttempts?: number;
lockTime?: number;
useAPIKey?: boolean;
depth?: number
cookies?: {
secure?: boolean;
sameSite?: string;

View File

@@ -1,13 +1,14 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { DeepRequired } from 'ts-essentials';
import { PaginateModel, Document, PassportLocalModel } from 'mongoose';
import { PaginateModel, Document as MongooseDocument, PassportLocalModel } from 'mongoose';
import { Access } from '../../config/types';
import { Field } from '../../fields/config/types';
import { Document } from '../../types';
import { PayloadRequest } from '../../express/types/payloadRequest';
import { IncomingAuthType, Auth } from '../../auth/types';
import { IncomingUploadType, Upload } from '../../uploads/types';
interface CollectionModel extends PaginateModel<Document>, PassportLocalModel<Document>{}
interface CollectionModel extends PaginateModel<MongooseDocument>, PassportLocalModel<MongooseDocument>{}
export type HookOperationType =
| 'create'
@@ -91,7 +92,9 @@ export type PayloadCollectionConfig = {
useAsTitle?: string;
defaultColumns?: string[];
components?: any;
enableRichTextRelationship?: boolean
};
preview?: (doc: Document, token: string) => string
hooks?: {
beforeOperation?: BeforeOperationHook[];
beforeValidate?: BeforeValidateHook[];

View File

@@ -4,7 +4,6 @@ import { PayloadRequest } from '../../express/types/payloadRequest';
import { Access } from '../../config/types';
import { Document } from '../../types';
// TODO: add generic type and use mongoose types for originalDoc & data
export type FieldHook = (args: {
value?: unknown,
originalDoc?: Document,
@@ -15,7 +14,23 @@ export type FieldHook = (args: {
req: PayloadRequest
}) => Promise<unknown> | unknown;
type FieldBase = {
type Admin = {
position?: string;
width?: string;
style?: CSSProperties;
readOnly?: boolean;
disabled?: boolean;
condition?: (...args: any[]) => any | void;
components?: { [key: string]: React.ComponentType };
hidden?: boolean
}
type Labels = {
singular: string;
plural: string;
};
interface FieldBase {
name?: string;
label?: string;
slug?: string;
@@ -24,96 +39,112 @@ type FieldBase = {
index?: boolean;
defaultValue?: any;
hidden?: boolean;
saveToJWT?: boolean
localized?: boolean;
maxLength?: number;
validate?: (value: any, field: Field) => any;
// eslint-disable-next-line no-use-before-define
hooks?: {
beforeValidate?: FieldHook[];
beforeChange?: FieldHook[];
afterChange?: FieldHook[];
afterRead?: FieldHook[];
}
admin?: {
position?: string;
width?: string;
style?: CSSProperties;
readOnly?: boolean;
disabled?: boolean;
condition?: (...args: any[]) => any | void;
components?: { [key: string]: React.ComponentType };
};
admin?: Admin;
access?: {
create?: Access;
read?: Access;
update?: Access;
delete?: Access;
admin?: Access;
unlock?: Access;
};
}
export type StandardField = FieldBase & {
type: string;
fields?: Field[];
export type NumberField = FieldBase & {
type: 'number';
min?: number
max?: number
}
export type NumberField = StandardField & { type: 'number'; };
export type TextField = StandardField & { type: 'text'; };
export type EmailField = StandardField & { type: 'email'; };
export type TextareaField = StandardField & { type: 'textarea'; };
export type CodeField = StandardField & { type: 'code'; };
export type CheckboxField = StandardField & { type: 'checkbox'; };
export type DateField = StandardField & { type: 'date'; };
export type GroupField = StandardField & { type: 'group'; };
export type RowField = StandardField & { type: 'row'; };
export type TextField = FieldBase & {
type: 'text';
maxLength?: number
minLength?: number
}
export type EmailField = FieldBase & {
type: 'email';
}
export type TextareaField = FieldBase & {
type: 'textarea';
maxLength?: number
minLength?: number
}
export type CheckboxField = FieldBase & {
type: 'checkbox';
}
export type DateField = FieldBase & {
type: 'date';
}
export type GroupField = FieldBase & {
type: 'group';
fields: Field[];
}
export type RowField = FieldBase & {
type: 'row';
fields: Field[];
}
export type UploadField = FieldBase & {
type: 'upload';
relationTo: string;
}
type CodeAdmin = Admin & {
language?: string;
}
export type CodeField = Omit<FieldBase, 'admin'> & {
admin?: CodeAdmin
type: 'code';
}
export type SelectField = FieldBase & {
type: 'select';
options: {
value: string;
label: string;
}[];
}[] | string[];
hasMany?: boolean;
}
export type SelectManyField = SelectField & {
hasMany: true;
}
export type RelationshipSingleField = FieldBase & {
export type RelationshipField = FieldBase & {
type: 'relationship';
relationTo: string;
hasMany?: false;
relationTo: string | string[];
hasMany?: boolean;
}
export type RelationshipManyField = FieldBase & {
type: 'relationship';
relationTo: string[];
hasMany: true;
}
export type RelationshipField = RelationshipSingleField | RelationshipManyField;
type RichTextElements = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'blockquote' | 'ul' | 'ol' | 'link';
type RichTextLeaves = 'bold' | 'italic' | 'underline' | 'strikethrough';
export type RichTextField = FieldBase & {
type RichTextAdmin = Admin & {
elements?: RichTextElements[];
leaves?: RichTextLeaves[];
}
export type RichTextField = Omit<FieldBase, 'admin'> & {
type: 'richText';
admin?: {
elements?: RichTextElements[];
leaves?: RichTextLeaves[];
}
admin?: RichTextAdmin
}
export type ArrayField = FieldBase & {
type: 'array';
minRows?: number;
maxRows?: number;
labels?: Labels;
fields?: Field[];
}
@@ -122,16 +153,13 @@ export type RadioField = FieldBase & {
options: {
value: string;
label: string;
}[];
}[] | string[];
hasMany?: boolean;
}
export type Block = {
slug: string,
labels: {
singular: string;
plural: string;
};
labels: Labels
fields: Field[],
}
@@ -139,16 +167,25 @@ export type BlockField = FieldBase & {
type: 'blocks';
minRows?: number;
maxRows?: number;
blocks?: Field[];
};
blocks?: Block[];
defaultValue?: unknown
labels?: Labels
}
export type Field =
| StandardField
TextField
| NumberField
| EmailField
| TextareaField
| CheckboxField
| DateField
| BlockField
| GroupField
| RadioField
| RelationshipField
| ArrayField
| RichTextField
| SelectField
| SelectManyField
| UploadField;
| UploadField
| CodeField
| RowField;

View File

@@ -23,7 +23,7 @@ export type ImageSize = {
name: string,
width: number,
height: number,
crop: string, // comes from sharp package
crop?: string, // comes from sharp package
};
export type IncomingUploadType = {