@cms-lab/strapi 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
@@ -7,7 +7,14 @@ cms: {
7
7
  provider: "strapi",
8
8
  url: "http://localhost:1337",
9
9
  token: process.env.STRAPI_TOKEN,
10
- collections: [{ type: "page", endpoint: "pages" }],
10
+ collections: [
11
+ {
12
+ type: "page",
13
+ endpoint: "pages",
14
+ uidField: "routing.slug",
15
+ urlField: "routing.url",
16
+ },
17
+ ],
11
18
  }
12
19
  ```
13
20
 
@@ -17,6 +24,9 @@ newer flat REST payloads, keeps native SEO/media fields in `document.data`, uses
17
24
  `documentId`, `slug`, or numeric `id` as stable identity values, and treats
18
25
  non-published statuses as `draft`.
19
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
+
20
30
  ## Open Source
21
31
 
22
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,9 +1,9 @@
1
- import { FetchLike, StrapiCmsProviderConfig, CMSDocument } from '@cms-lab/core';
1
+ import { FetchLike, StrapiCmsProviderConfig, CMSDocument, StrapiCollectionConfig } from '@cms-lab/core';
2
2
 
3
3
  type FetchStrapiDocumentsOptions = {
4
4
  fetch?: FetchLike;
5
5
  };
6
6
  declare function fetchStrapiDocuments(config: StrapiCmsProviderConfig, options?: FetchStrapiDocumentsOptions): Promise<CMSDocument[]>;
7
- declare function normalizeStrapiItem(type: string, item: unknown): CMSDocument;
7
+ declare function normalizeStrapiItem(collection: string | StrapiCollectionConfig, item: unknown): CMSDocument;
8
8
 
9
9
  export { type FetchStrapiDocumentsOptions, fetchStrapiDocuments, normalizeStrapiItem };
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
  async function fetchStrapiDocuments(config, options = {}) {
6
7
  const fetchImpl = options.fetch ?? fetch;
@@ -22,7 +23,7 @@ async function fetchStrapiDocuments(config, options = {}) {
22
23
  );
23
24
  documents.push(
24
25
  ...(response.data ?? []).map(
25
- (item) => normalizeStrapiItem(collection.type, item)
26
+ (item) => normalizeStrapiItem(collection, item)
26
27
  )
27
28
  );
28
29
  const pageCount = response.meta?.pagination?.pageCount ?? page;
@@ -34,7 +35,8 @@ async function fetchStrapiDocuments(config, options = {}) {
34
35
  }
35
36
  return documents;
36
37
  }
37
- function normalizeStrapiItem(type, item) {
38
+ function normalizeStrapiItem(collection, item) {
39
+ const config = collectionConfig(collection);
38
40
  const record = asRecord(item);
39
41
  const attributes = optionalRecord(record.attributes);
40
42
  const data = attributes ? { id: record.id, ...attributes } : record;
@@ -44,10 +46,11 @@ function normalizeStrapiItem(type, item) {
44
46
  );
45
47
  return {
46
48
  id,
47
- type,
49
+ type: config.type,
48
50
  uid: optionalString(
49
- data.uid ?? data.slug ?? record.documentId ?? record.id
51
+ mappedValue(data, config.uidField) ?? data.uid ?? data.slug ?? record.documentId ?? record.id
50
52
  ),
53
+ url: optionalString(mappedValue(data, config.urlField)),
51
54
  status: normalizeStatus(data),
52
55
  data
53
56
  };
@@ -72,7 +75,15 @@ function authHeaders(token) {
72
75
  return token ? { Accept: "application/json", Authorization: `Bearer ${token}` } : { Accept: "application/json" };
73
76
  }
74
77
  function trimSlashes(value) {
75
- return value.replace(/^\/+|\/+$/g, "");
78
+ let start = 0;
79
+ let end = value.length;
80
+ while (start < end && value.charCodeAt(start) === 47) {
81
+ start += 1;
82
+ }
83
+ while (end > start && value.charCodeAt(end - 1) === 47) {
84
+ end -= 1;
85
+ }
86
+ return value.slice(start, end);
76
87
  }
77
88
  function asRecord(value) {
78
89
  return value && typeof value === "object" ? value : {};
@@ -98,6 +109,12 @@ function optionalString(value) {
98
109
  }
99
110
  return void 0;
100
111
  }
112
+ function collectionConfig(collection) {
113
+ return typeof collection === "string" ? { type: collection, endpoint: collection } : collection;
114
+ }
115
+ function mappedValue(data, path) {
116
+ return path ? readCmsDataPath(data, path) : void 0;
117
+ }
101
118
  function normalizeStatus(data) {
102
119
  if ("publishedAt" in data && data.publishedAt === null) {
103
120
  return "draft";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cms-lab/strapi",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "type": "module",
5
5
  "description": "Strapi 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": {