@klnap/cache-invalidation 0.0.0-stage → 0.0.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.
- package/CHANGELOG.md +139 -0
- package/LICENSE +21 -0
- package/README.md +176 -2
- package/dist/consumers/next/compile-tag-template.d.ts +16 -0
- package/dist/consumers/next/compile-tag-template.js +83 -0
- package/dist/consumers/next/consumer-types.d.ts +98 -0
- package/dist/consumers/next/consumer-types.js +3 -0
- package/dist/consumers/next/define-cache.d.ts +4 -0
- package/dist/consumers/next/define-cache.js +281 -0
- package/dist/consumers/next/host-types.d.ts +44 -0
- package/dist/consumers/next/host-types.js +3 -0
- package/dist/consumers/next/index.d.ts +5 -0
- package/dist/consumers/next/index.js +2 -0
- package/dist/consumers/next/param-values.d.ts +8 -0
- package/dist/consumers/next/param-values.js +23 -0
- package/dist/consumers/next/profile.d.ts +2 -0
- package/dist/consumers/next/profile.js +18 -0
- package/dist/consumers/next/registry-builders.d.ts +3 -0
- package/dist/consumers/next/registry-builders.js +155 -0
- package/dist/consumers/next/revalidation-handler.d.ts +6 -0
- package/dist/consumers/next/revalidation-handler.js +2 -0
- package/dist/consumers/next/runtime.d.ts +5 -0
- package/dist/consumers/next/runtime.js +560 -0
- package/dist/core/cloudevents.d.ts +27 -0
- package/dist/core/cloudevents.js +172 -0
- package/dist/core/dispatch-outcome.d.ts +4 -0
- package/dist/core/dispatch-outcome.js +17 -0
- package/dist/core/errors.d.ts +16 -0
- package/dist/core/errors.js +28 -0
- package/dist/core/event-limits.d.ts +15 -0
- package/dist/core/event-limits.js +20 -0
- package/dist/core/event-selector.d.ts +9 -0
- package/dist/core/event-selector.js +5 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/index.js +6 -0
- package/dist/core/resource-changed-schema.d.ts +35 -0
- package/dist/core/resource-changed-schema.js +12 -0
- package/dist/core/standard-schema.d.ts +2 -0
- package/dist/core/standard-schema.js +7 -0
- package/dist/core/types.d.ts +35 -0
- package/dist/core/types.js +1 -0
- package/dist/core/url-segment.d.ts +2 -0
- package/dist/core/url-segment.js +17 -0
- package/dist/dispatchers/http/index.d.ts +20 -0
- package/dist/dispatchers/http/index.js +274 -0
- package/dist/dispatchers/http/targets.d.ts +14 -0
- package/dist/dispatchers/http/targets.js +26 -0
- package/dist/dispatchers/index.d.ts +1 -0
- package/dist/dispatchers/index.js +1 -0
- package/dist/producers/payload/exclude-fields.d.ts +19 -0
- package/dist/producers/payload/exclude-fields.js +75 -0
- package/dist/producers/payload/index.d.ts +4 -0
- package/dist/producers/payload/index.js +3 -0
- package/dist/producers/payload/lifecycle/deferred.d.ts +55 -0
- package/dist/producers/payload/lifecycle/deferred.js +111 -0
- package/dist/producers/payload/lifecycle/dispatch.d.ts +18 -0
- package/dist/producers/payload/lifecycle/dispatch.js +21 -0
- package/dist/producers/payload/lifecycle/doc-content.d.ts +8 -0
- package/dist/producers/payload/lifecycle/doc-content.js +88 -0
- package/dist/producers/payload/lifecycle/draft-status.d.ts +6 -0
- package/dist/producers/payload/lifecycle/draft-status.js +15 -0
- package/dist/producers/payload/lifecycle/helpers.d.ts +13 -0
- package/dist/producers/payload/lifecycle/helpers.js +12 -0
- package/dist/producers/payload/lifecycle/identifier-field.d.ts +8 -0
- package/dist/producers/payload/lifecycle/identifier-field.js +48 -0
- package/dist/producers/payload/lifecycle/identifiers.d.ts +4 -0
- package/dist/producers/payload/lifecycle/identifiers.js +5 -0
- package/dist/producers/payload/lifecycle/locale-published.d.ts +7 -0
- package/dist/producers/payload/lifecycle/locale-published.js +19 -0
- package/dist/producers/payload/lifecycle/locale.d.ts +24 -0
- package/dist/producers/payload/lifecycle/locale.js +44 -0
- package/dist/producers/payload/lifecycle/log.d.ts +31 -0
- package/dist/producers/payload/lifecycle/log.js +67 -0
- package/dist/producers/payload/lifecycle/media-equality.d.ts +4 -0
- package/dist/producers/payload/lifecycle/media-equality.js +36 -0
- package/dist/producers/payload/lifecycle/operation.d.ts +2 -0
- package/dist/producers/payload/lifecycle/operation.js +7 -0
- package/dist/producers/payload/lifecycle/orderable-hooks.d.ts +21 -0
- package/dist/producers/payload/lifecycle/orderable-hooks.js +26 -0
- package/dist/producers/payload/lifecycle/orderable-neighbors.d.ts +21 -0
- package/dist/producers/payload/lifecycle/orderable-neighbors.js +151 -0
- package/dist/producers/payload/lifecycle/public-snapshots.d.ts +16 -0
- package/dist/producers/payload/lifecycle/public-snapshots.js +173 -0
- package/dist/producers/payload/lifecycle/revalidate.d.ts +16 -0
- package/dist/producers/payload/lifecycle/revalidate.js +162 -0
- package/dist/producers/payload/lifecycle/skip-rules.d.ts +19 -0
- package/dist/producers/payload/lifecycle/skip-rules.js +57 -0
- package/dist/producers/payload/lifecycle/types.d.ts +32 -0
- package/dist/producers/payload/lifecycle/types.js +1 -0
- package/dist/producers/payload/media/media-references.d.ts +31 -0
- package/dist/producers/payload/media/media-references.js +127 -0
- package/dist/producers/payload/media/resolve-media-affected-locales.d.ts +22 -0
- package/dist/producers/payload/media/resolve-media-affected-locales.js +70 -0
- package/dist/producers/payload/media/resolve-media-dependents.d.ts +39 -0
- package/dist/producers/payload/media/resolve-media-dependents.js +157 -0
- package/dist/producers/payload/media/resolve-media-slug.d.ts +7 -0
- package/dist/producers/payload/media/resolve-media-slug.js +17 -0
- package/dist/producers/payload/media/revalidate-media-dependents.d.ts +14 -0
- package/dist/producers/payload/media/revalidate-media-dependents.js +222 -0
- package/dist/producers/payload/plugin.d.ts +36 -0
- package/dist/producers/payload/plugin.js +235 -0
- package/dist/producers/payload/publish-all/context.d.ts +2 -0
- package/dist/producers/payload/publish-all/context.js +2 -0
- package/dist/producers/payload/publish-all/edited-locale.d.ts +5 -0
- package/dist/producers/payload/publish-all/edited-locale.js +5 -0
- package/dist/producers/payload/publish-all/index.d.ts +3 -0
- package/dist/producers/payload/publish-all/index.js +3 -0
- package/dist/producers/payload/publish-all/localization.d.ts +7 -0
- package/dist/producers/payload/publish-all/localization.js +1 -0
- package/dist/producers/payload/publish-all/mirror-publish-all-locales-to-req.d.ts +19 -0
- package/dist/producers/payload/publish-all/mirror-publish-all-locales-to-req.js +32 -0
- package/dist/producers/payload/publish-all/preserve-locales-on-publish-all.d.ts +2 -0
- package/dist/producers/payload/publish-all/preserve-locales-on-publish-all.js +145 -0
- package/dist/producers/payload/publish-all/read-localized-scalar.d.ts +2 -0
- package/dist/producers/payload/publish-all/read-localized-scalar.js +24 -0
- package/dist/producers/payload/publish-all/request-flags.d.ts +13 -0
- package/dist/producers/payload/publish-all/request-flags.js +9 -0
- package/dist/producers/payload/publish-all/restore-guard.d.ts +2 -0
- package/dist/producers/payload/publish-all/restore-guard.js +12 -0
- package/dist/producers/payload/publish-all/restore-locales-after-publish-all.d.ts +2 -0
- package/dist/producers/payload/publish-all/restore-locales-after-publish-all.js +85 -0
- package/dist/producers/payload/revalidation-custom.d.ts +37 -0
- package/dist/producers/payload/revalidation-custom.js +54 -0
- package/dist/producers/payload/schema.d.ts +3 -0
- package/dist/producers/payload/schema.js +23 -0
- package/docs/concepts/architecture.md +32 -0
- package/docs/concepts/cache-tags.md +97 -0
- package/docs/concepts/event-contract.md +38 -0
- package/docs/consumers/custom.md +19 -0
- package/docs/consumers/next.md +277 -0
- package/docs/dispatchers/custom.md +38 -0
- package/docs/dispatchers/http.md +66 -0
- package/docs/extending.md +26 -0
- package/docs/migration/0.3.0-to-0.4.0.md +67 -0
- package/docs/migration/0.4.0-to-0.5.0.md +60 -0
- package/docs/migration/0.5.x-to-0.0.0.md +143 -0
- package/docs/producers/custom.md +63 -0
- package/docs/producers/payload-media-dependencies.md +45 -0
- package/docs/producers/payload.md +140 -0
- package/docs/reliability.md +35 -0
- package/docs/security.md +34 -0
- package/docs/troubleshooting.md +14 -0
- package/package.json +120 -4
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { toTypedLocale } from './helpers.js';
|
|
2
|
+
const linksOf = (list) => {
|
|
3
|
+
const links = new Map();
|
|
4
|
+
list.forEach((entry, index) => {
|
|
5
|
+
links.set(entry.id, {
|
|
6
|
+
prev: list[index - 1]?.id ?? null,
|
|
7
|
+
next: list[index + 1]?.id ?? null,
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
return links;
|
|
11
|
+
};
|
|
12
|
+
const insertByKey = (list, entry) => {
|
|
13
|
+
const index = list.findIndex((candidate) => candidate.key > entry.key);
|
|
14
|
+
return index === -1 ? [...list, entry] : [...list.slice(0, index), entry, ...list.slice(index)];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Exact neighbour diff for a document that moved to a new order key in one locale.
|
|
18
|
+
*
|
|
19
|
+
* Lists the published documents of the locale before and after the move (the moved document is
|
|
20
|
+
* placed at its previous key for "before"). Returns the documents whose visible Previous/Next
|
|
21
|
+
* changed. Returns `null` when the move is not a key change, so the caller uses the broad rule.
|
|
22
|
+
*/
|
|
23
|
+
const exactMoveNeighbours = async ({ req, collectionSlug, locale, change, orderField, visible, }) => {
|
|
24
|
+
const previous = change.previousOrder;
|
|
25
|
+
const current = change.doc[orderField];
|
|
26
|
+
if (!previous || typeof current !== 'string' || current === previous.key)
|
|
27
|
+
return null;
|
|
28
|
+
if (!previous.orderOnly && previous.visible === undefined)
|
|
29
|
+
return null;
|
|
30
|
+
const { docs } = await req.payload.find({
|
|
31
|
+
collection: collectionSlug,
|
|
32
|
+
locale: toTypedLocale(locale),
|
|
33
|
+
fallbackLocale: false,
|
|
34
|
+
depth: 0,
|
|
35
|
+
draft: false,
|
|
36
|
+
pagination: false,
|
|
37
|
+
overrideAccess: true,
|
|
38
|
+
req,
|
|
39
|
+
sort: orderField,
|
|
40
|
+
where: visible,
|
|
41
|
+
});
|
|
42
|
+
const movedId = String(change.doc.id);
|
|
43
|
+
const after = docs.map((doc) => ({
|
|
44
|
+
id: String(doc.id),
|
|
45
|
+
key: String(doc[orderField]),
|
|
46
|
+
doc: doc,
|
|
47
|
+
}));
|
|
48
|
+
const isVisibleAfter = after.some((entry) => entry.id === movedId);
|
|
49
|
+
// An order-only write does not change publication: visible before iff visible after.
|
|
50
|
+
const wasVisible = previous.visible ?? isVisibleAfter;
|
|
51
|
+
if (!isVisibleAfter && !wasVisible)
|
|
52
|
+
return [];
|
|
53
|
+
const others = after.filter((entry) => entry.id !== movedId);
|
|
54
|
+
const before = wasVisible
|
|
55
|
+
? insertByKey(others, { id: movedId, key: previous.key, doc: change.doc })
|
|
56
|
+
: others;
|
|
57
|
+
const linksBefore = linksOf(before);
|
|
58
|
+
const linksAfter = linksOf(after);
|
|
59
|
+
const changed = [];
|
|
60
|
+
for (const entry of others) {
|
|
61
|
+
const b = linksBefore.get(entry.id);
|
|
62
|
+
const a = linksAfter.get(entry.id);
|
|
63
|
+
if (b?.prev !== a?.prev || b?.next !== a?.next)
|
|
64
|
+
changed.push(entry.doc);
|
|
65
|
+
}
|
|
66
|
+
return changed;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Documents whose Previous/Next a change can affect, per locale, excluding the changed documents.
|
|
70
|
+
*
|
|
71
|
+
* - Moves to a new order key: exact diff of visible neighbours in that locale only.
|
|
72
|
+
* - Everything else (content, slug, create, delete, publish state at the same key): the documents
|
|
73
|
+
* directly before and after the document's current and previous keys, read in the same locale.
|
|
74
|
+
* Broader than strictly required but it covers link labels and slugs.
|
|
75
|
+
*/
|
|
76
|
+
export const resolveOrderableNeighborChanges = async ({ req, collectionSlug, changes, orderField, draftsEnabled, previousOrderKey, toChange, }) => {
|
|
77
|
+
const seen = new Set(changes.map((change) => `${change.locale}:${String(change.doc.id)}`));
|
|
78
|
+
const visible = {
|
|
79
|
+
[orderField]: { exists: true },
|
|
80
|
+
...(draftsEnabled ? { _status: { equals: 'published' } } : {}),
|
|
81
|
+
};
|
|
82
|
+
const neighbours = [];
|
|
83
|
+
const add = (doc, locale) => {
|
|
84
|
+
const dedupeKey = `${locale}:${String(doc.id)}`;
|
|
85
|
+
if (seen.has(dedupeKey))
|
|
86
|
+
return;
|
|
87
|
+
seen.add(dedupeKey);
|
|
88
|
+
neighbours.push(toChange(doc, locale));
|
|
89
|
+
};
|
|
90
|
+
for (const change of changes) {
|
|
91
|
+
if (change.type !== 'collection')
|
|
92
|
+
continue;
|
|
93
|
+
const id = String(change.doc.id);
|
|
94
|
+
const exact = change.operation === 'update' &&
|
|
95
|
+
change.previousOrder &&
|
|
96
|
+
(change.previousOrder.orderOnly || change.previousOrder.routeSlug === change.routeSlug)
|
|
97
|
+
? await exactMoveNeighbours({
|
|
98
|
+
req,
|
|
99
|
+
collectionSlug,
|
|
100
|
+
locale: change.locale,
|
|
101
|
+
change,
|
|
102
|
+
orderField,
|
|
103
|
+
visible,
|
|
104
|
+
})
|
|
105
|
+
: null;
|
|
106
|
+
if (exact) {
|
|
107
|
+
for (const doc of exact)
|
|
108
|
+
add(doc, change.locale);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const keys = [change.doc[orderField], change.previousOrder?.key, previousOrderKey].filter((key) => typeof key === 'string' && key.length > 0);
|
|
112
|
+
for (const key of new Set(keys)) {
|
|
113
|
+
const locale = toTypedLocale(change.locale);
|
|
114
|
+
const [before, after] = await Promise.all([
|
|
115
|
+
req.payload.find({
|
|
116
|
+
collection: collectionSlug,
|
|
117
|
+
locale,
|
|
118
|
+
fallbackLocale: false,
|
|
119
|
+
depth: 0,
|
|
120
|
+
draft: false,
|
|
121
|
+
limit: 1,
|
|
122
|
+
pagination: false,
|
|
123
|
+
overrideAccess: true,
|
|
124
|
+
req,
|
|
125
|
+
sort: `-${orderField}`,
|
|
126
|
+
where: {
|
|
127
|
+
and: [{ [orderField]: { less_than: key } }, { id: { not_equals: id } }, visible],
|
|
128
|
+
},
|
|
129
|
+
}),
|
|
130
|
+
req.payload.find({
|
|
131
|
+
collection: collectionSlug,
|
|
132
|
+
locale,
|
|
133
|
+
fallbackLocale: false,
|
|
134
|
+
depth: 0,
|
|
135
|
+
draft: false,
|
|
136
|
+
limit: 1,
|
|
137
|
+
pagination: false,
|
|
138
|
+
overrideAccess: true,
|
|
139
|
+
req,
|
|
140
|
+
sort: orderField,
|
|
141
|
+
where: {
|
|
142
|
+
and: [{ [orderField]: { greater_than: key } }, { id: { not_equals: id } }, visible],
|
|
143
|
+
},
|
|
144
|
+
}),
|
|
145
|
+
]);
|
|
146
|
+
for (const result of [...before.docs, ...after.docs])
|
|
147
|
+
add(result, change.locale);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return neighbours;
|
|
151
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CollectionBeforeChangeHook, CollectionBeforeDeleteHook, GlobalBeforeChangeHook, PayloadRequest } from 'payload';
|
|
2
|
+
import type { PayloadCacheInvalidationOptions } from '../revalidation-custom.js';
|
|
3
|
+
import type { RevalidateArgs } from './types.js';
|
|
4
|
+
export type PublicSnapshot = Record<string, unknown>;
|
|
5
|
+
export declare const isPublicSnapshot: (args: RevalidateArgs, snapshot: PublicSnapshot, locale: string) => boolean;
|
|
6
|
+
export declare const projectPayloadSnapshot: (data: PublicSnapshot, locale: string, operation: 'create' | 'update' | 'delete', resource: string, localized: boolean, projection?: PayloadCacheInvalidationOptions['projection']) => PublicSnapshot;
|
|
7
|
+
export declare const snapshotKey: (type: 'collection' | 'global', slug: string, id: unknown) => string;
|
|
8
|
+
/** Reads a public collection representation, bypassing the caller's authenticated identity. */
|
|
9
|
+
export declare const readPublishedRepresentation: (req: PayloadRequest, collection: string, id: string | number, locale: string) => Promise<PublicSnapshot | null>;
|
|
10
|
+
/** Reads the representation through Payload's normal read access and field visibility. */
|
|
11
|
+
export declare const readPublicSnapshot: (args: RevalidateArgs, locale: string) => Promise<PublicSnapshot | null>;
|
|
12
|
+
export declare const takePublicBefore: (req: PayloadRequest, type: 'collection' | 'global', slug: string, id: unknown, locale: string) => PublicSnapshot | null | undefined;
|
|
13
|
+
export declare const clearPublicBefore: (req: PayloadRequest, type: 'collection' | 'global', slug: string, id: unknown, locales: readonly string[]) => void;
|
|
14
|
+
export declare const createCaptureCollectionPublicBeforeChange: () => CollectionBeforeChangeHook;
|
|
15
|
+
export declare const createCaptureCollectionPublicBeforeDelete: () => CollectionBeforeDeleteHook;
|
|
16
|
+
export declare const createCaptureGlobalPublicBeforeChange: () => GlobalBeforeChangeHook;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { listLocales, toTypedLocale } from './locale.js';
|
|
2
|
+
import { draftVersionsEnabled } from './skip-rules.js';
|
|
3
|
+
import { hasLocalizedDraftStatus, isLocalePublished } from './locale-published.js';
|
|
4
|
+
import { revalidationLogError } from './log.js';
|
|
5
|
+
export const isPublicSnapshot = (args, snapshot, locale) => {
|
|
6
|
+
const schema = 'collection' in args ? args.collection : args.global;
|
|
7
|
+
if (!draftVersionsEnabled(schema))
|
|
8
|
+
return true;
|
|
9
|
+
return isLocalePublished(snapshot._status, locale, hasLocalizedDraftStatus(schema));
|
|
10
|
+
};
|
|
11
|
+
export const projectPayloadSnapshot = (data, locale, operation, resource, localized, projection) => {
|
|
12
|
+
const projected = projection ? projection({ data, locale, operation, resource }) : data;
|
|
13
|
+
if (!projected || typeof projected !== 'object' || Array.isArray(projected)) {
|
|
14
|
+
throw new TypeError('revalidation: projection must return an object');
|
|
15
|
+
}
|
|
16
|
+
const result = { ...projected };
|
|
17
|
+
if (result.id === undefined && data.id !== undefined)
|
|
18
|
+
result.id = data.id;
|
|
19
|
+
if (localized)
|
|
20
|
+
result.locale = locale;
|
|
21
|
+
return result;
|
|
22
|
+
};
|
|
23
|
+
const SNAPSHOT_KEY = '__klnapRevalidationPublicSnapshots';
|
|
24
|
+
const storeFor = (req) => {
|
|
25
|
+
req.context ??= {};
|
|
26
|
+
const context = req.context;
|
|
27
|
+
const existing = context[SNAPSHOT_KEY];
|
|
28
|
+
if (existing && typeof existing === 'object')
|
|
29
|
+
return existing;
|
|
30
|
+
const store = Object.create(null);
|
|
31
|
+
context[SNAPSHOT_KEY] = store;
|
|
32
|
+
return store;
|
|
33
|
+
};
|
|
34
|
+
export const snapshotKey = (type, slug, id) => `${type}\u0000${slug}\u0000${String(id ?? '')}`;
|
|
35
|
+
const anonymousRequest = (req) => {
|
|
36
|
+
const anonymous = Object.create(req);
|
|
37
|
+
Object.defineProperty(anonymous, 'user', { value: undefined, writable: true, configurable: true });
|
|
38
|
+
return anonymous;
|
|
39
|
+
};
|
|
40
|
+
const fallbackLocaleFor = (req) => {
|
|
41
|
+
const localization = req.payload.config.localization;
|
|
42
|
+
return localization && localization.fallback ? toTypedLocale(localization.defaultLocale) : false;
|
|
43
|
+
};
|
|
44
|
+
/** Reads a public collection representation, bypassing the caller's authenticated identity. */
|
|
45
|
+
export const readPublishedRepresentation = async (req, collection, id, locale) => {
|
|
46
|
+
try {
|
|
47
|
+
const result = await req.payload.findByID({
|
|
48
|
+
collection,
|
|
49
|
+
id,
|
|
50
|
+
locale: toTypedLocale(locale),
|
|
51
|
+
fallbackLocale: fallbackLocaleFor(req),
|
|
52
|
+
depth: 0,
|
|
53
|
+
draft: false,
|
|
54
|
+
overrideAccess: false,
|
|
55
|
+
showHiddenFields: false,
|
|
56
|
+
req: anonymousRequest(req),
|
|
57
|
+
user: undefined,
|
|
58
|
+
});
|
|
59
|
+
if (!result || typeof result !== 'object')
|
|
60
|
+
return null;
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
if (error && typeof error === 'object' && 'status' in error && error.status === 404) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
/** Reads the representation through Payload's normal read access and field visibility. */
|
|
71
|
+
export const readPublicSnapshot = async (args, locale) => {
|
|
72
|
+
const { req, doc } = args;
|
|
73
|
+
const publicReq = anonymousRequest(req);
|
|
74
|
+
const common = {
|
|
75
|
+
locale: toTypedLocale(locale),
|
|
76
|
+
fallbackLocale: fallbackLocaleFor(req),
|
|
77
|
+
depth: 0,
|
|
78
|
+
draft: false,
|
|
79
|
+
overrideAccess: false,
|
|
80
|
+
showHiddenFields: false,
|
|
81
|
+
req: publicReq,
|
|
82
|
+
user: undefined,
|
|
83
|
+
};
|
|
84
|
+
try {
|
|
85
|
+
const result = 'collection' in args
|
|
86
|
+
? await req.payload.findByID({
|
|
87
|
+
...common,
|
|
88
|
+
collection: args.collection.slug,
|
|
89
|
+
id: doc.id,
|
|
90
|
+
})
|
|
91
|
+
: await req.payload.findGlobal({
|
|
92
|
+
...common,
|
|
93
|
+
slug: args.global.slug,
|
|
94
|
+
});
|
|
95
|
+
if (!result || typeof result !== 'object')
|
|
96
|
+
return null;
|
|
97
|
+
const snapshot = result;
|
|
98
|
+
return snapshot;
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
if (error && typeof error === 'object' && 'status' in error && error.status === 404) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const readLocalesBeforeWrite = async (args) => {
|
|
108
|
+
const type = 'collection' in args ? 'collection' : 'global';
|
|
109
|
+
const slug = 'collection' in args ? args.collection.slug : args.global.slug;
|
|
110
|
+
const key = snapshotKey(type, slug, 'collection' in args ? args.doc?.id : undefined);
|
|
111
|
+
const store = storeFor(args.req);
|
|
112
|
+
for (const locale of listLocales(args.req)) {
|
|
113
|
+
const entryKey = `${key}\u0000${locale}`;
|
|
114
|
+
if (!Object.hasOwn(store, entryKey)) {
|
|
115
|
+
const snapshot = await readPublicSnapshot(args, locale);
|
|
116
|
+
store[entryKey] = snapshot && isPublicSnapshot(args, snapshot, locale) ? snapshot : null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
export const takePublicBefore = (req, type, slug, id, locale) => {
|
|
121
|
+
const store = storeFor(req);
|
|
122
|
+
const key = `${snapshotKey(type, slug, id)}\u0000${locale}`;
|
|
123
|
+
if (!Object.hasOwn(store, key))
|
|
124
|
+
return undefined;
|
|
125
|
+
const value = store[key];
|
|
126
|
+
return value ?? null;
|
|
127
|
+
};
|
|
128
|
+
export const clearPublicBefore = (req, type, slug, id, locales) => {
|
|
129
|
+
const store = storeFor(req);
|
|
130
|
+
for (const locale of locales)
|
|
131
|
+
delete store[`${snapshotKey(type, slug, id)}\u0000${locale}`];
|
|
132
|
+
};
|
|
133
|
+
export const createCaptureCollectionPublicBeforeChange = () => async (args) => {
|
|
134
|
+
if (args.operation === 'update' && args.originalDoc?.id !== undefined) {
|
|
135
|
+
try {
|
|
136
|
+
await readLocalesBeforeWrite({
|
|
137
|
+
...args,
|
|
138
|
+
doc: args.originalDoc,
|
|
139
|
+
operation: args.operation,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
revalidationLogError(args.req.payload.logger, error instanceof Error ? error.message : String(error), 'snapshot');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return args.data;
|
|
147
|
+
};
|
|
148
|
+
export const createCaptureCollectionPublicBeforeDelete = () => async (args) => {
|
|
149
|
+
try {
|
|
150
|
+
const synthetic = {
|
|
151
|
+
...args,
|
|
152
|
+
doc: { id: args.id },
|
|
153
|
+
operation: 'delete',
|
|
154
|
+
};
|
|
155
|
+
await readLocalesBeforeWrite(synthetic);
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
revalidationLogError(args.req.payload.logger, error instanceof Error ? error.message : String(error), 'snapshot');
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
export const createCaptureGlobalPublicBeforeChange = () => async (args) => {
|
|
162
|
+
try {
|
|
163
|
+
await readLocalesBeforeWrite({
|
|
164
|
+
...args,
|
|
165
|
+
doc: {},
|
|
166
|
+
operation: 'update',
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
revalidationLogError(args.req.payload.logger, error instanceof Error ? error.message : String(error), 'snapshot');
|
|
171
|
+
}
|
|
172
|
+
return args.data;
|
|
173
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CollectionAfterChangeHook, CollectionAfterDeleteHook, GlobalAfterChangeHook } from 'payload';
|
|
2
|
+
import type { Dispatcher } from '../../../core/index.js';
|
|
3
|
+
import type { PayloadCacheInvalidationOptions } from '../revalidation-custom.js';
|
|
4
|
+
import type { RevalidateArgs } from './types.js';
|
|
5
|
+
export type RevalidationHookOptions = {
|
|
6
|
+
source: string;
|
|
7
|
+
pluginExcludeFields?: readonly string[];
|
|
8
|
+
pluginCollectionExcludeFields?: Readonly<Record<string, readonly string[]>>;
|
|
9
|
+
pluginGlobalExcludeFields?: Readonly<Record<string, readonly string[]>>;
|
|
10
|
+
collections?: Readonly<Record<string, true | PayloadCacheInvalidationOptions>>;
|
|
11
|
+
globals?: Readonly<Record<string, true | PayloadCacheInvalidationOptions>>;
|
|
12
|
+
};
|
|
13
|
+
export declare const runRevalidationHookFailOpen: (args: RevalidateArgs, dispatcher: Dispatcher, options: RevalidationHookOptions) => Promise<void>;
|
|
14
|
+
export declare const createCollectionRevalidateAfterChange: (dispatcher: Dispatcher, options: RevalidationHookOptions) => CollectionAfterChangeHook;
|
|
15
|
+
export declare const createCollectionRevalidateAfterDelete: (dispatcher: Dispatcher, options: RevalidationHookOptions) => CollectionAfterDeleteHook;
|
|
16
|
+
export declare const createGlobalRevalidateAfterChange: (dispatcher: Dispatcher, options: RevalidationHookOptions) => GlobalAfterChangeHook;
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { resolveExcludeFields } from '../exclude-fields.js';
|
|
2
|
+
import { isLocaleRestoreReq } from '../publish-all/restore-guard.js';
|
|
3
|
+
import { normalizeCollectionRevalidation, normalizeGlobalRevalidation, } from '../revalidation-custom.js';
|
|
4
|
+
import { dispatchRevalidationChanges, isRevalidationOrderOnly } from './deferred.js';
|
|
5
|
+
import { docsContentEqual } from './doc-content.js';
|
|
6
|
+
import { getOperation, isDraftSave, listLocales } from './helpers.js';
|
|
7
|
+
import { logRevalidateDispatch, revalidationLog, revalidationLogError, revalidationSubject, } from './log.js';
|
|
8
|
+
import { resolveOrderableNeighborChanges } from './orderable-neighbors.js';
|
|
9
|
+
import { clearPublicBefore, isPublicSnapshot, projectPayloadSnapshot, readPublicSnapshot, readPublishedRepresentation, takePublicBefore, } from './public-snapshots.js';
|
|
10
|
+
const optionsFor = (args, options) => {
|
|
11
|
+
const collection = 'collection' in args;
|
|
12
|
+
const slug = collection ? args.collection.slug : args.global.slug;
|
|
13
|
+
const raw = (collection ? options.collections : options.globals)?.[slug] ?? true;
|
|
14
|
+
return collection ? normalizeCollectionRevalidation(raw) : normalizeGlobalRevalidation(raw);
|
|
15
|
+
};
|
|
16
|
+
const resourceType = (args) => 'collection' in args ? 'collection' : 'global';
|
|
17
|
+
const resourceSlug = (args) => 'collection' in args ? args.collection.slug : args.global.slug;
|
|
18
|
+
const resourceId = (args) => 'collection' in args ? ('id' in args ? args.id : args.doc?.id) : undefined;
|
|
19
|
+
const addRepresentationFields = (snapshot, args, locale) => {
|
|
20
|
+
const output = { ...snapshot };
|
|
21
|
+
if (!('collection' in args) && output.id === undefined)
|
|
22
|
+
output.id = resourceSlug(args);
|
|
23
|
+
if (args.req.payload.config.localization)
|
|
24
|
+
output.locale = locale;
|
|
25
|
+
return output;
|
|
26
|
+
};
|
|
27
|
+
const project = (data, args, locale, operation, options) => {
|
|
28
|
+
return projectPayloadSnapshot(data, locale, operation, `${resourceType(args)}.${resourceSlug(args)}`, Boolean(args.req.payload.config.localization), options.projection);
|
|
29
|
+
};
|
|
30
|
+
const previousSnapshot = (args, locale) => takePublicBefore(args.req, resourceType(args), resourceSlug(args), resourceId(args), locale);
|
|
31
|
+
const compareOptions = (args, options) => {
|
|
32
|
+
const meta = optionsFor(args, options);
|
|
33
|
+
return resolveExcludeFields({
|
|
34
|
+
schemaCustom: 'collection' in args ? args.collection.custom : args.global.custom,
|
|
35
|
+
pluginExcludeFields: meta.excludeFields !== undefined ? meta.excludeFields : options.pluginExcludeFields,
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
const run = async (args, dispatcher, hookOptions) => {
|
|
39
|
+
const { req } = args;
|
|
40
|
+
const logger = req.payload.logger;
|
|
41
|
+
const operation = getOperation(args);
|
|
42
|
+
if (isLocaleRestoreReq(req) || isDraftSave(req))
|
|
43
|
+
return;
|
|
44
|
+
const effective = optionsFor(args, hookOptions);
|
|
45
|
+
if (!effective.enabled)
|
|
46
|
+
return;
|
|
47
|
+
const type = resourceType(args);
|
|
48
|
+
const slug = resourceSlug(args);
|
|
49
|
+
const locales = listLocales(req);
|
|
50
|
+
const excludeFields = compareOptions(args, hookOptions);
|
|
51
|
+
const changes = [];
|
|
52
|
+
const seen = new Set();
|
|
53
|
+
for (const locale of locales) {
|
|
54
|
+
const beforeValue = operation === 'create' ? null : previousSnapshot(args, locale);
|
|
55
|
+
if (operation !== 'create' && beforeValue === undefined) {
|
|
56
|
+
throw new Error(`Public previous state unavailable for ${type}.${slug} (${locale})`);
|
|
57
|
+
}
|
|
58
|
+
const before = beforeValue ? addRepresentationFields(beforeValue, args, locale) : null;
|
|
59
|
+
const afterRead = operation === 'delete' ? null : await readPublicSnapshot(args, locale);
|
|
60
|
+
const afterValue = afterRead && isPublicSnapshot(args, afterRead, locale) ? afterRead : null;
|
|
61
|
+
const after = afterValue ? addRepresentationFields(afterValue, args, locale) : null;
|
|
62
|
+
if (before === null && after === null)
|
|
63
|
+
continue;
|
|
64
|
+
if (before && after && docsContentEqual(before, after, { excludeFields }))
|
|
65
|
+
continue;
|
|
66
|
+
if (effective.filter &&
|
|
67
|
+
!effective.filter({ before, after, locale, operation, resource: `${type}.${slug}` })) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const identity = String((after ?? before)?.id ?? slug);
|
|
71
|
+
const key = `${locale}\u0000${identity}`;
|
|
72
|
+
if (seen.has(key))
|
|
73
|
+
continue;
|
|
74
|
+
seen.add(key);
|
|
75
|
+
const projectedBefore = before ? project(before, args, locale, operation, effective) : null;
|
|
76
|
+
const projectedAfter = after ? project(after, args, locale, operation, effective) : null;
|
|
77
|
+
const previousOrderKey = before?.[effective.orderField ?? '_order'];
|
|
78
|
+
changes.push({
|
|
79
|
+
type,
|
|
80
|
+
slug,
|
|
81
|
+
operation,
|
|
82
|
+
locale,
|
|
83
|
+
doc: (after ?? before ?? {}),
|
|
84
|
+
before: projectedBefore,
|
|
85
|
+
after: projectedAfter,
|
|
86
|
+
...(typeof previousOrderKey === 'string'
|
|
87
|
+
? {
|
|
88
|
+
previousOrder: {
|
|
89
|
+
key: previousOrderKey,
|
|
90
|
+
visible: before !== null,
|
|
91
|
+
orderOnly: isRevalidationOrderOnly(req),
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
: {}),
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
if (!changes.length) {
|
|
98
|
+
revalidationLog.skip(logger, revalidationSubject(args), 'unchanged public state');
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if ('collection' in args && (args.collection.orderable || effective.orderField)) {
|
|
102
|
+
const neighbors = await resolveOrderableNeighborChanges({
|
|
103
|
+
req,
|
|
104
|
+
collectionSlug: slug,
|
|
105
|
+
changes,
|
|
106
|
+
orderField: effective.orderField ?? '_order',
|
|
107
|
+
draftsEnabled: Boolean(args.collection.versions &&
|
|
108
|
+
typeof args.collection.versions === 'object' &&
|
|
109
|
+
args.collection.versions.drafts),
|
|
110
|
+
toChange: (neighbor, locale) => ({
|
|
111
|
+
type: 'collection',
|
|
112
|
+
slug,
|
|
113
|
+
operation: 'update',
|
|
114
|
+
locale,
|
|
115
|
+
doc: neighbor,
|
|
116
|
+
}),
|
|
117
|
+
});
|
|
118
|
+
for (const neighbor of neighbors) {
|
|
119
|
+
const parent = changes.find((change) => change.locale === neighbor.locale);
|
|
120
|
+
const id = neighbor.doc.id;
|
|
121
|
+
if (!parent || (typeof id !== 'string' && typeof id !== 'number'))
|
|
122
|
+
continue;
|
|
123
|
+
const publicNeighbor = await readPublishedRepresentation(req, slug, id, neighbor.locale);
|
|
124
|
+
if (!publicNeighbor)
|
|
125
|
+
continue;
|
|
126
|
+
const snapshot = addRepresentationFields(publicNeighbor, args, neighbor.locale);
|
|
127
|
+
const data = project(snapshot, args, neighbor.locale, 'update', effective);
|
|
128
|
+
parent.affected = [...(parent.affected ?? []), { type: `payload.collection.${slug}`, data }];
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
logRevalidateDispatch(logger, revalidationSubject(args), changes.map((change) => change.locale));
|
|
132
|
+
await dispatchRevalidationChanges({
|
|
133
|
+
req,
|
|
134
|
+
changes,
|
|
135
|
+
source: hookOptions.source,
|
|
136
|
+
logger,
|
|
137
|
+
dispatcher,
|
|
138
|
+
});
|
|
139
|
+
clearPublicBefore(req, type, slug, resourceId(args), locales);
|
|
140
|
+
};
|
|
141
|
+
export const runRevalidationHookFailOpen = async (args, dispatcher, options) => {
|
|
142
|
+
try {
|
|
143
|
+
await run(args, dispatcher, options);
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
revalidationLogError(args.req.payload.logger, error instanceof Error ? error.message : String(error), 'build');
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
clearPublicBefore(args.req, resourceType(args), resourceSlug(args), resourceId(args), listLocales(args.req));
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
export const createCollectionRevalidateAfterChange = (dispatcher, options) => async (args) => {
|
|
153
|
+
await runRevalidationHookFailOpen(args, dispatcher, options);
|
|
154
|
+
return args.doc;
|
|
155
|
+
};
|
|
156
|
+
export const createCollectionRevalidateAfterDelete = (dispatcher, options) => async (args) => {
|
|
157
|
+
await runRevalidationHookFailOpen(args, dispatcher, options);
|
|
158
|
+
};
|
|
159
|
+
export const createGlobalRevalidateAfterChange = (dispatcher, options) => async (args) => {
|
|
160
|
+
await runRevalidationHookFailOpen({ ...args, operation: 'update' }, dispatcher, options);
|
|
161
|
+
return args.doc;
|
|
162
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { CollectionConfig, GlobalConfig } from 'payload';
|
|
2
|
+
import type { RevalidateArgs, RevalidationChange } from './types.js';
|
|
3
|
+
export declare const draftVersionsEnabled: (schema: {
|
|
4
|
+
versions?: CollectionConfig['versions'] | GlobalConfig['versions'];
|
|
5
|
+
}) => boolean;
|
|
6
|
+
export declare const shouldSkipDraftRevalidation: (args: RevalidateArgs) => boolean;
|
|
7
|
+
/** Collection or global with drafts — per-locale or string `_status`. */
|
|
8
|
+
export declare const shouldSkipUnpublishedLocaleDoc: ({ args, locale, localeDoc, operation, }: {
|
|
9
|
+
args: RevalidateArgs;
|
|
10
|
+
locale: string;
|
|
11
|
+
localeDoc: Record<string, unknown>;
|
|
12
|
+
operation: RevalidationChange['operation'];
|
|
13
|
+
}) => boolean;
|
|
14
|
+
export declare const shouldSkipUnchangedLocale: ({ current, previous, operation, excludeFields, }: {
|
|
15
|
+
current: Record<string, unknown>;
|
|
16
|
+
previous?: Record<string, unknown>;
|
|
17
|
+
operation: RevalidationChange['operation'];
|
|
18
|
+
excludeFields?: readonly string[];
|
|
19
|
+
}) => boolean;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { docsContentEqual } from './doc-content.js';
|
|
2
|
+
import { isUnpublishedStatus } from './draft-status.js';
|
|
3
|
+
import { getRequestLocale, isDraftSave } from './locale.js';
|
|
4
|
+
import { hasLocalizedDraftStatus, isLocaleUnpublished } from './locale-published.js';
|
|
5
|
+
import { getOperation } from './operation.js';
|
|
6
|
+
export const draftVersionsEnabled = (schema) => {
|
|
7
|
+
const versions = schema.versions;
|
|
8
|
+
if (!versions || typeof versions !== 'object' || !('drafts' in versions))
|
|
9
|
+
return false;
|
|
10
|
+
return Boolean(versions.drafts);
|
|
11
|
+
};
|
|
12
|
+
export const shouldSkipDraftRevalidation = (args) => {
|
|
13
|
+
if (getOperation(args) === 'delete')
|
|
14
|
+
return false;
|
|
15
|
+
if (isDraftSave(args.req))
|
|
16
|
+
return true;
|
|
17
|
+
if (!('doc' in args) || !args.doc)
|
|
18
|
+
return false;
|
|
19
|
+
const schema = 'collection' in args ? args.collection : args.global;
|
|
20
|
+
if (!draftVersionsEnabled(schema))
|
|
21
|
+
return false;
|
|
22
|
+
if (hasLocalizedDraftStatus(schema) && getRequestLocale(args.req)) {
|
|
23
|
+
// Locale-tab publish/unpublish passes a locale-scoped `_status` string — per-locale loop decides dispatch.
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return isUnpublishedStatus(args.doc._status);
|
|
27
|
+
};
|
|
28
|
+
/** Collection or global with drafts — per-locale or string `_status`. */
|
|
29
|
+
export const shouldSkipUnpublishedLocaleDoc = ({ args, locale, localeDoc, operation, }) => {
|
|
30
|
+
if (operation === 'delete')
|
|
31
|
+
return false;
|
|
32
|
+
const schema = 'collection' in args ? args.collection : args.global;
|
|
33
|
+
if (!draftVersionsEnabled(schema))
|
|
34
|
+
return false;
|
|
35
|
+
const localizeStatus = hasLocalizedDraftStatus(schema);
|
|
36
|
+
return isLocaleUnpublished(localeDoc._status, locale, localizeStatus);
|
|
37
|
+
};
|
|
38
|
+
export const shouldSkipUnchangedLocale = ({ current, previous, operation, excludeFields, }) => {
|
|
39
|
+
if (operation === 'delete')
|
|
40
|
+
return false;
|
|
41
|
+
if (!previous)
|
|
42
|
+
return false;
|
|
43
|
+
const becamePublished = current._status === 'published' && previous._status !== 'published';
|
|
44
|
+
if (becamePublished)
|
|
45
|
+
return false;
|
|
46
|
+
const statusSignature = (status) => {
|
|
47
|
+
if (status === undefined)
|
|
48
|
+
return '';
|
|
49
|
+
if (typeof status !== 'object' || status === null)
|
|
50
|
+
return String(status);
|
|
51
|
+
return JSON.stringify(Object.entries(status).sort(([a], [b]) => a.localeCompare(b)));
|
|
52
|
+
};
|
|
53
|
+
if (statusSignature(current._status) !== statusSignature(previous._status)) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
return docsContentEqual(current, previous, { excludeFields });
|
|
57
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { CollectionAfterChangeHook, CollectionAfterDeleteHook, GlobalAfterChangeHook } from 'payload';
|
|
2
|
+
export type RevalidationChange = {
|
|
3
|
+
/** Internal marker for the triggering resource, not a serialized field. */
|
|
4
|
+
root?: boolean;
|
|
5
|
+
type: 'collection' | 'global';
|
|
6
|
+
slug: string;
|
|
7
|
+
operation: 'create' | 'update' | 'delete';
|
|
8
|
+
locale: string;
|
|
9
|
+
doc: Record<string, unknown>;
|
|
10
|
+
/** Safe, public snapshots. Undefined only for internal orderable helper records. */
|
|
11
|
+
before?: Record<string, unknown> | null;
|
|
12
|
+
after?: Record<string, unknown> | null;
|
|
13
|
+
affected?: Array<{
|
|
14
|
+
type: string;
|
|
15
|
+
data: Record<string, unknown>;
|
|
16
|
+
}>;
|
|
17
|
+
/** Public route slug used to compare orderable neighbors. */
|
|
18
|
+
routeSlug?: string;
|
|
19
|
+
/**
|
|
20
|
+
* The same document in this locale before the save: its order key and whether it was published.
|
|
21
|
+
* Present for updates where the previous version is known; enables an exact neighbour diff.
|
|
22
|
+
*/
|
|
23
|
+
previousOrder?: {
|
|
24
|
+
key: string;
|
|
25
|
+
/** Whether the locale was published before. Undefined when the previous version was not found. */
|
|
26
|
+
visible?: boolean;
|
|
27
|
+
routeSlug?: string;
|
|
28
|
+
/** The write changed only `_order`: publication and content are unchanged. */
|
|
29
|
+
orderOnly?: boolean;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
export type RevalidateArgs = Parameters<CollectionAfterChangeHook>[0] | Parameters<CollectionAfterDeleteHook>[0] | Parameters<GlobalAfterChangeHook>[0];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { SanitizedConfig } from 'payload';
|
|
2
|
+
import { type PayloadCacheInvalidationOptions } from '../revalidation-custom.js';
|
|
3
|
+
export type CollectionMediaRef = {
|
|
4
|
+
type: 'collection';
|
|
5
|
+
slug: string;
|
|
6
|
+
/** Payload `where` key, dot-separated through groups and arrays (for example `seo.image`). */
|
|
7
|
+
wherePath: string;
|
|
8
|
+
hasDrafts: boolean;
|
|
9
|
+
/** Upload field stores multiple media IDs when true (`where` uses `in`). */
|
|
10
|
+
hasMany?: boolean;
|
|
11
|
+
};
|
|
12
|
+
export type GlobalMediaRef = {
|
|
13
|
+
type: 'global';
|
|
14
|
+
slug: string;
|
|
15
|
+
/** Keys from the global root to the upload field. */
|
|
16
|
+
path: string[];
|
|
17
|
+
};
|
|
18
|
+
export type MediaReference = CollectionMediaRef | GlobalMediaRef;
|
|
19
|
+
export type MediaReferenceOptions = {
|
|
20
|
+
collections?: Readonly<Record<string, true | PayloadCacheInvalidationOptions>>;
|
|
21
|
+
globals?: Readonly<Record<string, true | PayloadCacheInvalidationOptions>>;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Finds the collections and globals that reference `mediaSlug` through upload fields.
|
|
25
|
+
* Only entities that opted in with `custom.cacheInvalidation` are included. Derived from the
|
|
26
|
+
* sanitized Payload config, so no registry has to be maintained by hand.
|
|
27
|
+
*/
|
|
28
|
+
export declare const discoverMediaReferences: (config: Pick<SanitizedConfig, 'collections' | 'globals'>, mediaSlug: string, options?: MediaReferenceOptions) => MediaReference[];
|
|
29
|
+
export declare const resolveMediaId: (value: unknown) => number | string | null;
|
|
30
|
+
export declare const getAtPath: (doc: Record<string, unknown>, path: string[]) => unknown;
|
|
31
|
+
export declare const mediaIdsEqual: (a: number | string, b: number | string) => boolean;
|