@ainsleydev/payload-helper 0.0.12 → 0.0.13
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 +23 -0
- package/dist/index.js.map +1 -1
- package/dist/plugin/hooks.d.ts +12 -0
- package/dist/plugin/hooks.js +32 -0
- package/dist/plugin/hooks.js.map +1 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cacheHook } from './plugin/hooks.js';
|
|
1
2
|
import { fieldMapper, schemas } from './plugin/schema.js';
|
|
2
3
|
import env from './util/env.js';
|
|
3
4
|
/**
|
|
@@ -17,6 +18,28 @@ import env from './util/env.js';
|
|
|
17
18
|
}
|
|
18
19
|
incomingConfig.typescript = incomingConfig.typescript || {};
|
|
19
20
|
incomingConfig.typescript.outputFile = './src/types/payload.ts';
|
|
21
|
+
// Map collections & add hooks
|
|
22
|
+
(incomingConfig.collections || []).map((collection)=>{
|
|
23
|
+
return {
|
|
24
|
+
...collection,
|
|
25
|
+
hooks: {
|
|
26
|
+
afterChange: [
|
|
27
|
+
cacheHook(pluginOptions?.webServer?.cacheEndpoint ?? '/cache/', collection.slug, collection.fields, true)
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
// Map globals & add hooks
|
|
33
|
+
(incomingConfig.globals || []).map((global)=>{
|
|
34
|
+
return {
|
|
35
|
+
...global,
|
|
36
|
+
hooks: {
|
|
37
|
+
afterChange: [
|
|
38
|
+
cacheHook(pluginOptions?.webServer?.cacheEndpoint ?? '/cache/', global.slug, global.fields, false)
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
});
|
|
20
43
|
return incomingConfig;
|
|
21
44
|
};
|
|
22
45
|
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Config } from 'payload';\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\tincomingConfig.typescript = incomingConfig.typescript || {};\n\t\tincomingConfig.typescript.outputFile = './src/types/payload.ts';\n\n\t\treturn incomingConfig;\n\t};\n"],"names":["fieldMapper","schemas","env","payloadHelper","pluginOptions","incomingConfig","genGoLang","bool","typescript","schema","outputFile"],"mappings":"AACA,SAASA,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;QAEAA,eAAeG,UAAU,GAAGH,eAAeG,UAAU,IAAI,CAAC;QAC1DH,eAAeG,UAAU,CAACE,UAAU,GAAG;QAEvC,
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Config } from 'payload';\nimport { cacheHook } 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\tincomingConfig.typescript = incomingConfig.typescript || {};\n\t\tincomingConfig.typescript.outputFile = './src/types/payload.ts';\n\n\t\t// Map collections & add hooks\n\t\t(incomingConfig.collections || []).map((collection) => {\n\t\t\treturn {\n\t\t\t\t...collection,\n\t\t\t\thooks: {\n\t\t\t\t\tafterChange: [\n\t\t\t\t\t\tcacheHook(\n\t\t\t\t\t\t\tpluginOptions?.webServer?.cacheEndpoint ?? '/cache/',\n\t\t\t\t\t\t\tcollection.slug,\n\t\t\t\t\t\t\tcollection.fields,\n\t\t\t\t\t\t\ttrue,\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\t(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\tcacheHook(\n\t\t\t\t\t\t\tpluginOptions?.webServer?.cacheEndpoint ?? '/cache/',\n\t\t\t\t\t\t\tglobal.slug,\n\t\t\t\t\t\t\tglobal.fields,\n\t\t\t\t\t\t\tfalse,\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":["cacheHook","fieldMapper","schemas","env","payloadHelper","pluginOptions","incomingConfig","genGoLang","bool","typescript","schema","outputFile","collections","map","collection","hooks","afterChange","webServer","cacheEndpoint","slug","fields","globals","global"],"mappings":"AACA,SAASA,SAAS,QAAQ,oBAAoB;AAC9C,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;QAEAA,eAAeG,UAAU,GAAGH,eAAeG,UAAU,IAAI,CAAC;QAC1DH,eAAeG,UAAU,CAACE,UAAU,GAAG;QAEvC,8BAA8B;QAC7BL,CAAAA,eAAeM,WAAW,IAAI,EAAE,AAAD,EAAGC,GAAG,CAAC,CAACC;YACvC,OAAO;gBACN,GAAGA,UAAU;gBACbC,OAAO;oBACNC,aAAa;wBACZhB,UACCK,eAAeY,WAAWC,iBAAiB,WAC3CJ,WAAWK,IAAI,EACfL,WAAWM,MAAM,EACjB;qBAED;gBACF;YACD;QACD;QAEA,0BAA0B;QACzBd,CAAAA,eAAee,OAAO,IAAI,EAAE,AAAD,EAAGR,GAAG,CAAC,CAACS;YACnC,OAAO;gBACN,GAAGA,MAAM;gBACTP,OAAO;oBACNC,aAAa;wBACZhB,UACCK,eAAeY,WAAWC,iBAAiB,WAC3CI,OAAOH,IAAI,EACXG,OAAOF,MAAM,EACb;qBAED;gBACF;YACD;QACD;QAEA,OAAOd;IACR,EAAE"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Field } from 'payload';
|
|
2
|
+
import type { CollectionAfterChangeHook, GlobalAfterChangeHook } from 'payload';
|
|
3
|
+
/**
|
|
4
|
+
* AfterChangeHook is a hook for either the Collection or Global.
|
|
5
|
+
*/
|
|
6
|
+
type AfterChangeHook = CollectionAfterChangeHook | GlobalAfterChangeHook;
|
|
7
|
+
/**
|
|
8
|
+
* Cache hook is responsible for notifying the web server of changes
|
|
9
|
+
* on Collections or Globals as defined by the endpoint.
|
|
10
|
+
*/
|
|
11
|
+
export declare const cacheHook: (endpoint: string, slug: string, fields: Field[], isCollection: boolean) => AfterChangeHook;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache hook is responsible for notifying the web server of changes
|
|
3
|
+
* on Collections or Globals as defined by the endpoint.
|
|
4
|
+
*/ export const cacheHook = (endpoint, slug, fields, isCollection)=>{
|
|
5
|
+
//@ts-expect-error
|
|
6
|
+
return async ({ doc, previousDoc, operation })=>{
|
|
7
|
+
if (operation !== 'update') {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
try {
|
|
11
|
+
const response = await fetch(endpoint, {
|
|
12
|
+
method: 'POST',
|
|
13
|
+
headers: {
|
|
14
|
+
'Content-Type': 'application/json'
|
|
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
|
+
}
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
//# sourceMappingURL=hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/plugin/hooks.ts"],"sourcesContent":["import type { Field } from 'payload';\nimport type { CollectionAfterChangeHook, GlobalAfterChangeHook } from 'payload';\n\n/**\n * AfterChangeHook is a hook for either the Collection or Global.\n */\ntype AfterChangeHook = CollectionAfterChangeHook | GlobalAfterChangeHook;\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 */\nexport const cacheHook = (\n\tendpoint: string,\n\tslug: string,\n\tfields: Field[],\n\tisCollection: boolean,\n): AfterChangeHook => {\n\t//@ts-expect-error\n\treturn async ({ doc, previousDoc, operation }) => {\n\t\tif (operation !== 'update') {\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tconst response = await fetch(endpoint, {\n\t\t\t\tmethod: 'POST',\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tslug: slug,\n\t\t\t\t\tfields: fields,\n\t\t\t\t\ttype: isCollection ? 'collection' : 'global',\n\t\t\t\t\tdoc: doc,\n\t\t\t\t\tprevDoc: previousDoc,\n\t\t\t\t}),\n\t\t\t});\n\t\t\tconst json = await response.json();\n\t\t\tconsole.log('Webhook response', json);\n\t\t} catch (err) {\n\t\t\tconsole.error('Webhook error', err);\n\t\t}\n\t};\n};\n"],"names":["cacheHook","endpoint","slug","fields","isCollection","doc","previousDoc","operation","response","fetch","method","headers","body","JSON","stringify","type","prevDoc","json","console","log","err","error"],"mappings":"AAQA;;;CAGC,GACD,OAAO,MAAMA,YAAY,CACxBC,UACAC,MACAC,QACAC;IAEA,kBAAkB;IAClB,OAAO,OAAO,EAAEC,GAAG,EAAEC,WAAW,EAAEC,SAAS,EAAE;QAC5C,IAAIA,cAAc,UAAU;YAC3B;QACD;QACA,IAAI;YACH,MAAMC,WAAW,MAAMC,MAAMR,UAAU;gBACtCS,QAAQ;gBACRC,SAAS;oBACR,gBAAgB;gBACjB;gBACAC,MAAMC,KAAKC,SAAS,CAAC;oBACpBZ,MAAMA;oBACNC,QAAQA;oBACRY,MAAMX,eAAe,eAAe;oBACpCC,KAAKA;oBACLW,SAASV;gBACV;YACD;YACA,MAAMW,OAAO,MAAMT,SAASS,IAAI;YAChCC,QAAQC,GAAG,CAAC,oBAAoBF;QACjC,EAAE,OAAOG,KAAK;YACbF,QAAQG,KAAK,CAAC,iBAAiBD;QAChC;IACD;AACD,EAAE"}
|
package/dist/types.d.ts
CHANGED
|
@@ -5,6 +5,11 @@ export type SettingsConfig = {
|
|
|
5
5
|
config: GlobalConfig;
|
|
6
6
|
}) => GlobalConfig;
|
|
7
7
|
};
|
|
8
|
+
export type WebServerConfig = {
|
|
9
|
+
apiKey?: string;
|
|
10
|
+
cacheEndpoint?: string;
|
|
11
|
+
};
|
|
8
12
|
export type PayloadHelperPluginConfig = {
|
|
9
13
|
settings?: SettingsConfig;
|
|
14
|
+
webServer?: WebServerConfig;
|
|
10
15
|
};
|
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};\nexport type PayloadHelperPluginConfig = {\n\tsettings?: SettingsConfig;\n\t// seo?: (args: {\n\t// \tconfig: SEOPluginConfig;\n\t// }) => SEOPluginConfig;\n};\n"],"names":[],"mappings":"AAAA,uEAAuE;
|
|
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?: 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;AAevE,WAOE"}
|