@swell/apps-sdk 1.0.39 → 1.0.40

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/src/api.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import SwellJS from 'swell-js';
2
- import { Cache } from './cache';
3
- export * from './resources';
4
2
  import type { SwellAppConfig, SwellErrorOptions, SwellMenu, SwellData, CFThemeEnv, SwellAppShopifyCompatibilityConfig } from '../types/swell';
3
+ export * from './resources';
5
4
  export declare class Swell {
6
5
  url: URL;
7
6
  headers: SwellData;
@@ -16,7 +15,6 @@ export declare class Swell {
16
15
  isPreview: boolean;
17
16
  isEditor: boolean;
18
17
  sentResponse: boolean;
19
- static cache: Map<string, Cache>;
20
18
  storefront_url?: string;
21
19
  constructor(params: {
22
20
  url: URL | string;
@@ -36,40 +34,17 @@ export declare class Swell {
36
34
  swellHeaders: SwellData;
37
35
  };
38
36
  static formatQueryParams(queryParams?: URLSearchParams | SwellData): SwellData;
39
- getClientProps(): {
40
- url: URL;
41
- headers: SwellData;
42
- swellHeaders: SwellData;
43
- queryParams: SwellData;
44
- instanceId: string;
45
- isPreview: boolean;
46
- isEditor: boolean;
47
- cache: unknown[][];
48
- storefrontSettingStates: {
49
- state: any;
50
- menuState: any;
51
- paymentState: any;
52
- subscriptionState: any;
53
- sessionState: any;
54
- };
55
- };
56
- getStorefrontInstance(params: SwellData): typeof SwellJS;
57
- isStorefrontRequestCacheable(method: string, url: string): boolean;
58
- getCacheableStorefrontRequestHandler<T>(storefront: typeof SwellJS): (method: string, url: string, id?: any, data?: any, opt?: any) => T | Promise<T | undefined> | undefined;
59
- getCacheInstance(): Cache;
60
- setCacheValues(values: any[]): void;
61
- getCacheKeyPrefix(): string;
62
- getCacheKey(key: string, args?: unknown[]): string;
63
- setCachedSync(key: string, args: unknown[], value: unknown, timeout?: number, isSync?: boolean): void | Promise<void>;
64
- getCachedSync<T>(key: string, args?: unknown[] | (() => Promise<T> | T), handler?: () => Promise<T> | T, timeout?: number, isSync?: boolean): Promise<T | undefined> | T | undefined;
65
- resolveCacheHandler<T>(cacheInstance: Cache, cacheKey: string, cacheHandler: () => Promise<T> | T, timeout?: number, isSync?: boolean): Promise<T> | T | undefined;
66
- setCached(key: string, args: unknown[], value: unknown, timeout?: number): Promise<any>;
67
- getCached<T>(key: string, args?: unknown[] | (() => Promise<T> | T), handler?: () => Promise<T> | T, timeout?: number): Promise<T | undefined>;
68
- getCachedVersion<T>(key: string[], version: string, handler: () => Promise<T> | T, timeout?: number): Promise<T | undefined>;
69
- getCachedResource<T>(key: string, args: unknown[] | (() => Promise<T>), handler?: () => Promise<T>, timeout?: number): Promise<T | undefined>;
37
+ /**
38
+ * Fetches a theme version resource
39
+ * First attempts to fetch from cache.
40
+ */
41
+ getCachedThemeVersion<T>(key: string, version: string, handler: () => T | Promise<T>): Promise<T>;
42
+ /**
43
+ * Fetches a resource.
44
+ * First attempts to fetch from cache.
45
+ */
46
+ getCachedResource<T>(key: string, args: unknown[], handler: () => T | Promise<T>): Promise<T | undefined>;
70
47
  updateCacheModified(cacheModified: string): Promise<void>;
71
- deleteCachedVersion(key: string): Promise<void>;
72
- clearCache(prefix: string): Promise<void>;
73
48
  getAppSettings(): Promise<SwellData>;
74
49
  getStorefrontSettings(): Promise<SwellData>;
75
50
  getStorefrontMenus(): SwellMenu[];
@@ -77,6 +52,22 @@ export declare class Swell {
77
52
  put(...args: Parameters<SwellBackendAPI['put']>): Promise<SwellData | undefined>;
78
53
  post(...args: Parameters<SwellBackendAPI['post']>): Promise<SwellData | undefined>;
79
54
  delete(...args: Parameters<SwellBackendAPI['delete']>): Promise<SwellData | undefined>;
55
+ private getStorefrontInstance;
56
+ private isStorefrontRequestCacheable;
57
+ private getCacheableStorefrontRequestHandler;
58
+ /**
59
+ * Caches client theme in perstent cache + in memory.
60
+ */
61
+ private getThemeCache;
62
+ /**
63
+ * Caches client resources in memory.
64
+ */
65
+ private getResourceCache;
66
+ /**
67
+ * Caches client storefront API requests in memory.
68
+ */
69
+ private getRequestCache;
70
+ private getCacheKey;
80
71
  }
81
72
  export declare class SwellBackendAPI {
82
73
  apiHost: string;
@@ -0,0 +1,15 @@
1
+ import { type CreateCacheOptions } from 'cache-manager';
2
+ export declare class Cache {
3
+ private client;
4
+ constructor(options?: CreateCacheOptions);
5
+ fetch<T>(key: string, fetchFn: () => T | Promise<T>): Promise<T>;
6
+ get<T>(key: string): Promise<T | null>;
7
+ set<T>(key: string, value: T, ttl?: number): Promise<T>;
8
+ flush(key: string): Promise<void>;
9
+ /**
10
+ * Flushes the entire cache.
11
+ * Warning: If the cache store is shared among many cache clients,
12
+ * this will flush entries for other clients.
13
+ */
14
+ flushAll(): Promise<void>;
15
+ }
@@ -0,0 +1,19 @@
1
+ import type { CFWorkerKV } from 'types/swell';
2
+ /**
3
+ * CloudFlare Workers KV adapter for Keyv.
4
+ * Includes namespacing to prevent conflicts within shared storage.
5
+ */
6
+ export declare class CFWorkerKVKeyvAdapter {
7
+ private namespace;
8
+ private store;
9
+ constructor(namespace: string, store: CFWorkerKV);
10
+ has(key: string): Promise<boolean>;
11
+ get(key: string): Promise<any>;
12
+ set(key: string, value: any): Promise<any>;
13
+ delete(key: string): Promise<void>;
14
+ clear(): Promise<void>;
15
+ /**
16
+ * Generates a namespaced cache key.
17
+ */
18
+ private cacheKey;
19
+ }
@@ -0,0 +1,5 @@
1
+ import { Cache } from './cache';
2
+ import { RequestCache } from './request-cache';
3
+ import { ResourceCache } from './resource-cache';
4
+ import { ThemeCache } from './theme-cache';
5
+ export { Cache, RequestCache, ResourceCache, ThemeCache };
@@ -0,0 +1,4 @@
1
+ import { Cache } from './cache';
2
+ export declare class RequestCache extends Cache {
3
+ constructor();
4
+ }
@@ -0,0 +1,5 @@
1
+ import { type CreateCacheOptions } from 'cache-manager';
2
+ import { Cache } from './cache';
3
+ export declare class ResourceCache extends Cache {
4
+ constructor(options?: CreateCacheOptions);
5
+ }
@@ -0,0 +1,9 @@
1
+ import type { CFWorkerKV } from 'types/swell';
2
+ import { Cache } from './cache';
3
+ /**
4
+ * Theme cache is a 2-layer cache (memory, KV*)
5
+ * The KV layer supports namespacing and compression.
6
+ */
7
+ export declare class ThemeCache extends Cache {
8
+ constructor(namespace: string, store?: CFWorkerKV);
9
+ }
@@ -3,7 +3,6 @@ import { ShopifyCompatibility } from './compatibility/shopify';
3
3
  import type { SwellData, SwellRecord, SwellCollection, InferSwellCollection, SwellCollectionPages, StorefrontResourceGetter } from '../types/swell';
4
4
  export declare const MAX_QUERY_PAGE_LIMIT = 100;
5
5
  export declare const DEFAULT_QUERY_PAGE_LIMIT = 15;
6
- export declare const CACHE_TIMEOUT_RESOURCES: number;
7
6
  export declare class StorefrontResource<T extends SwellData = SwellData> {
8
7
  _getter?: StorefrontResourceGetter<T>;
9
8
  _getterHash?: string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@swell/apps-sdk",
3
3
  "type": "module",
4
- "version": "1.0.39",
4
+ "version": "1.0.40",
5
5
  "description": "Swell SDK for building isomorphic apps.",
6
6
  "author": "Swell",
7
7
  "license": "MIT",
@@ -38,11 +38,15 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "@swell/easyblocks-core": "^1.0.10",
41
+ "@types/lodash": "^4.17.5",
42
+ "cache-manager": "^6.3.2",
41
43
  "color": "^4.2.3",
44
+ "keyv": "^5.2.3",
42
45
  "liquidjs": "^10.16.1",
43
46
  "lodash": "^4.17.21",
44
47
  "lodash-es": "^4.17.21",
45
48
  "qs": "^6.12.3",
49
+ "quick-lru": "^7.0.0",
46
50
  "strftime": "^0.10.3",
47
51
  "swell-js": "^4.2.3"
48
52
  },
@@ -51,7 +55,6 @@
51
55
  "@kruining/waterlogged": "^1.1.45",
52
56
  "@types/color": "^3.0.6",
53
57
  "@types/jest": "29.5.14",
54
- "@types/lodash": "^4.17.5",
55
58
  "@types/lodash-es": "^4.17.12",
56
59
  "@types/qs": "^6.9.15",
57
60
  "@types/react": "^18.2.64",
@@ -1,18 +0,0 @@
1
- import type { CFWorkerKV } from 'types/swell';
2
- export declare class Cache {
3
- private map;
4
- private kvStore?;
5
- private timeoutDefault;
6
- constructor(kvStore?: CFWorkerKV, timeoutDefault?: number);
7
- setValues(values: Array<[string, unknown]>): void;
8
- getValues(): unknown[][];
9
- get<T>(key: string): Promise<T | undefined>;
10
- getSync<T>(key: string): T | undefined;
11
- set(key: string, value: any, timeout?: number): Promise<void>;
12
- setSync(key: string, value: any, timeout?: number): void;
13
- delete(key: string): Promise<void>;
14
- deleteSync(key: string): void;
15
- has(key: string): Promise<boolean>;
16
- hasSync(key: string): boolean;
17
- clear(prefix?: string): Promise<void>;
18
- }