Skip to content

Commit 6badb5f

Browse files
authored
chore(live-preview): enable TypeScript strict (#11840)
1 parent 5b0e0ab commit 6badb5f

File tree

6 files changed

+35
-27
lines changed

6 files changed

+35
-27
lines changed

packages/live-preview/src/handleMessage.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import type { FieldSchemaJSON } from 'payload'
2+
13
import type { LivePreviewMessageEvent } from './types.js'
24

35
import { isLivePreviewEvent } from './isLivePreviewEvent.js'
46
import { mergeData } from './mergeData.js'
57

6-
const _payloadLivePreview = {
8+
const _payloadLivePreview: {
9+
fieldSchema: FieldSchemaJSON | undefined
10+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11+
previousData: any
12+
} = {
713
/**
814
* For performance reasons, `fieldSchemaJSON` will only be sent once on the initial message
915
* We need to cache this value so that it can be used across subsequent messages
@@ -18,7 +24,7 @@ const _payloadLivePreview = {
1824
previousData: undefined,
1925
}
2026

21-
export const handleMessage = async <T>(args: {
27+
export const handleMessage = async <T extends Record<string, any>>(args: {
2228
apiRoute?: string
2329
depth?: number
2430
event: LivePreviewMessageEvent<T>

packages/live-preview/src/mergeData.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ import type { PopulationsByCollection } from './types.js'
44

55
import { traverseFields } from './traverseFields.js'
66

7-
const defaultRequestHandler = ({ apiPath, endpoint, serverURL }) => {
7+
const defaultRequestHandler = ({
8+
apiPath,
9+
endpoint,
10+
serverURL,
11+
}: {
12+
apiPath: string
13+
endpoint: string
14+
serverURL: string
15+
}) => {
816
const url = `${serverURL}${apiPath}/${endpoint}`
917
return fetch(url, {
1018
credentials: 'include',
@@ -19,7 +27,7 @@ const defaultRequestHandler = ({ apiPath, endpoint, serverURL }) => {
1927
// Instead, we keep track of the old locale ourselves and trigger a re-population when it changes
2028
let prevLocale: string | undefined
2129

22-
export const mergeData = async <T>(args: {
30+
export const mergeData = async <T extends Record<string, any>>(args: {
2331
apiRoute?: string
2432
collectionPopulationRequestHandler?: ({
2533
apiPath,
@@ -86,7 +94,7 @@ export const mergeData = async <T>(args: {
8694

8795
if (res?.docs?.length > 0) {
8896
res.docs.forEach((doc) => {
89-
populationsByCollection[collection].forEach((population) => {
97+
populationsByCollection[collection]?.forEach((population) => {
9098
if (population.id === doc.id) {
9199
population.ref[population.accessor] = doc
92100
}

packages/live-preview/src/subscribe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { handleMessage } from './handleMessage.js'
22

3-
export const subscribe = <T>(args: {
3+
export const subscribe = <T extends Record<string, any>>(args: {
44
apiRoute?: string
55
callback: (data: T) => void
66
depth?: number

packages/live-preview/src/traverseFields.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import type { DocumentEvent } from 'payload'
2-
import type { fieldSchemaToJSON } from 'payload/shared'
1+
import type { DocumentEvent, FieldSchemaJSON } from 'payload'
32

43
import type { PopulationsByCollection } from './types.js'
54

65
import { traverseRichText } from './traverseRichText.js'
76

8-
export const traverseFields = <T>(args: {
7+
export const traverseFields = <T extends Record<string, any>>(args: {
98
externallyUpdatedRelationship?: DocumentEvent
10-
fieldSchema: ReturnType<typeof fieldSchemaToJSON>
9+
fieldSchema: FieldSchemaJSON
1110
incomingData: T
1211
localeChanged: boolean
1312
populationsByCollection: PopulationsByCollection
14-
result: T
13+
result: Record<string, any>
1514
}): void => {
1615
const {
1716
externallyUpdatedRelationship,
@@ -48,7 +47,7 @@ export const traverseFields = <T>(args: {
4847

4948
traverseFields({
5049
externallyUpdatedRelationship,
51-
fieldSchema: fieldSchema.fields,
50+
fieldSchema: fieldSchema.fields!,
5251
incomingData: incomingRow,
5352
localeChanged,
5453
populationsByCollection,
@@ -64,7 +63,7 @@ export const traverseFields = <T>(args: {
6463
case 'blocks':
6564
if (Array.isArray(incomingData[fieldName])) {
6665
result[fieldName] = incomingData[fieldName].map((incomingBlock, i) => {
67-
const incomingBlockJSON = fieldSchema.blocks[incomingBlock.blockType]
66+
const incomingBlockJSON = fieldSchema.blocks?.[incomingBlock.blockType]
6867

6968
if (!result[fieldName]) {
7069
result[fieldName] = []
@@ -82,7 +81,7 @@ export const traverseFields = <T>(args: {
8281

8382
traverseFields({
8483
externallyUpdatedRelationship,
85-
fieldSchema: incomingBlockJSON.fields,
84+
fieldSchema: incomingBlockJSON!.fields!,
8685
incomingData: incomingBlock,
8786
localeChanged,
8887
populationsByCollection,
@@ -106,7 +105,7 @@ export const traverseFields = <T>(args: {
106105

107106
traverseFields({
108107
externallyUpdatedRelationship,
109-
fieldSchema: fieldSchema.fields,
108+
fieldSchema: fieldSchema.fields!,
110109
incomingData: incomingData[fieldName] || {},
111110
localeChanged,
112111
populationsByCollection,
@@ -166,11 +165,11 @@ export const traverseFields = <T>(args: {
166165
incomingRelation === externallyUpdatedRelationship?.id
167166

168167
if (hasChanged || hasUpdated || localeChanged) {
169-
if (!populationsByCollection[fieldSchema.relationTo]) {
170-
populationsByCollection[fieldSchema.relationTo] = []
168+
if (!populationsByCollection[fieldSchema.relationTo!]) {
169+
populationsByCollection[fieldSchema.relationTo!] = []
171170
}
172171

173-
populationsByCollection[fieldSchema.relationTo].push({
172+
populationsByCollection[fieldSchema.relationTo!]?.push({
174173
id: incomingRelation,
175174
accessor: i,
176175
ref: result[fieldName],
@@ -265,11 +264,11 @@ export const traverseFields = <T>(args: {
265264
// if the new value is not empty, populate it
266265
// otherwise set the value to null
267266
if (newID) {
268-
if (!populationsByCollection[fieldSchema.relationTo]) {
269-
populationsByCollection[fieldSchema.relationTo] = []
267+
if (!populationsByCollection[fieldSchema.relationTo!]) {
268+
populationsByCollection[fieldSchema.relationTo!] = []
270269
}
271270

272-
populationsByCollection[fieldSchema.relationTo].push({
271+
populationsByCollection[fieldSchema.relationTo!]?.push({
273272
id: newID,
274273
accessor: fieldName,
275274
ref: result as Record<string, unknown>,

packages/live-preview/src/traverseRichText.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const traverseRichText = ({
7979
populationsByCollection[incomingData.relationTo] = []
8080
}
8181

82-
populationsByCollection[incomingData.relationTo].push({
82+
populationsByCollection[incomingData.relationTo]?.push({
8383
id:
8484
incomingData[key] && typeof incomingData[key] === 'object'
8585
? incomingData[key].id

packages/live-preview/tsconfig.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
{
22
"extends": "../../tsconfig.base.json",
3-
"compilerOptions": {
4-
/* TODO: remove the following lines */
5-
"strict": false,
6-
"noUncheckedIndexedAccess": false,
7-
},
83
"references": [{ "path": "../payload" }]
94
}

0 commit comments

Comments
 (0)