@cms-lab/contentful 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 +21 -0
- package/README.md +28 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +132 -0
- package/package.json +44 -0
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/contentful
|
|
2
|
+
|
|
3
|
+
Contentful adapter for cms-lab.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
cms: {
|
|
7
|
+
provider: "contentful",
|
|
8
|
+
spaceId: "my-space",
|
|
9
|
+
environment: "master",
|
|
10
|
+
accessToken: process.env.CONTENTFUL_DELIVERY_TOKEN!,
|
|
11
|
+
contentTypes: [
|
|
12
|
+
{
|
|
13
|
+
type: "page",
|
|
14
|
+
contentType: "page",
|
|
15
|
+
uidField: "routing.slug",
|
|
16
|
+
urlField: "routing.url",
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The adapter reads Contentful Content Delivery API entries, paginates with
|
|
23
|
+
`limit` and `skip`, and normalizes them into cms-lab `CMSDocument` objects.
|
|
24
|
+
Top-level localized fields are flattened to the default locale when possible so
|
|
25
|
+
route mappings can use common fields such as `doc.uid` or `doc.data.slug`.
|
|
26
|
+
|
|
27
|
+
Use `uidField` or `urlField` when your project stores route values in custom
|
|
28
|
+
fields. Both options read dotted paths from normalized `document.data`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FetchLike, ContentfulCmsProviderConfig, CMSDocument, ContentfulContentTypeConfig } from '@cms-lab/core';
|
|
2
|
+
|
|
3
|
+
type FetchContentfulDocumentsOptions = {
|
|
4
|
+
fetch?: FetchLike;
|
|
5
|
+
};
|
|
6
|
+
declare function fetchContentfulDocuments(config: ContentfulCmsProviderConfig, options?: FetchContentfulDocumentsOptions): Promise<CMSDocument[]>;
|
|
7
|
+
declare function normalizeContentfulEntry(contentType: string | ContentfulContentTypeConfig, entry: unknown): CMSDocument;
|
|
8
|
+
|
|
9
|
+
export { type FetchContentfulDocumentsOptions, fetchContentfulDocuments, normalizeContentfulEntry };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import {
|
|
3
|
+
CmsFetchError,
|
|
4
|
+
readCmsDataPath
|
|
5
|
+
} from "@cms-lab/core";
|
|
6
|
+
var defaultApiUrl = "https://cdn.contentful.com";
|
|
7
|
+
var defaultEnvironment = "master";
|
|
8
|
+
var pageSize = 100;
|
|
9
|
+
async function fetchContentfulDocuments(config, options = {}) {
|
|
10
|
+
const fetchImpl = options.fetch ?? fetch;
|
|
11
|
+
const documents = [];
|
|
12
|
+
for (const contentType of config.contentTypes) {
|
|
13
|
+
let skip = 0;
|
|
14
|
+
while (true) {
|
|
15
|
+
const url = new URL(
|
|
16
|
+
`/spaces/${encodeURIComponent(config.spaceId)}/environments/${encodeURIComponent(config.environment ?? defaultEnvironment)}/entries`,
|
|
17
|
+
config.apiUrl ?? defaultApiUrl
|
|
18
|
+
);
|
|
19
|
+
url.searchParams.set("content_type", contentType.contentType);
|
|
20
|
+
url.searchParams.set("limit", String(pageSize));
|
|
21
|
+
url.searchParams.set("skip", String(skip));
|
|
22
|
+
const response = await fetchJson(
|
|
23
|
+
fetchImpl,
|
|
24
|
+
url,
|
|
25
|
+
authHeaders(config.accessToken)
|
|
26
|
+
);
|
|
27
|
+
const rows = response.items ?? [];
|
|
28
|
+
documents.push(
|
|
29
|
+
...rows.map((entry) => normalizeContentfulEntry(contentType, entry))
|
|
30
|
+
);
|
|
31
|
+
skip += rows.length;
|
|
32
|
+
const total = response.total ?? skip;
|
|
33
|
+
if (rows.length === 0 || skip >= total) {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return documents;
|
|
39
|
+
}
|
|
40
|
+
function normalizeContentfulEntry(contentType, entry) {
|
|
41
|
+
const config = contentTypeConfig(contentType);
|
|
42
|
+
const record = asContentfulEntry(entry);
|
|
43
|
+
const data = normalizeFields(record.fields ?? {});
|
|
44
|
+
return {
|
|
45
|
+
id: stringFrom(record.sys?.id, "Contentful entry is missing id"),
|
|
46
|
+
type: config.type,
|
|
47
|
+
uid: optionalString(
|
|
48
|
+
mappedValue(data, config.uidField) ?? data.uid ?? data.slug
|
|
49
|
+
),
|
|
50
|
+
url: optionalString(mappedValue(data, config.urlField)),
|
|
51
|
+
status: normalizeStatus(record.sys),
|
|
52
|
+
data
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
async function fetchJson(fetchImpl, url, headers) {
|
|
56
|
+
let response;
|
|
57
|
+
try {
|
|
58
|
+
response = await fetchImpl(url, { headers });
|
|
59
|
+
} catch (error) {
|
|
60
|
+
throw new CmsFetchError(
|
|
61
|
+
error instanceof Error ? error.message : `Failed to reach ${url}`
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
if (!response.ok) {
|
|
65
|
+
throw new CmsFetchError(
|
|
66
|
+
`Contentful request failed with HTTP ${response.status}`
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return await response.json();
|
|
70
|
+
}
|
|
71
|
+
function authHeaders(token) {
|
|
72
|
+
return {
|
|
73
|
+
Accept: "application/json",
|
|
74
|
+
Authorization: `Bearer ${token}`
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function asContentfulEntry(value) {
|
|
78
|
+
return value && typeof value === "object" ? value : {};
|
|
79
|
+
}
|
|
80
|
+
function normalizeFields(fields) {
|
|
81
|
+
return Object.fromEntries(
|
|
82
|
+
Object.entries(fields).map(([key, value]) => [key, selectLocalized(value)])
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
function selectLocalized(value) {
|
|
86
|
+
const record = asRecord(value);
|
|
87
|
+
if (!record || !isLocalizedRecord(record)) {
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
return record["en-US"] ?? record.en ?? Object.values(record)[0];
|
|
91
|
+
}
|
|
92
|
+
function isLocalizedRecord(record) {
|
|
93
|
+
const keys = Object.keys(record);
|
|
94
|
+
return keys.length > 0 && keys.every((key) => /^[a-z]{2}(?:-[A-Z]{2})?$/.test(key));
|
|
95
|
+
}
|
|
96
|
+
function asRecord(value) {
|
|
97
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
98
|
+
}
|
|
99
|
+
function stringFrom(value, message) {
|
|
100
|
+
if (typeof value === "string" && value.length > 0) {
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
104
|
+
return String(value);
|
|
105
|
+
}
|
|
106
|
+
throw new CmsFetchError(message);
|
|
107
|
+
}
|
|
108
|
+
function optionalString(value) {
|
|
109
|
+
if (typeof value === "string" && value.length > 0) {
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
113
|
+
return String(value);
|
|
114
|
+
}
|
|
115
|
+
return void 0;
|
|
116
|
+
}
|
|
117
|
+
function contentTypeConfig(contentType) {
|
|
118
|
+
return typeof contentType === "string" ? { type: contentType, contentType } : contentType;
|
|
119
|
+
}
|
|
120
|
+
function mappedValue(data, path) {
|
|
121
|
+
return path ? readCmsDataPath(data, path) : void 0;
|
|
122
|
+
}
|
|
123
|
+
function normalizeStatus(sys) {
|
|
124
|
+
if (sys?.publishedVersion || sys?.publishedAt || sys?.updatedAt) {
|
|
125
|
+
return "published";
|
|
126
|
+
}
|
|
127
|
+
return "draft";
|
|
128
|
+
}
|
|
129
|
+
export {
|
|
130
|
+
fetchContentfulDocuments,
|
|
131
|
+
normalizeContentfulEntry
|
|
132
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cms-lab/contentful",
|
|
3
|
+
"version": "1.0.5",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Contentful 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/contentful"
|
|
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
|
+
"contentful",
|
|
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
|
+
}
|