@makeswift/runtime 0.12.4 → 0.13.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/dist/index.cjs.js CHANGED
@@ -810,16 +810,11 @@ const getPageAPISchema = zod.z.object({
810
810
  }))
811
811
  });
812
812
  class Makeswift {
813
- constructor(apiKey, {
814
- apiOrigin = "https://api.makeswift.com",
815
- runtime = ReactRuntime,
816
- siteVersion
817
- } = {}) {
813
+ constructor(apiKey, { apiOrigin = "https://api.makeswift.com", runtime = ReactRuntime } = {}) {
818
814
  __publicField(this, "apiKey");
819
815
  __publicField(this, "apiOrigin");
820
816
  __publicField(this, "graphqlClient");
821
817
  __publicField(this, "runtime");
822
- __publicField(this, "siteVersion");
823
818
  if (typeof apiKey !== "string") {
824
819
  throw new Error(`The Makeswift client must be passed a valid Makeswift site API key: \`new Makeswift('<makeswift_site_api_key>')\`
825
820
  Received "${apiKey}" instead.`);
@@ -832,29 +827,25 @@ Received "${apiKey}" instead.`);
832
827
  }
833
828
  this.graphqlClient = new GraphQLClient(new URL("graphql", apiOrigin).href);
834
829
  this.runtime = runtime;
835
- this.siteVersion = siteVersion != null ? siteVersion : null;
836
830
  }
837
831
  static getSiteVersion(previewData) {
838
832
  var _a;
839
833
  return (_a = getMakeswiftSiteVersion(previewData)) != null ? _a : MakeswiftSiteVersion.Live;
840
834
  }
841
- async fetch(path, init) {
842
- var _a;
835
+ async fetch(path, siteVersion = MakeswiftSiteVersion.Live, init) {
843
836
  const response = await fetch(new URL(path, this.apiOrigin).toString(), __spreadProps(__spreadValues({}, init), {
844
837
  headers: __spreadValues({
845
838
  ["X-API-Key"]: this.apiKey,
846
839
  "Makeswift-Site-API-Key": this.apiKey,
847
- "Makeswift-Site-Version": (_a = this.siteVersion) != null ? _a : MakeswiftSiteVersion.Live
840
+ "Makeswift-Site-Version": siteVersion
848
841
  }, init == null ? void 0 : init.headers)
849
842
  }));
850
843
  return response;
851
844
  }
852
- async getPages() {
853
- const response = await this.fetch(`/${this.siteVersion == null ? "v2" : "v3"}/pages`, {
854
- headers: {
855
- "Makeswift-Site-Version": MakeswiftSiteVersion.Live
856
- }
857
- });
845
+ async getPages({
846
+ siteVersion = MakeswiftSiteVersion.Live
847
+ } = {}) {
848
+ const response = await this.fetch(`v3/pages`, siteVersion);
858
849
  if (!response.ok) {
859
850
  console.error("Failed to get pages", await response.json());
860
851
  throw new Error(`Failed to get pages with error: "${response.statusText}"`);
@@ -863,17 +854,13 @@ Received "${apiKey}" instead.`);
863
854
  return json;
864
855
  }
865
856
  async getPage(pathname, {
866
- preview: previewOverride = false,
857
+ siteVersion = MakeswiftSiteVersion.Live,
867
858
  locale: localeInput
868
859
  } = {}) {
869
- var _a;
870
- const siteVersion = (_a = this.siteVersion) != null ? _a : previewOverride ? MakeswiftSiteVersion.Working : MakeswiftSiteVersion.Live;
871
- const url = new URL(`${this.siteVersion == null ? "v1" : "v2"}/pages/${encodeURIComponent(pathname)}`, this.apiOrigin);
860
+ const url = new URL(`v2/pages/${encodeURIComponent(pathname)}`, this.apiOrigin);
872
861
  if (localeInput)
873
862
  url.searchParams.set("locale", localeInput);
874
- const response = await this.fetch(url.pathname + url.search, {
875
- headers: { "Makeswift-Site-Version": siteVersion }
876
- });
863
+ const response = await this.fetch(url.pathname + url.search, siteVersion);
877
864
  if (!response.ok) {
878
865
  if (response.status === 404)
879
866
  return null;
@@ -883,19 +870,14 @@ Received "${apiKey}" instead.`);
883
870
  const json = await response.json();
884
871
  return getPageAPISchema.parse(json);
885
872
  }
886
- async getTypographies(typographyIds, preview) {
887
- var _a;
873
+ async getTypographies(typographyIds, siteVersion) {
888
874
  if (typographyIds.length === 0)
889
875
  return [];
890
- const url = new URL(`${this.siteVersion == null ? "v1" : "v2"}/typographies/bulk`, this.apiOrigin);
876
+ const url = new URL(`v2/typographies/bulk`, this.apiOrigin);
891
877
  typographyIds.forEach((id) => {
892
878
  url.searchParams.append("ids", id);
893
879
  });
894
- const response = await this.fetch(url.pathname + url.search, {
895
- headers: {
896
- "Makeswift-Site-Version": (_a = this.siteVersion) != null ? _a : preview ? MakeswiftSiteVersion.Working : MakeswiftSiteVersion.Live
897
- }
898
- });
880
+ const response = await this.fetch(url.pathname + url.search, siteVersion);
899
881
  if (!response.ok) {
900
882
  console.error("Failed to get typographies", await response.json());
901
883
  return [];
@@ -903,36 +885,31 @@ Received "${apiKey}" instead.`);
903
885
  const body = await response.json();
904
886
  return body;
905
887
  }
906
- async getSwatches(ids, preview) {
907
- var _a;
888
+ async getSwatches(ids, siteVersion) {
908
889
  if (ids.length === 0)
909
890
  return [];
910
- const url = new URL(`${this.siteVersion == null ? "v1" : "v2"}/swatches/bulk`, this.apiOrigin);
891
+ const url = new URL(`v2/swatches/bulk`, this.apiOrigin);
911
892
  ids.forEach((id) => {
912
893
  url.searchParams.append("ids", id);
913
894
  });
914
- const response = await this.fetch(url.pathname + url.search, {
915
- headers: {
916
- "Makeswift-Site-Version": (_a = this.siteVersion) != null ? _a : preview ? MakeswiftSiteVersion.Working : MakeswiftSiteVersion.Live
917
- }
918
- });
895
+ const response = await this.fetch(url.pathname + url.search, siteVersion);
919
896
  if (!response.ok) {
920
897
  console.error("Failed to get swatches", await response.json());
921
898
  return [];
922
899
  }
923
900
  return await response.json();
924
901
  }
925
- async getIntrospectedResources(_a, preview) {
902
+ async getIntrospectedResources(_a, siteVersion) {
926
903
  var _b = _a, {
927
904
  swatchIds
928
905
  } = _b, introspectedResourceIds = __objRest(_b, [
929
906
  "swatchIds"
930
907
  ]);
931
908
  const result = await this.graphqlClient.request(IntrospectedResourcesQuery, introspectedResourceIds);
932
- const swatches = await this.getSwatches(swatchIds, preview);
909
+ const swatches = await this.getSwatches(swatchIds, siteVersion);
933
910
  return __spreadProps(__spreadValues({}, result), { swatches });
934
911
  }
935
- async introspect(element, preview, locale) {
912
+ async introspect(element, siteVersion, locale) {
936
913
  var _a;
937
914
  const runtime = this.runtime;
938
915
  const descriptors = reactPage.getPropControllerDescriptors(runtime.store.getState());
@@ -968,10 +945,10 @@ Received "${apiKey}" instead.`);
968
945
  let element2;
969
946
  if (reactPage.isElementReference(current)) {
970
947
  const globalElementId = current.value;
971
- const globalElement = await this.getGlobalElement(globalElementId);
948
+ const globalElement = await this.getGlobalElement(globalElementId, siteVersion);
972
949
  let elementData = globalElement == null ? void 0 : globalElement.data;
973
950
  if (locale) {
974
- const localizedGlobalElement = await this.getLocalizedGlobalElement(globalElementId, locale);
951
+ const localizedGlobalElement = await this.getLocalizedGlobalElement(globalElementId, locale, siteVersion);
975
952
  localizedResourcesMap.set(globalElementId, (_a = localizedGlobalElement == null ? void 0 : localizedGlobalElement.id) != null ? _a : null);
976
953
  if (localizedGlobalElement) {
977
954
  elementData = localizedGlobalElement.data;
@@ -990,7 +967,7 @@ Received "${apiKey}" instead.`);
990
967
  continue;
991
968
  getResourcesFromElementDescriptors(elementDescriptors, element2.props);
992
969
  }
993
- const typographies = await this.getTypographies([...typographyIds], preview);
970
+ const typographies = await this.getTypographies([...typographyIds], siteVersion);
994
971
  typographies.forEach((typography) => {
995
972
  typography == null ? void 0 : typography.style.forEach((style) => {
996
973
  var _a2;
@@ -999,12 +976,12 @@ Received "${apiKey}" instead.`);
999
976
  swatchIds.add(swatchId);
1000
977
  });
1001
978
  });
1002
- const pagePathnames = await this.getPagePathnameSlices([...pageIds], { preview, locale });
979
+ const pagePathnames = await this.getPagePathnameSlices([...pageIds], siteVersion, { locale });
1003
980
  const { swatches, files, tables } = await this.getIntrospectedResources({
1004
981
  swatchIds: [...swatchIds],
1005
982
  fileIds: [...fileIds],
1006
983
  tableIds: [...tableIds]
1007
- }, preview);
984
+ }, siteVersion);
1008
985
  const cacheData = {
1009
986
  [types.APIResourceType.Swatch]: [...swatchIds].map((id) => {
1010
987
  var _a2;
@@ -1052,19 +1029,13 @@ Received "${apiKey}" instead.`);
1052
1029
  };
1053
1030
  return { cacheData, localizedResourcesMap: Object.fromEntries(localizedResourcesMap.entries()) };
1054
1031
  }
1055
- async getPageSnapshot(pathname, {
1056
- preview: previewOverride = false,
1057
- locale: localeInput
1058
- } = {}) {
1059
- var _a, _b;
1060
- const siteVersion = (_a = this.siteVersion) != null ? _a : previewOverride ? MakeswiftSiteVersion.Working : MakeswiftSiteVersion.Live;
1032
+ async getPageSnapshot(pathname, { siteVersion, locale: localeInput }) {
1033
+ var _a;
1061
1034
  const searchParams = new URLSearchParams();
1062
1035
  if (localeInput) {
1063
1036
  searchParams.set("locale", localeInput);
1064
1037
  }
1065
- const response = await this.fetch(`/${this.siteVersion == null ? "v2" : "v3"}/pages/${encodeURIComponent(pathname)}/document?${searchParams.toString()}`, {
1066
- headers: { "Makeswift-Site-Version": siteVersion }
1067
- });
1038
+ const response = await this.fetch(`v3/pages/${encodeURIComponent(pathname)}/document?${searchParams.toString()}`, siteVersion);
1068
1039
  if (!response.ok) {
1069
1040
  if (response.status === 404)
1070
1041
  return null;
@@ -1074,7 +1045,7 @@ Received "${apiKey}" instead.`);
1074
1045
  const document2 = await response.json();
1075
1046
  const baseLocalizedPage = document2.localizedPages.find(({ parentId }) => parentId == null);
1076
1047
  const locale = document2.locale;
1077
- const { cacheData, localizedResourcesMap } = await this.introspect((_b = baseLocalizedPage == null ? void 0 : baseLocalizedPage.data) != null ? _b : document2.data, previewOverride, locale != null ? locale : void 0);
1048
+ const { cacheData, localizedResourcesMap } = await this.introspect((_a = baseLocalizedPage == null ? void 0 : baseLocalizedPage.data) != null ? _a : document2.data, siteVersion, locale != null ? locale : void 0);
1078
1049
  const apiOrigin = this.apiOrigin.href;
1079
1050
  const preview = siteVersion === MakeswiftSiteVersion.Working;
1080
1051
  return {
@@ -1086,8 +1057,8 @@ Received "${apiKey}" instead.`);
1086
1057
  locale
1087
1058
  };
1088
1059
  }
1089
- async getSwatch(swatchId) {
1090
- const response = await this.fetch(`${this.siteVersion == null ? "v1" : "v2"}/swatches/${swatchId}`);
1060
+ async getSwatch(swatchId, siteVersion) {
1061
+ const response = await this.fetch(`v2/swatches/${swatchId}`, siteVersion);
1091
1062
  if (!response.ok) {
1092
1063
  if (response.status !== 404)
1093
1064
  console.error("Failed to get swatch", await response.json());
@@ -1100,8 +1071,8 @@ Received "${apiKey}" instead.`);
1100
1071
  const result = await this.graphqlClient.request(FileQuery, { fileId });
1101
1072
  return result.file;
1102
1073
  }
1103
- async getTypography(typographyId) {
1104
- const response = await this.fetch(`${this.siteVersion == null ? "v1" : "v2"}/typographies/${typographyId}`);
1074
+ async getTypography(typographyId, siteVersion) {
1075
+ const response = await this.fetch(`v2/typographies/${typographyId}`, siteVersion);
1105
1076
  if (!response.ok) {
1106
1077
  if (response.status !== 404)
1107
1078
  console.error("Failed to get typography", await response.json());
@@ -1110,8 +1081,8 @@ Received "${apiKey}" instead.`);
1110
1081
  const typography = await response.json();
1111
1082
  return typography;
1112
1083
  }
1113
- async getGlobalElement(globalElementId) {
1114
- const response = await this.fetch(`${this.siteVersion == null ? "v1" : "v2"}/global-elements/${globalElementId}`);
1084
+ async getGlobalElement(globalElementId, siteVersion) {
1085
+ const response = await this.fetch(`v2/global-elements/${globalElementId}`, siteVersion);
1115
1086
  if (!response.ok) {
1116
1087
  if (response.status !== 404)
1117
1088
  console.error("Failed to get global element", await response.json());
@@ -1120,8 +1091,8 @@ Received "${apiKey}" instead.`);
1120
1091
  const globalElement = await response.json();
1121
1092
  return globalElement;
1122
1093
  }
1123
- async getLocalizedGlobalElement(globalElementId, locale) {
1124
- const response = await this.fetch(`${this.siteVersion == null ? "v1" : "v2"}/localized-global-elements/${globalElementId}?locale=${locale}`);
1094
+ async getLocalizedGlobalElement(globalElementId, locale, siteVersion) {
1095
+ const response = await this.fetch(`v2/localized-global-elements/${globalElementId}?locale=${locale}`, siteVersion);
1125
1096
  if (!response.ok) {
1126
1097
  if (response.status !== 404)
1127
1098
  console.error("Failed to get localized global element", await response.json());
@@ -1130,19 +1101,14 @@ Received "${apiKey}" instead.`);
1130
1101
  const localizedGlobalElement = await response.json();
1131
1102
  return localizedGlobalElement;
1132
1103
  }
1133
- async getPagePathnameSlices(pageIds, { preview: previewOverride = false, locale } = {}) {
1134
- var _a;
1104
+ async getPagePathnameSlices(pageIds, siteVersion, { locale }) {
1135
1105
  if (pageIds.length === 0)
1136
1106
  return [];
1137
- const url = new URL(`${this.siteVersion == null ? "v1" : "v2"}/page-pathname-slices/bulk`, this.apiOrigin);
1107
+ const url = new URL(`v2/page-pathname-slices/bulk`, this.apiOrigin);
1138
1108
  pageIds.forEach((id) => url.searchParams.append("ids", id));
1139
1109
  if (locale != null)
1140
1110
  url.searchParams.set("locale", locale);
1141
- const response = await this.fetch(url.pathname + url.search, {
1142
- headers: {
1143
- "Makeswift-Site-Version": (_a = this.siteVersion) != null ? _a : previewOverride ? MakeswiftSiteVersion.Working : MakeswiftSiteVersion.Live
1144
- }
1145
- });
1111
+ const response = await this.fetch(url.pathname + url.search, siteVersion);
1146
1112
  if (!response.ok) {
1147
1113
  console.error("Failed to get page pathname slices", await response.json());
1148
1114
  return [];
@@ -1159,9 +1125,9 @@ Received "${apiKey}" instead.`);
1159
1125
  };
1160
1126
  });
1161
1127
  }
1162
- async getPagePathnameSlice(pageId, { preview = false, locale } = {}) {
1128
+ async getPagePathnameSlice(pageId, siteVersion, { locale } = {}) {
1163
1129
  var _a;
1164
- const pagePathnameSlices = await this.getPagePathnameSlices([pageId], { preview, locale });
1130
+ const pagePathnameSlices = await this.getPagePathnameSlices([pageId], siteVersion, { locale });
1165
1131
  return (_a = pagePathnameSlices.at(0)) != null ? _a : null;
1166
1132
  }
1167
1133
  async getTable(tableId) {
@@ -1251,8 +1217,8 @@ async function fonts(_req, res, { getFonts } = {}) {
1251
1217
  const fonts2 = (_a = await (getFonts == null ? void 0 : getFonts())) != null ? _a : [];
1252
1218
  return res.json(fonts2);
1253
1219
  }
1254
- const version = "0.12.4";
1255
- async function handler(req, res, { apiKey, siteVersions }) {
1220
+ const version = "0.13.0";
1221
+ async function handler(req, res, { apiKey }) {
1256
1222
  if (req.query.secret !== apiKey) {
1257
1223
  return res.status(401).json({ message: "Unauthorized" });
1258
1224
  }
@@ -1263,8 +1229,8 @@ async function handler(req, res, { apiKey, siteVersions }) {
1263
1229
  clientSideNavigation: true,
1264
1230
  elementFromPoint: false,
1265
1231
  customBreakpoints: true,
1266
- siteVersions,
1267
- unstable_siteVersions: siteVersions,
1232
+ siteVersions: true,
1233
+ unstable_siteVersions: true,
1268
1234
  localizedPageSSR: true
1269
1235
  });
1270
1236
  }
@@ -1381,7 +1347,6 @@ function MakeswiftApiHandler(apiKey, {
1381
1347
  appOrigin = "https://app.makeswift.com",
1382
1348
  apiOrigin = "https://api.makeswift.com",
1383
1349
  getFonts,
1384
- siteVersions = false,
1385
1350
  runtime = ReactRuntime
1386
1351
  } = {}) {
1387
1352
  const cors = Cors__default["default"]({ origin: appOrigin });
@@ -1404,16 +1369,13 @@ Received "${apiKey}" instead.`);
1404
1369
  Received "${makeswift}" for the \`makeswift\` param instead.
1405
1370
  Read more about dynamic catch-all routes here: https://nextjs.org/docs/routing/dynamic-routes#catch-all-routes`);
1406
1371
  }
1407
- const client = new Makeswift(apiKey, {
1408
- apiOrigin,
1409
- siteVersion: siteVersions ? Makeswift.getSiteVersion(req.previewData) : void 0,
1410
- runtime
1411
- });
1372
+ const client = new Makeswift(apiKey, { apiOrigin, runtime });
1373
+ const siteVersion = Makeswift.getSiteVersion(req.previewData);
1412
1374
  const action = "/" + makeswift.join("/");
1413
1375
  const matches = (pattern) => pathToRegexp.match(pattern, { decode: decodeURIComponent })(action);
1414
1376
  let m;
1415
1377
  if (matches("/manifest"))
1416
- return handler(req, res, { apiKey, siteVersions });
1378
+ return handler(req, res, { apiKey });
1417
1379
  if (matches("/revalidate"))
1418
1380
  return revalidate(req, res, { apiKey });
1419
1381
  if (matches("/proxy-preview-mode"))
@@ -1428,23 +1390,23 @@ Read more about dynamic catch-all routes here: https://nextjs.org/docs/routing/d
1428
1390
  return mergeTranslatedData(req, res, client);
1429
1391
  const handleResource = (resource) => resource === null ? res.status(404).json({ message: "Not Found" }) : res.json(resource);
1430
1392
  if (m = matches("/swatches/:id")) {
1431
- return client.getSwatch(m.params.id).then(handleResource);
1393
+ return client.getSwatch(m.params.id, siteVersion).then(handleResource);
1432
1394
  }
1433
1395
  if (m = matches("/files/:id")) {
1434
1396
  return client.getFile(m.params.id).then(handleResource);
1435
1397
  }
1436
1398
  if (m = matches("/typographies/:id")) {
1437
- return client.getTypography(m.params.id).then(handleResource);
1399
+ return client.getTypography(m.params.id, siteVersion).then(handleResource);
1438
1400
  }
1439
1401
  if (m = matches("/global-elements/:id")) {
1440
- return client.getGlobalElement(m.params.id).then(handleResource);
1402
+ return client.getGlobalElement(m.params.id, siteVersion).then(handleResource);
1441
1403
  }
1442
1404
  if (m = matches("/localized-global-elements/:globalElementId/:locale")) {
1443
- return client.getLocalizedGlobalElement(m.params.globalElementId, m.params.locale).then((resource) => resource === null ? res.json({ message: "Not Found" }) : res.json(resource));
1405
+ return client.getLocalizedGlobalElement(m.params.globalElementId, m.params.locale, siteVersion).then((resource) => resource === null ? res.json({ message: "Not Found" }) : res.json(resource));
1444
1406
  }
1445
1407
  if (m = matches("/page-pathname-slices/:id")) {
1446
1408
  const locale = typeof req.query.locale === "string" ? req.query.locale : void 0;
1447
- return client.getPagePathnameSlice(m.params.id, { locale }).then(handleResource);
1409
+ return client.getPagePathnameSlice(m.params.id, siteVersion, { locale }).then(handleResource);
1448
1410
  }
1449
1411
  if (m = matches("/tables/:id")) {
1450
1412
  return client.getTable(m.params.id).then(handleResource);
@@ -1524,14 +1486,14 @@ async function getStaticPaths() {
1524
1486
  }
1525
1487
  const REVALIDATE_SECONDS = 1;
1526
1488
  async function getStaticProps(ctx) {
1527
- var _a, _b, _c;
1489
+ var _a, _b;
1528
1490
  deprecationWarning("getStaticProps");
1529
1491
  const makeswift = new Makeswift(getApiKey(), {
1530
1492
  apiOrigin: getApiOrigin()
1531
1493
  });
1532
1494
  const path = "/" + ((_b = (_a = ctx.params) == null ? void 0 : _a.path) != null ? _b : []).join("/");
1533
1495
  const snapshot = await makeswift.getPageSnapshot(path, {
1534
- preview: ((_c = ctx.previewData) == null ? void 0 : _c.makeswift) === true
1496
+ siteVersion: Makeswift.getSiteVersion(ctx.previewData)
1535
1497
  });
1536
1498
  if (snapshot == null)
1537
1499
  return {
@@ -1553,7 +1515,7 @@ async function getServerSideProps(ctx) {
1553
1515
  });
1554
1516
  const path = "/" + ((_b = (_a = ctx.params) == null ? void 0 : _a.path) != null ? _b : []).join("/");
1555
1517
  const snapshot = await makeswift.getPageSnapshot(path, {
1556
- preview: true
1518
+ siteVersion: MakeswiftSiteVersion.Working
1557
1519
  });
1558
1520
  if (snapshot == null)
1559
1521
  return {