payload-smart-cache 1.1.2 → 1.1.4
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/README.md +18 -14
- package/dist/const.d.ts +2 -2
- package/dist/const.js +1 -1
- package/dist/endpoints/publish.d.ts.map +1 -1
- package/dist/endpoints/publish.js +5 -3
- package/dist/endpoints/publish.js.map +1 -1
- package/dist/{_common → internals}/index.d.ts +3 -1
- package/dist/internals/index.d.ts.map +1 -0
- package/dist/{_common → internals}/index.js +8 -0
- package/dist/internals/index.js.map +1 -0
- package/dist/internals/procedure.d.ts.map +1 -0
- package/dist/internals/procedure.js.map +1 -0
- package/dist/internals/urls.d.ts.map +1 -0
- package/dist/internals/urls.js.map +1 -0
- package/dist/internals/utils.d.ts +8 -0
- package/dist/internals/utils.d.ts.map +1 -0
- package/dist/{_common → internals}/utils.js +20 -5
- package/dist/internals/utils.js.map +1 -0
- package/dist/utils/tracked-collections.js +1 -1
- package/package.json +2 -2
- package/dist/_common/index.d.ts.map +0 -1
- package/dist/_common/index.js.map +0 -1
- package/dist/_common/procedure.d.ts.map +0 -1
- package/dist/_common/procedure.js.map +0 -1
- package/dist/_common/urls.d.ts.map +0 -1
- package/dist/_common/urls.js.map +0 -1
- package/dist/_common/utils.d.ts +0 -5
- package/dist/_common/utils.d.ts.map +0 -1
- package/dist/_common/utils.js.map +0 -1
- package/dist/utils/collection-changes.test.d.ts +0 -2
- package/dist/utils/collection-changes.test.d.ts.map +0 -1
- package/dist/utils/dependency-graph.test.d.ts +0 -2
- package/dist/utils/dependency-graph.test.d.ts.map +0 -1
- package/dist/utils/tracked-collections.test.d.ts +0 -2
- package/dist/utils/tracked-collections.test.d.ts.map +0 -1
- /package/dist/{_common → internals}/procedure.d.ts +0 -0
- /package/dist/{_common → internals}/procedure.js +0 -0
- /package/dist/{_common → internals}/urls.d.ts +0 -0
- /package/dist/{_common → internals}/urls.js +0 -0
package/README.md
CHANGED
|
@@ -24,18 +24,22 @@ pnpm add payload-smart-cache
|
|
|
24
24
|
|
|
25
25
|
## Usage
|
|
26
26
|
|
|
27
|
+
**Important:** `smartCachePlugin` scans collection and global fields at config time to auto-discover referenced collections. It must be listed **after** any plugin that registers collections or injects relationship fields, so those are visible during the scan.
|
|
28
|
+
|
|
27
29
|
```ts
|
|
28
30
|
// payload.config.ts
|
|
29
|
-
import { buildConfig } from
|
|
30
|
-
import {
|
|
31
|
+
import { buildConfig } from "payload";
|
|
32
|
+
import { discussionsPlugin } from "payload-discussions";
|
|
33
|
+
import { smartCachePlugin } from "payload-smart-cache";
|
|
31
34
|
|
|
32
35
|
export default buildConfig({
|
|
33
36
|
// ...
|
|
34
37
|
plugins: [
|
|
38
|
+
discussionsPlugin({ collections: ["posts"] }), // registers collections & injects fields
|
|
35
39
|
smartCachePlugin({
|
|
36
|
-
collections: [
|
|
37
|
-
globals: [
|
|
38
|
-
}),
|
|
40
|
+
collections: ["pages", "posts"],
|
|
41
|
+
globals: ["site-settings"],
|
|
42
|
+
}), // must come after
|
|
39
43
|
],
|
|
40
44
|
});
|
|
41
45
|
```
|
|
@@ -43,25 +47,25 @@ export default buildConfig({
|
|
|
43
47
|
Wrap your data-fetching functions with `createRequestHandler` so they are cached by entity tags and automatically revalidated on publish:
|
|
44
48
|
|
|
45
49
|
```ts
|
|
46
|
-
import { createRequestHandler } from
|
|
50
|
+
import { createRequestHandler } from "payload-smart-cache";
|
|
47
51
|
|
|
48
52
|
const getPosts = createRequestHandler(
|
|
49
53
|
async () => {
|
|
50
54
|
const payload = await getPayload({ config });
|
|
51
|
-
return payload.find({ collection:
|
|
55
|
+
return payload.find({ collection: "posts" });
|
|
52
56
|
},
|
|
53
|
-
[
|
|
57
|
+
["posts"], // cache tags — revalidated when posts change
|
|
54
58
|
);
|
|
55
59
|
```
|
|
56
60
|
|
|
57
61
|
### Options
|
|
58
62
|
|
|
59
|
-
| Option
|
|
60
|
-
|
|
61
|
-
| `collections`
|
|
62
|
-
| `globals`
|
|
63
|
-
| `disableAutoTracking` | `boolean`
|
|
64
|
-
| `publishHandler`
|
|
63
|
+
| Option | Type | Default | Description |
|
|
64
|
+
| --------------------- | ------------------------------------------------------ | ------- | ------------------------------------------------------------------------------------------------------------------------------- |
|
|
65
|
+
| `collections` | `CollectionSlug[]` | `[]` | Collections to track changes for. Referenced collections are auto-tracked. |
|
|
66
|
+
| `globals` | `GlobalSlug[]` | `[]` | Globals to track changes for. Referenced collections are auto-tracked. |
|
|
67
|
+
| `disableAutoTracking` | `boolean` | `false` | Disable automatic tracking of collections referenced via relationship/upload fields. |
|
|
68
|
+
| `publishHandler` | `(changes: ChangedDocuments) => void \| Promise<void>` | — | Custom handler called when changes are published. Receives a record mapping collection slugs to arrays of changed document IDs. |
|
|
65
69
|
|
|
66
70
|
## Contributing
|
|
67
71
|
|
package/dist/const.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare const ENDPOINTS: {
|
|
2
|
-
publishChanges: import("./
|
|
3
|
-
checkChanges: import("./
|
|
2
|
+
publishChanges: import("./internals/index.js").ProcedureBuilder<void>;
|
|
3
|
+
checkChanges: import("./internals/index.js").Procedure<void, {
|
|
4
4
|
hasChanges: boolean;
|
|
5
5
|
}>;
|
|
6
6
|
};
|
package/dist/const.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"publish.d.ts","sourceRoot":"","sources":["../../src/endpoints/publish.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAkB,QAAQ,EAAE,MAAM,SAAS,CAAC;AAGxD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAKvD,eAAO,MAAM,4BAA4B,mBACvB,sBAAsB,CAAC,gBAAgB,CAAC,KACvD,
|
|
1
|
+
{"version":3,"file":"publish.d.ts","sourceRoot":"","sources":["../../src/endpoints/publish.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAkB,QAAQ,EAAE,MAAM,SAAS,CAAC;AAGxD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAKvD,eAAO,MAAM,4BAA4B,mBACvB,sBAAsB,CAAC,gBAAgB,CAAC,KACvD,QAoIC,CAAC"}
|
|
@@ -13,8 +13,8 @@ export const createPublishChangesEndpoint = (publishHandler)=>ENDPOINTS.publishC
|
|
|
13
13
|
limit: 0,
|
|
14
14
|
sort: '-updatedAt'
|
|
15
15
|
});
|
|
16
|
-
if (changesToPublish.length === 0) return
|
|
17
|
-
|
|
16
|
+
if (changesToPublish.length === 0) return Response.json({
|
|
17
|
+
message: 'No changes to publish'
|
|
18
18
|
});
|
|
19
19
|
const tagsToInvalidate = new Set();
|
|
20
20
|
const collectionChanges = new CollectionChanges();
|
|
@@ -96,7 +96,9 @@ export const createPublishChangesEndpoint = (publishHandler)=>ENDPOINTS.publishC
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
});
|
|
99
|
-
return
|
|
99
|
+
return Response.json({
|
|
100
|
+
message: 'OK'
|
|
101
|
+
}, {
|
|
100
102
|
headers: headersWithCors({
|
|
101
103
|
headers: new Headers(),
|
|
102
104
|
req
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/endpoints/publish.ts"],"sourcesContent":["import { revalidateTag } from 'next/cache';\nimport type { CollectionSlug, Endpoint } from 'payload';\nimport { APIError, headersWithCors } from 'payload';\nimport { ENDPOINTS } from '@/const';\nimport type { SmartCachePluginConfig } from '../index';\nimport type { EntitySlug } from '../types';\nimport { CollectionChanges } from '../utils/collection-changes';\nimport { createDependencyGraph } from '../utils/dependency-graph';\n\nexport const createPublishChangesEndpoint = (\n publishHandler: SmartCachePluginConfig['publishHandler'],\n): Endpoint =>\n ENDPOINTS.publishChanges.endpoint(async (req) => {\n if (!req.user) {\n throw new APIError('Unauthorized', 401);\n }\n\n const { payload } = req;\n\n const { docs: changesToPublish } = await payload.find({\n collection: 'publish-queue',\n limit: 0,\n sort: '-updatedAt',\n });\n\n if (changesToPublish.length === 0)\n return
|
|
1
|
+
{"version":3,"sources":["../../src/endpoints/publish.ts"],"sourcesContent":["import { revalidateTag } from 'next/cache';\nimport type { CollectionSlug, Endpoint } from 'payload';\nimport { APIError, headersWithCors } from 'payload';\nimport { ENDPOINTS } from '@/const';\nimport type { SmartCachePluginConfig } from '../index';\nimport type { EntitySlug } from '../types';\nimport { CollectionChanges } from '../utils/collection-changes';\nimport { createDependencyGraph } from '../utils/dependency-graph';\n\nexport const createPublishChangesEndpoint = (\n publishHandler: SmartCachePluginConfig['publishHandler'],\n): Endpoint =>\n ENDPOINTS.publishChanges.endpoint(async (req) => {\n if (!req.user) {\n throw new APIError('Unauthorized', 401);\n }\n\n const { payload } = req;\n\n const { docs: changesToPublish } = await payload.find({\n collection: 'publish-queue',\n limit: 0,\n sort: '-updatedAt',\n });\n\n if (changesToPublish.length === 0)\n return Response.json({ message: 'No changes to publish' });\n\n const tagsToInvalidate = new Set<EntitySlug>();\n const collectionChanges = new CollectionChanges();\n\n for (const change of changesToPublish) {\n if (typeof change.entityId !== 'string') {\n tagsToInvalidate.add(change.entityType as EntitySlug);\n }\n }\n\n collectionChanges.initialize(changesToPublish);\n\n const graph = createDependencyGraph(payload);\n\n async function trackAffectedItems(\n collection: CollectionSlug,\n ids: string[],\n visited: Set<string>,\n ): Promise<void> {\n const dependents = graph.getDependants(collection);\n\n if (dependents.length === 0) return;\n\n for (const dependent of dependents) {\n if (dependent.entity.type === 'global') {\n tagsToInvalidate.add(dependent.entity.slug);\n continue;\n }\n\n if (visited.has(dependent.entity.slug)) continue;\n\n // Query each field separately to avoid duplicate table alias errors\n // when multiple fields map to the same table (e.g., highlights.split-image-text.image\n // and architecture.split-image-text.image both use projects_blocks_split_image_text)\n const allAffectedItems = new Map<\n string,\n {\n id: string | number;\n }\n >();\n\n for (const field of dependent.fields) {\n const { docs } = await payload.find({\n collection: dependent.entity.slug,\n where: field.polymorphic\n ? {\n and: [\n { [`${field.field}.relationTo`]: { equals: collection } },\n { [`${field.field}.value`]: { in: ids } },\n ],\n }\n : {\n [field.field]: {\n in: ids,\n },\n },\n });\n\n // Use a Map keyed by ID to deduplicate results\n for (const item of docs) {\n allAffectedItems.set(item.id.toString(), item);\n }\n }\n\n const affectedItems = Array.from(allAffectedItems.values());\n\n visited.add(dependent.entity.slug);\n\n if (affectedItems.length === 0) continue;\n\n for (const item of affectedItems) {\n collectionChanges.addItem(dependent.entity.slug, item.id.toString());\n }\n\n await trackAffectedItems(\n dependent.entity.slug,\n affectedItems.map((item) => item.id.toString()),\n visited,\n );\n }\n }\n\n const initialCollections = Array.from(collectionChanges.entries()).map(\n ([slug, ids]) => [slug, Array.from(ids)] as const,\n );\n\n for (const [collection, ids] of initialCollections) {\n await trackAffectedItems(collection, ids, new Set<string>());\n }\n\n for (const entity of collectionChanges.keys()) {\n tagsToInvalidate.add(entity);\n }\n\n for (const tag of tagsToInvalidate) {\n revalidateTag(tag);\n }\n\n await publishHandler?.(collectionChanges.serialize());\n\n await payload.delete({\n collection: 'publish-queue',\n where: {\n id: { in: changesToPublish.map((change) => change.id) },\n },\n });\n\n return Response.json(\n { message: 'OK' },\n {\n headers: headersWithCors({\n headers: new Headers(),\n req,\n }),\n },\n );\n });\n"],"names":["revalidateTag","APIError","headersWithCors","ENDPOINTS","CollectionChanges","createDependencyGraph","createPublishChangesEndpoint","publishHandler","publishChanges","endpoint","req","user","payload","docs","changesToPublish","find","collection","limit","sort","length","Response","json","message","tagsToInvalidate","Set","collectionChanges","change","entityId","add","entityType","initialize","graph","trackAffectedItems","ids","visited","dependents","getDependants","dependent","entity","type","slug","has","allAffectedItems","Map","field","fields","where","polymorphic","and","equals","in","item","set","id","toString","affectedItems","Array","from","values","addItem","map","initialCollections","entries","keys","tag","serialize","delete","headers","Headers"],"mappings":"AAAA,SAASA,aAAa,QAAQ,aAAa;AAE3C,SAASC,QAAQ,EAAEC,eAAe,QAAQ,UAAU;AACpD,SAASC,SAAS,QAAQ,UAAU;AAGpC,SAASC,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BAAmBhB,YAAYiB,KAAK,IAAIT;QAChD;QAEA,KAAK,MAAMc,UAAUb,kBAAkBsC,IAAI,GAAI;YAC7CxC,iBAAiBK,GAAG,CAACU;QACvB;QAEA,KAAK,MAAM0B,OAAOzC,iBAAkB;YAClCvB,cAAcgE;QAChB;QAEA,MAAMzD,iBAAiBkB,kBAAkBwC,SAAS;QAElD,MAAMrD,QAAQsD,MAAM,CAAC;YACnBlD,YAAY;YACZ8B,OAAO;gBACLO,IAAI;oBAAEH,IAAIpC,iBAAiB8C,GAAG,CAAC,CAAClC,SAAWA,OAAO2B,EAAE;gBAAE;YACxD;QACF;QAEA,OAAOjC,SAASC,IAAI,CAClB;YAAEC,SAAS;QAAK,GAChB;YACE6C,SAASjE,gBAAgB;gBACvBiE,SAAS,IAAIC;gBACb1D;YACF;QACF;IAEJ,GAAG"}
|
|
@@ -10,7 +10,9 @@ export declare const createCollectionConfigFactory: <T extends Record<string, un
|
|
|
10
10
|
}) => Omit<CollectionConfig, "slug">)) => (options: T & {
|
|
11
11
|
slug: CollectionSlug;
|
|
12
12
|
}) => CollectionConfig;
|
|
13
|
-
export
|
|
13
|
+
export type EntityReference = TypeWithID['id'] | TypeWithID;
|
|
14
|
+
export declare function isEntityReference(value: unknown): value is EntityReference;
|
|
15
|
+
export declare const resolveForeignKey: (entity: EntityReference) => string | number;
|
|
14
16
|
export { defineProcedure, type Procedure, type ProcedureBuilder, } from './procedure';
|
|
15
17
|
export { getAdminURL, getApiURL, getServerURL } from './urls';
|
|
16
18
|
export { findFields, uncaughtSwitchCase } from './utils';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/internals/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC5E,OAAO,CAAC,MAAM,KAAK,CAAC;AAIpB,eAAO,MAAM,cAAc,iDAAoC,CAAC;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,SAAS,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC"}
|
|
@@ -19,6 +19,14 @@ export const createCollectionConfigFactory = (factory)=>(options)=>({
|
|
|
19
19
|
slug: options.slug,
|
|
20
20
|
...typeof factory === 'function' ? factory(options) : factory
|
|
21
21
|
});
|
|
22
|
+
export function isEntityReference(value) {
|
|
23
|
+
if (typeof value === 'string') return true;
|
|
24
|
+
if (typeof value === 'number') return true;
|
|
25
|
+
if (value === null) return false;
|
|
26
|
+
if (typeof value !== 'object') return true;
|
|
27
|
+
if (!('id' in value)) return false;
|
|
28
|
+
return isEntityReference(value.id);
|
|
29
|
+
}
|
|
22
30
|
export const resolveForeignKey = (entity)=>typeof entity === 'object' ? entity.id : entity;
|
|
23
31
|
export { defineProcedure } from './procedure';
|
|
24
32
|
export { getAdminURL, getApiURL, getServerURL } from './urls';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/internals/index.ts"],"sourcesContent":["import type { CollectionConfig, CollectionSlug, TypeWithID } from 'payload';\nimport z from 'zod';\n\n// MARK: Types\n\nexport const entityIdSchema = z.union([z.number(), z.string()]);\nexport type EntityID = TypeWithID['id'] & z.infer<typeof entityIdSchema>;\n\n// MARK: Type Guards\n\nexport const isPopulated = <T extends TypeWithID>(\n relationship: T | EntityID,\n): relationship is T => typeof relationship === 'object';\n\nexport function assertPopulated<T extends TypeWithID | null>(\n docsOrIds: (T | EntityID)[],\n errorMessage?: (id: EntityID) => string,\n): T[];\nexport function assertPopulated<T extends TypeWithID | null>(\n docOrId: T | EntityID,\n errorMessage?: (id: EntityID) => string,\n): T;\nexport function assertPopulated<T extends TypeWithID | null>(\n value: T | EntityID | (T | EntityID)[],\n errorMessage = (id: EntityID) => `Doc is not populated: [${id}]`,\n): T | T[] {\n if (value === null) return value;\n if (Array.isArray(value)) {\n return value.map((item) => assertPopulated(item, errorMessage));\n }\n if (isPopulated(value)) return value;\n throw new Error(errorMessage(value as EntityID));\n}\n\n// MARK: Utilities\n\nexport const createCollectionConfigFactory =\n <T extends Record<string, unknown>>(\n factory:\n | Omit<CollectionConfig, 'slug'>\n | ((\n options: T & { slug: CollectionSlug },\n ) => Omit<CollectionConfig, 'slug'>),\n ) =>\n (options: T & { slug: CollectionSlug }): CollectionConfig => ({\n slug: options.slug,\n ...(typeof factory === 'function' ? factory(options) : factory),\n });\n\nexport type EntityReference = TypeWithID['id'] | TypeWithID;\n\nexport function isEntityReference(value: unknown): value is EntityReference {\n if (typeof value === 'string') return true;\n if (typeof value === 'number') return true;\n if (value === null) return false;\n if (typeof value !== 'object') return true;\n if (!('id' in value)) return false;\n return isEntityReference(value.id);\n}\n\nexport const resolveForeignKey = (entity: EntityReference) =>\n typeof entity === 'object' ? entity.id : entity;\n\nexport {\n defineProcedure,\n type Procedure,\n type ProcedureBuilder,\n} from './procedure';\nexport { getAdminURL, getApiURL, getServerURL } from './urls';\nexport { findFields, uncaughtSwitchCase } from './utils';\n"],"names":["z","entityIdSchema","union","number","string","isPopulated","relationship","assertPopulated","value","errorMessage","id","Array","isArray","map","item","Error","createCollectionConfigFactory","factory","options","slug","isEntityReference","resolveForeignKey","entity","defineProcedure","getAdminURL","getApiURL","getServerURL","findFields","uncaughtSwitchCase"],"mappings":"AACA,OAAOA,OAAO,MAAM;AAEpB,cAAc;AAEd,OAAO,MAAMC,iBAAiBD,EAAEE,KAAK,CAAC;IAACF,EAAEG,MAAM;IAAIH,EAAEI,MAAM;CAAG,EAAE;AAGhE,oBAAoB;AAEpB,OAAO,MAAMC,cAAc,CACzBC,eACsB,OAAOA,iBAAiB,SAAS;AAUzD,OAAO,SAASC,gBACdC,KAAsC,EACtCC,eAAe,CAACC,KAAiB,CAAC,uBAAuB,EAAEA,GAAG,CAAC,CAAC;IAEhE,IAAIF,UAAU,MAAM,OAAOA;IAC3B,IAAIG,MAAMC,OAAO,CAACJ,QAAQ;QACxB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Q,CAAC;IACZ,IAAI,CACF,MAAM,EAAE,MAAM,EACd,GAAG,IAAI,EAAE,MAAM,SAAS,IAAI,GAAG,EAAE,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,GAClD,OAAO,CAAC,OAAO,CAAC,CAAC;CACrB;AA2GD,wBAAgB,eAAe,CAC7B,OAAO,SAAS,OAAO,GAAG,SAAS,GAAG,SAAS,EAE/C,MAAM,EAAE,eAAe,CAAC,OAAO,CAAC,GAC/B,gBAAgB,CAAC,OAAO,SAAS,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,CAazE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/internals/procedure.ts"],"sourcesContent":["import type { Endpoint, PayloadRequest } from 'payload';\n\ntype Method = 'get' | 'post' | 'put' | 'patch' | 'delete';\n\n/** Any Zod-like schema with safeParse and inferred output */\ninterface ZodLike<TOutput = unknown> {\n safeParse(\n data: unknown,\n ): { success: true; data: TOutput } | { success: false; error: unknown };\n}\n\ntype InferOutput<T> = T extends ZodLike<infer O> ? O : never;\n\ninterface ProcedureConfig<TSchema extends ZodLike | undefined = undefined> {\n path: `/${string}`;\n method: Method;\n input?: TSchema;\n}\n\nexport interface Procedure<TInput, TOutput> {\n path: `/${string}`;\n method: Method;\n endpoint(\n handler: (\n req: PayloadRequest,\n ...args: TInput extends void ? [] : [input: TInput]\n ) => Promise<unknown | Response>,\n ): Endpoint;\n call(\n apiUrl: string,\n ...args: TInput extends void ? [] : [input: TInput]\n ): Promise<TOutput>;\n}\n\nexport interface ProcedureBuilder<TInput> {\n path: string;\n method: Method;\n returns<TOutput>(): Procedure<TInput, TOutput>;\n endpoint(\n handler: (\n req: PayloadRequest,\n ...args: TInput extends void ? [] : [input: TInput]\n ) => Promise<unknown | Response>,\n ): Endpoint;\n call(\n apiUrl: string,\n ...args: TInput extends void ? [] : [input: TInput]\n ): Promise<unknown>;\n}\n\nfunction wrapOutput(output: unknown): Response {\n if (output instanceof Response) return output;\n return Response.json(output);\n}\n\nfunction createProcedure<TInput, TOutput>(\n config: ProcedureConfig<ZodLike | undefined>,\n inputSchema: ZodLike | undefined,\n): Procedure<TInput, TOutput> {\n return {\n path: config.path,\n method: config.method,\n endpoint(handler) {\n return {\n path: config.path,\n method: config.method,\n handler: async (req) => {\n if (inputSchema) {\n if (config.method === 'get') {\n const routeParams = req.routeParams ?? {};\n const searchParams = req.searchParams\n ? Object.fromEntries(req.searchParams.entries())\n : {};\n const merged = { ...searchParams, ...routeParams };\n const result = inputSchema.safeParse(merged);\n if (!result.success) {\n return Response.json({ error: result.error }, { status: 400 });\n }\n // biome-ignore lint/complexity/noBannedTypes: ugly type cast\n const output = await (handler as Function)(req, result.data);\n return wrapOutput(output);\n }\n\n const { addDataAndFileToRequest } = await import(\n /* webpackIgnore: true */ 'payload'\n );\n await addDataAndFileToRequest(req);\n const result = inputSchema.safeParse(req.data);\n if (!result.success) {\n return Response.json({ error: result.error }, { status: 400 });\n }\n // biome-ignore lint/complexity/noBannedTypes: ugly type cast\n const output = await (handler as Function)(req, result.data);\n return wrapOutput(output);\n }\n // biome-ignore lint/complexity/noBannedTypes: ugly type cast\n const output = await (handler as Function)(req);\n return wrapOutput(output);\n },\n };\n },\n call(apiUrl, ...args) {\n const input = args[0] as Record<string, unknown> | undefined;\n\n if (config.method === 'get') {\n let resolvedPath = config.path;\n const queryParams: Record<string, string> = {};\n\n if (input) {\n for (const [key, value] of Object.entries(input)) {\n if (resolvedPath.includes(`:${key}`)) {\n resolvedPath = resolvedPath.replace(\n `:${key}`,\n encodeURIComponent(String(value)),\n ) as `/${string}`;\n } else {\n queryParams[key] = String(value);\n }\n }\n }\n\n const queryString = new URLSearchParams(queryParams).toString();\n const url = `${apiUrl}${resolvedPath}${queryString ? `?${queryString}` : ''}`;\n\n return fetch(url, {\n method: 'GET',\n credentials: 'include',\n }).then(async (response) => {\n if (!response.ok) {\n throw new Error(\n `Request failed: ${response.status} ${response.statusText}`,\n );\n }\n return response.json();\n }) as Promise<TOutput>;\n }\n\n const url = `${apiUrl}${config.path}`;\n return fetch(url, {\n method: config.method.toUpperCase(),\n credentials: 'include',\n headers: { 'Content-Type': 'application/json' },\n body: input ? JSON.stringify(input) : undefined,\n }).then(async (response) => {\n if (!response.ok) {\n throw new Error(\n `Request failed: ${response.status} ${response.statusText}`,\n );\n }\n return response.json();\n }) as Promise<TOutput>;\n },\n };\n}\n\nexport function defineProcedure<\n TSchema extends ZodLike | undefined = undefined,\n>(\n config: ProcedureConfig<TSchema>,\n): ProcedureBuilder<TSchema extends ZodLike ? InferOutput<TSchema> : void> {\n type TInput = TSchema extends ZodLike ? InferOutput<TSchema> : undefined;\n const proc = createProcedure<TInput, unknown>(config, config.input);\n\n return {\n path: config.path,\n method: config.method,\n returns<TOutput>(): Procedure<TInput, TOutput> {\n return createProcedure<TInput, TOutput>(config, config.input);\n },\n endpoint: proc.endpoint as unknown as ProcedureBuilder<TInput>['endpoint'],\n call: proc.call as ProcedureBuilder<TInput>['call'],\n };\n}\n"],"names":["wrapOutput","output","Response","json","createProcedure","config","inputSchema","path","method","endpoint","handler","req","routeParams","searchParams","Object","fromEntries","entries","merged","result","safeParse","success","error","status","data","addDataAndFileToRequest","call","apiUrl","args","input","resolvedPath","queryParams","key","value","includes","replace","encodeURIComponent","String","queryString","URLSearchParams","toString","url","fetch","credentials","then","response","ok","Error","statusText","toUpperCase","headers","body","JSON","stringify","undefined","defineProcedure","proc","returns"],"mappings":"AAkDA,SAASA,WAAWC,MAAe;IACjC,IAAIA,kBAAkBC,UAAU,OAAOD;IACvC,OAAOC,SAASC,IAAI,CAACF;AACvB;AAEA,SAASG,gBACPC,MAA4C,EAC5CC,WAAgC;IAEhC,OAAO;QACLC,MAAMF,OAAOE,IAAI;QACjBC,QAAQH,OAAOG,MAAM;QACrBC,UAASC,OAAO;YACd,OAAO;gBACLH,MAAMF,OAAOE,IAAI;gBACjBC,QAAQH,OAAOG,MAAM;gBACrBE,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;AAEA,OAAO,SAASmD,gBAGdjD,MAAgC;IAGhC,MAAMkD,OAAOnD,gBAAiCC,QAAQA,OAAOuB,KAAK;IAElE,OAAO;QACLrB,MAAMF,OAAOE,IAAI;QACjBC,QAAQH,OAAOG,MAAM;QACrBgD;YACE,OAAOpD,gBAAiCC,QAAQA,OAAOuB,KAAK;QAC9D;QACAnB,UAAU8C,KAAK9C,QAAQ;QACvBgB,MAAM8B,KAAK9B,IAAI;IACjB;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"urls.d.ts","sourceRoot":"","sources":["../../src/internals/urls.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAG9C,wBAAgB,YAAY,CAAC,GAAG,EAAE,cAAc,GAAG,MAAM,CAWxD;AAED,wBAAgB,WAAW,CAAC,EAC1B,GAAG,EACH,IAAI,GACL,EAAE;IACD,GAAG,EAAE,cAAc,CAAC;IACpB,IAAI,CAAC,EAAE,EAAE,GAAG,IAAI,MAAM,EAAE,GAAG,IAAI,CAAC;CACjC,GAAG,MAAM,CAMT;AAED,wBAAgB,SAAS,CAAC,EACxB,GAAG,EACH,IAAI,GACL,EAAE;IACD,GAAG,EAAE,cAAc,CAAC;IACpB,IAAI,CAAC,EAAE,EAAE,GAAG,IAAI,MAAM,EAAE,GAAG,IAAI,CAAC;CACjC,GAAG,MAAM,CAMT"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/internals/urls.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload';\nimport { formatAdminURL } from 'payload/shared';\n\nexport function getServerURL(req: PayloadRequest): string {\n if (!req.url)\n throw new Error(\n 'Could not get serverURL, since request URL is not available',\n );\n\n const { config } = req.payload;\n\n if (config.serverURL) return config.serverURL;\n\n return `${new URL(req.url).protocol}//${req.headers.get('host')}`;\n}\n\nexport function getAdminURL({\n req,\n path,\n}: {\n req: PayloadRequest;\n path?: '' | `/${string}` | null;\n}): string {\n return formatAdminURL({\n adminRoute: req.payload.config.routes.admin,\n serverURL: getServerURL(req),\n path,\n });\n}\n\nexport function getApiURL({\n req,\n path,\n}: {\n req: PayloadRequest;\n path?: '' | `/${string}` | null;\n}): string {\n return formatAdminURL({\n apiRoute: req.payload.config.routes.api,\n serverURL: getServerURL(req),\n path,\n });\n}\n"],"names":["formatAdminURL","getServerURL","req","url","Error","config","payload","serverURL","URL","protocol","headers","get","getAdminURL","path","adminRoute","routes","admin","getApiURL","apiRoute","api"],"mappings":"AACA,SAASA,cAAc,QAAQ,iBAAiB;AAEhD,OAAO,SAASC,aAAaC,GAAmB;IAC9C,IAAI,CAACA,IAAIC,GAAG,EACV,MAAM,IAAIC,MACR;IAGJ,MAAM,EAAEC,MAAM,EAAE,GAAGH,IAAII,OAAO;IAE9B,IAAID,OAAOE,SAAS,EAAE,OAAOF,OAAOE,SAAS;IAE7C,OAAO,GAAG,IAAIC,IAAIN,IAAIC,GAAG,EAAEM,QAAQ,CAAC,EAAE,EAAEP,IAAIQ,OAAO,CAACC,GAAG,CAAC,SAAS;AACnE;AAEA,OAAO,SAASC,YAAY,EAC1BV,GAAG,EACHW,IAAI,EAIL;IACC,OAAOb,eAAe;QACpBc,YAAYZ,IAAII,OAAO,CAACD,MAAM,CAACU,MAAM,CAACC,KAAK;QAC3CT,WAAWN,aAAaC;QACxBW;IACF;AACF;AAEA,OAAO,SAASI,UAAU,EACxBf,GAAG,EACHW,IAAI,EAIL;IACC,OAAOb,eAAe;QACpBkB,UAAUhB,IAAII,OAAO,CAACD,MAAM,CAACU,MAAM,CAACI,GAAG;QACvCZ,WAAWN,aAAaC;QACxBW;IACF;AACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Field } from 'payload';
|
|
2
|
+
export declare const uncaughtSwitchCase: (value: never) => never;
|
|
3
|
+
export type FieldWithPath<T extends Field> = T & {
|
|
4
|
+
path: string[];
|
|
5
|
+
};
|
|
6
|
+
export declare function findFields<T extends Field>(fields: Field[], condition: (field: Field) => field is T, path?: string[]): FieldWithPath<T>[];
|
|
7
|
+
export declare function findFields(fields: Field[], condition: (field: Field) => boolean, path?: string[]): FieldWithPath<Field>[];
|
|
8
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/internals/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAErC,eAAO,MAAM,kBAAkB,UAAW,KAAK,UAE9C,CAAC;AAEF,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,KAAK,IAAI,CAAC,GAAG;IAC/C,IAAI,EAAE,MAAM,EAAE,CAAC;CAChB,CAAC;AAEF,wBAAgB,UAAU,CAAC,CAAC,SAAS,KAAK,EACxC,MAAM,EAAE,KAAK,EAAE,EACf,SAAS,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,KAAK,IAAI,CAAC,EACvC,IAAI,CAAC,EAAE,MAAM,EAAE,GACd,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC;AACtB,wBAAgB,UAAU,CACxB,MAAM,EAAE,KAAK,EAAE,EACf,SAAS,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,EACpC,IAAI,CAAC,EAAE,MAAM,EAAE,GACd,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC"}
|
|
@@ -1,21 +1,36 @@
|
|
|
1
1
|
export const uncaughtSwitchCase = (value)=>{
|
|
2
2
|
throw new Error(`Unhandled switch case: ${value}`);
|
|
3
3
|
};
|
|
4
|
-
export function findFields(fields, condition) {
|
|
4
|
+
export function findFields(fields, condition, path = []) {
|
|
5
5
|
return fields.flatMap((field)=>{
|
|
6
6
|
if (condition(field)) {
|
|
7
7
|
return [
|
|
8
|
-
|
|
8
|
+
{
|
|
9
|
+
...field,
|
|
10
|
+
path: 'name' in field ? [
|
|
11
|
+
...path,
|
|
12
|
+
field.name
|
|
13
|
+
] : path
|
|
14
|
+
}
|
|
9
15
|
];
|
|
10
16
|
}
|
|
11
17
|
if ('fields' in field) {
|
|
12
|
-
return findFields(field.fields, condition
|
|
18
|
+
return findFields(field.fields, condition, 'name' in field ? [
|
|
19
|
+
...path,
|
|
20
|
+
field.name
|
|
21
|
+
] : path);
|
|
13
22
|
}
|
|
14
23
|
switch(field.type){
|
|
15
24
|
case 'blocks':
|
|
16
|
-
return field.blocks.flatMap((block)=>findFields(block.fields, condition
|
|
25
|
+
return field.blocks.flatMap((block)=>findFields(block.fields, condition, [
|
|
26
|
+
...path,
|
|
27
|
+
field.name
|
|
28
|
+
]));
|
|
17
29
|
case 'tabs':
|
|
18
|
-
return field.tabs.flatMap((tab)=>findFields(tab.fields, condition
|
|
30
|
+
return field.tabs.flatMap((tab)=>findFields(tab.fields, condition, 'name' in tab ? [
|
|
31
|
+
...path,
|
|
32
|
+
tab.name
|
|
33
|
+
] : path));
|
|
19
34
|
case 'text':
|
|
20
35
|
case 'richText':
|
|
21
36
|
case 'number':
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/internals/utils.ts"],"sourcesContent":["import type { Field } from 'payload';\n\nexport const uncaughtSwitchCase = (value: never) => {\n throw new Error(`Unhandled switch case: ${value}`);\n};\n\nexport type FieldWithPath<T extends Field> = T & {\n path: string[];\n};\n\nexport function findFields<T extends Field>(\n fields: Field[],\n condition: (field: Field) => field is T,\n path?: string[],\n): FieldWithPath<T>[];\nexport function findFields(\n fields: Field[],\n condition: (field: Field) => boolean,\n path?: string[],\n): FieldWithPath<Field>[];\nexport function findFields(\n fields: Field[],\n condition: (field: Field) => boolean,\n path: string[] = [],\n): FieldWithPath<Field>[] {\n return fields.flatMap((field) => {\n if (condition(field)) {\n return [\n { ...field, path: 'name' in field ? [...path, field.name] : path },\n ];\n }\n\n if ('fields' in field) {\n return findFields(\n field.fields,\n condition,\n 'name' in field ? [...path, field.name] : path,\n );\n }\n\n switch (field.type) {\n case 'blocks':\n return field.blocks.flatMap((block) =>\n findFields(block.fields, condition, [...path, field.name]),\n );\n case 'tabs':\n return field.tabs.flatMap((tab) =>\n findFields(\n tab.fields,\n condition,\n 'name' in tab ? [...path, tab.name] : path,\n ),\n );\n case 'text':\n case 'richText':\n case 'number':\n case 'checkbox':\n case 'date':\n case 'email':\n case 'select':\n case 'json':\n case 'code':\n case 'join':\n case 'point':\n case 'radio':\n case 'textarea':\n case 'ui':\n case 'relationship':\n case 'upload':\n return [];\n default:\n return uncaughtSwitchCase(field);\n }\n });\n}\n"],"names":["uncaughtSwitchCase","value","Error","findFields","fields","condition","path","flatMap","field","name","type","blocks","block","tabs","tab"],"mappings":"AAEA,OAAO,MAAMA,qBAAqB,CAACC;IACjC,MAAM,IAAIC,MAAM,CAAC,uBAAuB,EAAED,OAAO;AACnD,EAAE;AAgBF,OAAO,SAASE,WACdC,MAAe,EACfC,SAAoC,EACpCC,OAAiB,EAAE;IAEnB,OAAOF,OAAOG,OAAO,CAAC,CAACC;QACrB,IAAIH,UAAUG,QAAQ;YACpB,OAAO;gBACL;oBAAE,GAAGA,KAAK;oBAAEF,MAAM,UAAUE,QAAQ;2BAAIF;wBAAME,MAAMC,IAAI;qBAAC,GAAGH;gBAAK;aAClE;QACH;QAEA,IAAI,YAAYE,OAAO;YACrB,OAAOL,WACLK,MAAMJ,MAAM,EACZC,WACA,UAAUG,QAAQ;mBAAIF;gBAAME,MAAMC,IAAI;aAAC,GAAGH;QAE9C;QAEA,OAAQE,MAAME,IAAI;YAChB,KAAK;gBACH,OAAOF,MAAMG,MAAM,CAACJ,OAAO,CAAC,CAACK,QAC3BT,WAAWS,MAAMR,MAAM,EAAEC,WAAW;2BAAIC;wBAAME,MAAMC,IAAI;qBAAC;YAE7D,KAAK;gBACH,OAAOD,MAAMK,IAAI,CAACN,OAAO,CAAC,CAACO,MACzBX,WACEW,IAAIV,MAAM,EACVC,WACA,UAAUS,MAAM;2BAAIR;wBAAMQ,IAAIL,IAAI;qBAAC,GAAGH;YAG5C,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;gBACH,OAAO,EAAE;YACX;gBACE,OAAON,mBAAmBQ;QAC9B;IACF;AACF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { findFields } from '../
|
|
1
|
+
import { findFields } from '../internals/index.js';
|
|
2
2
|
export function getTrackedCollections(options, { collections: allCollections, globals: allGlobals }) {
|
|
3
3
|
const tracked = new Set();
|
|
4
4
|
for (const slug of options.globals){
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "payload-smart-cache",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "Payload Plugin for Cached Data",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"payload",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"access": "public"
|
|
75
75
|
},
|
|
76
76
|
"scripts": {
|
|
77
|
-
"prebuild": "rm -f src/
|
|
77
|
+
"prebuild": "rm -f src/internals 2>/dev/null; pnpm typecheck",
|
|
78
78
|
"build": "plugin-build",
|
|
79
79
|
"clean": "rm -rf dist && rm -rf node_modules",
|
|
80
80
|
"dev": "plugin-build --watch",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/_common/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC5E,OAAO,CAAC,MAAM,KAAK,CAAC;AAIpB,eAAO,MAAM,cAAc,iDAAoC,CAAC;AAChE,MAAM,MAAM,QAAQ,GAAG,UAAU,CAA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../../src/_common/index.ts"],"sourcesContent":["import type { CollectionConfig, CollectionSlug, TypeWithID } from 'payload';\nimport z from 'zod';\n\n// MARK: Types\n\nexport const entityIdSchema = z.union([z.number(), z.string()]);\nexport type EntityID = TypeWithID['id'] & z.infer<typeof entityIdSchema>;\n\n// MARK: Type Guards\n\nexport const isPopulated = <T extends TypeWithID>(\n relationship: T | EntityID,\n): relationship is T => typeof relationship === 'object';\n\nexport function assertPopulated<T extends TypeWithID | null>(\n docsOrIds: (T | EntityID)[],\n errorMessage?: (id: EntityID) => string,\n): T[];\nexport function assertPopulated<T extends TypeWithID | null>(\n docOrId: T | EntityID,\n errorMessage?: (id: EntityID) => string,\n): T;\nexport function assertPopulated<T extends TypeWithID | null>(\n value: T | EntityID | (T | EntityID)[],\n errorMessage = (id: EntityID) => `Doc is not populated: [${id}]`,\n): T | T[] {\n if (value === null) return value;\n if (Array.isArray(value)) {\n return value.map((item) => assertPopulated(item, errorMessage));\n }\n if (isPopulated(value)) return value;\n throw new Error(errorMessage(value as EntityID));\n}\n\n// MARK: Utilities\n\nexport const createCollectionConfigFactory =\n <T extends Record<string, unknown>>(\n factory:\n | Omit<CollectionConfig, 'slug'>\n | ((\n options: T & { slug: CollectionSlug },\n ) => Omit<CollectionConfig, 'slug'>),\n ) =>\n (options: T & { slug: CollectionSlug }): CollectionConfig => ({\n slug: options.slug,\n ...(typeof factory === 'function' ? factory(options) : factory),\n });\n\nexport const resolveForeignKey = (entity: TypeWithID['id'] | TypeWithID) =>\n typeof entity === 'object' ? entity.id : entity;\n\nexport {\n defineProcedure,\n type Procedure,\n type ProcedureBuilder,\n} from './procedure';\nexport { getAdminURL, getApiURL, getServerURL } from './urls';\nexport { findFields, uncaughtSwitchCase } from './utils';\n"],"names":["z","entityIdSchema","union","number","string","isPopulated","relationship","assertPopulated","value","errorMessage","id","Array","isArray","map","item","Error","createCollectionConfigFactory","factory","options","slug","resolveForeignKey","entity","defineProcedure","getAdminURL","getApiURL","getServerURL","findFields","unca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,GAAG,IAAI,EAAE,MAAM,SAAS,IAAI,GAAG,EAAE,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,KAChD,OAAO,CAAC,OAAO,GAAG,QAAQ,CAAC,GAC/B,QAAQ,CAAC;IACZ,IAAI,CACF,MAAM,EAAE,MAAM,EACd,GAAG,IAAI,EAAE,MAAM,SAAS,IAAI,GAAG,EAAE,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,GAClD,OAAO,CAAC,OAAO,CAAC,CAAC;CACrB;AA2GD,wBAAgB,eAAe,CAC7B,OAAO,SAAS,OAAO,GAAG,SAAS,GAAG,SAAS,EAE/C,MAAM,EAAE,eAAe,CAAC,OAAO,CAAC,GAC/B,gBAAgB,CAAC,OAAO,SAAS,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,CAazE"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/_common/procedure.ts"],"sourcesContent":["import type { Endpoint, PayloadRequest } from 'payload';\n\ntype Method = 'get' | 'post' | 'put' | 'patch' | 'delete';\n\n/** Any Zod-like schema with safeParse and inferred output */\ninterface ZodLike<TOutput = unknown> {\n safeParse(\n data: unknown,\n ): { success: true; data: TOutput } | { success: false; error: unknown };\n}\n\ntype InferOutput<T> = T extends ZodLike<infer O> ? O : never;\n\ninterface ProcedureConfig<TSchema extends ZodLike | undefined = undefined> {\n path: `/${string}`;\n method: Method;\n input?: TSchema;\n}\n\nexport interface Procedure<TInput, TOutput> {\n path: `/${string}`;\n method: Method;\n endpoint(\n handler: (\n req: PayloadRequest,\n ...args: TInput extends void ? [] : [input: TInput]\n ) => Promise<unknown | Response>,\n ): Endpoint;\n call(\n apiUrl: string,\n ...args: TInput extends void ? [] : [input: TInput]\n ): Promise<TOutput>;\n}\n\nexport interface ProcedureBuilder<TInput> {\n path: string;\n method: Method;\n returns<TOutput>(): Procedure<TInput, TOutput>;\n endpoint(\n handler: (\n req: PayloadRequest,\n ...args: TInput extends void ? [] : [input: TInput]\n ) => Promise<unknown | Response>,\n ): Endpoint;\n call(\n apiUrl: string,\n ...args: TInput extends void ? [] : [input: TInput]\n ): Promise<unknown>;\n}\n\nfunction wrapOutput(output: unknown): Response {\n if (output instanceof Response) return output;\n return Response.json(output);\n}\n\nfunction createProcedure<TInput, TOutput>(\n config: ProcedureConfig<ZodLike | undefined>,\n inputSchema: ZodLike | undefined,\n): Procedure<TInput, TOutput> {\n return {\n path: config.path,\n method: config.method,\n endpoint(handler) {\n return {\n path: config.path,\n method: config.method,\n handler: async (req) => {\n if (inputSchema) {\n if (config.method === 'get') {\n const routeParams = req.routeParams ?? {};\n const searchParams = req.searchParams\n ? Object.fromEntries(req.searchParams.entries())\n : {};\n const merged = { ...searchParams, ...routeParams };\n const result = inputSchema.safeParse(merged);\n if (!result.success) {\n return Response.json({ error: result.error }, { status: 400 });\n }\n // biome-ignore lint/complexity/noBannedTypes: ugly type cast\n const output = await (handler as Function)(req, result.data);\n return wrapOutput(output);\n }\n\n const { addDataAndFileToRequest } = await import(\n /* webpackIgnore: true */ 'payload'\n );\n await addDataAndFileToRequest(req);\n const result = inputSchema.safeParse(req.data);\n if (!result.success) {\n return Response.json({ error: result.error }, { status: 400 });\n }\n // biome-ignore lint/complexity/noBannedTypes: ugly type cast\n const output = await (handler as Function)(req, result.data);\n return wrapOutput(output);\n }\n // biome-ignore lint/complexity/noBannedTypes: ugly type cast\n const output = await (handler as Function)(req);\n return wrapOutput(output);\n },\n };\n },\n call(apiUrl, ...args) {\n const input = args[0] as Record<string, unknown> | undefined;\n\n if (config.method === 'get') {\n let resolvedPath = config.path;\n const queryParams: Record<string, string> = {};\n\n if (input) {\n for (const [key, value] of Object.entries(input)) {\n if (resolvedPath.includes(`:${key}`)) {\n resolvedPath = resolvedPath.replace(\n `:${key}`,\n encodeURIComponent(String(value)),\n ) as `/${string}`;\n } else {\n queryParams[key] = String(value);\n }\n }\n }\n\n const queryString = new URLSearchParams(queryParams).toString();\n const url = `${apiUrl}${resolvedPath}${queryString ? `?${queryString}` : ''}`;\n\n return fetch(url, {\n method: 'GET',\n credentials: 'include',\n }).then(async (response) => {\n if (!response.ok) {\n throw new Error(\n `Request failed: ${response.status} ${response.statusText}`,\n );\n }\n return response.json();\n }) as Promise<TOutput>;\n }\n\n const url = `${apiUrl}${config.path}`;\n return fetch(url, {\n method: config.method.toUpperCase(),\n credentials: 'include',\n headers: { 'Content-Type': 'application/json' },\n body: input ? JSON.stringify(input) : undefined,\n }).then(async (response) => {\n if (!response.ok) {\n throw new Error(\n `Request failed: ${response.status} ${response.statusText}`,\n );\n }\n return response.json();\n }) as Promise<TOutput>;\n },\n };\n}\n\nexport function defineProcedure<\n TSchema extends ZodLike | undefined = undefined,\n>(\n config: ProcedureConfig<TSchema>,\n): ProcedureBuilder<TSchema extends ZodLike ? InferOutput<TSchema> : void> {\n type TInput = TSchema extends ZodLike ? InferOutput<TSchema> : undefined;\n const proc = createProcedure<TInput, unknown>(config, config.input);\n\n return {\n path: config.path,\n method: config.method,\n returns<TOutput>(): Procedure<TInput, TOutput> {\n return createProcedure<TInput, TOutput>(config, config.input);\n },\n endpoint: proc.endpoint as unknown as ProcedureBuilder<TInput>['endpoint'],\n call: proc.call as ProcedureBuilder<TInput>['call'],\n };\n}\n"],"names":["wrapOutput","output","Response","json","createProcedure","config","inputSchema","path","method","endpoint","handler","req","routeParams","searchParams","Object","fromEntries","entries","merged","result","safeParse","success","error","status","data","addDataAndFileToRequest","call","apiUrl","args","input","resolvedPath","queryParams","key","value","includes","replace","encodeURIComponent","String","queryString","URLSearchParams","toString","url","fetch","credentials","then","response","ok","Error","statusText","toUpperCase","headers","body","JSON","stringify","undefined","defineProcedure","proc","returns"],"mappings":"AAkDA,SAASA,WAAWC,MAAe;IACjC,IAAIA,kBAAkBC,UAAU,OAAOD;IACvC,OAAOC,SAASC,IAAI,CAACF;AACvB;AAEA,SAASG,gBACPC,MAA4C,EAC5CC,WAAgC;IAEhC,OAAO;QACLC,MAA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IAAIC,MACR,CAAC,gBAAgB,EAAEF,SAAStB,MAAM,CAAC,CAAC,EAAEsB,SAASG,UAAU,EAAE;gBAE/D;gBACA,OAAOH,SAASzC,IAAI;YACtB;QACF;IACF;AACF;AAEA,OAAO,SAASmD,gBAGdjD,MAAgC;IAGhC,MAAMkD,OAAOnD,gBAAiCC,QAAQA,OAAOuB,KAAK;IAElE,OAAO;QACLrB,MAAMF,OAAOE,IAAI;QACjBC,QAAQH,OAAOG,MAAM;QACrBgD;YACE,OAAOpD,gBAAiCC,QAAQA,OAAOuB,KAAK;QAC9D;QACAnB,UAAU8C,KAAK9C,QAAQ;QACvBgB,MAAM8B,KAAK9B,IAAI;IACjB;AACF"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"urls.d.ts","sourceRoot":"","sources":["../../src/_common/urls.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAG9C,wBAAgB,YAAY,CAAC,GAAG,EAAE,cAAc,GAAG,MAAM,CAWxD;AAED,wBAAgB,WAAW,CAAC,EAC1B,GAAG,EACH,IAAI,GACL,EAAE;IACD,GAAG,EAAE,cAAc,CAAC;IACpB,IAAI,CAAC,EAAE,EAAE,GAAG,IAAI,MAAM,EAAE,GAAG,IAAI,CAAC;CACjC,GAAG,MAAM,CAMT;AAED,wBAAgB,SAAS,CAAC,EACxB,GAAG,EACH,IAAI,GACL,EAAE;IACD,GAAG,EAAE,cAAc,CAAC;IACpB,IAAI,CAAC,EAAE,EAAE,GAAG,IAAI,MAAM,EAAE,GAAG,IAAI,CAAC;CACjC,GAAG,MAAM,CAMT"}
|
package/dist/_common/urls.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/_common/urls.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload';\nimport { formatAdminURL } from 'payload/shared';\n\nexport function getServerURL(req: PayloadRequest): string {\n if (!req.url)\n throw new Error(\n 'Could not get serverURL, since request URL is not available',\n );\n\n const { config } = req.payload;\n\n if (config.serverURL) return config.serverURL;\n\n return `${new URL(req.url).protocol}//${req.headers.get('host')}`;\n}\n\nexport function getAdminURL({\n req,\n path,\n}: {\n req: PayloadRequest;\n path?: '' | `/${string}` | null;\n}): string {\n return formatAdminURL({\n adminRoute: req.payload.config.routes.admin,\n serverURL: getServerURL(req),\n path,\n });\n}\n\nexport function getApiURL({\n req,\n path,\n}: {\n req: PayloadRequest;\n path?: '' | `/${string}` | null;\n}): string {\n return formatAdminURL({\n apiRoute: req.payload.config.routes.api,\n serverURL: getServerURL(req),\n path,\n });\n}\n"],"names":["formatAdminURL","getServerURL","req","url","Error","config","payload","serverURL","URL","protocol","headers","get","getAdminURL","path","adminRoute","routes","admin","getApiURL","apiRoute","api"],"mappings":"AACA,SAASA,cAAc,QAAQ,iBAAiB;AAEhD,OAAO,SAASC,aAAaC,GAAmB;IAC9C,IAAI,CAACA,IAAIC,GAAG,EACV,MAAM,IAAIC,MACR;IAGJ,MAAM,EAAEC,MAAM,EAAE,GAAGH,IAAII,OAAO;IAE9B,IAAID,OAAOE,SAAS,EAAE,OAAOF,OAAOE,SAAS;IAE7C,OAAO,GAAG,IAAIC,IAAIN,IAAIC,GAAG,EAAEM,QAAQ,CAAC,EAAE,EAAEP,IAAIQ,OAAO,CAACC,GAAG,CAAC,SAAS;AACnE;AAEA,OAAO,SAASC,YAAY,EAC1BV,GAAG,EACHW,IAAI,EAIL;IACC,OAAOb,eAAe;QACpBc,YAAYZ,IAAII,OAAO,CAACD,MAAM,CAACU,MAAM,CAACC,KAAK;QAC3CT,WAAWN,aAAaC;QACxBW;IACF;AACF;AAEA,OAAO,SAASI,UAAU,EACxBf,GAAG,EACHW,IAAI,EAIL;IACC,OAAOb,eAAe;QACpBkB,UAAUhB,IAAII,OAAO,CAACD,MAAM,CAACU,MAAM,CAACI,GAAG;QACvCZ,WAAWN,aAAaC;QACxBW;IACF;AACF"}
|
package/dist/_common/utils.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { Field } from 'payload';
|
|
2
|
-
export declare const uncaughtSwitchCase: (value: never) => never;
|
|
3
|
-
export declare function findFields<T extends Field>(fields: Field[], condition: (field: Field) => field is T): T[];
|
|
4
|
-
export declare function findFields(fields: Field[], condition: (field: Field) => boolean): Field[];
|
|
5
|
-
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/_common/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAErC,eAAO,MAAM,kBAAkB,UAAW,KAAK,UAE9C,CAAC;AAEF,wBAAgB,UAAU,CAAC,CAAC,SAAS,KAAK,EACxC,MAAM,EAAE,KAAK,EAAE,EACf,SAAS,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,KAAK,IAAI,CAAC,GACtC,CAAC,EAAE,CAAC;AACP,wBAAgB,UAAU,CACxB,MAAM,EAAE,KAAK,EAAE,EACf,SAAS,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,GACnC,KAAK,EAAE,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/_common/utils.ts"],"sourcesContent":["import type { Field } from 'payload';\n\nexport const uncaughtSwitchCase = (value: never) => {\n throw new Error(`Unhandled switch case: ${value}`);\n};\n\nexport function findFields<T extends Field>(\n fields: Field[],\n condition: (field: Field) => field is T,\n): T[];\nexport function findFields(\n fields: Field[],\n condition: (field: Field) => boolean,\n): Field[];\nexport function findFields(\n fields: Field[],\n condition: (field: Field) => boolean,\n): Field[] {\n return fields.flatMap((field) => {\n if (condition(field)) {\n return [field];\n }\n\n if ('fields' in field) {\n return findFields(field.fields, condition);\n }\n\n switch (field.type) {\n case 'blocks':\n return field.blocks.flatMap((block) =>\n findFields(block.fields, condition),\n );\n case 'tabs':\n return field.tabs.flatMap((tab) => findFields(tab.fields, condition));\n case 'text':\n case 'richText':\n case 'number':\n case 'checkbox':\n case 'date':\n case 'email':\n case 'select':\n case 'json':\n case 'code':\n case 'join':\n case 'point':\n case 'radio':\n case 'textarea':\n case 'ui':\n case 'relationship':\n case 'upload':\n return [];\n default:\n return uncaughtSwitchCase(field);\n }\n });\n}\n"],"names":["uncaughtSwitchCase","value","Error","findFields","fields","condition","flatMap","field","type","blocks","block","tabs","tab"],"mappings":"AAEA,OAAO,MAAMA,qBAAqB,CAACC;IACjC,MAAM,IAAIC,MAAM,CAAC,uBAAuB,EAAED,OAAO;AACnD,EAAE;AAUF,OAAO,SAASE,WACdC,MAAe,EACfC,SAAoC;IAEpC,OAAOD,OAAOE,OAAO,CAAC,CAACC;QACrB,IAAIF,UAAUE,QAAQ;YACpB,OAAO;gBAACA;aAAM;QAChB;QAEA,IAAI,YAAYA,OAAO;YACrB,OAAOJ,WAAWI,MAAMH,MAAM,EAAEC;QAClC;QAEA,OAAQE,MAAMC,IAAI;YAChB,KAAK;gBACH,OAAOD,MAAME,MAAM,CAACH,OAAO,CAAC,CAACI,QAC3BP,WAAWO,MAAMN,MAAM,EAAEC;YAE7B,KAAK;gBACH,OAAOE,MAAMI,IAAI,CAACL,OAAO,CAAC,CAACM,MAAQT,WAAWS,IAAIR,MAAM,EAAEC;YAC5D,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;YACL,KAAK;gBACH,OAAO,EAAE;YACX;gBACE,OAAOL,mBAAmBO;QAC9B;IACF;AACF"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"collection-changes.test.d.ts","sourceRoot":"","sources":["../../src/utils/collection-changes.test.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dependency-graph.test.d.ts","sourceRoot":"","sources":["../../src/utils/dependency-graph.test.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tracked-collections.test.d.ts","sourceRoot":"","sources":["../../src/utils/tracked-collections.test.ts"],"names":[],"mappings":""}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|