@payloadcms/live-preview 3.55.0-internal.f8c92b5 → 3.55.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"handleMessage.d.ts","sourceRoot":"","sources":["../src/handleMessage.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kCAAkC,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAwB7F,eAAO,MAAM,aAAa,GAAU,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ;IACvE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,uBAAuB,CAAC,CAAC,CAAC,CAAA;IACjC,WAAW,EAAE,CAAC,CAAA;IACd,cAAc,CAAC,EAAE,kCAAkC,CAAA;IACnD,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,OAAO,CAAC,CAAC,CAyCZ,CAAA"}
1
+ {"version":3,"file":"handleMessage.d.ts","sourceRoot":"","sources":["../src/handleMessage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kCAAkC,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAgB7F,eAAO,MAAM,aAAa,GAAU,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ;IACvE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,uBAAuB,CAAC,CAAC,CAAC,CAAA;IACjC,WAAW,EAAE,CAAC,CAAA;IACd,cAAc,CAAC,EAAE,kCAAkC,CAAA;IACnD,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,OAAO,CAAC,CAAC,CA4BZ,CAAA"}
@@ -2,12 +2,6 @@ import { isLivePreviewEvent } from './isLivePreviewEvent.js';
2
2
  import { mergeData } from './mergeData.js';
3
3
  const _payloadLivePreview = {
4
4
  /**
5
- * For performance reasons, `fieldSchemaJSON` will only be sent once on the initial message
6
- * We need to cache this value so that it can be used across subsequent messages
7
- * To do this, save `fieldSchemaJSON` when it arrives as a global variable
8
- * Send this cached value to `mergeData`, instead of `eventData.fieldSchemaJSON` directly
9
- */ fieldSchema: undefined,
10
- /**
11
5
  * Each time the data is merged, cache the result as a `previousData` variable
12
6
  * This will ensure changes compound overtop of each other
13
7
  */ previousData: undefined
@@ -15,20 +9,12 @@ const _payloadLivePreview = {
15
9
  export const handleMessage = async (args)=>{
16
10
  const { apiRoute, depth, event, initialData, requestHandler, serverURL } = args;
17
11
  if (isLivePreviewEvent(event, serverURL)) {
18
- const { data, externallyUpdatedRelationship, fieldSchemaJSON, locale } = event.data;
19
- if (!_payloadLivePreview?.fieldSchema && fieldSchemaJSON) {
20
- _payloadLivePreview.fieldSchema = fieldSchemaJSON;
21
- }
22
- if (!_payloadLivePreview?.fieldSchema) {
23
- // eslint-disable-next-line no-console
24
- console.warn('Payload Live Preview: No `fieldSchemaJSON` was received from the parent window. Unable to merge data.');
25
- return initialData;
26
- }
12
+ const { collectionSlug, data, globalSlug, locale } = event.data;
27
13
  const mergedData = await mergeData({
28
14
  apiRoute,
15
+ collectionSlug,
29
16
  depth,
30
- externallyUpdatedRelationship,
31
- fieldSchema: _payloadLivePreview.fieldSchema,
17
+ globalSlug,
32
18
  incomingData: data,
33
19
  initialData: _payloadLivePreview?.previousData || initialData,
34
20
  locale,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/handleMessage.ts"],"sourcesContent":["import type { FieldSchemaJSON } from 'payload'\n\nimport type { CollectionPopulationRequestHandler, LivePreviewMessageEvent } from './types.js'\n\nimport { isLivePreviewEvent } from './isLivePreviewEvent.js'\nimport { mergeData } from './mergeData.js'\n\nconst _payloadLivePreview: {\n fieldSchema: FieldSchemaJSON | undefined\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n previousData: any\n} = {\n /**\n * For performance reasons, `fieldSchemaJSON` will only be sent once on the initial message\n * We need to cache this value so that it can be used across subsequent messages\n * To do this, save `fieldSchemaJSON` when it arrives as a global variable\n * Send this cached value to `mergeData`, instead of `eventData.fieldSchemaJSON` directly\n */\n fieldSchema: undefined,\n /**\n * Each time the data is merged, cache the result as a `previousData` variable\n * This will ensure changes compound overtop of each other\n */\n previousData: undefined,\n}\n\nexport const handleMessage = async <T extends Record<string, any>>(args: {\n apiRoute?: string\n depth?: number\n event: LivePreviewMessageEvent<T>\n initialData: T\n requestHandler?: CollectionPopulationRequestHandler\n serverURL: string\n}): Promise<T> => {\n const { apiRoute, depth, event, initialData, requestHandler, serverURL } = args\n\n if (isLivePreviewEvent(event, serverURL)) {\n const { data, externallyUpdatedRelationship, fieldSchemaJSON, locale } = event.data\n\n if (!_payloadLivePreview?.fieldSchema && fieldSchemaJSON) {\n _payloadLivePreview.fieldSchema = fieldSchemaJSON\n }\n\n if (!_payloadLivePreview?.fieldSchema) {\n // eslint-disable-next-line no-console\n console.warn(\n 'Payload Live Preview: No `fieldSchemaJSON` was received from the parent window. Unable to merge data.',\n )\n\n return initialData\n }\n\n const mergedData = await mergeData<T>({\n apiRoute,\n depth,\n externallyUpdatedRelationship,\n fieldSchema: _payloadLivePreview.fieldSchema,\n incomingData: data,\n initialData: _payloadLivePreview?.previousData || initialData,\n locale,\n requestHandler,\n serverURL,\n })\n\n _payloadLivePreview.previousData = mergedData\n\n return mergedData\n }\n\n if (!_payloadLivePreview.previousData) {\n _payloadLivePreview.previousData = initialData\n }\n\n return _payloadLivePreview.previousData as T\n}\n"],"names":["isLivePreviewEvent","mergeData","_payloadLivePreview","fieldSchema","undefined","previousData","handleMessage","args","apiRoute","depth","event","initialData","requestHandler","serverURL","data","externallyUpdatedRelationship","fieldSchemaJSON","locale","console","warn","mergedData","incomingData"],"mappings":"AAIA,SAASA,kBAAkB,QAAQ,0BAAyB;AAC5D,SAASC,SAAS,QAAQ,iBAAgB;AAE1C,MAAMC,sBAIF;IACF;;;;;GAKC,GACDC,aAAaC;IACb;;;GAGC,GACDC,cAAcD;AAChB;AAEA,OAAO,MAAME,gBAAgB,OAAsCC;IAQjE,MAAM,EAAEC,QAAQ,EAAEC,KAAK,EAAEC,KAAK,EAAEC,WAAW,EAAEC,cAAc,EAAEC,SAAS,EAAE,GAAGN;IAE3E,IAAIP,mBAAmBU,OAAOG,YAAY;QACxC,MAAM,EAAEC,IAAI,EAAEC,6BAA6B,EAAEC,eAAe,EAAEC,MAAM,EAAE,GAAGP,MAAMI,IAAI;QAEnF,IAAI,CAACZ,qBAAqBC,eAAea,iBAAiB;YACxDd,oBAAoBC,WAAW,GAAGa;QACpC;QAEA,IAAI,CAACd,qBAAqBC,aAAa;YACrC,sCAAsC;YACtCe,QAAQC,IAAI,CACV;YAGF,OAAOR;QACT;QAEA,MAAMS,aAAa,MAAMnB,UAAa;YACpCO;YACAC;YACAM;YACAZ,aAAaD,oBAAoBC,WAAW;YAC5CkB,cAAcP;YACdH,aAAaT,qBAAqBG,gBAAgBM;YAClDM;YACAL;YACAC;QACF;QAEAX,oBAAoBG,YAAY,GAAGe;QAEnC,OAAOA;IACT;IAEA,IAAI,CAAClB,oBAAoBG,YAAY,EAAE;QACrCH,oBAAoBG,YAAY,GAAGM;IACrC;IAEA,OAAOT,oBAAoBG,YAAY;AACzC,EAAC"}
1
+ {"version":3,"sources":["../src/handleMessage.ts"],"sourcesContent":["import type { CollectionPopulationRequestHandler, LivePreviewMessageEvent } from './types.js'\n\nimport { isLivePreviewEvent } from './isLivePreviewEvent.js'\nimport { mergeData } from './mergeData.js'\n\nconst _payloadLivePreview: {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n previousData: any\n} = {\n /**\n * Each time the data is merged, cache the result as a `previousData` variable\n * This will ensure changes compound overtop of each other\n */\n previousData: undefined,\n}\n\nexport const handleMessage = async <T extends Record<string, any>>(args: {\n apiRoute?: string\n depth?: number\n event: LivePreviewMessageEvent<T>\n initialData: T\n requestHandler?: CollectionPopulationRequestHandler\n serverURL: string\n}): Promise<T> => {\n const { apiRoute, depth, event, initialData, requestHandler, serverURL } = args\n\n if (isLivePreviewEvent(event, serverURL)) {\n const { collectionSlug, data, globalSlug, locale } = event.data\n\n const mergedData = await mergeData<T>({\n apiRoute,\n collectionSlug,\n depth,\n globalSlug,\n incomingData: data,\n initialData: _payloadLivePreview?.previousData || initialData,\n locale,\n requestHandler,\n serverURL,\n })\n\n _payloadLivePreview.previousData = mergedData\n\n return mergedData\n }\n\n if (!_payloadLivePreview.previousData) {\n _payloadLivePreview.previousData = initialData\n }\n\n return _payloadLivePreview.previousData as T\n}\n"],"names":["isLivePreviewEvent","mergeData","_payloadLivePreview","previousData","undefined","handleMessage","args","apiRoute","depth","event","initialData","requestHandler","serverURL","collectionSlug","data","globalSlug","locale","mergedData","incomingData"],"mappings":"AAEA,SAASA,kBAAkB,QAAQ,0BAAyB;AAC5D,SAASC,SAAS,QAAQ,iBAAgB;AAE1C,MAAMC,sBAGF;IACF;;;GAGC,GACDC,cAAcC;AAChB;AAEA,OAAO,MAAMC,gBAAgB,OAAsCC;IAQjE,MAAM,EAAEC,QAAQ,EAAEC,KAAK,EAAEC,KAAK,EAAEC,WAAW,EAAEC,cAAc,EAAEC,SAAS,EAAE,GAAGN;IAE3E,IAAIN,mBAAmBS,OAAOG,YAAY;QACxC,MAAM,EAAEC,cAAc,EAAEC,IAAI,EAAEC,UAAU,EAAEC,MAAM,EAAE,GAAGP,MAAMK,IAAI;QAE/D,MAAMG,aAAa,MAAMhB,UAAa;YACpCM;YACAM;YACAL;YACAO;YACAG,cAAcJ;YACdJ,aAAaR,qBAAqBC,gBAAgBO;YAClDM;YACAL;YACAC;QACF;QAEAV,oBAAoBC,YAAY,GAAGc;QAEnC,OAAOA;IACT;IAEA,IAAI,CAACf,oBAAoBC,YAAY,EAAE;QACrCD,oBAAoBC,YAAY,GAAGO;IACrC;IAEA,OAAOR,oBAAoBC,YAAY;AACzC,EAAC"}
package/dist/index.d.ts CHANGED
@@ -4,7 +4,6 @@ export { isLivePreviewEvent } from './isLivePreviewEvent.js';
4
4
  export { mergeData } from './mergeData.js';
5
5
  export { ready } from './ready.js';
6
6
  export { subscribe } from './subscribe.js';
7
- export { traverseRichText } from './traverseRichText.js';
8
7
  export type { LivePreviewMessageEvent } from './types.js';
9
8
  export { unsubscribe } from './unsubscribe.js';
10
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,YAAY,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,YAAY,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA"}
package/dist/index.js CHANGED
@@ -4,7 +4,6 @@ export { isLivePreviewEvent } from './isLivePreviewEvent.js';
4
4
  export { mergeData } from './mergeData.js';
5
5
  export { ready } from './ready.js';
6
6
  export { subscribe } from './subscribe.js';
7
- export { traverseRichText } from './traverseRichText.js';
8
7
  export { unsubscribe } from './unsubscribe.js';
9
8
 
10
9
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export { handleMessage } from './handleMessage.js'\nexport { isDocumentEvent } from './isDocumentEvent.js'\nexport { isLivePreviewEvent } from './isLivePreviewEvent.js'\nexport { mergeData } from './mergeData.js'\nexport { ready } from './ready.js'\nexport { subscribe } from './subscribe.js'\nexport { traverseRichText } from './traverseRichText.js'\nexport type { LivePreviewMessageEvent } from './types.js'\nexport { unsubscribe } from './unsubscribe.js'\n"],"names":["handleMessage","isDocumentEvent","isLivePreviewEvent","mergeData","ready","subscribe","traverseRichText","unsubscribe"],"mappings":"AAAA,SAASA,aAAa,QAAQ,qBAAoB;AAClD,SAASC,eAAe,QAAQ,uBAAsB;AACtD,SAASC,kBAAkB,QAAQ,0BAAyB;AAC5D,SAASC,SAAS,QAAQ,iBAAgB;AAC1C,SAASC,KAAK,QAAQ,aAAY;AAClC,SAASC,SAAS,QAAQ,iBAAgB;AAC1C,SAASC,gBAAgB,QAAQ,wBAAuB;AAExD,SAASC,WAAW,QAAQ,mBAAkB"}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export { handleMessage } from './handleMessage.js'\nexport { isDocumentEvent } from './isDocumentEvent.js'\nexport { isLivePreviewEvent } from './isLivePreviewEvent.js'\nexport { mergeData } from './mergeData.js'\nexport { ready } from './ready.js'\nexport { subscribe } from './subscribe.js'\nexport type { LivePreviewMessageEvent } from './types.js'\nexport { unsubscribe } from './unsubscribe.js'\n"],"names":["handleMessage","isDocumentEvent","isLivePreviewEvent","mergeData","ready","subscribe","unsubscribe"],"mappings":"AAAA,SAASA,aAAa,QAAQ,qBAAoB;AAClD,SAASC,eAAe,QAAQ,uBAAsB;AACtD,SAASC,kBAAkB,QAAQ,0BAAyB;AAC5D,SAASC,SAAS,QAAQ,iBAAgB;AAC1C,SAASC,KAAK,QAAQ,aAAY;AAClC,SAASC,SAAS,QAAQ,iBAAgB;AAE1C,SAASC,WAAW,QAAQ,mBAAkB"}
@@ -1,21 +1,13 @@
1
- import type { DocumentEvent, FieldSchemaJSON } from 'payload';
2
1
  import type { CollectionPopulationRequestHandler } from './types.js';
3
2
  export declare const mergeData: <T extends Record<string, any>>(args: {
4
3
  apiRoute?: string;
5
- /**
6
- * @deprecated Use `requestHandler` instead
7
- */
8
- collectionPopulationRequestHandler?: CollectionPopulationRequestHandler;
4
+ collectionSlug?: string;
9
5
  depth?: number;
10
- externallyUpdatedRelationship?: DocumentEvent;
11
- fieldSchema: FieldSchemaJSON;
6
+ globalSlug?: string;
12
7
  incomingData: Partial<T>;
13
8
  initialData: T;
14
9
  locale?: string;
15
10
  requestHandler?: CollectionPopulationRequestHandler;
16
- returnNumberOfRequests?: boolean;
17
11
  serverURL: string;
18
- }) => Promise<{
19
- _numberOfRequests?: number;
20
- } & T>;
12
+ }) => Promise<T>;
21
13
  //# sourceMappingURL=mergeData.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mergeData.d.ts","sourceRoot":"","sources":["../src/mergeData.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAiB,MAAM,SAAS,CAAA;AAE5E,OAAO,KAAK,EAAE,kCAAkC,EAA2B,MAAM,YAAY,CAAA;AA2B7F,eAAO,MAAM,SAAS,GAAU,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ;IACnE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;OAEG;IACH,kCAAkC,CAAC,EAAE,kCAAkC,CAAA;IACvE,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,6BAA6B,CAAC,EAAE,aAAa,CAAA;IAC7C,WAAW,EAAE,eAAe,CAAA;IAC5B,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA;IACxB,WAAW,EAAE,CAAC,CAAA;IACd,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,cAAc,CAAC,EAAE,kCAAkC,CAAA;IACnD,sBAAsB,CAAC,EAAE,OAAO,CAAA;IAChC,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,OAAO,CACT;IACE,iBAAiB,CAAC,EAAE,MAAM,CAAA;CAC3B,GAAG,CAAC,CAmEN,CAAA"}
1
+ {"version":3,"file":"mergeData.d.ts","sourceRoot":"","sources":["../src/mergeData.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kCAAkC,EAAE,MAAM,YAAY,CAAA;AAqBpE,eAAO,MAAM,SAAS,GAAU,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ;IACnE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA;IACxB,WAAW,EAAE,CAAC,CAAA;IACd,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,cAAc,CAAC,EAAE,kCAAkC,CAAA;IACnD,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,OAAO,CAAC,CAAC,CA4BZ,CAAA"}
package/dist/mergeData.js CHANGED
@@ -1,62 +1,29 @@
1
- import { traverseFields } from './traverseFields.js';
2
- const defaultRequestHandler = ({ apiPath, endpoint, serverURL })=>{
1
+ const defaultRequestHandler = ({ apiPath, data, endpoint, serverURL })=>{
3
2
  const url = `${serverURL}${apiPath}/${endpoint}`;
4
3
  return fetch(url, {
4
+ body: JSON.stringify(data),
5
5
  credentials: 'include',
6
6
  headers: {
7
- 'Content-Type': 'application/json'
8
- }
7
+ 'Content-Type': 'application/json',
8
+ 'X-Payload-HTTP-Method-Override': 'GET'
9
+ },
10
+ method: 'POST'
9
11
  });
10
12
  };
11
- // Relationships are only updated when their `id` or `relationTo` changes, by comparing the old and new values
12
- // This needs to also happen when locale changes, except this is not not part of the API response
13
- // Instead, we keep track of the old locale ourselves and trigger a re-population when it changes
14
- let prevLocale;
15
13
  export const mergeData = async (args)=>{
16
- const { apiRoute, depth, externallyUpdatedRelationship, fieldSchema, incomingData, initialData, locale, returnNumberOfRequests, serverURL } = args;
17
- const result = {
18
- ...initialData
19
- };
20
- const populationsByCollection = {};
21
- traverseFields({
22
- externallyUpdatedRelationship,
23
- fieldSchema,
24
- incomingData,
25
- localeChanged: prevLocale !== locale,
26
- populationsByCollection,
27
- result
28
- });
29
- await Promise.all(Object.entries(populationsByCollection).map(async ([collection, populations])=>{
30
- let res;
31
- const ids = new Set(populations.map(({ id })=>id));
32
- const requestHandler = args.collectionPopulationRequestHandler || args.requestHandler || defaultRequestHandler;
33
- try {
34
- res = await requestHandler({
35
- apiPath: apiRoute || '/api',
36
- endpoint: encodeURI(`${collection}?depth=${depth}&limit=${ids.size}&where[id][in]=${Array.from(ids).join(',')}${locale ? `&locale=${locale}` : ''}`),
37
- serverURL
38
- }).then((res)=>res.json());
39
- if (res?.docs?.length > 0) {
40
- res.docs.forEach((doc)=>{
41
- populationsByCollection[collection]?.forEach((population)=>{
42
- if (population.id === doc.id) {
43
- population.ref[population.accessor] = doc;
44
- }
45
- });
46
- });
47
- }
48
- } catch (err) {
49
- console.error(err) // eslint-disable-line no-console
50
- ;
51
- }
52
- }));
53
- prevLocale = locale;
54
- return {
55
- ...result,
56
- ...returnNumberOfRequests ? {
57
- _numberOfRequests: Object.keys(populationsByCollection).length
58
- } : {}
59
- };
14
+ const { apiRoute, collectionSlug, depth, globalSlug, incomingData, initialData, locale, serverURL } = args;
15
+ const requestHandler = args.requestHandler || defaultRequestHandler;
16
+ const result = await requestHandler({
17
+ apiPath: apiRoute || '/api',
18
+ data: {
19
+ data: incomingData,
20
+ depth,
21
+ locale
22
+ },
23
+ endpoint: encodeURI(`${globalSlug ? 'globals/' : ''}${collectionSlug ?? globalSlug}${collectionSlug ? `/${initialData.id}` : ''}`),
24
+ serverURL
25
+ }).then((res)=>res.json());
26
+ return result;
60
27
  };
61
28
 
62
29
  //# sourceMappingURL=mergeData.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/mergeData.ts"],"sourcesContent":["import type { DocumentEvent, FieldSchemaJSON, PaginatedDocs } from 'payload'\n\nimport type { CollectionPopulationRequestHandler, PopulationsByCollection } from './types.js'\n\nimport { traverseFields } from './traverseFields.js'\n\nconst defaultRequestHandler = ({\n apiPath,\n endpoint,\n serverURL,\n}: {\n apiPath: string\n endpoint: string\n serverURL: string\n}) => {\n const url = `${serverURL}${apiPath}/${endpoint}`\n return fetch(url, {\n credentials: 'include',\n headers: {\n 'Content-Type': 'application/json',\n },\n })\n}\n\n// Relationships are only updated when their `id` or `relationTo` changes, by comparing the old and new values\n// This needs to also happen when locale changes, except this is not not part of the API response\n// Instead, we keep track of the old locale ourselves and trigger a re-population when it changes\nlet prevLocale: string | undefined\n\nexport const mergeData = async <T extends Record<string, any>>(args: {\n apiRoute?: string\n /**\n * @deprecated Use `requestHandler` instead\n */\n collectionPopulationRequestHandler?: CollectionPopulationRequestHandler\n depth?: number\n externallyUpdatedRelationship?: DocumentEvent\n fieldSchema: FieldSchemaJSON\n incomingData: Partial<T>\n initialData: T\n locale?: string\n requestHandler?: CollectionPopulationRequestHandler\n returnNumberOfRequests?: boolean\n serverURL: string\n}): Promise<\n {\n _numberOfRequests?: number\n } & T\n> => {\n const {\n apiRoute,\n depth,\n externallyUpdatedRelationship,\n fieldSchema,\n incomingData,\n initialData,\n locale,\n returnNumberOfRequests,\n serverURL,\n } = args\n\n const result = { ...initialData }\n\n const populationsByCollection: PopulationsByCollection = {}\n\n traverseFields({\n externallyUpdatedRelationship,\n fieldSchema,\n incomingData,\n localeChanged: prevLocale !== locale,\n populationsByCollection,\n result,\n })\n\n await Promise.all(\n Object.entries(populationsByCollection).map(async ([collection, populations]) => {\n let res: PaginatedDocs\n\n const ids = new Set(populations.map(({ id }) => id))\n const requestHandler =\n args.collectionPopulationRequestHandler || args.requestHandler || defaultRequestHandler\n\n try {\n res = await requestHandler({\n apiPath: apiRoute || '/api',\n endpoint: encodeURI(\n `${collection}?depth=${depth}&limit=${ids.size}&where[id][in]=${Array.from(ids).join(',')}${locale ? `&locale=${locale}` : ''}`,\n ),\n serverURL,\n }).then((res) => res.json())\n\n if (res?.docs?.length > 0) {\n res.docs.forEach((doc) => {\n populationsByCollection[collection]?.forEach((population) => {\n if (population.id === doc.id) {\n population.ref[population.accessor] = doc\n }\n })\n })\n }\n } catch (err) {\n console.error(err) // eslint-disable-line no-console\n }\n }),\n )\n\n prevLocale = locale\n\n return {\n ...result,\n ...(returnNumberOfRequests\n ? { _numberOfRequests: Object.keys(populationsByCollection).length }\n : {}),\n }\n}\n"],"names":["traverseFields","defaultRequestHandler","apiPath","endpoint","serverURL","url","fetch","credentials","headers","prevLocale","mergeData","args","apiRoute","depth","externallyUpdatedRelationship","fieldSchema","incomingData","initialData","locale","returnNumberOfRequests","result","populationsByCollection","localeChanged","Promise","all","Object","entries","map","collection","populations","res","ids","Set","id","requestHandler","collectionPopulationRequestHandler","encodeURI","size","Array","from","join","then","json","docs","length","forEach","doc","population","ref","accessor","err","console","error","_numberOfRequests","keys"],"mappings":"AAIA,SAASA,cAAc,QAAQ,sBAAqB;AAEpD,MAAMC,wBAAwB,CAAC,EAC7BC,OAAO,EACPC,QAAQ,EACRC,SAAS,EAKV;IACC,MAAMC,MAAM,GAAGD,YAAYF,QAAQ,CAAC,EAAEC,UAAU;IAChD,OAAOG,MAAMD,KAAK;QAChBE,aAAa;QACbC,SAAS;YACP,gBAAgB;QAClB;IACF;AACF;AAEA,8G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;YACF;QACF,EAAE,OAAOI,KAAK;YACZC,QAAQC,KAAK,CAACF,KAAK,iCAAiC;;QACtD;IACF;IAGFzC,aAAaS;IAEb,OAAO;QACL,GAAGE,MAAM;QACT,GAAID,yBACA;YAAEkC,mBAAmB5B,OAAO6B,IAAI,CAACjC,yBAAyBuB,MAAM;QAAC,IACjE,CAAC,CAAC;IACR;AACF,EAAC"}
1
+ {"version":3,"sources":["../src/mergeData.ts"],"sourcesContent":["import type { CollectionPopulationRequestHandler } from './types.js'\n\nconst defaultRequestHandler: CollectionPopulationRequestHandler = ({\n apiPath,\n data,\n endpoint,\n serverURL,\n}) => {\n const url = `${serverURL}${apiPath}/${endpoint}`\n\n return fetch(url, {\n body: JSON.stringify(data),\n credentials: 'include',\n headers: {\n 'Content-Type': 'application/json',\n 'X-Payload-HTTP-Method-Override': 'GET',\n },\n method: 'POST',\n })\n}\n\nexport const mergeData = async <T extends Record<string, any>>(args: {\n apiRoute?: string\n collectionSlug?: string\n depth?: number\n globalSlug?: string\n incomingData: Partial<T>\n initialData: T\n locale?: string\n requestHandler?: CollectionPopulationRequestHandler\n serverURL: string\n}): Promise<T> => {\n const {\n apiRoute,\n collectionSlug,\n depth,\n globalSlug,\n incomingData,\n initialData,\n locale,\n serverURL,\n } = args\n\n const requestHandler = args.requestHandler || defaultRequestHandler\n\n const result = await requestHandler({\n apiPath: apiRoute || '/api',\n data: {\n data: incomingData,\n depth,\n locale,\n },\n endpoint: encodeURI(\n `${globalSlug ? 'globals/' : ''}${collectionSlug ?? globalSlug}${collectionSlug ? `/${initialData.id}` : ''}`,\n ),\n serverURL,\n }).then((res) => res.json())\n\n return result\n}\n"],"names":["defaultRequestHandler","apiPath","data","endpoint","serverURL","url","fetch","body","JSON","stringify","credentials","headers","method","mergeData","args","apiRoute","collectionSlug","depth","globalSlug","incomingData","initialData","locale","requestHandler","result","encodeURI","id","then","res","json"],"mappings":"AAEA,MAAMA,wBAA4D,CAAC,EACjEC,OAAO,EACPC,IAAI,EACJC,QAAQ,EACRC,SAAS,EACV;IACC,MAAMC,MAAM,GAAGD,YAAYH,QAAQ,CAAC,EAAEE,UAAU;IAEhD,OAAOG,MAAMD,KAAK;QAChBE,MAAMC,KAAKC,SAAS,CAACP;QACrBQ,aAAa;QACbC,SAAS;YACP,gBAAgB;YAChB,kCAAkC;QACpC;QACAC,QAAQ;IACV;AACF;AAEA,OAAO,MAAMC,YAAY,OAAsCC;IAW7D,MAAM,EACJC,QAAQ,EACRC,cAAc,EACdC,KAAK,EACLC,UAAU,EACVC,YAAY,EACZC,WAAW,EACXC,MAAM,EACNjB,SAAS,EACV,GAAGU;IAEJ,MAAMQ,iBAAiBR,KAAKQ,cAAc,IAAItB;IAE9C,MAAMuB,SAAS,MAAMD,eAAe;QAClCrB,SAASc,YAAY;QACrBb,MAAM;YACJA,MAAMiB;YACNF;YACAI;QACF;QACAlB,UAAUqB,UACR,GAAGN,aAAa,aAAa,KAAKF,kBAAkBE,aAAaF,iBAAiB,CAAC,CAAC,EAAEI,YAAYK,EAAE,EAAE,GAAG,IAAI;QAE/GrB;IACF,GAAGsB,IAAI,CAAC,CAACC,MAAQA,IAAIC,IAAI;IAEzB,OAAOL;AACT,EAAC"}
package/dist/types.d.ts CHANGED
@@ -1,22 +1,17 @@
1
- import type { DocumentEvent, FieldSchemaJSON } from 'payload';
2
- export type CollectionPopulationRequestHandler = ({ apiPath, endpoint, serverURL, }: {
1
+ import type { DocumentEvent } from 'payload';
2
+ export type CollectionPopulationRequestHandler = ({ apiPath, data, endpoint, serverURL, }: {
3
3
  apiPath: string;
4
+ data: Record<string, any>;
4
5
  endpoint: string;
5
6
  serverURL: string;
6
7
  }) => Promise<Response>;
7
8
  export type LivePreviewArgs = {};
8
9
  export type LivePreview = void;
9
- export type PopulationsByCollection = {
10
- [slug: string]: Array<{
11
- accessor: number | string;
12
- id: number | string;
13
- ref: Record<string, unknown>;
14
- }>;
15
- };
16
10
  export type LivePreviewMessageEvent<T> = MessageEvent<{
11
+ collectionSlug?: string;
17
12
  data: T;
18
13
  externallyUpdatedRelationship?: DocumentEvent;
19
- fieldSchemaJSON: FieldSchemaJSON;
14
+ globalSlug?: string;
20
15
  locale?: string;
21
16
  type: 'payload-live-preview';
22
17
  }>;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAE7D,MAAM,MAAM,kCAAkC,GAAG,CAAC,EAChD,OAAO,EACP,QAAQ,EACR,SAAS,GACV,EAAE;IACD,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;CAClB,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;AAEvB,MAAM,MAAM,eAAe,GAAG,EAAE,CAAA;AAEhC,MAAM,MAAM,WAAW,GAAG,IAAI,CAAA;AAE9B,MAAM,MAAM,uBAAuB,GAAG;IACpC,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC;QACpB,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAA;QACzB,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;QACnB,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAC7B,CAAC,CAAA;CACH,CAAA;AAED,MAAM,MAAM,uBAAuB,CAAC,CAAC,IAAI,YAAY,CAAC;IACpD,IAAI,EAAE,CAAC,CAAA;IACP,6BAA6B,CAAC,EAAE,aAAa,CAAA;IAC7C,eAAe,EAAE,eAAe,CAAA;IAChC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,sBAAsB,CAAA;CAC7B,CAAC,CAAA"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAE5C,MAAM,MAAM,kCAAkC,GAAG,CAAC,EAChD,OAAO,EACP,IAAI,EACJ,QAAQ,EACR,SAAS,GACV,EAAE;IACD,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;CAClB,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;AAEvB,MAAM,MAAM,eAAe,GAAG,EAAE,CAAA;AAEhC,MAAM,MAAM,WAAW,GAAG,IAAI,CAAA;AAE9B,MAAM,MAAM,uBAAuB,CAAC,CAAC,IAAI,YAAY,CAAC;IACpD,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,IAAI,EAAE,CAAC,CAAA;IACP,6BAA6B,CAAC,EAAE,aAAa,CAAA;IAC7C,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,sBAAsB,CAAA;CAC7B,CAAC,CAAA"}
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types.ts"],"sourcesContent":["import type { DocumentEvent, FieldSchemaJSON } from 'payload'\n\nexport type CollectionPopulationRequestHandler = ({\n apiPath,\n endpoint,\n serverURL,\n}: {\n apiPath: string\n endpoint: string\n serverURL: string\n}) => Promise<Response>\n\nexport type LivePreviewArgs = {}\n\nexport type LivePreview = void\n\nexport type PopulationsByCollection = {\n [slug: string]: Array<{\n accessor: number | string\n id: number | string\n ref: Record<string, unknown>\n }>\n}\n\nexport type LivePreviewMessageEvent<T> = MessageEvent<{\n data: T\n externallyUpdatedRelationship?: DocumentEvent\n fieldSchemaJSON: FieldSchemaJSON\n locale?: string\n type: 'payload-live-preview'\n}>\n"],"names":[],"mappings":"AAwBA,WAME"}
1
+ {"version":3,"sources":["../src/types.ts"],"sourcesContent":["import type { DocumentEvent } from 'payload'\n\nexport type CollectionPopulationRequestHandler = ({\n apiPath,\n data,\n endpoint,\n serverURL,\n}: {\n apiPath: string\n data: Record<string, any>\n endpoint: string\n serverURL: string\n}) => Promise<Response>\n\nexport type LivePreviewArgs = {}\n\nexport type LivePreview = void\n\nexport type LivePreviewMessageEvent<T> = MessageEvent<{\n collectionSlug?: string\n data: T\n externallyUpdatedRelationship?: DocumentEvent\n globalSlug?: string\n locale?: string\n type: 'payload-live-preview'\n}>\n"],"names":[],"mappings":"AAkBA,WAOE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@payloadcms/live-preview",
3
- "version": "3.55.0-internal.f8c92b5",
3
+ "version": "3.55.0",
4
4
  "description": "The official live preview JavaScript SDK for Payload",
5
5
  "homepage": "https://payloadcms.com",
6
6
  "repository": {
@@ -33,7 +33,7 @@
33
33
  ],
34
34
  "devDependencies": {
35
35
  "@payloadcms/eslint-config": "3.28.0",
36
- "payload": "3.55.0-internal.f8c92b5"
36
+ "payload": "3.55.0"
37
37
  },
38
38
  "publishConfig": {
39
39
  "registry": "https://registry.npmjs.org/"
@@ -1,11 +0,0 @@
1
- import type { DocumentEvent, FieldSchemaJSON } from 'payload';
2
- import type { PopulationsByCollection } from './types.js';
3
- export declare const traverseFields: <T extends Record<string, any>>(args: {
4
- externallyUpdatedRelationship?: DocumentEvent;
5
- fieldSchema: FieldSchemaJSON;
6
- incomingData: T;
7
- localeChanged: boolean;
8
- populationsByCollection: PopulationsByCollection;
9
- result: Record<string, any>;
10
- }) => void;
11
- //# sourceMappingURL=traverseFields.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"traverseFields.d.ts","sourceRoot":"","sources":["../src/traverseFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAE7D,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAIzD,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ;IAClE,6BAA6B,CAAC,EAAE,aAAa,CAAA;IAC7C,WAAW,EAAE,eAAe,CAAA;IAC5B,YAAY,EAAE,CAAC,CAAA;IACf,aAAa,EAAE,OAAO,CAAA;IACtB,uBAAuB,EAAE,uBAAuB,CAAA;IAChD,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC5B,KAAG,IA6RH,CAAA"}
@@ -1,201 +0,0 @@
1
- import { traverseRichText } from './traverseRichText.js';
2
- export const traverseFields = (args)=>{
3
- const { externallyUpdatedRelationship, fieldSchema: fieldSchemas, incomingData, localeChanged, populationsByCollection, result } = args;
4
- fieldSchemas.forEach((fieldSchema)=>{
5
- if ('name' in fieldSchema && typeof fieldSchema.name === 'string') {
6
- const fieldName = fieldSchema.name;
7
- switch(fieldSchema.type){
8
- case 'array':
9
- if (!incomingData[fieldName] && incomingData[fieldName] !== undefined && result?.[fieldName] !== undefined) {
10
- result[fieldName] = [];
11
- }
12
- if (Array.isArray(incomingData[fieldName])) {
13
- result[fieldName] = incomingData[fieldName].map((incomingRow, i)=>{
14
- if (!result[fieldName]) {
15
- result[fieldName] = [];
16
- }
17
- if (!result[fieldName][i]) {
18
- result[fieldName][i] = {};
19
- }
20
- traverseFields({
21
- externallyUpdatedRelationship,
22
- fieldSchema: fieldSchema.fields,
23
- incomingData: incomingRow,
24
- localeChanged,
25
- populationsByCollection,
26
- result: result[fieldName][i]
27
- });
28
- return result[fieldName][i];
29
- });
30
- }
31
- break;
32
- case 'blocks':
33
- if (Array.isArray(incomingData[fieldName])) {
34
- result[fieldName] = incomingData[fieldName].map((incomingBlock, i)=>{
35
- const incomingBlockJSON = fieldSchema.blocks?.[incomingBlock.blockType];
36
- if (!result[fieldName]) {
37
- result[fieldName] = [];
38
- }
39
- if (!result[fieldName][i] || result[fieldName][i].id !== incomingBlock.id || result[fieldName][i].blockType !== incomingBlock.blockType) {
40
- result[fieldName][i] = {
41
- blockType: incomingBlock.blockType
42
- };
43
- }
44
- traverseFields({
45
- externallyUpdatedRelationship,
46
- fieldSchema: incomingBlockJSON.fields,
47
- incomingData: incomingBlock,
48
- localeChanged,
49
- populationsByCollection,
50
- result: result[fieldName][i]
51
- });
52
- return result[fieldName][i];
53
- });
54
- } else {
55
- result[fieldName] = [];
56
- }
57
- break;
58
- case 'group':
59
- // falls through
60
- case 'tabs':
61
- if (!result[fieldName]) {
62
- result[fieldName] = {};
63
- }
64
- traverseFields({
65
- externallyUpdatedRelationship,
66
- fieldSchema: fieldSchema.fields,
67
- incomingData: incomingData[fieldName] || {},
68
- localeChanged,
69
- populationsByCollection,
70
- result: result[fieldName]
71
- });
72
- break;
73
- case 'relationship':
74
- // falls through
75
- case 'upload':
76
- // Handle `hasMany` relationships
77
- if (fieldSchema.hasMany && Array.isArray(incomingData[fieldName])) {
78
- if (!result[fieldName] || !incomingData[fieldName].length) {
79
- result[fieldName] = [];
80
- }
81
- incomingData[fieldName].forEach((incomingRelation, i)=>{
82
- // Handle `hasMany` polymorphic
83
- if (Array.isArray(fieldSchema.relationTo)) {
84
- // if the field doesn't exist on the result, create it
85
- // the value will be populated later
86
- if (!result[fieldName][i]) {
87
- result[fieldName][i] = {
88
- relationTo: incomingRelation.relationTo
89
- };
90
- }
91
- const oldID = result[fieldName][i]?.value?.id;
92
- const oldRelation = result[fieldName][i]?.relationTo;
93
- const newID = incomingRelation.value;
94
- const newRelation = incomingRelation.relationTo;
95
- const hasChanged = newID !== oldID || newRelation !== oldRelation;
96
- const hasUpdated = newRelation === externallyUpdatedRelationship?.entitySlug && newID === externallyUpdatedRelationship?.id;
97
- if (hasChanged || hasUpdated || localeChanged) {
98
- if (!populationsByCollection[newRelation]) {
99
- populationsByCollection[newRelation] = [];
100
- }
101
- populationsByCollection[newRelation].push({
102
- id: incomingRelation.value,
103
- accessor: 'value',
104
- ref: result[fieldName][i]
105
- });
106
- }
107
- } else {
108
- // Handle `hasMany` monomorphic
109
- const hasChanged = incomingRelation !== result[fieldName][i]?.id;
110
- const hasUpdated = fieldSchema.relationTo === externallyUpdatedRelationship?.entitySlug && incomingRelation === externallyUpdatedRelationship?.id;
111
- if (hasChanged || hasUpdated || localeChanged) {
112
- if (!populationsByCollection[fieldSchema.relationTo]) {
113
- populationsByCollection[fieldSchema.relationTo] = [];
114
- }
115
- populationsByCollection[fieldSchema.relationTo]?.push({
116
- id: incomingRelation,
117
- accessor: i,
118
- ref: result[fieldName]
119
- });
120
- }
121
- }
122
- });
123
- } else {
124
- // Handle `hasOne` polymorphic
125
- if (Array.isArray(fieldSchema.relationTo)) {
126
- // if the field doesn't exist on the result, create it
127
- // the value will be populated later
128
- if (!result[fieldName]) {
129
- result[fieldName] = {
130
- relationTo: incomingData[fieldName]?.relationTo
131
- };
132
- }
133
- const hasNewValue = incomingData[fieldName] && typeof incomingData[fieldName] === 'object' && incomingData[fieldName] !== null;
134
- const hasOldValue = result[fieldName] && typeof result[fieldName] === 'object' && result[fieldName] !== null;
135
- const newID = hasNewValue ? typeof incomingData[fieldName].value === 'object' ? incomingData[fieldName].value.id : incomingData[fieldName].value : '';
136
- const oldID = hasOldValue ? typeof result[fieldName].value === 'object' ? result[fieldName].value.id : result[fieldName].value : '';
137
- const newRelation = hasNewValue ? incomingData[fieldName].relationTo : '';
138
- const oldRelation = hasOldValue ? result[fieldName].relationTo : '';
139
- const hasChanged = newID !== oldID || newRelation !== oldRelation;
140
- const hasUpdated = newRelation === externallyUpdatedRelationship?.entitySlug && newID === externallyUpdatedRelationship?.id;
141
- // if the new value/relation is different from the old value/relation
142
- // populate the new value, otherwise leave it alone
143
- if (hasChanged || hasUpdated || localeChanged) {
144
- // if the new value is not empty, populate it
145
- // otherwise set the value to null
146
- if (newID) {
147
- if (!populationsByCollection[newRelation]) {
148
- populationsByCollection[newRelation] = [];
149
- }
150
- populationsByCollection[newRelation].push({
151
- id: newID,
152
- accessor: 'value',
153
- ref: result[fieldName]
154
- });
155
- } else {
156
- result[fieldName] = null;
157
- }
158
- }
159
- } else {
160
- // Handle `hasOne` monomorphic
161
- const newID = incomingData[fieldName] && typeof incomingData[fieldName] === 'object' && incomingData[fieldName].id || incomingData[fieldName];
162
- const oldID = result[fieldName] && typeof result[fieldName] === 'object' && result[fieldName].id || result[fieldName];
163
- const hasChanged = newID !== oldID;
164
- const hasUpdated = fieldSchema.relationTo === externallyUpdatedRelationship?.entitySlug && newID === externallyUpdatedRelationship?.id;
165
- // if the new value is different from the old value
166
- // populate the new value, otherwise leave it alone
167
- if (hasChanged || hasUpdated || localeChanged) {
168
- // if the new value is not empty, populate it
169
- // otherwise set the value to null
170
- if (newID) {
171
- if (!populationsByCollection[fieldSchema.relationTo]) {
172
- populationsByCollection[fieldSchema.relationTo] = [];
173
- }
174
- populationsByCollection[fieldSchema.relationTo]?.push({
175
- id: newID,
176
- accessor: fieldName,
177
- ref: result
178
- });
179
- } else {
180
- result[fieldName] = null;
181
- }
182
- }
183
- }
184
- }
185
- break;
186
- case 'richText':
187
- result[fieldName] = traverseRichText({
188
- externallyUpdatedRelationship,
189
- incomingData: incomingData[fieldName],
190
- populationsByCollection,
191
- result: result[fieldName]
192
- });
193
- break;
194
- default:
195
- result[fieldName] = incomingData[fieldName];
196
- }
197
- }
198
- });
199
- };
200
-
201
- //# sourceMappingURL=traverseFields.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/traverseFields.ts"],"sourcesContent":["import type { DocumentEvent, FieldSchemaJSON } from 'payload'\n\nimport type { PopulationsByCollection } from './types.js'\n\nimport { traverseRichText } from './traverseRichText.js'\n\nexport const traverseFields = <T extends Record<string, any>>(args: {\n externallyUpdatedRelationship?: DocumentEvent\n fieldSchema: FieldSchemaJSON\n incomingData: T\n localeChanged: boolean\n populationsByCollection: PopulationsByCollection\n result: Record<string, any>\n}): void => {\n const {\n externallyUpdatedRelationship,\n fieldSchema: fieldSchemas,\n incomingData,\n localeChanged,\n populationsByCollection,\n result,\n } = args\n\n fieldSchemas.forEach((fieldSchema) => {\n if ('name' in fieldSchema && typeof fieldSchema.name === 'string') {\n const fieldName = fieldSchema.name\n\n switch (fieldSchema.type) {\n case 'array':\n if (\n !incomingData[fieldName] &&\n incomingData[fieldName] !== undefined &&\n result?.[fieldName] !== undefined\n ) {\n result[fieldName] = []\n }\n\n if (Array.isArray(incomingData[fieldName])) {\n result[fieldName] = incomingData[fieldName].map((incomingRow, i) => {\n if (!result[fieldName]) {\n result[fieldName] = []\n }\n\n if (!result[fieldName][i]) {\n result[fieldName][i] = {}\n }\n\n traverseFields({\n externallyUpdatedRelationship,\n fieldSchema: fieldSchema.fields!,\n incomingData: incomingRow,\n localeChanged,\n populationsByCollection,\n result: result[fieldName][i],\n })\n\n return result[fieldName][i]\n })\n }\n\n break\n\n case 'blocks':\n if (Array.isArray(incomingData[fieldName])) {\n result[fieldName] = incomingData[fieldName].map((incomingBlock, i) => {\n const incomingBlockJSON = fieldSchema.blocks?.[incomingBlock.blockType]\n\n if (!result[fieldName]) {\n result[fieldName] = []\n }\n\n if (\n !result[fieldName][i] ||\n result[fieldName][i].id !== incomingBlock.id ||\n result[fieldName][i].blockType !== incomingBlock.blockType\n ) {\n result[fieldName][i] = {\n blockType: incomingBlock.blockType,\n }\n }\n\n traverseFields({\n externallyUpdatedRelationship,\n fieldSchema: incomingBlockJSON!.fields!,\n incomingData: incomingBlock,\n localeChanged,\n populationsByCollection,\n result: result[fieldName][i],\n })\n\n return result[fieldName][i]\n })\n } else {\n result[fieldName] = []\n }\n\n break\n\n case 'group':\n // falls through\n case 'tabs':\n if (!result[fieldName]) {\n result[fieldName] = {}\n }\n\n traverseFields({\n externallyUpdatedRelationship,\n fieldSchema: fieldSchema.fields!,\n incomingData: incomingData[fieldName] || {},\n localeChanged,\n populationsByCollection,\n result: result[fieldName],\n })\n\n break\n\n case 'relationship':\n // falls through\n case 'upload':\n // Handle `hasMany` relationships\n if (fieldSchema.hasMany && Array.isArray(incomingData[fieldName])) {\n if (!result[fieldName] || !incomingData[fieldName].length) {\n result[fieldName] = []\n }\n\n incomingData[fieldName].forEach((incomingRelation, i) => {\n // Handle `hasMany` polymorphic\n if (Array.isArray(fieldSchema.relationTo)) {\n // if the field doesn't exist on the result, create it\n // the value will be populated later\n if (!result[fieldName][i]) {\n result[fieldName][i] = {\n relationTo: incomingRelation.relationTo,\n }\n }\n\n const oldID = result[fieldName][i]?.value?.id\n const oldRelation = result[fieldName][i]?.relationTo\n const newID = incomingRelation.value\n const newRelation = incomingRelation.relationTo\n\n const hasChanged = newID !== oldID || newRelation !== oldRelation\n\n const hasUpdated =\n newRelation === externallyUpdatedRelationship?.entitySlug &&\n newID === externallyUpdatedRelationship?.id\n\n if (hasChanged || hasUpdated || localeChanged) {\n if (!populationsByCollection[newRelation]) {\n populationsByCollection[newRelation] = []\n }\n\n populationsByCollection[newRelation].push({\n id: incomingRelation.value,\n accessor: 'value',\n ref: result[fieldName][i],\n })\n }\n } else {\n // Handle `hasMany` monomorphic\n const hasChanged = incomingRelation !== result[fieldName][i]?.id\n\n const hasUpdated =\n fieldSchema.relationTo === externallyUpdatedRelationship?.entitySlug &&\n incomingRelation === externallyUpdatedRelationship?.id\n\n if (hasChanged || hasUpdated || localeChanged) {\n if (!populationsByCollection[fieldSchema.relationTo!]) {\n populationsByCollection[fieldSchema.relationTo!] = []\n }\n\n populationsByCollection[fieldSchema.relationTo!]?.push({\n id: incomingRelation,\n accessor: i,\n ref: result[fieldName],\n })\n }\n }\n })\n } else {\n // Handle `hasOne` polymorphic\n if (Array.isArray(fieldSchema.relationTo)) {\n // if the field doesn't exist on the result, create it\n // the value will be populated later\n if (!result[fieldName]) {\n result[fieldName] = {\n relationTo: incomingData[fieldName]?.relationTo,\n }\n }\n\n const hasNewValue =\n incomingData[fieldName] &&\n typeof incomingData[fieldName] === 'object' &&\n incomingData[fieldName] !== null\n\n const hasOldValue =\n result[fieldName] &&\n typeof result[fieldName] === 'object' &&\n result[fieldName] !== null\n\n const newID = hasNewValue\n ? typeof incomingData[fieldName].value === 'object'\n ? incomingData[fieldName].value.id\n : incomingData[fieldName].value\n : ''\n\n const oldID = hasOldValue\n ? typeof result[fieldName].value === 'object'\n ? result[fieldName].value.id\n : result[fieldName].value\n : ''\n\n const newRelation = hasNewValue ? incomingData[fieldName].relationTo : ''\n const oldRelation = hasOldValue ? result[fieldName].relationTo : ''\n\n const hasChanged = newID !== oldID || newRelation !== oldRelation\n\n const hasUpdated =\n newRelation === externallyUpdatedRelationship?.entitySlug &&\n newID === externallyUpdatedRelationship?.id\n\n // if the new value/relation is different from the old value/relation\n // populate the new value, otherwise leave it alone\n if (hasChanged || hasUpdated || localeChanged) {\n // if the new value is not empty, populate it\n // otherwise set the value to null\n if (newID) {\n if (!populationsByCollection[newRelation]) {\n populationsByCollection[newRelation] = []\n }\n\n populationsByCollection[newRelation].push({\n id: newID,\n accessor: 'value',\n ref: result[fieldName],\n })\n } else {\n result[fieldName] = null\n }\n }\n } else {\n // Handle `hasOne` monomorphic\n const newID: number | string | undefined =\n (incomingData[fieldName] &&\n typeof incomingData[fieldName] === 'object' &&\n incomingData[fieldName].id) ||\n incomingData[fieldName]\n\n const oldID: number | string | undefined =\n (result[fieldName] &&\n typeof result[fieldName] === 'object' &&\n result[fieldName].id) ||\n result[fieldName]\n\n const hasChanged = newID !== oldID\n\n const hasUpdated =\n fieldSchema.relationTo === externallyUpdatedRelationship?.entitySlug &&\n newID === externallyUpdatedRelationship?.id\n\n // if the new value is different from the old value\n // populate the new value, otherwise leave it alone\n if (hasChanged || hasUpdated || localeChanged) {\n // if the new value is not empty, populate it\n // otherwise set the value to null\n if (newID) {\n if (!populationsByCollection[fieldSchema.relationTo!]) {\n populationsByCollection[fieldSchema.relationTo!] = []\n }\n\n populationsByCollection[fieldSchema.relationTo!]?.push({\n id: newID,\n accessor: fieldName,\n ref: result as Record<string, unknown>,\n })\n } else {\n result[fieldName] = null\n }\n }\n }\n }\n\n break\n case 'richText':\n result[fieldName] = traverseRichText({\n externallyUpdatedRelationship,\n incomingData: incomingData[fieldName],\n populationsByCollection,\n result: result[fieldName],\n })\n\n break\n\n default:\n result[fieldName] = incomingData[fieldName]\n }\n }\n })\n}\n"],"names":["traverseRichText","traverseFields","args","externallyUpdatedRelationship","fieldSchema","fieldSchemas","incomingData","localeChanged","populationsByCollection","result","forEach","name","fieldName","type","undefined","Array","isArray","map","incomingRow","i","fields","incomingBlock","incomingBlockJSON","blocks","blockType","id","hasMany","length","incomingRelation","relationTo","oldID","value","oldRelation","newID","newRelation","hasChanged","hasUpdated","entitySlug","push","accessor","ref","hasNewValue","hasOldValue"],"mappings":"AAIA,SAASA,gBAAgB,QAAQ,wBAAuB;AAExD,OAAO,MAAMC,iBAAiB,CAAgCC;IAQ5D,MAAM,EACJC,6BAA6B,EAC7BC,aAAaC,YAAY,EACzBC,YAAY,EACZC,aAAa,EACbC,uBAAuB,EACvBC,MAAM,EACP,GAAGP;IAEJG,aAAaK,OAAO,CAAC,CAACN;QACpB,IAAI,UAAUA,eAAe,OAAOA,YAAYO,IAAI,KAAK,UAAU;YACjE,MAAMC,YAAYR,YAAYO,IAAI;YAElC,OAAQP,YAAYS,IAAI;gBACtB,KAAK;oBACH,IACE,CAACP,YAAY,CAACM,UAAU,IACxBN,YAAY,CAACM,UAAU,KAAKE,aAC5BL,QAAQ,CAACG,UAAU,KAAKE,WACxB;wBACAL,MAAM,CAACG,UAAU,GAAG,EAAE;oBACxB;oBAEA,IAAIG,MAAMC,OAAO,CAACV,YAAY,CAACM,UAAU,GAAG;wBAC1CH,MAAM,CAACG,UAAU,GAAGN,YAAY,CAACM,UAAU,CAACK,GAAG,CAAC,CAACC,aAAaC;4BAC5D,IAAI,CAACV,MAAM,CAACG,UAAU,EAAE;gCACtBH,MAAM,CAACG,UAAU,GAAG,EAAE;4BACxB;4BAEA,IAAI,CAACH,MAAM,CAACG,UAAU,CAACO,EAAE,EAAE;gCACzBV,MAAM,CAACG,UAAU,CAACO,EAAE,GAAG,CAAC;4BAC1B;4BAEAlB,eAAe;gCACbE;g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A+BsB;4BAE3C,mDAAmD;4BACnD,mDAAmD;4BACnD,IAAIU,cAAcC,cAAc7B,eAAe;gCAC7C,6CAA6C;gCAC7C,kCAAkC;gCAClC,IAAI0B,OAAO;oCACT,IAAI,CAACzB,uBAAuB,CAACJ,YAAYyB,UAAU,CAAE,EAAE;wCACrDrB,uBAAuB,CAACJ,YAAYyB,UAAU,CAAE,GAAG,EAAE;oCACvD;oCAEArB,uBAAuB,CAACJ,YAAYyB,UAAU,CAAE,EAAES,KAAK;wCACrDb,IAAIQ;wCACJM,UAAU3B;wCACV4B,KAAK/B;oCACP;gCACF,OAAO;oCACLA,MAAM,CAACG,UAAU,GAAG;gCACtB;4BACF;wBACF;oBACF;oBAEA;gBACF,KAAK;oBACHH,MAAM,CAACG,UAAU,GAAGZ,iBAAiB;wBACnCG;wBACAG,cAAcA,YAAY,CAACM,UAAU;wBACrCJ;wBACAC,QAAQA,MAAM,CAACG,UAAU;oBAC3B;oBAEA;gBAEF;oBACEH,MAAM,CAACG,UAAU,GAAGN,YAAY,CAACM,UAAU;YAC/C;QACF;IACF;AACF,EAAC"}
@@ -1,9 +0,0 @@
1
- import type { DocumentEvent } from 'payload';
2
- import type { PopulationsByCollection } from './types.js';
3
- export declare const traverseRichText: ({ externallyUpdatedRelationship, incomingData, populationsByCollection, result, }: {
4
- externallyUpdatedRelationship?: DocumentEvent;
5
- incomingData: any;
6
- populationsByCollection: PopulationsByCollection;
7
- result: any;
8
- }) => any;
9
- //# sourceMappingURL=traverseRichText.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"traverseRichText.d.ts","sourceRoot":"","sources":["../src/traverseRichText.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAE5C,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAEzD,eAAO,MAAM,gBAAgB,sFAK1B;IACD,6BAA6B,CAAC,EAAE,aAAa,CAAA;IAC7C,YAAY,EAAE,GAAG,CAAA;IACjB,uBAAuB,EAAE,uBAAuB,CAAA;IAChD,MAAM,EAAE,GAAG,CAAA;CACZ,KAAG,GA0FH,CAAA"}
@@ -1,67 +0,0 @@
1
- export const traverseRichText = ({ externallyUpdatedRelationship, incomingData, populationsByCollection, result })=>{
2
- if (Array.isArray(incomingData)) {
3
- if (!result) {
4
- result = [];
5
- }
6
- result = incomingData.map((item, index)=>{
7
- if (!result[index]) {
8
- result[index] = item;
9
- }
10
- return traverseRichText({
11
- externallyUpdatedRelationship,
12
- incomingData: item,
13
- populationsByCollection,
14
- result: result[index]
15
- });
16
- });
17
- } else if (incomingData && typeof incomingData === 'object') {
18
- if (!result) {
19
- result = {};
20
- }
21
- // Remove keys from `result` that do not appear in `incomingData`
22
- // There's likely another way to do this,
23
- // But recursion and references make this very difficult
24
- Object.keys(result).forEach((key)=>{
25
- if (!(key in incomingData)) {
26
- delete result[key];
27
- }
28
- });
29
- // Iterate over the keys of `incomingData` and populate `result`
30
- Object.keys(incomingData).forEach((key)=>{
31
- if (!result[key]) {
32
- // Instantiate the key in `result` if it doesn't exist
33
- // Ensure its type matches the type of the `incomingData`
34
- // We don't have a schema to check against here
35
- result[key] = incomingData[key] && typeof incomingData[key] === 'object' ? Array.isArray(incomingData[key]) ? [] : {} : undefined;
36
- }
37
- const isRelationship = key === 'value' && 'relationTo' in incomingData;
38
- if (isRelationship) {
39
- // or if there are no keys besides id
40
- const needsPopulation = !result.value || typeof result.value !== 'object' || typeof result.value === 'object' && Object.keys(result.value).length === 1 && 'id' in result.value;
41
- const hasChanged = result && typeof result === 'object' && result.value.id === externallyUpdatedRelationship?.id;
42
- if (needsPopulation || hasChanged) {
43
- if (!populationsByCollection[incomingData.relationTo]) {
44
- populationsByCollection[incomingData.relationTo] = [];
45
- }
46
- populationsByCollection[incomingData.relationTo]?.push({
47
- id: incomingData[key] && typeof incomingData[key] === 'object' ? incomingData[key].id : incomingData[key],
48
- accessor: 'value',
49
- ref: result
50
- });
51
- }
52
- } else {
53
- result[key] = traverseRichText({
54
- externallyUpdatedRelationship,
55
- incomingData: incomingData[key],
56
- populationsByCollection,
57
- result: result[key]
58
- });
59
- }
60
- });
61
- } else {
62
- result = incomingData;
63
- }
64
- return result;
65
- };
66
-
67
- //# sourceMappingURL=traverseRichText.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/traverseRichText.ts"],"sourcesContent":["import type { DocumentEvent } from 'payload'\n\nimport type { PopulationsByCollection } from './types.js'\n\nexport const traverseRichText = ({\n externallyUpdatedRelationship,\n incomingData,\n populationsByCollection,\n result,\n}: {\n externallyUpdatedRelationship?: DocumentEvent\n incomingData: any\n populationsByCollection: PopulationsByCollection\n result: any\n}): any => {\n if (Array.isArray(incomingData)) {\n if (!result) {\n result = []\n }\n\n result = incomingData.map((item, index) => {\n if (!result[index]) {\n result[index] = item\n }\n\n return traverseRichText({\n externallyUpdatedRelationship,\n incomingData: item,\n populationsByCollection,\n result: result[index],\n })\n })\n } else if (incomingData && typeof incomingData === 'object') {\n if (!result) {\n result = {}\n }\n\n // Remove keys from `result` that do not appear in `incomingData`\n // There's likely another way to do this,\n // But recursion and references make this very difficult\n Object.keys(result).forEach((key) => {\n if (!(key in incomingData)) {\n delete result[key]\n }\n })\n\n // Iterate over the keys of `incomingData` and populate `result`\n Object.keys(incomingData).forEach((key) => {\n if (!result[key]) {\n // Instantiate the key in `result` if it doesn't exist\n // Ensure its type matches the type of the `incomingData`\n // We don't have a schema to check against here\n result[key] =\n incomingData[key] && typeof incomingData[key] === 'object'\n ? Array.isArray(incomingData[key])\n ? []\n : {}\n : undefined\n }\n\n const isRelationship = key === 'value' && 'relationTo' in incomingData\n\n if (isRelationship) {\n // or if there are no keys besides id\n const needsPopulation =\n !result.value ||\n typeof result.value !== 'object' ||\n (typeof result.value === 'object' &&\n Object.keys(result.value).length === 1 &&\n 'id' in result.value)\n\n const hasChanged =\n result &&\n typeof result === 'object' &&\n result.value.id === externallyUpdatedRelationship?.id\n\n if (needsPopulation || hasChanged) {\n if (!populationsByCollection[incomingData.relationTo]) {\n populationsByCollection[incomingData.relationTo] = []\n }\n\n populationsByCollection[incomingData.relationTo]?.push({\n id:\n incomingData[key] && typeof incomingData[key] === 'object'\n ? incomingData[key].id\n : incomingData[key],\n accessor: 'value',\n ref: result,\n })\n }\n } else {\n result[key] = traverseRichText({\n externallyUpdatedRelationship,\n incomingData: incomingData[key],\n populationsByCollection,\n result: result[key],\n })\n }\n })\n } else {\n result = incomingData\n }\n\n return result\n}\n"],"names":["traverseRichText","externallyUpdatedRelationship","incomingData","populationsByCollection","result","Array","isArray","map","item","index","Object","keys","forEach","key","undefined","isRelationship","needsPopulation","value","length","hasChanged","id","relationTo","push","accessor","ref"],"mappings":"AAIA,OAAO,MAAMA,mBAAmB,CAAC,EAC/BC,6BAA6B,EAC7BC,YAAY,EACZC,uBAAuB,EACvBC,MAAM,EAMP;IACC,IAAIC,MAAMC,OAAO,CAACJ,eAAe;QAC/B,IAAI,CAACE,QAAQ;YACXA,SAAS,EAAE;QACb;QAEAA,SAASF,aAAaK,GAAG,CAAC,CAACC,MAAMC;YAC/B,IAAI,CAACL,MAAM,CAACK,MAAM,EAAE;gBAClBL,MAAM,CAACK,MAAM,GAAGD;YAClB;YAEA,OAAOR,iBAAiB;gBACtBC;gBACAC,cAAcM;gBACdL;gBACAC,QAAQA,MAAM,CAACK,MAAM;YACvB;QACF;IACF,OAAO,IAAIP,gBAAgB,OAAOA,iBAAiB,UAAU;QAC3D,IAAI,CAACE,QAAQ;YACXA,SAAS,CAAC;QACZ;QAEA,iEAAiE;QACjE,yCAAyC;QACzC,wDAAwD;QACxDM,OAAOC,IAAI,CAACP,QAAQQ,OAAO,CAAC,CAACC;YAC3B,IAAI,CAAEA,CAAAA,OAAOX,YAAW,GAAI;gBAC1B,OAAOE,MAAM,CAACS,IAAI;YACpB;QACF;QAEA,gEAAgE;QAChEH,OAAOC,IAAI,CAACT,cAAcU,OAAO,CAAC,CAACC;YACjC,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