@makeswift/runtime 0.24.7-canary.1 → 0.24.7-canary.2
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/client/package.json +5 -0
- package/dist/cjs/client/index.js +686 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/next/api-handler/handlers/manifest.js +1 -1
- package/dist/cjs/next/client.js +8 -654
- package/dist/cjs/next/client.js.map +1 -1
- package/dist/cjs/next/components/MakeswiftComponent.js +1 -1
- package/dist/cjs/next/components/MakeswiftComponent.js.map +1 -1
- package/dist/cjs/next/components/Slot.js.map +1 -1
- package/dist/cjs/next/components/page.js +1 -1
- package/dist/cjs/next/components/page.js.map +1 -1
- package/dist/cjs/next/index.js.map +1 -1
- package/dist/cjs/runtimes/react/testing/element-data.js.map +1 -1
- package/dist/cjs/utils/async-iterable.js.map +1 -0
- package/dist/cjs/utils/pagination.js.map +1 -0
- package/dist/esm/client/index.js +671 -0
- package/dist/esm/client/index.js.map +1 -0
- package/dist/esm/next/api-handler/handlers/manifest.js +1 -1
- package/dist/esm/next/client.js +7 -662
- package/dist/esm/next/client.js.map +1 -1
- package/dist/esm/next/components/MakeswiftComponent.js +1 -1
- package/dist/esm/next/components/MakeswiftComponent.js.map +1 -1
- package/dist/esm/next/components/Slot.js.map +1 -1
- package/dist/esm/next/components/page.js +1 -1
- package/dist/esm/next/components/page.js.map +1 -1
- package/dist/esm/next/index.js.map +1 -1
- package/dist/esm/runtimes/react/testing/element-data.js.map +1 -1
- package/dist/esm/utils/async-iterable.js.map +1 -0
- package/dist/esm/utils/pagination.js.map +1 -0
- package/dist/types/client/index.d.ts +697 -0
- package/dist/types/client/index.d.ts.map +1 -0
- package/dist/types/client/tests/client.error-handling.test.d.ts.map +1 -0
- package/dist/types/client/tests/client.get-component-snapshot.test.d.ts.map +1 -0
- package/dist/types/client/tests/client.get-page-snapshot.test.d.ts.map +1 -0
- package/dist/types/client/tests/client.get-pages.test.d.ts.map +1 -0
- package/dist/types/client/tests/client.get-resources.test.d.ts.map +1 -0
- package/dist/types/next/client.d.ts +3 -690
- package/dist/types/next/client.d.ts.map +1 -1
- package/dist/types/next/components/MakeswiftComponent.d.ts +1 -1
- package/dist/types/next/components/MakeswiftComponent.d.ts.map +1 -1
- package/dist/types/next/components/Slot.d.ts +1 -1
- package/dist/types/next/components/Slot.d.ts.map +1 -1
- package/dist/types/next/components/page.d.ts +1 -1
- package/dist/types/next/components/page.d.ts.map +1 -1
- package/dist/types/next/index.d.ts +1 -1
- package/dist/types/next/index.d.ts.map +1 -1
- package/dist/types/runtimes/react/testing/element-data.d.ts +1 -1
- package/dist/types/runtimes/react/testing/element-data.d.ts.map +1 -1
- package/dist/types/utils/async-iterable.d.ts.map +1 -0
- package/dist/types/utils/pagination.d.ts.map +1 -0
- package/package.json +7 -1
- package/dist/cjs/next/utils/async-iterable.js.map +0 -1
- package/dist/cjs/next/utils/pagination.js.map +0 -1
- package/dist/esm/next/utils/async-iterable.js.map +0 -1
- package/dist/esm/next/utils/pagination.js.map +0 -1
- package/dist/types/next/tests/client.error-handling.test.d.ts.map +0 -1
- package/dist/types/next/tests/client.get-component-snapshot.test.d.ts.map +0 -1
- package/dist/types/next/tests/client.get-page-snapshot.test.d.ts.map +0 -1
- package/dist/types/next/tests/client.get-pages.test.d.ts.map +0 -1
- package/dist/types/next/tests/client.get-resources.test.d.ts.map +0 -1
- package/dist/types/next/utils/async-iterable.d.ts.map +0 -1
- package/dist/types/next/utils/pagination.d.ts.map +0 -1
- /package/dist/cjs/{next/utils → utils}/async-iterable.js +0 -0
- /package/dist/cjs/{next/utils → utils}/pagination.js +0 -0
- /package/dist/esm/{next/utils → utils}/async-iterable.js +0 -0
- /package/dist/esm/{next/utils → utils}/pagination.js +0 -0
- /package/dist/types/{next → client}/tests/client.error-handling.test.d.ts +0 -0
- /package/dist/types/{next → client}/tests/client.get-component-snapshot.test.d.ts +0 -0
- /package/dist/types/{next → client}/tests/client.get-page-snapshot.test.d.ts +0 -0
- /package/dist/types/{next → client}/tests/client.get-pages.test.d.ts +0 -0
- /package/dist/types/{next → client}/tests/client.get-resources.test.d.ts +0 -0
- /package/dist/types/{next/utils → utils}/async-iterable.d.ts +0 -0
- /package/dist/types/{next/utils → utils}/pagination.d.ts +0 -0
|
@@ -0,0 +1,686 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var client_exports = {};
|
|
20
|
+
__export(client_exports, {
|
|
21
|
+
MakeswiftClient: () => MakeswiftClient,
|
|
22
|
+
componentDocumentToRootEmbeddedDocument: () => componentDocumentToRootEmbeddedDocument,
|
|
23
|
+
failedResponseBody: () => failedResponseBody,
|
|
24
|
+
pageToRootDocument: () => pageToRootDocument
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(client_exports);
|
|
27
|
+
var import_zod = require("zod");
|
|
28
|
+
var import_api = require("../api");
|
|
29
|
+
var import_client = require("../api/graphql/client");
|
|
30
|
+
var import_documents = require("../api/graphql/documents");
|
|
31
|
+
var import_react = require("../api/react");
|
|
32
|
+
var import_introspection = require("../prop-controllers/introspection");
|
|
33
|
+
var import_react_page = require("../state/react-page");
|
|
34
|
+
var import_site_version = require("../api/site-version");
|
|
35
|
+
var import_pagination = require("../utils/pagination");
|
|
36
|
+
var import_deterministic_uuid = require("../utils/deterministic-uuid");
|
|
37
|
+
var import_controls = require("@makeswift/controls");
|
|
38
|
+
var import_read_only_documents = require("../state/modules/read-only-documents");
|
|
39
|
+
const makeswiftPageResultSchema = import_zod.z.object({
|
|
40
|
+
id: import_zod.z.string(),
|
|
41
|
+
path: import_zod.z.string(),
|
|
42
|
+
title: import_zod.z.string().nullable(),
|
|
43
|
+
description: import_zod.z.string().nullable(),
|
|
44
|
+
canonicalUrl: import_zod.z.string().nullable(),
|
|
45
|
+
socialImageUrl: import_zod.z.string().nullable(),
|
|
46
|
+
sitemapPriority: import_zod.z.number().nullable(),
|
|
47
|
+
sitemapFrequency: import_zod.z.enum(["always", "hourly", "daily", "weekly", "monthly", "yearly", "never"]).nullable(),
|
|
48
|
+
createdAt: import_zod.z.string(),
|
|
49
|
+
updatedAt: import_zod.z.string(),
|
|
50
|
+
publishedAt: import_zod.z.string().nullable(),
|
|
51
|
+
isOnline: import_zod.z.boolean().nullable(),
|
|
52
|
+
excludedFromSearch: import_zod.z.boolean().nullable(),
|
|
53
|
+
locale: import_zod.z.string(),
|
|
54
|
+
localizedVariants: import_zod.z.array(
|
|
55
|
+
import_zod.z.object({
|
|
56
|
+
locale: import_zod.z.string(),
|
|
57
|
+
path: import_zod.z.string()
|
|
58
|
+
})
|
|
59
|
+
)
|
|
60
|
+
});
|
|
61
|
+
const makeswiftGetPagesResultAPISchema = import_zod.z.object({
|
|
62
|
+
data: import_zod.z.array(makeswiftPageResultSchema),
|
|
63
|
+
hasMore: import_zod.z.boolean()
|
|
64
|
+
});
|
|
65
|
+
const makeswiftGetPagesParamsSchema = import_zod.z.object({
|
|
66
|
+
limit: import_zod.z.number().optional(),
|
|
67
|
+
after: import_zod.z.string().optional(),
|
|
68
|
+
sortBy: import_zod.z.enum(["title", "path", "description", "createdAt", "updatedAt"]).optional(),
|
|
69
|
+
sortDirection: import_zod.z.enum(["asc", "desc"]).optional(),
|
|
70
|
+
includeOffline: import_zod.z.boolean().optional(),
|
|
71
|
+
pathPrefix: import_zod.z.string().optional(),
|
|
72
|
+
locale: import_zod.z.string().optional()
|
|
73
|
+
});
|
|
74
|
+
function getPagesQueryParams({
|
|
75
|
+
limit = 20,
|
|
76
|
+
after,
|
|
77
|
+
sortBy,
|
|
78
|
+
sortDirection,
|
|
79
|
+
includeOffline,
|
|
80
|
+
pathPrefix,
|
|
81
|
+
locale
|
|
82
|
+
}) {
|
|
83
|
+
const params = new URLSearchParams();
|
|
84
|
+
if (limit != null)
|
|
85
|
+
params.set("limit", limit.toString());
|
|
86
|
+
if (after != null)
|
|
87
|
+
params.set("after", after);
|
|
88
|
+
if (sortBy != null)
|
|
89
|
+
params.set("sortBy", sortBy);
|
|
90
|
+
if (sortDirection != null)
|
|
91
|
+
params.set("sortDirection", sortDirection);
|
|
92
|
+
if (includeOffline != null)
|
|
93
|
+
params.set("includeOffline", includeOffline.toString());
|
|
94
|
+
if (pathPrefix != null)
|
|
95
|
+
params.set("pathPrefix", pathPrefix);
|
|
96
|
+
if (locale != null)
|
|
97
|
+
params.set("locale", locale);
|
|
98
|
+
return params;
|
|
99
|
+
}
|
|
100
|
+
function pageToRootDocument(pageDocument) {
|
|
101
|
+
const { locale, localizedPages, id, data } = pageDocument;
|
|
102
|
+
const localizedPage = localizedPages.find(({ parentId }) => parentId == null);
|
|
103
|
+
return localizedPage ? { key: localizedPage.elementTreeId, rootElement: localizedPage.data, locale } : { key: id, rootElement: data, locale };
|
|
104
|
+
}
|
|
105
|
+
const makeswiftComponentDocumentSchema = import_zod.z.object({
|
|
106
|
+
id: import_zod.z.string(),
|
|
107
|
+
name: import_zod.z.string().nullable(),
|
|
108
|
+
locale: import_zod.z.string().nullable(),
|
|
109
|
+
data: import_controls.Schema.element,
|
|
110
|
+
siteId: import_zod.z.string(),
|
|
111
|
+
inheritsFromParent: import_zod.z.boolean()
|
|
112
|
+
});
|
|
113
|
+
const makeswiftComponentDocumentFallbackSchema = import_zod.z.object({
|
|
114
|
+
id: import_zod.z.string(),
|
|
115
|
+
locale: import_zod.z.string().nullable(),
|
|
116
|
+
data: import_zod.z.null()
|
|
117
|
+
});
|
|
118
|
+
function componentDocumentToRootEmbeddedDocument({
|
|
119
|
+
document,
|
|
120
|
+
documentKey,
|
|
121
|
+
name,
|
|
122
|
+
type,
|
|
123
|
+
meta
|
|
124
|
+
}) {
|
|
125
|
+
const { data: rootElement, locale, id } = document;
|
|
126
|
+
if (rootElement != null && rootElement.type !== type) {
|
|
127
|
+
throw new Error(
|
|
128
|
+
`Type "${rootElement.type}" does not match the expected type "${type}" from the snapshot`
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
const rootDocument = {
|
|
132
|
+
key: documentKey,
|
|
133
|
+
rootElement: rootElement ?? {
|
|
134
|
+
// Fallback rootElement
|
|
135
|
+
// Create a stable uuid so two different clients will have the same empty element data.
|
|
136
|
+
// This is needed to make presence feature work for an element that is not yet created.
|
|
137
|
+
key: (0, import_deterministic_uuid.deterministicUUID)({ id, locale, seed: documentKey }),
|
|
138
|
+
type,
|
|
139
|
+
props: {}
|
|
140
|
+
},
|
|
141
|
+
locale,
|
|
142
|
+
id,
|
|
143
|
+
type,
|
|
144
|
+
name,
|
|
145
|
+
meta,
|
|
146
|
+
__type: import_read_only_documents.EMBEDDED_DOCUMENT_TYPE
|
|
147
|
+
};
|
|
148
|
+
return rootDocument;
|
|
149
|
+
}
|
|
150
|
+
async function failedResponseBody(response) {
|
|
151
|
+
try {
|
|
152
|
+
const text = await response.text();
|
|
153
|
+
try {
|
|
154
|
+
return JSON.parse(text);
|
|
155
|
+
} catch {
|
|
156
|
+
return text;
|
|
157
|
+
}
|
|
158
|
+
} catch (e) {
|
|
159
|
+
return `Failed to extract response body: ${e}`;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
function responseError(response) {
|
|
163
|
+
return `${response.status} ${response.statusText}`;
|
|
164
|
+
}
|
|
165
|
+
const pagePathnameSlicesAPISchema = import_zod.z.array(
|
|
166
|
+
import_zod.z.object({
|
|
167
|
+
id: import_zod.z.string(),
|
|
168
|
+
basePageId: import_zod.z.string(),
|
|
169
|
+
pathname: import_zod.z.string(),
|
|
170
|
+
localizedPathname: import_zod.z.string().optional(),
|
|
171
|
+
__typename: import_zod.z.literal("PagePathnameSlice")
|
|
172
|
+
}).nullable()
|
|
173
|
+
);
|
|
174
|
+
const getPageAPISchema = import_zod.z.object({
|
|
175
|
+
pathname: import_zod.z.string(),
|
|
176
|
+
locale: import_zod.z.string(),
|
|
177
|
+
alternate: import_zod.z.array(
|
|
178
|
+
import_zod.z.object({
|
|
179
|
+
pathname: import_zod.z.string(),
|
|
180
|
+
locale: import_zod.z.string()
|
|
181
|
+
})
|
|
182
|
+
)
|
|
183
|
+
});
|
|
184
|
+
class MakeswiftClient {
|
|
185
|
+
graphqlClient;
|
|
186
|
+
runtime;
|
|
187
|
+
apiKey;
|
|
188
|
+
apiOrigin;
|
|
189
|
+
constructor(apiKey, { apiOrigin = "https://api.makeswift.com", runtime }) {
|
|
190
|
+
if (typeof apiKey !== "string") {
|
|
191
|
+
throw new Error(
|
|
192
|
+
`The Makeswift client must be passed a valid Makeswift site API key: \`new Makeswift('<makeswift_site_api_key>')\`
|
|
193
|
+
Received "${apiKey}" instead.`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
this.apiKey = apiKey;
|
|
197
|
+
try {
|
|
198
|
+
this.apiOrigin = new URL(apiOrigin);
|
|
199
|
+
} catch {
|
|
200
|
+
throw new Error(
|
|
201
|
+
`The Makeswift client received an invalid \`apiOrigin\` parameter: "${apiOrigin}".`
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
this.graphqlClient = new import_client.GraphQLClient(new URL("graphql", apiOrigin).href);
|
|
205
|
+
this.runtime = runtime;
|
|
206
|
+
}
|
|
207
|
+
async fetch(path, siteVersion) {
|
|
208
|
+
const response = await fetch(new URL(path, this.apiOrigin).toString(), {
|
|
209
|
+
headers: {
|
|
210
|
+
["X-API-Key"]: this.apiKey,
|
|
211
|
+
"Makeswift-Site-API-Key": this.apiKey,
|
|
212
|
+
"Makeswift-Site-Version": siteVersion
|
|
213
|
+
},
|
|
214
|
+
...siteVersion === import_site_version.MakeswiftSiteVersion.Working ? { cache: "no-store" } : {},
|
|
215
|
+
...this.fetchOptions(siteVersion)
|
|
216
|
+
});
|
|
217
|
+
return response;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Override this method to provide additional fetch options, e.g. revalidation, cache tags, etc.
|
|
221
|
+
*/
|
|
222
|
+
fetchOptions(_siteVersion) {
|
|
223
|
+
return {};
|
|
224
|
+
}
|
|
225
|
+
getPagesInternal = async ({
|
|
226
|
+
siteVersion = import_site_version.MakeswiftSiteVersion.Live,
|
|
227
|
+
...params
|
|
228
|
+
} = {}) => {
|
|
229
|
+
const queryParams = getPagesQueryParams(params);
|
|
230
|
+
const response = await this.fetch(`v4/pages?${queryParams.toString()}`, siteVersion);
|
|
231
|
+
if (!response.ok) {
|
|
232
|
+
console.error("Failed to get pages", {
|
|
233
|
+
response: await failedResponseBody(response),
|
|
234
|
+
siteVersion,
|
|
235
|
+
params
|
|
236
|
+
});
|
|
237
|
+
throw new Error(`Failed to get pages: ${responseError(response)}`);
|
|
238
|
+
}
|
|
239
|
+
const result = await response.json();
|
|
240
|
+
const parsedResponse = makeswiftGetPagesResultAPISchema.safeParse(result);
|
|
241
|
+
if (!parsedResponse.success) {
|
|
242
|
+
throw new Error(
|
|
243
|
+
`Failed to parse 'getPages' response: ${parsedResponse.error.errors.map((e) => e.message).join("; ")}`
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
return parsedResponse.data;
|
|
247
|
+
};
|
|
248
|
+
getPages = (0, import_pagination.toIterablePaginationResult)(this.getPagesInternal);
|
|
249
|
+
async getPage(pathname, {
|
|
250
|
+
siteVersion = import_site_version.MakeswiftSiteVersion.Live,
|
|
251
|
+
locale
|
|
252
|
+
} = {}) {
|
|
253
|
+
const url = new URL(`v2/pages/${encodeURIComponent(pathname)}`, this.apiOrigin);
|
|
254
|
+
if (locale)
|
|
255
|
+
url.searchParams.set("locale", locale);
|
|
256
|
+
const response = await this.fetch(url.pathname + url.search, siteVersion);
|
|
257
|
+
if (!response.ok) {
|
|
258
|
+
if (response.status === 404)
|
|
259
|
+
return null;
|
|
260
|
+
console.error(`Failed to get page snapshot for '${pathname}'`, {
|
|
261
|
+
response: await failedResponseBody(response),
|
|
262
|
+
siteVersion,
|
|
263
|
+
locale
|
|
264
|
+
});
|
|
265
|
+
throw new Error(`Failed to get page snapshot for '${pathname}': ${responseError(response)}`);
|
|
266
|
+
}
|
|
267
|
+
const json = await response.json();
|
|
268
|
+
return getPageAPISchema.parse(json);
|
|
269
|
+
}
|
|
270
|
+
async getTypographies(typographyIds, siteVersion) {
|
|
271
|
+
if (typographyIds.length === 0)
|
|
272
|
+
return [];
|
|
273
|
+
const url = new URL(`v2/typographies/bulk`, this.apiOrigin);
|
|
274
|
+
typographyIds.forEach((id) => {
|
|
275
|
+
url.searchParams.append("ids", id);
|
|
276
|
+
});
|
|
277
|
+
const response = await this.fetch(url.pathname + url.search, siteVersion);
|
|
278
|
+
if (!response.ok) {
|
|
279
|
+
console.error(`Failed to get typographies for [${typographyIds.join(", ")}]`, {
|
|
280
|
+
response: await failedResponseBody(response),
|
|
281
|
+
siteVersion
|
|
282
|
+
});
|
|
283
|
+
return [];
|
|
284
|
+
}
|
|
285
|
+
const body = await response.json();
|
|
286
|
+
return body;
|
|
287
|
+
}
|
|
288
|
+
async getSwatches(ids, siteVersion) {
|
|
289
|
+
if (ids.length === 0)
|
|
290
|
+
return [];
|
|
291
|
+
const url = new URL(`v2/swatches/bulk`, this.apiOrigin);
|
|
292
|
+
ids.forEach((id) => {
|
|
293
|
+
url.searchParams.append("ids", id);
|
|
294
|
+
});
|
|
295
|
+
const response = await this.fetch(url.pathname + url.search, siteVersion);
|
|
296
|
+
if (!response.ok) {
|
|
297
|
+
console.error(`Failed to get swatches for ${ids.join(", ")}`, {
|
|
298
|
+
response: await failedResponseBody(response),
|
|
299
|
+
siteVersion
|
|
300
|
+
});
|
|
301
|
+
return [];
|
|
302
|
+
}
|
|
303
|
+
return await response.json();
|
|
304
|
+
}
|
|
305
|
+
async getIntrospectedResources({
|
|
306
|
+
swatchIds,
|
|
307
|
+
...introspectedResourceIds
|
|
308
|
+
}, siteVersion) {
|
|
309
|
+
const result = await this.graphqlClient.request(import_documents.IntrospectedResourcesQuery, introspectedResourceIds);
|
|
310
|
+
const swatches = await this.getSwatches(swatchIds, siteVersion);
|
|
311
|
+
return { ...result, swatches };
|
|
312
|
+
}
|
|
313
|
+
async introspect(element, siteVersion, locale) {
|
|
314
|
+
const runtime = this.runtime;
|
|
315
|
+
const descriptors = (0, import_react_page.getPropControllerDescriptors)(runtime.store.getState());
|
|
316
|
+
const swatchIds = /* @__PURE__ */ new Set();
|
|
317
|
+
const fileIds = /* @__PURE__ */ new Set();
|
|
318
|
+
const typographyIds = /* @__PURE__ */ new Set();
|
|
319
|
+
const tableIds = /* @__PURE__ */ new Set();
|
|
320
|
+
const pageIds = /* @__PURE__ */ new Set();
|
|
321
|
+
const globalElements = /* @__PURE__ */ new Map();
|
|
322
|
+
const localizedGlobalElements = /* @__PURE__ */ new Map();
|
|
323
|
+
const localizedResourcesMap = /* @__PURE__ */ new Map();
|
|
324
|
+
const remaining = [element];
|
|
325
|
+
const seen = /* @__PURE__ */ new Set();
|
|
326
|
+
let current;
|
|
327
|
+
while (current = remaining.pop()) {
|
|
328
|
+
let getResourcesFromElementDescriptors2 = function(elementDescriptors2, props) {
|
|
329
|
+
Object.entries(elementDescriptors2).forEach(([propName, descriptor]) => {
|
|
330
|
+
(0, import_introspection.getSwatchIds)(descriptor, props[propName]).forEach((swatchId) => {
|
|
331
|
+
swatchIds.add(swatchId);
|
|
332
|
+
});
|
|
333
|
+
(0, import_introspection.getFileIds)(descriptor, props[propName]).forEach((fileId) => fileIds.add(fileId));
|
|
334
|
+
(0, import_introspection.getTypographyIds)(descriptor, props[propName]).forEach(
|
|
335
|
+
(typographyId) => typographyIds.add(typographyId)
|
|
336
|
+
);
|
|
337
|
+
(0, import_introspection.getTableIds)(descriptor, props[propName]).forEach((tableId) => tableIds.add(tableId));
|
|
338
|
+
(0, import_introspection.getPageIds)(descriptor, props[propName]).forEach((pageId) => pageIds.add(pageId));
|
|
339
|
+
(0, import_introspection.getElementChildren)(descriptor, props[propName]).forEach((child) => {
|
|
340
|
+
if (!seen.has(child.key)) {
|
|
341
|
+
seen.add(child.key);
|
|
342
|
+
remaining.push(child);
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
};
|
|
347
|
+
var getResourcesFromElementDescriptors = getResourcesFromElementDescriptors2;
|
|
348
|
+
let element2;
|
|
349
|
+
if ((0, import_react_page.isElementReference)(current)) {
|
|
350
|
+
const globalElementId = current.value;
|
|
351
|
+
const globalElement = await this.getGlobalElement(globalElementId, siteVersion);
|
|
352
|
+
let elementData = globalElement?.data;
|
|
353
|
+
if (locale) {
|
|
354
|
+
const localizedGlobalElement = await this.getLocalizedGlobalElement(
|
|
355
|
+
globalElementId,
|
|
356
|
+
locale,
|
|
357
|
+
siteVersion
|
|
358
|
+
);
|
|
359
|
+
if (localizedGlobalElement) {
|
|
360
|
+
elementData = localizedGlobalElement.data;
|
|
361
|
+
localizedResourcesMap.set(globalElementId, localizedGlobalElement.id);
|
|
362
|
+
localizedGlobalElements.set(localizedGlobalElement.id, localizedGlobalElement);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
globalElements.set(globalElementId, globalElement);
|
|
366
|
+
if (elementData == null)
|
|
367
|
+
continue;
|
|
368
|
+
element2 = elementData;
|
|
369
|
+
} else {
|
|
370
|
+
element2 = current;
|
|
371
|
+
}
|
|
372
|
+
const elementDescriptors = descriptors.get(element2.type);
|
|
373
|
+
if (elementDescriptors == null)
|
|
374
|
+
continue;
|
|
375
|
+
getResourcesFromElementDescriptors2(elementDescriptors, element2.props);
|
|
376
|
+
}
|
|
377
|
+
const typographies = await this.getTypographies([...typographyIds], siteVersion);
|
|
378
|
+
typographies.forEach((typography) => {
|
|
379
|
+
typography?.style.forEach((style) => {
|
|
380
|
+
const swatchId = style.value.color?.swatchId;
|
|
381
|
+
if (swatchId != null)
|
|
382
|
+
swatchIds.add(swatchId);
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
const pagePathnames = await this.getPagePathnameSlices([...pageIds], siteVersion, { locale });
|
|
386
|
+
const { swatches, files, tables } = await this.getIntrospectedResources(
|
|
387
|
+
{
|
|
388
|
+
swatchIds: [...swatchIds],
|
|
389
|
+
fileIds: [...fileIds],
|
|
390
|
+
tableIds: [...tableIds]
|
|
391
|
+
},
|
|
392
|
+
siteVersion
|
|
393
|
+
);
|
|
394
|
+
const apiResources = {
|
|
395
|
+
[import_api.APIResourceType.Swatch]: [...swatchIds].map((id) => ({
|
|
396
|
+
id,
|
|
397
|
+
value: swatches.find((swatch) => swatch?.id === id) ?? null
|
|
398
|
+
})),
|
|
399
|
+
[import_api.APIResourceType.File]: [...fileIds].map((id) => ({
|
|
400
|
+
id,
|
|
401
|
+
value: files.find((file) => file?.id === id) ?? null
|
|
402
|
+
})),
|
|
403
|
+
[import_api.APIResourceType.Typography]: [...typographyIds].map((id) => ({
|
|
404
|
+
id,
|
|
405
|
+
value: typographies.find((typography) => typography?.id === id) ?? null
|
|
406
|
+
})),
|
|
407
|
+
[import_api.APIResourceType.Table]: [...tableIds].map((id) => ({
|
|
408
|
+
id,
|
|
409
|
+
value: tables.find((table) => table?.id === id) ?? null
|
|
410
|
+
})),
|
|
411
|
+
[import_api.APIResourceType.PagePathnameSlice]: [...pageIds].map((id) => ({
|
|
412
|
+
id,
|
|
413
|
+
value: pagePathnames.find((pagePathnameSlice) => pagePathnameSlice?.id === id) ?? null,
|
|
414
|
+
locale
|
|
415
|
+
})),
|
|
416
|
+
[import_api.APIResourceType.GlobalElement]: [...globalElements.entries()].map(([id, value]) => ({
|
|
417
|
+
id,
|
|
418
|
+
value
|
|
419
|
+
})),
|
|
420
|
+
[import_api.APIResourceType.LocalizedGlobalElement]: [...localizedGlobalElements.entries()].map(
|
|
421
|
+
([id, value]) => ({
|
|
422
|
+
id,
|
|
423
|
+
value,
|
|
424
|
+
locale
|
|
425
|
+
})
|
|
426
|
+
)
|
|
427
|
+
};
|
|
428
|
+
return {
|
|
429
|
+
apiResources,
|
|
430
|
+
localizedResourcesMap: locale != null ? { [locale]: Object.fromEntries(localizedResourcesMap.entries()) } : {}
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
async getPageSnapshot(pathname, {
|
|
434
|
+
siteVersion: siteVersionPromise,
|
|
435
|
+
locale,
|
|
436
|
+
allowLocaleFallback = true
|
|
437
|
+
}) {
|
|
438
|
+
const queryParams = () => {
|
|
439
|
+
const params = new URLSearchParams();
|
|
440
|
+
if (locale)
|
|
441
|
+
params.set("locale", locale);
|
|
442
|
+
if (allowLocaleFallback != null)
|
|
443
|
+
params.set("allowLocaleFallback", `${allowLocaleFallback}`);
|
|
444
|
+
return params.toString();
|
|
445
|
+
};
|
|
446
|
+
const siteVersion = await siteVersionPromise;
|
|
447
|
+
const response = await this.fetch(
|
|
448
|
+
`v3/pages/${encodeURIComponent(pathname)}/document?${queryParams()}`,
|
|
449
|
+
siteVersion
|
|
450
|
+
);
|
|
451
|
+
if (!response.ok) {
|
|
452
|
+
if (response.status === 404)
|
|
453
|
+
return null;
|
|
454
|
+
console.error(`Failed to get page snapshot for '${pathname}'`, {
|
|
455
|
+
response: await failedResponseBody(response),
|
|
456
|
+
siteVersion,
|
|
457
|
+
locale
|
|
458
|
+
});
|
|
459
|
+
throw new Error(`Failed to get page snapshot for '${pathname}': ${responseError(response)}`);
|
|
460
|
+
}
|
|
461
|
+
const document = await response.json();
|
|
462
|
+
const baseLocalizedPage = document.localizedPages.find(({ parentId }) => parentId == null);
|
|
463
|
+
const cacheData = await this.introspect(
|
|
464
|
+
baseLocalizedPage?.data ?? document.data,
|
|
465
|
+
siteVersion,
|
|
466
|
+
// The /v3/pages endpoint returns null for document.locale when the requested locale is the default.
|
|
467
|
+
// This legacy behavior is set to change with the upcoming /v4/pages endpoint.
|
|
468
|
+
// We rely on document.locale when reading from the API cache, so ensure the cache is built during
|
|
469
|
+
// introspection using the same value.
|
|
470
|
+
document.locale
|
|
471
|
+
);
|
|
472
|
+
return {
|
|
473
|
+
document,
|
|
474
|
+
cacheData
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
async getComponentSnapshot(id, {
|
|
478
|
+
siteVersion: siteVersionPromise,
|
|
479
|
+
locale,
|
|
480
|
+
allowLocaleFallback = true
|
|
481
|
+
}) {
|
|
482
|
+
const searchParams = new URLSearchParams();
|
|
483
|
+
if (locale)
|
|
484
|
+
searchParams.set("locale", locale);
|
|
485
|
+
const siteVersion = await siteVersionPromise;
|
|
486
|
+
const key = (0, import_deterministic_uuid.deterministicUUID)({ id, locale, seed: this.apiKey.split("-").at(0) });
|
|
487
|
+
const baseLocaleWasRequested = locale == null;
|
|
488
|
+
const canAttemptLocaleFallback = !baseLocaleWasRequested && allowLocaleFallback;
|
|
489
|
+
let response;
|
|
490
|
+
const responseForRequestedLocale = await this.fetch(
|
|
491
|
+
`v1/element-trees/${encodeURIComponent(id)}?${searchParams.toString()}`,
|
|
492
|
+
siteVersion
|
|
493
|
+
);
|
|
494
|
+
if (responseForRequestedLocale.status === 404 && canAttemptLocaleFallback) {
|
|
495
|
+
response = await this.fetch(`v1/element-trees/${encodeURIComponent(id)}`, siteVersion);
|
|
496
|
+
} else {
|
|
497
|
+
response = responseForRequestedLocale;
|
|
498
|
+
}
|
|
499
|
+
if (!response.ok) {
|
|
500
|
+
if (response.status === 404) {
|
|
501
|
+
return {
|
|
502
|
+
document: {
|
|
503
|
+
id,
|
|
504
|
+
locale: locale ?? null,
|
|
505
|
+
data: null
|
|
506
|
+
},
|
|
507
|
+
key,
|
|
508
|
+
cacheData: import_react.CacheData.empty(),
|
|
509
|
+
meta: {
|
|
510
|
+
allowLocaleFallback,
|
|
511
|
+
requestedLocale: locale ?? null
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
console.error(`Failed to get component snapshot for '${id}':`, {
|
|
516
|
+
response: await failedResponseBody(response),
|
|
517
|
+
siteVersion,
|
|
518
|
+
locale
|
|
519
|
+
});
|
|
520
|
+
throw new Error(`Failed to get component snapshot for '${id}': ${responseError(response)}`);
|
|
521
|
+
}
|
|
522
|
+
const document = makeswiftComponentDocumentSchema.parse(await response.json());
|
|
523
|
+
const cacheData = await this.introspect(document.data, siteVersion, locale ?? null);
|
|
524
|
+
return {
|
|
525
|
+
document,
|
|
526
|
+
cacheData,
|
|
527
|
+
key,
|
|
528
|
+
meta: {
|
|
529
|
+
allowLocaleFallback,
|
|
530
|
+
requestedLocale: locale ?? null
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
async getSwatch(swatchId, siteVersion) {
|
|
535
|
+
const response = await this.fetch(`v2/swatches/${swatchId}`, siteVersion);
|
|
536
|
+
if (!response.ok) {
|
|
537
|
+
if (response.status !== 404) {
|
|
538
|
+
console.error(`Failed to get swatch '${swatchId}'`, {
|
|
539
|
+
response: await failedResponseBody(response),
|
|
540
|
+
siteVersion
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
return null;
|
|
544
|
+
}
|
|
545
|
+
const swatch = await response.json();
|
|
546
|
+
return swatch;
|
|
547
|
+
}
|
|
548
|
+
async getFile(fileId) {
|
|
549
|
+
const result = await this.graphqlClient.request(
|
|
550
|
+
import_documents.FileQuery,
|
|
551
|
+
{ fileId }
|
|
552
|
+
);
|
|
553
|
+
return result.file;
|
|
554
|
+
}
|
|
555
|
+
async getTypography(typographyId, siteVersion) {
|
|
556
|
+
const response = await this.fetch(`v2/typographies/${typographyId}`, siteVersion);
|
|
557
|
+
if (!response.ok) {
|
|
558
|
+
if (response.status !== 404) {
|
|
559
|
+
console.error(`Failed to get typography '${typographyId}'`, {
|
|
560
|
+
response: await failedResponseBody(response),
|
|
561
|
+
siteVersion
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
return null;
|
|
565
|
+
}
|
|
566
|
+
const typography = await response.json();
|
|
567
|
+
return typography;
|
|
568
|
+
}
|
|
569
|
+
async getGlobalElement(globalElementId, siteVersion) {
|
|
570
|
+
const response = await this.fetch(`v2/global-elements/${globalElementId}`, siteVersion);
|
|
571
|
+
if (!response.ok) {
|
|
572
|
+
if (response.status !== 404) {
|
|
573
|
+
console.error(`Failed to get global element '${globalElementId}'`, {
|
|
574
|
+
response: await failedResponseBody(response),
|
|
575
|
+
siteVersion
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
return null;
|
|
579
|
+
}
|
|
580
|
+
const globalElement = await response.json();
|
|
581
|
+
return globalElement;
|
|
582
|
+
}
|
|
583
|
+
async getLocalizedGlobalElement(globalElementId, locale, siteVersion) {
|
|
584
|
+
const response = await this.fetch(
|
|
585
|
+
`v2/localized-global-elements/${globalElementId}?locale=${locale}`,
|
|
586
|
+
siteVersion
|
|
587
|
+
);
|
|
588
|
+
if (!response.ok) {
|
|
589
|
+
if (response.status !== 404) {
|
|
590
|
+
console.error(`Failed to get localized global element '${globalElementId}'`, {
|
|
591
|
+
response: await failedResponseBody(response),
|
|
592
|
+
siteVersion,
|
|
593
|
+
locale
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
return null;
|
|
597
|
+
}
|
|
598
|
+
const localizedGlobalElement = await response.json();
|
|
599
|
+
return localizedGlobalElement;
|
|
600
|
+
}
|
|
601
|
+
async getPagePathnameSlices(pageIds, siteVersion, { locale }) {
|
|
602
|
+
if (pageIds.length === 0)
|
|
603
|
+
return [];
|
|
604
|
+
const url = new URL(`v2/page-pathname-slices/bulk`, this.apiOrigin);
|
|
605
|
+
pageIds.forEach((id) => url.searchParams.append("ids", id));
|
|
606
|
+
if (locale != null)
|
|
607
|
+
url.searchParams.set("locale", locale);
|
|
608
|
+
const response = await this.fetch(url.pathname + url.search, siteVersion);
|
|
609
|
+
if (!response.ok) {
|
|
610
|
+
console.error(`Failed to get page pathname slice(s) for ${pageIds.join(", ")}`, {
|
|
611
|
+
response: await failedResponseBody(response),
|
|
612
|
+
siteVersion,
|
|
613
|
+
locale
|
|
614
|
+
});
|
|
615
|
+
return [];
|
|
616
|
+
}
|
|
617
|
+
const json = await response.json();
|
|
618
|
+
const pagePathnameSlices = pagePathnameSlicesAPISchema.parse(json);
|
|
619
|
+
return pagePathnameSlices.map((pagePathnameSlice) => {
|
|
620
|
+
if (pagePathnameSlice == null)
|
|
621
|
+
return null;
|
|
622
|
+
return {
|
|
623
|
+
...pagePathnameSlice,
|
|
624
|
+
id: pagePathnameSlice.basePageId,
|
|
625
|
+
localizedPathname: pagePathnameSlice.localizedPathname ?? null
|
|
626
|
+
};
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
async getPagePathnameSlice(pageId, siteVersion, { locale } = {}) {
|
|
630
|
+
const pagePathnameSlices = await this.getPagePathnameSlices([pageId], siteVersion, { locale });
|
|
631
|
+
return pagePathnameSlices.at(0) ?? null;
|
|
632
|
+
}
|
|
633
|
+
async getTable(tableId) {
|
|
634
|
+
const result = await this.graphqlClient.request(
|
|
635
|
+
import_documents.TableQuery,
|
|
636
|
+
{ tableId }
|
|
637
|
+
);
|
|
638
|
+
return result.table;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* @deprecated `getSitemap` is deprecated. We recommend constructing a sitemap
|
|
642
|
+
* using data from `getPages` instead.
|
|
643
|
+
*/
|
|
644
|
+
async getSitemap({
|
|
645
|
+
limit = 50,
|
|
646
|
+
after,
|
|
647
|
+
pathnamePrefix,
|
|
648
|
+
locale
|
|
649
|
+
} = {}) {
|
|
650
|
+
const url = new URL("v1/sitemap", this.apiOrigin);
|
|
651
|
+
url.searchParams.set("limit", limit.toString());
|
|
652
|
+
if (after != null)
|
|
653
|
+
url.searchParams.set("after", after);
|
|
654
|
+
if (pathnamePrefix != null)
|
|
655
|
+
url.searchParams.set("pathnamePrefix", pathnamePrefix);
|
|
656
|
+
if (locale != null)
|
|
657
|
+
url.searchParams.set("locale", locale);
|
|
658
|
+
const response = await this.fetch(url.pathname + url.search, import_site_version.MakeswiftSiteVersion.Live);
|
|
659
|
+
if (!response.ok) {
|
|
660
|
+
console.error("Failed to get sitemap ", {
|
|
661
|
+
response: await failedResponseBody(response),
|
|
662
|
+
limit,
|
|
663
|
+
after,
|
|
664
|
+
pathnamePrefix,
|
|
665
|
+
locale
|
|
666
|
+
});
|
|
667
|
+
throw new Error(`Failed to get sitemap with error: ${responseError(response)}`);
|
|
668
|
+
}
|
|
669
|
+
const sitemap = await response.json();
|
|
670
|
+
return sitemap;
|
|
671
|
+
}
|
|
672
|
+
getTranslatableData(elementTree) {
|
|
673
|
+
return this.runtime.getTranslatableData(elementTree);
|
|
674
|
+
}
|
|
675
|
+
mergeTranslatedData(elementTree, translatedData) {
|
|
676
|
+
return this.runtime.mergeTranslatedData(elementTree, translatedData);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
680
|
+
0 && (module.exports = {
|
|
681
|
+
MakeswiftClient,
|
|
682
|
+
componentDocumentToRootEmbeddedDocument,
|
|
683
|
+
failedResponseBody,
|
|
684
|
+
pageToRootDocument
|
|
685
|
+
});
|
|
686
|
+
//# sourceMappingURL=index.js.map
|