@foundrykit/search-plugin 0.2.4 → 0.3.0
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 +4 -0
- package/dist/index.js +10 -2
- package/dist/index.js.map +1 -1
- package/dist/options.d.ts +5 -0
- package/dist/options.js +4 -0
- package/dist/options.js.map +1 -1
- package/dist/types.d.ts +7 -0
- package/dist/types.js.map +1 -1
- package/package.json +9 -8
package/README.md
CHANGED
|
@@ -97,6 +97,7 @@ you set `requireAdminForOperations: false`.
|
|
|
97
97
|
| `fuzzy` | `{ threshold: 0.35, distance: 100, minMatchCharLength: 3 }` | Fuse tuning. |
|
|
98
98
|
| `decodeEntities` | `true` | Decode HTML entities in titles. |
|
|
99
99
|
| `analytics` | `false` | `true`, or `{ retentionDays, collectionSlug }`. |
|
|
100
|
+
| `rateLimit` | `{ limit: 30, window: 60 }` | Per-IP limit on the public track endpoint. `false` disables it. |
|
|
100
101
|
| `requireAdminForOperations` | `true` | Guard reindex and inspect. |
|
|
101
102
|
| `routePrefix` | `/site-search` | Base path for endpoints. |
|
|
102
103
|
| `disabled` | `false` | Bypass the plugin entirely. |
|
|
@@ -143,6 +144,9 @@ relationship is stored.
|
|
|
143
144
|
|
|
144
145
|
- **Recording.** The browser reports searches to `POST /api/site-search/track`. The `GET` search
|
|
145
146
|
endpoint no longer records anything.
|
|
147
|
+
- **Rate limit.** The track endpoint is public and writes a row per call, so it is limited per
|
|
148
|
+
client IP (default 30 a minute, via `@foundrykit/rate-limit` and Payload's KV store). Set
|
|
149
|
+
`rateLimit.trustedProxies` (e.g. `true` on Vercel) or every visitor shares one bucket.
|
|
146
150
|
- **`visitorCookie`** (opt-in). Name a cookie and the plugin stores its value on the event, so you
|
|
147
151
|
can tell repeat searchers apart. The plugin only _reads_ it; your site sets it, and should do so
|
|
148
152
|
only with consent.
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { withRateLimit } from '@foundrykit/rate-limit';
|
|
1
2
|
import { recordActivePlugin } from './activePlugins.js';
|
|
2
3
|
import { searchIndexCache } from './cache.js';
|
|
3
4
|
import { createSearchEventsCollection } from './collections/SearchEvents.js';
|
|
4
5
|
import { createInspectHandler, createReindexHandler, createSearchHandler, createTrackHandler } from './endpoints/handlers.js';
|
|
5
|
-
import { assertConfig, resolveAnalytics } from './options.js';
|
|
6
|
+
import { assertConfig, DEFAULT_TRACK_RATE_LIMIT, resolveAnalytics } from './options.js';
|
|
6
7
|
export { aggregateEvents, cleanSearchTerm } from './analytics.js';
|
|
7
8
|
export { SearchIndexCache, searchIndexCache } from './cache.js';
|
|
8
9
|
export { decodeEntities } from './decodeEntities.js';
|
|
@@ -67,8 +68,15 @@ export { deviceOf, isBot, parseTrackPayload, recordSearchEvent } from './track.j
|
|
|
67
68
|
},
|
|
68
69
|
...analytics.enabled ? [
|
|
69
70
|
{
|
|
70
|
-
|
|
71
|
+
// Public and writes a row per call, so it is always limited unless the host opts out.
|
|
72
|
+
handler: pluginOptions.rateLimit === false ? createTrackHandler({
|
|
71
73
|
config: pluginOptions
|
|
74
|
+
}) : withRateLimit(createTrackHandler({
|
|
75
|
+
config: pluginOptions
|
|
76
|
+
}), {
|
|
77
|
+
key: (_req, ip)=>`search-track:${ip}`,
|
|
78
|
+
...DEFAULT_TRACK_RATE_LIMIT,
|
|
79
|
+
...pluginOptions.rateLimit
|
|
72
80
|
}),
|
|
73
81
|
method: 'post',
|
|
74
82
|
path: `${prefix}/track`
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Config } from 'payload';\nimport { recordActivePlugin } from './activePlugins.js';\nimport { searchIndexCache } from './cache.js';\nimport { createSearchEventsCollection } from './collections/SearchEvents.js';\nimport {\n createInspectHandler,\n createReindexHandler,\n createSearchHandler,\n createTrackHandler,\n} from './endpoints/handlers.js';\nimport {
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { withRateLimit } from '@foundrykit/rate-limit';\nimport type { Config } from 'payload';\nimport { recordActivePlugin } from './activePlugins.js';\nimport { searchIndexCache } from './cache.js';\nimport { createSearchEventsCollection } from './collections/SearchEvents.js';\nimport {\n createInspectHandler,\n createReindexHandler,\n createSearchHandler,\n createTrackHandler,\n} from './endpoints/handlers.js';\nimport {\n assertConfig,\n DEFAULT_TRACK_RATE_LIMIT,\n resolveAnalytics,\n} from './options.js';\nimport type { SearchPluginConfig } from './types.js';\n\nexport { aggregateEvents, cleanSearchTerm } from './analytics.js';\nexport { SearchIndexCache, searchIndexCache } from './cache.js';\nexport { decodeEntities } from './decodeEntities.js';\nexport { hrefFor, projectDocs } from './project.js';\nexport {\n DEFAULT_FIELDS,\n DEFAULT_FUZZY,\n groupResults,\n presetResults,\n rankDocs,\n} from './rank.js';\nexport { createSearchService } from './resolve.js';\nexport type { ShownResult, TrackPayload } from './track.js';\nexport {\n deviceOf,\n isBot,\n parseTrackPayload,\n recordSearchEvent,\n} from './track.js';\nexport type * from './types.js';\n\n/**\n * Cached, grouped and ranked site search over the `@payloadcms/plugin-search` index.\n *\n * Layered on top of plugin-search rather than replacing it: that plugin already keeps a `search`\n * collection in sync through afterChange/afterDelete hooks, and sync is the part of an index\n * implementation that rots. This owns the read side — projection, caching, fuzzy ranking,\n * grouping — plus the operational surface an editor needs.\n *\n * Install it AFTER searchPlugin in the plugins array, since it reads the collection that one\n * creates.\n */\nexport const searchPlugin =\n (pluginOptions: SearchPluginConfig) =>\n (config: Config): Config => {\n if (pluginOptions.disabled) {\n return config;\n }\n\n assertConfig(pluginOptions);\n\n const analytics = resolveAnalytics(pluginOptions.analytics);\n const prefix = pluginOptions.routePrefix ?? '/site-search';\n const requireAdmin = pluginOptions.requireAdminForOperations ?? true;\n\n searchIndexCache.configure(pluginOptions.cacheTTL ?? 60_000);\n\n if (analytics.enabled) {\n config.collections = [\n ...(config.collections || []),\n createSearchEventsCollection({ slug: analytics.collectionSlug }),\n ];\n }\n\n config.endpoints = [\n ...(config.endpoints || []),\n {\n handler: createSearchHandler({\n cache: searchIndexCache,\n config: pluginOptions,\n }),\n method: 'get',\n path: prefix,\n },\n {\n handler: createReindexHandler({\n cache: searchIndexCache,\n config: pluginOptions,\n requireAdmin,\n }),\n method: 'post',\n path: `${prefix}/reindex`,\n },\n {\n handler: createInspectHandler({\n cache: searchIndexCache,\n config: pluginOptions,\n requireAdmin,\n }),\n method: 'get',\n path: `${prefix}/inspect`,\n },\n ...(analytics.enabled\n ? [\n {\n // Public and writes a row per call, so it is always limited unless the host opts out.\n handler:\n pluginOptions.rateLimit === false\n ? createTrackHandler({ config: pluginOptions })\n : withRateLimit(\n createTrackHandler({ config: pluginOptions }),\n {\n key: (_req, ip) => `search-track:${ip}`,\n ...DEFAULT_TRACK_RATE_LIMIT,\n ...pluginOptions.rateLimit,\n }\n ),\n method: 'post' as const,\n path: `${prefix}/track`,\n },\n ]\n : []),\n ];\n\n return recordActivePlugin(config, '@foundrykit/search-plugin');\n };\n"],"names":["withRateLimit","recordActivePlugin","searchIndexCache","createSearchEventsCollection","createInspectHandler","createReindexHandler","createSearchHandler","createTrackHandler","assertConfig","DEFAULT_TRACK_RATE_LIMIT","resolveAnalytics","aggregateEvents","cleanSearchTerm","SearchIndexCache","decodeEntities","hrefFor","projectDocs","DEFAULT_FIELDS","DEFAULT_FUZZY","groupResults","presetResults","rankDocs","createSearchService","deviceOf","isBot","parseTrackPayload","recordSearchEvent","searchPlugin","pluginOptions","config","disabled","analytics","prefix","routePrefix","requireAdmin","requireAdminForOperations","configure","cacheTTL","enabled","collections","slug","collectionSlug","endpoints","handler","cache","method","path","rateLimit","key","_req","ip"],"mappings":"AAAA,SAASA,aAAa,QAAQ,yBAAyB;AAEvD,SAASC,kBAAkB,QAAQ,qBAAqB;AACxD,SAASC,gBAAgB,QAAQ,aAAa;AAC9C,SAASC,4BAA4B,QAAQ,gCAAgC;AAC7E,SACEC,oBAAoB,EACpBC,oBAAoB,EACpBC,mBAAmB,EACnBC,kBAAkB,QACb,0BAA0B;AACjC,SACEC,YAAY,EACZC,wBAAwB,EACxBC,gBAAgB,QACX,eAAe;AAGtB,SAASC,eAAe,EAAEC,eAAe,QAAQ,iBAAiB;AAClE,SAASC,gBAAgB,EAAEX,gBAAgB,QAAQ,aAAa;AAChE,SAASY,cAAc,QAAQ,sBAAsB;AACrD,SAASC,OAAO,EAAEC,WAAW,QAAQ,eAAe;AACpD,SACEC,cAAc,EACdC,aAAa,EACbC,YAAY,EACZC,aAAa,EACbC,QAAQ,QACH,YAAY;AACnB,SAASC,mBAAmB,QAAQ,eAAe;AAEnD,SACEC,QAAQ,EACRC,KAAK,EACLC,iBAAiB,EACjBC,iBAAiB,QACZ,aAAa;AAGpB;;;;;;;;;;CAUC,GACD,OAAO,MAAMC,eACX,CAACC,gBACD,CAACC;QACC,IAAID,cAAcE,QAAQ,EAAE;YAC1B,OAAOD;QACT;QAEArB,aAAaoB;QAEb,MAAMG,YAAYrB,iBAAiBkB,cAAcG,SAAS;QAC1D,MAAMC,SAASJ,cAAcK,WAAW,IAAI;QAC5C,MAAMC,eAAeN,cAAcO,yBAAyB,IAAI;QAEhEjC,iBAAiBkC,SAAS,CAACR,cAAcS,QAAQ,IAAI;QAErD,IAAIN,UAAUO,OAAO,EAAE;YACrBT,OAAOU,WAAW,GAAG;mBACfV,OAAOU,WAAW,IAAI,EAAE;gBAC5BpC,6BAA6B;oBAAEqC,MAAMT,UAAUU,cAAc;gBAAC;aAC/D;QACH;QAEAZ,OAAOa,SAAS,GAAG;eACbb,OAAOa,SAAS,IAAI,EAAE;YAC1B;gBACEC,SAASrC,oBAAoB;oBAC3BsC,OAAO1C;oBACP2B,QAAQD;gBACV;gBACAiB,QAAQ;gBACRC,MAAMd;YACR;YACA;gBACEW,SAAStC,qBAAqB;oBAC5BuC,OAAO1C;oBACP2B,QAAQD;oBACRM;gBACF;gBACAW,QAAQ;gBACRC,MAAM,GAAGd,OAAO,QAAQ,CAAC;YAC3B;YACA;gBACEW,SAASvC,qBAAqB;oBAC5BwC,OAAO1C;oBACP2B,QAAQD;oBACRM;gBACF;gBACAW,QAAQ;gBACRC,MAAM,GAAGd,OAAO,QAAQ,CAAC;YAC3B;eACID,UAAUO,OAAO,GACjB;gBACE;oBACE,sFAAsF;oBACtFK,SACEf,cAAcmB,SAAS,KAAK,QACxBxC,mBAAmB;wBAAEsB,QAAQD;oBAAc,KAC3C5B,cACEO,mBAAmB;wBAAEsB,QAAQD;oBAAc,IAC3C;wBACEoB,KAAK,CAACC,MAAMC,KAAO,CAAC,aAAa,EAAEA,IAAI;wBACvC,GAAGzC,wBAAwB;wBAC3B,GAAGmB,cAAcmB,SAAS;oBAC5B;oBAERF,QAAQ;oBACRC,MAAM,GAAGd,OAAO,MAAM,CAAC;gBACzB;aACD,GACD,EAAE;SACP;QAED,OAAO/B,mBAAmB4B,QAAQ;IACpC,EAAE"}
|
package/dist/options.d.ts
CHANGED
|
@@ -9,3 +9,8 @@ export declare const resolveAnalytics: (analytics: SearchPluginConfig["analytics
|
|
|
9
9
|
* — results vanishing — is indistinguishable from an empty index.
|
|
10
10
|
*/
|
|
11
11
|
export declare function assertConfig(config: SearchPluginConfig): void;
|
|
12
|
+
/** Track calls allowed per client IP per minute. A settled search sends one, plus one per click. */
|
|
13
|
+
export declare const DEFAULT_TRACK_RATE_LIMIT: {
|
|
14
|
+
limit: number;
|
|
15
|
+
window: number;
|
|
16
|
+
};
|
package/dist/options.js
CHANGED
|
@@ -41,5 +41,9 @@ export const DEFAULT_ANALYTICS = {
|
|
|
41
41
|
throw new Error(`search-plugin: duplicate group "${duplicateGroup.slug}".`);
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
|
+
/** Track calls allowed per client IP per minute. A settled search sends one, plus one per click. */ export const DEFAULT_TRACK_RATE_LIMIT = {
|
|
45
|
+
limit: 30,
|
|
46
|
+
window: 60
|
|
47
|
+
};
|
|
44
48
|
|
|
45
49
|
//# sourceMappingURL=options.js.map
|
package/dist/options.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/options.ts"],"sourcesContent":["import type { ResolvedAnalytics, SearchPluginConfig } from './types.js';\n\nexport const DEFAULT_ANALYTICS: ResolvedAnalytics = {\n collectionSlug: 'search-events',\n enabled: false,\n isEnabled: () => true,\n // Long enough to compare a full year, short enough that the log stays bounded and the\n // retention question is answered up front rather than by accumulation.\n retentionDays: 400,\n};\n\n/** Normalise the `analytics` option, which accepts a boolean shorthand. */\nexport const resolveAnalytics = (\n analytics: SearchPluginConfig['analytics']\n): ResolvedAnalytics => {\n if (analytics === undefined) {\n return DEFAULT_ANALYTICS;\n }\n if (typeof analytics === 'boolean') {\n return { ...DEFAULT_ANALYTICS, enabled: analytics };\n }\n return {\n collectionSlug:\n analytics.collectionSlug ?? DEFAULT_ANALYTICS.collectionSlug,\n enabled: analytics.enabled ?? true,\n isEnabled: analytics.isEnabled ?? DEFAULT_ANALYTICS.isEnabled,\n retentionDays: analytics.retentionDays ?? DEFAULT_ANALYTICS.retentionDays,\n visitorCookie: analytics.visitorCookie,\n };\n};\n\n/**\n * Fail loudly on a misconfiguration rather than silently returning nothing.\n *\n * A collection pointing at a group that does not exist is the most likely mistake, and its symptom\n * — results vanishing — is indistinguishable from an empty index.\n */\nexport function assertConfig(config: SearchPluginConfig): void {\n const groups = new Set(config.groups.map(group => group.slug));\n\n for (const entry of config.collections) {\n if (!groups.has(entry.group)) {\n throw new Error(\n `search-plugin: collection \"${String(entry.slug)}\" is assigned to group \"${entry.group}\", which is not configured.`\n );\n }\n }\n\n const duplicateGroup = config.groups.find(\n (group, index) =>\n config.groups.findIndex(other => other.slug === group.slug) !== index\n );\n if (duplicateGroup) {\n throw new Error(`search-plugin: duplicate group \"${duplicateGroup.slug}\".`);\n }\n}\n"],"names":["DEFAULT_ANALYTICS","collectionSlug","enabled","isEnabled","retentionDays","resolveAnalytics","analytics","undefined","visitorCookie","assertConfig","config","groups","Set","map","group","slug","entry","collections","has","Error","String","duplicateGroup","find","index","findIndex","other"],"mappings":"AAEA,OAAO,MAAMA,oBAAuC;IAClDC,gBAAgB;IAChBC,SAAS;IACTC,WAAW,IAAM;IACjB,sFAAsF;IACtF,uEAAuE;IACvEC,eAAe;AACjB,EAAE;AAEF,yEAAyE,GACzE,OAAO,MAAMC,mBAAmB,CAC9BC;IAEA,IAAIA,cAAcC,WAAW;QAC3B,OAAOP;IACT;IACA,IAAI,OAAOM,cAAc,WAAW;QAClC,OAAO;YAAE,GAAGN,iBAAiB;YAAEE,SAASI;QAAU;IACpD;IACA,OAAO;QACLL,gBACEK,UAAUL,cAAc,IAAID,kBAAkBC,cAAc;QAC9DC,SAASI,UAAUJ,OAAO,IAAI;QAC9BC,WAAWG,UAAUH,SAAS,IAAIH,kBAAkBG,SAAS;QAC7DC,eAAeE,UAAUF,aAAa,IAAIJ,kBAAkBI,aAAa;QACzEI,eAAeF,UAAUE,aAAa;IACxC;AACF,EAAE;AAEF;;;;;CAKC,GACD,OAAO,SAASC,aAAaC,MAA0B;IACrD,MAAMC,SAAS,IAAIC,IAAIF,OAAOC,MAAM,CAACE,GAAG,CAACC,CAAAA,QAASA,MAAMC,IAAI;IAE5D,KAAK,MAAMC,SAASN,OAAOO,WAAW,CAAE;QACtC,IAAI,CAACN,OAAOO,GAAG,CAACF,MAAMF,KAAK,GAAG;YAC5B,MAAM,IAAIK,MACR,CAAC,2BAA2B,EAAEC,OAAOJ,MAAMD,IAAI,EAAE,wBAAwB,EAAEC,MAAMF,KAAK,CAAC,2BAA2B,CAAC;QAEvH;IACF;IAEA,MAAMO,iBAAiBX,OAAOC,MAAM,CAACW,IAAI,CACvC,CAACR,OAAOS,QACNb,OAAOC,MAAM,CAACa,SAAS,CAACC,CAAAA,QAASA,MAAMV,IAAI,KAAKD,MAAMC,IAAI,MAAMQ;IAEpE,IAAIF,gBAAgB;QAClB,MAAM,IAAIF,MAAM,CAAC,gCAAgC,EAAEE,eAAeN,IAAI,CAAC,EAAE,CAAC;IAC5E;AACF"}
|
|
1
|
+
{"version":3,"sources":["../src/options.ts"],"sourcesContent":["import type { ResolvedAnalytics, SearchPluginConfig } from './types.js';\n\nexport const DEFAULT_ANALYTICS: ResolvedAnalytics = {\n collectionSlug: 'search-events',\n enabled: false,\n isEnabled: () => true,\n // Long enough to compare a full year, short enough that the log stays bounded and the\n // retention question is answered up front rather than by accumulation.\n retentionDays: 400,\n};\n\n/** Normalise the `analytics` option, which accepts a boolean shorthand. */\nexport const resolveAnalytics = (\n analytics: SearchPluginConfig['analytics']\n): ResolvedAnalytics => {\n if (analytics === undefined) {\n return DEFAULT_ANALYTICS;\n }\n if (typeof analytics === 'boolean') {\n return { ...DEFAULT_ANALYTICS, enabled: analytics };\n }\n return {\n collectionSlug:\n analytics.collectionSlug ?? DEFAULT_ANALYTICS.collectionSlug,\n enabled: analytics.enabled ?? true,\n isEnabled: analytics.isEnabled ?? DEFAULT_ANALYTICS.isEnabled,\n retentionDays: analytics.retentionDays ?? DEFAULT_ANALYTICS.retentionDays,\n visitorCookie: analytics.visitorCookie,\n };\n};\n\n/**\n * Fail loudly on a misconfiguration rather than silently returning nothing.\n *\n * A collection pointing at a group that does not exist is the most likely mistake, and its symptom\n * — results vanishing — is indistinguishable from an empty index.\n */\nexport function assertConfig(config: SearchPluginConfig): void {\n const groups = new Set(config.groups.map(group => group.slug));\n\n for (const entry of config.collections) {\n if (!groups.has(entry.group)) {\n throw new Error(\n `search-plugin: collection \"${String(entry.slug)}\" is assigned to group \"${entry.group}\", which is not configured.`\n );\n }\n }\n\n const duplicateGroup = config.groups.find(\n (group, index) =>\n config.groups.findIndex(other => other.slug === group.slug) !== index\n );\n if (duplicateGroup) {\n throw new Error(`search-plugin: duplicate group \"${duplicateGroup.slug}\".`);\n }\n}\n\n/** Track calls allowed per client IP per minute. A settled search sends one, plus one per click. */\nexport const DEFAULT_TRACK_RATE_LIMIT = { limit: 30, window: 60 };\n"],"names":["DEFAULT_ANALYTICS","collectionSlug","enabled","isEnabled","retentionDays","resolveAnalytics","analytics","undefined","visitorCookie","assertConfig","config","groups","Set","map","group","slug","entry","collections","has","Error","String","duplicateGroup","find","index","findIndex","other","DEFAULT_TRACK_RATE_LIMIT","limit","window"],"mappings":"AAEA,OAAO,MAAMA,oBAAuC;IAClDC,gBAAgB;IAChBC,SAAS;IACTC,WAAW,IAAM;IACjB,sFAAsF;IACtF,uEAAuE;IACvEC,eAAe;AACjB,EAAE;AAEF,yEAAyE,GACzE,OAAO,MAAMC,mBAAmB,CAC9BC;IAEA,IAAIA,cAAcC,WAAW;QAC3B,OAAOP;IACT;IACA,IAAI,OAAOM,cAAc,WAAW;QAClC,OAAO;YAAE,GAAGN,iBAAiB;YAAEE,SAASI;QAAU;IACpD;IACA,OAAO;QACLL,gBACEK,UAAUL,cAAc,IAAID,kBAAkBC,cAAc;QAC9DC,SAASI,UAAUJ,OAAO,IAAI;QAC9BC,WAAWG,UAAUH,SAAS,IAAIH,kBAAkBG,SAAS;QAC7DC,eAAeE,UAAUF,aAAa,IAAIJ,kBAAkBI,aAAa;QACzEI,eAAeF,UAAUE,aAAa;IACxC;AACF,EAAE;AAEF;;;;;CAKC,GACD,OAAO,SAASC,aAAaC,MAA0B;IACrD,MAAMC,SAAS,IAAIC,IAAIF,OAAOC,MAAM,CAACE,GAAG,CAACC,CAAAA,QAASA,MAAMC,IAAI;IAE5D,KAAK,MAAMC,SAASN,OAAOO,WAAW,CAAE;QACtC,IAAI,CAACN,OAAOO,GAAG,CAACF,MAAMF,KAAK,GAAG;YAC5B,MAAM,IAAIK,MACR,CAAC,2BAA2B,EAAEC,OAAOJ,MAAMD,IAAI,EAAE,wBAAwB,EAAEC,MAAMF,KAAK,CAAC,2BAA2B,CAAC;QAEvH;IACF;IAEA,MAAMO,iBAAiBX,OAAOC,MAAM,CAACW,IAAI,CACvC,CAACR,OAAOS,QACNb,OAAOC,MAAM,CAACa,SAAS,CAACC,CAAAA,QAASA,MAAMV,IAAI,KAAKD,MAAMC,IAAI,MAAMQ;IAEpE,IAAIF,gBAAgB;QAClB,MAAM,IAAIF,MAAM,CAAC,gCAAgC,EAAEE,eAAeN,IAAI,CAAC,EAAE,CAAC;IAC5E;AACF;AAEA,kGAAkG,GAClG,OAAO,MAAMW,2BAA2B;IAAEC,OAAO;IAAIC,QAAQ;AAAG,EAAE"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { RateLimitOptions } from '@foundrykit/rate-limit';
|
|
1
2
|
import type { CollectionSlug, PayloadRequest } from 'payload';
|
|
2
3
|
/**
|
|
3
4
|
* A collection that should appear in site search, and how its results present.
|
|
@@ -88,6 +89,12 @@ export type SearchPluginConfig = {
|
|
|
88
89
|
fields?: SearchField[];
|
|
89
90
|
fuzzy?: FuzzyOptions;
|
|
90
91
|
groups: SearchGroup[];
|
|
92
|
+
/**
|
|
93
|
+
* Rate-limit the public track endpoint per client IP, using Payload's KV store. Default
|
|
94
|
+
* `{ limit: 30, window: 60 }`; `false` turns it off. Set `trustedProxies` (see
|
|
95
|
+
* `@foundrykit/rate-limit`) or every visitor shares one bucket.
|
|
96
|
+
*/
|
|
97
|
+
rateLimit?: false | Partial<Pick<RateLimitOptions, 'limit' | 'window' | 'trustedProxies'>>;
|
|
91
98
|
/** Restrict the reindex and inspect endpoints to admin users. */
|
|
92
99
|
requireAdminForOperations?: boolean;
|
|
93
100
|
/** Base path for the plugin's endpoints. */
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["import type { CollectionSlug, PayloadRequest } from 'payload';\n\n/**\n * A collection that should appear in site search, and how its results present.\n *\n * Everything a host project would otherwise hardcode lives here: which group a collection lands\n * in, how its URLs are built, how strongly it ranks, and which documents are held back.\n */\nexport type SearchableCollection = {\n /** Documents whose slug appears here are never returned. */\n excludeSlugs?: string[];\n /** Group key this collection's results are filed under. Must match a configured group. */\n group: string;\n /** Opaque icon token passed through to the consumer's own icon map. */\n icon?: string;\n /**\n * URL prefix for a result, without a trailing slash. Use an empty string for collections served\n * at the site root — it is deliberately distinct from \"unset\".\n */\n path: string;\n slug: CollectionSlug;\n /**\n * Ranking multiplier relative to other collections. 1 is neutral; raise it to float a\n * collection's matches above equally-good matches elsewhere.\n */\n weight?: number;\n};\n\n/** A presentation group that results are bucketed into. */\nexport type SearchGroup = {\n /** Human label. The plugin never renders it, but ships it so consumers stay consistent. */\n label: string;\n /** Maximum results returned for this group. Omit for no cap. */\n limit?: number;\n /** Sort position in the returned group list. Lower comes first. */\n order?: number;\n /** Whether this group appears in the empty-query preset list. */\n showWhenEmpty?: boolean;\n slug: string;\n};\n\n/**\n * A field on the indexed document to match against, with a relative weight.\n *\n * Weights are what stop the \"search for tuni, get unrelated results\" class of complaint: a title\n * hit and an incidental description hit stop being treated identically.\n */\nexport type SearchField = {\n name: string;\n weight?: number;\n};\n\nexport type FuzzyOptions = {\n /** Character distance a match may drift from the expected location. */\n distance?: number;\n /** Shortest run of characters that counts as a match. Guards against 1-2 letter noise. */\n minMatchCharLength?: number;\n /** 0 demands a perfect match, 1 matches anything. */\n threshold?: number;\n};\n\nexport type AnalyticsOptions = {\n /** Collection slug for stored events. */\n collectionSlug?: string;\n enabled?: boolean;\n /** Per-request gate, e.g. a consent check. Return false to record nothing. Default: always true. */\n isEnabled?: (req: PayloadRequest) => boolean | Promise<boolean>;\n /**\n * Days to keep events. Events older than this are pruned on write rather than accumulating an\n * unbounded log.\n */\n retentionDays?: number;\n /** Name of the cookie holding a visitor ID. Unset means visitor IDs are never read. */\n visitorCookie?: string;\n};\n\n/** `AnalyticsOptions` after defaults: `isEnabled` is always callable, `visitorCookie` stays optional. */\nexport type ResolvedAnalytics = {\n isEnabled: NonNullable<AnalyticsOptions['isEnabled']>;\n} & Omit<Required<AnalyticsOptions>, 'isEnabled' | 'visitorCookie'> &\n Pick<AnalyticsOptions, 'visitorCookie'>;\n\nexport type SearchPluginConfig = {\n analytics?: AnalyticsOptions | boolean;\n /** Milliseconds to hold the projected index in memory. */\n cacheTTL?: number;\n collections: SearchableCollection[];\n /**\n * Decode HTML entities in titles before returning them. Content migrated out of a CMS that\n * stored encoded markup will otherwise surface \"&\" as literal text wherever a result is\n * rendered as plain text.\n */\n decodeEntities?: boolean;\n disabled?: boolean;\n /** Fields matched against, with relative weights. */\n fields?: SearchField[];\n fuzzy?: FuzzyOptions;\n groups: SearchGroup[];\n /** Restrict the reindex and inspect endpoints to admin users. */\n requireAdminForOperations?: boolean;\n /** Base path for the plugin's endpoints. */\n routePrefix?: string;\n};\n\n/** One document as held in the in-memory projection. */\nexport type IndexedDoc = {\n collection: string;\n description?: string;\n group: string;\n icon?: string;\n id: string;\n path: string;\n slug: string;\n title: string;\n weight: number;\n};\n\nexport type SearchResult = {\n collection: string;\n group: string;\n href: string;\n icon?: string;\n id: string;\n /** Fuse score, carried through so consumers can debug ranking. Lower is a better match. */\n score?: number;\n title: string;\n};\n\nexport type GroupedResults = Record<string, SearchResult[]>;\n\n/** What the inspector reports, so \"is this searchable\" stops needing a code read. */\nexport type IndexReport = {\n builtAt: null | string;\n collections: {\n excluded: number;\n indexed: number;\n slug: string;\n }[];\n skipped: {\n id: string;\n reason: string;\n slug: string;\n }[];\n total: number;\n};\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["import type { RateLimitOptions } from '@foundrykit/rate-limit';\nimport type { CollectionSlug, PayloadRequest } from 'payload';\n\n/**\n * A collection that should appear in site search, and how its results present.\n *\n * Everything a host project would otherwise hardcode lives here: which group a collection lands\n * in, how its URLs are built, how strongly it ranks, and which documents are held back.\n */\nexport type SearchableCollection = {\n /** Documents whose slug appears here are never returned. */\n excludeSlugs?: string[];\n /** Group key this collection's results are filed under. Must match a configured group. */\n group: string;\n /** Opaque icon token passed through to the consumer's own icon map. */\n icon?: string;\n /**\n * URL prefix for a result, without a trailing slash. Use an empty string for collections served\n * at the site root — it is deliberately distinct from \"unset\".\n */\n path: string;\n slug: CollectionSlug;\n /**\n * Ranking multiplier relative to other collections. 1 is neutral; raise it to float a\n * collection's matches above equally-good matches elsewhere.\n */\n weight?: number;\n};\n\n/** A presentation group that results are bucketed into. */\nexport type SearchGroup = {\n /** Human label. The plugin never renders it, but ships it so consumers stay consistent. */\n label: string;\n /** Maximum results returned for this group. Omit for no cap. */\n limit?: number;\n /** Sort position in the returned group list. Lower comes first. */\n order?: number;\n /** Whether this group appears in the empty-query preset list. */\n showWhenEmpty?: boolean;\n slug: string;\n};\n\n/**\n * A field on the indexed document to match against, with a relative weight.\n *\n * Weights are what stop the \"search for tuni, get unrelated results\" class of complaint: a title\n * hit and an incidental description hit stop being treated identically.\n */\nexport type SearchField = {\n name: string;\n weight?: number;\n};\n\nexport type FuzzyOptions = {\n /** Character distance a match may drift from the expected location. */\n distance?: number;\n /** Shortest run of characters that counts as a match. Guards against 1-2 letter noise. */\n minMatchCharLength?: number;\n /** 0 demands a perfect match, 1 matches anything. */\n threshold?: number;\n};\n\nexport type AnalyticsOptions = {\n /** Collection slug for stored events. */\n collectionSlug?: string;\n enabled?: boolean;\n /** Per-request gate, e.g. a consent check. Return false to record nothing. Default: always true. */\n isEnabled?: (req: PayloadRequest) => boolean | Promise<boolean>;\n /**\n * Days to keep events. Events older than this are pruned on write rather than accumulating an\n * unbounded log.\n */\n retentionDays?: number;\n /** Name of the cookie holding a visitor ID. Unset means visitor IDs are never read. */\n visitorCookie?: string;\n};\n\n/** `AnalyticsOptions` after defaults: `isEnabled` is always callable, `visitorCookie` stays optional. */\nexport type ResolvedAnalytics = {\n isEnabled: NonNullable<AnalyticsOptions['isEnabled']>;\n} & Omit<Required<AnalyticsOptions>, 'isEnabled' | 'visitorCookie'> &\n Pick<AnalyticsOptions, 'visitorCookie'>;\n\nexport type SearchPluginConfig = {\n analytics?: AnalyticsOptions | boolean;\n /** Milliseconds to hold the projected index in memory. */\n cacheTTL?: number;\n collections: SearchableCollection[];\n /**\n * Decode HTML entities in titles before returning them. Content migrated out of a CMS that\n * stored encoded markup will otherwise surface \"&\" as literal text wherever a result is\n * rendered as plain text.\n */\n decodeEntities?: boolean;\n disabled?: boolean;\n /** Fields matched against, with relative weights. */\n fields?: SearchField[];\n fuzzy?: FuzzyOptions;\n groups: SearchGroup[];\n /**\n * Rate-limit the public track endpoint per client IP, using Payload's KV store. Default\n * `{ limit: 30, window: 60 }`; `false` turns it off. Set `trustedProxies` (see\n * `@foundrykit/rate-limit`) or every visitor shares one bucket.\n */\n rateLimit?:\n | false\n | Partial<Pick<RateLimitOptions, 'limit' | 'window' | 'trustedProxies'>>;\n /** Restrict the reindex and inspect endpoints to admin users. */\n requireAdminForOperations?: boolean;\n /** Base path for the plugin's endpoints. */\n routePrefix?: string;\n};\n\n/** One document as held in the in-memory projection. */\nexport type IndexedDoc = {\n collection: string;\n description?: string;\n group: string;\n icon?: string;\n id: string;\n path: string;\n slug: string;\n title: string;\n weight: number;\n};\n\nexport type SearchResult = {\n collection: string;\n group: string;\n href: string;\n icon?: string;\n id: string;\n /** Fuse score, carried through so consumers can debug ranking. Lower is a better match. */\n score?: number;\n title: string;\n};\n\nexport type GroupedResults = Record<string, SearchResult[]>;\n\n/** What the inspector reports, so \"is this searchable\" stops needing a code read. */\nexport type IndexReport = {\n builtAt: null | string;\n collections: {\n excluded: number;\n indexed: number;\n slug: string;\n }[];\n skipped: {\n id: string;\n reason: string;\n slug: string;\n }[];\n total: number;\n};\n"],"names":[],"mappings":"AA2IA,mFAAmF,GACnF,WAaE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@foundrykit/search-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A plugin for Payload CMS that provides a cached, grouped and ranked site search over the plugin-search index, with fuzzy matching, editor-controlled relevance and search analytics.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -39,25 +39,26 @@
|
|
|
39
39
|
"dist"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"fuse.js": "^7.5.0"
|
|
42
|
+
"fuse.js": "^7.5.0",
|
|
43
|
+
"@foundrykit/rate-limit": "^0.1.1"
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
|
-
"@payloadcms/plugin-search": "3.
|
|
46
|
-
"@payloadcms/ui": "3.
|
|
46
|
+
"@payloadcms/plugin-search": "3.90.0",
|
|
47
|
+
"@payloadcms/ui": "3.90.0",
|
|
47
48
|
"@swc/cli": "0.6.0",
|
|
48
49
|
"@types/node": "22.19.9",
|
|
49
50
|
"@types/react": "19.2.9",
|
|
50
51
|
"copyfiles": "2.4.1",
|
|
51
52
|
"cross-env": "^7.0.3",
|
|
52
|
-
"payload": "3.
|
|
53
|
-
"react": "19.2.
|
|
53
|
+
"payload": "3.90.0",
|
|
54
|
+
"react": "19.2.8",
|
|
54
55
|
"rimraf": "3.0.2",
|
|
55
56
|
"typescript": "5.9.2",
|
|
56
57
|
"vitest": "4.1.10"
|
|
57
58
|
},
|
|
58
59
|
"peerDependencies": {
|
|
59
|
-
"@payloadcms/plugin-search": "^3.
|
|
60
|
-
"payload": "^3.
|
|
60
|
+
"@payloadcms/plugin-search": "^3.84.0",
|
|
61
|
+
"payload": "^3.84.0",
|
|
61
62
|
"react": "^19.0.0"
|
|
62
63
|
},
|
|
63
64
|
"engines": {
|