chore(live-preview): enable TypeScript strict (#11840)

This commit is contained in:
Germán Jabloñski
2025-04-01 10:03:39 -03:00
committed by GitHub
parent 5b0e0ab788
commit 6badb5ffcf
6 changed files with 35 additions and 27 deletions

View File

@@ -1,9 +1,15 @@
import type { FieldSchemaJSON } from 'payload'
import type { LivePreviewMessageEvent } from './types.js' import type { LivePreviewMessageEvent } from './types.js'
import { isLivePreviewEvent } from './isLivePreviewEvent.js' import { isLivePreviewEvent } from './isLivePreviewEvent.js'
import { mergeData } from './mergeData.js' import { mergeData } from './mergeData.js'
const _payloadLivePreview = { const _payloadLivePreview: {
fieldSchema: FieldSchemaJSON | undefined
// eslint-disable-next-line @typescript-eslint/no-explicit-any
previousData: any
} = {
/** /**
* For performance reasons, `fieldSchemaJSON` will only be sent once on the initial message * For performance reasons, `fieldSchemaJSON` will only be sent once on the initial message
* We need to cache this value so that it can be used across subsequent messages * We need to cache this value so that it can be used across subsequent messages
@@ -18,7 +24,7 @@ const _payloadLivePreview = {
previousData: undefined, previousData: undefined,
} }
export const handleMessage = async <T>(args: { export const handleMessage = async <T extends Record<string, any>>(args: {
apiRoute?: string apiRoute?: string
depth?: number depth?: number
event: LivePreviewMessageEvent<T> event: LivePreviewMessageEvent<T>

View File

@@ -4,7 +4,15 @@ import type { PopulationsByCollection } from './types.js'
import { traverseFields } from './traverseFields.js' import { traverseFields } from './traverseFields.js'
const defaultRequestHandler = ({ apiPath, endpoint, serverURL }) => { const defaultRequestHandler = ({
apiPath,
endpoint,
serverURL,
}: {
apiPath: string
endpoint: string
serverURL: string
}) => {
const url = `${serverURL}${apiPath}/${endpoint}` const url = `${serverURL}${apiPath}/${endpoint}`
return fetch(url, { return fetch(url, {
credentials: 'include', credentials: 'include',
@@ -19,7 +27,7 @@ const defaultRequestHandler = ({ apiPath, endpoint, serverURL }) => {
// Instead, we keep track of the old locale ourselves and trigger a re-population when it changes // Instead, we keep track of the old locale ourselves and trigger a re-population when it changes
let prevLocale: string | undefined let prevLocale: string | undefined
export const mergeData = async <T>(args: { export const mergeData = async <T extends Record<string, any>>(args: {
apiRoute?: string apiRoute?: string
collectionPopulationRequestHandler?: ({ collectionPopulationRequestHandler?: ({
apiPath, apiPath,
@@ -86,7 +94,7 @@ export const mergeData = async <T>(args: {
if (res?.docs?.length > 0) { if (res?.docs?.length > 0) {
res.docs.forEach((doc) => { res.docs.forEach((doc) => {
populationsByCollection[collection].forEach((population) => { populationsByCollection[collection]?.forEach((population) => {
if (population.id === doc.id) { if (population.id === doc.id) {
population.ref[population.accessor] = doc population.ref[population.accessor] = doc
} }

View File

@@ -1,6 +1,6 @@
import { handleMessage } from './handleMessage.js' import { handleMessage } from './handleMessage.js'
export const subscribe = <T>(args: { export const subscribe = <T extends Record<string, any>>(args: {
apiRoute?: string apiRoute?: string
callback: (data: T) => void callback: (data: T) => void
depth?: number depth?: number

View File

@@ -1,17 +1,16 @@
import type { DocumentEvent } from 'payload' import type { DocumentEvent, FieldSchemaJSON } from 'payload'
import type { fieldSchemaToJSON } from 'payload/shared'
import type { PopulationsByCollection } from './types.js' import type { PopulationsByCollection } from './types.js'
import { traverseRichText } from './traverseRichText.js' import { traverseRichText } from './traverseRichText.js'
export const traverseFields = <T>(args: { export const traverseFields = <T extends Record<string, any>>(args: {
externallyUpdatedRelationship?: DocumentEvent externallyUpdatedRelationship?: DocumentEvent
fieldSchema: ReturnType<typeof fieldSchemaToJSON> fieldSchema: FieldSchemaJSON
incomingData: T incomingData: T
localeChanged: boolean localeChanged: boolean
populationsByCollection: PopulationsByCollection populationsByCollection: PopulationsByCollection
result: T result: Record<string, any>
}): void => { }): void => {
const { const {
externallyUpdatedRelationship, externallyUpdatedRelationship,
@@ -48,7 +47,7 @@ export const traverseFields = <T>(args: {
traverseFields({ traverseFields({
externallyUpdatedRelationship, externallyUpdatedRelationship,
fieldSchema: fieldSchema.fields, fieldSchema: fieldSchema.fields!,
incomingData: incomingRow, incomingData: incomingRow,
localeChanged, localeChanged,
populationsByCollection, populationsByCollection,
@@ -64,7 +63,7 @@ export const traverseFields = <T>(args: {
case 'blocks': case 'blocks':
if (Array.isArray(incomingData[fieldName])) { if (Array.isArray(incomingData[fieldName])) {
result[fieldName] = incomingData[fieldName].map((incomingBlock, i) => { result[fieldName] = incomingData[fieldName].map((incomingBlock, i) => {
const incomingBlockJSON = fieldSchema.blocks[incomingBlock.blockType] const incomingBlockJSON = fieldSchema.blocks?.[incomingBlock.blockType]
if (!result[fieldName]) { if (!result[fieldName]) {
result[fieldName] = [] result[fieldName] = []
@@ -82,7 +81,7 @@ export const traverseFields = <T>(args: {
traverseFields({ traverseFields({
externallyUpdatedRelationship, externallyUpdatedRelationship,
fieldSchema: incomingBlockJSON.fields, fieldSchema: incomingBlockJSON!.fields!,
incomingData: incomingBlock, incomingData: incomingBlock,
localeChanged, localeChanged,
populationsByCollection, populationsByCollection,
@@ -106,7 +105,7 @@ export const traverseFields = <T>(args: {
traverseFields({ traverseFields({
externallyUpdatedRelationship, externallyUpdatedRelationship,
fieldSchema: fieldSchema.fields, fieldSchema: fieldSchema.fields!,
incomingData: incomingData[fieldName] || {}, incomingData: incomingData[fieldName] || {},
localeChanged, localeChanged,
populationsByCollection, populationsByCollection,
@@ -166,11 +165,11 @@ export const traverseFields = <T>(args: {
incomingRelation === externallyUpdatedRelationship?.id incomingRelation === externallyUpdatedRelationship?.id
if (hasChanged || hasUpdated || localeChanged) { if (hasChanged || hasUpdated || localeChanged) {
if (!populationsByCollection[fieldSchema.relationTo]) { if (!populationsByCollection[fieldSchema.relationTo!]) {
populationsByCollection[fieldSchema.relationTo] = [] populationsByCollection[fieldSchema.relationTo!] = []
} }
populationsByCollection[fieldSchema.relationTo].push({ populationsByCollection[fieldSchema.relationTo!]?.push({
id: incomingRelation, id: incomingRelation,
accessor: i, accessor: i,
ref: result[fieldName], ref: result[fieldName],
@@ -265,11 +264,11 @@ export const traverseFields = <T>(args: {
// if the new value is not empty, populate it // if the new value is not empty, populate it
// otherwise set the value to null // otherwise set the value to null
if (newID) { if (newID) {
if (!populationsByCollection[fieldSchema.relationTo]) { if (!populationsByCollection[fieldSchema.relationTo!]) {
populationsByCollection[fieldSchema.relationTo] = [] populationsByCollection[fieldSchema.relationTo!] = []
} }
populationsByCollection[fieldSchema.relationTo].push({ populationsByCollection[fieldSchema.relationTo!]?.push({
id: newID, id: newID,
accessor: fieldName, accessor: fieldName,
ref: result as Record<string, unknown>, ref: result as Record<string, unknown>,

View File

@@ -79,7 +79,7 @@ export const traverseRichText = ({
populationsByCollection[incomingData.relationTo] = [] populationsByCollection[incomingData.relationTo] = []
} }
populationsByCollection[incomingData.relationTo].push({ populationsByCollection[incomingData.relationTo]?.push({
id: id:
incomingData[key] && typeof incomingData[key] === 'object' incomingData[key] && typeof incomingData[key] === 'object'
? incomingData[key].id ? incomingData[key].id

View File

@@ -1,9 +1,4 @@
{ {
"extends": "../../tsconfig.base.json", "extends": "../../tsconfig.base.json",
"compilerOptions": {
/* TODO: remove the following lines */
"strict": false,
"noUncheckedIndexedAccess": false,
},
"references": [{ "path": "../payload" }] "references": [{ "path": "../payload" }]
} }