@cms-lab/sanity 1.0.5

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 Afaq Rashid
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 ADDED
@@ -0,0 +1,28 @@
1
+ # @cms-lab/sanity
2
+
3
+ Sanity adapter for cms-lab.
4
+
5
+ ```ts
6
+ cms: {
7
+ provider: "sanity",
8
+ projectId: "my-project",
9
+ dataset: "production",
10
+ apiVersion: "2025-02-19",
11
+ token: process.env.SANITY_READ_TOKEN,
12
+ contentTypes: [
13
+ {
14
+ type: "page",
15
+ documentType: "page",
16
+ uidField: "slug.current",
17
+ urlField: "seo.canonical",
18
+ },
19
+ ],
20
+ }
21
+ ```
22
+
23
+ The adapter reads Sanity documents through the HTTP Query API, one configured
24
+ document type at a time, and normalizes them into cms-lab `CMSDocument` objects.
25
+ Documents whose `_id` starts with `drafts.` are marked as drafts.
26
+
27
+ Use `uidField` or `urlField` when your project stores route values in custom
28
+ fields. Both options read dotted paths from `document.data`.
@@ -0,0 +1,9 @@
1
+ import { FetchLike, SanityCmsProviderConfig, CMSDocument, SanityContentTypeConfig } from '@cms-lab/core';
2
+
3
+ type FetchSanityDocumentsOptions = {
4
+ fetch?: FetchLike;
5
+ };
6
+ declare function fetchSanityDocuments(config: SanityCmsProviderConfig, options?: FetchSanityDocumentsOptions): Promise<CMSDocument[]>;
7
+ declare function normalizeSanityDocument(contentType: string | SanityContentTypeConfig, document: unknown): CMSDocument;
8
+
9
+ export { type FetchSanityDocumentsOptions, fetchSanityDocuments, normalizeSanityDocument };
package/dist/index.js ADDED
@@ -0,0 +1,108 @@
1
+ // src/index.ts
2
+ import {
3
+ CmsFetchError,
4
+ readCmsDataPath
5
+ } from "@cms-lab/core";
6
+ var defaultApiVersion = "2025-02-19";
7
+ async function fetchSanityDocuments(config, options = {}) {
8
+ const fetchImpl = options.fetch ?? fetch;
9
+ const documents = [];
10
+ for (const contentType of config.contentTypes) {
11
+ const url = sanityQueryUrl(config);
12
+ url.searchParams.set("query", "*[_type == $type]");
13
+ url.searchParams.set("$type", JSON.stringify(contentType.documentType));
14
+ url.searchParams.set("perspective", config.perspective ?? "published");
15
+ const response = await fetchJson(
16
+ fetchImpl,
17
+ url,
18
+ authHeaders(config.token)
19
+ );
20
+ const rows = Array.isArray(response.result) ? response.result : [];
21
+ documents.push(
22
+ ...rows.map((document) => normalizeSanityDocument(contentType, document))
23
+ );
24
+ }
25
+ return documents;
26
+ }
27
+ function normalizeSanityDocument(contentType, document) {
28
+ const config = contentTypeConfig(contentType);
29
+ const data = asRecord(document);
30
+ const id = stringFrom(data._id, "Sanity document is missing _id");
31
+ return {
32
+ id,
33
+ type: config.type,
34
+ uid: optionalString(
35
+ mappedValue(data, config.uidField) ?? data.uid ?? slugValue(data.slug)
36
+ ),
37
+ url: optionalString(mappedValue(data, config.urlField)),
38
+ status: id.startsWith("drafts.") ? "draft" : "published",
39
+ data
40
+ };
41
+ }
42
+ async function fetchJson(fetchImpl, url, headers) {
43
+ let response;
44
+ try {
45
+ response = await fetchImpl(url, { headers });
46
+ } catch (error) {
47
+ throw new CmsFetchError(
48
+ error instanceof Error ? error.message : `Failed to reach ${url}`
49
+ );
50
+ }
51
+ if (!response.ok) {
52
+ throw new CmsFetchError(
53
+ `Sanity request failed with HTTP ${response.status}`
54
+ );
55
+ }
56
+ return await response.json();
57
+ }
58
+ function sanityQueryUrl(config) {
59
+ const host = `${config.projectId}.${config.useCdn ? "apicdn" : "api"}.sanity.io`;
60
+ const version = apiVersion(config.apiVersion ?? defaultApiVersion);
61
+ return new URL(
62
+ `https://${host}/${version}/data/query/${encodeURIComponent(config.dataset)}`
63
+ );
64
+ }
65
+ function apiVersion(value) {
66
+ return value.startsWith("v") ? value : `v${value}`;
67
+ }
68
+ function authHeaders(token) {
69
+ return token ? { Accept: "application/json", Authorization: `Bearer ${token}` } : { Accept: "application/json" };
70
+ }
71
+ function asRecord(value) {
72
+ return value && typeof value === "object" ? value : {};
73
+ }
74
+ function slugValue(value) {
75
+ if (typeof value === "string") {
76
+ return value;
77
+ }
78
+ const record = value && typeof value === "object" ? value : void 0;
79
+ return record?.current;
80
+ }
81
+ function stringFrom(value, message) {
82
+ if (typeof value === "string" && value.length > 0) {
83
+ return value;
84
+ }
85
+ if (typeof value === "number" && Number.isFinite(value)) {
86
+ return String(value);
87
+ }
88
+ throw new CmsFetchError(message);
89
+ }
90
+ function optionalString(value) {
91
+ if (typeof value === "string" && value.length > 0) {
92
+ return value;
93
+ }
94
+ if (typeof value === "number" && Number.isFinite(value)) {
95
+ return String(value);
96
+ }
97
+ return void 0;
98
+ }
99
+ function contentTypeConfig(contentType) {
100
+ return typeof contentType === "string" ? { type: contentType, documentType: contentType } : contentType;
101
+ }
102
+ function mappedValue(data, path) {
103
+ return path ? readCmsDataPath(data, path) : void 0;
104
+ }
105
+ export {
106
+ fetchSanityDocuments,
107
+ normalizeSanityDocument
108
+ };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@cms-lab/sanity",
3
+ "version": "1.0.5",
4
+ "type": "module",
5
+ "description": "Sanity document adapter for cms-lab.",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/i-afaqrashid/cms-lab.git",
10
+ "directory": "packages/sanity"
11
+ },
12
+ "homepage": "https://cmslab.afaqrashid.com",
13
+ "bugs": {
14
+ "url": "https://github.com/i-afaqrashid/cms-lab/issues"
15
+ },
16
+ "keywords": [
17
+ "cms",
18
+ "sanity",
19
+ "testing"
20
+ ],
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "import": "./dist/index.js"
25
+ }
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "README.md"
30
+ ],
31
+ "engines": {
32
+ "node": ">=20.10"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "dependencies": {
38
+ "@cms-lab/core": "1.0.5"
39
+ },
40
+ "author": "Afaq Rashid",
41
+ "scripts": {
42
+ "build": "tsup src/index.ts --format esm --dts --clean --tsconfig ../../tsconfig.base.json"
43
+ }
44
+ }