@makeswift/runtime 0.28.7-canary.4 → 0.28.7-canary.5
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/dist/cjs/api/graphql/documents/queries.js +6 -6
- package/dist/cjs/api/graphql/documents/queries.js.map +1 -1
- package/dist/cjs/api/graphql/generated/types.js.map +1 -1
- package/dist/cjs/api/graphql-api-client.js +23 -1
- package/dist/cjs/api/graphql-api-client.js.map +1 -1
- package/dist/cjs/api/{client.js → host-api-resources-client.js} +7 -7
- package/dist/cjs/api/host-api-resources-client.js.map +1 -0
- package/dist/cjs/api/rest-api-client.js +187 -0
- package/dist/cjs/api/rest-api-client.js.map +1 -0
- package/dist/cjs/api/schema/index.js +23 -0
- package/dist/cjs/api/schema/index.js.map +1 -0
- package/dist/cjs/api/schema/page-pathname-slice.js +38 -0
- package/dist/cjs/api/schema/page-pathname-slice.js.map +1 -0
- package/dist/cjs/api-handler/handlers/manifest.js +1 -1
- package/dist/cjs/client/component-snapshot.js +63 -0
- package/dist/cjs/client/component-snapshot.js.map +1 -0
- package/dist/cjs/client/index.js +102 -360
- package/dist/cjs/client/index.js.map +1 -1
- package/dist/cjs/client/page-snapshot.js +33 -0
- package/dist/cjs/client/page-snapshot.js.map +1 -0
- package/dist/cjs/client/schema/component-document.js +45 -0
- package/dist/cjs/client/schema/component-document.js.map +1 -0
- package/dist/cjs/client/schema/fonts.js +38 -0
- package/dist/cjs/client/schema/fonts.js.map +1 -0
- package/dist/cjs/client/schema/get-page.js +39 -0
- package/dist/cjs/client/schema/get-page.js.map +1 -0
- package/dist/cjs/client/schema/get-pages.js +68 -0
- package/dist/cjs/client/schema/get-pages.js.map +1 -0
- package/dist/cjs/client/schema/index.js +31 -0
- package/dist/cjs/client/schema/index.js.map +1 -0
- package/dist/cjs/client/schema/preview-token-payload.js +34 -0
- package/dist/cjs/client/schema/preview-token-payload.js.map +1 -0
- package/dist/cjs/next/components/tests/controls/color-control/fixtures.js.map +1 -1
- package/dist/cjs/next/components/tests/controls/page-control-prop-rendering.js.map +1 -1
- package/dist/cjs/next/components/tests/controls/rich-text-v2-control/fixtures.js.map +1 -1
- package/dist/cjs/next/testing/element-data.js +3 -3
- package/dist/cjs/next/testing/element-data.js.map +1 -1
- package/dist/cjs/runtimes/react/hooks/use-cache-data.js.map +1 -1
- package/dist/cjs/runtimes/react/runtime-core.js +2 -2
- package/dist/cjs/runtimes/react/runtime-core.js.map +1 -1
- package/dist/esm/api/graphql/documents/queries.js +4 -4
- package/dist/esm/api/graphql/documents/queries.js.map +1 -1
- package/dist/esm/api/graphql/generated/types.js.map +1 -1
- package/dist/esm/api/graphql-api-client.js +29 -2
- package/dist/esm/api/graphql-api-client.js.map +1 -1
- package/dist/esm/api/{client.js → host-api-resources-client.js} +3 -3
- package/dist/esm/api/host-api-resources-client.js.map +1 -0
- package/dist/esm/api/rest-api-client.js +152 -0
- package/dist/esm/api/rest-api-client.js.map +1 -0
- package/dist/esm/api/schema/index.js +2 -0
- package/dist/esm/api/schema/index.js.map +1 -0
- package/dist/esm/api/schema/page-pathname-slice.js +14 -0
- package/dist/esm/api/schema/page-pathname-slice.js.map +1 -0
- package/dist/esm/api-handler/handlers/manifest.js +1 -1
- package/dist/esm/client/component-snapshot.js +39 -0
- package/dist/esm/client/component-snapshot.js.map +1 -0
- package/dist/esm/client/index.js +67 -329
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/page-snapshot.js +9 -0
- package/dist/esm/client/page-snapshot.js.map +1 -0
- package/dist/esm/client/schema/component-document.js +20 -0
- package/dist/esm/client/schema/component-document.js.map +1 -0
- package/dist/esm/client/schema/fonts.js +14 -0
- package/dist/esm/client/schema/fonts.js.map +1 -0
- package/dist/esm/client/schema/get-page.js +15 -0
- package/dist/esm/client/schema/get-page.js.map +1 -0
- package/dist/esm/client/schema/get-pages.js +42 -0
- package/dist/esm/client/schema/get-pages.js.map +1 -0
- package/dist/esm/client/schema/index.js +6 -0
- package/dist/esm/client/schema/index.js.map +1 -0
- package/dist/esm/client/schema/preview-token-payload.js +10 -0
- package/dist/esm/client/schema/preview-token-payload.js.map +1 -0
- package/dist/esm/next/components/tests/controls/color-control/fixtures.js.map +1 -1
- package/dist/esm/next/components/tests/controls/page-control-prop-rendering.js.map +1 -1
- package/dist/esm/next/components/tests/controls/rich-text-v2-control/fixtures.js.map +1 -1
- package/dist/esm/next/testing/element-data.js +1 -1
- package/dist/esm/next/testing/element-data.js.map +1 -1
- package/dist/esm/runtimes/react/hooks/use-cache-data.js.map +1 -1
- package/dist/esm/runtimes/react/runtime-core.js +2 -2
- package/dist/esm/runtimes/react/runtime-core.js.map +1 -1
- package/dist/types/api/__tests__/rest-api-client.error-handling.test.d.ts +2 -0
- package/dist/types/api/__tests__/rest-api-client.error-handling.test.d.ts.map +1 -0
- package/dist/types/api/__tests__/rest-api-client.get-resources.test.d.ts +2 -0
- package/dist/types/api/__tests__/rest-api-client.get-resources.test.d.ts.map +1 -0
- package/dist/types/api/graphql/documents/queries.d.ts +1 -1
- package/dist/types/api/graphql/documents/queries.d.ts.map +1 -1
- package/dist/types/api/graphql/generated/types.d.ts +2 -2
- package/dist/types/api/graphql/generated/types.d.ts.map +1 -1
- package/dist/types/api/graphql-api-client.d.ts +5 -0
- package/dist/types/api/graphql-api-client.d.ts.map +1 -1
- package/dist/types/api/{client.d.ts → host-api-resources-client.d.ts} +2 -2
- package/dist/types/api/host-api-resources-client.d.ts.map +1 -0
- package/dist/types/api/rest-api-client.d.ts +27 -0
- package/dist/types/api/rest-api-client.d.ts.map +1 -0
- package/dist/types/api/schema/index.d.ts +2 -0
- package/dist/types/api/schema/index.d.ts.map +1 -0
- package/dist/types/api/schema/page-pathname-slice.d.ts +21 -0
- package/dist/types/api/schema/page-pathname-slice.d.ts.map +1 -0
- package/dist/types/client/component-snapshot.d.ts +25 -0
- package/dist/types/client/component-snapshot.d.ts.map +1 -0
- package/dist/types/client/index.d.ts +19 -640
- package/dist/types/client/index.d.ts.map +1 -1
- package/dist/types/client/page-snapshot.d.ts +62 -0
- package/dist/types/client/page-snapshot.d.ts.map +1 -0
- package/dist/types/client/schema/component-document.d.ts +285 -0
- package/dist/types/client/schema/component-document.d.ts.map +1 -0
- package/dist/types/client/schema/fonts.d.ts +27 -0
- package/dist/types/client/schema/fonts.d.ts.map +1 -0
- package/dist/types/client/schema/get-page.d.ts +30 -0
- package/dist/types/client/schema/get-page.d.ts.map +1 -0
- package/dist/types/client/schema/get-pages.d.ts +202 -0
- package/dist/types/client/schema/get-pages.d.ts.map +1 -0
- package/dist/types/client/schema/index.d.ts +6 -0
- package/dist/types/client/schema/index.d.ts.map +1 -0
- package/dist/types/client/schema/preview-token-payload.d.ts +19 -0
- package/dist/types/client/schema/preview-token-payload.d.ts.map +1 -0
- package/dist/types/next/components/tests/controls/color-control/fixtures.d.ts +1 -1
- package/dist/types/next/components/tests/controls/color-control/fixtures.d.ts.map +1 -1
- package/dist/types/next/components/tests/controls/page-control-prop-rendering.d.ts +1 -1
- package/dist/types/next/components/tests/controls/page-control-prop-rendering.d.ts.map +1 -1
- package/dist/types/next/components/tests/controls/rich-text-v2-control/fixtures.d.ts +1 -1
- package/dist/types/next/components/tests/controls/rich-text-v2-control/fixtures.d.ts.map +1 -1
- package/dist/types/runtimes/react/hooks/use-cache-data.d.ts +1 -1
- package/dist/types/runtimes/react/hooks/use-cache-data.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/cjs/api/client.js.map +0 -1
- package/dist/esm/api/client.js.map +0 -1
- package/dist/types/api/client.d.ts.map +0 -1
- package/dist/types/client/tests/client.error-handling.test.d.ts +0 -2
- package/dist/types/client/tests/client.error-handling.test.d.ts.map +0 -1
- package/dist/types/client/tests/client.get-resources.test.d.ts +0 -2
- package/dist/types/client/tests/client.get-resources.test.d.ts.map +0 -1
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { EMBEDDED_DOCUMENT_TYPE } from "../state/modules/read-only-documents";
|
|
2
|
+
import { deterministicUUID } from "../utils/deterministic-uuid";
|
|
3
|
+
function componentDocumentToRootEmbeddedDocument({
|
|
4
|
+
document,
|
|
5
|
+
documentKey,
|
|
6
|
+
name,
|
|
7
|
+
type,
|
|
8
|
+
description,
|
|
9
|
+
meta
|
|
10
|
+
}) {
|
|
11
|
+
const { data: rootElement, locale, id } = document;
|
|
12
|
+
if (rootElement != null && rootElement.type !== type) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
`Type "${rootElement.type}" does not match the expected type "${type}" from the snapshot`
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
const rootDocument = {
|
|
18
|
+
key: documentKey,
|
|
19
|
+
rootElement: rootElement ?? {
|
|
20
|
+
// Fallback rootElement
|
|
21
|
+
// Create a stable uuid so two different clients will have the same empty element data.
|
|
22
|
+
// This is needed to make presence feature work for an element that is not yet created.
|
|
23
|
+
key: deterministicUUID({ id, locale, seed: documentKey }),
|
|
24
|
+
type,
|
|
25
|
+
props: {}
|
|
26
|
+
},
|
|
27
|
+
locale,
|
|
28
|
+
id,
|
|
29
|
+
type,
|
|
30
|
+
name,
|
|
31
|
+
meta: { ...meta, description },
|
|
32
|
+
__type: EMBEDDED_DOCUMENT_TYPE
|
|
33
|
+
};
|
|
34
|
+
return rootDocument;
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
componentDocumentToRootEmbeddedDocument
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=component-snapshot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/client/component-snapshot.ts"],"sourcesContent":["import { z } from 'zod'\n\nimport { type CacheData } from '../api/api-resources-client'\n\nimport { EMBEDDED_DOCUMENT_TYPE, type EmbeddedDocument } from '../state/modules/read-only-documents'\n\nimport { deterministicUUID } from '../utils/deterministic-uuid'\n\nimport * as Schema from './schema/component-document'\n\nexport type MakeswiftComponentDocument = z.infer<typeof Schema.componentDocument>\nexport type MakeswiftComponentDocumentFallback = z.infer<typeof Schema.componentDocumentFallback>\n\nexport type MakeswiftComponentMetadata = {\n allowLocaleFallback: boolean\n requestedLocale: string | null\n}\n\nexport function componentDocumentToRootEmbeddedDocument({\n document,\n documentKey,\n name,\n type,\n description,\n meta,\n}: {\n document: MakeswiftComponentDocument | MakeswiftComponentDocumentFallback\n documentKey: string\n name: string\n type: string\n description?: string\n meta: MakeswiftComponentMetadata\n}): EmbeddedDocument {\n const { data: rootElement, locale, id } = document\n\n if (rootElement != null && rootElement.type !== type) {\n throw new Error(\n `Type \"${rootElement.type}\" does not match the expected type \"${type}\" from the snapshot`,\n )\n }\n\n const rootDocument: EmbeddedDocument = {\n key: documentKey,\n rootElement: rootElement ?? {\n // Fallback rootElement\n // Create a stable uuid so two different clients will have the same empty element data.\n // This is needed to make presence feature work for an element that is not yet created.\n key: deterministicUUID({ id, locale, seed: documentKey }),\n type,\n props: {},\n },\n locale,\n id,\n type,\n name,\n meta: { ...meta, description },\n __type: EMBEDDED_DOCUMENT_TYPE,\n }\n\n return rootDocument\n}\n\nexport type MakeswiftComponentSnapshot = {\n document: MakeswiftComponentDocument | MakeswiftComponentDocumentFallback\n key: string\n cacheData: CacheData\n meta: MakeswiftComponentMetadata\n}\n"],"mappings":"AAIA,SAAS,8BAAqD;AAE9D,SAAS,yBAAyB;AAY3B,SAAS,wCAAwC;AAAA,EACtD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAOqB;AACnB,QAAM,EAAE,MAAM,aAAa,QAAQ,GAAG,IAAI;AAE1C,MAAI,eAAe,QAAQ,YAAY,SAAS,MAAM;AACpD,UAAM,IAAI;AAAA,MACR,SAAS,YAAY,IAAI,uCAAuC,IAAI;AAAA,IACtE;AAAA,EACF;AAEA,QAAM,eAAiC;AAAA,IACrC,KAAK;AAAA,IACL,aAAa,eAAe;AAAA;AAAA;AAAA;AAAA,MAI1B,KAAK,kBAAkB,EAAE,IAAI,QAAQ,MAAM,YAAY,CAAC;AAAA,MACxD;AAAA,MACA,OAAO,CAAC;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,MAAM,EAAE,GAAG,MAAM,YAAY;AAAA,IAC7B,QAAQ;AAAA,EACV;AAEA,SAAO;AACT;","names":[]}
|
package/dist/esm/client/index.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
1
|
import {
|
|
3
2
|
APIResourceType
|
|
4
|
-
} from "../api";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
3
|
+
} from "../api/types";
|
|
4
|
+
import { CacheData } from "../api/api-resources-client";
|
|
5
|
+
import { MakeswiftGraphQLApiClient } from "../api/graphql-api-client";
|
|
6
|
+
import { MakeswiftRestAPIClient, failedResponseBody } from "../api/rest-api-client";
|
|
8
7
|
import {
|
|
9
8
|
getElementChildren,
|
|
10
9
|
getSwatchIds,
|
|
@@ -17,223 +16,39 @@ import {
|
|
|
17
16
|
getPropControllerDescriptors,
|
|
18
17
|
isElementReference
|
|
19
18
|
} from "../state/read-only-state";
|
|
20
|
-
import { toIterablePaginationResult } from "../utils/pagination";
|
|
21
|
-
import { deterministicUUID } from "../utils/deterministic-uuid";
|
|
22
|
-
import { Schema } from "@makeswift/controls";
|
|
23
|
-
import { EMBEDDED_DOCUMENT_TYPE } from "../state/modules/read-only-documents";
|
|
24
19
|
import { mergeTranslatedContent } from "../state/translations/merge";
|
|
25
20
|
import { getTranslatableContent } from "../state/translations/get";
|
|
21
|
+
import { deterministicUUID } from "../utils/deterministic-uuid";
|
|
22
|
+
import { toIterablePaginationResult } from "../utils/pagination";
|
|
26
23
|
import { isNonNullable } from "../utils/isNonNullable";
|
|
27
|
-
import
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
sitemapFrequency: z.enum(["always", "hourly", "daily", "weekly", "monthly", "yearly", "never"]).nullable(),
|
|
37
|
-
createdAt: z.string(),
|
|
38
|
-
updatedAt: z.string(),
|
|
39
|
-
publishedAt: z.string().nullable(),
|
|
40
|
-
isOnline: z.boolean().nullable(),
|
|
41
|
-
excludedFromSearch: z.boolean().nullable(),
|
|
42
|
-
locale: z.string(),
|
|
43
|
-
localizedVariants: z.array(
|
|
44
|
-
z.object({
|
|
45
|
-
locale: z.string(),
|
|
46
|
-
path: z.string()
|
|
47
|
-
})
|
|
48
|
-
)
|
|
49
|
-
});
|
|
50
|
-
const makeswiftGetPagesResultAPISchema = z.object({
|
|
51
|
-
data: z.array(makeswiftPageResultSchema),
|
|
52
|
-
hasMore: z.boolean()
|
|
53
|
-
});
|
|
54
|
-
const makeswiftGetPagesParamsSchema = z.object({
|
|
55
|
-
limit: z.number().optional(),
|
|
56
|
-
after: z.string().optional(),
|
|
57
|
-
sortBy: z.enum(["title", "path", "description", "createdAt", "updatedAt"]).optional(),
|
|
58
|
-
sortDirection: z.enum(["asc", "desc"]).optional(),
|
|
59
|
-
includeOffline: z.boolean().optional(),
|
|
60
|
-
pathPrefix: z.string().optional(),
|
|
61
|
-
locale: z.string().optional()
|
|
62
|
-
});
|
|
63
|
-
function getPagesQueryParams({
|
|
64
|
-
limit = 100,
|
|
65
|
-
after,
|
|
66
|
-
sortBy,
|
|
67
|
-
sortDirection,
|
|
68
|
-
includeOffline,
|
|
69
|
-
pathPrefix,
|
|
70
|
-
locale
|
|
71
|
-
}) {
|
|
72
|
-
const params = new URLSearchParams();
|
|
73
|
-
if (limit != null)
|
|
74
|
-
params.set("limit", limit.toString());
|
|
75
|
-
if (after != null)
|
|
76
|
-
params.set("after", after);
|
|
77
|
-
if (sortBy != null)
|
|
78
|
-
params.set("sortBy", sortBy);
|
|
79
|
-
if (sortDirection != null)
|
|
80
|
-
params.set("sortDirection", sortDirection);
|
|
81
|
-
if (includeOffline != null)
|
|
82
|
-
params.set("includeOffline", includeOffline.toString());
|
|
83
|
-
if (pathPrefix != null)
|
|
84
|
-
params.set("pathPrefix", pathPrefix);
|
|
85
|
-
if (locale != null)
|
|
86
|
-
params.set("locale", locale);
|
|
87
|
-
return params;
|
|
88
|
-
}
|
|
89
|
-
function pageToRootDocument(pageDocument) {
|
|
90
|
-
const { locale, localizedPages, id, data } = pageDocument;
|
|
91
|
-
const localizedPage = localizedPages.find(({ parentId }) => parentId == null);
|
|
92
|
-
return localizedPage ? { key: localizedPage.elementTreeId, rootElement: localizedPage.data, locale } : { key: id, rootElement: data, locale };
|
|
93
|
-
}
|
|
94
|
-
const makeswiftComponentDocumentSchema = z.object({
|
|
95
|
-
id: z.string(),
|
|
96
|
-
name: z.string().nullable(),
|
|
97
|
-
locale: z.string().nullable(),
|
|
98
|
-
data: Schema.element,
|
|
99
|
-
siteId: z.string(),
|
|
100
|
-
inheritsFromParent: z.boolean()
|
|
101
|
-
});
|
|
102
|
-
const makeswiftComponentDocumentFallbackSchema = z.object({
|
|
103
|
-
id: z.string(),
|
|
104
|
-
locale: z.string().nullable(),
|
|
105
|
-
data: z.null()
|
|
106
|
-
});
|
|
107
|
-
const previewTokenPayloadSchema = z.object({
|
|
108
|
-
payload: z.object({
|
|
109
|
-
version: z.string()
|
|
110
|
-
})
|
|
111
|
-
});
|
|
112
|
-
function componentDocumentToRootEmbeddedDocument({
|
|
113
|
-
document,
|
|
114
|
-
documentKey,
|
|
115
|
-
name,
|
|
116
|
-
type,
|
|
117
|
-
description,
|
|
118
|
-
meta
|
|
119
|
-
}) {
|
|
120
|
-
const { data: rootElement, locale, id } = document;
|
|
121
|
-
if (rootElement != null && rootElement.type !== type) {
|
|
122
|
-
throw new Error(
|
|
123
|
-
`Type "${rootElement.type}" does not match the expected type "${type}" from the snapshot`
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
const rootDocument = {
|
|
127
|
-
key: documentKey,
|
|
128
|
-
rootElement: rootElement ?? {
|
|
129
|
-
// Fallback rootElement
|
|
130
|
-
// Create a stable uuid so two different clients will have the same empty element data.
|
|
131
|
-
// This is needed to make presence feature work for an element that is not yet created.
|
|
132
|
-
key: deterministicUUID({ id, locale, seed: documentKey }),
|
|
133
|
-
type,
|
|
134
|
-
props: {}
|
|
135
|
-
},
|
|
136
|
-
locale,
|
|
137
|
-
id,
|
|
138
|
-
type,
|
|
139
|
-
name,
|
|
140
|
-
meta: { ...meta, description },
|
|
141
|
-
__type: EMBEDDED_DOCUMENT_TYPE
|
|
142
|
-
};
|
|
143
|
-
return rootDocument;
|
|
144
|
-
}
|
|
145
|
-
async function failedResponseBody(response) {
|
|
146
|
-
try {
|
|
147
|
-
const text = await response.text();
|
|
148
|
-
try {
|
|
149
|
-
return JSON.parse(text);
|
|
150
|
-
} catch {
|
|
151
|
-
return text;
|
|
152
|
-
}
|
|
153
|
-
} catch (e) {
|
|
154
|
-
return `Failed to extract response body: ${e}`;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
function responseError(response) {
|
|
158
|
-
return `${response.status} ${response.statusText}`;
|
|
159
|
-
}
|
|
160
|
-
const pagePathnameSlicesAPISchema = z.array(
|
|
161
|
-
z.object({
|
|
162
|
-
id: z.string(),
|
|
163
|
-
basePageId: z.string(),
|
|
164
|
-
pathname: z.string(),
|
|
165
|
-
localizedPathname: z.string().optional(),
|
|
166
|
-
__typename: z.literal("PagePathnameSlice")
|
|
167
|
-
}).nullable()
|
|
168
|
-
);
|
|
169
|
-
const getPageAPISchema = z.object({
|
|
170
|
-
pathname: z.string(),
|
|
171
|
-
locale: z.string(),
|
|
172
|
-
alternate: z.array(
|
|
173
|
-
z.object({
|
|
174
|
-
pathname: z.string(),
|
|
175
|
-
locale: z.string()
|
|
176
|
-
})
|
|
177
|
-
)
|
|
178
|
-
});
|
|
179
|
-
const getFontsAPISchema = z.object({
|
|
180
|
-
googleFonts: z.array(
|
|
181
|
-
z.object({
|
|
182
|
-
family: z.string(),
|
|
183
|
-
variants: z.array(z.string())
|
|
184
|
-
})
|
|
185
|
-
),
|
|
186
|
-
siteId: z.string()
|
|
187
|
-
});
|
|
188
|
-
class MakeswiftClient {
|
|
24
|
+
import * as Schema from "./schema";
|
|
25
|
+
import { SnippetLocation } from "../api/graphql/generated/types";
|
|
26
|
+
import {
|
|
27
|
+
componentDocumentToRootEmbeddedDocument
|
|
28
|
+
} from "./component-snapshot";
|
|
29
|
+
import {
|
|
30
|
+
pageToRootDocument
|
|
31
|
+
} from "./page-snapshot";
|
|
32
|
+
class MakeswiftClient extends MakeswiftRestAPIClient {
|
|
189
33
|
graphqlClient;
|
|
190
34
|
runtime;
|
|
191
|
-
apiKey
|
|
192
|
-
|
|
35
|
+
constructor(apiKey, {
|
|
36
|
+
runtime
|
|
37
|
+
}) {
|
|
193
38
|
if (typeof apiKey !== "string") {
|
|
194
39
|
throw new Error(
|
|
195
40
|
`The Makeswift client must be passed a valid Makeswift site API key: \`new Makeswift('<makeswift_site_api_key>')\`
|
|
196
41
|
Received "${apiKey}" instead.`
|
|
197
42
|
);
|
|
198
43
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
});
|
|
203
|
-
this.runtime = runtime;
|
|
204
|
-
}
|
|
205
|
-
get apiOrigin() {
|
|
206
|
-
return this.runtime.apiOrigin;
|
|
207
|
-
}
|
|
208
|
-
async fetch(path, siteVersion, init) {
|
|
209
|
-
const requestUrl = new URL(path, this.apiOrigin);
|
|
210
|
-
const requestHeaders = new Headers({
|
|
211
|
-
"x-api-key": this.apiKey,
|
|
212
|
-
"makeswift-site-api-key": this.apiKey,
|
|
213
|
-
"makeswift-runtime-version": "0.28.7-canary.4"
|
|
44
|
+
super({
|
|
45
|
+
apiKey,
|
|
46
|
+
apiOrigin: runtime.apiOrigin
|
|
214
47
|
});
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
requestHeaders.set("makeswift-preview-token", siteVersion.token);
|
|
218
|
-
}
|
|
219
|
-
if (init?.headers) {
|
|
220
|
-
new Headers(init.headers).forEach((value, key) => {
|
|
221
|
-
requestHeaders.set(key, value);
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
const response = await fetch(requestUrl.toString(), {
|
|
225
|
-
...init,
|
|
226
|
-
headers: requestHeaders,
|
|
227
|
-
...siteVersion != null ? { cache: "no-store" } : {},
|
|
228
|
-
...this.fetchOptions(siteVersion)
|
|
48
|
+
this.graphqlClient = new MakeswiftGraphQLApiClient({
|
|
49
|
+
endpoint: runtime.graphqlApiEndpoint
|
|
229
50
|
});
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* Override this method to provide additional fetch options, e.g. revalidation, cache tags, etc.
|
|
234
|
-
*/
|
|
235
|
-
fetchOptions(_siteVersion) {
|
|
236
|
-
return {};
|
|
51
|
+
this.runtime = runtime;
|
|
237
52
|
}
|
|
238
53
|
getPagesInternal = async ({
|
|
239
54
|
siteVersion = null,
|
|
@@ -250,7 +65,7 @@ Received "${apiKey}" instead.`
|
|
|
250
65
|
throw new Error(`Failed to get pages: ${responseError(response)}`);
|
|
251
66
|
}
|
|
252
67
|
const result = await response.json();
|
|
253
|
-
const parsedResponse =
|
|
68
|
+
const parsedResponse = Schema.getPagesResult.safeParse(result);
|
|
254
69
|
if (!parsedResponse.success) {
|
|
255
70
|
throw new Error(
|
|
256
71
|
`Failed to parse 'getPages' response: ${parsedResponse.error.errors.map((e) => e.message).join("; ")}`
|
|
@@ -276,7 +91,7 @@ Received "${apiKey}" instead.`
|
|
|
276
91
|
throw new Error(`Failed to get page snapshot for '${pathname}': ${responseError(response)}`);
|
|
277
92
|
}
|
|
278
93
|
const json = await response.json();
|
|
279
|
-
return
|
|
94
|
+
return Schema.getPageResult.parse(json);
|
|
280
95
|
}
|
|
281
96
|
async getTypographies(typographyIds, siteVersion) {
|
|
282
97
|
if (typographyIds.length === 0)
|
|
@@ -337,16 +152,16 @@ Received "${apiKey}" instead.`
|
|
|
337
152
|
}
|
|
338
153
|
const responseBody = await response.json();
|
|
339
154
|
return responseBody.map(
|
|
340
|
-
(item) => item != null ?
|
|
155
|
+
(item) => item != null ? Schema.componentDocument.parse(item) : null
|
|
341
156
|
);
|
|
342
157
|
}
|
|
343
158
|
async getIntrospectedResources({
|
|
344
159
|
swatchIds,
|
|
345
|
-
...
|
|
160
|
+
...unversionedResourceIds
|
|
346
161
|
}, siteVersion) {
|
|
347
|
-
const
|
|
162
|
+
const unversionedResources = await this.graphqlClient.getUnversionedResources(unversionedResourceIds);
|
|
348
163
|
const swatches = await this.getSwatches(swatchIds, siteVersion);
|
|
349
|
-
return { ...
|
|
164
|
+
return { ...unversionedResources, swatches };
|
|
350
165
|
}
|
|
351
166
|
// TODO: Consolidate this method with the introspectMany method once the
|
|
352
167
|
// unstable_getComponentSnapshots method is stable and tested in production.
|
|
@@ -751,7 +566,7 @@ Received "${apiKey}" instead.`
|
|
|
751
566
|
});
|
|
752
567
|
throw new Error(`Failed to get component snapshot for '${id}': ${responseError(response)}`);
|
|
753
568
|
}
|
|
754
|
-
const document =
|
|
569
|
+
const document = Schema.componentDocument.parse(await response.json());
|
|
755
570
|
const cacheData = await this.introspect(document.data, siteVersion, locale ?? null);
|
|
756
571
|
return {
|
|
757
572
|
document,
|
|
@@ -815,115 +630,11 @@ Received "${apiKey}" instead.`
|
|
|
815
630
|
};
|
|
816
631
|
});
|
|
817
632
|
}
|
|
818
|
-
async getSwatch(swatchId, siteVersion) {
|
|
819
|
-
const response = await this.fetch(`v3/swatches/${swatchId}`, siteVersion);
|
|
820
|
-
if (!response.ok) {
|
|
821
|
-
const failedBody = await failedResponseBody(response);
|
|
822
|
-
if (response.status !== 404) {
|
|
823
|
-
console.error(`Failed to get swatch '${swatchId}'`, {
|
|
824
|
-
response: failedBody,
|
|
825
|
-
siteVersion
|
|
826
|
-
});
|
|
827
|
-
}
|
|
828
|
-
return null;
|
|
829
|
-
}
|
|
830
|
-
const swatch = await response.json();
|
|
831
|
-
return swatch;
|
|
832
|
-
}
|
|
833
633
|
async getFile(fileId) {
|
|
834
|
-
|
|
835
|
-
FileQuery,
|
|
836
|
-
{ fileId }
|
|
837
|
-
);
|
|
838
|
-
return result.file;
|
|
839
|
-
}
|
|
840
|
-
async getTypography(typographyId, siteVersion) {
|
|
841
|
-
const response = await this.fetch(`v3/typographies/${typographyId}`, siteVersion);
|
|
842
|
-
if (!response.ok) {
|
|
843
|
-
const failedBody = await failedResponseBody(response);
|
|
844
|
-
if (response.status !== 404) {
|
|
845
|
-
console.error(`Failed to get typography '${typographyId}'`, {
|
|
846
|
-
response: failedBody,
|
|
847
|
-
siteVersion
|
|
848
|
-
});
|
|
849
|
-
}
|
|
850
|
-
return null;
|
|
851
|
-
}
|
|
852
|
-
const typography = await response.json();
|
|
853
|
-
return typography;
|
|
854
|
-
}
|
|
855
|
-
async getGlobalElement(globalElementId, siteVersion) {
|
|
856
|
-
const response = await this.fetch(`v3/global-elements/${globalElementId}`, siteVersion);
|
|
857
|
-
if (!response.ok) {
|
|
858
|
-
const failedBody = await failedResponseBody(response);
|
|
859
|
-
if (response.status !== 404) {
|
|
860
|
-
console.error(`Failed to get global element '${globalElementId}'`, {
|
|
861
|
-
response: failedBody,
|
|
862
|
-
siteVersion
|
|
863
|
-
});
|
|
864
|
-
}
|
|
865
|
-
return null;
|
|
866
|
-
}
|
|
867
|
-
const globalElement = await response.json();
|
|
868
|
-
return globalElement;
|
|
869
|
-
}
|
|
870
|
-
async getLocalizedGlobalElement(globalElementId, locale, siteVersion) {
|
|
871
|
-
const response = await this.fetch(
|
|
872
|
-
`v3/localized-global-elements/${globalElementId}?locale=${locale}`,
|
|
873
|
-
siteVersion
|
|
874
|
-
);
|
|
875
|
-
if (!response.ok) {
|
|
876
|
-
const failedBody = await failedResponseBody(response);
|
|
877
|
-
if (response.status !== 404) {
|
|
878
|
-
console.error(`Failed to get localized global element '${globalElementId}'`, {
|
|
879
|
-
response: failedBody,
|
|
880
|
-
siteVersion,
|
|
881
|
-
locale
|
|
882
|
-
});
|
|
883
|
-
}
|
|
884
|
-
return null;
|
|
885
|
-
}
|
|
886
|
-
const localizedGlobalElement = await response.json();
|
|
887
|
-
return localizedGlobalElement;
|
|
888
|
-
}
|
|
889
|
-
async getPagePathnameSlices(pageIds, siteVersion, { locale }) {
|
|
890
|
-
if (pageIds.length === 0)
|
|
891
|
-
return [];
|
|
892
|
-
const url = new URL(`v3/page-pathname-slices/bulk`, this.apiOrigin);
|
|
893
|
-
pageIds.forEach((id) => url.searchParams.append("ids", id));
|
|
894
|
-
if (locale != null)
|
|
895
|
-
url.searchParams.set("locale", locale);
|
|
896
|
-
const response = await this.fetch(url.pathname + url.search, siteVersion);
|
|
897
|
-
if (!response.ok) {
|
|
898
|
-
console.error(`Failed to get page pathname slice(s) for ${pageIds.join(", ")}`, {
|
|
899
|
-
response: await failedResponseBody(response),
|
|
900
|
-
siteVersion,
|
|
901
|
-
locale
|
|
902
|
-
});
|
|
903
|
-
return [];
|
|
904
|
-
}
|
|
905
|
-
const json = await response.json();
|
|
906
|
-
const pagePathnameSlices = pagePathnameSlicesAPISchema.parse(json);
|
|
907
|
-
return pagePathnameSlices.map((pagePathnameSlice) => {
|
|
908
|
-
if (pagePathnameSlice == null)
|
|
909
|
-
return null;
|
|
910
|
-
return {
|
|
911
|
-
...pagePathnameSlice,
|
|
912
|
-
id: pagePathnameSlice.basePageId,
|
|
913
|
-
localizedPathname: pagePathnameSlice.localizedPathname ?? null
|
|
914
|
-
};
|
|
915
|
-
});
|
|
916
|
-
}
|
|
917
|
-
async getPagePathnameSlice(pageId, siteVersion, { locale } = {}) {
|
|
918
|
-
const pagePathnameSlices = await this.getPagePathnameSlices([pageId], siteVersion, { locale });
|
|
919
|
-
return pagePathnameSlices.at(0) ?? null;
|
|
634
|
+
return this.graphqlClient.getFile(fileId);
|
|
920
635
|
}
|
|
921
636
|
async getTable(tableId) {
|
|
922
|
-
|
|
923
|
-
TableQuery,
|
|
924
|
-
{ tableId }
|
|
925
|
-
);
|
|
926
|
-
return result.table;
|
|
637
|
+
return this.graphqlClient.getTable(tableId);
|
|
927
638
|
}
|
|
928
639
|
getTranslatableData(elementTree) {
|
|
929
640
|
return getTranslatableContent(this.getElementDescriptors(), elementTree);
|
|
@@ -937,7 +648,7 @@ Received "${apiKey}" instead.`
|
|
|
937
648
|
headers: {
|
|
938
649
|
"x-api-key": this.apiKey,
|
|
939
650
|
"makeswift-site-api-key": this.apiKey,
|
|
940
|
-
"makeswift-runtime-version": "0.28.7-canary.
|
|
651
|
+
"makeswift-runtime-version": "0.28.7-canary.5",
|
|
941
652
|
"content-type": "application/json"
|
|
942
653
|
},
|
|
943
654
|
body: JSON.stringify({ token }),
|
|
@@ -957,7 +668,7 @@ Received "${apiKey}" instead.`
|
|
|
957
668
|
return null;
|
|
958
669
|
}
|
|
959
670
|
const json = await response.json();
|
|
960
|
-
const parsed =
|
|
671
|
+
const parsed = Schema.previewTokenPayload.safeParse(json);
|
|
961
672
|
if (!parsed.success) {
|
|
962
673
|
throw new Error(
|
|
963
674
|
`Failed to parse preview token payload: ${parsed.error.errors.map((e) => e.message).join("; ")}`
|
|
@@ -975,7 +686,7 @@ Received "${apiKey}" instead.`
|
|
|
975
686
|
return null;
|
|
976
687
|
}
|
|
977
688
|
const json = await response.json();
|
|
978
|
-
const parsed =
|
|
689
|
+
const parsed = Schema.fonts.safeParse(json);
|
|
979
690
|
if (!parsed.success) {
|
|
980
691
|
console.error("Failed to parse fonts API response", {
|
|
981
692
|
response: json,
|
|
@@ -989,12 +700,39 @@ Received "${apiKey}" instead.`
|
|
|
989
700
|
return getPropControllerDescriptors(this.runtime.protoStore.getState());
|
|
990
701
|
}
|
|
991
702
|
}
|
|
703
|
+
function getPagesQueryParams({
|
|
704
|
+
limit = 100,
|
|
705
|
+
after,
|
|
706
|
+
sortBy,
|
|
707
|
+
sortDirection,
|
|
708
|
+
includeOffline,
|
|
709
|
+
pathPrefix,
|
|
710
|
+
locale
|
|
711
|
+
}) {
|
|
712
|
+
const params = new URLSearchParams();
|
|
713
|
+
if (limit != null)
|
|
714
|
+
params.set("limit", limit.toString());
|
|
715
|
+
if (after != null)
|
|
716
|
+
params.set("after", after);
|
|
717
|
+
if (sortBy != null)
|
|
718
|
+
params.set("sortBy", sortBy);
|
|
719
|
+
if (sortDirection != null)
|
|
720
|
+
params.set("sortDirection", sortDirection);
|
|
721
|
+
if (includeOffline != null)
|
|
722
|
+
params.set("includeOffline", includeOffline.toString());
|
|
723
|
+
if (pathPrefix != null)
|
|
724
|
+
params.set("pathPrefix", pathPrefix);
|
|
725
|
+
if (locale != null)
|
|
726
|
+
params.set("locale", locale);
|
|
727
|
+
return params;
|
|
728
|
+
}
|
|
729
|
+
function responseError(response) {
|
|
730
|
+
return `${response.status} ${response.statusText}`;
|
|
731
|
+
}
|
|
992
732
|
export {
|
|
993
733
|
MakeswiftClient,
|
|
994
|
-
|
|
734
|
+
SnippetLocation,
|
|
995
735
|
componentDocumentToRootEmbeddedDocument,
|
|
996
|
-
|
|
997
|
-
pageToRootDocument,
|
|
998
|
-
previewTokenPayloadSchema
|
|
736
|
+
pageToRootDocument
|
|
999
737
|
};
|
|
1000
738
|
//# sourceMappingURL=index.js.map
|