@swishapp/sdk 0.130.0 → 0.131.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.
@@ -1,3 +1,4 @@
1
+ import { Signal } from "@preact/signals-core";
1
2
  import { SwishApp } from "../swish";
2
3
  export interface ItemContext {
3
4
  loading: boolean;
@@ -6,4 +7,10 @@ export interface ItemContext {
6
7
  variantId?: string;
7
8
  itemId?: string;
8
9
  }
9
- export declare const itemContextSignal: (swish: SwishApp) => (source: HTMLElement | Location) => import("@preact/signals-core").Signal<ItemContext>;
10
+ export interface ItemContextIds {
11
+ productId?: string | number | null;
12
+ variantId?: string | number | null;
13
+ itemId?: string | null;
14
+ }
15
+ export declare const itemContextFromIds: (ids: ItemContextIds) => Signal<ItemContext>;
16
+ export declare const itemContextSignal: (swish: SwishApp) => (source: HTMLElement | Location) => Signal<ItemContext>;
@@ -1,19 +1,47 @@
1
1
  import { Signal } from "@preact/signals-core";
2
2
  import { ApiError } from "@swishapp/api-client";
3
- import { ItemContext } from "./item-context-signal";
3
+ import type { EditItemListsSuccessResponse, EditItemVariantSuccessResponse, SaveItemSuccessResponse, UnsaveItemSuccessResponse } from "../intents/types";
4
4
  import { SwishApp } from "../swish";
5
- export declare const itemStateSignal: (swish: SwishApp) => (context: Signal<ItemContext>) => import("@preact/signals-core").ReadonlySignal<{
5
+ import { ItemContext } from "./item-context-signal";
6
+ export type ItemMatch = "variant" | "product" | "auto";
7
+ export interface ItemStateCallbacks {
8
+ onSave?: (data: SaveItemSuccessResponse["data"]) => void;
9
+ onUnsave?: (data: UnsaveItemSuccessResponse["data"]) => void;
10
+ onListsUpdate?: (data: EditItemListsSuccessResponse["data"]) => void;
11
+ onVariantUpdate?: (data: EditItemVariantSuccessResponse["data"]) => void;
12
+ }
13
+ export interface ItemStateOptions extends ItemStateCallbacks {
14
+ /**
15
+ * How to interpret the API response when deciding `saved`:
16
+ * - `"variant"`: only count the item as saved if the variant matches.
17
+ * - `"product"`: count any saved variant of the product as saved. Also
18
+ * causes `save()` to ignore `variantId` so the saved item is not pinned
19
+ * to whichever variant the consumer happened to render.
20
+ * - `"auto"` (default): variant-match when a `variantId` is in the context,
21
+ * product-match otherwise.
22
+ */
23
+ match?: ItemMatch;
24
+ }
25
+ export declare const itemStateSignal: (swish: SwishApp) => (context: Signal<ItemContext>, optionsInput?: ItemStateOptions) => import("@preact/signals-core").ReadonlySignal<{
6
26
  error: ApiError | null;
7
- status: string;
27
+ status: "saving" | "saved" | "unsaved";
8
28
  savedItemId: string | null;
29
+ savedItemIds: string[];
9
30
  loading: boolean;
10
31
  submitting: boolean;
11
32
  saved: boolean;
12
33
  saving: boolean;
34
+ unsaving: boolean;
13
35
  }> & {
14
36
  save: () => Promise<void>;
15
37
  unsave: () => Promise<void>;
38
+ updateLists: () => Promise<void>;
39
+ updateVariant: () => Promise<void>;
40
+ /** @deprecated Use `updateLists` instead. */
16
41
  update: () => Promise<void>;
17
42
  toggle: () => Promise<void>;
43
+ setOptions: (next: ItemStateOptions) => void;
44
+ /** @deprecated Use `setOptions` instead. */
45
+ setCallbacks: (next: ItemStateOptions) => void;
18
46
  dispose: () => void;
19
47
  };
package/dist/swish.d.ts CHANGED
@@ -61,19 +61,26 @@ declare class SwishApp {
61
61
  };
62
62
  readonly state: {
63
63
  itemContext: (source: HTMLElement | Location) => import("@preact/signals-core").Signal<import("./state").ItemContext>;
64
- itemState: (context: import("@preact/signals-core").Signal<import("./state").ItemContext>) => import("@preact/signals-core").ReadonlySignal<{
64
+ itemContextFromIds: (ids: import("./state").ItemContextIds) => import("@preact/signals-core").Signal<import("./state").ItemContext>;
65
+ itemState: (context: import("@preact/signals-core").Signal<import("./state").ItemContext>, optionsInput?: import("./state").ItemStateOptions) => import("@preact/signals-core").ReadonlySignal<{
65
66
  error: import("@swishapp/api-client").ApiError | null;
66
- status: string;
67
+ status: "saving" | "saved" | "unsaved";
67
68
  savedItemId: string | null;
69
+ savedItemIds: string[];
68
70
  loading: boolean;
69
71
  submitting: boolean;
70
72
  saved: boolean;
71
73
  saving: boolean;
74
+ unsaving: boolean;
72
75
  }> & {
73
76
  save: () => Promise<void>;
74
77
  unsave: () => Promise<void>;
78
+ updateLists: () => Promise<void>;
79
+ updateVariant: () => Promise<void>;
75
80
  update: () => Promise<void>;
76
81
  toggle: () => Promise<void>;
82
+ setOptions: (next: import("./state").ItemStateOptions) => void;
83
+ setCallbacks: (next: import("./state").ItemStateOptions) => void;
77
84
  dispose: () => void;
78
85
  };
79
86
  itemCount: () => import("@preact/signals-core").ReadonlySignal<{
@@ -107,6 +114,8 @@ export type * from "./intents/types";
107
114
  export type { SwishOptionsInput } from "./options/swish-options";
108
115
  export type * from "./options/types";
109
116
  export { getSwishQueryStorageKey } from "./query";
117
+ export type { ItemMatch, ItemStateCallbacks, ItemStateOptions, } from "./state/item-state-signal";
118
+ export type { ItemContext, ItemContextIds } from "./state/item-context-signal";
110
119
  export type * from "./storefront-api/storefront-api-client";
111
120
  export type { Ref, SwishUiElement } from "./swish-ui/swish-ui-element";
112
121
  export type { ListSelectOptions, ListSelectSubmitData, QuickBuyOptions, QuickBuySubmitData, Toast, ToastManager, UnsaveAlertOptions, UnsaveAlertSubmitData, VariantSelectOptions, VariantSelectSubmitData, } from "./swish-ui/types";