chore(live-preview): strongly types message events (#10148)

Live Preview message events were typed with the generic `MessageEvent`
interface without passing any of the Live Preview specific properties,
leading to unknown types upon use. To fix this, there is a new
`LivePreviewMessageEvent` which properly extends the underlying
`MessageEvent` interface, providing much needed type safety to these
functions. In the same vein, the `UpdatedDocument` type was not being
properly shared across packages, leading to multiple independent
definitions of this type. This type is now exported from `payload`
itself and renamed to `DocumentEvent` for improved semantics. Same with
the `FieldSchemaJSON` type. This PR also adjusts where globally scoped
variables are set, putting them within the shared `_payloadLivePreview`
namespace instead of setting them individually at the top-level.
This commit is contained in:
Jacob Fletcher
2024-12-23 13:19:52 -05:00
committed by GitHub
parent 0588394a47
commit 466f109152
12 changed files with 66 additions and 99 deletions

View File

@@ -1,6 +1,11 @@
import type { Payload } from 'payload'
import { handleMessage, mergeData, traverseRichText } from '@payloadcms/live-preview'
import {
handleMessage,
type LivePreviewMessageEvent,
mergeData,
traverseRichText,
} from '@payloadcms/live-preview'
import path from 'path'
import { getFileByPath } from 'payload'
import { fieldSchemaToJSON } from 'payload/shared'
@@ -97,7 +102,7 @@ describe('Collections - Live Preview', () => {
type: 'payload-live-preview',
},
origin: serverURL,
} as MessageEvent,
} as MessageEvent as LivePreviewMessageEvent<Page>,
initialData: {
title: 'Test Page',
} as Page,
@@ -118,7 +123,7 @@ describe('Collections - Live Preview', () => {
type: 'payload-live-preview',
},
origin: serverURL,
} as MessageEvent,
} as MessageEvent as LivePreviewMessageEvent<Page>,
initialData: {
title: 'Test Page',
} as Page,

View File

@@ -135,9 +135,6 @@ export interface Page {
} | null);
url?: string | null;
label: string;
/**
* Choose how the link should be rendered.
*/
appearance?: ('primary' | 'secondary') | null;
};
id?: string | null;
@@ -172,9 +169,6 @@ export interface Page {
} | null);
url?: string | null;
label: string;
/**
* Choose how the link should be rendered.
*/
appearance?: ('default' | 'primary' | 'secondary') | null;
};
id?: string | null;
@@ -208,18 +202,12 @@ export interface Page {
value: string | Post;
}[]
| null;
/**
* This field is auto-populated after-read
*/
populatedDocs?:
| {
relationTo: 'posts';
value: string | Post;
}[]
| null;
/**
* This field is auto-populated after-read
*/
populatedDocsTotal?: number | null;
id?: string | null;
blockName?: string | null;
@@ -379,9 +367,6 @@ export interface Post {
} | null);
url?: string | null;
label: string;
/**
* Choose how the link should be rendered.
*/
appearance?: ('primary' | 'secondary') | null;
};
id?: string | null;
@@ -416,9 +401,6 @@ export interface Post {
} | null);
url?: string | null;
label: string;
/**
* Choose how the link should be rendered.
*/
appearance?: ('default' | 'primary' | 'secondary') | null;
};
id?: string | null;
@@ -452,18 +434,12 @@ export interface Post {
value: string | Post;
}[]
| null;
/**
* This field is auto-populated after-read
*/
populatedDocs?:
| {
relationTo: 'posts';
value: string | Post;
}[]
| null;
/**
* This field is auto-populated after-read
*/
populatedDocsTotal?: number | null;
id?: string | null;
blockName?: string | null;
@@ -534,9 +510,6 @@ export interface Ssr {
} | null);
url?: string | null;
label: string;
/**
* Choose how the link should be rendered.
*/
appearance?: ('primary' | 'secondary') | null;
};
id?: string | null;
@@ -571,9 +544,6 @@ export interface Ssr {
} | null);
url?: string | null;
label: string;
/**
* Choose how the link should be rendered.
*/
appearance?: ('default' | 'primary' | 'secondary') | null;
};
id?: string | null;
@@ -607,18 +577,12 @@ export interface Ssr {
value: string | Post;
}[]
| null;
/**
* This field is auto-populated after-read
*/
populatedDocs?:
| {
relationTo: 'posts';
value: string | Post;
}[]
| null;
/**
* This field is auto-populated after-read
*/
populatedDocsTotal?: number | null;
id?: string | null;
blockName?: string | null;
@@ -677,9 +641,6 @@ export interface SsrAutosave {
} | null);
url?: string | null;
label: string;
/**
* Choose how the link should be rendered.
*/
appearance?: ('primary' | 'secondary') | null;
};
id?: string | null;
@@ -714,9 +675,6 @@ export interface SsrAutosave {
} | null);
url?: string | null;
label: string;
/**
* Choose how the link should be rendered.
*/
appearance?: ('default' | 'primary' | 'secondary') | null;
};
id?: string | null;
@@ -750,18 +708,12 @@ export interface SsrAutosave {
value: string | Post;
}[]
| null;
/**
* This field is auto-populated after-read
*/
populatedDocs?:
| {
relationTo: 'posts';
value: string | Post;
}[]
| null;
/**
* This field is auto-populated after-read
*/
populatedDocsTotal?: number | null;
id?: string | null;
blockName?: string | null;
@@ -1393,9 +1345,6 @@ export interface Header {
} | null);
url?: string | null;
label: string;
/**
* Choose how the link should be rendered.
*/
appearance?: ('default' | 'primary' | 'secondary') | null;
};
id?: string | null;
@@ -1426,9 +1375,6 @@ export interface Footer {
} | null);
url?: string | null;
label: string;
/**
* Choose how the link should be rendered.
*/
appearance?: ('default' | 'primary' | 'secondary') | null;
};
id?: string | null;