@conloca/content-api-client 0.1.16 → 0.2.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.
Files changed (45) hide show
  1. package/README.md +190 -105
  2. package/dist/client.d.ts +12 -14
  3. package/dist/client.d.ts.map +1 -1
  4. package/dist/client.js +26 -25
  5. package/dist/client.js.map +1 -0
  6. package/dist/data-bindings.d.ts +13 -0
  7. package/dist/data-bindings.d.ts.map +1 -0
  8. package/dist/data-bindings.js +71 -0
  9. package/dist/data-bindings.js.map +1 -0
  10. package/dist/hooks.d.ts +42 -39
  11. package/dist/hooks.d.ts.map +1 -1
  12. package/dist/hooks.js +93 -47
  13. package/dist/hooks.js.map +1 -0
  14. package/dist/index.d.ts +5 -5
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +5 -4
  17. package/dist/index.js.map +1 -0
  18. package/dist/lib/content-api-client.js +1 -0
  19. package/dist/lib/content-api-client.js.map +1 -0
  20. package/dist/test-helpers.d.ts +1 -2
  21. package/dist/test-helpers.d.ts.map +1 -1
  22. package/dist/test-helpers.js +2 -2
  23. package/dist/test-helpers.js.map +1 -0
  24. package/dist/testing.d.ts +1 -1
  25. package/dist/testing.d.ts.map +1 -1
  26. package/dist/testing.js +2 -1
  27. package/dist/testing.js.map +1 -0
  28. package/package.json +44 -44
  29. package/src/client.ts +513 -0
  30. package/src/data-bindings.ts +104 -0
  31. package/src/hooks.tsx +959 -0
  32. package/src/index.ts +50 -0
  33. package/src/lib/content-api-client.ts +3 -0
  34. package/src/test-helpers.ts +282 -0
  35. package/src/testing.ts +8 -0
  36. package/dist/index.d.mts +0 -234
  37. package/dist/index.d.mts.map +0 -1
  38. package/dist/index.mjs +0 -1106
  39. package/dist/index.mjs.map +0 -1
  40. package/dist/test-helpers-BFyjmKzv.mjs +0 -221
  41. package/dist/test-helpers-BFyjmKzv.mjs.map +0 -1
  42. package/dist/test-helpers-B_YQhpE2.d.mts +0 -93
  43. package/dist/test-helpers-B_YQhpE2.d.mts.map +0 -1
  44. package/dist/testing.d.mts +0 -2
  45. package/dist/testing.mjs +0 -2
@@ -0,0 +1,104 @@
1
+ import {
2
+ type DataBindingConfig,
3
+ type DataCollectionEntry,
4
+ type DataContext,
5
+ type EditorDataBindings,
6
+ isContentPublished,
7
+ type PageReference,
8
+ pageReferenceOf,
9
+ selectPagesByPrefix,
10
+ } from '@conloca/content-api';
11
+ import type { ContentAPIClient } from './client.js';
12
+
13
+ /** Everything in a page's data context that its route binds: all of it but the page. */
14
+ export type RouteData = Omit<DataContext, 'page'>;
15
+
16
+ /**
17
+ * What the route serving a page in `collection` binds, as the published page
18
+ * gets it, built from the content listing the content API serves. Undefined when
19
+ * no route serves the collection: such a page has no published render either.
20
+ * A read that fails rejects: blocks resolved against a partial context would
21
+ * show a page that differs from the published one without saying so.
22
+ */
23
+ export async function buildRouteData(
24
+ client: ContentAPIClient,
25
+ collection: string,
26
+ bindings: EditorDataBindings,
27
+ ): Promise<RouteData | undefined> {
28
+ const route = bindings.routes[collection];
29
+ if (!route) {
30
+ return undefined;
31
+ }
32
+
33
+ const locale = route.locale ?? bindings.locale;
34
+ const collectionEntries = await Promise.all(
35
+ (route.collections ?? []).map(async (name) => [name, await dataCollectionEntries(client, name, locale)] as const),
36
+ );
37
+ const collections = Object.fromEntries(collectionEntries);
38
+
39
+ return {
40
+ collections,
41
+ ...(route.pages?.prefix ? { pages: await pagesByPrefix(client, bindings, locale, route.pages) } : {}),
42
+ locale,
43
+ siteName: bindings.siteName,
44
+ };
45
+ }
46
+
47
+ /** The published pages of every routed collection, narrowed to the binding's feed. */
48
+ async function pagesByPrefix(
49
+ client: ContentAPIClient,
50
+ bindings: EditorDataBindings,
51
+ locale: string,
52
+ { prefix, sort, limit }: NonNullable<DataBindingConfig['pages']>,
53
+ ): Promise<PageReference[]> {
54
+ const { items } = await client.listAllContent({
55
+ kind: 'page',
56
+ site: bindings.siteName,
57
+ locales: [locale],
58
+ type: 'puck',
59
+ });
60
+ const routedCollections = new Set(Object.keys(bindings.routes));
61
+ const pages = items.flatMap((manifest) => {
62
+ const version = manifest.locales[locale];
63
+ return routedCollections.has(manifest.collection) && version && isContentPublished(version)
64
+ ? [pageReferenceOf(manifest, version)]
65
+ : [];
66
+ });
67
+ return selectPagesByPrefix(pages, prefix, { sort, limit });
68
+ }
69
+
70
+ const DATA_ENTRY_READ_CONCURRENCY = 8;
71
+
72
+ /** A data collection's published, named entries in `locale`, with their data. */
73
+ async function dataCollectionEntries(
74
+ client: ContentAPIClient,
75
+ collection: string,
76
+ locale: string,
77
+ ): Promise<DataCollectionEntry[]> {
78
+ const { items } = await client.getData(collection, locale);
79
+ const published = items.flatMap((manifest) => {
80
+ const version = manifest.locales[locale];
81
+ const name = version?.name;
82
+ if (!version || typeof name !== 'string' || !isContentPublished(version)) return [];
83
+ return [{ manifest, name, meta: version.meta }];
84
+ });
85
+ const entries: DataCollectionEntry[] = [];
86
+ entries.length = published.length;
87
+ const loadLane = (index: number): Promise<void> => {
88
+ const entry = published[index];
89
+ if (!entry) return Promise.resolve();
90
+ return client.getLocalized(entry.manifest.id, locale).then((localized) => {
91
+ entries[index] = {
92
+ id: entry.manifest.id,
93
+ name: entry.name,
94
+ data: localized?.localized.content.data ?? {},
95
+ meta: { title: entry.meta.title, description: entry.meta.description },
96
+ };
97
+ return loadLane(index + DATA_ENTRY_READ_CONCURRENCY);
98
+ });
99
+ };
100
+ await Promise.all(
101
+ Array.from({ length: Math.min(DATA_ENTRY_READ_CONCURRENCY, published.length) }, (_, index) => loadLane(index)),
102
+ );
103
+ return entries;
104
+ }