@cms-lab/strapi 1.0.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 +21 -0
- package/README.md +22 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +114 -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,22 @@
|
|
|
1
|
+
# @cms-lab/strapi
|
|
2
|
+
|
|
3
|
+
Strapi adapter for cms-lab.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
cms: {
|
|
7
|
+
provider: "strapi",
|
|
8
|
+
url: "http://localhost:1337",
|
|
9
|
+
token: process.env.STRAPI_TOKEN,
|
|
10
|
+
collections: [{ type: "page", endpoint: "pages" }],
|
|
11
|
+
}
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
The adapter reads Strapi REST collection responses and normalizes them into
|
|
15
|
+
cms-lab `CMSDocument` objects. It supports Strapi v4 `attributes` payloads and
|
|
16
|
+
newer flat REST payloads, keeps native SEO/media fields in `document.data`, uses
|
|
17
|
+
`documentId`, `slug`, or numeric `id` as stable identity values, and treats
|
|
18
|
+
non-published statuses as `draft`.
|
|
19
|
+
|
|
20
|
+
## Open Source
|
|
21
|
+
|
|
22
|
+
MIT licensed. See the repository [license](https://github.com/i-afaqrashid/cms-lab/blob/main/LICENSE), [contributing guide](https://github.com/i-afaqrashid/cms-lab/blob/main/CONTRIBUTING.md), and [support guide](https://github.com/i-afaqrashid/cms-lab/blob/main/SUPPORT.md).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FetchLike, StrapiCmsProviderConfig, CMSDocument } from '@cms-lab/core';
|
|
2
|
+
|
|
3
|
+
type FetchStrapiDocumentsOptions = {
|
|
4
|
+
fetch?: FetchLike;
|
|
5
|
+
};
|
|
6
|
+
declare function fetchStrapiDocuments(config: StrapiCmsProviderConfig, options?: FetchStrapiDocumentsOptions): Promise<CMSDocument[]>;
|
|
7
|
+
declare function normalizeStrapiItem(type: string, item: unknown): CMSDocument;
|
|
8
|
+
|
|
9
|
+
export { type FetchStrapiDocumentsOptions, fetchStrapiDocuments, normalizeStrapiItem };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import {
|
|
3
|
+
CmsFetchError
|
|
4
|
+
} from "@cms-lab/core";
|
|
5
|
+
async function fetchStrapiDocuments(config, options = {}) {
|
|
6
|
+
const fetchImpl = options.fetch ?? fetch;
|
|
7
|
+
const documents = [];
|
|
8
|
+
for (const collection of config.collections) {
|
|
9
|
+
let page = 1;
|
|
10
|
+
while (true) {
|
|
11
|
+
const url = new URL(
|
|
12
|
+
`/api/${trimSlashes(collection.endpoint)}`,
|
|
13
|
+
config.url
|
|
14
|
+
);
|
|
15
|
+
url.searchParams.set("pagination[pageSize]", "100");
|
|
16
|
+
url.searchParams.set("pagination[page]", String(page));
|
|
17
|
+
url.searchParams.set("populate", "*");
|
|
18
|
+
const response = await fetchJson(
|
|
19
|
+
fetchImpl,
|
|
20
|
+
url,
|
|
21
|
+
authHeaders(config.token)
|
|
22
|
+
);
|
|
23
|
+
documents.push(
|
|
24
|
+
...(response.data ?? []).map(
|
|
25
|
+
(item) => normalizeStrapiItem(collection.type, item)
|
|
26
|
+
)
|
|
27
|
+
);
|
|
28
|
+
const pageCount = response.meta?.pagination?.pageCount ?? page;
|
|
29
|
+
if (page >= pageCount) {
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
page += 1;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return documents;
|
|
36
|
+
}
|
|
37
|
+
function normalizeStrapiItem(type, item) {
|
|
38
|
+
const record = asRecord(item);
|
|
39
|
+
const attributes = optionalRecord(record.attributes);
|
|
40
|
+
const data = attributes ? { id: record.id, ...attributes } : record;
|
|
41
|
+
const id = stringFrom(
|
|
42
|
+
record.documentId ?? record.id ?? data.id,
|
|
43
|
+
"Strapi item is missing id"
|
|
44
|
+
);
|
|
45
|
+
return {
|
|
46
|
+
id,
|
|
47
|
+
type,
|
|
48
|
+
uid: optionalString(
|
|
49
|
+
data.uid ?? data.slug ?? record.documentId ?? record.id
|
|
50
|
+
),
|
|
51
|
+
status: normalizeStatus(data),
|
|
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
|
+
`Strapi request failed with HTTP ${response.status}`
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return await response.json();
|
|
70
|
+
}
|
|
71
|
+
function authHeaders(token) {
|
|
72
|
+
return token ? { Accept: "application/json", Authorization: `Bearer ${token}` } : { Accept: "application/json" };
|
|
73
|
+
}
|
|
74
|
+
function trimSlashes(value) {
|
|
75
|
+
return value.replace(/^\/+|\/+$/g, "");
|
|
76
|
+
}
|
|
77
|
+
function asRecord(value) {
|
|
78
|
+
return value && typeof value === "object" ? value : {};
|
|
79
|
+
}
|
|
80
|
+
function optionalRecord(value) {
|
|
81
|
+
return value && typeof value === "object" ? value : void 0;
|
|
82
|
+
}
|
|
83
|
+
function stringFrom(value, message) {
|
|
84
|
+
if (typeof value === "string" && value.length > 0) {
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
87
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
88
|
+
return String(value);
|
|
89
|
+
}
|
|
90
|
+
throw new CmsFetchError(message);
|
|
91
|
+
}
|
|
92
|
+
function optionalString(value) {
|
|
93
|
+
if (typeof value === "string" && value.length > 0) {
|
|
94
|
+
return value;
|
|
95
|
+
}
|
|
96
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
97
|
+
return String(value);
|
|
98
|
+
}
|
|
99
|
+
return void 0;
|
|
100
|
+
}
|
|
101
|
+
function normalizeStatus(data) {
|
|
102
|
+
if ("publishedAt" in data && data.publishedAt === null) {
|
|
103
|
+
return "draft";
|
|
104
|
+
}
|
|
105
|
+
const status = optionalString(data.status)?.toLowerCase();
|
|
106
|
+
if (status && !["publish", "published", "live"].includes(status)) {
|
|
107
|
+
return "draft";
|
|
108
|
+
}
|
|
109
|
+
return "published";
|
|
110
|
+
}
|
|
111
|
+
export {
|
|
112
|
+
fetchStrapiDocuments,
|
|
113
|
+
normalizeStrapiItem
|
|
114
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cms-lab/strapi",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Strapi 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/strapi"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://cms-lab.dev",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/i-afaqrashid/cms-lab/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"cms",
|
|
18
|
+
"strapi",
|
|
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.0"
|
|
39
|
+
},
|
|
40
|
+
"author": "Afaq Rashid",
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup src/index.ts --format esm --dts --clean --tsconfig ../../tsconfig.base.json"
|
|
43
|
+
}
|
|
44
|
+
}
|