@cms-lab/wordpress 1.0.4 → 1.0.6

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/README.md CHANGED
@@ -8,7 +8,12 @@ cms: {
8
8
  url: "http://localhost:8080",
9
9
  contentTypes: [
10
10
  { type: "page", endpoint: "pages" },
11
- { type: "post", endpoint: "posts" },
11
+ {
12
+ type: "post",
13
+ endpoint: "posts",
14
+ uidField: "acf.handle",
15
+ urlField: "acf.permalink",
16
+ },
12
17
  ],
13
18
  }
14
19
  ```
@@ -19,6 +24,9 @@ in `document.data`, stores the REST `link` as `document.url`, uses `slug` as the
19
24
  UID when available, and treats scheduled, draft, pending, and private content as
20
25
  `draft`.
21
26
 
27
+ Use `uidField` or `urlField` when your project stores route values in custom
28
+ fields. Both options read dotted paths from `document.data`.
29
+
22
30
  ## Open Source
23
31
 
24
32
  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 CHANGED
@@ -1,10 +1,10 @@
1
- import { FetchLike, WordPressCmsProviderConfig, CMSDocument } from '@cms-lab/core';
1
+ import { FetchLike, WordPressCmsProviderConfig, CMSDocument, WordPressContentTypeConfig } from '@cms-lab/core';
2
2
 
3
3
  type WordPressItem = Record<string, unknown>;
4
4
  type FetchWordPressDocumentsOptions = {
5
5
  fetch?: FetchLike;
6
6
  };
7
7
  declare function fetchWordPressDocuments(config: WordPressCmsProviderConfig, options?: FetchWordPressDocumentsOptions): Promise<CMSDocument[]>;
8
- declare function normalizeWordPressItem(type: string, data: WordPressItem): CMSDocument;
8
+ declare function normalizeWordPressItem(contentType: string | WordPressContentTypeConfig, data: WordPressItem): CMSDocument;
9
9
 
10
10
  export { type FetchWordPressDocumentsOptions, fetchWordPressDocuments, normalizeWordPressItem };
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // src/index.ts
2
2
  import {
3
- CmsFetchError
3
+ CmsFetchError,
4
+ readCmsDataPath
4
5
  } from "@cms-lab/core";
5
6
  var defaultContentTypes = [
6
7
  { type: "page", endpoint: "pages" },
@@ -20,7 +21,7 @@ async function fetchWordPressDocuments(config, options = {}) {
20
21
  url.searchParams.set("page", String(page));
21
22
  const { rows, pages } = await fetchRows(fetchImpl, url);
22
23
  documents.push(
23
- ...rows.map((row) => normalizeWordPressItem(contentType.type, row))
24
+ ...rows.map((row) => normalizeWordPressItem(contentType, row))
24
25
  );
25
26
  const totalPages = pages;
26
27
  if (page >= totalPages) {
@@ -52,18 +53,29 @@ async function fetchRows(fetchImpl, url) {
52
53
  pages: Number(response.headers.get("x-wp-totalpages") ?? "1") || 1
53
54
  };
54
55
  }
55
- function normalizeWordPressItem(type, data) {
56
+ function normalizeWordPressItem(contentType, data) {
57
+ const config = contentTypeConfig(contentType);
56
58
  return {
57
59
  id: stringFrom(data.id, "WordPress item is missing id"),
58
- type,
59
- uid: optionalString(data.slug ?? data.id),
60
- url: optionalString(data.link),
60
+ type: config.type,
61
+ uid: optionalString(
62
+ mappedValue(data, config.uidField) ?? data.slug ?? data.id
63
+ ),
64
+ url: optionalString(mappedValue(data, config.urlField) ?? data.link),
61
65
  status: normalizeStatus(data.status),
62
66
  data
63
67
  };
64
68
  }
65
69
  function trimSlashes(value) {
66
- return value.replace(/^\/+|\/+$/g, "");
70
+ let start = 0;
71
+ let end = value.length;
72
+ while (start < end && value.charCodeAt(start) === 47) {
73
+ start += 1;
74
+ }
75
+ while (end > start && value.charCodeAt(end - 1) === 47) {
76
+ end -= 1;
77
+ }
78
+ return value.slice(start, end);
67
79
  }
68
80
  function stringFrom(value, message) {
69
81
  if (typeof value === "string" && value.length > 0) {
@@ -83,6 +95,12 @@ function optionalString(value) {
83
95
  }
84
96
  return void 0;
85
97
  }
98
+ function contentTypeConfig(contentType) {
99
+ return typeof contentType === "string" ? { type: contentType, endpoint: contentType } : contentType;
100
+ }
101
+ function mappedValue(data, path) {
102
+ return path ? readCmsDataPath(data, path) : void 0;
103
+ }
86
104
  function normalizeStatus(value) {
87
105
  const status = optionalString(value)?.toLowerCase();
88
106
  if (status && !["publish", "published"].includes(status)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cms-lab/wordpress",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "type": "module",
5
5
  "description": "WordPress REST document adapter for cms-lab.",
6
6
  "license": "MIT",
@@ -35,7 +35,7 @@
35
35
  "access": "public"
36
36
  },
37
37
  "dependencies": {
38
- "@cms-lab/core": "1.0.4"
38
+ "@cms-lab/core": "1.0.6"
39
39
  },
40
40
  "author": "Afaq Rashid",
41
41
  "scripts": {