@makeswift/runtime 0.12.4 → 0.13.1

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/index.es.js CHANGED
@@ -777,16 +777,11 @@ const getPageAPISchema = z.object({
777
777
  }))
778
778
  });
779
779
  class Makeswift {
780
- constructor(apiKey, {
781
- apiOrigin = "https://api.makeswift.com",
782
- runtime = ReactRuntime,
783
- siteVersion
784
- } = {}) {
780
+ constructor(apiKey, { apiOrigin = "https://api.makeswift.com", runtime = ReactRuntime } = {}) {
785
781
  __publicField(this, "apiKey");
786
782
  __publicField(this, "apiOrigin");
787
783
  __publicField(this, "graphqlClient");
788
784
  __publicField(this, "runtime");
789
- __publicField(this, "siteVersion");
790
785
  if (typeof apiKey !== "string") {
791
786
  throw new Error(`The Makeswift client must be passed a valid Makeswift site API key: \`new Makeswift('<makeswift_site_api_key>')\`
792
787
  Received "${apiKey}" instead.`);
@@ -799,29 +794,25 @@ Received "${apiKey}" instead.`);
799
794
  }
800
795
  this.graphqlClient = new GraphQLClient(new URL("graphql", apiOrigin).href);
801
796
  this.runtime = runtime;
802
- this.siteVersion = siteVersion != null ? siteVersion : null;
803
797
  }
804
798
  static getSiteVersion(previewData) {
805
799
  var _a;
806
800
  return (_a = getMakeswiftSiteVersion(previewData)) != null ? _a : MakeswiftSiteVersion.Live;
807
801
  }
808
- async fetch(path, init) {
809
- var _a;
802
+ async fetch(path, siteVersion = MakeswiftSiteVersion.Live, init) {
810
803
  const response = await fetch(new URL(path, this.apiOrigin).toString(), __spreadProps(__spreadValues({}, init), {
811
804
  headers: __spreadValues({
812
805
  ["X-API-Key"]: this.apiKey,
813
806
  "Makeswift-Site-API-Key": this.apiKey,
814
- "Makeswift-Site-Version": (_a = this.siteVersion) != null ? _a : MakeswiftSiteVersion.Live
807
+ "Makeswift-Site-Version": siteVersion
815
808
  }, init == null ? void 0 : init.headers)
816
809
  }));
817
810
  return response;
818
811
  }
819
- async getPages() {
820
- const response = await this.fetch(`/${this.siteVersion == null ? "v2" : "v3"}/pages`, {
821
- headers: {
822
- "Makeswift-Site-Version": MakeswiftSiteVersion.Live
823
- }
824
- });
812
+ async getPages({
813
+ siteVersion = MakeswiftSiteVersion.Live
814
+ } = {}) {
815
+ const response = await this.fetch(`v3/pages`, siteVersion);
825
816
  if (!response.ok) {
826
817
  console.error("Failed to get pages", await response.json());
827
818
  throw new Error(`Failed to get pages with error: "${response.statusText}"`);
@@ -830,17 +821,13 @@ Received "${apiKey}" instead.`);
830
821
  return json;
831
822
  }
832
823
  async getPage(pathname, {
833
- preview: previewOverride = false,
824
+ siteVersion = MakeswiftSiteVersion.Live,
834
825
  locale: localeInput
835
826
  } = {}) {
836
- var _a;
837
- const siteVersion = (_a = this.siteVersion) != null ? _a : previewOverride ? MakeswiftSiteVersion.Working : MakeswiftSiteVersion.Live;
838
- const url = new URL(`${this.siteVersion == null ? "v1" : "v2"}/pages/${encodeURIComponent(pathname)}`, this.apiOrigin);
827
+ const url = new URL(`v2/pages/${encodeURIComponent(pathname)}`, this.apiOrigin);
839
828
  if (localeInput)
840
829
  url.searchParams.set("locale", localeInput);
841
- const response = await this.fetch(url.pathname + url.search, {
842
- headers: { "Makeswift-Site-Version": siteVersion }
843
- });
830
+ const response = await this.fetch(url.pathname + url.search, siteVersion);
844
831
  if (!response.ok) {
845
832
  if (response.status === 404)
846
833
  return null;
@@ -850,19 +837,14 @@ Received "${apiKey}" instead.`);
850
837
  const json = await response.json();
851
838
  return getPageAPISchema.parse(json);
852
839
  }
853
- async getTypographies(typographyIds, preview) {
854
- var _a;
840
+ async getTypographies(typographyIds, siteVersion) {
855
841
  if (typographyIds.length === 0)
856
842
  return [];
857
- const url = new URL(`${this.siteVersion == null ? "v1" : "v2"}/typographies/bulk`, this.apiOrigin);
843
+ const url = new URL(`v2/typographies/bulk`, this.apiOrigin);
858
844
  typographyIds.forEach((id) => {
859
845
  url.searchParams.append("ids", id);
860
846
  });
861
- const response = await this.fetch(url.pathname + url.search, {
862
- headers: {
863
- "Makeswift-Site-Version": (_a = this.siteVersion) != null ? _a : preview ? MakeswiftSiteVersion.Working : MakeswiftSiteVersion.Live
864
- }
865
- });
847
+ const response = await this.fetch(url.pathname + url.search, siteVersion);
866
848
  if (!response.ok) {
867
849
  console.error("Failed to get typographies", await response.json());
868
850
  return [];
@@ -870,36 +852,31 @@ Received "${apiKey}" instead.`);
870
852
  const body = await response.json();
871
853
  return body;
872
854
  }
873
- async getSwatches(ids, preview) {
874
- var _a;
855
+ async getSwatches(ids, siteVersion) {
875
856
  if (ids.length === 0)
876
857
  return [];
877
- const url = new URL(`${this.siteVersion == null ? "v1" : "v2"}/swatches/bulk`, this.apiOrigin);
858
+ const url = new URL(`v2/swatches/bulk`, this.apiOrigin);
878
859
  ids.forEach((id) => {
879
860
  url.searchParams.append("ids", id);
880
861
  });
881
- const response = await this.fetch(url.pathname + url.search, {
882
- headers: {
883
- "Makeswift-Site-Version": (_a = this.siteVersion) != null ? _a : preview ? MakeswiftSiteVersion.Working : MakeswiftSiteVersion.Live
884
- }
885
- });
862
+ const response = await this.fetch(url.pathname + url.search, siteVersion);
886
863
  if (!response.ok) {
887
864
  console.error("Failed to get swatches", await response.json());
888
865
  return [];
889
866
  }
890
867
  return await response.json();
891
868
  }
892
- async getIntrospectedResources(_a, preview) {
869
+ async getIntrospectedResources(_a, siteVersion) {
893
870
  var _b = _a, {
894
871
  swatchIds
895
872
  } = _b, introspectedResourceIds = __objRest(_b, [
896
873
  "swatchIds"
897
874
  ]);
898
875
  const result = await this.graphqlClient.request(IntrospectedResourcesQuery, introspectedResourceIds);
899
- const swatches = await this.getSwatches(swatchIds, preview);
876
+ const swatches = await this.getSwatches(swatchIds, siteVersion);
900
877
  return __spreadProps(__spreadValues({}, result), { swatches });
901
878
  }
902
- async introspect(element, preview, locale) {
879
+ async introspect(element, siteVersion, locale) {
903
880
  var _a;
904
881
  const runtime = this.runtime;
905
882
  const descriptors = getPropControllerDescriptors(runtime.store.getState());
@@ -935,10 +912,10 @@ Received "${apiKey}" instead.`);
935
912
  let element2;
936
913
  if (isElementReference(current)) {
937
914
  const globalElementId = current.value;
938
- const globalElement = await this.getGlobalElement(globalElementId);
915
+ const globalElement = await this.getGlobalElement(globalElementId, siteVersion);
939
916
  let elementData = globalElement == null ? void 0 : globalElement.data;
940
917
  if (locale) {
941
- const localizedGlobalElement = await this.getLocalizedGlobalElement(globalElementId, locale);
918
+ const localizedGlobalElement = await this.getLocalizedGlobalElement(globalElementId, locale, siteVersion);
942
919
  localizedResourcesMap.set(globalElementId, (_a = localizedGlobalElement == null ? void 0 : localizedGlobalElement.id) != null ? _a : null);
943
920
  if (localizedGlobalElement) {
944
921
  elementData = localizedGlobalElement.data;
@@ -957,7 +934,7 @@ Received "${apiKey}" instead.`);
957
934
  continue;
958
935
  getResourcesFromElementDescriptors(elementDescriptors, element2.props);
959
936
  }
960
- const typographies = await this.getTypographies([...typographyIds], preview);
937
+ const typographies = await this.getTypographies([...typographyIds], siteVersion);
961
938
  typographies.forEach((typography) => {
962
939
  typography == null ? void 0 : typography.style.forEach((style) => {
963
940
  var _a2;
@@ -966,12 +943,12 @@ Received "${apiKey}" instead.`);
966
943
  swatchIds.add(swatchId);
967
944
  });
968
945
  });
969
- const pagePathnames = await this.getPagePathnameSlices([...pageIds], { preview, locale });
946
+ const pagePathnames = await this.getPagePathnameSlices([...pageIds], siteVersion, { locale });
970
947
  const { swatches, files, tables } = await this.getIntrospectedResources({
971
948
  swatchIds: [...swatchIds],
972
949
  fileIds: [...fileIds],
973
950
  tableIds: [...tableIds]
974
- }, preview);
951
+ }, siteVersion);
975
952
  const cacheData = {
976
953
  [APIResourceType.Swatch]: [...swatchIds].map((id) => {
977
954
  var _a2;
@@ -1019,19 +996,13 @@ Received "${apiKey}" instead.`);
1019
996
  };
1020
997
  return { cacheData, localizedResourcesMap: Object.fromEntries(localizedResourcesMap.entries()) };
1021
998
  }
1022
- async getPageSnapshot(pathname, {
1023
- preview: previewOverride = false,
1024
- locale: localeInput
1025
- } = {}) {
1026
- var _a, _b;
1027
- const siteVersion = (_a = this.siteVersion) != null ? _a : previewOverride ? MakeswiftSiteVersion.Working : MakeswiftSiteVersion.Live;
999
+ async getPageSnapshot(pathname, { siteVersion, locale: localeInput }) {
1000
+ var _a;
1028
1001
  const searchParams = new URLSearchParams();
1029
1002
  if (localeInput) {
1030
1003
  searchParams.set("locale", localeInput);
1031
1004
  }
1032
- const response = await this.fetch(`/${this.siteVersion == null ? "v2" : "v3"}/pages/${encodeURIComponent(pathname)}/document?${searchParams.toString()}`, {
1033
- headers: { "Makeswift-Site-Version": siteVersion }
1034
- });
1005
+ const response = await this.fetch(`v3/pages/${encodeURIComponent(pathname)}/document?${searchParams.toString()}`, siteVersion);
1035
1006
  if (!response.ok) {
1036
1007
  if (response.status === 404)
1037
1008
  return null;
@@ -1041,7 +1012,7 @@ Received "${apiKey}" instead.`);
1041
1012
  const document2 = await response.json();
1042
1013
  const baseLocalizedPage = document2.localizedPages.find(({ parentId }) => parentId == null);
1043
1014
  const locale = document2.locale;
1044
- const { cacheData, localizedResourcesMap } = await this.introspect((_b = baseLocalizedPage == null ? void 0 : baseLocalizedPage.data) != null ? _b : document2.data, previewOverride, locale != null ? locale : void 0);
1015
+ const { cacheData, localizedResourcesMap } = await this.introspect((_a = baseLocalizedPage == null ? void 0 : baseLocalizedPage.data) != null ? _a : document2.data, siteVersion, locale != null ? locale : void 0);
1045
1016
  const apiOrigin = this.apiOrigin.href;
1046
1017
  const preview = siteVersion === MakeswiftSiteVersion.Working;
1047
1018
  return {
@@ -1053,8 +1024,8 @@ Received "${apiKey}" instead.`);
1053
1024
  locale
1054
1025
  };
1055
1026
  }
1056
- async getSwatch(swatchId) {
1057
- const response = await this.fetch(`${this.siteVersion == null ? "v1" : "v2"}/swatches/${swatchId}`);
1027
+ async getSwatch(swatchId, siteVersion) {
1028
+ const response = await this.fetch(`v2/swatches/${swatchId}`, siteVersion);
1058
1029
  if (!response.ok) {
1059
1030
  if (response.status !== 404)
1060
1031
  console.error("Failed to get swatch", await response.json());
@@ -1067,8 +1038,8 @@ Received "${apiKey}" instead.`);
1067
1038
  const result = await this.graphqlClient.request(FileQuery, { fileId });
1068
1039
  return result.file;
1069
1040
  }
1070
- async getTypography(typographyId) {
1071
- const response = await this.fetch(`${this.siteVersion == null ? "v1" : "v2"}/typographies/${typographyId}`);
1041
+ async getTypography(typographyId, siteVersion) {
1042
+ const response = await this.fetch(`v2/typographies/${typographyId}`, siteVersion);
1072
1043
  if (!response.ok) {
1073
1044
  if (response.status !== 404)
1074
1045
  console.error("Failed to get typography", await response.json());
@@ -1077,8 +1048,8 @@ Received "${apiKey}" instead.`);
1077
1048
  const typography = await response.json();
1078
1049
  return typography;
1079
1050
  }
1080
- async getGlobalElement(globalElementId) {
1081
- const response = await this.fetch(`${this.siteVersion == null ? "v1" : "v2"}/global-elements/${globalElementId}`);
1051
+ async getGlobalElement(globalElementId, siteVersion) {
1052
+ const response = await this.fetch(`v2/global-elements/${globalElementId}`, siteVersion);
1082
1053
  if (!response.ok) {
1083
1054
  if (response.status !== 404)
1084
1055
  console.error("Failed to get global element", await response.json());
@@ -1087,8 +1058,8 @@ Received "${apiKey}" instead.`);
1087
1058
  const globalElement = await response.json();
1088
1059
  return globalElement;
1089
1060
  }
1090
- async getLocalizedGlobalElement(globalElementId, locale) {
1091
- const response = await this.fetch(`${this.siteVersion == null ? "v1" : "v2"}/localized-global-elements/${globalElementId}?locale=${locale}`);
1061
+ async getLocalizedGlobalElement(globalElementId, locale, siteVersion) {
1062
+ const response = await this.fetch(`v2/localized-global-elements/${globalElementId}?locale=${locale}`, siteVersion);
1092
1063
  if (!response.ok) {
1093
1064
  if (response.status !== 404)
1094
1065
  console.error("Failed to get localized global element", await response.json());
@@ -1097,19 +1068,14 @@ Received "${apiKey}" instead.`);
1097
1068
  const localizedGlobalElement = await response.json();
1098
1069
  return localizedGlobalElement;
1099
1070
  }
1100
- async getPagePathnameSlices(pageIds, { preview: previewOverride = false, locale } = {}) {
1101
- var _a;
1071
+ async getPagePathnameSlices(pageIds, siteVersion, { locale }) {
1102
1072
  if (pageIds.length === 0)
1103
1073
  return [];
1104
- const url = new URL(`${this.siteVersion == null ? "v1" : "v2"}/page-pathname-slices/bulk`, this.apiOrigin);
1074
+ const url = new URL(`v2/page-pathname-slices/bulk`, this.apiOrigin);
1105
1075
  pageIds.forEach((id) => url.searchParams.append("ids", id));
1106
1076
  if (locale != null)
1107
1077
  url.searchParams.set("locale", locale);
1108
- const response = await this.fetch(url.pathname + url.search, {
1109
- headers: {
1110
- "Makeswift-Site-Version": (_a = this.siteVersion) != null ? _a : previewOverride ? MakeswiftSiteVersion.Working : MakeswiftSiteVersion.Live
1111
- }
1112
- });
1078
+ const response = await this.fetch(url.pathname + url.search, siteVersion);
1113
1079
  if (!response.ok) {
1114
1080
  console.error("Failed to get page pathname slices", await response.json());
1115
1081
  return [];
@@ -1126,9 +1092,9 @@ Received "${apiKey}" instead.`);
1126
1092
  };
1127
1093
  });
1128
1094
  }
1129
- async getPagePathnameSlice(pageId, { preview = false, locale } = {}) {
1095
+ async getPagePathnameSlice(pageId, siteVersion, { locale } = {}) {
1130
1096
  var _a;
1131
- const pagePathnameSlices = await this.getPagePathnameSlices([pageId], { preview, locale });
1097
+ const pagePathnameSlices = await this.getPagePathnameSlices([pageId], siteVersion, { locale });
1132
1098
  return (_a = pagePathnameSlices.at(0)) != null ? _a : null;
1133
1099
  }
1134
1100
  async getTable(tableId) {
@@ -1218,8 +1184,8 @@ async function fonts(_req, res, { getFonts } = {}) {
1218
1184
  const fonts2 = (_a = await (getFonts == null ? void 0 : getFonts())) != null ? _a : [];
1219
1185
  return res.json(fonts2);
1220
1186
  }
1221
- const version = "0.12.4";
1222
- async function handler(req, res, { apiKey, siteVersions }) {
1187
+ const version = "0.13.1";
1188
+ async function handler(req, res, { apiKey }) {
1223
1189
  if (req.query.secret !== apiKey) {
1224
1190
  return res.status(401).json({ message: "Unauthorized" });
1225
1191
  }
@@ -1230,8 +1196,8 @@ async function handler(req, res, { apiKey, siteVersions }) {
1230
1196
  clientSideNavigation: true,
1231
1197
  elementFromPoint: false,
1232
1198
  customBreakpoints: true,
1233
- siteVersions,
1234
- unstable_siteVersions: siteVersions,
1199
+ siteVersions: true,
1200
+ unstable_siteVersions: true,
1235
1201
  localizedPageSSR: true
1236
1202
  });
1237
1203
  }
@@ -1348,7 +1314,6 @@ function MakeswiftApiHandler(apiKey, {
1348
1314
  appOrigin = "https://app.makeswift.com",
1349
1315
  apiOrigin = "https://api.makeswift.com",
1350
1316
  getFonts,
1351
- siteVersions = false,
1352
1317
  runtime = ReactRuntime
1353
1318
  } = {}) {
1354
1319
  const cors = Cors({ origin: appOrigin });
@@ -1371,16 +1336,13 @@ Received "${apiKey}" instead.`);
1371
1336
  Received "${makeswift}" for the \`makeswift\` param instead.
1372
1337
  Read more about dynamic catch-all routes here: https://nextjs.org/docs/routing/dynamic-routes#catch-all-routes`);
1373
1338
  }
1374
- const client = new Makeswift(apiKey, {
1375
- apiOrigin,
1376
- siteVersion: siteVersions ? Makeswift.getSiteVersion(req.previewData) : void 0,
1377
- runtime
1378
- });
1339
+ const client = new Makeswift(apiKey, { apiOrigin, runtime });
1340
+ const siteVersion = Makeswift.getSiteVersion(req.previewData);
1379
1341
  const action = "/" + makeswift.join("/");
1380
1342
  const matches = (pattern) => match(pattern, { decode: decodeURIComponent })(action);
1381
1343
  let m;
1382
1344
  if (matches("/manifest"))
1383
- return handler(req, res, { apiKey, siteVersions });
1345
+ return handler(req, res, { apiKey });
1384
1346
  if (matches("/revalidate"))
1385
1347
  return revalidate(req, res, { apiKey });
1386
1348
  if (matches("/proxy-preview-mode"))
@@ -1395,23 +1357,23 @@ Read more about dynamic catch-all routes here: https://nextjs.org/docs/routing/d
1395
1357
  return mergeTranslatedData(req, res, client);
1396
1358
  const handleResource = (resource) => resource === null ? res.status(404).json({ message: "Not Found" }) : res.json(resource);
1397
1359
  if (m = matches("/swatches/:id")) {
1398
- return client.getSwatch(m.params.id).then(handleResource);
1360
+ return client.getSwatch(m.params.id, siteVersion).then(handleResource);
1399
1361
  }
1400
1362
  if (m = matches("/files/:id")) {
1401
1363
  return client.getFile(m.params.id).then(handleResource);
1402
1364
  }
1403
1365
  if (m = matches("/typographies/:id")) {
1404
- return client.getTypography(m.params.id).then(handleResource);
1366
+ return client.getTypography(m.params.id, siteVersion).then(handleResource);
1405
1367
  }
1406
1368
  if (m = matches("/global-elements/:id")) {
1407
- return client.getGlobalElement(m.params.id).then(handleResource);
1369
+ return client.getGlobalElement(m.params.id, siteVersion).then(handleResource);
1408
1370
  }
1409
1371
  if (m = matches("/localized-global-elements/:globalElementId/:locale")) {
1410
- return client.getLocalizedGlobalElement(m.params.globalElementId, m.params.locale).then((resource) => resource === null ? res.json({ message: "Not Found" }) : res.json(resource));
1372
+ return client.getLocalizedGlobalElement(m.params.globalElementId, m.params.locale, siteVersion).then((resource) => resource === null ? res.json({ message: "Not Found" }) : res.json(resource));
1411
1373
  }
1412
1374
  if (m = matches("/page-pathname-slices/:id")) {
1413
1375
  const locale = typeof req.query.locale === "string" ? req.query.locale : void 0;
1414
- return client.getPagePathnameSlice(m.params.id, { locale }).then(handleResource);
1376
+ return client.getPagePathnameSlice(m.params.id, siteVersion, { locale }).then(handleResource);
1415
1377
  }
1416
1378
  if (m = matches("/tables/:id")) {
1417
1379
  return client.getTable(m.params.id).then(handleResource);
@@ -1491,14 +1453,14 @@ async function getStaticPaths() {
1491
1453
  }
1492
1454
  const REVALIDATE_SECONDS = 1;
1493
1455
  async function getStaticProps(ctx) {
1494
- var _a, _b, _c;
1456
+ var _a, _b;
1495
1457
  deprecationWarning("getStaticProps");
1496
1458
  const makeswift = new Makeswift(getApiKey(), {
1497
1459
  apiOrigin: getApiOrigin()
1498
1460
  });
1499
1461
  const path = "/" + ((_b = (_a = ctx.params) == null ? void 0 : _a.path) != null ? _b : []).join("/");
1500
1462
  const snapshot = await makeswift.getPageSnapshot(path, {
1501
- preview: ((_c = ctx.previewData) == null ? void 0 : _c.makeswift) === true
1463
+ siteVersion: Makeswift.getSiteVersion(ctx.previewData)
1502
1464
  });
1503
1465
  if (snapshot == null)
1504
1466
  return {
@@ -1520,7 +1482,7 @@ async function getServerSideProps(ctx) {
1520
1482
  });
1521
1483
  const path = "/" + ((_b = (_a = ctx.params) == null ? void 0 : _a.path) != null ? _b : []).join("/");
1522
1484
  const snapshot = await makeswift.getPageSnapshot(path, {
1523
- preview: true
1485
+ siteVersion: MakeswiftSiteVersion.Working
1524
1486
  });
1525
1487
  if (snapshot == null)
1526
1488
  return {