@makeswift/runtime 0.28.7-canary.9 → 0.28.8-canary.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.
Files changed (41) hide show
  1. package/dist/cjs/api/api-resources-client.js +47 -0
  2. package/dist/cjs/api/api-resources-client.js.map +1 -1
  3. package/dist/cjs/api/graphql-api-client.js +1 -1
  4. package/dist/cjs/api/host-api-resources-client.js +38 -32
  5. package/dist/cjs/api/host-api-resources-client.js.map +1 -1
  6. package/dist/cjs/api/makeswift-api-resources-client.js +70 -0
  7. package/dist/cjs/api/makeswift-api-resources-client.js.map +1 -0
  8. package/dist/cjs/api/rest-api-client.js +1 -1
  9. package/dist/cjs/api/rest-api-client.js.map +1 -1
  10. package/dist/cjs/api/types.js.map +1 -1
  11. package/dist/cjs/api-handler/handlers/manifest.js +1 -1
  12. package/dist/cjs/client/index.js +1 -1
  13. package/dist/cjs/state/api-client/fetch-api-resource.js +5 -47
  14. package/dist/cjs/state/api-client/fetch-api-resource.js.map +1 -1
  15. package/dist/esm/api/api-resources-client.js +47 -0
  16. package/dist/esm/api/api-resources-client.js.map +1 -1
  17. package/dist/esm/api/graphql-api-client.js +1 -1
  18. package/dist/esm/api/host-api-resources-client.js +38 -33
  19. package/dist/esm/api/host-api-resources-client.js.map +1 -1
  20. package/dist/esm/api/makeswift-api-resources-client.js +46 -0
  21. package/dist/esm/api/makeswift-api-resources-client.js.map +1 -0
  22. package/dist/esm/api/rest-api-client.js +1 -1
  23. package/dist/esm/api/rest-api-client.js.map +1 -1
  24. package/dist/esm/api/types.js.map +1 -1
  25. package/dist/esm/api-handler/handlers/manifest.js +1 -1
  26. package/dist/esm/client/index.js +1 -1
  27. package/dist/esm/state/api-client/fetch-api-resource.js +5 -47
  28. package/dist/esm/state/api-client/fetch-api-resource.js.map +1 -1
  29. package/dist/types/api/api-resources-client.d.ts +18 -10
  30. package/dist/types/api/api-resources-client.d.ts.map +1 -1
  31. package/dist/types/api/host-api-resources-client.d.ts +9 -14
  32. package/dist/types/api/host-api-resources-client.d.ts.map +1 -1
  33. package/dist/types/api/makeswift-api-resources-client.d.ts +30 -0
  34. package/dist/types/api/makeswift-api-resources-client.d.ts.map +1 -0
  35. package/dist/types/api/rest-api-client.d.ts +1 -1
  36. package/dist/types/api/rest-api-client.d.ts.map +1 -1
  37. package/dist/types/api/types.d.ts +3 -0
  38. package/dist/types/api/types.d.ts.map +1 -1
  39. package/dist/types/state/api-client/fetch-api-resource.d.ts +3 -4
  40. package/dist/types/state/api-client/fetch-api-resource.d.ts.map +1 -1
  41. package/package.json +4 -4
@@ -1,4 +1,5 @@
1
1
  import * as ApiClientState from "../state/api-client/state";
2
+ import { fetchAPIResource } from "../state/api-client/fetch-api-resource";
2
3
  import {
3
4
  APIResourceType
4
5
  } from "./types";
@@ -17,6 +18,10 @@ class ApiResourcesClient {
17
18
  this.store = store;
18
19
  this.subscribe = this.store.subscribe;
19
20
  }
21
+ async fetchSwatch(swatchId) {
22
+ const fetch = (id, version) => this.fetchSwatchImpl(id, version);
23
+ return await this.store.dispatch(fetchAPIResource(APIResourceType.Swatch, swatchId, fetch));
24
+ }
20
25
  readSwatch(swatchId) {
21
26
  return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.Swatch, swatchId);
22
27
  }
@@ -27,6 +32,10 @@ class ApiResourcesClient {
27
32
  fetch: (id) => this.fetchSwatch(id)
28
33
  });
29
34
  }
35
+ async fetchFile(fileId) {
36
+ const fetch = (id, version) => this.fetchFileImpl(id, version);
37
+ return await this.store.dispatch(fetchAPIResource(APIResourceType.File, fileId, fetch));
38
+ }
30
39
  readFile(fileId) {
31
40
  return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.File, fileId);
32
41
  }
@@ -37,6 +46,12 @@ class ApiResourcesClient {
37
46
  fetch: (id) => this.fetchFile(id)
38
47
  });
39
48
  }
49
+ async fetchTypography(typographyId) {
50
+ const fetch = (id, version) => this.fetchTypographyImpl(id, version);
51
+ return await this.store.dispatch(
52
+ fetchAPIResource(APIResourceType.Typography, typographyId, fetch)
53
+ );
54
+ }
40
55
  readTypography(typographyId) {
41
56
  return ApiClientState.getAPIResource(
42
57
  this.store.getState(),
@@ -51,6 +66,12 @@ class ApiResourcesClient {
51
66
  fetch: (id) => this.fetchTypography(id)
52
67
  });
53
68
  }
69
+ async fetchGlobalElement(globalElementId) {
70
+ const fetch = (id, version) => this.fetchGlobalElementImpl(id, version);
71
+ return await this.store.dispatch(
72
+ fetchAPIResource(APIResourceType.GlobalElement, globalElementId, fetch)
73
+ );
74
+ }
54
75
  readGlobalElement(globalElementId) {
55
76
  return ApiClientState.getAPIResource(
56
77
  this.store.getState(),
@@ -58,6 +79,19 @@ class ApiResourcesClient {
58
79
  globalElementId
59
80
  );
60
81
  }
82
+ async fetchLocalizedGlobalElement({
83
+ globalElementId,
84
+ locale
85
+ }) {
86
+ const fetch = (id, version, locale2) => {
87
+ if (locale2 == null)
88
+ throw new Error("Locale is required to fetch LocalizedGlobalElement");
89
+ return this.fetchLocalizedGlobalElementImpl(id, version, locale2);
90
+ };
91
+ return await this.store.dispatch(
92
+ fetchAPIResource(APIResourceType.LocalizedGlobalElement, globalElementId, fetch, locale)
93
+ );
94
+ }
61
95
  readLocalizedGlobalElement({
62
96
  globalElementId,
63
97
  locale
@@ -69,6 +103,15 @@ class ApiResourcesClient {
69
103
  locale
70
104
  );
71
105
  }
106
+ async fetchPagePathnameSlice({
107
+ pageId,
108
+ locale
109
+ }) {
110
+ const fetch = (id, version, locale2) => this.fetchPagePathnameSliceImpl(id, version, locale2);
111
+ return await this.store.dispatch(
112
+ fetchAPIResource(APIResourceType.PagePathnameSlice, pageId, fetch, locale)
113
+ );
114
+ }
72
115
  readPagePathnameSlice({
73
116
  pageId,
74
117
  locale
@@ -107,6 +150,10 @@ class ApiResourcesClient {
107
150
  fetch: async () => id != null ? fetch(id) : null
108
151
  };
109
152
  }
153
+ async fetchTable(tableId) {
154
+ const fetch = (id, version) => this.fetchTableImpl(id, version);
155
+ return await this.store.dispatch(fetchAPIResource(APIResourceType.Table, tableId, fetch));
156
+ }
110
157
  readTable(tableId) {
111
158
  return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.Table, tableId);
112
159
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/api/api-resources-client.ts"],"sourcesContent":["import { type FetchableValue } from '@makeswift/controls'\n\nimport { type Store as ApiClientStore } from '../state/api-client/store'\nimport * as ApiClientState from '../state/api-client/state'\n\nimport {\n type File,\n type GlobalElement,\n type LocalizedGlobalElement,\n type Page,\n type PagePathnameSlice,\n type Site,\n type Snippet,\n type Swatch,\n type Table,\n type Typography,\n APIResourceType,\n} from './types'\n\nexport type CacheData = ApiClientState.SerializedState\n\nexport const CacheData = {\n empty(): CacheData {\n return {\n apiResources: {},\n localizedResourcesMap: {},\n }\n },\n}\n\nexport abstract class ApiResourcesClient {\n readonly store: ApiClientStore\n readonly subscribe: ApiClientStore['subscribe']\n\n constructor({ store }: { store: ApiClientStore }) {\n this.store = store\n this.subscribe = this.store.subscribe\n }\n\n abstract fetchSwatch(swatchId: string): Promise<Swatch | null>\n\n readSwatch(swatchId: string): Swatch | null {\n return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.Swatch, swatchId)\n }\n\n resolveSwatch(swatchId: string | undefined): FetchableValue<Swatch | null> {\n return this.resolveResource(APIResourceType.Swatch, {\n id: swatchId,\n read: id => this.readSwatch(id),\n fetch: id => this.fetchSwatch(id),\n })\n }\n\n abstract fetchFile(fileId: string): Promise<File | null>\n\n readFile(fileId: string): File | null {\n return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.File, fileId)\n }\n\n resolveFile(fileId: string | undefined): FetchableValue<File | null> {\n return this.resolveResource(APIResourceType.File, {\n id: fileId,\n read: id => this.readFile(id),\n fetch: id => this.fetchFile(id),\n })\n }\n\n abstract fetchTypography(typographyId: string): Promise<Typography | null>\n\n readTypography(typographyId: string): Typography | null {\n return ApiClientState.getAPIResource(\n this.store.getState(),\n APIResourceType.Typography,\n typographyId,\n )\n }\n\n resolveTypography(typographyId: string | undefined): FetchableValue<Typography | null> {\n return this.resolveResource(APIResourceType.Typography, {\n id: typographyId,\n read: id => this.readTypography(id),\n fetch: id => this.fetchTypography(id),\n })\n }\n\n abstract fetchGlobalElement(globalElementId: string): Promise<GlobalElement | null>\n\n readGlobalElement(globalElementId: string): GlobalElement | null {\n return ApiClientState.getAPIResource(\n this.store.getState(),\n APIResourceType.GlobalElement,\n globalElementId,\n )\n }\n\n abstract fetchLocalizedGlobalElement({\n globalElementId,\n locale,\n }: {\n globalElementId: string\n locale: string\n }): Promise<LocalizedGlobalElement | null>\n\n readLocalizedGlobalElement({\n globalElementId,\n locale,\n }: {\n globalElementId: string\n locale: string\n }): LocalizedGlobalElement | null {\n return ApiClientState.getAPIResource(\n this.store.getState(),\n APIResourceType.LocalizedGlobalElement,\n globalElementId,\n locale,\n )\n }\n\n readPagePathnameSlice({\n pageId,\n locale,\n }: {\n pageId: string\n locale: string | null\n }): PagePathnameSlice | null {\n return ApiClientState.getAPIResource(\n this.store.getState(),\n APIResourceType.PagePathnameSlice,\n pageId,\n locale,\n )\n }\n\n abstract fetchPagePathnameSlice({\n pageId,\n locale,\n }: {\n pageId: string\n locale: string | null\n }): Promise<PagePathnameSlice | null>\n\n resolvePagePathnameSlice({\n pageId,\n locale,\n }: {\n pageId: string | undefined\n locale: string | null\n }): FetchableValue<PagePathnameSlice | null> {\n return this.resolveResource(APIResourceType.PagePathnameSlice, {\n id: pageId,\n read: id => this.readPagePathnameSlice({ pageId: id, locale }),\n fetch: id => this.fetchPagePathnameSlice({ pageId: id, locale }),\n })\n }\n\n resolveResource<R>(\n type: APIResourceType,\n {\n id,\n read,\n fetch,\n }: {\n id: string | undefined\n read: (id: string) => R | null\n fetch: (id: string) => Promise<R | null>\n },\n ): FetchableValue<R | null> {\n const _read = () => (id != null ? read(id) : null)\n let lastValue: R | null = null\n return {\n name: `${type}:${id}`,\n readStable: () => (lastValue = _read()),\n subscribe: (onUpdate: () => void) =>\n this.subscribe(() => {\n if (_read() !== lastValue) onUpdate()\n }),\n fetch: async () => (id != null ? fetch(id) : null),\n }\n }\n\n abstract fetchTable(tableId: string): Promise<Table | null>\n\n readTable(tableId: string): Table | null {\n return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.Table, tableId)\n }\n\n readSite(siteId: string): Site | null {\n return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.Site, siteId)\n }\n\n readPage(pageId: string): Page | null {\n return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.Page, pageId)\n }\n\n readSnippet(snippetId: string): Snippet | null {\n return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.Snippet, snippetId)\n }\n}\n"],"mappings":"AAGA,YAAY,oBAAoB;AAEhC;AAAA,EAWE;AAAA,OACK;AAIA,MAAM,YAAY;AAAA,EACvB,QAAmB;AACjB,WAAO;AAAA,MACL,cAAc,CAAC;AAAA,MACf,uBAAuB,CAAC;AAAA,IAC1B;AAAA,EACF;AACF;AAEO,MAAe,mBAAmB;AAAA,EAC9B;AAAA,EACA;AAAA,EAET,YAAY,EAAE,MAAM,GAA8B;AAChD,SAAK,QAAQ;AACb,SAAK,YAAY,KAAK,MAAM;AAAA,EAC9B;AAAA,EAIA,WAAW,UAAiC;AAC1C,WAAO,eAAe,eAAe,KAAK,MAAM,SAAS,GAAG,gBAAgB,QAAQ,QAAQ;AAAA,EAC9F;AAAA,EAEA,cAAc,UAA6D;AACzE,WAAO,KAAK,gBAAgB,gBAAgB,QAAQ;AAAA,MAClD,IAAI;AAAA,MACJ,MAAM,QAAM,KAAK,WAAW,EAAE;AAAA,MAC9B,OAAO,QAAM,KAAK,YAAY,EAAE;AAAA,IAClC,CAAC;AAAA,EACH;AAAA,EAIA,SAAS,QAA6B;AACpC,WAAO,eAAe,eAAe,KAAK,MAAM,SAAS,GAAG,gBAAgB,MAAM,MAAM;AAAA,EAC1F;AAAA,EAEA,YAAY,QAAyD;AACnE,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,OAAO,CAAC;AAAA,IACjE,CAAC;AAAA,EACH;AAAA,EAEA,gBACE,MACA;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAK0B;AAC1B,UAAM,QAAQ,MAAO,MAAM,OAAO,KAAK,EAAE,IAAI;AAC7C,QAAI,YAAsB;AAC1B,WAAO;AAAA,MACL,MAAM,GAAG,IAAI,IAAI,EAAE;AAAA,MACnB,YAAY,MAAO,YAAY,MAAM;AAAA,MACrC,WAAW,CAAC,aACV,KAAK,UAAU,MAAM;AACnB,YAAI,MAAM,MAAM;AAAW,mBAAS;AAAA,MACtC,CAAC;AAAA,MACH,OAAO,YAAa,MAAM,OAAO,MAAM,EAAE,IAAI;AAAA,IAC/C;AAAA,EACF;AAAA,EAIA,UAAU,SAA+B;AACvC,WAAO,eAAe,eAAe,KAAK,MAAM,SAAS,GAAG,gBAAgB,OAAO,OAAO;AAAA,EAC5F;AAAA,EAEA,SAAS,QAA6B;AACpC,WAAO,eAAe,eAAe,KAAK,MAAM,SAAS,GAAG,gBAAgB,MAAM,MAAM;AAAA,EAC1F;AAAA,EAEA,SAAS,QAA6B;AACpC,WAAO,eAAe,eAAe,KAAK,MAAM,SAAS,GAAG,gBAAgB,MAAM,MAAM;AAAA,EAC1F;AAAA,EAEA,YAAY,WAAmC;AAC7C,WAAO,eAAe,eAAe,KAAK,MAAM,SAAS,GAAG,gBAAgB,SAAS,SAAS;AAAA,EAChG;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/api/api-resources-client.ts"],"sourcesContent":["import { type FetchableValue } from '@makeswift/controls'\n\nimport { type Store as ApiClientStore } from '../state/api-client/store'\nimport * as ApiClientState from '../state/api-client/state'\nimport { fetchAPIResource } from '../state/api-client/fetch-api-resource'\n\nimport {\n type File,\n type GlobalElement,\n type LocalizedGlobalElement,\n type Page,\n type PagePathnameSlice,\n type Site,\n type Snippet,\n type Swatch,\n type Table,\n type Typography,\n APIResourceType,\n} from './types'\n\nimport { type SiteVersion } from './site-version'\n\nexport type CacheData = ApiClientState.SerializedState\n\nexport const CacheData = {\n empty(): CacheData {\n return {\n apiResources: {},\n localizedResourcesMap: {},\n }\n },\n}\n\nexport abstract class ApiResourcesClient {\n readonly store: ApiClientStore\n readonly subscribe: ApiClientStore['subscribe']\n\n constructor({ store }: { store: ApiClientStore }) {\n this.store = store\n this.subscribe = this.store.subscribe\n }\n\n async fetchSwatch(swatchId: string): Promise<Swatch | null> {\n const fetch = (id: string, version: SiteVersion | null) => this.fetchSwatchImpl(id, version)\n\n return await this.store.dispatch(fetchAPIResource(APIResourceType.Swatch, swatchId, fetch))\n }\n\n readSwatch(swatchId: string): Swatch | null {\n return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.Swatch, swatchId)\n }\n\n resolveSwatch(swatchId: string | undefined): FetchableValue<Swatch | null> {\n return this.resolveResource(APIResourceType.Swatch, {\n id: swatchId,\n read: id => this.readSwatch(id),\n fetch: id => this.fetchSwatch(id),\n })\n }\n\n async fetchFile(fileId: string): Promise<File | null> {\n const fetch = (id: string, version: SiteVersion | null) => this.fetchFileImpl(id, version)\n\n return await this.store.dispatch(fetchAPIResource(APIResourceType.File, fileId, fetch))\n }\n\n readFile(fileId: string): File | null {\n return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.File, fileId)\n }\n\n resolveFile(fileId: string | undefined): FetchableValue<File | null> {\n return this.resolveResource(APIResourceType.File, {\n id: fileId,\n read: id => this.readFile(id),\n fetch: id => this.fetchFile(id),\n })\n }\n\n async fetchTypography(typographyId: string): Promise<Typography | null> {\n const fetch = (id: string, version: SiteVersion | null) => this.fetchTypographyImpl(id, version)\n\n return await this.store.dispatch(\n fetchAPIResource(APIResourceType.Typography, typographyId, fetch),\n )\n }\n\n readTypography(typographyId: string): Typography | null {\n return ApiClientState.getAPIResource(\n this.store.getState(),\n APIResourceType.Typography,\n typographyId,\n )\n }\n\n resolveTypography(typographyId: string | undefined): FetchableValue<Typography | null> {\n return this.resolveResource(APIResourceType.Typography, {\n id: typographyId,\n read: id => this.readTypography(id),\n fetch: id => this.fetchTypography(id),\n })\n }\n\n async fetchGlobalElement(globalElementId: string): Promise<GlobalElement | null> {\n const fetch = (id: string, version: SiteVersion | null) =>\n this.fetchGlobalElementImpl(id, version)\n\n return await this.store.dispatch(\n fetchAPIResource(APIResourceType.GlobalElement, globalElementId, fetch),\n )\n }\n\n readGlobalElement(globalElementId: string): GlobalElement | null {\n return ApiClientState.getAPIResource(\n this.store.getState(),\n APIResourceType.GlobalElement,\n globalElementId,\n )\n }\n\n async fetchLocalizedGlobalElement({\n globalElementId,\n locale,\n }: {\n globalElementId: string\n locale: string\n }): Promise<LocalizedGlobalElement | null> {\n const fetch = (id: string, version: SiteVersion | null, locale: string | null | undefined) => {\n if (locale == null) throw new Error('Locale is required to fetch LocalizedGlobalElement')\n return this.fetchLocalizedGlobalElementImpl(id, version, locale)\n }\n\n return await this.store.dispatch(\n fetchAPIResource(APIResourceType.LocalizedGlobalElement, globalElementId, fetch, locale),\n )\n }\n\n readLocalizedGlobalElement({\n globalElementId,\n locale,\n }: {\n globalElementId: string\n locale: string\n }): LocalizedGlobalElement | null {\n return ApiClientState.getAPIResource(\n this.store.getState(),\n APIResourceType.LocalizedGlobalElement,\n globalElementId,\n locale,\n )\n }\n\n async fetchPagePathnameSlice({\n pageId,\n locale,\n }: {\n pageId: string\n locale: string | null\n }): Promise<PagePathnameSlice | null> {\n const fetch = (id: string, version: SiteVersion | null, locale: string | null | undefined) =>\n this.fetchPagePathnameSliceImpl(id, version, locale)\n\n return await this.store.dispatch(\n fetchAPIResource(APIResourceType.PagePathnameSlice, pageId, fetch, locale),\n )\n }\n\n readPagePathnameSlice({\n pageId,\n locale,\n }: {\n pageId: string\n locale: string | null\n }): PagePathnameSlice | null {\n return ApiClientState.getAPIResource(\n this.store.getState(),\n APIResourceType.PagePathnameSlice,\n pageId,\n locale,\n )\n }\n\n resolvePagePathnameSlice({\n pageId,\n locale,\n }: {\n pageId: string | undefined\n locale: string | null\n }): FetchableValue<PagePathnameSlice | null> {\n return this.resolveResource(APIResourceType.PagePathnameSlice, {\n id: pageId,\n read: id => this.readPagePathnameSlice({ pageId: id, locale }),\n fetch: id => this.fetchPagePathnameSlice({ pageId: id, locale }),\n })\n }\n\n resolveResource<R>(\n type: APIResourceType,\n {\n id,\n read,\n fetch,\n }: {\n id: string | undefined\n read: (id: string) => R | null\n fetch: (id: string) => Promise<R | null>\n },\n ): FetchableValue<R | null> {\n const _read = () => (id != null ? read(id) : null)\n let lastValue: R | null = null\n return {\n name: `${type}:${id}`,\n readStable: () => (lastValue = _read()),\n subscribe: (onUpdate: () => void) =>\n this.subscribe(() => {\n if (_read() !== lastValue) onUpdate()\n }),\n fetch: async () => (id != null ? fetch(id) : null),\n }\n }\n\n async fetchTable(tableId: string): Promise<Table | null> {\n const fetch = (id: string, version: SiteVersion | null) => this.fetchTableImpl(id, version)\n\n return await this.store.dispatch(fetchAPIResource(APIResourceType.Table, tableId, fetch))\n }\n\n readTable(tableId: string): Table | null {\n return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.Table, tableId)\n }\n\n readSite(siteId: string): Site | null {\n return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.Site, siteId)\n }\n\n readPage(pageId: string): Page | null {\n return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.Page, pageId)\n }\n\n readSnippet(snippetId: string): Snippet | null {\n return ApiClientState.getAPIResource(this.store.getState(), APIResourceType.Snippet, snippetId)\n }\n\n protected abstract fetchSwatchImpl(\n id: string,\n version: SiteVersion | null,\n ): Promise<Swatch | null>\n\n protected abstract fetchFileImpl(id: string, version: SiteVersion | null): Promise<File | null>\n protected abstract fetchTypographyImpl(\n id: string,\n version: SiteVersion | null,\n ): Promise<Typography | null>\n\n protected abstract fetchGlobalElementImpl(\n id: string,\n version: SiteVersion | null,\n ): Promise<GlobalElement | null>\n\n protected abstract fetchLocalizedGlobalElementImpl(\n id: string,\n version: SiteVersion | null,\n locale: string,\n ): Promise<LocalizedGlobalElement | null>\n\n protected abstract fetchPagePathnameSliceImpl(\n id: string,\n version: SiteVersion | null,\n locale: string | null | undefined,\n ): Promise<PagePathnameSlice | null>\n\n protected abstract fetchTableImpl(id: string, version: SiteVersion | null): Promise<Table | null>\n}\n"],"mappings":"AAGA,YAAY,oBAAoB;AAChC,SAAS,wBAAwB;AAEjC;AAAA,EAWE;AAAA,OACK;AAMA,MAAM,YAAY;AAAA,EACvB,QAAmB;AACjB,WAAO;AAAA,MACL,cAAc,CAAC;AAAA,MACf,uBAAuB,CAAC;AAAA,IAC1B;AAAA,EACF;AACF;AAEO,MAAe,mBAAmB;AAAA,EAC9B;AAAA,EACA;AAAA,EAET,YAAY,EAAE,MAAM,GAA8B;AAChD,SAAK,QAAQ;AACb,SAAK,YAAY,KAAK,MAAM;AAAA,EAC9B;AAAA,EAEA,MAAM,YAAY,UAA0C;AAC1D,UAAM,QAAQ,CAAC,IAAY,YAAgC,KAAK,gBAAgB,IAAI,OAAO;AAE3F,WAAO,MAAM,KAAK,MAAM,SAAS,iBAAiB,gBAAgB,QAAQ,UAAU,KAAK,CAAC;AAAA,EAC5F;AAAA,EAEA,WAAW,UAAiC;AAC1C,WAAO,eAAe,eAAe,KAAK,MAAM,SAAS,GAAG,gBAAgB,QAAQ,QAAQ;AAAA,EAC9F;AAAA,EAEA,cAAc,UAA6D;AACzE,WAAO,KAAK,gBAAgB,gBAAgB,QAAQ;AAAA,MAClD,IAAI;AAAA,MACJ,MAAM,QAAM,KAAK,WAAW,EAAE;AAAA,MAC9B,OAAO,QAAM,KAAK,YAAY,EAAE;AAAA,IAClC,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,UAAU,QAAsC;AACpD,UAAM,QAAQ,CAAC,IAAY,YAAgC,KAAK,cAAc,IAAI,OAAO;AAEzF,WAAO,MAAM,KAAK,MAAM,SAAS,iBAAiB,gBAAgB,MAAM,QAAQ,KAAK,CAAC;AAAA,EACxF;AAAA,EAEA,SAAS,QAA6B;AACpC,WAA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,MAAO,MAAM,OAAO,KAAK,EAAE,IAAI;AAC7C,QAAI,YAAsB;AAC1B,WAAO;AAAA,MACL,MAAM,GAAG,IAAI,IAAI,EAAE;AAAA,MACnB,YAAY,MAAO,YAAY,MAAM;AAAA,MACrC,WAAW,CAAC,aACV,KAAK,UAAU,MAAM;AACnB,YAAI,MAAM,MAAM;AAAW,mBAAS;AAAA,MACtC,CAAC;AAAA,MACH,OAAO,YAAa,MAAM,OAAO,MAAM,EAAE,IAAI;AAAA,IAC/C;AAAA,EACF;AAAA,EAEA,MAAM,WAAW,SAAwC;AACvD,UAAM,QAAQ,CAAC,IAAY,YAAgC,KAAK,eAAe,IAAI,OAAO;AAE1F,WAAO,MAAM,KAAK,MAAM,SAAS,iBAAiB,gBAAgB,OAAO,SAAS,KAAK,CAAC;AAAA,EAC1F;AAAA,EAEA,UAAU,SAA+B;AACvC,WAAO,eAAe,eAAe,KAAK,MAAM,SAAS,GAAG,gBAAgB,OAAO,OAAO;AAAA,EAC5F;AAAA,EAEA,SAAS,QAA6B;AACpC,WAAO,eAAe,eAAe,KAAK,MAAM,SAAS,GAAG,gBAAgB,MAAM,MAAM;AAAA,EAC1F;AAAA,EAEA,SAAS,QAA6B;AACpC,WAAO,eAAe,eAAe,KAAK,MAAM,SAAS,GAAG,gBAAgB,MAAM,MAAM;AAAA,EAC1F;AAAA,EAEA,YAAY,WAAmC;AAC7C,WAAO,eAAe,eAAe,KAAK,MAAM,SAAS,GAAG,gBAAgB,SAAS,SAAS;AAAA,EAChG;AA+BF;","names":["locale"]}
@@ -9,7 +9,7 @@ class MakeswiftGraphQLApiClient {
9
9
  graphqlClient;
10
10
  constructor({ endpoint }) {
11
11
  this.graphqlClient = new GraphQLClient(endpoint, {
12
- "makeswift-runtime-version": "0.28.7-canary.9"
12
+ "makeswift-runtime-version": "0.28.8-canary.0"
13
13
  });
14
14
  }
15
15
  async createTableRecord(tableId, columns) {
@@ -1,10 +1,6 @@
1
- import { fetchAPIResource } from "../state/api-client/fetch-api-resource";
2
1
  import { configureClientStore } from "../state/api-client/client-store";
3
- import {
4
- APIResourceType
5
- } from "./types";
2
+ import { ApiHandlerHeaders, serializeSiteVersion } from "./site-version";
6
3
  import { ApiResourcesClient } from "./api-resources-client";
7
- import { CacheData } from "./api-resources-client";
8
4
  class HostApiResourcesClient extends ApiResourcesClient {
9
5
  fetch;
10
6
  constructor({
@@ -16,44 +12,53 @@ class HostApiResourcesClient extends ApiResourcesClient {
16
12
  });
17
13
  this.fetch = fetch;
18
14
  }
19
- async fetchSwatch(swatchId) {
20
- return await this.store.dispatch(fetchAPIResource(APIResourceType.Swatch, swatchId, this.fetch));
15
+ async fetchSwatchImpl(id, version) {
16
+ return await this.fetchVersioned(`/api/makeswift/swatches/${id}`, version);
21
17
  }
22
- async fetchFile(fileId) {
23
- return await this.store.dispatch(fetchAPIResource(APIResourceType.File, fileId, this.fetch));
18
+ async fetchFileImpl(id, version) {
19
+ return await this.fetchVersioned(`/api/makeswift/files/${id}`, version);
24
20
  }
25
- async fetchTypography(typographyId) {
26
- return await this.store.dispatch(
27
- fetchAPIResource(APIResourceType.Typography, typographyId, this.fetch)
28
- );
21
+ async fetchTypographyImpl(id, version) {
22
+ return await this.fetchVersioned(`/api/makeswift/typographies/${id}`, version);
29
23
  }
30
- async fetchGlobalElement(globalElementId) {
31
- return await this.store.dispatch(
32
- fetchAPIResource(APIResourceType.GlobalElement, globalElementId, this.fetch)
33
- );
24
+ async fetchGlobalElementImpl(id, version) {
25
+ return await this.fetchVersioned(`/api/makeswift/global-elements/${id}`, version);
34
26
  }
35
- async fetchLocalizedGlobalElement({
36
- globalElementId,
37
- locale
38
- }) {
39
- return await this.store.dispatch(
40
- fetchAPIResource(APIResourceType.LocalizedGlobalElement, globalElementId, this.fetch, locale)
27
+ async fetchLocalizedGlobalElementImpl(id, version, locale) {
28
+ return await this.fetchVersioned(
29
+ `/api/makeswift/localized-global-elements/${id}/${locale}`,
30
+ version
41
31
  );
42
32
  }
43
- async fetchPagePathnameSlice({
44
- pageId,
45
- locale
46
- }) {
47
- return await this.store.dispatch(
48
- fetchAPIResource(APIResourceType.PagePathnameSlice, pageId, this.fetch, locale)
49
- );
33
+ async fetchPagePathnameSliceImpl(id, version, locale) {
34
+ const url = new URL(`/api/makeswift/page-pathname-slices/${id}`, "http://n");
35
+ if (locale != null)
36
+ url.searchParams.set("locale", locale);
37
+ return await this.fetchVersioned(url.pathname + url.search, version);
38
+ }
39
+ async fetchTableImpl(id, version) {
40
+ return await this.fetchVersioned(`/api/makeswift/tables/${id}`, version);
50
41
  }
51
- async fetchTable(tableId) {
52
- return await this.store.dispatch(fetchAPIResource(APIResourceType.Table, tableId, this.fetch));
42
+ async fetchVersioned(url, version) {
43
+ const response = await this.fetch(url, {
44
+ headers: {
45
+ "Content-Type": "application/json",
46
+ ...version != null ? { [ApiHandlerHeaders.SiteVersion]: serializeSiteVersion(version) } : {}
47
+ }
48
+ });
49
+ if (response.status === 404)
50
+ return null;
51
+ if (!response.ok)
52
+ throw new Error(response.statusText);
53
+ if (response.headers.get("content-type")?.includes("application/json") !== true) {
54
+ throw new Error(
55
+ `Expected JSON response from "${url}" but got "${response.headers.get("content-type")}"`
56
+ );
57
+ }
58
+ return response.json();
53
59
  }
54
60
  }
55
61
  export {
56
- CacheData,
57
62
  HostApiResourcesClient
58
63
  };
59
64
  //# sourceMappingURL=host-api-resources-client.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/api/host-api-resources-client.ts"],"sourcesContent":["import { type State as ApiClientState } from '../state/api-client/state'\nimport { fetchAPIResource } from '../state/api-client/fetch-api-resource'\nimport { configureClientStore } from '../state/api-client/client-store'\n\nimport {\n type File,\n type GlobalElement,\n type LocalizedGlobalElement,\n type PagePathnameSlice,\n type Swatch,\n type Table,\n type Typography,\n type HttpFetch,\n APIResourceType,\n} from './types'\n\nimport { ApiResourcesClient } from './api-resources-client'\n\nexport { CacheData } from './api-resources-client'\n\n/**\n * NOTE(miguel): This \"client\" is used to fetch Makeswift API resources needed for the host. For\n * example, swatches, files, typographies, etc. Ideally it's internal to the runtime and is only\n * used by controls to transform API references to API resources.\n *\n * Moreover, its use should be reserved for the builder only, since for live pages all Makeswift\n * API resources should be embedded in the \"page snapshot\". In the builder, this client serves the\n * purpose of sending requests for API resources and keeping a cache so that changes that happen in\n * the builder, like modifying a swatch, can be sent via `postMessage` to the host and the cache can\n * immediately update the value and re-render.\n *\n * Furthermore, the API resources requested shouldn't be requested directly from the Makeswift API\n * as that would require those resources to not be authenticated since the requests come from the\n * browser when running the host. Instead, the requests should go to the host directly, at the\n * Makeswift API endpoint (i.g., `/api/makeswift/[...makeswift]` dynamic route) where the host's\n * API key can be used, securely, in the server. For this reason, this client should really be a\n * client of the host's API, not Makeswift's, intended to build and continuously maintain a realtime\n * snapshot for use in the builder, not the lives pages.\n */\nexport class HostApiResourcesClient extends ApiResourcesClient {\n readonly fetch: HttpFetch\n\n constructor({\n fetch,\n preloadedState,\n }: {\n fetch: HttpFetch\n preloadedState: Partial<ApiClientState>\n }) {\n super({\n store: configureClientStore({ preloadedState }),\n })\n\n this.fetch = fetch\n }\n\n async fetchSwatch(swatchId: string): Promise<Swatch | null> {\n return await this.store.dispatch(fetchAPIResource(APIResourceType.Swatch, swatchId, this.fetch))\n }\n\n async fetchFile(fileId: string): Promise<File | null> {\n return await this.store.dispatch(fetchAPIResource(APIResourceType.File, fileId, this.fetch))\n }\n\n async fetchTypography(typographyId: string): Promise<Typography | null> {\n return await this.store.dispatch(\n fetchAPIResource(APIResourceType.Typography, typographyId, this.fetch),\n )\n }\n\n async fetchGlobalElement(globalElementId: string): Promise<GlobalElement | null> {\n return await this.store.dispatch(\n fetchAPIResource(APIResourceType.GlobalElement, globalElementId, this.fetch),\n )\n }\n\n async fetchLocalizedGlobalElement({\n globalElementId,\n locale,\n }: {\n globalElementId: string\n locale: string\n }): Promise<LocalizedGlobalElement | null> {\n return await this.store.dispatch(\n fetchAPIResource(APIResourceType.LocalizedGlobalElement, globalElementId, this.fetch, locale),\n )\n }\n\n async fetchPagePathnameSlice({\n pageId,\n locale,\n }: {\n pageId: string\n locale: string | null\n }): Promise<PagePathnameSlice | null> {\n return await this.store.dispatch(\n fetchAPIResource(APIResourceType.PagePathnameSlice, pageId, this.fetch, locale),\n )\n }\n\n async fetchTable(tableId: string): Promise<Table | null> {\n return await this.store.dispatch(fetchAPIResource(APIResourceType.Table, tableId, this.fetch))\n }\n}\n"],"mappings":"AACA,SAAS,wBAAwB;AACjC,SAAS,4BAA4B;AAErC;AAAA,EASE;AAAA,OACK;AAEP,SAAS,0BAA0B;AAEnC,SAAS,iBAAiB;AAqBnB,MAAM,+BAA+B,mBAAmB;AAAA,EACpD;AAAA,EAET,YAAY;AAAA,IACV;AAAA,IACA;AAAA,EACF,GAGG;AACD,UAAM;AAAA,MACJ,OAAO,qBAAqB,EAAE,eAAe,CAAC;AAAA,IAChD,CAAC;AAED,SAAK,QAAQ;AAAA,EACf;AAAA,EAEA,MAAM,YAAY,UAA0C;AAC1D,WAAO,MAAM,KAAK,MAAM,SAAS,iBAAiB,gBAAgB,QAAQ,UAAU,KAAK,KAAK,CAAC;AAAA,EACjG;AAAA,EAEA,MAAM,UAAU,QAAsC;AACpD,WAAO,MAAM,KAAK,MAAM,SAAS,iBAAiB,gBAAgB,MAAM,QAAQ,KAAK,KAAK,CAAC;AAAA,EAC7F;AAAA,EAEA,MAAM,gBAAgB,cAAkD;AACtE,WAAO,MAAM,KAAK,MAAM;AAAA,MACtB,iBAAiB,gBAAgB,YAAY,cAAc,KAAK,KAAK;AAAA,IACvE;AAAA,EACF;AAAA,EAEA,MAAM,mBAAmB,iBAAwD;AAC/E,WAAO,MAAM,KAAK,MAAM;AAAA,MACtB,iBAAiB,gBAAgB,eAAe,iBAAiB,KAAK,KAAK;AAAA,IAC7E;AAAA,EACF;AAAA,EAEA,MAAM,4BAA4B;AAAA,IAChC;AAAA,IACA;AAAA,EACF,GAG2C;AACzC,WAAO,MAAM,KAAK,MAAM;AAAA,MACtB,iBAAiB,gBAAgB,wBAAwB,iBAAiB,KAAK,OAAO,MAAM;AAAA,IAC9F;AAAA,EACF;AAAA,EAEA,MAAM,uBAAuB;AAAA,IAC3B;AAAA,IACA;AAAA,EACF,GAGsC;AACpC,WAAO,MAAM,KAAK,MAAM;AAAA,MACtB,iBAAiB,gBAAgB,mBAAmB,QAAQ,KAAK,OAAO,MAAM;AAAA,IAChF;AAAA,EACF;AAAA,EAEA,MAAM,WAAW,SAAwC;AACvD,WAAO,MAAM,KAAK,MAAM,SAAS,iBAAiB,gBAAgB,OAAO,SAAS,KAAK,KAAK,CAAC;AAAA,EAC/F;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/api/host-api-resources-client.ts"],"sourcesContent":["import { type State as ApiClientState } from '../state/api-client/state'\n\nimport { configureClientStore } from '../state/api-client/client-store'\n\nimport {\n type File,\n type GlobalElement,\n type LocalizedGlobalElement,\n type PagePathnameSlice,\n type Swatch,\n type Table,\n type Typography,\n type HttpFetch,\n} from './types'\n\nimport { type SiteVersion, ApiHandlerHeaders, serializeSiteVersion } from './site-version'\nimport { ApiResourcesClient } from './api-resources-client'\n\n/**\n * NOTE(miguel): This \"client\" is used to fetch Makeswift API resources needed for the host. For\n * example, swatches, files, typographies, etc. Ideally it's internal to the runtime and is only\n * used by controls to transform API references to API resources.\n *\n * Moreover, its use should be reserved for the builder only, since for live pages all Makeswift\n * API resources should be embedded in the \"page snapshot\". In the builder, this client serves the\n * purpose of sending requests for API resources and keeping a cache so that changes that happen in\n * the builder, like modifying a swatch, can be sent via `postMessage` to the host and the cache can\n * immediately update the value and re-render.\n *\n * Furthermore, the API resources requested shouldn't be requested directly from the Makeswift API\n * as that would require those resources to not be authenticated since the requests come from the\n * browser when running the host. Instead, the requests should go to the host directly, at the\n * Makeswift API endpoint (i.g., `/api/makeswift/[...makeswift]` dynamic route) where the host's\n * API key can be used, securely, in the server. For this reason, this client should really be a\n * client of the host's API, not Makeswift's, intended to build and continuously maintain a realtime\n * snapshot for use in the builder, not the lives pages.\n */\nexport class HostApiResourcesClient extends ApiResourcesClient {\n readonly fetch: HttpFetch\n\n constructor({\n fetch,\n preloadedState,\n }: {\n fetch: HttpFetch\n preloadedState: Partial<ApiClientState>\n }) {\n super({\n store: configureClientStore({ preloadedState }),\n })\n\n this.fetch = fetch\n }\n\n protected async fetchSwatchImpl(id: string, version: SiteVersion | null): Promise<Swatch | null> {\n return await this.fetchVersioned<Swatch>(`/api/makeswift/swatches/${id}`, version)\n }\n\n protected async fetchFileImpl(id: string, version: SiteVersion | null): Promise<File | null> {\n return await this.fetchVersioned<File>(`/api/makeswift/files/${id}`, version)\n }\n\n protected async fetchTypographyImpl(\n id: string,\n version: SiteVersion | null,\n ): Promise<Typography | null> {\n return await this.fetchVersioned<Typography>(`/api/makeswift/typographies/${id}`, version)\n }\n\n protected async fetchGlobalElementImpl(\n id: string,\n version: SiteVersion | null,\n ): Promise<GlobalElement | null> {\n return await this.fetchVersioned<GlobalElement>(`/api/makeswift/global-elements/${id}`, version)\n }\n\n protected async fetchLocalizedGlobalElementImpl(\n id: string,\n version: SiteVersion | null,\n locale: string,\n ): Promise<LocalizedGlobalElement | null> {\n return await this.fetchVersioned<LocalizedGlobalElement>(\n `/api/makeswift/localized-global-elements/${id}/${locale}`,\n version,\n )\n }\n\n protected async fetchPagePathnameSliceImpl(\n id: string,\n version: SiteVersion | null,\n locale: string | null | undefined,\n ): Promise<PagePathnameSlice | null> {\n const url = new URL(`/api/makeswift/page-pathname-slices/${id}`, 'http://n')\n\n if (locale != null) url.searchParams.set('locale', locale)\n\n return await this.fetchVersioned<PagePathnameSlice>(url.pathname + url.search, version)\n }\n\n protected async fetchTableImpl(id: string, version: SiteVersion | null): Promise<Table | null> {\n return await this.fetchVersioned<Table>(`/api/makeswift/tables/${id}`, version)\n }\n\n private async fetchVersioned<T>(url: string, version: SiteVersion | null): Promise<T | null> {\n const response = await this.fetch(url, {\n headers: {\n 'Content-Type': 'application/json',\n ...(version != null\n ? { [ApiHandlerHeaders.SiteVersion]: serializeSiteVersion(version) }\n : {}),\n },\n })\n\n if (response.status === 404) return null\n if (!response.ok) throw new Error(response.statusText)\n\n if (response.headers.get('content-type')?.includes('application/json') !== true) {\n throw new Error(\n `Expected JSON response from \"${url}\" but got \"${response.headers.get('content-type')}\"`,\n )\n }\n\n return response.json()\n }\n}\n"],"mappings":"AAEA,SAAS,4BAA4B;AAarC,SAA2B,mBAAmB,4BAA4B;AAC1E,SAAS,0BAA0B;AAqB5B,MAAM,+BAA+B,mBAAmB;AAAA,EACpD;AAAA,EAET,YAAY;AAAA,IACV;AAAA,IACA;AAAA,EACF,GAGG;AACD,UAAM;AAAA,MACJ,OAAO,qBAAqB,EAAE,eAAe,CAAC;AAAA,IAChD,CAAC;AAED,SAAK,QAAQ;AAAA,EACf;AAAA,EAEA,MAAgB,gBAAgB,IAAY,SAAqD;AAC/F,WAAO,MAAM,KAAK,eAAuB,2BAA2B,EAAE,IAAI,OAAO;AAAA,EACnF;AAAA,EAEA,MAAgB,cAAc,IAAY,SAAmD;AAC3F,WAAO,MAAM,KAAK,eAAqB,wBAAwB,EAAE,IAAI,OAAO;AAAA,EAC9E;AAAA,EAEA,MAAgB,oBACd,IACA,SAC4B;AAC5B,WAAO,MAAM,KAAK,eAA2B,+BAA+B,EAAE,IAAI,OAAO;AAAA,EAC3F;AAAA,EAEA,MAAgB,uBACd,IACA,SAC+B;AAC/B,WAAO,MAAM,KAAK,eAA8B,kCAAkC,EAAE,IAAI,OAAO;AAAA,EACjG;AAAA,EAEA,MAAgB,gCACd,IACA,SACA,QACwC;AACxC,WAAO,MAAM,KAAK;AAAA,MAChB,4CAA4C,EAAE,IAAI,MAAM;AAAA,MACxD;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAgB,2BACd,IACA,SACA,QACmC;AACnC,UAAM,MAAM,IAAI,IAAI,uCAAuC,EAAE,IAAI,UAAU;AAE3E,QAAI,UAAU;AAAM,UAAI,aAAa,IAAI,UAAU,MAAM;AAEzD,WAAO,MAAM,KAAK,eAAkC,IAAI,WAAW,IAAI,QAAQ,OAAO;AAAA,EACxF;AAAA,EAEA,MAAgB,eAAe,IAAY,SAAoD;AAC7F,WAAO,MAAM,KAAK,eAAsB,yBAAyB,EAAE,IAAI,OAAO;AAAA,EAChF;AAAA,EAEA,MAAc,eAAkB,KAAa,SAAgD;AAC3F,UAAM,WAAW,MAAM,KAAK,MAAM,KAAK;AAAA,MACrC,SAAS;AAAA,QACP,gBAAgB;AAAA,QAChB,GAAI,WAAW,OACX,EAAE,CAAC,kBAAkB,WAAW,GAAG,qBAAqB,OAAO,EAAE,IACjE,CAAC;AAAA,MACP;AAAA,IACF,CAAC;AAED,QAAI,SAAS,WAAW;AAAK,aAAO;AACpC,QAAI,CAAC,SAAS;AAAI,YAAM,IAAI,MAAM,SAAS,UAAU;AAErD,QAAI,SAAS,QAAQ,IAAI,cAAc,GAAG,SAAS,kBAAkB,MAAM,MAAM;AAC/E,YAAM,IAAI;AAAA,QACR,gCAAgC,GAAG,cAAc,SAAS,QAAQ,IAAI,cAAc,CAAC;AAAA,MACvF;AAAA,IACF;AAEA,WAAO,SAAS,KAAK;AAAA,EACvB;AACF;","names":[]}
@@ -0,0 +1,46 @@
1
+ import { configureClientStore } from "../state/api-client/client-store";
2
+ import { ApiResourcesClient } from "./api-resources-client";
3
+ import { MakeswiftGraphQLApiClient } from "./graphql-api-client";
4
+ import { MakeswiftRestAPIClient } from "./rest-api-client";
5
+ class MakeswiftApiResourcesClient extends ApiResourcesClient {
6
+ restApiClient;
7
+ graphQlClient;
8
+ constructor({
9
+ fetch,
10
+ apiKey,
11
+ apiOrigin,
12
+ graphqlApiEndpoint,
13
+ preloadedState
14
+ }) {
15
+ super({
16
+ store: configureClientStore({ preloadedState })
17
+ });
18
+ this.restApiClient = new MakeswiftRestAPIClient({ fetch, apiKey, apiOrigin });
19
+ this.graphQlClient = new MakeswiftGraphQLApiClient({ endpoint: graphqlApiEndpoint });
20
+ }
21
+ async fetchSwatchImpl(id, version) {
22
+ return this.restApiClient.getSwatch(id, version);
23
+ }
24
+ async fetchFileImpl(id, _version) {
25
+ return this.graphQlClient.getFile(id);
26
+ }
27
+ async fetchTypographyImpl(id, version) {
28
+ return this.restApiClient.getTypography(id, version);
29
+ }
30
+ async fetchGlobalElementImpl(id, version) {
31
+ return this.restApiClient.getGlobalElement(id, version);
32
+ }
33
+ async fetchLocalizedGlobalElementImpl(id, version, locale) {
34
+ return this.restApiClient.getLocalizedGlobalElement(id, locale, version);
35
+ }
36
+ async fetchPagePathnameSliceImpl(id, version, locale) {
37
+ return this.restApiClient.getPagePathnameSlice(id, version, { locale });
38
+ }
39
+ async fetchTableImpl(id, _version) {
40
+ return this.graphQlClient.getTable(id);
41
+ }
42
+ }
43
+ export {
44
+ MakeswiftApiResourcesClient
45
+ };
46
+ //# sourceMappingURL=makeswift-api-resources-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/api/makeswift-api-resources-client.ts"],"sourcesContent":["import { type State as ApiClientState } from '../state/api-client/state'\nimport { configureClientStore } from '../state/api-client/client-store'\n\nimport {\n type File,\n type GlobalElement,\n type LocalizedGlobalElement,\n type PagePathnameSlice,\n type Swatch,\n type Table,\n type Typography,\n type HttpFetch,\n} from './types'\n\nimport { ApiResourcesClient } from './api-resources-client'\nimport { MakeswiftGraphQLApiClient } from './graphql-api-client'\nimport { MakeswiftRestAPIClient } from './rest-api-client'\nimport { type SiteVersion } from './site-version'\n\n/**\n * Server-side implementation that makes direct authenticated requests to the Makeswift API.\n * Requires an API key.\n *\n * For client-side usage, see `HostApiResourcesClient`, which goes through the host via\n * `/api/makeswift/...` endpoints.\n */\nexport class MakeswiftApiResourcesClient extends ApiResourcesClient {\n private restApiClient: MakeswiftRestAPIClient\n private graphQlClient: MakeswiftGraphQLApiClient\n\n constructor({\n fetch,\n apiKey,\n apiOrigin,\n graphqlApiEndpoint,\n preloadedState,\n }: {\n fetch: HttpFetch\n apiKey: string\n apiOrigin: string\n graphqlApiEndpoint: string\n preloadedState: Partial<ApiClientState>\n }) {\n super({\n store: configureClientStore({ preloadedState }),\n })\n\n this.restApiClient = new MakeswiftRestAPIClient({ fetch, apiKey, apiOrigin })\n this.graphQlClient = new MakeswiftGraphQLApiClient({ endpoint: graphqlApiEndpoint })\n }\n\n protected async fetchSwatchImpl(id: string, version: SiteVersion | null): Promise<Swatch | null> {\n return this.restApiClient.getSwatch(id, version)\n }\n\n protected async fetchFileImpl(id: string, _version: SiteVersion | null): Promise<File | null> {\n // files are unversioned\n return this.graphQlClient.getFile(id)\n }\n\n protected async fetchTypographyImpl(\n id: string,\n version: SiteVersion | null,\n ): Promise<Typography | null> {\n return this.restApiClient.getTypography(id, version)\n }\n\n protected async fetchGlobalElementImpl(\n id: string,\n version: SiteVersion | null,\n ): Promise<GlobalElement | null> {\n return this.restApiClient.getGlobalElement(id, version)\n }\n\n protected async fetchLocalizedGlobalElementImpl(\n id: string,\n version: SiteVersion | null,\n locale: string,\n ): Promise<LocalizedGlobalElement | null> {\n return this.restApiClient.getLocalizedGlobalElement(id, locale, version)\n }\n\n protected async fetchPagePathnameSliceImpl(\n id: string,\n version: SiteVersion | null,\n locale: string | null | undefined,\n ): Promise<PagePathnameSlice | null> {\n return this.restApiClient.getPagePathnameSlice(id, version, { locale })\n }\n\n protected async fetchTableImpl(id: string, _version: SiteVersion | null): Promise<Table | null> {\n // tables are unversioned\n return this.graphQlClient.getTable(id)\n }\n}\n"],"mappings":"AACA,SAAS,4BAA4B;AAarC,SAAS,0BAA0B;AACnC,SAAS,iCAAiC;AAC1C,SAAS,8BAA8B;AAUhC,MAAM,oCAAoC,mBAAmB;AAAA,EAC1D;AAAA,EACA;AAAA,EAER,YAAY;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAMG;AACD,UAAM;AAAA,MACJ,OAAO,qBAAqB,EAAE,eAAe,CAAC;AAAA,IAChD,CAAC;AAED,SAAK,gBAAgB,IAAI,uBAAuB,EAAE,OAAO,QAAQ,UAAU,CAAC;AAC5E,SAAK,gBAAgB,IAAI,0BAA0B,EAAE,UAAU,mBAAmB,CAAC;AAAA,EACrF;AAAA,EAEA,MAAgB,gBAAgB,IAAY,SAAqD;AAC/F,WAAO,KAAK,cAAc,UAAU,IAAI,OAAO;AAAA,EACjD;AAAA,EAEA,MAAgB,cAAc,IAAY,UAAoD;AAE5F,WAAO,KAAK,cAAc,QAAQ,EAAE;AAAA,EACtC;AAAA,EAEA,MAAgB,oBACd,IACA,SAC4B;AAC5B,WAAO,KAAK,cAAc,cAAc,IAAI,OAAO;AAAA,EACrD;AAAA,EAEA,MAAgB,uBACd,IACA,SAC+B;AAC/B,WAAO,KAAK,cAAc,iBAAiB,IAAI,OAAO;AAAA,EACxD;AAAA,EAEA,MAAgB,gCACd,IACA,SACA,QACwC;AACxC,WAAO,KAAK,cAAc,0BAA0B,IAAI,QAAQ,OAAO;AAAA,EACzE;AAAA,EAEA,MAAgB,2BACd,IACA,SACA,QACmC;AACnC,WAAO,KAAK,cAAc,qBAAqB,IAAI,SAAS,EAAE,OAAO,CAAC;AAAA,EACxE;AAAA,EAEA,MAAgB,eAAe,IAAY,UAAqD;AAE9F,WAAO,KAAK,cAAc,SAAS,EAAE;AAAA,EACvC;AACF;","names":[]}
@@ -113,7 +113,7 @@ class MakeswiftRestAPIClient {
113
113
  const requestHeaders = new Headers({
114
114
  "x-api-key": this.apiKey,
115
115
  "makeswift-site-api-key": this.apiKey,
116
- "makeswift-runtime-version": "0.28.7-canary.9"
116
+ "makeswift-runtime-version": "0.28.8-canary.0"
117
117
  });
118
118
  if (siteVersion?.token) {
119
119
  requestUrl.searchParams.set("version", siteVersion.version);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/api/rest-api-client.ts"],"sourcesContent":["import {\n type GlobalElement,\n type LocalizedGlobalElement,\n type PagePathnameSlice,\n type Swatch,\n type Typography,\n type HttpFetch,\n} from './types'\n\nimport { type SiteVersion } from './site-version'\nimport * as Schema from './schema'\n\nexport class MakeswiftRestAPIClient {\n private _fetch: HttpFetch\n\n readonly apiKey: string\n readonly apiOrigin: string\n\n constructor({\n fetch,\n apiKey,\n apiOrigin,\n }: {\n fetch: HttpFetch\n apiKey: string\n apiOrigin: string\n }) {\n this._fetch = fetch\n this.apiKey = apiKey\n this.apiOrigin = apiOrigin\n }\n\n async getSwatch(swatchId: string, siteVersion: SiteVersion | null): Promise<Swatch | null> {\n const response = await this.fetch(`v3/swatches/${swatchId}`, siteVersion)\n\n if (!response.ok) {\n const failedBody = await failedResponseBody(response)\n if (response.status !== 404) {\n console.error(`Failed to get swatch '${swatchId}'`, {\n response: failedBody,\n siteVersion,\n })\n }\n\n return null\n }\n\n const swatch = await response.json()\n\n return swatch\n }\n\n async getTypography(\n typographyId: string,\n siteVersion: SiteVersion | null,\n ): Promise<Typography | null> {\n const response = await this.fetch(`v3/typographies/${typographyId}`, siteVersion)\n\n if (!response.ok) {\n const failedBody = await failedResponseBody(response)\n if (response.status !== 404) {\n console.error(`Failed to get typography '${typographyId}'`, {\n response: failedBody,\n siteVersion,\n })\n }\n\n return null\n }\n\n const typography = await response.json()\n\n return typography\n }\n\n async getGlobalElement(\n globalElementId: string,\n siteVersion: SiteVersion | null,\n ): Promise<GlobalElement | null> {\n const response = await this.fetch(`v3/global-elements/${globalElementId}`, siteVersion)\n\n if (!response.ok) {\n const failedBody = await failedResponseBody(response)\n if (response.status !== 404) {\n console.error(`Failed to get global element '${globalElementId}'`, {\n response: failedBody,\n siteVersion,\n })\n }\n\n return null\n }\n\n const globalElement = await response.json()\n\n return globalElement\n }\n\n async getLocalizedGlobalElement(\n globalElementId: string,\n locale: string,\n siteVersion: SiteVersion | null,\n ): Promise<LocalizedGlobalElement | null> {\n const response = await this.fetch(\n `v3/localized-global-elements/${globalElementId}?locale=${locale}`,\n siteVersion,\n )\n\n if (!response.ok) {\n const failedBody = await failedResponseBody(response)\n if (response.status !== 404) {\n console.error(`Failed to get localized global element '${globalElementId}'`, {\n response: failedBody,\n siteVersion,\n locale,\n })\n }\n\n return null\n }\n\n const localizedGlobalElement = await response.json()\n\n return localizedGlobalElement\n }\n\n async getPagePathnameSlices(\n pageIds: string[],\n siteVersion: SiteVersion | null,\n { locale }: { locale?: string | null },\n ): Promise<(PagePathnameSlice | null)[]> {\n if (pageIds.length === 0) return []\n\n const url = new URL(`v3/page-pathname-slices/bulk`, this.apiOrigin)\n\n pageIds.forEach(id => url.searchParams.append('ids', id))\n if (locale != null) url.searchParams.set('locale', locale)\n\n const response = await this.fetch(url.pathname + url.search, siteVersion)\n\n if (!response.ok) {\n console.error(`Failed to get page pathname slice(s) for ${pageIds.join(', ')}`, {\n response: await failedResponseBody(response),\n siteVersion,\n locale,\n })\n\n return []\n }\n\n const json = await response.json()\n\n const pagePathnameSlices = Schema.pagePathnameSlices.parse(json)\n\n // We're mapping the basePageId to be the id, because we're still using the GraphQL\n // fragment as our APIResource. The id on the APIResource needs to match the pageId\n // so that we can find the corresponding page pathname slice when we call getPagePathnameSlice(pageId).\n // TODO: Update this once we move away from the GraphQL fragments.\n return pagePathnameSlices.map(pagePathnameSlice => {\n if (pagePathnameSlice == null) return null\n\n return {\n ...pagePathnameSlice,\n id: pagePathnameSlice.basePageId,\n localizedPathname: pagePathnameSlice.localizedPathname ?? null,\n }\n })\n }\n\n async getPagePathnameSlice(\n pageId: string,\n siteVersion: SiteVersion | null,\n { locale }: { locale?: string } = {},\n ): Promise<PagePathnameSlice | null> {\n const pagePathnameSlices = await this.getPagePathnameSlices([pageId], siteVersion, { locale })\n\n return pagePathnameSlices.at(0) ?? null\n }\n\n protected async fetch(\n path: string,\n siteVersion: SiteVersion | null,\n init?: RequestInit,\n ): Promise<Response> {\n const requestUrl = new URL(path, this.apiOrigin)\n\n const requestHeaders = new Headers({\n 'x-api-key': this.apiKey,\n 'makeswift-site-api-key': this.apiKey,\n 'makeswift-runtime-version': PACKAGE_VERSION,\n })\n\n if (siteVersion?.token) {\n requestUrl.searchParams.set('version', siteVersion.version)\n requestHeaders.set('makeswift-preview-token', siteVersion.token)\n }\n\n if (init?.headers) {\n new Headers(init.headers).forEach((value, key) => {\n requestHeaders.set(key, value)\n })\n }\n\n const response = await this._fetch(requestUrl.toString(), {\n ...init,\n headers: requestHeaders,\n ...(siteVersion != null ? { cache: 'no-store' } : {}),\n })\n\n return response\n }\n}\n\n// This function attempts to consume the response body of a failed response, and\n// returns either the parsed JSON or raw text. This is useful for logging more\n// detailed error information when an API request fails.\n//\n// Cloudflare Worker Note: The Cloudflare Worker runtime has automatic deadlock\n// prevention (in the form of auto-cancelling responses) that triggers when too\n// many response bodies are unconsumed. This applies for error responses as\n// well. As such, in this client we use this function to consume the response\n// body whenever the request fails, even if we don't end up logging the body\n// itself, to avoid hitting the deadlock prevention.\nexport async function failedResponseBody(response: Response): Promise<unknown> {\n try {\n const text = await response.text()\n try {\n return JSON.parse(text)\n } catch {\n return text\n }\n } catch (e) {\n return `Failed to extract response body: ${e}`\n }\n}\n"],"mappings":"AAUA,YAAY,YAAY;AAEjB,MAAM,uBAAuB;AAAA,EAC1B;AAAA,EAEC;AAAA,EACA;AAAA,EAET,YAAY;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAIG;AACD,SAAK,SAAS;AACd,SAAK,SAAS;AACd,SAAK,YAAY;AAAA,EACnB;AAAA,EAEA,MAAM,UAAU,UAAkB,aAAyD;AACzF,UAAM,WAAW,MAAM,KAAK,MAAM,eAAe,QAAQ,IAAI,WAAW;AAExE,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,aAAa,MAAM,mBAAmB,QAAQ;AACpD,UAAI,SAAS,WAAW,KAAK;AAC3B,gBAAQ,MAAM,yBAAyB,QAAQ,KAAK;AAAA,UAClD,UAAU;AAAA,UACV;AAAA,QACF,CAAC;AAAA,MACH;AAEA,aAAO;AAAA,IACT;AAEA,UAAM,SAAS,MAAM,SAAS,KAAK;AAEnC,WAAO;AAAA,EACT;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BAAW,aAAa,IAAI,WAAW,YAAY,OAAO;AAC1D,qBAAe,IAAI,2BAA2B,YAAY,KAAK;AAAA,IACjE;AAEA,QAAI,MAAM,SAAS;AACjB,UAAI,QAAQ,KAAK,OAAO,EAAE,QAAQ,CAAC,OAAO,QAAQ;AAChD,uBAAe,IAAI,KAAK,KAAK;AAAA,MAC/B,CAAC;AAAA,IACH;AAEA,UAAM,WAAW,MAAM,KAAK,OAAO,WAAW,SAAS,GAAG;AAAA,MACxD,GAAG;AAAA,MACH,SAAS;AAAA,MACT,GAAI,eAAe,OAAO,EAAE,OAAO,WAAW,IAAI,CAAC;AAAA,IACrD,CAAC;AAED,WAAO;AAAA,EACT;AACF;AAYA,eAAsB,mBAAmB,UAAsC;AAC7E,MAAI;AACF,UAAM,OAAO,MAAM,SAAS,KAAK;AACjC,QAAI;AACF,aAAO,KAAK,MAAM,IAAI;AAAA,IACxB,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF,SAAS,GAAG;AACV,WAAO,oCAAoC,CAAC;AAAA,EAC9C;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/api/rest-api-client.ts"],"sourcesContent":["import {\n type GlobalElement,\n type LocalizedGlobalElement,\n type PagePathnameSlice,\n type Swatch,\n type Typography,\n type HttpFetch,\n} from './types'\n\nimport { type SiteVersion } from './site-version'\nimport * as Schema from './schema'\n\nexport class MakeswiftRestAPIClient {\n private _fetch: HttpFetch\n\n readonly apiKey: string\n readonly apiOrigin: string\n\n constructor({\n fetch,\n apiKey,\n apiOrigin,\n }: {\n fetch: HttpFetch\n apiKey: string\n apiOrigin: string\n }) {\n this._fetch = fetch\n this.apiKey = apiKey\n this.apiOrigin = apiOrigin\n }\n\n async getSwatch(swatchId: string, siteVersion: SiteVersion | null): Promise<Swatch | null> {\n const response = await this.fetch(`v3/swatches/${swatchId}`, siteVersion)\n\n if (!response.ok) {\n const failedBody = await failedResponseBody(response)\n if (response.status !== 404) {\n console.error(`Failed to get swatch '${swatchId}'`, {\n response: failedBody,\n siteVersion,\n })\n }\n\n return null\n }\n\n const swatch = await response.json()\n\n return swatch\n }\n\n async getTypography(\n typographyId: string,\n siteVersion: SiteVersion | null,\n ): Promise<Typography | null> {\n const response = await this.fetch(`v3/typographies/${typographyId}`, siteVersion)\n\n if (!response.ok) {\n const failedBody = await failedResponseBody(response)\n if (response.status !== 404) {\n console.error(`Failed to get typography '${typographyId}'`, {\n response: failedBody,\n siteVersion,\n })\n }\n\n return null\n }\n\n const typography = await response.json()\n\n return typography\n }\n\n async getGlobalElement(\n globalElementId: string,\n siteVersion: SiteVersion | null,\n ): Promise<GlobalElement | null> {\n const response = await this.fetch(`v3/global-elements/${globalElementId}`, siteVersion)\n\n if (!response.ok) {\n const failedBody = await failedResponseBody(response)\n if (response.status !== 404) {\n console.error(`Failed to get global element '${globalElementId}'`, {\n response: failedBody,\n siteVersion,\n })\n }\n\n return null\n }\n\n const globalElement = await response.json()\n\n return globalElement\n }\n\n async getLocalizedGlobalElement(\n globalElementId: string,\n locale: string,\n siteVersion: SiteVersion | null,\n ): Promise<LocalizedGlobalElement | null> {\n const response = await this.fetch(\n `v3/localized-global-elements/${globalElementId}?locale=${locale}`,\n siteVersion,\n )\n\n if (!response.ok) {\n const failedBody = await failedResponseBody(response)\n if (response.status !== 404) {\n console.error(`Failed to get localized global element '${globalElementId}'`, {\n response: failedBody,\n siteVersion,\n locale,\n })\n }\n\n return null\n }\n\n const localizedGlobalElement = await response.json()\n\n return localizedGlobalElement\n }\n\n async getPagePathnameSlices(\n pageIds: string[],\n siteVersion: SiteVersion | null,\n { locale }: { locale?: string | null },\n ): Promise<(PagePathnameSlice | null)[]> {\n if (pageIds.length === 0) return []\n\n const url = new URL(`v3/page-pathname-slices/bulk`, this.apiOrigin)\n\n pageIds.forEach(id => url.searchParams.append('ids', id))\n if (locale != null) url.searchParams.set('locale', locale)\n\n const response = await this.fetch(url.pathname + url.search, siteVersion)\n\n if (!response.ok) {\n console.error(`Failed to get page pathname slice(s) for ${pageIds.join(', ')}`, {\n response: await failedResponseBody(response),\n siteVersion,\n locale,\n })\n\n return []\n }\n\n const json = await response.json()\n\n const pagePathnameSlices = Schema.pagePathnameSlices.parse(json)\n\n // We're mapping the basePageId to be the id, because we're still using the GraphQL\n // fragment as our APIResource. The id on the APIResource needs to match the pageId\n // so that we can find the corresponding page pathname slice when we call getPagePathnameSlice(pageId).\n // TODO: Update this once we move away from the GraphQL fragments.\n return pagePathnameSlices.map(pagePathnameSlice => {\n if (pagePathnameSlice == null) return null\n\n return {\n ...pagePathnameSlice,\n id: pagePathnameSlice.basePageId,\n localizedPathname: pagePathnameSlice.localizedPathname ?? null,\n }\n })\n }\n\n async getPagePathnameSlice(\n pageId: string,\n siteVersion: SiteVersion | null,\n { locale }: { locale?: string | null } = {},\n ): Promise<PagePathnameSlice | null> {\n const pagePathnameSlices = await this.getPagePathnameSlices([pageId], siteVersion, { locale })\n\n return pagePathnameSlices.at(0) ?? null\n }\n\n protected async fetch(\n path: string,\n siteVersion: SiteVersion | null,\n init?: RequestInit,\n ): Promise<Response> {\n const requestUrl = new URL(path, this.apiOrigin)\n\n const requestHeaders = new Headers({\n 'x-api-key': this.apiKey,\n 'makeswift-site-api-key': this.apiKey,\n 'makeswift-runtime-version': PACKAGE_VERSION,\n })\n\n if (siteVersion?.token) {\n requestUrl.searchParams.set('version', siteVersion.version)\n requestHeaders.set('makeswift-preview-token', siteVersion.token)\n }\n\n if (init?.headers) {\n new Headers(init.headers).forEach((value, key) => {\n requestHeaders.set(key, value)\n })\n }\n\n const response = await this._fetch(requestUrl.toString(), {\n ...init,\n headers: requestHeaders,\n ...(siteVersion != null ? { cache: 'no-store' } : {}),\n })\n\n return response\n }\n}\n\n// This function attempts to consume the response body of a failed response, and\n// returns either the parsed JSON or raw text. This is useful for logging more\n// detailed error information when an API request fails.\n//\n// Cloudflare Worker Note: The Cloudflare Worker runtime has automatic deadlock\n// prevention (in the form of auto-cancelling responses) that triggers when too\n// many response bodies are unconsumed. This applies for error responses as\n// well. As such, in this client we use this function to consume the response\n// body whenever the request fails, even if we don't end up logging the body\n// itself, to avoid hitting the deadlock prevention.\nexport async function failedResponseBody(response: Response): Promise<unknown> {\n try {\n const text = await response.text()\n try {\n return JSON.parse(text)\n } catch {\n return text\n }\n } catch (e) {\n return `Failed to extract response body: ${e}`\n }\n}\n"],"mappings":"AAUA,YAAY,YAAY;AAEjB,MAAM,uBAAuB;AAAA,EAC1B;AAAA,EAEC;AAAA,EACA;AAAA,EAET,YAAY;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAIG;AACD,SAAK,SAAS;AACd,SAAK,SAAS;AACd,SAAK,YAAY;AAAA,EACnB;AAAA,EAEA,MAAM,UAAU,UAAkB,aAAyD;AACzF,UAAM,WAAW,MAAM,KAAK,MAAM,eAAe,QAAQ,IAAI,WAAW;AAExE,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,aAAa,MAAM,mBAAmB,QAAQ;AACpD,UAAI,SAAS,WAAW,KAAK;AAC3B,gBAAQ,MAAM,yBAAyB,QAAQ,KAAK;AAAA,UAClD,UAAU;AAAA,UACV;AAAA,QACF,CAAC;AAAA,MACH;AAEA,aAAO;AAAA,IACT;AAEA,UAAM,SAAS,MAAM,SAAS,KAAK;AAEnC,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,cACJ,cACA,aAC4B;AAC5B,UAAM,WAAW,MAAM,KAAK,MAAM,mBAAmB,YAAY,IAAI,WAAW;AAEhF,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,aAAa,MAAM,mBAAmB,QAAQ;AACpD,UAAI,SAAS,WAAW,KAAK;AAC3B,gBAAQ,MAAM,6BAA6B,YAAY,KAAK;AAAA,UAC1D,UAAU;AAAA,UACV;AAAA,QACF,CAAC;AAAA,MACH;AAEA,aAAO;AAAA,IACT;AAEA,UAAM,aAAa,MAAM,SAAS,KAAK;AAEvC,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,iBACJ,iBACA,aAC+B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EACT;AACF;AAYA,eAAsB,mBAAmB,UAAsC;AAC7E,MAAI;AACF,UAAM,OAAO,MAAM,SAAS,KAAK;AACjC,QAAI;AACF,aAAO,KAAK,MAAM,IAAI;AAAA,IACxB,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF,SAAS,GAAG;AACV,WAAO,oCAAoC,CAAC;AAAA,EAC9C;AACF;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/api/types.ts"],"sourcesContent":["import {\n SwatchFragment as Swatch,\n FileFragment as File,\n TypographyFragment as Typography,\n PagePathnameSliceFragment,\n GlobalElementFragment as GlobalElement,\n LocalizedGlobalElementFragment as LocalizedGlobalElement,\n TableFragment as Table,\n SnippetFragment as Snippet,\n PageFragment as Page,\n SiteFragment as Site,\n} from './graphql/generated/types'\n\ntype PagePathnameSlice = PagePathnameSliceFragment & {\n basePageId?: string\n localizedPathname?: string | null\n}\n\nexport type {\n Swatch,\n File,\n Typography,\n PagePathnameSlice,\n GlobalElement,\n LocalizedGlobalElement,\n Table,\n Snippet,\n Page,\n Site,\n}\n\nexport type LocalizableAPIResource = PagePathnameSlice | LocalizedGlobalElement\n\nexport const LocalizableAPIResourceType: {\n [R in LocalizableAPIResource as R['__typename']]: R['__typename']\n} = {\n PagePathnameSlice: 'PagePathnameSlice',\n LocalizedGlobalElement: 'LocalizedGlobalElement',\n}\n\nexport type LocalizableAPIResourceType =\n (typeof LocalizableAPIResourceType)[keyof typeof LocalizableAPIResourceType]\n\nexport type APIResource =\n | LocalizableAPIResource\n | Swatch\n | File\n | Typography\n | GlobalElement\n | Table\n | Snippet\n | Page\n | Site\n\nexport const APIResourceType: { [R in APIResource as R['__typename']]: R['__typename'] } = {\n ...LocalizableAPIResourceType,\n Swatch: 'Swatch',\n File: 'File',\n Typography: 'Typography',\n GlobalElement: 'GlobalElement',\n Table: 'Table',\n Snippet: 'Snippet',\n Page: 'Page',\n Site: 'Site',\n}\n\nexport type APIResourceType = (typeof APIResourceType)[keyof typeof APIResourceType]\n\nexport type APIResourceLocale<R extends APIResource | APIResourceType> = R extends\n | LocalizableAPIResource\n | LocalizableAPIResourceType\n ? string | null\n : never\n\nexport type HttpFetch = (url: string | URL, init?: RequestInit) => Promise<Response>\n"],"mappings":"AAiCO,MAAM,6BAET;AAAA,EACF,mBAAmB;AAAA,EACnB,wBAAwB;AAC1B;AAgBO,MAAM,kBAA8E;AAAA,EACzF,GAAG;AAAA,EACH,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,YAAY;AAAA,EACZ,eAAe;AAAA,EACf,OAAO;AAAA,EACP,SAAS;AAAA,EACT,MAAM;AAAA,EACN,MAAM;AACR;","names":[]}
1
+ {"version":3,"sources":["../../../src/api/types.ts"],"sourcesContent":["import {\n SwatchFragment as Swatch,\n FileFragment as File,\n TypographyFragment as Typography,\n PagePathnameSliceFragment,\n GlobalElementFragment as GlobalElement,\n LocalizedGlobalElementFragment as LocalizedGlobalElement,\n TableFragment as Table,\n SnippetFragment as Snippet,\n PageFragment as Page,\n SiteFragment as Site,\n} from './graphql/generated/types'\n\ntype PagePathnameSlice = PagePathnameSliceFragment & {\n basePageId?: string\n localizedPathname?: string | null\n}\n\nexport type {\n Swatch,\n File,\n Typography,\n PagePathnameSlice,\n GlobalElement,\n LocalizedGlobalElement,\n Table,\n Snippet,\n Page,\n Site,\n}\n\nexport type LocalizableAPIResource = PagePathnameSlice | LocalizedGlobalElement\n\nexport const LocalizableAPIResourceType: {\n [R in LocalizableAPIResource as R['__typename']]: R['__typename']\n} = {\n PagePathnameSlice: 'PagePathnameSlice',\n LocalizedGlobalElement: 'LocalizedGlobalElement',\n}\n\nexport type LocalizableAPIResourceType =\n (typeof LocalizableAPIResourceType)[keyof typeof LocalizableAPIResourceType]\n\nexport type APIResource =\n | LocalizableAPIResource\n | Swatch\n | File\n | Typography\n | GlobalElement\n | Table\n | Snippet\n | Page\n | Site\n\nexport const APIResourceType: { [R in APIResource as R['__typename']]: R['__typename'] } = {\n ...LocalizableAPIResourceType,\n Swatch: 'Swatch',\n File: 'File',\n Typography: 'Typography',\n GlobalElement: 'GlobalElement',\n Table: 'Table',\n Snippet: 'Snippet',\n Page: 'Page',\n Site: 'Site',\n}\n\nexport type APIResourceType = (typeof APIResourceType)[keyof typeof APIResourceType]\n\nexport type APIResourceByType<T extends APIResourceType> = Extract<APIResource, { __typename: T }>\n\nexport type APIResourceLocale<R extends APIResource | APIResourceType> = R extends\n | LocalizableAPIResource\n | LocalizableAPIResourceType\n ? string | null\n : never\n\nexport type HttpFetch = (url: string | URL, init?: RequestInit) => Promise<Response>\n"],"mappings":"AAiCO,MAAM,6BAET;AAAA,EACF,mBAAmB;AAAA,EACnB,wBAAwB;AAC1B;AAgBO,MAAM,kBAA8E;AAAA,EACzF,GAAG;AAAA,EACH,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,YAAY;AAAA,EACZ,eAAe;AAAA,EACf,OAAO;AAAA,EACP,SAAS;AAAA,EACT,MAAM;AAAA,EACN,MAAM;AACR;","names":[]}
@@ -8,7 +8,7 @@ async function manifestHandler(req, { apiKey, manifest }) {
8
8
  return ApiResponse.json({ message: "Unauthorized" }, { status: 401 });
9
9
  }
10
10
  return ApiResponse.json({
11
- version: "0.28.7-canary.9",
11
+ version: "0.28.8-canary.0",
12
12
  interactionMode: true,
13
13
  clientSideNavigation: false,
14
14
  elementFromPoint: false,
@@ -669,7 +669,7 @@ Received "${apiKey}" instead.`
669
669
  headers: {
670
670
  "x-api-key": this.apiKey,
671
671
  "makeswift-site-api-key": this.apiKey,
672
- "makeswift-runtime-version": "0.28.7-canary.9",
672
+ "makeswift-runtime-version": "0.28.8-canary.0",
673
673
  "content-type": "application/json"
674
674
  },
675
675
  body: JSON.stringify({ token }),
@@ -1,4 +1,3 @@
1
- import { ApiHandlerHeaders, serializeSiteVersion } from "../../api/site-version";
2
1
  import * as SiteVersionState from "../modules/site-version";
3
2
  import { apiResourceFulfilled } from "../actions/internal/read-only-actions";
4
3
  import { setLocalizedResourceId } from "../host-api";
@@ -6,25 +5,7 @@ import {
6
5
  APIResourceType
7
6
  } from "../../api/types";
8
7
  import { getHasAPIResource, getAPIResource, getLocalizedResourceId } from "./state";
9
- function fetchAPIResource(resourceType, resourceId, fetch, locale) {
10
- const fetchVersioned = async (url, version) => {
11
- const response = await fetch(url, {
12
- headers: {
13
- "Content-Type": "application/json",
14
- ...version != null ? { [ApiHandlerHeaders.SiteVersion]: serializeSiteVersion(version) } : {}
15
- }
16
- });
17
- if (response.status === 404)
18
- return null;
19
- if (!response.ok)
20
- throw new Error(response.statusText);
21
- if (response.headers.get("content-type")?.includes("application/json") !== true) {
22
- throw new Error(
23
- `Expected JSON response from "${url}" but got "${response.headers.get("content-type")}"`
24
- );
25
- }
26
- return response.json();
27
- };
8
+ function fetchAPIResource(resourceType, resourceId, fetchResource, locale) {
28
9
  return async (dispatch, getState) => {
29
10
  const state = getState();
30
11
  const version = SiteVersionState.getSiteVersion(state.siteVersion);
@@ -34,22 +15,12 @@ function fetchAPIResource(resourceType, resourceId, fetch, locale) {
34
15
  let resource;
35
16
  switch (resourceType) {
36
17
  case APIResourceType.Swatch:
37
- resource = await fetchVersioned(`/api/makeswift/swatches/${resourceId}`, version);
38
- break;
39
18
  case APIResourceType.File:
40
- resource = await fetchVersioned(`/api/makeswift/files/${resourceId}`, version);
41
- break;
42
19
  case APIResourceType.Typography:
43
- resource = await fetchVersioned(
44
- `/api/makeswift/typographies/${resourceId}`,
45
- version
46
- );
47
- break;
48
20
  case APIResourceType.GlobalElement:
49
- resource = await fetchVersioned(
50
- `/api/makeswift/global-elements/${resourceId}`,
51
- version
52
- );
21
+ case APIResourceType.PagePathnameSlice:
22
+ case APIResourceType.Table:
23
+ resource = await fetchResource(resourceId, version, locale);
53
24
  break;
54
25
  case APIResourceType.LocalizedGlobalElement: {
55
26
  if (locale == null)
@@ -57,10 +28,7 @@ function fetchAPIResource(resourceType, resourceId, fetch, locale) {
57
28
  if (getLocalizedResourceId(state, locale, resourceId) === null) {
58
29
  return null;
59
30
  }
60
- resource = await fetchVersioned(
61
- `/api/makeswift/localized-global-elements/${resourceId}/${locale}`,
62
- version
63
- );
31
+ resource = await fetchResource(resourceId, version, locale);
64
32
  dispatch(
65
33
  setLocalizedResourceId({
66
34
  locale,
@@ -70,16 +38,6 @@ function fetchAPIResource(resourceType, resourceId, fetch, locale) {
70
38
  );
71
39
  break;
72
40
  }
73
- case APIResourceType.PagePathnameSlice: {
74
- const url = new URL(`/api/makeswift/page-pathname-slices/${resourceId}`, "http://n");
75
- if (locale != null)
76
- url.searchParams.set("locale", locale);
77
- resource = await fetchVersioned(url.pathname + url.search, version);
78
- break;
79
- }
80
- case APIResourceType.Table:
81
- resource = await fetchVersioned(`/api/makeswift/tables/${resourceId}`, version);
82
- break;
83
41
  default:
84
42
  resource = null;
85
43
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/state/api-client/fetch-api-resource.ts"],"sourcesContent":["import { type ThunkAction } from '@reduxjs/toolkit'\n\nimport { type SiteVersion, ApiHandlerHeaders, serializeSiteVersion } from '../../api/site-version'\n\nimport * as SiteVersionState from '../modules/site-version'\n\nimport { type Action } from '../actions'\nimport { apiResourceFulfilled } from '../actions/internal/read-only-actions'\nimport { setLocalizedResourceId } from '../host-api'\n\nimport {\n APIResourceType,\n type APIResource,\n type Swatch,\n type File,\n type Typography,\n type GlobalElement,\n type PagePathnameSlice,\n type Table,\n type LocalizedGlobalElement,\n type APIResourceLocale,\n type HttpFetch,\n} from '../../api/types'\n\nimport { type State, getHasAPIResource, getAPIResource, getLocalizedResourceId } from './state'\n\ntype Thunk<ReturnType> = ThunkAction<ReturnType, State, unknown, Action>\n\nexport function fetchAPIResource<T extends APIResourceType>(\n resourceType: T,\n resourceId: string,\n fetch: HttpFetch,\n locale?: APIResourceLocale<T>,\n): Thunk<Promise<Extract<APIResource, { __typename: T }> | null>> {\n const fetchVersioned = async <T>(url: string, version: SiteVersion | null): Promise<T | null> => {\n const response = await fetch(url, {\n headers: {\n 'Content-Type': 'application/json',\n ...(version != null\n ? { [ApiHandlerHeaders.SiteVersion]: serializeSiteVersion(version) }\n : {}),\n },\n })\n\n if (response.status === 404) return null\n if (!response.ok) throw new Error(response.statusText)\n\n if (response.headers.get('content-type')?.includes('application/json') !== true) {\n throw new Error(\n `Expected JSON response from \"${url}\" but got \"${response.headers.get('content-type')}\"`,\n )\n }\n\n return response.json()\n }\n\n return async (dispatch, getState) => {\n const state = getState()\n const version = SiteVersionState.getSiteVersion(state.siteVersion)\n\n if (getHasAPIResource(state, resourceType, resourceId, locale)) {\n return getAPIResource(state, resourceType, resourceId, locale)\n }\n\n let resource: APIResource | null\n\n switch (resourceType) {\n case APIResourceType.Swatch:\n resource = await fetchVersioned<Swatch>(`/api/makeswift/swatches/${resourceId}`, version)\n break\n\n case APIResourceType.File:\n resource = await fetchVersioned<File>(`/api/makeswift/files/${resourceId}`, version)\n break\n\n case APIResourceType.Typography:\n resource = await fetchVersioned<Typography>(\n `/api/makeswift/typographies/${resourceId}`,\n version,\n )\n break\n\n case APIResourceType.GlobalElement:\n resource = await fetchVersioned<GlobalElement>(\n `/api/makeswift/global-elements/${resourceId}`,\n version,\n )\n break\n\n case APIResourceType.LocalizedGlobalElement: {\n if (locale == null) throw new Error('Locale is required to fetch LocalizedGlobalElement')\n\n // If `getLocalizedResourceId` returns null, it means we have tried to fetch the resource,\n // but the resource is not available. If we haven't fetched it yet, it'll return undefined.\n if (getLocalizedResourceId(state, locale, resourceId) === null) {\n return null\n }\n\n resource = await fetchVersioned<LocalizedGlobalElement>(\n `/api/makeswift/localized-global-elements/${resourceId}/${locale}`,\n version,\n )\n\n dispatch(\n setLocalizedResourceId({\n locale,\n resourceId,\n localizedResourceId: resource?.id ?? null,\n }),\n )\n\n break\n }\n\n case APIResourceType.PagePathnameSlice: {\n const url = new URL(`/api/makeswift/page-pathname-slices/${resourceId}`, 'http://n')\n\n if (locale != null) url.searchParams.set('locale', locale)\n\n resource = await fetchVersioned<PagePathnameSlice>(url.pathname + url.search, version)\n break\n }\n\n case APIResourceType.Table:\n resource = await fetchVersioned<Table>(`/api/makeswift/tables/${resourceId}`, version)\n break\n\n default:\n resource = null\n }\n\n dispatch(apiResourceFulfilled(resourceType, resourceId, resource, locale))\n\n return resource as Extract<APIResource, { __typename: T }> | null\n }\n}\n"],"mappings":"AAEA,SAA2B,mBAAmB,4BAA4B;AAE1E,YAAY,sBAAsB;AAGlC,SAAS,4BAA4B;AACrC,SAAS,8BAA8B;AAEvC;AAAA,EACE;AAAA,OAWK;AAEP,SAAqB,mBAAmB,gBAAgB,8BAA8B;AAI/E,SAAS,iBACd,cACA,YACA,OACA,QACgE;AAChE,QAAM,iBAAiB,OAAU,KAAa,YAAmD;AAC/F,UAAM,WAAW,MAAM,MAAM,KAAK;AAAA,MAChC,SAAS;AAAA,QACP,gBAAgB;AAAA,QAChB,GAAI,WAAW,OACX,EAAE,CAAC,kBAAkB,WAAW,GAAG,qBAAqB,OAAO,EAAE,IACjE,CAAC;AAAA,MACP;AAAA,IACF,CAAC;AAED,QAAI,SAAS,WAAW;AAAK,aAAO;AACpC,QAAI,CAAC,SAAS;AAAI,YAAM,IAAI,MAAM,SAAS,UAAU;AAErD,QAAI,SAAS,QAAQ,IAAI,cAAc,GAAG,SAAS,kBAAkB,MAAM,MAAM;AAC/E,YAAM,IAAI;AAAA,QACR,gCAAgC,GAAG,cAAc,SAAS,QAAQ,IAAI,cAAc,CAAC;AAAA,MACvF;AAAA,IACF;AAEA,WAAO,SAAS,KAAK;AAAA,EACvB;AAEA,SAAO,OAAO,UAAU,aAAa;AACnC,UAAM,QAAQ,SAAS;AACvB,UAAM,UAAU,iBAAiB,eAAe,MAAM,WAAW;AAEjE,QAAI,kBAAkB,OAAO,cAAc,YAAY,MAAM,GAAG;AAC9D,aAAO,eAAe,OAAO,cAAc,YAAY,MAAM;AAAA,IAC/D;AAEA,QAAI;AAEJ,YAAQ,cAAc;AAAA,MACpB,KAAK,gBAAgB;AACnB,mBAAW,MAAM,eAAuB,2BAA2B,UAAU,IAAI,OAAO;AACxF;AAAA,MAEF,KAAK,gBAAgB;AACnB,mBAAW,MAAM,eAAqB,wBAAwB,UAAU,IAAI,OAAO;AACnF;AAAA,MAEF,KAAK,gBAAgB;AACnB,mBAAW,MAAM;AAAA,UACf,+BAA+B,UAAU;AAAA,UACzC;AAAA,QACF;AACA;AAAA,MAEF,KAAK,gBAAgB;AACnB,mBAAW,MAAM;AAAA,UACf,kCAAkC,UAAU;AAAA,UAC5C;AAAA,QACF;AACA;AAAA,MAEF,KAAK,gBAAgB,wBAAwB;AAC3C,YAAI,UAAU;AAAM,gBAAM,IAAI,MAAM,oDAAoD;AAIxF,YAAI,uBAAuB,OAAO,QAAQ,UAAU,MAAM,MAAM;AAC9D,iBAAO;AAAA,QACT;AAEA,mBAAW,MAAM;AAAA,UACf,4CAA4C,UAAU,IAAI,MAAM;AAAA,UAChE;AAAA,QACF;AAEA;AAAA,UACE,uBAAuB;AAAA,YACrB;AAAA,YACA;AAAA,YACA,qBAAqB,UAAU,MAAM;AAAA,UACvC,CAAC;AAAA,QACH;AAEA;AAAA,MACF;AAAA,MAEA,KAAK,gBAAgB,mBAAmB;AACtC,cAAM,MAAM,IAAI,IAAI,uCAAuC,UAAU,IAAI,UAAU;AAEnF,YAAI,UAAU;AAAM,cAAI,aAAa,IAAI,UAAU,MAAM;AAEzD,mBAAW,MAAM,eAAkC,IAAI,WAAW,IAAI,QAAQ,OAAO;AACrF;AAAA,MACF;AAAA,MAEA,KAAK,gBAAgB;AACnB,mBAAW,MAAM,eAAsB,yBAAyB,UAAU,IAAI,OAAO;AACrF;AAAA,MAEF;AACE,mBAAW;AAAA,IACf;AAEA,aAAS,qBAAqB,cAAc,YAAY,UAAU,MAAM,CAAC;AAEzE,WAAO;AAAA,EACT;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../../src/state/api-client/fetch-api-resource.ts"],"sourcesContent":["import { type ThunkAction } from '@reduxjs/toolkit'\n\nimport { type SiteVersion } from '../../api/site-version'\n\nimport * as SiteVersionState from '../modules/site-version'\n\nimport { type Action } from '../actions'\nimport { apiResourceFulfilled } from '../actions/internal/read-only-actions'\nimport { setLocalizedResourceId } from '../host-api'\n\nimport {\n APIResourceType,\n type APIResource,\n type APIResourceByType,\n type APIResourceLocale,\n} from '../../api/types'\n\nimport { type State, getHasAPIResource, getAPIResource, getLocalizedResourceId } from './state'\n\ntype Thunk<ReturnType> = ThunkAction<ReturnType, State, unknown, Action>\n\nexport function fetchAPIResource<T extends APIResourceType>(\n resourceType: T,\n resourceId: string,\n fetchResource: (\n resourceId: string,\n version: SiteVersion | null,\n locale?: APIResourceLocale<T>,\n ) => Promise<APIResourceByType<T> | null>,\n locale?: APIResourceLocale<T>,\n): Thunk<Promise<APIResourceByType<T> | null>> {\n return async (dispatch, getState) => {\n const state = getState()\n const version = SiteVersionState.getSiteVersion(state.siteVersion)\n\n if (getHasAPIResource(state, resourceType, resourceId, locale)) {\n return getAPIResource(state, resourceType, resourceId, locale)\n }\n\n let resource: APIResource | null\n\n switch (resourceType) {\n case APIResourceType.Swatch:\n case APIResourceType.File:\n case APIResourceType.Typography:\n case APIResourceType.GlobalElement:\n case APIResourceType.PagePathnameSlice:\n case APIResourceType.Table:\n resource = await fetchResource(resourceId, version, locale)\n break\n\n case APIResourceType.LocalizedGlobalElement: {\n if (locale == null) throw new Error('Locale is required to fetch LocalizedGlobalElement')\n\n // If `getLocalizedResourceId` returns null, it means we have tried to fetch the resource,\n // but the resource is not available. If we haven't fetched it yet, it'll return undefined.\n if (getLocalizedResourceId(state, locale, resourceId) === null) {\n return null\n }\n\n resource = await fetchResource(resourceId, version, locale)\n\n dispatch(\n setLocalizedResourceId({\n locale,\n resourceId,\n localizedResourceId: resource?.id ?? null,\n }),\n )\n\n break\n }\n\n default:\n resource = null\n }\n\n dispatch(apiResourceFulfilled(resourceType, resourceId, resource, locale))\n\n return resource as APIResourceByType<T> | null\n }\n}\n"],"mappings":"AAIA,YAAY,sBAAsB;AAGlC,SAAS,4BAA4B;AACrC,SAAS,8BAA8B;AAEvC;AAAA,EACE;AAAA,OAIK;AAEP,SAAqB,mBAAmB,gBAAgB,8BAA8B;AAI/E,SAAS,iBACd,cACA,YACA,eAKA,QAC6C;AAC7C,SAAO,OAAO,UAAU,aAAa;AACnC,UAAM,QAAQ,SAAS;AACvB,UAAM,UAAU,iBAAiB,eAAe,MAAM,WAAW;AAEjE,QAAI,kBAAkB,OAAO,cAAc,YAAY,MAAM,GAAG;AAC9D,aAAO,eAAe,OAAO,cAAc,YAAY,MAAM;AAAA,IAC/D;AAEA,QAAI;AAEJ,YAAQ,cAAc;AAAA,MACpB,KAAK,gBAAgB;AAAA,MACrB,KAAK,gBAAgB;AAAA,MACrB,KAAK,gBAAgB;AAAA,MACrB,KAAK,gBAAgB;AAAA,MACrB,KAAK,gBAAgB;AAAA,MACrB,KAAK,gBAAgB;AACnB,mBAAW,MAAM,cAAc,YAAY,SAAS,MAAM;AAC1D;AAAA,MAEF,KAAK,gBAAgB,wBAAwB;AAC3C,YAAI,UAAU;AAAM,gBAAM,IAAI,MAAM,oDAAoD;AAIxF,YAAI,uBAAuB,OAAO,QAAQ,UAAU,MAAM,MAAM;AAC9D,iBAAO;AAAA,QACT;AAEA,mBAAW,MAAM,cAAc,YAAY,SAAS,MAAM;AAE1D;AAAA,UACE,uBAAuB;AAAA,YACrB;AAAA,YACA;AAAA,YACA,qBAAqB,UAAU,MAAM;AAAA,UACvC,CAAC;AAAA,QACH;AAEA;AAAA,MACF;AAAA,MAEA;AACE,mBAAW;AAAA,IACf;AAEA,aAAS,qBAAqB,cAAc,YAAY,UAAU,MAAM,CAAC;AAEzE,WAAO;AAAA,EACT;AACF;","names":[]}
@@ -2,6 +2,7 @@ import { type FetchableValue } from '@makeswift/controls';
2
2
  import { type Store as ApiClientStore } from '../state/api-client/store';
3
3
  import * as ApiClientState from '../state/api-client/state';
4
4
  import { type File, type GlobalElement, type LocalizedGlobalElement, type Page, type PagePathnameSlice, type Site, type Snippet, type Swatch, type Table, type Typography, APIResourceType } from './types';
5
+ import { type SiteVersion } from './site-version';
5
6
  export type CacheData = ApiClientState.SerializedState;
6
7
  export declare const CacheData: {
7
8
  empty(): CacheData;
@@ -12,18 +13,18 @@ export declare abstract class ApiResourcesClient {
12
13
  constructor({ store }: {
13
14
  store: ApiClientStore;
14
15
  });
15
- abstract fetchSwatch(swatchId: string): Promise<Swatch | null>;
16
+ fetchSwatch(swatchId: string): Promise<Swatch | null>;
16
17
  readSwatch(swatchId: string): Swatch | null;
17
18
  resolveSwatch(swatchId: string | undefined): FetchableValue<Swatch | null>;
18
- abstract fetchFile(fileId: string): Promise<File | null>;
19
+ fetchFile(fileId: string): Promise<File | null>;
19
20
  readFile(fileId: string): File | null;
20
21
  resolveFile(fileId: string | undefined): FetchableValue<File | null>;
21
- abstract fetchTypography(typographyId: string): Promise<Typography | null>;
22
+ fetchTypography(typographyId: string): Promise<Typography | null>;
22
23
  readTypography(typographyId: string): Typography | null;
23
24
  resolveTypography(typographyId: string | undefined): FetchableValue<Typography | null>;
24
- abstract fetchGlobalElement(globalElementId: string): Promise<GlobalElement | null>;
25
+ fetchGlobalElement(globalElementId: string): Promise<GlobalElement | null>;
25
26
  readGlobalElement(globalElementId: string): GlobalElement | null;
26
- abstract fetchLocalizedGlobalElement({ globalElementId, locale, }: {
27
+ fetchLocalizedGlobalElement({ globalElementId, locale, }: {
27
28
  globalElementId: string;
28
29
  locale: string;
29
30
  }): Promise<LocalizedGlobalElement | null>;
@@ -31,14 +32,14 @@ export declare abstract class ApiResourcesClient {
31
32
  globalElementId: string;
32
33
  locale: string;
33
34
  }): LocalizedGlobalElement | null;
34
- readPagePathnameSlice({ pageId, locale, }: {
35
+ fetchPagePathnameSlice({ pageId, locale, }: {
35
36
  pageId: string;
36
37
  locale: string | null;
37
- }): PagePathnameSlice | null;
38
- abstract fetchPagePathnameSlice({ pageId, locale, }: {
38
+ }): Promise<PagePathnameSlice | null>;
39
+ readPagePathnameSlice({ pageId, locale, }: {
39
40
  pageId: string;
40
41
  locale: string | null;
41
- }): Promise<PagePathnameSlice | null>;
42
+ }): PagePathnameSlice | null;
42
43
  resolvePagePathnameSlice({ pageId, locale, }: {
43
44
  pageId: string | undefined;
44
45
  locale: string | null;
@@ -48,10 +49,17 @@ export declare abstract class ApiResourcesClient {
48
49
  read: (id: string) => R | null;
49
50
  fetch: (id: string) => Promise<R | null>;
50
51
  }): FetchableValue<R | null>;
51
- abstract fetchTable(tableId: string): Promise<Table | null>;
52
+ fetchTable(tableId: string): Promise<Table | null>;
52
53
  readTable(tableId: string): Table | null;
53
54
  readSite(siteId: string): Site | null;
54
55
  readPage(pageId: string): Page | null;
55
56
  readSnippet(snippetId: string): Snippet | null;
57
+ protected abstract fetchSwatchImpl(id: string, version: SiteVersion | null): Promise<Swatch | null>;
58
+ protected abstract fetchFileImpl(id: string, version: SiteVersion | null): Promise<File | null>;
59
+ protected abstract fetchTypographyImpl(id: string, version: SiteVersion | null): Promise<Typography | null>;
60
+ protected abstract fetchGlobalElementImpl(id: string, version: SiteVersion | null): Promise<GlobalElement | null>;
61
+ protected abstract fetchLocalizedGlobalElementImpl(id: string, version: SiteVersion | null, locale: string): Promise<LocalizedGlobalElement | null>;
62
+ protected abstract fetchPagePathnameSliceImpl(id: string, version: SiteVersion | null, locale: string | null | undefined): Promise<PagePathnameSlice | null>;
63
+ protected abstract fetchTableImpl(id: string, version: SiteVersion | null): Promise<Table | null>;
56
64
  }
57
65
  //# sourceMappingURL=api-resources-client.d.ts.map