@cms-lab/wordpress 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 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,24 @@
1
+ # @cms-lab/wordpress
2
+
3
+ WordPress REST adapter for cms-lab.
4
+
5
+ ```ts
6
+ cms: {
7
+ provider: "wordpress",
8
+ url: "http://localhost:8080",
9
+ contentTypes: [
10
+ { type: "page", endpoint: "pages" },
11
+ { type: "post", endpoint: "posts" },
12
+ ],
13
+ }
14
+ ```
15
+
16
+ The adapter reads WordPress REST API content and normalizes it into cms-lab
17
+ `CMSDocument` objects. It preserves WordPress SEO plugin JSON and media fields
18
+ in `document.data`, stores the REST `link` as `document.url`, uses `slug` as the
19
+ UID when available, and treats scheduled, draft, pending, and private content as
20
+ `draft`.
21
+
22
+ ## Open Source
23
+
24
+ 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).
@@ -0,0 +1,10 @@
1
+ import { FetchLike, WordPressCmsProviderConfig, CMSDocument } from '@cms-lab/core';
2
+
3
+ type WordPressItem = Record<string, unknown>;
4
+ type FetchWordPressDocumentsOptions = {
5
+ fetch?: FetchLike;
6
+ };
7
+ declare function fetchWordPressDocuments(config: WordPressCmsProviderConfig, options?: FetchWordPressDocumentsOptions): Promise<CMSDocument[]>;
8
+ declare function normalizeWordPressItem(type: string, data: WordPressItem): CMSDocument;
9
+
10
+ export { type FetchWordPressDocumentsOptions, fetchWordPressDocuments, normalizeWordPressItem };
package/dist/index.js ADDED
@@ -0,0 +1,96 @@
1
+ // src/index.ts
2
+ import {
3
+ CmsFetchError
4
+ } from "@cms-lab/core";
5
+ var defaultContentTypes = [
6
+ { type: "page", endpoint: "pages" },
7
+ { type: "post", endpoint: "posts" }
8
+ ];
9
+ async function fetchWordPressDocuments(config, options = {}) {
10
+ const fetchImpl = options.fetch ?? fetch;
11
+ const documents = [];
12
+ for (const contentType of config.contentTypes ?? defaultContentTypes) {
13
+ let page = 1;
14
+ while (true) {
15
+ const url = new URL(
16
+ `/wp-json/wp/v2/${trimSlashes(contentType.endpoint)}`,
17
+ config.url
18
+ );
19
+ url.searchParams.set("per_page", "100");
20
+ url.searchParams.set("page", String(page));
21
+ const { rows, pages } = await fetchRows(fetchImpl, url);
22
+ documents.push(
23
+ ...rows.map((row) => normalizeWordPressItem(contentType.type, row))
24
+ );
25
+ const totalPages = pages;
26
+ if (page >= totalPages) {
27
+ break;
28
+ }
29
+ page += 1;
30
+ }
31
+ }
32
+ return documents;
33
+ }
34
+ async function fetchRows(fetchImpl, url) {
35
+ let response;
36
+ try {
37
+ response = await fetchImpl(url, {
38
+ headers: { Accept: "application/json" }
39
+ });
40
+ } catch (error) {
41
+ throw new CmsFetchError(
42
+ error instanceof Error ? error.message : `Failed to reach ${url}`
43
+ );
44
+ }
45
+ if (!response.ok) {
46
+ throw new CmsFetchError(
47
+ `WordPress request failed with HTTP ${response.status}`
48
+ );
49
+ }
50
+ return {
51
+ rows: await response.json(),
52
+ pages: Number(response.headers.get("x-wp-totalpages") ?? "1") || 1
53
+ };
54
+ }
55
+ function normalizeWordPressItem(type, data) {
56
+ return {
57
+ id: stringFrom(data.id, "WordPress item is missing id"),
58
+ type,
59
+ uid: optionalString(data.slug ?? data.id),
60
+ url: optionalString(data.link),
61
+ status: normalizeStatus(data.status),
62
+ data
63
+ };
64
+ }
65
+ function trimSlashes(value) {
66
+ return value.replace(/^\/+|\/+$/g, "");
67
+ }
68
+ function stringFrom(value, message) {
69
+ if (typeof value === "string" && value.length > 0) {
70
+ return value;
71
+ }
72
+ if (typeof value === "number" && Number.isFinite(value)) {
73
+ return String(value);
74
+ }
75
+ throw new CmsFetchError(message);
76
+ }
77
+ function optionalString(value) {
78
+ if (typeof value === "string" && value.length > 0) {
79
+ return value;
80
+ }
81
+ if (typeof value === "number" && Number.isFinite(value)) {
82
+ return String(value);
83
+ }
84
+ return void 0;
85
+ }
86
+ function normalizeStatus(value) {
87
+ const status = optionalString(value)?.toLowerCase();
88
+ if (status && !["publish", "published"].includes(status)) {
89
+ return "draft";
90
+ }
91
+ return "published";
92
+ }
93
+ export {
94
+ fetchWordPressDocuments,
95
+ normalizeWordPressItem
96
+ };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@cms-lab/wordpress",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "description": "WordPress REST 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/wordpress"
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
+ "wordpress",
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
+ }