@makeswift/runtime 0.11.7 → 0.11.8

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
@@ -246,7 +246,7 @@ function fetchAPIResource(resourceType, resourceId, locale) {
246
246
  break;
247
247
  }
248
248
  case types.APIResourceType.PagePathnameSlice:
249
- resource = await fetchJson(`/api/makeswift/page-pathname-slices/${resourceId}`);
249
+ resource = await fetchJson(`/api/makeswift/page-pathname-slices/${resourceId}?locale=${locale}`);
250
250
  break;
251
251
  case types.APIResourceType.Table:
252
252
  resource = await fetchJson(`/api/makeswift/tables/${resourceId}`);
@@ -296,13 +296,6 @@ const FileFragment = `
296
296
  }
297
297
  }
298
298
  `;
299
- const PagePathnameSliceFragment = `
300
- fragment PagePathnameSlice on PagePathnameSlice {
301
- __typename
302
- id
303
- pathname
304
- }
305
- `;
306
299
  const TableFragment = `
307
300
  fragment Table on Table {
308
301
  __typename
@@ -330,22 +323,17 @@ const TableFragment = `
330
323
  }
331
324
  `;
332
325
  const IntrospectedResourcesQuery = `
333
- query IntrospectedResources($fileIds: [ID!]!, $pageIds: [ID!]!, $tableIds: [ID!]!) {
326
+ query IntrospectedResources($fileIds: [ID!]!, $tableIds: [ID!]!) {
334
327
  files(ids: $fileIds) {
335
328
  ...File
336
329
  }
337
330
 
338
- pagePathnamesById(ids: $pageIds) {
339
- ...PagePathnameSlice
340
- }
341
-
342
331
  tables(ids: $tableIds) {
343
332
  ...Table
344
333
  }
345
334
  }
346
335
 
347
336
  ${FileFragment}
348
- ${PagePathnameSliceFragment}
349
337
  ${TableFragment}
350
338
  `;
351
339
  const FileQuery = `
@@ -357,15 +345,6 @@ const FileQuery = `
357
345
 
358
346
  ${FileFragment}
359
347
  `;
360
- const PagePathnamesByIdQuery = `
361
- query PagePathnamesById($pageIds: [ID!]!) {
362
- pagePathnamesById(ids: $pageIds) {
363
- ...PagePathnameSlice
364
- }
365
- }
366
-
367
- ${PagePathnameSliceFragment}
368
- `;
369
348
  const TableQuery = `
370
349
  query Table($tableId: ID!) {
371
350
  table(id: $tableId) {
@@ -456,7 +435,7 @@ class MakeswiftClient {
456
435
  return getAPIResource(this.makeswiftApiClient.getState(), types.APIResourceType.PagePathnameSlice, pageId);
457
436
  }
458
437
  async fetchPagePathnameSlice(pageId) {
459
- return await this.makeswiftApiClient.dispatch(fetchAPIResource(types.APIResourceType.PagePathnameSlice, pageId));
438
+ return await this.makeswiftApiClient.dispatch(fetchAPIResource(types.APIResourceType.PagePathnameSlice, pageId, this.locale));
460
439
  }
461
440
  readTable(tableId) {
462
441
  return getAPIResource(this.makeswiftApiClient.getState(), types.APIResourceType.Table, tableId);
@@ -812,6 +791,20 @@ if (window.parent !== window) {
812
791
  })]
813
792
  });
814
793
  }
794
+ const pagePathnameSlicesAPISchema = zod.z.array(zod.z.object({
795
+ id: zod.z.string(),
796
+ basePageId: zod.z.string(),
797
+ pathname: zod.z.string(),
798
+ __typename: zod.z.literal("PagePathnameSlice")
799
+ }).nullable());
800
+ const getPageAPISchema = zod.z.object({
801
+ pathname: zod.z.string(),
802
+ locale: zod.z.string(),
803
+ alternate: zod.z.array(zod.z.object({
804
+ pathname: zod.z.string(),
805
+ locale: zod.z.string()
806
+ }))
807
+ });
815
808
  class Makeswift {
816
809
  constructor(apiKey, {
817
810
  apiOrigin = "https://api.makeswift.com",
@@ -865,6 +858,25 @@ Received "${apiKey}" instead.`);
865
858
  const json = await response.json();
866
859
  return json;
867
860
  }
861
+ async getPage(pathname, {
862
+ preview: previewOverride = false,
863
+ locale: localeInput
864
+ } = {}) {
865
+ var _a;
866
+ const siteVersion = (_a = this.siteVersion) != null ? _a : previewOverride ? MakeswiftSiteVersion.Working : MakeswiftSiteVersion.Live;
867
+ const searchParams = new URLSearchParams();
868
+ if (localeInput)
869
+ searchParams.set("locale", localeInput);
870
+ const response = await this.fetch(`v1/pages/${encodeURIComponent(pathname)}?${searchParams.toString()}`, { headers: { "Makeswift-Site-Version": siteVersion } });
871
+ if (!response.ok) {
872
+ if (response.status === 404)
873
+ return null;
874
+ console.error("Failed to get page snapshot", await response.json());
875
+ throw new Error(`Failed to get page snapshot with error: "${response.statusText}"`);
876
+ }
877
+ const json = await response.json();
878
+ return getPageAPISchema.parse(json);
879
+ }
868
880
  async getTypographies(typographyIds, preview) {
869
881
  var _a;
870
882
  const url = new URL(`${this.siteVersion == null ? "v1" : "v2"}/typographies/bulk`, this.apiOrigin);
@@ -977,15 +989,12 @@ Received "${apiKey}" instead.`);
977
989
  swatchIds.add(swatchId);
978
990
  });
979
991
  });
980
- const { swatches, files, tables, pagePathnamesById } = await this.getIntrospectedResources({
992
+ const pagePathnames = await this.getPagePathnameSlices([...pageIds], { preview, locale });
993
+ const { swatches, files, tables } = await this.getIntrospectedResources({
981
994
  swatchIds: [...swatchIds],
982
995
  fileIds: [...fileIds],
983
- tableIds: [...tableIds],
984
- pageIds: [...pageIds]
996
+ tableIds: [...tableIds]
985
997
  }, preview);
986
- const pagePathnameSlices = pagePathnamesById.map((pagePathnameSlice) => pagePathnameSlice && __spreadProps(__spreadValues({}, pagePathnameSlice), {
987
- id: Buffer.from(`Page:${pagePathnameSlice.id}`).toString("base64")
988
- }));
989
998
  const cacheData = {
990
999
  [types.APIResourceType.Swatch]: [...swatchIds].map((id) => {
991
1000
  var _a2;
@@ -1019,7 +1028,7 @@ Received "${apiKey}" instead.`);
1019
1028
  var _a2;
1020
1029
  return {
1021
1030
  id,
1022
- value: (_a2 = pagePathnameSlices.find((pagePathnameSlice) => (pagePathnameSlice == null ? void 0 : pagePathnameSlice.id) === id)) != null ? _a2 : null
1031
+ value: (_a2 = pagePathnames.find((pagePathnameSlice) => (pagePathnameSlice == null ? void 0 : pagePathnameSlice.id) === id)) != null ? _a2 : null
1023
1032
  };
1024
1033
  }),
1025
1034
  [types.APIResourceType.GlobalElement]: [...globalElements.entries()].map(([id, value]) => ({
@@ -1111,10 +1120,37 @@ Received "${apiKey}" instead.`);
1111
1120
  const localizedGlobalElement = await response.json();
1112
1121
  return localizedGlobalElement;
1113
1122
  }
1114
- async getPagePathnameSlice(pageId) {
1123
+ async getPagePathnameSlices(pageIds, { preview: previewOverride = false, locale } = {}) {
1124
+ var _a;
1125
+ const url = new URL(`${this.siteVersion == null ? "v1" : "v2"}/page-pathname-slices/bulk`, this.apiOrigin);
1126
+ pageIds.forEach((id) => url.searchParams.append("ids", id));
1127
+ if (locale != null)
1128
+ url.searchParams.set("locale", locale);
1129
+ const response = await this.fetch(url.pathname + url.search, {
1130
+ headers: {
1131
+ "Makeswift-Site-Version": (_a = this.siteVersion) != null ? _a : previewOverride ? MakeswiftSiteVersion.Working : MakeswiftSiteVersion.Live
1132
+ }
1133
+ });
1134
+ if (!response.ok) {
1135
+ console.error("Failed to get page pathname slices", await response.json());
1136
+ return [];
1137
+ }
1138
+ const json = await response.json();
1139
+ const pagePathnameSlices = pagePathnameSlicesAPISchema.parse(json);
1140
+ return pagePathnameSlices.map((pagePathnameSlice) => {
1141
+ if (pagePathnameSlice == null)
1142
+ return null;
1143
+ return {
1144
+ id: pagePathnameSlice.basePageId,
1145
+ pathname: pagePathnameSlice.pathname,
1146
+ __typename: pagePathnameSlice.__typename
1147
+ };
1148
+ });
1149
+ }
1150
+ async getPagePathnameSlice(pageId, { preview = false, locale } = {}) {
1115
1151
  var _a;
1116
- const result = await this.graphqlClient.request(PagePathnamesByIdQuery, { pageIds: [pageId] });
1117
- return (_a = result.pagePathnamesById.at(0)) != null ? _a : null;
1152
+ const pagePathnameSlices = await this.getPagePathnameSlices([pageId], { preview, locale });
1153
+ return (_a = pagePathnameSlices.at(0)) != null ? _a : null;
1118
1154
  }
1119
1155
  async getTable(tableId) {
1120
1156
  const result = await this.graphqlClient.request(TableQuery, { tableId });
@@ -1203,7 +1239,7 @@ async function fonts(_req, res, { getFonts } = {}) {
1203
1239
  const fonts2 = (_a = await (getFonts == null ? void 0 : getFonts())) != null ? _a : [];
1204
1240
  return res.json(fonts2);
1205
1241
  }
1206
- const version = "0.11.7";
1242
+ const version = "0.11.8";
1207
1243
  async function handler(req, res, { apiKey, siteVersions }) {
1208
1244
  if (req.query.secret !== apiKey) {
1209
1245
  return res.status(401).json({ message: "Unauthorized" });
@@ -1392,7 +1428,8 @@ Read more about dynamic catch-all routes here: https://nextjs.org/docs/routing/d
1392
1428
  return client.getLocalizedGlobalElement(m.params.globalElementId, m.params.locale).then((resource) => resource === null ? res.json({ message: "Not Found" }) : res.json(resource));
1393
1429
  }
1394
1430
  if (m = matches("/page-pathname-slices/:id")) {
1395
- return client.getPagePathnameSlice(m.params.id).then(handleResource);
1431
+ const locale = typeof req.query.locale === "string" ? req.query.locale : void 0;
1432
+ return client.getPagePathnameSlice(m.params.id, { locale }).then(handleResource);
1396
1433
  }
1397
1434
  if (m = matches("/tables/:id")) {
1398
1435
  return client.getTable(m.params.id).then(handleResource);