payload-smart-deletion 1.0.2 → 1.0.3
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/dist/hooks.d.ts +1 -1
- package/dist/hooks.d.ts.map +1 -1
- package/dist/hooks.js +1 -1
- package/dist/hooks.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +7 -4
- package/dist/internals/index.d.ts +0 -30
- package/dist/internals/index.d.ts.map +0 -1
- package/dist/internals/index.js +0 -81
- package/dist/internals/index.js.map +0 -1
- package/dist/internals/procedure.d.ts +0 -34
- package/dist/internals/procedure.d.ts.map +0 -1
- package/dist/internals/procedure.js +0 -111
- package/dist/internals/procedure.js.map +0 -1
- package/dist/internals/urls.d.ts +0 -11
- package/dist/internals/urls.d.ts.map +0 -1
- package/dist/internals/urls.js +0 -23
- package/dist/internals/urls.js.map +0 -1
- package/dist/internals/utils.d.ts +0 -8
- package/dist/internals/utils.d.ts.map +0 -1
- package/dist/internals/utils.js +0 -57
- package/dist/internals/utils.js.map +0 -1
package/dist/hooks.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FieldWithPath } from '@
|
|
1
|
+
import type { FieldWithPath } from '@davincicoding/payload-plugin-kit';
|
|
2
2
|
import type { CollectionAfterChangeHook, CollectionAfterDeleteHook, RelationshipField } from 'payload';
|
|
3
3
|
/**
|
|
4
4
|
* Cascades hard-deletes to all documents referenced by a relationship field.
|
package/dist/hooks.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mCAAmC,CAAC;AAEvE,OAAO,KAAK,EACV,yBAAyB,EACzB,yBAAyB,EAEzB,iBAAiB,EAClB,MAAM,SAAS,CAAC;AAEjB;;GAEG;AACH,eAAO,MAAM,oBAAoB,0BAI5B,aAAa,CAAC,iBAAiB,CAAC,KAAG,yBAWrC,CAAC;AAEJ;;;GAGG;AACH,eAAO,MAAM,oBAAoB,0BAI5B,aAAa,CAAC,iBAAiB,CAAC,KAAG,yBAiBrC,CAAC;AAEJ;;;GAGG;AACH,eAAO,MAAM,iBAAiB,0BAIzB,aAAa,CAAC,iBAAiB,CAAC,KAAG,yBAkBrC,CAAC"}
|
package/dist/hooks.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { resolveDocumentID } from '@
|
|
1
|
+
import { resolveDocumentID } from '@davincicoding/payload-plugin-kit';
|
|
2
2
|
/**
|
|
3
3
|
* Cascades hard-deletes to all documents referenced by a relationship field.
|
|
4
4
|
*/ export const createHardDeleteHook = ({ path, relationTo })=>async ({ doc, req })=>{
|
package/dist/hooks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hooks.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../src/hooks.ts"],"sourcesContent":["import type { FieldWithPath } from '@davincicoding/payload-plugin-kit';\nimport { resolveDocumentID } from '@davincicoding/payload-plugin-kit';\nimport type {\n CollectionAfterChangeHook,\n CollectionAfterDeleteHook,\n CollectionSlug,\n RelationshipField,\n} from 'payload';\n\n/**\n * Cascades hard-deletes to all documents referenced by a relationship field.\n */\nexport const createHardDeleteHook =\n ({\n path,\n relationTo,\n }: FieldWithPath<RelationshipField>): CollectionAfterDeleteHook =>\n async ({ doc, req }) => {\n const grouped = groupByCollection(getByPath(doc, path), relationTo);\n\n for (const [collection, ids] of grouped) {\n await req.payload.delete({\n collection: collection as CollectionSlug,\n where: { id: { in: ids } },\n req,\n });\n }\n };\n\n/**\n * Cascades soft-deletes (trash) to all documents referenced by a relationship field\n * when the parent transitions from live to trashed.\n */\nexport const createSoftDeleteHook =\n ({\n path,\n relationTo,\n }: FieldWithPath<RelationshipField>): CollectionAfterChangeHook =>\n async ({ doc, previousDoc, req, operation }) => {\n if (operation !== 'update') return doc;\n if (previousDoc.deletedAt || !doc.deletedAt) return doc;\n\n const grouped = groupByCollection(getByPath(doc, path), relationTo);\n\n for (const [collection, ids] of grouped) {\n await req.payload.update({\n collection: collection as CollectionSlug,\n where: { id: { in: ids } },\n data: { deletedAt: new Date().toISOString() },\n req,\n });\n }\n\n return doc;\n };\n\n/**\n * Cascades restores to all documents referenced by a relationship field\n * when the parent transitions from trashed to live.\n */\nexport const createRestoreHook =\n ({\n path,\n relationTo,\n }: FieldWithPath<RelationshipField>): CollectionAfterChangeHook =>\n async ({ doc, previousDoc, req, operation }) => {\n if (operation !== 'update') return doc;\n if (!previousDoc.deletedAt || doc.deletedAt) return doc;\n\n const grouped = groupByCollection(getByPath(doc, path), relationTo);\n\n for (const [collection, ids] of grouped) {\n await req.payload.update({\n collection: collection as CollectionSlug,\n where: { id: { in: ids } },\n data: { deletedAt: null },\n req,\n trash: true,\n });\n }\n\n return doc;\n };\n\n// MARK: Utilities\n\nfunction getByPath(obj: Record<string, unknown>, path: string[]) {\n return path.reduce<unknown>(\n (acc, key) => (acc as Record<string, unknown> | undefined)?.[key],\n obj,\n );\n}\n\n/**\n * Groups relationship values by target collection slug.\n * Handles both non-polymorphic (plain IDs / populated objects)\n * and polymorphic (`{ relationTo, value }`) shapes.\n */\nfunction groupByCollection(\n value: unknown,\n relationTo: CollectionSlug | CollectionSlug[],\n): Map<string, (string | number)[]> {\n const entries = Array.isArray(value) ? value : [value];\n const isPolymorphic = Array.isArray(relationTo);\n const grouped = new Map<string, (string | number)[]>();\n\n for (const entry of entries) {\n if (entry == null) continue;\n const ref = entry as Record<string, unknown>;\n const col = isPolymorphic ? (ref.relationTo as string) : relationTo;\n const raw = isPolymorphic ? ref.value : entry;\n if (!col || raw == null) continue;\n const id = resolveDocumentID(raw);\n const ids = grouped.get(col);\n if (ids) {\n ids.push(id);\n } else {\n grouped.set(col, [id]);\n }\n }\n\n return grouped;\n}\n"],"names":["resolveDocumentID","createHardDeleteHook","path","relationTo","doc","req","grouped","groupByCollection","getByPath","collection","ids","payload","delete","where","id","in","createSoftDeleteHook","previousDoc","operation","deletedAt","update","data","Date","toISOString","createRestoreHook","trash","obj","reduce","acc","key","value","entries","Array","isArray","isPolymorphic","Map","entry","ref","col","raw","get","push","set"],"mappings":"AACA,SAASA,iBAAiB,QAAQ,oCAAoC;AAQtE;;CAEC,GACD,OAAO,MAAMC,uBACX,CAAC,EACCC,IAAI,EACJC,UAAU,EACuB,GACnC,OAAO,EAAEC,GAAG,EAAEC,GAAG,EAAE;QACjB,MAAMC,UAAUC,kBAAkBC,UAAUJ,KAAKF,OAAOC;QAExD,KAAK,MAAM,CAACM,YAAYC,IAAI,IAAIJ,QAAS;YACvC,MAAMD,IAAIM,OAAO,CAACC,MAAM,CAAC;gBACvBH,YAAYA;gBACZI,OAAO;oBAAEC,IAAI;wBAAEC,IAAIL;oBAAI;gBAAE;gBACzBL;YACF;QACF;IACF,EAAE;AAEJ;;;CAGC,GACD,OAAO,MAAMW,uBACX,CAAC,EACCd,IAAI,EACJC,UAAU,EACuB,GACnC,OAAO,EAAEC,GAAG,EAAEa,WAAW,EAAEZ,GAAG,EAAEa,SAAS,EAAE;QACzC,IAAIA,cAAc,UAAU,OAAOd;QACnC,IAAIa,YAAYE,SAAS,IAAI,CAACf,IAAIe,SAAS,EAAE,OAAOf;QAEpD,MAAME,UAAUC,kBAAkBC,UAAUJ,KAAKF,OAAOC;QAExD,KAAK,MAAM,CAACM,YAAYC,IAAI,IAAIJ,QAAS;YACvC,MAAMD,IAAIM,OAAO,CAACS,MAAM,CAAC;gBACvBX,YAAYA;gBACZI,OAAO;oBAAEC,IAAI;wBAAEC,IAAIL;oBAAI;gBAAE;gBACzBW,MAAM;oBAAEF,WAAW,IAAIG,OAAOC,WAAW;gBAAG;gBAC5ClB;YACF;QACF;QAEA,OAAOD;IAC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PA,IAAI+B,IAAI,CAAC3B;QACX,OAAO;YACLR,QAAQoC,GAAG,CAACJ,KAAK;gBAACxB;aAAG;QACvB;IACF;IAEA,OAAOR;AACT"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { findFields } from '@
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { findFields } from '@davincicoding/payload-plugin-kit';\nimport type { Field, Plugin, RelationshipField } from 'payload';\nimport { CUSTOM_KEY } from './const';\nimport {\n createHardDeleteHook,\n createRestoreHook,\n createSoftDeleteHook,\n} from './hooks';\n\nexport interface SmartDeletionPluginOptions {\n /**\n * When true, auto-enables `trash: true` on target collections\n * if the source collection uses trash and has a cascade field pointing to them.\n * When false, throws a config error instead.\n * @default true\n */\n autoEnableTrash?: boolean;\n}\n\n/**\n * Scans all collections for relationship fields marked with\n * `custom.smartDeletion: 'cascade'` and attaches afterDelete / afterChange\n * hooks that cascade hard-deletes, soft-deletes, and restores.\n */\nexport const smartDeletionPlugin =\n ({ autoEnableTrash = true }: SmartDeletionPluginOptions = {}): Plugin =>\n (config) => {\n config.collections ??= [];\n\n for (const collection of config.collections) {\n const cascadeFields = findFields(\n collection.fields,\n isCascadeRelationship,\n );\n if (cascadeFields.length === 0) continue;\n\n collection.hooks ??= {};\n collection.hooks.afterDelete ??= [];\n collection.hooks.afterChange ??= [];\n\n for (const field of cascadeFields) {\n const relationTo = field.relationTo;\n\n // Ensure soft-delete cascade support\n if (collection.trash) {\n const targets = Array.isArray(relationTo) ? relationTo : [relationTo];\n for (const target of targets) {\n const targetCollection = config.collections.find(\n ({ slug }) => slug === target,\n );\n if (!targetCollection) continue;\n if (targetCollection.trash) continue;\n if (autoEnableTrash) {\n targetCollection.trash = true;\n continue;\n }\n console.warn(\n `[payload-smart-deletion] Collection \"${target}\" does not have \\`trash: true\\`, ` +\n `but \"${collection.slug}\" does. Cascaded deletes from \"${collection.slug}\" ` +\n `will hard-delete \"${target}\" documents instead of trashing them. ` +\n `Enable \\`trash: true\\` on \"${target}\" or set \\`autoEnableTrash: true\\`.`,\n );\n }\n }\n\n collection.hooks.afterDelete.push(createHardDeleteHook(field));\n\n if (collection.trash) {\n collection.hooks.afterChange.push(createSoftDeleteHook(field));\n collection.hooks.afterChange.push(createRestoreHook(field));\n }\n }\n }\n\n return config;\n };\n\nfunction isCascadeRelationship(field: Field): field is RelationshipField {\n if (field.type !== 'relationship') return false;\n return field.custom?.[CUSTOM_KEY] === 'cascade';\n}\n"],"names":["findFields","CUSTOM_KEY","createHardDeleteHook","createRestoreHook","createSoftDeleteHook","smartDeletionPlugin","autoEnableTrash","config","collections","collection","cascadeFields","fields","isCascadeRelationship","length","hooks","afterDelete","afterChange","field","relationTo","trash","targets","Array","isArray","target","targetCollection","find","slug","console","warn","push","type","custom"],"mappings":"AAAA,SAASA,UAAU,QAAQ,oCAAoC;AAE/D,SAASC,UAAU,QAAQ,UAAU;AACrC,SACEC,oBAAoB,EACpBC,iBAAiB,EACjBC,oBAAoB,QACf,UAAU;AAYjB;;;;CAIC,GACD,OAAO,MAAMC,sBACX,CAAC,EAAEC,kBAAkB,IAAI,EAA8B,GAAG,CAAC,CAAC,GAC5D,CAACC;QACCA,OAAOC,WAAW,KAAK,EAAE;QAEzB,KAAK,MAAMC,cAAcF,OAAOC,WAAW,CAAE;YAC3C,MAAME,gBAAgBV,WACpBS,WAAWE,MAAM,EACjBC;YAEF,IAAIF,cAAcG,MAAM,KAAK,GAAG;YAEhCJ,WAAWK,KAAK,KAAK,CAAC;YACtBL,WAAWK,KAAK,CAACC,WAAW,KAAK,EAAE;YACnCN,WAAWK,KAAK,CAACE,WAAW,KAAK,EAAE;YAEnC,KAAK,MAAMC,SAASP,cAAe;gBACjC,MAAMQ,aAAaD,MAAMC,UAAU;gBAEnC,qCAAqC;gBACrC,IAAIT,WAAWU,KAAK,EAAE;oBACpB,MAAMC,UAAUC,MAAMC,OAAO,CAACJ,cAAcA,aAAa;wBAACA;qBAAW;oBACrE,KAAK,MAAMK,UAAUH,QAAS;wBAC5B,MAAMI,mBAAmBjB,OAAOC,WAAW,CAACiB,IAAI,CAC9C,CAAC,EAAEC,IAAI,EAAE,GAAKA,SAASH;wBAEzB,IAAI,CAACC,kBAAkB;wBACvB,IAAIA,iBAAiBL,KAAK,EAAE;wBAC5B,IAAIb,iBAAiB;4BACnBkB,iBAAiBL,KAAK,GAAG;4BACzB;wBACF;wBACAQ,QAAQC,IAAI,CACV,CAAC,qCAAqC,EAAEL,OAAO,iCAAiC,CAAC,GAC/E,CAAC,KAAK,EAAEd,WAAWiB,IAAI,CAAC,+BAA+B,EAAEjB,WAAWiB,IAAI,CAAC,EAAE,CAAC,GAC5E,CAAC,kBAAkB,EAAEH,OAAO,sCAAsC,CAAC,GACnE,CAAC,2BAA2B,EAAEA,OAAO,mCAAmC,CAAC;oBAE/E;gBACF;gBAEAd,WAAWK,KAAK,CAACC,WAAW,CAACc,IAAI,CAAC3B,qBAAqBe;gBAEvD,IAAIR,WAAWU,KAAK,EAAE;oBACpBV,WAAWK,KAAK,CAACE,WAAW,CAACa,IAAI,CAACzB,qBAAqBa;oBACvDR,WAAWK,KAAK,CAACE,WAAW,CAACa,IAAI,CAAC1B,kBAAkBc;gBACtD;YACF;QACF;QAEA,OAAOV;IACT,EAAE;AAEJ,SAASK,sBAAsBK,KAAY;IACzC,IAAIA,MAAMa,IAAI,KAAK,gBAAgB,OAAO;IAC1C,OAAOb,MAAMc,MAAM,EAAE,CAAC9B,WAAW,KAAK;AACxC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "payload-smart-deletion",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Payload CMS plugin that adds PostgreSQL-style referential actions (cascade, set_null, restrict) to relationship fields.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"payload"
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "https://github.com/davincicoding-org/payload-plugins.git",
|
|
11
|
-
"directory": "
|
|
11
|
+
"directory": "plugins/smart-deletion"
|
|
12
12
|
},
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"author": "DAVINCI CODING GmbH",
|
|
@@ -25,6 +25,9 @@
|
|
|
25
25
|
"files": [
|
|
26
26
|
"dist"
|
|
27
27
|
],
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@davincicoding/payload-utils": "0.0.1"
|
|
30
|
+
},
|
|
28
31
|
"devDependencies": {
|
|
29
32
|
"@types/node": "^22.5.4",
|
|
30
33
|
"payload": "3.72.0",
|
|
@@ -32,7 +35,7 @@
|
|
|
32
35
|
"vite": "7.0.6",
|
|
33
36
|
"vitest": "^3.1.2",
|
|
34
37
|
"zod": "4.3.5",
|
|
35
|
-
"@
|
|
38
|
+
"@davincicoding/payload-plugin-kit": "0.0.1"
|
|
36
39
|
},
|
|
37
40
|
"peerDependencies": {
|
|
38
41
|
"payload": ">=3.72.0"
|
|
@@ -45,7 +48,7 @@
|
|
|
45
48
|
"access": "public"
|
|
46
49
|
},
|
|
47
50
|
"scripts": {
|
|
48
|
-
"prebuild": "
|
|
51
|
+
"prebuild": "pnpm check:types",
|
|
49
52
|
"build": "plugin-build",
|
|
50
53
|
"check:types": "tsc --noEmit",
|
|
51
54
|
"clean": "rm -rf dist && rm -rf node_modules",
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { BasePayload, CollectionConfig, JsonObject, TypeWithID } from 'payload';
|
|
2
|
-
import z from 'zod';
|
|
3
|
-
export declare const documentIdSchema: z.ZodUnion<readonly [z.ZodNumber, z.ZodString]>;
|
|
4
|
-
export type DocumentID = TypeWithID['id'] & z.infer<typeof documentIdSchema>;
|
|
5
|
-
export declare const documentReferenceSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
6
|
-
entity: z.ZodLiteral<"collection">;
|
|
7
|
-
slug: z.ZodString;
|
|
8
|
-
id: z.ZodUnion<readonly [z.ZodNumber, z.ZodString]>;
|
|
9
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
10
|
-
entity: z.ZodLiteral<"global">;
|
|
11
|
-
slug: z.ZodString;
|
|
12
|
-
}, z.core.$strip>], "entity">;
|
|
13
|
-
export type DocumentReference = z.infer<typeof documentReferenceSchema>;
|
|
14
|
-
export declare const isPopulated: <T extends TypeWithID>(relationship: T | DocumentID) => relationship is T;
|
|
15
|
-
export declare function assertPopulated<T extends TypeWithID | null>(docsOrIds: (T | DocumentID)[], errorMessage?: (id: DocumentID) => string): T[];
|
|
16
|
-
export declare function assertPopulated<T extends TypeWithID | null>(docOrId: T | DocumentID, errorMessage?: (id: DocumentID) => string): T;
|
|
17
|
-
export declare const createCollectionConfigFactory: <T extends Record<string, unknown>>(factory: Omit<CollectionConfig, "slug"> | ((options: T & {
|
|
18
|
-
slug: string;
|
|
19
|
-
}) => Omit<CollectionConfig, "slug">)) => (options: T & {
|
|
20
|
-
slug: string;
|
|
21
|
-
}) => CollectionConfig;
|
|
22
|
-
export type IdentifiableDocument = TypeWithID['id'] | TypeWithID;
|
|
23
|
-
export declare function isIdentifiableDocument(value: unknown): value is IdentifiableDocument;
|
|
24
|
-
export declare const resolveDocumentID: (entity: IdentifiableDocument) => DocumentID;
|
|
25
|
-
export declare function fetchDocumentByReference(payload: BasePayload, ref: DocumentReference): Promise<JsonObject>;
|
|
26
|
-
export declare function updateDocumentByReference(payload: BasePayload, ref: DocumentReference, data: JsonObject): Promise<JsonObject>;
|
|
27
|
-
export { defineProcedure, type Procedure, type ProcedureBuilder, } from './procedure';
|
|
28
|
-
export { getAdminURL, getApiURL, getServerURL } from './urls';
|
|
29
|
-
export { findFields, uncaughtSwitchCase } from './utils';
|
|
30
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|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E,IAAI,EAAE,MAAM,CAAA;CAAE,KAAG,gBAG/B,CAAC;AAEL,MAAM,MAAM,oBAAoB,GAAG,UAAU,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC;AAEjE,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,oBAAoB,CAO/B;AAED,eAAO,MAAM,iBAAiB,WAAY,oBAAoB,KAAG,UAChB,CAAC;AAElD,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,WAAW,EACpB,GAAG,EAAE,iBAAiB,GACrB,OAAO,CAAC,UAAU,CAAC,CAgBrB;AAED,wBAAsB,yBAAyB,CAC7C,OAAO,EAAE,WAAW,EACpB,GAAG,EAAE,iBAAiB,EACtB,IAAI,EAAE,UAAU,uBAiBjB;AAED,OAAO,EACL,eAAe,EACf,KAAK,SAAS,EACd,KAAK,gBAAgB,GACtB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC"}
|
package/dist/internals/index.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import z from 'zod';
|
|
2
|
-
import { uncaughtSwitchCase } from './utils';
|
|
3
|
-
// MARK: Types
|
|
4
|
-
export const documentIdSchema = z.union([
|
|
5
|
-
z.number(),
|
|
6
|
-
z.string()
|
|
7
|
-
]);
|
|
8
|
-
export const documentReferenceSchema = z.discriminatedUnion('entity', [
|
|
9
|
-
z.object({
|
|
10
|
-
entity: z.literal('collection'),
|
|
11
|
-
slug: z.string(),
|
|
12
|
-
id: documentIdSchema
|
|
13
|
-
}),
|
|
14
|
-
z.object({
|
|
15
|
-
entity: z.literal('global'),
|
|
16
|
-
slug: z.string()
|
|
17
|
-
})
|
|
18
|
-
]);
|
|
19
|
-
// MARK: Type Guards
|
|
20
|
-
export const isPopulated = (relationship)=>typeof relationship === 'object';
|
|
21
|
-
export function assertPopulated(value, errorMessage = (id)=>`Doc is not populated: [${id}]`) {
|
|
22
|
-
if (value === null) return value;
|
|
23
|
-
if (Array.isArray(value)) {
|
|
24
|
-
return value.map((item)=>assertPopulated(item, errorMessage));
|
|
25
|
-
}
|
|
26
|
-
if (isPopulated(value)) return value;
|
|
27
|
-
throw new Error(errorMessage(value));
|
|
28
|
-
}
|
|
29
|
-
// MARK: Utilities
|
|
30
|
-
export const createCollectionConfigFactory = (factory)=>(options)=>({
|
|
31
|
-
slug: options.slug,
|
|
32
|
-
...typeof factory === 'function' ? factory(options) : factory
|
|
33
|
-
});
|
|
34
|
-
export function isIdentifiableDocument(value) {
|
|
35
|
-
if (typeof value === 'string') return true;
|
|
36
|
-
if (typeof value === 'number') return true;
|
|
37
|
-
if (value === null) return false;
|
|
38
|
-
if (typeof value !== 'object') return true;
|
|
39
|
-
if (!('id' in value)) return false;
|
|
40
|
-
return isIdentifiableDocument(value.id);
|
|
41
|
-
}
|
|
42
|
-
export const resolveDocumentID = (entity)=>typeof entity === 'object' ? entity.id : entity;
|
|
43
|
-
export function fetchDocumentByReference(payload, ref) {
|
|
44
|
-
switch(ref.entity){
|
|
45
|
-
case 'collection':
|
|
46
|
-
return payload.findByID({
|
|
47
|
-
collection: ref.slug,
|
|
48
|
-
id: ref.id,
|
|
49
|
-
depth: 0
|
|
50
|
-
});
|
|
51
|
-
case 'global':
|
|
52
|
-
return payload.findGlobal({
|
|
53
|
-
slug: ref.slug,
|
|
54
|
-
depth: 0
|
|
55
|
-
});
|
|
56
|
-
default:
|
|
57
|
-
return uncaughtSwitchCase(ref);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
export async function updateDocumentByReference(payload, ref, data) {
|
|
61
|
-
switch(ref.entity){
|
|
62
|
-
case 'collection':
|
|
63
|
-
return payload.update({
|
|
64
|
-
collection: ref.slug,
|
|
65
|
-
id: ref.id,
|
|
66
|
-
data: data
|
|
67
|
-
});
|
|
68
|
-
case 'global':
|
|
69
|
-
return payload.updateGlobal({
|
|
70
|
-
slug: ref.slug,
|
|
71
|
-
data: data
|
|
72
|
-
});
|
|
73
|
-
default:
|
|
74
|
-
return uncaughtSwitchCase(ref);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
export { defineProcedure } from './procedure';
|
|
78
|
-
export { getAdminURL, getApiURL, getServerURL } from './urls';
|
|
79
|
-
export { findFields, uncaughtSwitchCase } from './utils';
|
|
80
|
-
|
|
81
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/internals/index.ts"],"sourcesContent":["import type {\n BasePayload,\n CollectionConfig,\n CollectionSlug,\n GlobalSlug,\n JsonObject,\n TypeWithID,\n} from 'payload';\nimport z from 'zod';\nimport { uncaughtSwitchCase } from './utils';\n\n// MARK: Types\n\nexport const documentIdSchema = z.union([z.number(), z.string()]);\nexport type DocumentID = TypeWithID['id'] & z.infer<typeof documentIdSchema>;\n\nexport const documentReferenceSchema = z.discriminatedUnion('entity', [\n z.object({\n entity: z.literal('collection'),\n slug: z.string(),\n id: documentIdSchema,\n }),\n z.object({\n entity: z.literal('global'),\n slug: z.string(),\n }),\n]);\n\nexport type DocumentReference = z.infer<typeof documentReferenceSchema>;\n\n// MARK: Type Guards\n\nexport const isPopulated = <T extends TypeWithID>(\n relationship: T | DocumentID,\n): relationship is T => typeof relationship === 'object';\n\nexport function assertPopulated<T extends TypeWithID | null>(\n docsOrIds: (T | DocumentID)[],\n errorMessage?: (id: DocumentID) => string,\n): T[];\nexport function assertPopulated<T extends TypeWithID | null>(\n docOrId: T | DocumentID,\n errorMessage?: (id: DocumentID) => string,\n): T;\nexport function assertPopulated<T extends TypeWithID | null>(\n value: T | DocumentID | (T | DocumentID)[],\n errorMessage = (id: DocumentID) => `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 DocumentID));\n}\n\n// MARK: Utilities\n\nexport const createCollectionConfigFactory =\n <T extends Record<string, unknown>>(\n factory:\n | Omit<CollectionConfig, 'slug'>\n | ((options: T & { slug: string }) => Omit<CollectionConfig, 'slug'>),\n ) =>\n (options: T & { slug: string }): CollectionConfig => ({\n slug: options.slug,\n ...(typeof factory === 'function' ? factory(options) : factory),\n });\n\nexport type IdentifiableDocument = TypeWithID['id'] | TypeWithID;\n\nexport function isIdentifiableDocument(\n value: unknown,\n): value is IdentifiableDocument {\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 isIdentifiableDocument(value.id);\n}\n\nexport const resolveDocumentID = (entity: IdentifiableDocument): DocumentID =>\n typeof entity === 'object' ? entity.id : entity;\n\nexport function fetchDocumentByReference(\n payload: BasePayload,\n ref: DocumentReference,\n): Promise<JsonObject> {\n switch (ref.entity) {\n case 'collection':\n return payload.findByID({\n collection: ref.slug as CollectionSlug,\n id: ref.id,\n depth: 0,\n });\n case 'global':\n return payload.findGlobal({\n slug: ref.slug as GlobalSlug,\n depth: 0,\n });\n default:\n return uncaughtSwitchCase(ref);\n }\n}\n\nexport async function updateDocumentByReference(\n payload: BasePayload,\n ref: DocumentReference,\n data: JsonObject,\n) {\n switch (ref.entity) {\n case 'collection':\n return payload.update({\n collection: ref.slug as CollectionSlug,\n id: ref.id,\n data: data,\n });\n case 'global':\n return payload.updateGlobal({\n slug: ref.slug as GlobalSlug,\n data: data as any,\n });\n default:\n return uncaughtSwitchCase(ref);\n }\n}\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","uncaughtSwitchCase","documentIdSchema","union","number","string","documentReferenceSchema","discriminatedUnion","object","entity","literal","slug","id","isPopulated","relationship","assertPopulated","value","errorMessage","Array","isArray","map","item","Error","createCollectionConfigFactory","factory","options","isIdentifiableDocument","resolveDocumentID","fetchDocumentByReference","payload","ref","findByID","collection","depth","findGlobal","updateDocumentByReference","data","update","updateGlobal","defineProcedure","getAdminURL","getApiURL","getServerURL","findFields"],"mappings":"AAQA,OAAOA,OAAO,MAAM;AACpB,SAASC,kBAAkB,QAAQ,UAAU;AAE7C,cAAc;AAEd,OAAO,MAAMC,mBAAmBF,EAAEG,KAAK,CAAC;IAACH,EAAEI,MAAM;IAAIJ,EAAEK,MAAM;CAAG,EAAE;AAGlE,OAAO,MAAMC,0BAA0BN,EAAEO,kBAAkB,CAAC,UAAU;IACpEP,EAAEQ,MAAM,CAAC;QACPC,QAAQT,EAAEU,OAAO,CAAC;QAClBC,MAAMX,EAAEK,MAAM;QACdO,IAAIV;IACN;IACAF,EAAEQ,MAAM,CAAC;QACPC,QAAQT,EAAEU,OAAO,CAAC;QAClBC,MAAMX,EAAEK,MAAM;IAChB;CACD,EAAE;AAIH,oBAAoB;AAEpB,OAAO,MAAMQ,cAAc,CACzBC,eACsB,OAAOA,iBAAiB,SAAS;AAUzD,OAAO,SAASC,gBACdC,KAA0C,EAC1CC,eAAe,CAACL,KAAmB,CAAC,uBAAuB,EAAEA,GAAG,CAAC,CAAC;IAElE,IAAII,UAAU,MAAM,OAAOA;IAC3B,IAAIE,MAAMC,OAAO,CAACH,QAAQ;QACxB,OAAOA,MAAMI,GAAG,CAAC,CAACC,OAASN,gBAAgBM,MAAMJ;IACnD;IACA,IAAIJ,YAAYG,QAAQ,OAAOA;IAC/B,MAAM,IAAIM,MAAML,aAAaD;AAC/B;AAEA,kBAAkB;AAElB,OAAO,MAAMO,gCACX,CACEC,UAIF,CAACC,UAAqD,CAAA;YACpDd,MAAMc,QAAQd,IAAI;YAClB,GAAI,OAAOa,YAAY,aAAaA,QAAQC,WAAWD,OAAO;QAChE,CAAA,EAAG;AAIL,OAAO,SAASE,uBACdV,KAAc;IAE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type Method = 'get' | 'post' | 'put' | 'patch' | 'delete';
|
|
3
|
-
/** Any Zod-like schema with safeParse and inferred output */
|
|
4
|
-
interface ZodLike<TOutput = unknown> {
|
|
5
|
-
safeParse(data: unknown): {
|
|
6
|
-
success: true;
|
|
7
|
-
data: TOutput;
|
|
8
|
-
} | {
|
|
9
|
-
success: false;
|
|
10
|
-
error: unknown;
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
type InferOutput<T> = T extends ZodLike<infer O> ? O : never;
|
|
14
|
-
interface ProcedureConfig<TSchema extends ZodLike | undefined = undefined> {
|
|
15
|
-
path: `/${string}`;
|
|
16
|
-
method: Method;
|
|
17
|
-
input?: TSchema;
|
|
18
|
-
}
|
|
19
|
-
export interface Procedure<TInput, TOutput> {
|
|
20
|
-
path: `/${string}`;
|
|
21
|
-
method: Method;
|
|
22
|
-
endpoint(handler: (req: PayloadRequest, ...args: TInput extends void ? [] : [input: TInput]) => Promise<unknown | Response>): Endpoint;
|
|
23
|
-
call(apiUrl: string, ...args: TInput extends void ? [] : [input: TInput]): Promise<TOutput>;
|
|
24
|
-
}
|
|
25
|
-
export interface ProcedureBuilder<TInput> {
|
|
26
|
-
path: string;
|
|
27
|
-
method: Method;
|
|
28
|
-
returns<TOutput>(): Procedure<TInput, TOutput>;
|
|
29
|
-
endpoint(handler: (req: PayloadRequest, ...args: TInput extends void ? [] : [input: TInput]) => Promise<unknown | Response>): Endpoint;
|
|
30
|
-
call(apiUrl: string, ...args: TInput extends void ? [] : [input: TInput]): Promise<unknown>;
|
|
31
|
-
}
|
|
32
|
-
export declare function defineProcedure<TSchema extends ZodLike | undefined = undefined>(config: ProcedureConfig<TSchema>): ProcedureBuilder<TSchema extends ZodLike ? InferOutput<TSchema> : void>;
|
|
33
|
-
export {};
|
|
34
|
-
//# sourceMappingURL=procedure.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"procedure.d.ts","sourceRoot":"","sources":["../../src/internals/procedure.ts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AM;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,OAAO,KAAK,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/C,QAAQ,CACN,OAAO,EAAE,CACP,GAAG,EAAE,cAAc,EACnB,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;AA4GD,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,111 +0,0 @@
|
|
|
1
|
-
function wrapOutput(output) {
|
|
2
|
-
if (output instanceof Response) return output;
|
|
3
|
-
return Response.json(output);
|
|
4
|
-
}
|
|
5
|
-
function createProcedure(config, inputSchema) {
|
|
6
|
-
return {
|
|
7
|
-
path: config.path,
|
|
8
|
-
method: config.method,
|
|
9
|
-
endpoint (handler) {
|
|
10
|
-
return {
|
|
11
|
-
path: config.path,
|
|
12
|
-
method: config.method,
|
|
13
|
-
handler: async (req)=>{
|
|
14
|
-
if (inputSchema) {
|
|
15
|
-
if (config.method === 'get') {
|
|
16
|
-
const routeParams = req.routeParams ?? {};
|
|
17
|
-
const searchParams = req.searchParams ? Object.fromEntries(req.searchParams.entries()) : {};
|
|
18
|
-
const merged = {
|
|
19
|
-
...searchParams,
|
|
20
|
-
...routeParams
|
|
21
|
-
};
|
|
22
|
-
const result = inputSchema.safeParse(merged);
|
|
23
|
-
if (!result.success) {
|
|
24
|
-
return Response.json({
|
|
25
|
-
error: result.error
|
|
26
|
-
}, {
|
|
27
|
-
status: 400
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
// biome-ignore lint/complexity/noBannedTypes: ugly type cast
|
|
31
|
-
const output = await handler(req, result.data);
|
|
32
|
-
return wrapOutput(output);
|
|
33
|
-
}
|
|
34
|
-
const { addDataAndFileToRequest } = await import(/* webpackIgnore: true */ 'payload');
|
|
35
|
-
await addDataAndFileToRequest(req);
|
|
36
|
-
const result = inputSchema.safeParse(req.data);
|
|
37
|
-
if (!result.success) {
|
|
38
|
-
return Response.json({
|
|
39
|
-
error: result.error
|
|
40
|
-
}, {
|
|
41
|
-
status: 400
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
// biome-ignore lint/complexity/noBannedTypes: ugly type cast
|
|
45
|
-
const output = await handler(req, result.data);
|
|
46
|
-
return wrapOutput(output);
|
|
47
|
-
}
|
|
48
|
-
// biome-ignore lint/complexity/noBannedTypes: ugly type cast
|
|
49
|
-
const output = await handler(req);
|
|
50
|
-
return wrapOutput(output);
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
},
|
|
54
|
-
call (apiUrl, ...args) {
|
|
55
|
-
const input = args[0];
|
|
56
|
-
if (config.method === 'get') {
|
|
57
|
-
let resolvedPath = config.path;
|
|
58
|
-
const queryParams = {};
|
|
59
|
-
if (input) {
|
|
60
|
-
for (const [key, value] of Object.entries(input)){
|
|
61
|
-
if (value == null) continue;
|
|
62
|
-
if (resolvedPath.includes(`:${key}`)) {
|
|
63
|
-
resolvedPath = resolvedPath.replace(`:${key}`, encodeURIComponent(String(value)));
|
|
64
|
-
} else {
|
|
65
|
-
queryParams[key] = String(value);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
const queryString = new URLSearchParams(queryParams).toString();
|
|
70
|
-
const url = `${apiUrl}${resolvedPath}${queryString ? `?${queryString}` : ''}`;
|
|
71
|
-
return fetch(url, {
|
|
72
|
-
method: 'GET',
|
|
73
|
-
credentials: 'include'
|
|
74
|
-
}).then(async (response)=>{
|
|
75
|
-
if (!response.ok) {
|
|
76
|
-
throw new Error(`Request failed: ${response.status} ${response.statusText}`);
|
|
77
|
-
}
|
|
78
|
-
return response.json();
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
const url = `${apiUrl}${config.path}`;
|
|
82
|
-
return fetch(url, {
|
|
83
|
-
method: config.method.toUpperCase(),
|
|
84
|
-
credentials: 'include',
|
|
85
|
-
headers: {
|
|
86
|
-
'Content-Type': 'application/json'
|
|
87
|
-
},
|
|
88
|
-
body: input ? JSON.stringify(input) : undefined
|
|
89
|
-
}).then(async (response)=>{
|
|
90
|
-
if (!response.ok) {
|
|
91
|
-
throw new Error(`Request failed: ${response.status} ${response.statusText}`);
|
|
92
|
-
}
|
|
93
|
-
return response.json();
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
export function defineProcedure(config) {
|
|
99
|
-
const proc = createProcedure(config, config.input);
|
|
100
|
-
return {
|
|
101
|
-
path: config.path,
|
|
102
|
-
method: config.method,
|
|
103
|
-
returns () {
|
|
104
|
-
return createProcedure(config, config.input);
|
|
105
|
-
},
|
|
106
|
-
endpoint: proc.endpoint,
|
|
107
|
-
call: proc.call
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
//# sourceMappingURL=procedure.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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 (value == null) continue;\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,SAAS,OAAOC;oBACd,IAAIL,aAAa;wBACf,IAAID,OAAOG,MAAM,KAAK,OAAO;4BAC3B,MAAMI,cAAcD,IAAIC,WAAW,IAAI,CAAC;4BACxC,MAAMC,eAAeF,IAAIE,YAAY,GACjCC,OAAOC,WAAW,CAACJ,IAAIE,YAAY,CAACG,OAAO,MAC3C,CAAC;4BACL,MAAMC,SAAS;gCAAE,GAAGJ,YAAY;gCAAE,GAAGD,WAAW;4BAAC;4BACjD,MAAMM,SAASZ,YAAYa,SAAS,CAACF;4BACrC,IAAI,CAACC,OAAOE,OAAO,EAAE;gCACnB,OAAOlB,SAASC,IAAI,CAAC;oCAAEkB,OAAOH,OAAOG,KAAK;gCAAC,GAAG;oCAAEC,QAAQ;gCAAI;4BAC9D;4BACA,6DAA6D;4BAC7D,MAAMrB,SAAS,MAAM,AAACS,QAAqBC,KAAKO,OAAOK,IAAI;4BAC3D,OAAOvB,WAAWC;wBACpB;wBAEA,MAAM,EAAEuB,uBAAuB,EAAE,GA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oBACb,IAAI,CAACA,SAASC,EAAE,EAAE;wBAChB,MAAM,IAAIC,MACR,CAAC,gBAAgB,EAAEF,SAAStB,MAAM,CAAC,CAAC,EAAEsB,SAASG,UAAU,EAAE;oBAE/D;oBACA,OAAOH,SAASzC,IAAI;gBACtB;YACF;YAEA,MAAMqC,MAAM,GAAGd,SAASrB,OAAOE,IAAI,EAAE;YACrC,OAAOkC,MAAMD,KAAK;gBAChBhC,QAAQH,OAAOG,MAAM,CAACwC,WAAW;gBACjCN,aAAa;gBACbO,SAAS;oBAAE,gBAAgB;gBAAmB;gBAC9CC,MAAMtB,QAAQuB,KAAKC,SAAS,CAACxB,SAASyB;YACxC,GAAGV,IAAI,CAAC,OAAOC;gBACb,IAAI,CAACA,SAASC,EAAE,EAAE;oBAChB,MAAM,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"}
|
package/dist/internals/urls.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { PayloadRequest } from 'payload';
|
|
2
|
-
export declare function getServerURL(req: PayloadRequest): string;
|
|
3
|
-
export declare function getAdminURL({ req, path, }: {
|
|
4
|
-
req: PayloadRequest;
|
|
5
|
-
path?: '' | `/${string}` | null;
|
|
6
|
-
}): string;
|
|
7
|
-
export declare function getApiURL({ req, path, }: {
|
|
8
|
-
req: PayloadRequest;
|
|
9
|
-
path?: '' | `/${string}` | null;
|
|
10
|
-
}): string;
|
|
11
|
-
//# sourceMappingURL=urls.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/dist/internals/urls.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { formatAdminURL } from 'payload/shared';
|
|
2
|
-
export function getServerURL(req) {
|
|
3
|
-
if (!req.url) throw new Error('Could not get serverURL, since request URL is not available');
|
|
4
|
-
const { config } = req.payload;
|
|
5
|
-
if (config.serverURL) return config.serverURL;
|
|
6
|
-
return `${new URL(req.url).protocol}//${req.headers.get('host')}`;
|
|
7
|
-
}
|
|
8
|
-
export function getAdminURL({ req, path }) {
|
|
9
|
-
return formatAdminURL({
|
|
10
|
-
adminRoute: req.payload.config.routes.admin,
|
|
11
|
-
serverURL: getServerURL(req),
|
|
12
|
-
path
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
export function getApiURL({ req, path }) {
|
|
16
|
-
return formatAdminURL({
|
|
17
|
-
apiRoute: req.payload.config.routes.api,
|
|
18
|
-
serverURL: getServerURL(req),
|
|
19
|
-
path
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
//# sourceMappingURL=urls.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,8 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/dist/internals/utils.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
export const uncaughtSwitchCase = (value)=>{
|
|
2
|
-
throw new Error(`Unhandled switch case: ${value}`);
|
|
3
|
-
};
|
|
4
|
-
export function findFields(fields, condition, path = []) {
|
|
5
|
-
return fields.flatMap((field)=>{
|
|
6
|
-
if (condition(field)) {
|
|
7
|
-
return [
|
|
8
|
-
{
|
|
9
|
-
...field,
|
|
10
|
-
path: 'name' in field ? [
|
|
11
|
-
...path,
|
|
12
|
-
field.name
|
|
13
|
-
] : path
|
|
14
|
-
}
|
|
15
|
-
];
|
|
16
|
-
}
|
|
17
|
-
if ('fields' in field) {
|
|
18
|
-
return findFields(field.fields, condition, 'name' in field ? [
|
|
19
|
-
...path,
|
|
20
|
-
field.name
|
|
21
|
-
] : path);
|
|
22
|
-
}
|
|
23
|
-
switch(field.type){
|
|
24
|
-
case 'blocks':
|
|
25
|
-
return field.blocks.flatMap((block)=>findFields(block.fields, condition, [
|
|
26
|
-
...path,
|
|
27
|
-
field.name
|
|
28
|
-
]));
|
|
29
|
-
case 'tabs':
|
|
30
|
-
return field.tabs.flatMap((tab)=>findFields(tab.fields, condition, 'name' in tab ? [
|
|
31
|
-
...path,
|
|
32
|
-
tab.name
|
|
33
|
-
] : path));
|
|
34
|
-
case 'text':
|
|
35
|
-
case 'richText':
|
|
36
|
-
case 'number':
|
|
37
|
-
case 'checkbox':
|
|
38
|
-
case 'date':
|
|
39
|
-
case 'email':
|
|
40
|
-
case 'select':
|
|
41
|
-
case 'json':
|
|
42
|
-
case 'code':
|
|
43
|
-
case 'join':
|
|
44
|
-
case 'point':
|
|
45
|
-
case 'radio':
|
|
46
|
-
case 'textarea':
|
|
47
|
-
case 'ui':
|
|
48
|
-
case 'relationship':
|
|
49
|
-
case 'upload':
|
|
50
|
-
return [];
|
|
51
|
-
default:
|
|
52
|
-
return uncaughtSwitchCase(field);
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
//# sourceMappingURL=utils.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|