Skip to content

Commit 35c0404

Browse files
feat(live-preview): expose requestHandler to subscribe.ts (#10947)
### What? As described in #10946, allow passing a custom `collectionPopulationRequestHandler` function to `subscribe`, which passes it along to `handleMessage` and `mergeData` ### Why? `mergeData` already supports a custom function for this, that functionality however isn't exposed. My use case so far was passing along custom Authorization headers. ### How? Move the functions type defined in `mergeData` to a dedicated `CollectionPopulationRequestHandler` type, reuse it across `subscribe`, `handleMessage` and `mergeData`. --------- Co-authored-by: Jacob Fletcher <jacobsfletch@gmail.com>
1 parent cfe8c97 commit 35c0404

File tree

5 files changed

+59
-46
lines changed

5 files changed

+59
-46
lines changed

packages/live-preview/src/handleMessage.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { FieldSchemaJSON } from 'payload'
22

3-
import type { LivePreviewMessageEvent } from './types.js'
3+
import type { CollectionPopulationRequestHandler, LivePreviewMessageEvent } from './types.js'
44

55
import { isLivePreviewEvent } from './isLivePreviewEvent.js'
66
import { mergeData } from './mergeData.js'
@@ -29,9 +29,10 @@ export const handleMessage = async <T extends Record<string, any>>(args: {
2929
depth?: number
3030
event: LivePreviewMessageEvent<T>
3131
initialData: T
32+
requestHandler?: CollectionPopulationRequestHandler
3233
serverURL: string
3334
}): Promise<T> => {
34-
const { apiRoute, depth, event, initialData, serverURL } = args
35+
const { apiRoute, depth, event, initialData, requestHandler, serverURL } = args
3536

3637
if (isLivePreviewEvent(event, serverURL)) {
3738
const { data, externallyUpdatedRelationship, fieldSchemaJSON, locale } = event.data
@@ -57,6 +58,7 @@ export const handleMessage = async <T extends Record<string, any>>(args: {
5758
incomingData: data,
5859
initialData: _payloadLivePreview?.previousData || initialData,
5960
locale,
61+
requestHandler,
6062
serverURL,
6163
})
6264

packages/live-preview/src/mergeData.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { DocumentEvent, FieldSchemaJSON, PaginatedDocs } from 'payload'
22

3-
import type { PopulationsByCollection } from './types.js'
3+
import type { CollectionPopulationRequestHandler, PopulationsByCollection } from './types.js'
44

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

@@ -29,21 +29,17 @@ let prevLocale: string | undefined
2929

3030
export const mergeData = async <T extends Record<string, any>>(args: {
3131
apiRoute?: string
32-
collectionPopulationRequestHandler?: ({
33-
apiPath,
34-
endpoint,
35-
serverURL,
36-
}: {
37-
apiPath: string
38-
endpoint: string
39-
serverURL: string
40-
}) => Promise<Response>
32+
/**
33+
* @deprecated Use `requestHandler` instead
34+
*/
35+
collectionPopulationRequestHandler?: CollectionPopulationRequestHandler
4136
depth?: number
4237
externallyUpdatedRelationship?: DocumentEvent
4338
fieldSchema: FieldSchemaJSON
4439
incomingData: Partial<T>
4540
initialData: T
4641
locale?: string
42+
requestHandler?: CollectionPopulationRequestHandler
4743
returnNumberOfRequests?: boolean
4844
serverURL: string
4945
}): Promise<
@@ -81,7 +77,8 @@ export const mergeData = async <T extends Record<string, any>>(args: {
8177
let res: PaginatedDocs
8278

8379
const ids = new Set(populations.map(({ id }) => id))
84-
const requestHandler = args.collectionPopulationRequestHandler || defaultRequestHandler
80+
const requestHandler =
81+
args.collectionPopulationRequestHandler || args.requestHandler || defaultRequestHandler
8582

8683
try {
8784
res = await requestHandler({

packages/live-preview/src/subscribe.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1+
import type { CollectionPopulationRequestHandler } from './types.js'
2+
13
import { handleMessage } from './handleMessage.js'
24

35
export const subscribe = <T extends Record<string, any>>(args: {
46
apiRoute?: string
57
callback: (data: T) => void
68
depth?: number
79
initialData: T
10+
requestHandler?: CollectionPopulationRequestHandler
811
serverURL: string
912
}): ((event: MessageEvent) => Promise<void> | void) => {
10-
const { apiRoute, callback, depth, initialData, serverURL } = args
13+
const { apiRoute, callback, depth, initialData, requestHandler, serverURL } = args
1114

1215
const onMessage = async (event: MessageEvent) => {
1316
const mergedData = await handleMessage<T>({
1417
apiRoute,
1518
depth,
1619
event,
1720
initialData,
21+
requestHandler,
1822
serverURL,
1923
})
2024

packages/live-preview/src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import type { DocumentEvent, FieldSchemaJSON } from 'payload'
22

3+
export type CollectionPopulationRequestHandler = ({
4+
apiPath,
5+
endpoint,
6+
serverURL,
7+
}: {
8+
apiPath: string
9+
endpoint: string
10+
serverURL: string
11+
}) => Promise<Response>
12+
313
export type LivePreviewArgs = {}
414

515
export type LivePreview = void

0 commit comments

Comments
 (0)