@tansy/search-algolia 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mohamed Salah
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.
package/README.md CHANGED
@@ -1,3 +1,13 @@
1
- # Temporary Holding Version
1
+ <!-- Generated by scripts/package-metadata.mjs. Change that script, not this file. -->
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ # @tansy/search-algolia
4
+
5
+ Algolia for tansy search.
6
+
7
+ ```bash
8
+ npm install @tansy/search-algolia
9
+ ```
10
+
11
+ Add it to `tansy.config.ts`. See [the search guide](https://github.com/mhmdsalahsebai/tansy/blob/main/docs/guides/search.md) for what it adds and the options it takes.
12
+
13
+ A first-party plugin for [tansy](https://github.com/mhmdsalahsebai/tansy), a self-hosted, runtime-schema CMS for Next.js. MIT licensed.
package/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ export interface AlgoliaOptions {
2
+ /** The application's id, from Algolia → Settings → API Keys. */
3
+ appId: string;
4
+ /**
5
+ * A key with the search, addObject, deleteObject, deleteIndex and editSettings
6
+ * rights, limited to this index. Never the search-only key: this one writes.
7
+ */
8
+ apiKey: string;
9
+ /** Letters, digits, `-` and `_`. Created if missing. Defaults to "tansy". */
10
+ index?: string;
11
+ }
12
+ /**
13
+ * Algolia, through its REST API. One index holds every collection, with each record's
14
+ * collection and language as attributes to filter on.
15
+ */
16
+ export declare function algolia({ appId, apiKey, index }: AlgoliaOptions, fetcher?: typeof fetch): import("tansy/search").SearchAdapter;
package/index.js ADDED
@@ -0,0 +1,57 @@
1
+ import { callEngine, defineSearchAdapter, documentId, quoted, recordIds } from "tansy/search";
2
+ const ENGINE = "Algolia";
3
+ const APP_ID = /^[A-Za-z0-9]{1,32}$/;
4
+ const INDEX_NAME = /^[A-Za-z0-9_-]{1,100}$/;
5
+ /** The most hits Algolia returns in one request. */
6
+ const MAX_HITS = 1000;
7
+ /**
8
+ * Algolia, through its REST API. One index holds every collection, with each record's
9
+ * collection and language as attributes to filter on.
10
+ */
11
+ export function algolia({ appId, apiKey, index = "tansy" }, fetcher = fetch) {
12
+ // The id becomes part of a host name, so it can't be allowed to pick another one.
13
+ if (!APP_ID.test(appId))
14
+ throw new Error(`${ENGINE} needs its application id: letters and digits only.`);
15
+ if (!apiKey)
16
+ throw new Error(`${ENGINE} needs an API key.`);
17
+ if (!INDEX_NAME.test(index))
18
+ throw new Error(`${ENGINE}'s index name may hold only letters, digits, - and _.`);
19
+ const headers = { "content-type": "application/json", "x-algolia-application-id": appId, "x-algolia-api-key": apiKey };
20
+ const write = `https://${appId}.algolia.net/1/indexes/${index}`;
21
+ const read = `https://${appId}-dsn.algolia.net/1/indexes/${index}`;
22
+ const call = (url, init) => callEngine(ENGINE, url, { ...init, headers }, fetcher);
23
+ // Algolia applies an index's writes in the order they arrive.
24
+ const deleteWhere = (filters) => call(`${write}/deleteBy`, { method: "POST", body: JSON.stringify({ filters }), allow: [404] });
25
+ return defineSearchAdapter({
26
+ name: "algolia",
27
+ target: `algolia:${appId}/${index}`,
28
+ async setup() {
29
+ // Creates the index too. Numbers filter without being facets, but the language needs to be one.
30
+ await call(`${write}/settings`, { method: "PUT", body: JSON.stringify({ searchableAttributes: ["text"], attributesForFaceting: ["filterOnly(locale)", "filterOnly(scope)"], attributesToRetrieve: ["recordId"] }) });
31
+ },
32
+ async index({ collectionId, recordId, text, locale, scope }) {
33
+ const document = { collectionId, recordId, text, ...(locale ? { locale } : {}), ...(scope ? { scope } : {}) };
34
+ await call(`${write}/${documentId(collectionId, recordId, locale)}`, { method: "PUT", body: JSON.stringify(document) });
35
+ },
36
+ async remove(collectionId, recordId, locale) {
37
+ if (locale)
38
+ await call(`${write}/${documentId(collectionId, recordId, locale)}`, { method: "DELETE", allow: [404] });
39
+ else
40
+ await deleteWhere(`collectionId=${collectionId} AND recordId=${recordId}`);
41
+ },
42
+ async removeCollection(collectionId) {
43
+ await deleteWhere(`collectionId=${collectionId}`);
44
+ },
45
+ async search(collectionId, query, limit, locale, scope) {
46
+ const filters = `collectionId=${collectionId}${locale ? ` AND locale:${quoted(locale)}` : ""}${scope ? ` AND scope:${quoted(scope)}` : ""}`;
47
+ const hitsPerPage = Math.min(limit, MAX_HITS);
48
+ // An index that doesn't exist yet has nothing to find.
49
+ const answer = await call(`${read}/query`, {
50
+ method: "POST",
51
+ body: JSON.stringify({ query, filters, hitsPerPage, attributesToRetrieve: ["recordId"], attributesToHighlight: [], attributesToSnippet: [], analytics: false }),
52
+ allow: [404],
53
+ });
54
+ return recordIds(answer.hits, hitsPerPage);
55
+ },
56
+ });
57
+ }
package/package.json CHANGED
@@ -1,6 +1,27 @@
1
1
  {
2
2
  "name": "@tansy/search-algolia",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Algolia for tansy search.",
5
+ "keywords": [
6
+ "tansy",
7
+ "tansy-search-adapter"
8
+ ],
9
+ "license": "MIT",
10
+ "author": "Mohamed Salah <mohamedsalahsebai@gmail.com>",
11
+ "homepage": "https://github.com/mhmdsalahsebai/tansy",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/mhmdsalahsebai/tansy.git",
15
+ "directory": "plugins/search-algolia"
16
+ },
17
+ "bugs": "https://github.com/mhmdsalahsebai/tansy/issues",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./index.d.ts",
21
+ "default": "./index.js"
22
+ }
23
+ },
24
+ "peerDependencies": {
25
+ "tansy": "^0.1.0"
26
+ }
27
+ }