@ainsleydev/payload-helper 0.0.13 → 0.0.15
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/index.js +17 -5
- package/dist/index.js.map +1 -1
- package/dist/plugin/hooks.d.ts +10 -8
- package/dist/plugin/hooks.js +30 -22
- package/dist/plugin/hooks.js.map +1 -1
- package/dist/types.d.ts +2 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { cacheHookCollections, cacheHookGlobals } from './plugin/hooks.js';
|
|
2
2
|
import { fieldMapper, schemas } from './plugin/schema.js';
|
|
3
3
|
import env from './util/env.js';
|
|
4
4
|
/**
|
|
@@ -16,26 +16,38 @@ import env from './util/env.js';
|
|
|
16
16
|
// biome-ignore lint/style/noParameterAssign: Need to change field mapper.
|
|
17
17
|
incomingConfig = fieldMapper(incomingConfig);
|
|
18
18
|
}
|
|
19
|
+
// TODO: Validate Config
|
|
20
|
+
// Update typescript generation file
|
|
19
21
|
incomingConfig.typescript = incomingConfig.typescript || {};
|
|
20
22
|
incomingConfig.typescript.outputFile = './src/types/payload.ts';
|
|
21
23
|
// Map collections & add hooks
|
|
22
|
-
(incomingConfig.collections || []).map((collection)=>{
|
|
24
|
+
incomingConfig.collections = (incomingConfig.collections || []).map((collection)=>{
|
|
23
25
|
return {
|
|
24
26
|
...collection,
|
|
25
27
|
hooks: {
|
|
26
28
|
afterChange: [
|
|
27
|
-
|
|
29
|
+
cacheHookCollections({
|
|
30
|
+
server: pluginOptions.webServer,
|
|
31
|
+
slug: collection.slug,
|
|
32
|
+
fields: collection.fields,
|
|
33
|
+
isCollection: true
|
|
34
|
+
})
|
|
28
35
|
]
|
|
29
36
|
}
|
|
30
37
|
};
|
|
31
38
|
});
|
|
32
39
|
// Map globals & add hooks
|
|
33
|
-
(incomingConfig.globals || []).map((global)=>{
|
|
40
|
+
incomingConfig.globals = (incomingConfig.globals || []).map((global)=>{
|
|
34
41
|
return {
|
|
35
42
|
...global,
|
|
36
43
|
hooks: {
|
|
37
44
|
afterChange: [
|
|
38
|
-
|
|
45
|
+
cacheHookGlobals({
|
|
46
|
+
server: pluginOptions.webServer,
|
|
47
|
+
slug: global.slug,
|
|
48
|
+
fields: global.fields,
|
|
49
|
+
isCollection: true
|
|
50
|
+
})
|
|
39
51
|
]
|
|
40
52
|
}
|
|
41
53
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Config } from 'payload';\nimport {
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { CollectionConfig, Config } from 'payload';\nimport { cacheHookCollections, cacheHookGlobals } from './plugin/hooks.js';\nimport { fieldMapper, schemas } from './plugin/schema.js';\nimport type { PayloadHelperPluginConfig } from './types.js';\nimport env from './util/env.js';\n\n/**\n * Payload Helper Plugin for websites at ainsley.dev\n *\n * @constructor\n * @param pluginOptions\n */\nexport const payloadHelper =\n\t(pluginOptions: PayloadHelperPluginConfig) =>\n\t(incomingConfig: Config): Config => {\n\t\tconst genGoLang = env.bool('GEN_GOLANG', false);\n\t\tif (genGoLang) {\n\t\t\tincomingConfig.typescript = {\n\t\t\t\t...incomingConfig.typescript,\n\t\t\t\tschema: schemas,\n\t\t\t};\n\t\t\t// biome-ignore lint/style/noParameterAssign: Need to change field mapper.\n\t\t\tincomingConfig = fieldMapper(incomingConfig);\n\t\t}\n\n\t\t// TODO: Validate Config\n\n\t\t// Update typescript generation file\n\t\tincomingConfig.typescript = incomingConfig.typescript || {};\n\t\tincomingConfig.typescript.outputFile = './src/types/payload.ts';\n\n\t\t// Map collections & add hooks\n\t\tincomingConfig.collections = (incomingConfig.collections || []).map(\n\t\t\t(collection): CollectionConfig => {\n\t\t\t\treturn {\n\t\t\t\t\t...collection,\n\t\t\t\t\thooks: {\n\t\t\t\t\t\tafterChange: [\n\t\t\t\t\t\t\tcacheHookCollections({\n\t\t\t\t\t\t\t\tserver: pluginOptions.webServer,\n\t\t\t\t\t\t\t\tslug: collection.slug,\n\t\t\t\t\t\t\t\tfields: collection.fields,\n\t\t\t\t\t\t\t\tisCollection: true,\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t},\n\t\t);\n\n\t\t// Map globals & add hooks\n\t\tincomingConfig.globals = (incomingConfig.globals || []).map((global) => {\n\t\t\treturn {\n\t\t\t\t...global,\n\t\t\t\thooks: {\n\t\t\t\t\tafterChange: [\n\t\t\t\t\t\tcacheHookGlobals({\n\t\t\t\t\t\t\tserver: pluginOptions.webServer,\n\t\t\t\t\t\t\tslug: global.slug,\n\t\t\t\t\t\t\tfields: global.fields,\n\t\t\t\t\t\t\tisCollection: true,\n\t\t\t\t\t\t}),\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\n\t\treturn incomingConfig;\n\t};\n"],"names":["cacheHookCollections","cacheHookGlobals","fieldMapper","schemas","env","payloadHelper","pluginOptions","incomingConfig","genGoLang","bool","typescript","schema","outputFile","collections","map","collection","hooks","afterChange","server","webServer","slug","fields","isCollection","globals","global"],"mappings":"AACA,SAASA,oBAAoB,EAAEC,gBAAgB,QAAQ,oBAAoB;AAC3E,SAASC,WAAW,EAAEC,OAAO,QAAQ,qBAAqB;AAE1D,OAAOC,SAAS,gBAAgB;AAEhC;;;;;CAKC,GACD,OAAO,MAAMC,gBACZ,CAACC,gBACD,CAACC;QACA,MAAMC,YAAYJ,IAAIK,IAAI,CAAC,cAAc;QACzC,IAAID,WAAW;YACdD,eAAeG,UAAU,GAAG;gBAC3B,GAAGH,eAAeG,UAAU;gBAC5BC,QAAQR;YACT;YACA,0EAA0E;YAC1EI,iBAAiBL,YAAYK;QAC9B;QAEA,wBAAwB;QAExB,oCAAoC;QACpCA,eAAeG,UAAU,GAAGH,eAAeG,UAAU,IAAI,CAAC;QAC1DH,eAAeG,UAAU,CAACE,UAAU,GAAG;QAEvC,8BAA8B;QAC9BL,eAAeM,WAAW,GAAG,AAACN,CAAAA,eAAeM,WAAW,IAAI,EAAE,AAAD,EAAGC,GAAG,CAClE,CAACC;YACA,OAAO;gBACN,GAAGA,UAAU;gBACbC,OAAO;oBACNC,aAAa;wBACZjB,qBAAqB;4BACpBkB,QAAQZ,cAAca,SAAS;4BAC/BC,MAAML,WAAWK,IAAI;4BACrBC,QAAQN,WAAWM,MAAM;4BACzBC,cAAc;wBACf;qBACA;gBACF;YACD;QACD;QAGD,0BAA0B;QAC1Bf,eAAegB,OAAO,GAAG,AAAChB,CAAAA,eAAegB,OAAO,IAAI,EAAE,AAAD,EAAGT,GAAG,CAAC,CAACU;YAC5D,OAAO;gBACN,GAAGA,MAAM;gBACTR,OAAO;oBACNC,aAAa;wBACZhB,iBAAiB;4BAChBiB,QAAQZ,cAAca,SAAS;4BAC/BC,MAAMI,OAAOJ,IAAI;4BACjBC,QAAQG,OAAOH,MAAM;4BACrBC,cAAc;wBACf;qBACA;gBACF;YACD;QACD;QAEA,OAAOf;IACR,EAAE"}
|
package/dist/plugin/hooks.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import type { Field } from 'payload';
|
|
2
2
|
import type { CollectionAfterChangeHook, GlobalAfterChangeHook } from 'payload';
|
|
3
|
+
import type { WebServerConfig } from '../types.js';
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
+
* TODO
|
|
5
6
|
*/
|
|
6
|
-
type
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export
|
|
7
|
+
export type CacheBustConfig = {
|
|
8
|
+
server?: WebServerConfig;
|
|
9
|
+
slug: string;
|
|
10
|
+
fields: Field[];
|
|
11
|
+
isCollection: boolean;
|
|
12
|
+
};
|
|
13
|
+
export declare const cacheHookCollections: (config: CacheBustConfig) => CollectionAfterChangeHook;
|
|
14
|
+
export declare const cacheHookGlobals: (config: CacheBustConfig) => GlobalAfterChangeHook;
|
package/dist/plugin/hooks.js
CHANGED
|
@@ -1,31 +1,39 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Cache hook is responsible for notifying the web server of changes
|
|
3
3
|
* on Collections or Globals as defined by the endpoint.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
*/ const cacheBust = async (config, payload, doc, previousDoc)=>{
|
|
5
|
+
const logger = payload.logger;
|
|
6
|
+
const endpoint = new URL(config?.server?.cacheEndpoint ?? '', config?.server?.baseURL ?? '').href ?? '';
|
|
7
|
+
try {
|
|
8
|
+
const response = await fetch(endpoint, {
|
|
9
|
+
method: 'POST',
|
|
10
|
+
headers: {
|
|
11
|
+
'Content-Type': 'application/json'
|
|
12
|
+
},
|
|
13
|
+
body: JSON.stringify({
|
|
14
|
+
slug: config.slug,
|
|
15
|
+
fields: config.fields,
|
|
16
|
+
type: config.isCollection ? 'collection' : 'global',
|
|
17
|
+
doc: doc,
|
|
18
|
+
prevDoc: previousDoc
|
|
19
|
+
})
|
|
20
|
+
});
|
|
21
|
+
logger.info(`Webhook response status: ${response.status}`);
|
|
22
|
+
} catch (err) {
|
|
23
|
+
logger.error(`Webhook error ${err}`);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
export const cacheHookCollections = (config)=>{
|
|
27
|
+
return async ({ req, doc, previousDoc, operation })=>{
|
|
7
28
|
if (operation !== 'update') {
|
|
8
29
|
return;
|
|
9
30
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
body: JSON.stringify({
|
|
17
|
-
slug: slug,
|
|
18
|
-
fields: fields,
|
|
19
|
-
type: isCollection ? 'collection' : 'global',
|
|
20
|
-
doc: doc,
|
|
21
|
-
prevDoc: previousDoc
|
|
22
|
-
})
|
|
23
|
-
});
|
|
24
|
-
const json = await response.json();
|
|
25
|
-
console.log('Webhook response', json);
|
|
26
|
-
} catch (err) {
|
|
27
|
-
console.error('Webhook error', err);
|
|
28
|
-
}
|
|
31
|
+
await cacheBust(config, req.payload, doc, previousDoc);
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
export const cacheHookGlobals = (config)=>{
|
|
35
|
+
return async ({ req, doc, previousDoc })=>{
|
|
36
|
+
await cacheBust(config, req.payload, doc, previousDoc);
|
|
29
37
|
};
|
|
30
38
|
};
|
|
31
39
|
|
package/dist/plugin/hooks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/plugin/hooks.ts"],"sourcesContent":["import type { Field } from 'payload';\nimport type { CollectionAfterChangeHook, GlobalAfterChangeHook } from 'payload';\n\n/**\n *
|
|
1
|
+
{"version":3,"sources":["../../src/plugin/hooks.ts"],"sourcesContent":["import type { Field, Payload } from 'payload';\nimport type { CollectionAfterChangeHook, GlobalAfterChangeHook } from 'payload';\nimport type { WebServerConfig } from '../types.js';\n\n/**\n * TODO\n */\nexport type CacheBustConfig = {\n\tserver?: WebServerConfig;\n\tslug: string;\n\tfields: Field[];\n\tisCollection: boolean;\n};\n\n/**\n * Cache hook is responsible for notifying the web server of changes\n * on Collections or Globals as defined by the endpoint.\n */\nconst cacheBust = async (\n\tconfig: CacheBustConfig,\n\tpayload: Payload,\n\tdoc: unknown,\n\tpreviousDoc: unknown,\n) => {\n\tconst logger = payload.logger;\n\n\tconst endpoint =\n\t\tnew URL(config?.server?.cacheEndpoint ?? '', config?.server?.baseURL ?? '').href ?? '';\n\n\ttry {\n\t\tconst response = await fetch(endpoint, {\n\t\t\tmethod: 'POST',\n\t\t\theaders: {\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tslug: config.slug,\n\t\t\t\tfields: config.fields,\n\t\t\t\ttype: config.isCollection ? 'collection' : 'global',\n\t\t\t\tdoc: doc,\n\t\t\t\tprevDoc: previousDoc,\n\t\t\t}),\n\t\t});\n\t\tlogger.info(`Webhook response status: ${response.status}`);\n\t} catch (err) {\n\t\tlogger.error(`Webhook error ${err}`);\n\t}\n};\n\nexport const cacheHookCollections = (config: CacheBustConfig): CollectionAfterChangeHook => {\n\treturn async ({ req, doc, previousDoc, operation }) => {\n\t\tif (operation !== 'update') {\n\t\t\treturn;\n\t\t}\n\t\tawait cacheBust(config, req.payload, doc, previousDoc);\n\t};\n};\n\nexport const cacheHookGlobals = (config: CacheBustConfig): GlobalAfterChangeHook => {\n\treturn async ({ req, doc, previousDoc }) => {\n\t\tawait cacheBust(config, req.payload, doc, previousDoc);\n\t};\n};\n"],"names":["cacheBust","config","payload","doc","previousDoc","logger","endpoint","URL","server","cacheEndpoint","baseURL","href","response","fetch","method","headers","body","JSON","stringify","slug","fields","type","isCollection","prevDoc","info","status","err","error","cacheHookCollections","req","operation","cacheHookGlobals"],"mappings":"AAcA;;;CAGC,GACD,MAAMA,YAAY,OACjBC,QACAC,SACAC,KACAC;IAEA,MAAMC,SAASH,QAAQG,MAAM;IAE7B,MAAMC,WACL,IAAIC,IAAIN,QAAQO,QAAQC,iBAAiB,IAAIR,QAAQO,QAAQE,WAAW,IAAIC,IAAI,IAAI;IAErF,IAAI;QACH,MAAMC,WAAW,MAAMC,MAAMP,UAAU;YACtCQ,QAAQ;YACRC,SAAS;gBACR,gBAAgB;YACjB;YACAC,MAAMC,KAAKC,SAAS,CAAC;gBACpBC,MAAMlB,OAAOkB,IAAI;gBACjBC,QAAQnB,OAAOmB,MAAM;gBACrBC,MAAMpB,OAAOqB,YAAY,GAAG,eAAe;gBAC3CnB,KAAKA;gBACLoB,SAASnB;YACV;QACD;QACAC,OAAOmB,IAAI,CAAC,CAAC,yBAAyB,EAAEZ,SAASa,MAAM,CAAC,CAAC;IAC1D,EAAE,OAAOC,KAAK;QACbrB,OAAOsB,KAAK,CAAC,CAAC,cAAc,EAAED,IAAI,CAAC;IACpC;AACD;AAEA,OAAO,MAAME,uBAAuB,CAAC3B;IACpC,OAAO,OAAO,EAAE4B,GAAG,EAAE1B,GAAG,EAAEC,WAAW,EAAE0B,SAAS,EAAE;QACjD,IAAIA,cAAc,UAAU;YAC3B;QACD;QACA,MAAM9B,UAAUC,QAAQ4B,IAAI3B,OAAO,EAAEC,KAAKC;IAC3C;AACD,EAAE;AAEF,OAAO,MAAM2B,mBAAmB,CAAC9B;IAChC,OAAO,OAAO,EAAE4B,GAAG,EAAE1B,GAAG,EAAEC,WAAW,EAAE;QACtC,MAAMJ,UAAUC,QAAQ4B,IAAI3B,OAAO,EAAEC,KAAKC;IAC3C;AACD,EAAE"}
|
package/dist/types.d.ts
CHANGED
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["// import type { SEOPluginConfig } from \"@payloadcms/plugin-seo/types\";\nimport type { GlobalConfig, Tab } from 'payload';\n\nexport type SettingsConfig = {\n\tadditionalTabs?: Tab[];\n\toverride: (args: {\n\t\tconfig: GlobalConfig;\n\t}) => GlobalConfig;\n};\n\nexport type WebServerConfig = {\n\tapiKey?: string;\n\tcacheEndpoint
|
|
1
|
+
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["// import type { SEOPluginConfig } from \"@payloadcms/plugin-seo/types\";\nimport type { GlobalConfig, Tab } from 'payload';\n\nexport type SettingsConfig = {\n\tadditionalTabs?: Tab[];\n\toverride: (args: {\n\t\tconfig: GlobalConfig;\n\t}) => GlobalConfig;\n};\n\nexport type WebServerConfig = {\n\tapiKey?: string;\n\tbaseURL: string;\n\tcacheEndpoint: string;\n};\n\nexport type PayloadHelperPluginConfig = {\n\tsettings?: SettingsConfig;\n\t// seo?: (args: {\n\t// \tconfig: SEOPluginConfig;\n\t// }) => SEOPluginConfig;\n\n\twebServer?: WebServerConfig;\n};\n"],"names":[],"mappings":"AAAA,uEAAuE;AAgBvE,WAOE"}
|