@swell/apps-sdk 1.0.139 → 1.0.140

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -149,31 +149,44 @@ var import_cache_manager = require("cache-manager");
149
149
 
150
150
  // src/cache/cf-worker-kv-keyv-adapter.ts
151
151
  var CFWorkerKVKeyvAdapter = class {
152
+ store;
152
153
  namespace;
153
154
  // magically passed in from Keyv
154
- store;
155
+ opts;
155
156
  constructor(store) {
156
157
  this.store = store;
158
+ this.opts = null;
157
159
  this.namespace = "dummy";
158
160
  }
159
161
  async has(key) {
160
- return this.get(key) !== void 0;
162
+ const stream = await this.store.get(key, "stream");
163
+ if (stream !== null) {
164
+ await stream.cancel();
165
+ return true;
166
+ }
167
+ return false;
161
168
  }
162
169
  async get(key) {
163
- return this.store.get(key);
170
+ const value = await this.store.get(key);
171
+ return value !== null ? value : void 0;
164
172
  }
165
- async set(key, value) {
166
- return this.store.put(key, value);
173
+ set(key, value, ttl) {
174
+ if (typeof ttl === "number") {
175
+ ttl = Math.max(60, ttl / 1e3);
176
+ }
177
+ return this.store.put(key, value, { expirationTtl: ttl });
167
178
  }
168
179
  async delete(key) {
169
180
  await this.store.delete(key);
181
+ return true;
170
182
  }
171
183
  async clear() {
172
184
  let cursor = "";
173
185
  let complete = false;
174
- while (!complete) {
186
+ const prefix = `${this.namespace}:`;
187
+ do {
175
188
  const response = await this.store.list({
176
- prefix: `${this.namespace}:`,
189
+ prefix,
177
190
  cursor: cursor || void 0
178
191
  });
179
192
  cursor = response.cursor ?? "";
@@ -185,7 +198,10 @@ var CFWorkerKVKeyvAdapter = class {
185
198
  })
186
199
  );
187
200
  }
188
- }
201
+ } while (!complete);
202
+ }
203
+ on(_event, _listener) {
204
+ return this;
189
205
  }
190
206
  };
191
207
 
@@ -7089,23 +7105,25 @@ var Cache = class {
7089
7105
  ...options
7090
7106
  });
7091
7107
  }
7092
- async fetch(key, fetchFn) {
7093
- return this.client.wrap(key, fetchFn);
7108
+ async fetch(key, fetchFn, ttl) {
7109
+ return this.client.wrap(key, fetchFn, ttl);
7094
7110
  }
7095
- // Fetch cache using SWR (stale-while-revalidate)
7096
- // This will always return the cached value immediately if exists
7111
+ /**
7112
+ * Fetch cache using SWR (stale-while-revalidate)
7113
+ *
7114
+ * This will always return the cached value immediately if exists
7115
+ */
7097
7116
  async fetchSWR(key, fetchFn, ttl = DEFAULT_SWR_TTL) {
7098
7117
  const cacheValue = await this.client.get(key);
7099
- const promiseValue = Promise.resolve().then(() => fetchFn()).then(async (value) => {
7118
+ const promiseValue = Promise.resolve().then(fetchFn).then(resolveAsyncResources).then(async (value) => {
7100
7119
  const isNull = value === null || value === void 0;
7101
- const valueResolved = await resolveAsyncResources(value);
7102
- await this.client.set(key, isNull ? NULL_VALUE : valueResolved, ttl);
7120
+ await this.client.set(key, isNull ? NULL_VALUE : value, ttl);
7103
7121
  return value;
7104
7122
  });
7105
7123
  if (this.workerCtx?.waitUntil) {
7106
7124
  this.workerCtx.waitUntil(promiseValue);
7107
7125
  }
7108
- if (cacheValue !== null) {
7126
+ if (cacheValue !== void 0) {
7109
7127
  return cacheValue === NULL_VALUE ? null : cacheValue;
7110
7128
  }
7111
7129
  const result = await promiseValue;
@@ -7122,8 +7140,9 @@ var Cache = class {
7122
7140
  }
7123
7141
  /**
7124
7142
  * Flushes the entire cache.
7125
- * WARNING: If the cache store is shared among many cache clients,
7126
- * this will flush entries for other clients.
7143
+ *
7144
+ * __WARNING__: If the cache store is shared among many cache clients,
7145
+ * this will flush entries for other clients.
7127
7146
  */
7128
7147
  async flushAll() {
7129
7148
  await this.client.clear();
@@ -7166,7 +7185,7 @@ var ResourceCache = class extends Cache {
7166
7185
  function buildStores2() {
7167
7186
  return [
7168
7187
  new import_keyv2.Keyv({
7169
- // Disabling serialization allows for pure memo-ization of class instances
7188
+ // Disabling serialization allows for pure memoization of class instances
7170
7189
  // at the tradeoff of no support for compression.
7171
7190
  serialize: void 0,
7172
7191
  deserialize: void 0
@@ -18924,28 +18943,30 @@ var ThemeLoader = class _ThemeLoader {
18924
18943
  * Fetches a theme config by file path.
18925
18944
  */
18926
18945
  async fetchThemeConfig(filePath) {
18927
- const themeConfig = this.configs.get(filePath);
18928
- if (themeConfig !== void 0) {
18929
- return themeConfig;
18946
+ const config = this.configs.get(filePath);
18947
+ if (config !== void 0) {
18948
+ return config;
18930
18949
  }
18931
18950
  const hash = this.manifest?.get(filePath);
18932
18951
  if (!hash) {
18933
18952
  return null;
18934
18953
  }
18935
18954
  const cache = this.getCache();
18936
- let config = await cache.get(`config:${hash}`);
18937
- if (config) {
18938
- const themeId = this.getThemeId();
18939
- if (themeId && config.file?.url) {
18940
- const fileUrl = await cache.get(
18941
- `file:${themeId}:${config.hash}`
18942
- );
18943
- if (fileUrl) {
18944
- config = { ...config, file: { ...config.file, url: fileUrl } };
18945
- }
18955
+ const themeId = this.getThemeId();
18956
+ const [themeConfig, fileUrl] = await Promise2.all([
18957
+ cache.get(`config:${hash}`),
18958
+ themeId ? cache.get(`file:${themeId}:${hash}`) : void 0
18959
+ ]);
18960
+ if (themeConfig) {
18961
+ let config2 = themeConfig;
18962
+ if (fileUrl && themeConfig.file?.url) {
18963
+ config2 = {
18964
+ ...themeConfig,
18965
+ file: { ...themeConfig.file, url: fileUrl }
18966
+ };
18946
18967
  }
18947
- this.configs.set(filePath, config);
18948
- return config;
18968
+ this.configs.set(filePath, config2);
18969
+ return config2;
18949
18970
  }
18950
18971
  return this.fetchThemeConfigsFromSourceByPath(filePath, hash);
18951
18972
  }
@@ -18994,18 +19015,20 @@ var ThemeLoader = class _ThemeLoader {
18994
19015
  await Promise2.map(
18995
19016
  manifest.values(),
18996
19017
  async (configHash) => {
18997
- let config = await cache.get(`config:${configHash}`);
18998
- if (!config) {
19018
+ const [themeConfig, fileUrl] = await Promise2.all([
19019
+ cache.get(`config:${configHash}`),
19020
+ themeId ? cache.get(`file:${themeId}:${configHash}`) : void 0
19021
+ ]);
19022
+ if (!themeConfig) {
18999
19023
  configHashesUnresolved.push(configHash);
19000
19024
  return;
19001
19025
  }
19002
- if (themeId && config.file?.url) {
19003
- const fileUrl = await cache.get(
19004
- `file:${themeId}:${configHash}`
19005
- );
19006
- if (fileUrl) {
19007
- config = { ...config, file: { ...config.file, url: fileUrl } };
19008
- }
19026
+ let config = themeConfig;
19027
+ if (fileUrl && themeConfig.file?.url) {
19028
+ config = {
19029
+ ...themeConfig,
19030
+ file: { ...themeConfig.file, url: fileUrl }
19031
+ };
19009
19032
  }
19010
19033
  configsByHash.set(config.hash, config);
19011
19034
  this.configs.set(config.file_path, config);
@@ -19389,13 +19412,15 @@ var SwellTheme3 = class {
19389
19412
  const [cart, account] = await Promise.all([
19390
19413
  this.fetchSingletonResourceCached(
19391
19414
  "cart",
19415
+ // The cached cart may be null, but we need the StorefrontResource
19392
19416
  () => this.fetchCart(),
19393
- {}
19417
+ // Default value (always StorefrontResource)
19418
+ () => this.fetchCart()
19394
19419
  ),
19395
19420
  this.fetchSingletonResourceCached(
19396
19421
  "account",
19397
19422
  () => this.fetchAccount(),
19398
- null
19423
+ () => null
19399
19424
  )
19400
19425
  ]);
19401
19426
  if (!cart) {
@@ -19418,13 +19443,14 @@ var SwellTheme3 = class {
19418
19443
  async fetchSingletonResourceCached(key, handler, defaultValue) {
19419
19444
  const cacheKey = this.swell.storefront.session.getCookie();
19420
19445
  if (!cacheKey) {
19421
- return defaultValue;
19446
+ return defaultValue();
19422
19447
  }
19423
- return this.swell.getCachedResource(
19448
+ const result = await this.swell.getCachedResource(
19424
19449
  `${key}-${cacheKey}`,
19425
19450
  [],
19426
- () => handler()
19451
+ handler
19427
19452
  );
19453
+ return result ?? defaultValue();
19428
19454
  }
19429
19455
  async fetchCart() {
19430
19456
  const CartResource = this.resources?.singletons?.cart;