From ed5a5ebe7e16d20577556ab233032a52bef6d2cb Mon Sep 17 00:00:00 2001 From: James Date: Tue, 16 Nov 2021 21:15:49 -0500 Subject: [PATCH] feat: finishes typing all fields --- payload-types.ts | 48 ++++++++++++++++++++++-- src/bin/generateTypes.ts | 79 ++++++++++++++++++++++++++++++++++------ 2 files changed, 112 insertions(+), 15 deletions(-) diff --git a/payload-types.ts b/payload-types.ts index 96a6a8afa7..1f8b81bc6b 100644 --- a/payload-types.ts +++ b/payload-types.ts @@ -29,6 +29,9 @@ export interface LocalizedPost { title: string; summary?: string; description: string; + richText?: { + [k: string]: unknown; + }[]; priority: number; localizedGroup?: { text?: string; @@ -41,6 +44,9 @@ export interface LocalizedPost { id?: string; }[]; richTextBlocks?: { + content?: { + [k: string]: unknown; + }[]; id?: string; blockName?: string; blockType: 'richTextBlock'; @@ -76,9 +82,11 @@ export interface BlocksGlobal { export interface PublicUser { email?: string; resetPasswordToken?: string; + resetPasswordExpiration?: string; _verified?: boolean; _verificationToken?: string; loginAttempts?: number; + lockUntil?: string; adminOnly?: string; } /** @@ -88,10 +96,13 @@ export interface PublicUser { export interface Admin { email?: string; resetPasswordToken?: string; + resetPasswordExpiration?: string; enableAPIKey?: boolean; apiKey?: string; apiKeyIndex?: string; loginAttempts?: number; + lockUntil?: string; + roles: ('admin' | 'editor' | 'moderator' | 'user' | 'viewer')[]; publicUser?: (string | PublicUser)[]; } /** @@ -103,6 +114,11 @@ export interface AllFields { descriptionText?: string; descriptionFunction?: string; image?: string | Media; + select: 'option-1' | 'option-2' | 'option-3' | 'option-4'; + selectMany: ('option-1' | 'option-2' | 'option-3' | 'option-4')[]; + dayOnlyDateFieldExample: string; + timeOnlyDateFieldExample?: string; + radioGroupExample: 'option-1' | 'option-2' | 'option-3'; email?: string; number?: number; group?: { @@ -157,8 +173,12 @@ export interface AllFields { relationTo: 'conditions'; }; textarea?: string; + richText: { + [k: string]: unknown; + }[]; slug: string; checkbox?: boolean; + dateFieldExample?: string; } /** * This interface was referenced by `Config`'s JSON-Schema @@ -321,6 +341,7 @@ export interface File { filename?: string; mimeType?: string; filesize?: number; + type: 'Type 1' | 'Type 2' | 'Type 3'; owner: string | Admin; } /** @@ -330,6 +351,9 @@ export interface File { export interface DefaultValueTest { text?: string; image?: string | Media; + select?: 'option-1' | 'option-2' | 'option-3' | 'option-4'; + selectMany?: ('option-1' | 'option-2' | 'option-3' | 'option-4')[]; + radioGroupExample?: 'option-1' | 'option-2' | 'option-3'; email?: string; number?: number; group?: { @@ -386,6 +410,9 @@ export interface DefaultValueTest { textarea?: string; slug?: string; checkbox?: boolean; + richText?: { + [k: string]: unknown; + }[]; } /** * This interface was referenced by `Config`'s JSON-Schema @@ -583,12 +610,24 @@ export interface StrictAccess { * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "rich-text". */ -export interface RichText {} +export interface RichText { + defaultRichText: { + [k: string]: unknown; + }[]; + customRichText: { + [k: string]: unknown; + }[]; +} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "select". */ -export interface Select {} +export interface Select { + Select: 'one' | 'two' | 'three'; + SelectHasMany: ('one' | 'two' | 'three')[]; + SelectJustStrings: ('blue' | 'green' | 'yellow')[]; + Radio: 'one' | 'two' | 'three'; +} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "validations". @@ -641,4 +680,7 @@ export interface UnstoredMedia { * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "geolocation". */ -export interface Geolocation {} +export interface Geolocation { + location?: [number, number]; + localizedPoint?: [number, number]; +} diff --git a/src/bin/generateTypes.ts b/src/bin/generateTypes.ts index f330fd3fef..22859efcd4 100644 --- a/src/bin/generateTypes.ts +++ b/src/bin/generateTypes.ts @@ -2,7 +2,7 @@ import fs from 'fs'; import type { JSONSchema4 } from 'json-schema'; import { compile } from 'json-schema-to-typescript'; -import { fieldIsPresentationalOnly, fieldAffectsData, Field } from '../fields/config/types'; +import { fieldAffectsData, Field, Option } from '../fields/config/types'; import { SanitizedCollectionConfig } from '../collections/config/types'; import { SanitizedConfig } from '../config/types'; import loadConfig from '../config/load'; @@ -23,6 +23,16 @@ function getCollectionIDType(collections: SanitizedCollectionConfig[], slug): 's return undefined; } +function returnOptionEnums(options: Option[]): string[] { + return options.map((option) => { + if (typeof option === 'object' && 'value' in option) { + return option.value; + } + + return option; + }); +} + function generateFieldTypes(config: SanitizedConfig, fields: Field[]): { properties: { [k: string]: JSONSchema4; @@ -41,7 +51,8 @@ function generateFieldTypes(config: SanitizedConfig, fields: Field[]): { case 'text': case 'textarea': case 'code': - case 'email': { + case 'email': + case 'date': { fieldSchema = { type: 'string' }; break; } @@ -56,9 +67,60 @@ function generateFieldTypes(config: SanitizedConfig, fields: Field[]): { break; } - // TODO: - // Add enum types like Radio and Select - // Add point field type + case 'richText': { + fieldSchema = { + type: 'array', + items: { + type: 'object', + }, + }; + + break; + } + + case 'radio': { + fieldSchema = { + type: 'string', + enum: returnOptionEnums(field.options), + }; + + break; + } + + case 'select': { + const selectType: JSONSchema4 = { + type: 'string', + enum: returnOptionEnums(field.options), + }; + + if (field.hasMany) { + fieldSchema = { + type: 'array', + items: selectType, + }; + } else { + fieldSchema = selectType; + } + + break; + } + + case 'point': { + fieldSchema = { + type: 'array', + minItems: 2, + maxItems: 2, + items: [ + { + type: 'number', + }, + { + type: 'number', + }, + ], + }; + break; + } case 'relationship': { if (Array.isArray(field.relationTo)) { @@ -230,12 +292,6 @@ function generateFieldTypes(config: SanitizedConfig, fields: Field[]): { } } - let default_ = {}; - - if (!fieldIsPresentationalOnly(field) && fieldAffectsData(field) && typeof field.defaultValue !== 'undefined') { - default_ = { default: field.defaultValue }; - } - if (fieldSchema && fieldAffectsData(field)) { return [ ...properties, @@ -243,7 +299,6 @@ function generateFieldTypes(config: SanitizedConfig, fields: Field[]): { field.name, { ...fieldSchema, - ...default_, }, ], ];