@uidu/client 3.9.0 → 3.11.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/src/sites.ts ADDED
@@ -0,0 +1,173 @@
1
+ import type { UiduClient } from './client';
2
+ import {
3
+ GetSiteByDomainDocument,
4
+ GetSiteDocument,
5
+ GetSitePageBySlugDocument,
6
+ GetSiteSingletonBlockDocument,
7
+ ListSitePagesDocument,
8
+ type PublishableStatus,
9
+ type SiteBlockItemFragment,
10
+ type SiteFieldsFragment,
11
+ type SiteModelFieldsFragment,
12
+ type SitePageContentFragment,
13
+ type SitePageMetaFragment,
14
+ } from './generated';
15
+
16
+ /**
17
+ * The new CMS: a Site, its page types (page-kind Models), its pages
18
+ * (ModelItems of those Models) and the blocks placed on each page (block-kind
19
+ * ModelItems, in order).
20
+ *
21
+ * This is not the legacy CMS in `cms.ts` (`getPage`/`listPages`, read
22
+ * through `currentWorkspace.project(id:)`): sites still deployed on a legacy
23
+ * Project keep calling those, unchanged. Use this module for Sites.
24
+ *
25
+ * Every read here is public: it works with a `publicToken` client, on the
26
+ * server (RSC) and in the browser.
27
+ */
28
+
29
+ export type Site = SiteFieldsFragment;
30
+
31
+ /** A Model as the site sees it: a page type, a block kind, a singleton. */
32
+ export type SiteModel = SiteModelFieldsFragment;
33
+
34
+ /** Field values keyed by field shortname: `{ title: '…', cover: {…} }`. */
35
+ export type SiteFields = Record<string, unknown>;
36
+
37
+ export type SiteBlock = {
38
+ /** The block ModelItem. */
39
+ id: string;
40
+ name: string | null;
41
+ /** The block kind (a block-kind Model). */
42
+ model: SiteModel;
43
+ fields: SiteFields;
44
+ position: number;
45
+ /** The placement (the child FieldValue) — what reordering/removing targets. */
46
+ placementId: string;
47
+ };
48
+
49
+ export type SitePageSummary = {
50
+ id: string;
51
+ name: string | null;
52
+ slug: string | null;
53
+ publishedAt: string | null;
54
+ status: PublishableStatus | null;
55
+ /** The page type (a page-kind Model), or the singleton Model. */
56
+ model: SiteModel;
57
+ };
58
+
59
+ export type SitePage = SitePageSummary & {
60
+ fields: SiteFields;
61
+ /** Ordered by position. */
62
+ blocks: SiteBlock[];
63
+ };
64
+
65
+ // --- mapping ----------------------------------------------------------------
66
+
67
+ function toSummary(item: SitePageMetaFragment): SitePageSummary {
68
+ return {
69
+ id: item.id,
70
+ name: item.name ?? null,
71
+ slug: item.slug ?? null,
72
+ publishedAt: item.publishedAt ?? null,
73
+ status: item.publishableStatus ?? null,
74
+ model: item.model,
75
+ };
76
+ }
77
+
78
+ function toBlock(
79
+ placement: SitePageContentFragment['blocks'][number],
80
+ ): SiteBlock {
81
+ const item: SiteBlockItemFragment = placement.modelItem;
82
+ return {
83
+ id: item.id,
84
+ name: item.name ?? null,
85
+ model: item.model,
86
+ fields: item.fieldValuesByShortname ?? {},
87
+ position: placement.position,
88
+ placementId: placement.fieldValueId,
89
+ };
90
+ }
91
+
92
+ function toPage(item: SitePageContentFragment): SitePage {
93
+ return {
94
+ ...toSummary(item),
95
+ fields: item.fieldValuesByShortname ?? {},
96
+ blocks: item.blocks.map(toBlock).sort((a, b) => a.position - b.position),
97
+ };
98
+ }
99
+
100
+ // --- reads ------------------------------------------------------------------
101
+
102
+ /** The Site served on `domain` (e.g. `www.acme.org`), or null. */
103
+ export async function getSiteByDomain(
104
+ client: UiduClient,
105
+ variables: { domain: string },
106
+ ): Promise<Site | null> {
107
+ const { siteByDomain } = await client.query(
108
+ GetSiteByDomainDocument,
109
+ variables,
110
+ );
111
+ return siteByDomain ?? null;
112
+ }
113
+
114
+ /** A Site by its id — for previews, where there is no domain to go by. */
115
+ export async function getSite(
116
+ client: UiduClient,
117
+ variables: { id: string },
118
+ ): Promise<Site | null> {
119
+ const { node } = await client.query(GetSiteDocument, variables);
120
+ return node?.__typename === 'Site' ? node : null;
121
+ }
122
+
123
+ /**
124
+ * A page of the Site by slug, with its field values and its ordered blocks.
125
+ * Only published pages unless `includeDrafts`.
126
+ */
127
+ export async function getPageBySlug(
128
+ client: UiduClient,
129
+ variables: { siteId: string; slug: string; includeDrafts?: boolean },
130
+ ): Promise<SitePage | null> {
131
+ const { node } = await client.query(GetSitePageBySlugDocument, variables);
132
+ if (node?.__typename !== 'Site' || !node.pageBySlug) return null;
133
+ return toPage(node.pageBySlug);
134
+ }
135
+
136
+ /**
137
+ * The Site's pages, for navigation and sitemaps: published only unless
138
+ * `includeDrafts`, singletons (header, footer) left out.
139
+ *
140
+ * `pageType` keeps the pages of one page type, matched against its Model's
141
+ * id or name.
142
+ */
143
+ export async function listSitePages(
144
+ client: UiduClient,
145
+ variables: { siteId: string; pageType?: string; includeDrafts?: boolean },
146
+ ): Promise<SitePageSummary[]> {
147
+ const { siteId, pageType, includeDrafts = false } = variables;
148
+ const { node } = await client.query(ListSitePagesDocument, { siteId });
149
+ if (node?.__typename !== 'Site') return [];
150
+ return node.pages
151
+ .filter((item) => item.model.kind === 'page')
152
+ .filter((item) => includeDrafts || item.publishableStatus === 'Published')
153
+ .filter(
154
+ (item) =>
155
+ pageType === undefined ||
156
+ item.model.id === pageType ||
157
+ item.model.name === pageType,
158
+ )
159
+ .map(toSummary);
160
+ }
161
+
162
+ /**
163
+ * A singleton of the Site — the header, the footer, the nav — by its Model's
164
+ * shortname, with its field values and blocks.
165
+ */
166
+ export async function getSingletonBlock(
167
+ client: UiduClient,
168
+ variables: { siteId: string; shortname: string },
169
+ ): Promise<SitePage | null> {
170
+ const { node } = await client.query(GetSiteSingletonBlockDocument, variables);
171
+ if (node?.__typename !== 'Site' || !node.singletonBlock) return null;
172
+ return toPage(node.singletonBlock);
173
+ }
package/src/types.ts CHANGED
@@ -1,7 +1,14 @@
1
1
  export interface UiduClientConfig {
2
- workspace: string;
2
+ /** Builds the default endpoint. Not needed when `endpoint` is given. */
3
+ workspace?: string;
3
4
  publicToken?: string;
4
5
  apiKey?: string;
6
+ /**
7
+ * A Bearer token fetched per request, for tokens that expire — a custom
8
+ * app's session from `@uidu/app-bridge` (see `fromBridge`). Takes
9
+ * precedence over `apiKey`.
10
+ */
11
+ getToken?: () => Promise<string>;
5
12
  endpoint?: string;
6
13
  headers?: Record<string, string>;
7
14
  }