@zenith-open/zenithcms-plugin-algolia 1.0.0-beta.10

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aman T Shekar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,3 @@
1
+ import type { ZenithPlugin } from '@zenith-open/zenithcms-types';
2
+ export declare const algoliaSearchMatrix: ZenithPlugin;
3
+ export default algoliaSearchMatrix;
package/dist/index.js ADDED
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.algoliaSearchMatrix = void 0;
4
+ const algoliasearch_1 = require("algoliasearch");
5
+ exports.algoliaSearchMatrix = {
6
+ id: 'algolia-search-matrix',
7
+ name: 'Algolia Search Matrix',
8
+ version: '1.0.0',
9
+ description: 'Automatically indexes newly published content collections to Algolia indexes for instantaneous keyword discovery.',
10
+ author: 'Zenith Plugin',
11
+ configSchema: {
12
+ appId: {
13
+ type: 'string',
14
+ label: 'Algolia Application ID',
15
+ required: true,
16
+ },
17
+ adminKey: {
18
+ type: 'secret',
19
+ label: 'Algolia Admin API Key',
20
+ description: 'Used to write and delete records in your Algolia indexes.',
21
+ required: true,
22
+ },
23
+ indexPrefix: {
24
+ type: 'string',
25
+ label: 'Index Prefix',
26
+ description: 'Prefix for Algolia indexes (e.g. "prod_")',
27
+ },
28
+ syncAllPublic: {
29
+ type: 'boolean',
30
+ label: 'Sync All Public Collections',
31
+ description: 'Automatically sync all collections that have publicRead enabled',
32
+ default: true,
33
+ },
34
+ },
35
+ apply: (config) => config,
36
+ onInit: (ctx) => {
37
+ const pluginConfig = ctx.config.plugins?.find((p) => p.id === 'algolia-search-matrix' || p.name === 'Algolia Search Matrix')?.config || {};
38
+ const { appId, adminKey, indexPrefix, syncAllPublic } = pluginConfig;
39
+ if (!appId || !adminKey) {
40
+ ctx.logger.warn('Missing App ID or Admin Key. Algolia sync is disabled.');
41
+ return;
42
+ }
43
+ const client = (0, algoliasearch_1.algoliasearch)(appId, adminKey);
44
+ const prefix = indexPrefix || '';
45
+ const syncAll = syncAllPublic !== false;
46
+ const shouldSyncCollection = (collection) => {
47
+ const explicitSync = collection.algoliaSync;
48
+ if (explicitSync === false)
49
+ return false;
50
+ if (explicitSync === true)
51
+ return true;
52
+ return syncAll && collection.publicRead === true;
53
+ };
54
+ ctx.config.collections.forEach((collection) => {
55
+ if (!shouldSyncCollection(collection))
56
+ return;
57
+ const indexName = `${prefix}${collection.slug}`;
58
+ ctx.hooks.on(`collection:${collection.slug}:afterCreate`, async (payload) => {
59
+ try {
60
+ const doc = payload.doc;
61
+ if (doc._status && doc._status !== 'published')
62
+ return payload;
63
+ const objectID = String(doc.id || doc._id);
64
+ await client.saveObject({
65
+ indexName,
66
+ body: {
67
+ ...doc,
68
+ objectID,
69
+ },
70
+ });
71
+ ctx.logger.debug(`Synced document ${objectID} to ${indexName}`);
72
+ }
73
+ catch (error) {
74
+ const message = error instanceof Error ? error.message : String(error);
75
+ ctx.logger.error('Failed to sync document on create', { err: message });
76
+ }
77
+ return payload;
78
+ });
79
+ ctx.hooks.on(`collection:${collection.slug}:afterUpdate`, async (payload) => {
80
+ try {
81
+ const doc = payload.doc;
82
+ const objectID = String(doc.id || doc._id);
83
+ if (doc._status && doc._status !== 'published') {
84
+ await client.deleteObject({ indexName, objectID });
85
+ ctx.logger.debug(`Removed unpublished document ${objectID} from ${indexName}`);
86
+ return payload;
87
+ }
88
+ await client.partialUpdateObject({
89
+ indexName,
90
+ objectID,
91
+ createIfNotExists: true,
92
+ attributesToUpdate: doc,
93
+ });
94
+ ctx.logger.debug(`Updated document ${objectID} in ${indexName}`);
95
+ }
96
+ catch (error) {
97
+ const message = error instanceof Error ? error.message : String(error);
98
+ ctx.logger.error('Failed to sync document on update', { err: message });
99
+ }
100
+ return payload;
101
+ });
102
+ ctx.hooks.on(`collection:${collection.slug}:afterDelete`, async (payload) => {
103
+ try {
104
+ await client.deleteObject({ indexName, objectID: String(payload.id) });
105
+ ctx.logger.debug(`Deleted document ${payload.id} from ${indexName}`);
106
+ }
107
+ catch (error) {
108
+ const message = error instanceof Error ? error.message : String(error);
109
+ ctx.logger.error('Failed to delete document', { err: message });
110
+ }
111
+ return payload;
112
+ });
113
+ });
114
+ ctx.logger.info('Algolia Search Matrix initialized and hooks attached.');
115
+ },
116
+ };
117
+ exports.default = exports.algoliaSearchMatrix;
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@zenith-open/zenithcms-plugin-algolia",
3
+ "version": "1.0.0-beta.10",
4
+ "description": "Algolia search indexing plugin for Zenith CMS",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "dependencies": {
8
+ "algoliasearch": "5.55.1"
9
+ },
10
+ "devDependencies": {
11
+ "typescript": "^5.4.5"
12
+ },
13
+ "peerDependencies": {
14
+ "@zenith-open/zenithcms-types": "1.0.0-beta.10",
15
+ "@zenith-open/zenithcms-core": "1.0.0-beta.10"
16
+ },
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "README.md"
23
+ ],
24
+ "scripts": {
25
+ "build": "tsc"
26
+ }
27
+ }