@viu/emporix-sdk-react 1.0.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.
package/dist/index.js ADDED
@@ -0,0 +1,30 @@
1
+ export { useActiveCart, useAddressMutations, useAssignContact, useCancelOrder, useCart, useCartMutations, useCategories, useCategoriesInfinite, useCategory, useCategoryTree, useChangePassword, useCheckout, useCompany, useCompanyContacts, useCompanyGroups, useCompanyLocations, useCompanySwitcher, useCreateCart, useCreateCompany, useCreateLocation, useCustomerAddresses, useCustomerSession, useDefaultSite, useDeleteCompany, useDeleteLocation, useMatchPrices, useMyCompanies, useMyOrders, useMyOrdersInfinite, useMySegmentCategories, useMySegmentCategoriesInfinite, useMySegmentCategoryTree, useMySegmentItems, useMySegmentProducts, useMySegmentProductsInfinite, useMySegments, useOrder, useOrderTransition, usePasswordReset, usePaymentModes, useProduct, useProductByCode, useProductMedia, useProductSearch, useProducts, useProductsInCategory, useProductsInCategoryInfinite, useProductsInfinite, useReorder, useSalesOrder, useSiteContext, useSites, useUnassignContact, useUpdateCompany, useUpdateContactAssignment, useUpdateCustomer, useUpdateLocation, useUpdateSalesOrder } from './chunk-N3VDSKCT.js';
2
+ export { CompanyContextProvider, EmporixCompanyContext, EmporixProvider, useActiveCompany, useEmporix, useEmporixTelemetry } from './chunk-D43CSHK3.js';
3
+ export { createCookieStorage, createLocalStorageStorage, createMemoryStorage } from './chunk-FBQY2N7S.js';
4
+ export { prefetchCart, prefetchOrder, prefetchProduct } from './chunk-TIS4BKHK.js';
5
+ import { Component } from 'react';
6
+ import { EmporixAuthError, EmporixError } from '@viu/emporix-sdk';
7
+
8
+ var EmporixErrorBoundary = class extends Component {
9
+ state = { error: null };
10
+ static getDerivedStateFromError(error) {
11
+ return { error };
12
+ }
13
+ componentDidCatch(error, info) {
14
+ this.props.onError?.(error, info);
15
+ }
16
+ render() {
17
+ if (this.state.error) return this.props.fallback;
18
+ return this.props.children;
19
+ }
20
+ };
21
+ function useEmporixErrorHandler(handlers) {
22
+ return (error) => {
23
+ if (error instanceof EmporixAuthError) handlers.onAuthError?.(error);
24
+ else if (error instanceof EmporixError) handlers.onError?.(error);
25
+ };
26
+ }
27
+
28
+ export { EmporixErrorBoundary, useEmporixErrorHandler };
29
+ //# sourceMappingURL=index.js.map
30
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/errors.tsx"],"names":[],"mappings":";;;;;;;AAaO,IAAM,oBAAA,GAAN,cAAmC,SAAA,CAAwB;AAAA,EAChE,KAAA,GAAe,EAAE,KAAA,EAAO,IAAA,EAAK;AAAA,EAE7B,OAAO,yBAAyB,KAAA,EAAqB;AACnD,IAAA,OAAO,EAAE,KAAA,EAAM;AAAA,EACjB;AAAA,EAEA,iBAAA,CAAkB,OAAc,IAAA,EAAuB;AACrD,IAAA,IAAA,CAAK,KAAA,CAAM,OAAA,GAAU,KAAA,EAAO,IAAI,CAAA;AAAA,EAClC;AAAA,EAEA,MAAA,GAAoB;AAClB,IAAA,IAAI,IAAA,CAAK,KAAA,CAAM,KAAA,EAAO,OAAO,KAAK,KAAA,CAAM,QAAA;AACxC,IAAA,OAAO,KAAK,KAAA,CAAM,QAAA;AAAA,EACpB;AACF;AAGO,SAAS,uBAAuB,QAAA,EAGV;AAC3B,EAAA,OAAO,CAAC,KAAA,KAAmB;AACzB,IAAA,IAAI,KAAA,YAAiB,gBAAA,EAAkB,QAAA,CAAS,WAAA,GAAc,KAAK,CAAA;AAAA,SAAA,IAC1D,KAAA,YAAiB,YAAA,EAAc,QAAA,CAAS,OAAA,GAAU,KAAK,CAAA;AAAA,EAClE,CAAA;AACF","file":"index.js","sourcesContent":["import { Component, type ErrorInfo, type ReactNode } from \"react\";\nimport { EmporixError, EmporixAuthError } from \"@viu/emporix-sdk\";\n\ninterface Props {\n children: ReactNode;\n fallback: ReactNode;\n onError?: (error: Error, info: ErrorInfo) => void;\n}\ninterface State {\n error: Error | null;\n}\n\n/** Catches render errors (including thrown {@link EmporixError}) and shows a fallback. */\nexport class EmporixErrorBoundary extends Component<Props, State> {\n state: State = { error: null };\n\n static getDerivedStateFromError(error: Error): State {\n return { error };\n }\n\n componentDidCatch(error: Error, info: ErrorInfo): void {\n this.props.onError?.(error, info);\n }\n\n render(): ReactNode {\n if (this.state.error) return this.props.fallback;\n return this.props.children;\n }\n}\n\n/** Returns a handler that runs `onAuthError` for {@link EmporixAuthError}, else `onError`. */\nexport function useEmporixErrorHandler(handlers: {\n onAuthError?: (e: EmporixAuthError) => void;\n onError?: (e: EmporixError) => void;\n}): (error: unknown) => void {\n return (error: unknown) => {\n if (error instanceof EmporixAuthError) handlers.onAuthError?.(error);\n else if (error instanceof EmporixError) handlers.onError?.(error);\n };\n}\n"]}
@@ -0,0 +1,133 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { QueryClient } from '@tanstack/react-query';
4
+ import { EmporixClient } from '@viu/emporix-sdk';
5
+ import { EmporixStorage } from './storage.cjs';
6
+
7
+ /**
8
+ * All telemetry events emitted through the EmporixProvider's `onTelemetry`
9
+ * callback. Discriminated by `type` — exhaustive switch is type-safe.
10
+ *
11
+ * Consumers can emit their own `{ type: "custom" }` events via
12
+ * {@link useEmporixTelemetry}. Namespace `name` with an app-specific
13
+ * prefix (e.g. `"app.checkout-cta-click"`) to avoid collisions with
14
+ * future SDK event types.
15
+ */
16
+ type EmporixTelemetryEvent = {
17
+ type: "cache.hit";
18
+ queryKey: readonly unknown[];
19
+ tenant: string;
20
+ } | {
21
+ type: "cache.miss";
22
+ queryKey: readonly unknown[];
23
+ tenant: string;
24
+ durationMs: number;
25
+ } | {
26
+ type: "query.refetch";
27
+ queryKey: readonly unknown[];
28
+ tenant: string;
29
+ reason: "invalidate" | "focus" | "stale";
30
+ } | {
31
+ type: "query.error";
32
+ queryKey: readonly unknown[];
33
+ tenant: string;
34
+ error: unknown;
35
+ } | {
36
+ type: "mutation.success";
37
+ mutationKey?: readonly unknown[];
38
+ tenant: string;
39
+ durationMs: number;
40
+ } | {
41
+ type: "mutation.error";
42
+ mutationKey?: readonly unknown[];
43
+ tenant: string;
44
+ error: unknown;
45
+ durationMs: number;
46
+ } | {
47
+ type: "auth.refresh";
48
+ kind: "anonymous" | "customer";
49
+ tenant: string;
50
+ success: boolean;
51
+ } | {
52
+ type: "storage.write";
53
+ key: "customerToken" | "cartId" | "siteCode" | "anonymousSession" | "activeLegalEntityId" | "refreshToken";
54
+ } | {
55
+ type: "company:switched";
56
+ from: string | null;
57
+ to: string | null;
58
+ durationMs: number;
59
+ } | {
60
+ type: "custom";
61
+ name: string;
62
+ props?: Record<string, unknown>;
63
+ };
64
+ /**
65
+ * Hook to emit custom telemetry events through the same channel as SDK
66
+ * events. Throws when used outside an {@link EmporixProvider}.
67
+ *
68
+ * When the provider has no `onTelemetry` callback configured, `emit` is a
69
+ * no-op — calling it is safe and incurs no overhead.
70
+ */
71
+ declare function useEmporixTelemetry(): {
72
+ emit: (event: EmporixTelemetryEvent) => void;
73
+ };
74
+
75
+ interface EmporixContextValue {
76
+ client: EmporixClient;
77
+ storage: EmporixStorage;
78
+ }
79
+ interface SiteContextValue {
80
+ siteCode: string | null;
81
+ /** MS-4 populates this from the active site's DTO. */
82
+ currency: string | null;
83
+ /** MS-4 populates this from the active site's DTO. */
84
+ targetLocation: string | null;
85
+ /**
86
+ * Asynchronous site switch. Updates local state + storage immediately
87
+ * (optimistic), then PATCHes `/session-context/{tenant}/me/context` so
88
+ * the server sees the same site on the next request. When no session
89
+ * context exists yet (first visit, before any cart), the PATCH is
90
+ * skipped — local state still flips.
91
+ *
92
+ * `isSwitching` is `true` while the PATCH is in flight. `switchError`
93
+ * surfaces a PATCH failure; the optimistic state is NOT rolled back
94
+ * (the cache was already invalidated, the UI already moved on).
95
+ */
96
+ setSite: (code: string | null) => Promise<void>;
97
+ isSwitching: boolean;
98
+ switchError: Error | null;
99
+ }
100
+ declare const EmporixSiteContext: react.Context<SiteContextValue | null>;
101
+ /** Props for {@link EmporixProvider}. */
102
+ interface EmporixProviderProps {
103
+ client: EmporixClient;
104
+ queryClient?: QueryClient;
105
+ storage?: EmporixStorage;
106
+ initialCustomerToken?: string;
107
+ /**
108
+ * Initial site code. Resolution order: this prop → `storage.getSiteCode()` →
109
+ * `client.config.credentials.storefront.context.siteCode` → `null`.
110
+ */
111
+ initialSiteCode?: string;
112
+ /**
113
+ * Initial active legal-entity id (B2B). When set, the CompanyContext
114
+ * provider tries to match it against `companies.listMine()` once the
115
+ * customer is loaded; mismatches are dropped silently.
116
+ */
117
+ initialActiveLegalEntityId?: string | null;
118
+ /**
119
+ * Opt-in telemetry callback. Receives a typed event stream covering cache
120
+ * hit/miss, refetches, errors, mutations, auth refreshes, storage writes,
121
+ * and consumer-emitted custom events. Wire this to Datadog/Sentry/custom
122
+ * analytics. The handler is wrapped in try/catch — a throwing handler
123
+ * never breaks the provider.
124
+ */
125
+ onTelemetry?: (event: EmporixTelemetryEvent) => void;
126
+ children: ReactNode;
127
+ }
128
+ /** Provides the SDK client, token storage, react-query client, and site context to the tree. */
129
+ declare function EmporixProvider({ client, queryClient, storage, initialCustomerToken, initialSiteCode, initialActiveLegalEntityId, onTelemetry, children, }: EmporixProviderProps): React.JSX.Element;
130
+ /** Returns the SDK client and token storage. Throws outside an {@link EmporixProvider}. */
131
+ declare function useEmporix(): EmporixContextValue;
132
+
133
+ export { EmporixProvider as E, type SiteContextValue as S, type EmporixProviderProps as a, EmporixSiteContext as b, type EmporixTelemetryEvent as c, useEmporixTelemetry as d, useEmporix as u };
@@ -0,0 +1,133 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { QueryClient } from '@tanstack/react-query';
4
+ import { EmporixClient } from '@viu/emporix-sdk';
5
+ import { EmporixStorage } from './storage.js';
6
+
7
+ /**
8
+ * All telemetry events emitted through the EmporixProvider's `onTelemetry`
9
+ * callback. Discriminated by `type` — exhaustive switch is type-safe.
10
+ *
11
+ * Consumers can emit their own `{ type: "custom" }` events via
12
+ * {@link useEmporixTelemetry}. Namespace `name` with an app-specific
13
+ * prefix (e.g. `"app.checkout-cta-click"`) to avoid collisions with
14
+ * future SDK event types.
15
+ */
16
+ type EmporixTelemetryEvent = {
17
+ type: "cache.hit";
18
+ queryKey: readonly unknown[];
19
+ tenant: string;
20
+ } | {
21
+ type: "cache.miss";
22
+ queryKey: readonly unknown[];
23
+ tenant: string;
24
+ durationMs: number;
25
+ } | {
26
+ type: "query.refetch";
27
+ queryKey: readonly unknown[];
28
+ tenant: string;
29
+ reason: "invalidate" | "focus" | "stale";
30
+ } | {
31
+ type: "query.error";
32
+ queryKey: readonly unknown[];
33
+ tenant: string;
34
+ error: unknown;
35
+ } | {
36
+ type: "mutation.success";
37
+ mutationKey?: readonly unknown[];
38
+ tenant: string;
39
+ durationMs: number;
40
+ } | {
41
+ type: "mutation.error";
42
+ mutationKey?: readonly unknown[];
43
+ tenant: string;
44
+ error: unknown;
45
+ durationMs: number;
46
+ } | {
47
+ type: "auth.refresh";
48
+ kind: "anonymous" | "customer";
49
+ tenant: string;
50
+ success: boolean;
51
+ } | {
52
+ type: "storage.write";
53
+ key: "customerToken" | "cartId" | "siteCode" | "anonymousSession" | "activeLegalEntityId" | "refreshToken";
54
+ } | {
55
+ type: "company:switched";
56
+ from: string | null;
57
+ to: string | null;
58
+ durationMs: number;
59
+ } | {
60
+ type: "custom";
61
+ name: string;
62
+ props?: Record<string, unknown>;
63
+ };
64
+ /**
65
+ * Hook to emit custom telemetry events through the same channel as SDK
66
+ * events. Throws when used outside an {@link EmporixProvider}.
67
+ *
68
+ * When the provider has no `onTelemetry` callback configured, `emit` is a
69
+ * no-op — calling it is safe and incurs no overhead.
70
+ */
71
+ declare function useEmporixTelemetry(): {
72
+ emit: (event: EmporixTelemetryEvent) => void;
73
+ };
74
+
75
+ interface EmporixContextValue {
76
+ client: EmporixClient;
77
+ storage: EmporixStorage;
78
+ }
79
+ interface SiteContextValue {
80
+ siteCode: string | null;
81
+ /** MS-4 populates this from the active site's DTO. */
82
+ currency: string | null;
83
+ /** MS-4 populates this from the active site's DTO. */
84
+ targetLocation: string | null;
85
+ /**
86
+ * Asynchronous site switch. Updates local state + storage immediately
87
+ * (optimistic), then PATCHes `/session-context/{tenant}/me/context` so
88
+ * the server sees the same site on the next request. When no session
89
+ * context exists yet (first visit, before any cart), the PATCH is
90
+ * skipped — local state still flips.
91
+ *
92
+ * `isSwitching` is `true` while the PATCH is in flight. `switchError`
93
+ * surfaces a PATCH failure; the optimistic state is NOT rolled back
94
+ * (the cache was already invalidated, the UI already moved on).
95
+ */
96
+ setSite: (code: string | null) => Promise<void>;
97
+ isSwitching: boolean;
98
+ switchError: Error | null;
99
+ }
100
+ declare const EmporixSiteContext: react.Context<SiteContextValue | null>;
101
+ /** Props for {@link EmporixProvider}. */
102
+ interface EmporixProviderProps {
103
+ client: EmporixClient;
104
+ queryClient?: QueryClient;
105
+ storage?: EmporixStorage;
106
+ initialCustomerToken?: string;
107
+ /**
108
+ * Initial site code. Resolution order: this prop → `storage.getSiteCode()` →
109
+ * `client.config.credentials.storefront.context.siteCode` → `null`.
110
+ */
111
+ initialSiteCode?: string;
112
+ /**
113
+ * Initial active legal-entity id (B2B). When set, the CompanyContext
114
+ * provider tries to match it against `companies.listMine()` once the
115
+ * customer is loaded; mismatches are dropped silently.
116
+ */
117
+ initialActiveLegalEntityId?: string | null;
118
+ /**
119
+ * Opt-in telemetry callback. Receives a typed event stream covering cache
120
+ * hit/miss, refetches, errors, mutations, auth refreshes, storage writes,
121
+ * and consumer-emitted custom events. Wire this to Datadog/Sentry/custom
122
+ * analytics. The handler is wrapped in try/catch — a throwing handler
123
+ * never breaks the provider.
124
+ */
125
+ onTelemetry?: (event: EmporixTelemetryEvent) => void;
126
+ children: ReactNode;
127
+ }
128
+ /** Provides the SDK client, token storage, react-query client, and site context to the tree. */
129
+ declare function EmporixProvider({ client, queryClient, storage, initialCustomerToken, initialSiteCode, initialActiveLegalEntityId, onTelemetry, children, }: EmporixProviderProps): React.JSX.Element;
130
+ /** Returns the SDK client and token storage. Throws outside an {@link EmporixProvider}. */
131
+ declare function useEmporix(): EmporixContextValue;
132
+
133
+ export { EmporixProvider as E, type SiteContextValue as S, type EmporixProviderProps as a, EmporixSiteContext as b, type EmporixTelemetryEvent as c, useEmporixTelemetry as d, useEmporix as u };