@makeswift/runtime 0.28.7 → 0.28.8-canary.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/api/api-resources-client.js +47 -0
- package/dist/cjs/api/api-resources-client.js.map +1 -1
- package/dist/cjs/api/graphql-api-client.js +1 -1
- package/dist/cjs/api/host-api-resources-client.js +38 -32
- package/dist/cjs/api/host-api-resources-client.js.map +1 -1
- package/dist/cjs/api/makeswift-api-resources-client.js +84 -0
- package/dist/cjs/api/makeswift-api-resources-client.js.map +1 -0
- package/dist/cjs/api/rest-api-client.js +1 -1
- package/dist/cjs/api/rest-api-client.js.map +1 -1
- package/dist/cjs/api/types.js.map +1 -1
- package/dist/cjs/api-handler/handlers/manifest.js +1 -1
- package/dist/cjs/client/index.js +1 -1
- package/dist/cjs/runtimes/react/components/ElementData.js +2 -2
- package/dist/cjs/runtimes/react/components/ElementData.js.map +1 -1
- package/dist/cjs/runtimes/react/components/resolve-props.js +43 -0
- package/dist/cjs/runtimes/react/components/resolve-props.js.map +1 -0
- package/dist/cjs/runtimes/react/hooks/use-control-defs.js +40 -0
- package/dist/cjs/runtimes/react/hooks/use-control-defs.js.map +1 -0
- package/dist/cjs/runtimes/react/runtime-core.js +13 -0
- package/dist/cjs/runtimes/react/runtime-core.js.map +1 -1
- package/dist/cjs/state/api-client/fetch-api-resource.js +5 -47
- package/dist/cjs/state/api-client/fetch-api-resource.js.map +1 -1
- package/dist/esm/api/api-resources-client.js +47 -0
- package/dist/esm/api/api-resources-client.js.map +1 -1
- package/dist/esm/api/graphql-api-client.js +1 -1
- package/dist/esm/api/host-api-resources-client.js +38 -33
- package/dist/esm/api/host-api-resources-client.js.map +1 -1
- package/dist/esm/api/makeswift-api-resources-client.js +50 -0
- package/dist/esm/api/makeswift-api-resources-client.js.map +1 -0
- package/dist/esm/api/rest-api-client.js +1 -1
- package/dist/esm/api/rest-api-client.js.map +1 -1
- package/dist/esm/api/types.js.map +1 -1
- package/dist/esm/api-handler/handlers/manifest.js +1 -1
- package/dist/esm/client/index.js +1 -1
- package/dist/esm/runtimes/react/components/ElementData.js +1 -1
- package/dist/esm/runtimes/react/components/ElementData.js.map +1 -1
- package/dist/esm/runtimes/react/components/resolve-props.js +19 -0
- package/dist/esm/runtimes/react/components/resolve-props.js.map +1 -0
- package/dist/esm/runtimes/react/hooks/use-control-defs.js +16 -0
- package/dist/esm/runtimes/react/hooks/use-control-defs.js.map +1 -0
- package/dist/esm/runtimes/react/runtime-core.js +13 -0
- package/dist/esm/runtimes/react/runtime-core.js.map +1 -1
- package/dist/esm/state/api-client/fetch-api-resource.js +5 -47
- package/dist/esm/state/api-client/fetch-api-resource.js.map +1 -1
- package/dist/types/api/api-resources-client.d.ts +18 -10
- package/dist/types/api/api-resources-client.d.ts.map +1 -1
- package/dist/types/api/host-api-resources-client.d.ts +9 -14
- package/dist/types/api/host-api-resources-client.d.ts.map +1 -1
- package/dist/types/api/makeswift-api-resources-client.d.ts +30 -0
- package/dist/types/api/makeswift-api-resources-client.d.ts.map +1 -0
- package/dist/types/api/rest-api-client.d.ts +1 -1
- package/dist/types/api/rest-api-client.d.ts.map +1 -1
- package/dist/types/api/types.d.ts +3 -0
- package/dist/types/api/types.d.ts.map +1 -1
- package/dist/types/runtimes/react/components/resolve-props.d.ts +7 -0
- package/dist/types/runtimes/react/components/resolve-props.d.ts.map +1 -0
- package/dist/types/runtimes/react/hooks/use-control-defs.d.ts +6 -0
- package/dist/types/runtimes/react/hooks/use-control-defs.d.ts.map +1 -0
- package/dist/types/runtimes/react/runtime-core.d.ts +4 -2
- package/dist/types/runtimes/react/runtime-core.d.ts.map +1 -1
- package/dist/types/state/api-client/fetch-api-resource.d.ts +3 -4
- package/dist/types/state/api-client/fetch-api-resource.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/cjs/runtimes/react/controls.js +0 -59
- package/dist/cjs/runtimes/react/controls.js.map +0 -1
- package/dist/esm/runtimes/react/controls.js +0 -25
- package/dist/esm/runtimes/react/controls.js.map +0 -1
- package/dist/types/runtimes/react/controls.d.ts +0 -9
- package/dist/types/runtimes/react/controls.d.ts.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/runtimes/react/hooks/use-control-defs.ts"],"sourcesContent":["import { useRef } from 'react'\n\nimport { ControlDefinition } from '@makeswift/controls'\n\nimport { partitionRecord } from '../../../utils/partition'\n\nimport { getComponentPropControllerDescriptors } from '../../../state/read-only-state'\nimport { type LegacyDescriptor, isLegacyDescriptor } from '../../../prop-controllers/descriptors'\n\nimport { useStore } from './use-store'\n\nexport function useControlDefs({\n elementType,\n}: {\n elementType: string\n}): readonly [Record<string, LegacyDescriptor>, Record<string, ControlDefinition>] {\n const store = useStore()\n const all = getComponentPropControllerDescriptors(store.getState(), elementType) ?? {}\n return useRef(partitionRecord(all, isLegacyDescriptor)).current\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAuB;AAIvB,uBAAgC;AAEhC,6BAAsD;AACtD,yBAA0D;AAE1D,uBAAyB;AAElB,SAAS,eAAe;AAAA,EAC7B;AACF,GAEmF;AACjF,QAAM,YAAQ,2BAAS;AACvB,QAAM,UAAM,8DAAsC,MAAM,SAAS,GAAG,WAAW,KAAK,CAAC;AACrF,aAAO,yBAAO,kCAAgB,KAAK,qCAAkB,CAAC,EAAE;AAC1D;","names":[]}
|
|
@@ -22,6 +22,7 @@ __export(runtime_core_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(runtime_core_exports);
|
|
24
24
|
var import_host_api_resources_client = require("../../api/host-api-resources-client");
|
|
25
|
+
var import_makeswift_api_resources_client = require("../../api/makeswift-api-resources-client");
|
|
25
26
|
var import_breakpoints = require("../../state/modules/breakpoints");
|
|
26
27
|
var import_copy_element_tree = require("../../state/ops/copy-element-tree");
|
|
27
28
|
var import_read_only_state = require("../../state/read-only-state");
|
|
@@ -41,6 +42,7 @@ class RuntimeCore {
|
|
|
41
42
|
// requested site version, which should be the only site version for which the store is requested
|
|
42
43
|
ttlCheck: import_ref_counted_map.RefCountedMap.TTLCheck.ON_RETAIN | import_ref_counted_map.RefCountedMap.TTLCheck.ON_RELEASE
|
|
43
44
|
});
|
|
45
|
+
apiKey;
|
|
44
46
|
protoStore;
|
|
45
47
|
appOrigin;
|
|
46
48
|
apiOrigin;
|
|
@@ -49,12 +51,14 @@ class RuntimeCore {
|
|
|
49
51
|
constructor({
|
|
50
52
|
appOrigin = "https://app.makeswift.com",
|
|
51
53
|
apiOrigin = "https://api.makeswift.com",
|
|
54
|
+
apiKey,
|
|
52
55
|
breakpoints,
|
|
53
56
|
requestKey,
|
|
54
57
|
fetch
|
|
55
58
|
}) {
|
|
56
59
|
this.appOrigin = validateOrigin(appOrigin, "appOrigin");
|
|
57
60
|
this.apiOrigin = validateOrigin(apiOrigin, "apiOrigin");
|
|
61
|
+
this.apiKey = apiKey;
|
|
58
62
|
this.requestKey = requestKey;
|
|
59
63
|
this.fetch = fetch;
|
|
60
64
|
this.protoStore = (0, import_store.configureProtoStore)({
|
|
@@ -107,6 +111,15 @@ class RuntimeCore {
|
|
|
107
111
|
return new URL("graphql", this.apiOrigin).href;
|
|
108
112
|
}
|
|
109
113
|
createApiResourcesClient(preloadedState) {
|
|
114
|
+
if ((0, import_is_server.isServer)() && this.apiKey) {
|
|
115
|
+
return new import_makeswift_api_resources_client.MakeswiftApiResourcesClient({
|
|
116
|
+
fetch: this.fetch,
|
|
117
|
+
apiKey: this.apiKey,
|
|
118
|
+
apiOrigin: this.apiOrigin,
|
|
119
|
+
graphqlApiEndpoint: this.graphqlApiEndpoint,
|
|
120
|
+
preloadedState
|
|
121
|
+
});
|
|
122
|
+
}
|
|
110
123
|
return new import_host_api_resources_client.HostApiResourcesClient({
|
|
111
124
|
fetch: this.fetch,
|
|
112
125
|
preloadedState
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/runtimes/react/runtime-core.ts"],"sourcesContent":["import { type SerializableReplacementContext } from '@makeswift/controls'\n\nimport { HostApiResourcesClient } from '../../api/host-api-resources-client'\nimport { type HttpFetch } from '../../api/types'\nimport { type SiteVersion } from '../../api/site-version'\n\nimport {\n Breakpoints,\n BreakpointsInput,\n parseBreakpointsInput,\n} from '../../state/modules/breakpoints'\n\nimport { copyElementTree } from '../../state/ops/copy-element-tree'\n\nimport { getBreakpoints, type Element, type ElementData } from '../../state/read-only-state'\nimport {\n configureProtoStore,\n configureReadWriteStore,\n type ProtoStore,\n type Store,\n} from '../../state/store'\n\nimport { RefCountedMap } from '../../utils/ref-counted-map'\nimport { isServer } from '../../utils/is-server'\n\nexport type StoreKey = {\n siteVersion: SiteVersion | null\n locale: string | undefined\n}\n\nconst VERSION_TAG_LIVE = 'ref:live'\n\nexport class RuntimeCore {\n // The unowned entry TTL affects performance, not correctness. The TTL controls how long an unretained store stays\n // in the map. If an entry expires and is removed from the map before React commits, the store remains retained\n // via `<StoreContext.Provider>`. The only impact is that future lookups will create a new store instance instead of\n // reusing the existing one, reducing cache efficiency.\n private readonly activeStores = new RefCountedMap<string | null, Store>({\n unownedEntryTtlMs: 1000,\n // Checking on retain/release is sufficient on the client, and we don't need to check on get on the server\n // as the only scenario in which we add the store to the map is when the runtime is already bound to the\n // requested site version, which should be the only site version for which the store is requested\n ttlCheck: RefCountedMap.TTLCheck.ON_RETAIN | RefCountedMap.TTLCheck.ON_RELEASE,\n })\n\n readonly protoStore: ProtoStore\n readonly appOrigin: string\n readonly apiOrigin: string\n readonly requestKey: StoreKey | undefined\n readonly fetch: HttpFetch\n\n constructor({\n appOrigin = 'https://app.makeswift.com',\n apiOrigin = 'https://api.makeswift.com',\n breakpoints,\n requestKey,\n fetch,\n }: {\n appOrigin?: string\n apiOrigin?: string\n breakpoints?: BreakpointsInput\n requestKey?: StoreKey\n fetch: HttpFetch\n }) {\n this.appOrigin = validateOrigin(appOrigin, 'appOrigin')\n this.apiOrigin = validateOrigin(apiOrigin, 'apiOrigin')\n this.requestKey = requestKey\n this.fetch = fetch\n\n this.protoStore = configureProtoStore({\n name: 'Runtime proto-store',\n breakpoints: breakpoints ? parseBreakpointsInput(breakpoints) : undefined,\n })\n }\n\n getOrCreateStore({ siteVersion, locale }: StoreKey): Store {\n const key = storeCacheKey({ siteVersion, locale })\n\n const createStore = () => {\n // API resources client includes an in-memory cache of the site's resources and thus also has to be versioned\n const apiResourcesClient = this.createApiResourcesClient({ siteVersion, locale })\n\n // TODO: we need to decouple editability from the site version; specifically, previewing\n // a draft version of the site should not lead to switching to the read-write state\n const isReadOnly = siteVersion == null\n\n return configureReadWriteStore({\n name: `Runtime read-write store (site version: ${key})`,\n appOrigin: this.appOrigin,\n apiResourcesClient,\n preloadedState: { ...this.protoStore.getState(), siteVersion, isReadOnly, locale },\n })\n }\n\n // On the server, stores are ephemeral by default so SSR does not retain them in a long-lived runtime.\n // The exception is a runtime that is already bound to the requested site version & locale, where we\n // can safely reuse the store across multiple root regions and benefit from the host API client's\n // in-memory cache.\n //\n // On the client, stores are reference-counted by site version so multiple root regions can share a store\n // instance, which preserves the pre-v0.17 per-site-version store behavior without requiring a separate\n // runtime per root.\n const usePersistentStore = this.shouldUsePersistentStore(key)\n return usePersistentStore ? this.activeStores.getOrCreate(key, createStore) : createStore()\n }\n\n retainStore({ siteVersion, locale }: StoreKey, store: Store): void {\n const key = storeCacheKey({ siteVersion, locale })\n\n if (!this.shouldUsePersistentStore(key)) {\n console.error('RuntimeCore: attempt to retain an ephemeral store', { key })\n return\n }\n\n if (this.activeStores.retain(key, store)) {\n store.startBreakpointWatch()\n }\n }\n\n releaseStore({ siteVersion, locale }: StoreKey, store: Store): void {\n const key = storeCacheKey({ siteVersion, locale })\n\n if (!this.shouldUsePersistentStore(key)) {\n console.error('RuntimeCore: attempt to release an ephemeral store', { key })\n return\n }\n\n if (this.activeStores.release(key, store)) {\n store.stopBreakpointWatch()\n }\n }\n\n copyElementTree(\n elementTree: ElementData,\n replacementContext: SerializableReplacementContext,\n ): Element {\n return copyElementTree(this.protoStore.getState(), elementTree, replacementContext)\n }\n\n getBreakpoints(): Breakpoints {\n return getBreakpoints(this.protoStore.getState())\n }\n\n get graphqlApiEndpoint(): string {\n return new URL('graphql', this.apiOrigin).href\n }\n\n private createApiResourcesClient(preloadedState: {\n siteVersion: SiteVersion | null\n locale: string | undefined\n }) {\n return new HostApiResourcesClient({\n fetch: this.fetch,\n preloadedState,\n })\n }\n\n private shouldUsePersistentStore(key: string): boolean {\n // don't persist stores on the server unless the runtime instance is bound to a request\n // and the requested store matches the request's site version and locale\n return !isServer() || (this.requestKey !== undefined && storeCacheKey(this.requestKey) === key)\n }\n}\n\nconst storeCacheKey = ({ siteVersion, locale }: StoreKey): string =>\n `${siteVersionTag(siteVersion)}/${locale ?? 'default'}`\n\nconst siteVersionTag = (siteVersion: SiteVersion | null): string =>\n siteVersion?.version ?? VERSION_TAG_LIVE\n\nfunction validateOrigin(url: string, name: string): string {\n try {\n return new URL(url).origin\n } catch {\n throw new Error(`The Makeswift runtime received an invalid \\`${name}\\` parameter: \"${url}\".`)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
1
|
+
{"version":3,"sources":["../../../../src/runtimes/react/runtime-core.ts"],"sourcesContent":["import { type SerializableReplacementContext } from '@makeswift/controls'\n\nimport { ApiResourcesClient } from '../../api/api-resources-client'\nimport { HostApiResourcesClient } from '../../api/host-api-resources-client'\nimport { MakeswiftApiResourcesClient } from '../../api/makeswift-api-resources-client'\nimport { type HttpFetch } from '../../api/types'\nimport { type SiteVersion } from '../../api/site-version'\n\nimport {\n type Breakpoints,\n type BreakpointsInput,\n parseBreakpointsInput,\n} from '../../state/modules/breakpoints'\n\nimport { copyElementTree } from '../../state/ops/copy-element-tree'\n\nimport { getBreakpoints, type Element, type ElementData } from '../../state/read-only-state'\nimport {\n configureProtoStore,\n configureReadWriteStore,\n type ProtoStore,\n type Store,\n} from '../../state/store'\n\nimport { RefCountedMap } from '../../utils/ref-counted-map'\nimport { isServer } from '../../utils/is-server'\n\nexport type StoreKey = {\n siteVersion: SiteVersion | null\n locale: string | undefined\n}\n\nconst VERSION_TAG_LIVE = 'ref:live'\n\nexport class RuntimeCore {\n // The unowned entry TTL affects performance, not correctness. The TTL controls how long an unretained store stays\n // in the map. If an entry expires and is removed from the map before React commits, the store remains retained\n // via `<StoreContext.Provider>`. The only impact is that future lookups will create a new store instance instead of\n // reusing the existing one, reducing cache efficiency.\n private readonly activeStores = new RefCountedMap<string | null, Store>({\n unownedEntryTtlMs: 1000,\n // Checking on retain/release is sufficient on the client, and we don't need to check on get on the server\n // as the only scenario in which we add the store to the map is when the runtime is already bound to the\n // requested site version, which should be the only site version for which the store is requested\n ttlCheck: RefCountedMap.TTLCheck.ON_RETAIN | RefCountedMap.TTLCheck.ON_RELEASE,\n })\n\n private readonly apiKey: string | undefined\n\n readonly protoStore: ProtoStore\n readonly appOrigin: string\n readonly apiOrigin: string\n readonly requestKey: StoreKey | undefined\n readonly fetch: HttpFetch\n\n constructor({\n appOrigin = 'https://app.makeswift.com',\n apiOrigin = 'https://api.makeswift.com',\n apiKey,\n breakpoints,\n requestKey,\n fetch,\n }: {\n appOrigin?: string\n apiOrigin?: string\n apiKey?: string\n breakpoints?: BreakpointsInput\n requestKey?: StoreKey\n fetch: HttpFetch\n }) {\n this.appOrigin = validateOrigin(appOrigin, 'appOrigin')\n this.apiOrigin = validateOrigin(apiOrigin, 'apiOrigin')\n this.apiKey = apiKey\n this.requestKey = requestKey\n this.fetch = fetch\n\n this.protoStore = configureProtoStore({\n name: 'Runtime proto-store',\n breakpoints: breakpoints ? parseBreakpointsInput(breakpoints) : undefined,\n })\n }\n\n getOrCreateStore({ siteVersion, locale }: StoreKey): Store {\n const key = storeCacheKey({ siteVersion, locale })\n\n const createStore = () => {\n // API resources client includes an in-memory cache of the site's resources and thus also has to be versioned\n const apiResourcesClient = this.createApiResourcesClient({ siteVersion, locale })\n\n // TODO: we need to decouple editability from the site version; specifically, previewing\n // a draft version of the site should not lead to switching to the read-write state\n const isReadOnly = siteVersion == null\n\n return configureReadWriteStore({\n name: `Runtime read-write store (site version: ${key})`,\n appOrigin: this.appOrigin,\n apiResourcesClient,\n preloadedState: { ...this.protoStore.getState(), siteVersion, isReadOnly, locale },\n })\n }\n\n // On the server, stores are ephemeral by default so SSR does not retain them in a long-lived runtime.\n // The exception is a runtime that is already bound to the requested site version & locale, where we\n // can safely reuse the store across multiple root regions and benefit from the host API client's\n // in-memory cache.\n //\n // On the client, stores are reference-counted by site version so multiple root regions can share a store\n // instance, which preserves the pre-v0.17 per-site-version store behavior without requiring a separate\n // runtime per root.\n const usePersistentStore = this.shouldUsePersistentStore(key)\n return usePersistentStore ? this.activeStores.getOrCreate(key, createStore) : createStore()\n }\n\n retainStore({ siteVersion, locale }: StoreKey, store: Store): void {\n const key = storeCacheKey({ siteVersion, locale })\n\n if (!this.shouldUsePersistentStore(key)) {\n console.error('RuntimeCore: attempt to retain an ephemeral store', { key })\n return\n }\n\n if (this.activeStores.retain(key, store)) {\n store.startBreakpointWatch()\n }\n }\n\n releaseStore({ siteVersion, locale }: StoreKey, store: Store): void {\n const key = storeCacheKey({ siteVersion, locale })\n\n if (!this.shouldUsePersistentStore(key)) {\n console.error('RuntimeCore: attempt to release an ephemeral store', { key })\n return\n }\n\n if (this.activeStores.release(key, store)) {\n store.stopBreakpointWatch()\n }\n }\n\n copyElementTree(\n elementTree: ElementData,\n replacementContext: SerializableReplacementContext,\n ): Element {\n return copyElementTree(this.protoStore.getState(), elementTree, replacementContext)\n }\n\n getBreakpoints(): Breakpoints {\n return getBreakpoints(this.protoStore.getState())\n }\n\n get graphqlApiEndpoint(): string {\n return new URL('graphql', this.apiOrigin).href\n }\n\n private createApiResourcesClient(preloadedState: {\n siteVersion: SiteVersion | null\n locale: string | undefined\n }): ApiResourcesClient {\n if (isServer() && this.apiKey) {\n return new MakeswiftApiResourcesClient({\n fetch: this.fetch,\n apiKey: this.apiKey,\n apiOrigin: this.apiOrigin,\n graphqlApiEndpoint: this.graphqlApiEndpoint,\n preloadedState,\n })\n }\n\n return new HostApiResourcesClient({\n fetch: this.fetch,\n preloadedState,\n })\n }\n\n private shouldUsePersistentStore(key: string): boolean {\n // don't persist stores on the server unless the runtime instance is bound to a request\n // and the requested store matches the request's site version and locale\n return !isServer() || (this.requestKey !== undefined && storeCacheKey(this.requestKey) === key)\n }\n}\n\nconst storeCacheKey = ({ siteVersion, locale }: StoreKey): string =>\n `${siteVersionTag(siteVersion)}/${locale ?? 'default'}`\n\nconst siteVersionTag = (siteVersion: SiteVersion | null): string =>\n siteVersion?.version ?? VERSION_TAG_LIVE\n\nfunction validateOrigin(url: string, name: string): string {\n try {\n return new URL(url).origin\n } catch {\n throw new Error(`The Makeswift runtime received an invalid \\`${name}\\` parameter: \"${url}\".`)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,uCAAuC;AACvC,4CAA4C;AAI5C,yBAIO;AAEP,+BAAgC;AAEhC,6BAA+D;AAC/D,mBAKO;AAEP,6BAA8B;AAC9B,uBAAyB;AAOzB,MAAM,mBAAmB;AAElB,MAAM,YAAY;AAAA;AAAA;AAAA;AAAA;AAAA,EAKN,eAAe,IAAI,qCAAoC;AAAA,IACtE,mBAAmB;AAAA;AAAA;AAAA;AAAA,IAInB,UAAU,qCAAc,SAAS,YAAY,qCAAc,SAAS;AAAA,EACtE,CAAC;AAAA,EAEgB;AAAA,EAER;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET,YAAY;AAAA,IACV,YAAY;AAAA,IACZ,YAAY;AAAA,IACZ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAOG;AACD,SAAK,YAAY,eAAe,WAAW,WAAW;AACtD,SAAK,YAAY,eAAe,WAAW,WAAW;AACtD,SAAK,SAAS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,YAAM,oBAAoB;AAAA,IAC5B;AAAA,EACF;AAAA,EAEA,gBACE,aACA,oBACS;AACT,eAAO,0CAAgB,KAAK,WAAW,SAAS,GAAG,aAAa,kBAAkB;AAAA,EACpF;AAAA,EAEA,iBAA8B;AAC5B,eAAO,uCAAe,KAAK,WAAW,SAAS,CAAC;AAAA,EAClD;AAAA,EAEA,IAAI,qBAA6B;AAC/B,WAAO,IAAI,IAAI,WAAW,KAAK,SAAS,EAAE;AAAA,EAC5C;AAAA,EAEQ,yBAAyB,gBAGV;AACrB,YAAI,2BAAS,KAAK,KAAK,QAAQ;AAC7B,aAAO,IAAI,kEAA4B;AAAA,QACrC,OAAO,KAAK;AAAA,QACZ,QAAQ,KAAK;AAAA,QACb,WAAW,KAAK;AAAA,QAChB,oBAAoB,KAAK;AAAA,QACzB;AAAA,MACF,CAAC;AAAA,IACH;AAEA,WAAO,IAAI,wDAAuB;AAAA,MAChC,OAAO,KAAK;AAAA,MACZ;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEQ,yBAAyB,KAAsB;AAGrD,WAAO,KAAC,2BAAS,KAAM,KAAK,eAAe,UAAa,cAAc,KAAK,UAAU,MAAM;AAAA,EAC7F;AACF;AAEA,MAAM,gBAAgB,CAAC,EAAE,aAAa,OAAO,MAC3C,GAAG,eAAe,WAAW,CAAC,IAAI,UAAU,SAAS;AAEvD,MAAM,iBAAiB,CAAC,gBACtB,aAAa,WAAW;AAE1B,SAAS,eAAe,KAAa,MAAsB;AACzD,MAAI;AACF,WAAO,IAAI,IAAI,GAAG,EAAE;AAAA,EACtB,QAAQ;AACN,UAAM,IAAI,MAAM,+CAA+C,IAAI,kBAAkB,GAAG,IAAI;AAAA,EAC9F;AACF;","names":[]}
|
|
@@ -31,31 +31,12 @@ __export(fetch_api_resource_exports, {
|
|
|
31
31
|
fetchAPIResource: () => fetchAPIResource
|
|
32
32
|
});
|
|
33
33
|
module.exports = __toCommonJS(fetch_api_resource_exports);
|
|
34
|
-
var import_site_version = require("../../api/site-version");
|
|
35
34
|
var SiteVersionState = __toESM(require("../modules/site-version"));
|
|
36
35
|
var import_read_only_actions = require("../actions/internal/read-only-actions");
|
|
37
36
|
var import_host_api = require("../host-api");
|
|
38
37
|
var import_types = require("../../api/types");
|
|
39
38
|
var import_state = require("./state");
|
|
40
|
-
function fetchAPIResource(resourceType, resourceId,
|
|
41
|
-
const fetchVersioned = async (url, version) => {
|
|
42
|
-
const response = await fetch(url, {
|
|
43
|
-
headers: {
|
|
44
|
-
"Content-Type": "application/json",
|
|
45
|
-
...version != null ? { [import_site_version.ApiHandlerHeaders.SiteVersion]: (0, import_site_version.serializeSiteVersion)(version) } : {}
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
if (response.status === 404)
|
|
49
|
-
return null;
|
|
50
|
-
if (!response.ok)
|
|
51
|
-
throw new Error(response.statusText);
|
|
52
|
-
if (response.headers.get("content-type")?.includes("application/json") !== true) {
|
|
53
|
-
throw new Error(
|
|
54
|
-
`Expected JSON response from "${url}" but got "${response.headers.get("content-type")}"`
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
return response.json();
|
|
58
|
-
};
|
|
39
|
+
function fetchAPIResource(resourceType, resourceId, fetchResource, locale) {
|
|
59
40
|
return async (dispatch, getState) => {
|
|
60
41
|
const state = getState();
|
|
61
42
|
const version = SiteVersionState.getSiteVersion(state.siteVersion);
|
|
@@ -65,22 +46,12 @@ function fetchAPIResource(resourceType, resourceId, fetch, locale) {
|
|
|
65
46
|
let resource;
|
|
66
47
|
switch (resourceType) {
|
|
67
48
|
case import_types.APIResourceType.Swatch:
|
|
68
|
-
resource = await fetchVersioned(`/api/makeswift/swatches/${resourceId}`, version);
|
|
69
|
-
break;
|
|
70
49
|
case import_types.APIResourceType.File:
|
|
71
|
-
resource = await fetchVersioned(`/api/makeswift/files/${resourceId}`, version);
|
|
72
|
-
break;
|
|
73
50
|
case import_types.APIResourceType.Typography:
|
|
74
|
-
resource = await fetchVersioned(
|
|
75
|
-
`/api/makeswift/typographies/${resourceId}`,
|
|
76
|
-
version
|
|
77
|
-
);
|
|
78
|
-
break;
|
|
79
51
|
case import_types.APIResourceType.GlobalElement:
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
);
|
|
52
|
+
case import_types.APIResourceType.PagePathnameSlice:
|
|
53
|
+
case import_types.APIResourceType.Table:
|
|
54
|
+
resource = await fetchResource(resourceId, version, locale);
|
|
84
55
|
break;
|
|
85
56
|
case import_types.APIResourceType.LocalizedGlobalElement: {
|
|
86
57
|
if (locale == null)
|
|
@@ -88,10 +59,7 @@ function fetchAPIResource(resourceType, resourceId, fetch, locale) {
|
|
|
88
59
|
if ((0, import_state.getLocalizedResourceId)(state, locale, resourceId) === null) {
|
|
89
60
|
return null;
|
|
90
61
|
}
|
|
91
|
-
resource = await
|
|
92
|
-
`/api/makeswift/localized-global-elements/${resourceId}/${locale}`,
|
|
93
|
-
version
|
|
94
|
-
);
|
|
62
|
+
resource = await fetchResource(resourceId, version, locale);
|
|
95
63
|
dispatch(
|
|
96
64
|
(0, import_host_api.setLocalizedResourceId)({
|
|
97
65
|
locale,
|
|
@@ -101,16 +69,6 @@ function fetchAPIResource(resourceType, resourceId, fetch, locale) {
|
|
|
101
69
|
);
|
|
102
70
|
break;
|
|
103
71
|
}
|
|
104
|
-
case import_types.APIResourceType.PagePathnameSlice: {
|
|
105
|
-
const url = new URL(`/api/makeswift/page-pathname-slices/${resourceId}`, "http://n");
|
|
106
|
-
if (locale != null)
|
|
107
|
-
url.searchParams.set("locale", locale);
|
|
108
|
-
resource = await fetchVersioned(url.pathname + url.search, version);
|
|
109
|
-
break;
|
|
110
|
-
}
|
|
111
|
-
case import_types.APIResourceType.Table:
|
|
112
|
-
resource = await fetchVersioned(`/api/makeswift/tables/${resourceId}`, version);
|
|
113
|
-
break;
|
|
114
72
|
default:
|
|
115
73
|
resource = null;
|
|
116
74
|
}
|
|
@@ -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
|
|
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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,uBAAkC;AAGlC,+BAAqC;AACrC,sBAAuC;AAEvC,mBAKO;AAEP,mBAAsF;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,YAAI,gCAAkB,OAAO,cAAc,YAAY,MAAM,GAAG;AAC9D,iBAAO,6BAAe,OAAO,cAAc,YAAY,MAAM;AAAA,IAC/D;AAEA,QAAI;AAEJ,YAAQ,cAAc;AAAA,MACpB,KAAK,6BAAgB;AAAA,MACrB,KAAK,6BAAgB;AAAA,MACrB,KAAK,6BAAgB;AAAA,MACrB,KAAK,6BAAgB;AAAA,MACrB,KAAK,6BAAgB;AAAA,MACrB,KAAK,6BAAgB;AACnB,mBAAW,MAAM,cAAc,YAAY,SAAS,MAAM;AAC1D;AAAA,MAEF,KAAK,6BAAgB,wBAAwB;AAC3C,YAAI,UAAU;AAAM,gBAAM,IAAI,MAAM,oDAAoD;AAIxF,gBAAI,qCAAuB,OAAO,QAAQ,UAAU,MAAM,MAAM;AAC9D,iBAAO;AAAA,QACT;AAEA,mBAAW,MAAM,cAAc,YAAY,SAAS,MAAM;AAE1D;AAAA,cACE,wCAAuB;AAAA,YACrB;AAAA,YACA;AAAA,YACA,qBAAqB,UAAU,MAAM;AAAA,UACvC,CAAC;AAAA,QACH;AAEA;AAAA,MACF;AAAA,MAEA;AACE,mBAAW;AAAA,IACf;AAEA,iBAAS,+CAAqB,cAAc,YAAY,UAAU,MAAM,CAAC;AAEzE,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
@@ -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,Q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AS;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,gBA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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.
|
|
12
|
+
"makeswift-runtime-version": "0.28.8-canary.1"
|
|
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
|
|
20
|
-
return await this.
|
|
15
|
+
async fetchSwatchImpl(id, version) {
|
|
16
|
+
return await this.fetchVersioned(`/api/makeswift/swatches/${id}`, version);
|
|
21
17
|
}
|
|
22
|
-
async
|
|
23
|
-
return await this.
|
|
18
|
+
async fetchFileImpl(id, version) {
|
|
19
|
+
return await this.fetchVersioned(`/api/makeswift/files/${id}`, version);
|
|
24
20
|
}
|
|
25
|
-
async
|
|
26
|
-
return await this.
|
|
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
|
|
31
|
-
return await this.
|
|
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
|
|
36
|
-
|
|
37
|
-
|
|
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
|
|
44
|
-
|
|
45
|
-
locale
|
|
46
|
-
|
|
47
|
-
return await this.
|
|
48
|
-
|
|
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
|
|
52
|
-
|
|
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'\
|
|
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,50 @@
|
|
|
1
|
+
import { configureClientStore } from "../state/api-client/client-store";
|
|
2
|
+
import { ApiResourcesClient } from "./api-resources-client";
|
|
3
|
+
class MakeswiftApiResourcesClient extends ApiResourcesClient {
|
|
4
|
+
restApiClient;
|
|
5
|
+
graphQlClient;
|
|
6
|
+
constructor({
|
|
7
|
+
fetch,
|
|
8
|
+
apiKey,
|
|
9
|
+
apiOrigin,
|
|
10
|
+
graphqlApiEndpoint,
|
|
11
|
+
preloadedState
|
|
12
|
+
}) {
|
|
13
|
+
super({
|
|
14
|
+
store: configureClientStore({ preloadedState })
|
|
15
|
+
});
|
|
16
|
+
this.restApiClient = (async () => {
|
|
17
|
+
const { MakeswiftRestAPIClient } = await import("./rest-api-client");
|
|
18
|
+
return new MakeswiftRestAPIClient({ fetch, apiKey, apiOrigin });
|
|
19
|
+
})();
|
|
20
|
+
this.graphQlClient = (async () => {
|
|
21
|
+
const { MakeswiftGraphQLApiClient } = await import("./graphql-api-client");
|
|
22
|
+
return new MakeswiftGraphQLApiClient({ endpoint: graphqlApiEndpoint });
|
|
23
|
+
})();
|
|
24
|
+
}
|
|
25
|
+
async fetchSwatchImpl(id, version) {
|
|
26
|
+
return (await this.restApiClient).getSwatch(id, version);
|
|
27
|
+
}
|
|
28
|
+
async fetchFileImpl(id, _version) {
|
|
29
|
+
return (await this.graphQlClient).getFile(id);
|
|
30
|
+
}
|
|
31
|
+
async fetchTypographyImpl(id, version) {
|
|
32
|
+
return (await this.restApiClient).getTypography(id, version);
|
|
33
|
+
}
|
|
34
|
+
async fetchGlobalElementImpl(id, version) {
|
|
35
|
+
return (await this.restApiClient).getGlobalElement(id, version);
|
|
36
|
+
}
|
|
37
|
+
async fetchLocalizedGlobalElementImpl(id, version, locale) {
|
|
38
|
+
return (await this.restApiClient).getLocalizedGlobalElement(id, locale, version);
|
|
39
|
+
}
|
|
40
|
+
async fetchPagePathnameSliceImpl(id, version, locale) {
|
|
41
|
+
return (await this.restApiClient).getPagePathnameSlice(id, version, { locale });
|
|
42
|
+
}
|
|
43
|
+
async fetchTableImpl(id, _version) {
|
|
44
|
+
return (await this.graphQlClient).getTable(id);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export {
|
|
48
|
+
MakeswiftApiResourcesClient
|
|
49
|
+
};
|
|
50
|
+
//# 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 { type SiteVersion } from './site-version'\n\n// see the comment in `MakeswiftApiResourcesClient` constructor below\nimport { type MakeswiftGraphQLApiClient } from './graphql-api-client'\nimport { type MakeswiftRestAPIClient } from './rest-api-client'\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: Promise<MakeswiftRestAPIClient>\n private graphQlClient: Promise<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 // `MakeswiftApiResourcesClient` is used by the runtime and therefore will be imported\n // on the client; lazy import server-side REST/GraphQL clients to avoid unnecessarily\n // pulling their code into a client bundle.\n //\n // (we can't easily lazy-load `MakeswiftApiResourcesClient` itself, at it will make its\n // construction async, but because it shares most of its implementation with\n // `HostApiResourcesClient`, the overhead of this module on its own is negligible)\n this.restApiClient = (async () => {\n const { MakeswiftRestAPIClient } = await import('./rest-api-client')\n return new MakeswiftRestAPIClient({ fetch, apiKey, apiOrigin })\n })()\n\n this.graphQlClient = (async () => {\n const { MakeswiftGraphQLApiClient } = await import('./graphql-api-client')\n return new MakeswiftGraphQLApiClient({ endpoint: graphqlApiEndpoint })\n })()\n }\n\n protected async fetchSwatchImpl(id: string, version: SiteVersion | null): Promise<Swatch | null> {\n return (await 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 (await this.graphQlClient).getFile(id)\n }\n\n protected async fetchTypographyImpl(\n id: string,\n version: SiteVersion | null,\n ): Promise<Typography | null> {\n return (await this.restApiClient).getTypography(id, version)\n }\n\n protected async fetchGlobalElementImpl(\n id: string,\n version: SiteVersion | null,\n ): Promise<GlobalElement | null> {\n return (await 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 (await 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 (await 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 (await this.graphQlClient).getTable(id)\n }\n}\n"],"mappings":"AACA,SAAS,4BAA4B;AAarC,SAAS,0BAA0B;AAc5B,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;AASD,SAAK,iBAAiB,YAAY;AAChC,YAAM,EAAE,uBAAuB,IAAI,MAAM,OAAO,mBAAmB;AACnE,aAAO,IAAI,uBAAuB,EAAE,OAAO,QAAQ,UAAU,CAAC;AAAA,IAChE,GAAG;AAEH,SAAK,iBAAiB,YAAY;AAChC,YAAM,EAAE,0BAA0B,IAAI,MAAM,OAAO,sBAAsB;AACzE,aAAO,IAAI,0BAA0B,EAAE,UAAU,mBAAmB,CAAC;AAAA,IACvE,GAAG;AAAA,EACL;AAAA,EAEA,MAAgB,gBAAgB,IAAY,SAAqD;AAC/F,YAAQ,MAAM,KAAK,eAAe,UAAU,IAAI,OAAO;AAAA,EACzD;AAAA,EAEA,MAAgB,cAAc,IAAY,UAAoD;AAE5F,YAAQ,MAAM,KAAK,eAAe,QAAQ,EAAE;AAAA,EAC9C;AAAA,EAEA,MAAgB,oBACd,IACA,SAC4B;AAC5B,YAAQ,MAAM,KAAK,eAAe,cAAc,IAAI,OAAO;AAAA,EAC7D;AAAA,EAEA,MAAgB,uBACd,IACA,SAC+B;AAC/B,YAAQ,MAAM,KAAK,eAAe,iBAAiB,IAAI,OAAO;AAAA,EAChE;AAAA,EAEA,MAAgB,gCACd,IACA,SACA,QACwC;AACxC,YAAQ,MAAM,KAAK,eAAe,0BAA0B,IAAI,QAAQ,OAAO;AAAA,EACjF;AAAA,EAEA,MAAgB,2BACd,IACA,SACA,QACmC;AACnC,YAAQ,MAAM,KAAK,eAAe,qBAAqB,IAAI,SAAS,EAAE,OAAO,CAAC;AAAA,EAChF;AAAA,EAEA,MAAgB,eAAe,IAAY,UAAqD;AAE9F,YAAQ,MAAM,KAAK,eAAe,SAAS,EAAE;AAAA,EAC/C;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.
|
|
116
|
+
"makeswift-runtime-version": "0.28.8-canary.1"
|
|
117
117
|
});
|
|
118
118
|
if (siteVersion?.token) {
|
|
119
119
|
requestUrl.searchParams.set("version", siteVersion.version);
|