@ecomweb/storefront-kit 0.2.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/LICENSE +21 -0
- package/README.md +55 -0
- package/dist/api/client.js +162 -0
- package/dist/api/client.js.map +1 -0
- package/dist/api/index.js +7 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/proxy.js +145 -0
- package/dist/api/proxy.js.map +1 -0
- package/dist/api/response.js +55 -0
- package/dist/api/response.js.map +1 -0
- package/dist/api/server-client.js +159 -0
- package/dist/api/server-client.js.map +1 -0
- package/dist/auth/client-ip.js +44 -0
- package/dist/auth/client-ip.js.map +1 -0
- package/dist/auth/context.js +125 -0
- package/dist/auth/context.js.map +1 -0
- package/dist/auth/cookies.js +97 -0
- package/dist/auth/cookies.js.map +1 -0
- package/dist/auth/error-copy.js +49 -0
- package/dist/auth/error-copy.js.map +1 -0
- package/dist/auth/password-policy.js +29 -0
- package/dist/auth/password-policy.js.map +1 -0
- package/dist/auth/route-handlers.js +337 -0
- package/dist/auth/route-handlers.js.map +1 -0
- package/dist/auth/safe-redirect.js +20 -0
- package/dist/auth/safe-redirect.js.map +1 -0
- package/dist/auth/sdk.js +76 -0
- package/dist/auth/sdk.js.map +1 -0
- package/dist/auth/session-refresh.js +81 -0
- package/dist/auth/session-refresh.js.map +1 -0
- package/dist/auth/session.js +26 -0
- package/dist/auth/session.js.map +1 -0
- package/dist/auth/use-public-auth-config.js +29 -0
- package/dist/auth/use-public-auth-config.js.map +1 -0
- package/dist/cart/api.js +60 -0
- package/dist/cart/api.js.map +1 -0
- package/dist/cart/server-context.js +279 -0
- package/dist/cart/server-context.js.map +1 -0
- package/dist/cart/types.js +1 -0
- package/dist/cart/types.js.map +1 -0
- package/dist/cart/utils.js +49 -0
- package/dist/cart/utils.js.map +1 -0
- package/dist/checkout/get-payment-state.js +15 -0
- package/dist/checkout/get-payment-state.js.map +1 -0
- package/dist/checkout/initiate-gateway-payment.js +13 -0
- package/dist/checkout/initiate-gateway-payment.js.map +1 -0
- package/dist/checkout/order-handoff.js +61 -0
- package/dist/checkout/order-handoff.js.map +1 -0
- package/dist/checkout/payment-redirect.js +11 -0
- package/dist/checkout/payment-redirect.js.map +1 -0
- package/dist/checkout/payment-state-poll.js +45 -0
- package/dist/checkout/payment-state-poll.js.map +1 -0
- package/dist/checkout/start-gateway-redirect.js +16 -0
- package/dist/checkout/start-gateway-redirect.js.map +1 -0
- package/dist/checkout/stock-issues.js +35 -0
- package/dist/checkout/stock-issues.js.map +1 -0
- package/dist/checkout/success-state.js +15 -0
- package/dist/checkout/success-state.js.map +1 -0
- package/dist/checkout/types.js +1 -0
- package/dist/checkout/types.js.map +1 -0
- package/dist/checkout/use-checkout.js +256 -0
- package/dist/checkout/use-checkout.js.map +1 -0
- package/dist/checkout/utils.js +7 -0
- package/dist/checkout/utils.js.map +1 -0
- package/dist/config/index.js +7 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/log-level.js +8 -0
- package/dist/config/log-level.js.map +1 -0
- package/dist/config/mode.js +18 -0
- package/dist/config/mode.js.map +1 -0
- package/dist/config/public.js +115 -0
- package/dist/config/public.js.map +1 -0
- package/dist/config/schema.js +70 -0
- package/dist/config/schema.js.map +1 -0
- package/dist/config/server.js +68 -0
- package/dist/config/server.js.map +1 -0
- package/dist/config.d.ts +16 -0
- package/dist/config.js +15 -0
- package/dist/config.js.map +1 -0
- package/dist/currency.js +68 -0
- package/dist/currency.js.map +1 -0
- package/dist/index.d.ts +968 -0
- package/dist/index.js +146 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/api.types.js +1 -0
- package/dist/interfaces/api.types.js.map +1 -0
- package/dist/interfaces/index.js +2 -0
- package/dist/interfaces/index.js.map +1 -0
- package/dist/logging.js +42 -0
- package/dist/logging.js.map +1 -0
- package/dist/middleware/create-storefront-middleware.js +175 -0
- package/dist/middleware/create-storefront-middleware.js.map +1 -0
- package/dist/middleware.d.ts +24 -0
- package/dist/middleware.js +9 -0
- package/dist/middleware.js.map +1 -0
- package/dist/mode-1ZKDELPZ.d.ts +16 -0
- package/dist/notices.js +31 -0
- package/dist/notices.js.map +1 -0
- package/dist/orders/api-client.js +32 -0
- package/dist/orders/api-client.js.map +1 -0
- package/dist/orders/derive-progress.js +36 -0
- package/dist/orders/derive-progress.js.map +1 -0
- package/dist/orders/index.js +6 -0
- package/dist/orders/index.js.map +1 -0
- package/dist/orders/types.js +13 -0
- package/dist/orders/types.js.map +1 -0
- package/dist/orders/use-order-status.js +137 -0
- package/dist/orders/use-order-status.js.map +1 -0
- package/dist/promotions/use-promotion-validation.js +106 -0
- package/dist/promotions/use-promotion-validation.js.map +1 -0
- package/dist/promotions/utils.js +40 -0
- package/dist/promotions/utils.js.map +1 -0
- package/dist/provider.js +25 -0
- package/dist/provider.js.map +1 -0
- package/dist/redirects/index.js +2 -0
- package/dist/redirects/index.js.map +1 -0
- package/dist/redirects/lookup-redirect.js +13 -0
- package/dist/redirects/lookup-redirect.js.map +1 -0
- package/dist/request/public-url.js +20 -0
- package/dist/request/public-url.js.map +1 -0
- package/dist/schema-CEMKsCwh.d.ts +52 -0
- package/dist/sdk.js +33 -0
- package/dist/sdk.js.map +1 -0
- package/dist/server/routes.js +56 -0
- package/dist/server/routes.js.map +1 -0
- package/dist/server.d.ts +58 -0
- package/dist/server.js +19 -0
- package/dist/server.js.map +1 -0
- package/dist/shipping/format-shipping-option.js +12 -0
- package/dist/shipping/format-shipping-option.js.map +1 -0
- package/dist/shipping/location-areas.js +104 -0
- package/dist/shipping/location-areas.js.map +1 -0
- package/dist/shipping/messages.js +19 -0
- package/dist/shipping/messages.js.map +1 -0
- package/dist/shipping/shipping-calculation-reducer.js +75 -0
- package/dist/shipping/shipping-calculation-reducer.js.map +1 -0
- package/dist/shipping/use-shipping-calculation.js +84 -0
- package/dist/shipping/use-shipping-calculation.js.map +1 -0
- package/dist/shipping/utils.js +18 -0
- package/dist/shipping/utils.js.map +1 -0
- package/dist/storage.js +116 -0
- package/dist/storage.js.map +1 -0
- package/dist/store/store-context.js +72 -0
- package/dist/store/store-context.js.map +1 -0
- package/dist/store-scope/index.js +33 -0
- package/dist/store-scope/index.js.map +1 -0
- package/dist/store-scope/root.js +15 -0
- package/dist/store-scope/root.js.map +1 -0
- package/dist/store-scope/store-url.js +45 -0
- package/dist/store-scope/store-url.js.map +1 -0
- package/dist/stores/basic-settings.js +67 -0
- package/dist/stores/basic-settings.js.map +1 -0
- package/dist/stores/cached.js +20 -0
- package/dist/stores/cached.js.map +1 -0
- package/dist/stores/get-store-by-host.js +38 -0
- package/dist/stores/get-store-by-host.js.map +1 -0
- package/dist/stores/index.js +7 -0
- package/dist/stores/index.js.map +1 -0
- package/dist/stores/metadata.js +19 -0
- package/dist/stores/metadata.js.map +1 -0
- package/dist/stores/store-path.js +9 -0
- package/dist/stores/store-path.js.map +1 -0
- package/dist/stores/store-suspension.js +14 -0
- package/dist/stores/store-suspension.js.map +1 -0
- package/package.json +64 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,968 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as react from 'react';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
import * as _ecomweb_sdk from '@ecomweb/sdk';
|
|
5
|
+
import { StoreConfigurationInfo, StorePaymentMethod, OrderWithItems, PublicAuthConfig, CurrentIdentity, PasswordPolicy, OrderStatus, Order, CreateOrderRequest, LocationAreasResponse, LocationArea, StoreConfig } from '@ecomweb/sdk';
|
|
6
|
+
import currencyJs from 'currency.js';
|
|
7
|
+
import { AxiosRequestConfig, AxiosInstance } from 'axios';
|
|
8
|
+
import { Metadata } from 'next';
|
|
9
|
+
|
|
10
|
+
type NoticeKind = "success" | "error" | "info";
|
|
11
|
+
/**
|
|
12
|
+
* Something the shopper should hear about (an item added, a failed request).
|
|
13
|
+
* The kit never renders these; the app decides how, usually as a toast.
|
|
14
|
+
*/
|
|
15
|
+
interface Notice {
|
|
16
|
+
kind: NoticeKind;
|
|
17
|
+
title: string;
|
|
18
|
+
description?: string;
|
|
19
|
+
/** Stable identifier for apps that render some notices specially. */
|
|
20
|
+
code?: string;
|
|
21
|
+
/** Values the description was built from, keyed by name. */
|
|
22
|
+
data?: Record<string, string>;
|
|
23
|
+
}
|
|
24
|
+
type NoticeHandler = (notice: Notice) => void;
|
|
25
|
+
/** Report a notice to the app; a no-op outside a NoticeProvider. */
|
|
26
|
+
declare function useNotify(): NoticeHandler;
|
|
27
|
+
|
|
28
|
+
interface StorefrontKitProviderProps {
|
|
29
|
+
store: StoreConfigurationInfo;
|
|
30
|
+
/** The ref the cart is scoped to; defaults to `store.ref`. */
|
|
31
|
+
storeRef?: string;
|
|
32
|
+
/** Receives the kit's notices (item added, request failed, …). */
|
|
33
|
+
onNotice?: NoticeHandler;
|
|
34
|
+
children: ReactNode;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The kit's providers in their required order: auth reads the store, and the
|
|
38
|
+
* cart reads the signed-in identity from auth.
|
|
39
|
+
*/
|
|
40
|
+
declare function StorefrontKitProvider({ store, storeRef, onNotice, children, }: StorefrontKitProviderProps): react_jsx_runtime.JSX.Element;
|
|
41
|
+
|
|
42
|
+
interface CartItem {
|
|
43
|
+
id: string;
|
|
44
|
+
productId: number;
|
|
45
|
+
variantId?: number;
|
|
46
|
+
name: string;
|
|
47
|
+
price: number;
|
|
48
|
+
originalPrice?: number;
|
|
49
|
+
image?: string;
|
|
50
|
+
quantity: number;
|
|
51
|
+
variantName?: string;
|
|
52
|
+
variantOptions?: Array<{
|
|
53
|
+
optionName: string;
|
|
54
|
+
optionValue: string;
|
|
55
|
+
}>;
|
|
56
|
+
promotions?: Array<{
|
|
57
|
+
id: number;
|
|
58
|
+
name: string;
|
|
59
|
+
description?: string;
|
|
60
|
+
mode: string;
|
|
61
|
+
code?: string;
|
|
62
|
+
type: string;
|
|
63
|
+
condition_type: string;
|
|
64
|
+
discount_method?: string;
|
|
65
|
+
discount_value?: number;
|
|
66
|
+
fixed_price?: number;
|
|
67
|
+
min_amount?: number;
|
|
68
|
+
min_quantity?: number;
|
|
69
|
+
start_at: string;
|
|
70
|
+
end_at?: string;
|
|
71
|
+
}>;
|
|
72
|
+
bestPromotion?: any;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** What the shopper entered; the app owns the form and its validation. */
|
|
76
|
+
interface PlaceOrderInput {
|
|
77
|
+
fullName: string;
|
|
78
|
+
phone: string;
|
|
79
|
+
email?: string;
|
|
80
|
+
street: string;
|
|
81
|
+
/** Province code, as quoted for shipping. */
|
|
82
|
+
province: string;
|
|
83
|
+
/** Ward code, as quoted for shipping. */
|
|
84
|
+
ward: string;
|
|
85
|
+
notes?: string;
|
|
86
|
+
/** A `payment_method` code from `paymentMethods`. */
|
|
87
|
+
paymentMethod: string;
|
|
88
|
+
}
|
|
89
|
+
interface PlacedOrder {
|
|
90
|
+
order_code: string;
|
|
91
|
+
total: number | string | null;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* - redirect: the shopper is being sent to the payment provider.
|
|
95
|
+
* - redirect_failed: the order exists but the provider hand-off did not
|
|
96
|
+
* start; offer retryPaymentRedirect(). Further placeOrder calls return
|
|
97
|
+
* this again instead of creating a second order.
|
|
98
|
+
* - success: an offline-payment order; show its confirmation.
|
|
99
|
+
* - stock_issues: nothing was placed; `issues` is copy keyed by cart line id.
|
|
100
|
+
* - error: nothing was placed.
|
|
101
|
+
* - busy: an earlier placeOrder is still running; this call did nothing.
|
|
102
|
+
*/
|
|
103
|
+
type PlaceOrderResult = {
|
|
104
|
+
kind: "redirect";
|
|
105
|
+
} | {
|
|
106
|
+
kind: "redirect_failed";
|
|
107
|
+
orderCode: string;
|
|
108
|
+
} | {
|
|
109
|
+
kind: "success";
|
|
110
|
+
order: PlacedOrder;
|
|
111
|
+
} | {
|
|
112
|
+
kind: "stock_issues";
|
|
113
|
+
issues: Record<string, string>;
|
|
114
|
+
} | {
|
|
115
|
+
kind: "error";
|
|
116
|
+
message: string;
|
|
117
|
+
} | {
|
|
118
|
+
kind: "busy";
|
|
119
|
+
};
|
|
120
|
+
interface UseCheckoutOptions {
|
|
121
|
+
/** The address fields shipping is quoted against (codes, may be empty). */
|
|
122
|
+
province: string;
|
|
123
|
+
ward: string;
|
|
124
|
+
/** Leaves for the payment provider; defaults to a full-page navigation. */
|
|
125
|
+
navigateToGateway?: (url: string) => void;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Checkout without the UI: payment methods, shipping quotes, promotions,
|
|
129
|
+
* totals, placing the order and the payment-provider hand-off. The app
|
|
130
|
+
* renders the form and decides what to show for each PlaceOrderResult.
|
|
131
|
+
*/
|
|
132
|
+
declare function useCheckout({ province, ward, navigateToGateway, }: UseCheckoutOptions): {
|
|
133
|
+
items: CartItem[];
|
|
134
|
+
isCartLoading: boolean;
|
|
135
|
+
paymentMethods: StorePaymentMethod[];
|
|
136
|
+
isLoadingPaymentMethods: boolean;
|
|
137
|
+
shipping: {
|
|
138
|
+
fee: number;
|
|
139
|
+
options: _ecomweb_sdk.ShippingOption[];
|
|
140
|
+
selectedRate: number | null;
|
|
141
|
+
isCalculating: boolean;
|
|
142
|
+
hasCalculated: boolean;
|
|
143
|
+
calculationFailed: boolean;
|
|
144
|
+
selectRate: (rateId: number) => void;
|
|
145
|
+
retry: () => Promise<void>;
|
|
146
|
+
};
|
|
147
|
+
promotion: {
|
|
148
|
+
code: string;
|
|
149
|
+
setCode: react.Dispatch<react.SetStateAction<string>>;
|
|
150
|
+
applied: any;
|
|
151
|
+
shippingDiscount: number;
|
|
152
|
+
isValidating: boolean;
|
|
153
|
+
error: string;
|
|
154
|
+
validate: () => Promise<void>;
|
|
155
|
+
remove: () => void;
|
|
156
|
+
applySuggestion: (code: string) => Promise<void>;
|
|
157
|
+
};
|
|
158
|
+
totals: {
|
|
159
|
+
subtotal: number;
|
|
160
|
+
originalSubtotal: number;
|
|
161
|
+
discount: number;
|
|
162
|
+
cartTotal: number;
|
|
163
|
+
shipping: number;
|
|
164
|
+
shippingDiscount: number;
|
|
165
|
+
total: number;
|
|
166
|
+
};
|
|
167
|
+
savedAddressId: number | undefined;
|
|
168
|
+
selectSavedAddress: react.Dispatch<react.SetStateAction<number | undefined>>;
|
|
169
|
+
stockIssues: Record<string, string>;
|
|
170
|
+
isSubmitting: boolean;
|
|
171
|
+
redirectError: {
|
|
172
|
+
order_code: string;
|
|
173
|
+
provider_code: string;
|
|
174
|
+
} | null;
|
|
175
|
+
isRetryingRedirect: boolean;
|
|
176
|
+
placeOrder: (input: PlaceOrderInput) => Promise<PlaceOrderResult>;
|
|
177
|
+
retryPaymentRedirect: () => Promise<PlaceOrderResult>;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
interface OrderHandoff {
|
|
181
|
+
order_code: string;
|
|
182
|
+
/** `cod`, `bank_transfer`, or a gateway code. */
|
|
183
|
+
payment_method: string;
|
|
184
|
+
/** Non-null only for gateway-backed methods. */
|
|
185
|
+
provider_code: string | null;
|
|
186
|
+
total: number;
|
|
187
|
+
currency_code: string;
|
|
188
|
+
written_at: number;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Returns the handoff only if it is present, well-formed, fresh, and for the
|
|
192
|
+
* order being shown. Any mismatch yields null: showing one order's amount on
|
|
193
|
+
* another order's screen would be worse than showing no amount at all.
|
|
194
|
+
*/
|
|
195
|
+
declare function readOrderHandoff(order_code: string | null): OrderHandoff | null;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Which success screen an order gets.
|
|
199
|
+
*
|
|
200
|
+
* `confirming` / `still-confirming` / `failed` belong to the gateway return
|
|
201
|
+
* flow, where the browser beats the provider's callback back to us and the
|
|
202
|
+
* page polls until the real state resolves.
|
|
203
|
+
*
|
|
204
|
+
* `placed` and `awaiting-transfer` are the offline methods, which now land
|
|
205
|
+
* here too rather than rendering an inline view on the checkout page. They
|
|
206
|
+
* are terminal on arrival: nothing is in flight, so there is nothing to poll
|
|
207
|
+
* for and the screen must not imply otherwise.
|
|
208
|
+
*
|
|
209
|
+
* `awaiting-transfer` exists because a bank_transfer order stays `pending`
|
|
210
|
+
* until the merchant confirms the money arrived. Without it such an order
|
|
211
|
+
* fell into `still-confirming`, whose copy says confirmation "is taking
|
|
212
|
+
* longer than usual" — actively wrong. Nothing is slow; the shopper simply
|
|
213
|
+
* has not paid yet.
|
|
214
|
+
*/
|
|
215
|
+
type PaymentConfirmationState = "confirming" | "paid" | "placed" | "awaiting-transfer" | "still-confirming" | "failed";
|
|
216
|
+
/**
|
|
217
|
+
* The state to render on arrival, before any polling.
|
|
218
|
+
*
|
|
219
|
+
* `order_code` absent means there is no order to confirm at all, which keeps
|
|
220
|
+
* the historical behaviour of treating the page as an immediate success
|
|
221
|
+
* screen.
|
|
222
|
+
*/
|
|
223
|
+
declare function resolveInitialState(order_code: string | null, handoff: OrderHandoff | null): PaymentConfirmationState;
|
|
224
|
+
/**
|
|
225
|
+
* Whether this state should start the gateway confirmation poll.
|
|
226
|
+
*
|
|
227
|
+
* Offline orders must not: there is no callback coming, so polling would burn
|
|
228
|
+
* the whole backoff budget and then downgrade a perfectly good screen to
|
|
229
|
+
* "still-confirming".
|
|
230
|
+
*/
|
|
231
|
+
declare function shouldPollForConfirmation(state: PaymentConfirmationState): boolean;
|
|
232
|
+
|
|
233
|
+
/** The bank_transfer slice of the public payment-methods projection. */
|
|
234
|
+
interface BankAccountView {
|
|
235
|
+
id: number;
|
|
236
|
+
bank_name: string;
|
|
237
|
+
bank_code: string;
|
|
238
|
+
account_number: string;
|
|
239
|
+
account_holder: string;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* What the shopper should be told about a just-placed order (the
|
|
243
|
+
* order-success page, which is also the payment gateway's return URL).
|
|
244
|
+
*
|
|
245
|
+
* Arriving back from a gateway proves nothing about the payment: the
|
|
246
|
+
* provider's server-to-server callback usually lands later, so a gateway
|
|
247
|
+
* order polls the API until it is paid or failed. Offline orders read the
|
|
248
|
+
* checkout's handoff and never poll. See checkout/success-state.ts.
|
|
249
|
+
*/
|
|
250
|
+
declare function useOrderStatus({ orderCode, wait, }: {
|
|
251
|
+
orderCode: string | null;
|
|
252
|
+
/** Delay between polls; injectable for tests. */
|
|
253
|
+
wait?: (ms: number) => Promise<void>;
|
|
254
|
+
}): {
|
|
255
|
+
handoff: OrderHandoff | null;
|
|
256
|
+
payment: {
|
|
257
|
+
state: PaymentConfirmationState;
|
|
258
|
+
isPolling: boolean;
|
|
259
|
+
};
|
|
260
|
+
bankTransfer: {
|
|
261
|
+
accounts: BankAccountView[];
|
|
262
|
+
note: string | null;
|
|
263
|
+
};
|
|
264
|
+
};
|
|
265
|
+
/**
|
|
266
|
+
* A guest's order, looked up by its code plus the email or phone it was
|
|
267
|
+
* placed with (the view-order page).
|
|
268
|
+
*/
|
|
269
|
+
declare function useOrderLookup({ orderCode, email, phone, }: {
|
|
270
|
+
orderCode: string | null;
|
|
271
|
+
email?: string | null;
|
|
272
|
+
phone?: string | null;
|
|
273
|
+
}): {
|
|
274
|
+
order: OrderWithItems | null;
|
|
275
|
+
isLoading: boolean;
|
|
276
|
+
error: string | null;
|
|
277
|
+
retry: () => Promise<void>;
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
type SupportedCurrency = "VND" | "USD" | "EUR" | "SGD";
|
|
281
|
+
declare function getDefaultLocale(currency: SupportedCurrency): string;
|
|
282
|
+
/**
|
|
283
|
+
* Format an amount as money in the given currency.
|
|
284
|
+
*
|
|
285
|
+
* `locale` is retained for call-site compatibility but no longer affects the
|
|
286
|
+
* output — the house convention in CURRENCY_FORMATS is uniform across
|
|
287
|
+
* locales. It still matters to formatNumber, which is not currency.
|
|
288
|
+
*/
|
|
289
|
+
declare function formatPrice(amount: number, locale?: string, code?: SupportedCurrency | string): string;
|
|
290
|
+
declare function formatNumber(amount: number, locale?: string): string;
|
|
291
|
+
declare function formatPriceRange(min?: number, max?: number, locale?: string, code?: SupportedCurrency): string;
|
|
292
|
+
declare const money: (value: number | string, locale?: string, code?: SupportedCurrency) => currencyJs;
|
|
293
|
+
declare function formatPriceWithoutSymbol(amount: number, locale?: string, code?: SupportedCurrency): string;
|
|
294
|
+
|
|
295
|
+
interface StoreContextData {
|
|
296
|
+
store: StoreConfigurationInfo;
|
|
297
|
+
}
|
|
298
|
+
declare function useStore(): StoreContextData;
|
|
299
|
+
declare function useStoreData(): StoreConfigurationInfo;
|
|
300
|
+
/** The store ref, or null outside a StoreProvider (e.g. a store-level 404). */
|
|
301
|
+
declare function useOptionalStoreId(): string | null;
|
|
302
|
+
declare function useStoreId(): string;
|
|
303
|
+
declare function useStoreCurrency(): SupportedCurrency;
|
|
304
|
+
declare function useStoreLocale(): string;
|
|
305
|
+
|
|
306
|
+
interface UsePublicAuthConfigResult {
|
|
307
|
+
/** `null` while loading and after a failed load — check `error` to tell them apart. */
|
|
308
|
+
config: PublicAuthConfig | null;
|
|
309
|
+
error: boolean;
|
|
310
|
+
reload: () => void;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* The store's public auth config (which sign-in methods are on, and the
|
|
314
|
+
* password policy). Served by the storefront's own route so no API key or
|
|
315
|
+
* backend origin is needed in the browser.
|
|
316
|
+
*
|
|
317
|
+
* Shared by the login/register form and the checkout create-account option so
|
|
318
|
+
* both gate on the same `password_enabled` / `password_policy`.
|
|
319
|
+
*/
|
|
320
|
+
declare function usePublicAuthConfig(storeRef: string): UsePublicAuthConfigResult;
|
|
321
|
+
|
|
322
|
+
interface ApiError {
|
|
323
|
+
message: string;
|
|
324
|
+
statusCode: number;
|
|
325
|
+
error?: string;
|
|
326
|
+
/** The business error's structured `details`, e.g. `{ items }` on INSUFFICIENT_INVENTORY. */
|
|
327
|
+
details?: unknown;
|
|
328
|
+
}
|
|
329
|
+
interface ApiResponse<T = any> {
|
|
330
|
+
data?: T;
|
|
331
|
+
error?: ApiError;
|
|
332
|
+
success: boolean;
|
|
333
|
+
}
|
|
334
|
+
interface RequestConfig {
|
|
335
|
+
headers?: Record<string, string>;
|
|
336
|
+
signal?: AbortSignal;
|
|
337
|
+
[key: string]: any;
|
|
338
|
+
}
|
|
339
|
+
interface AuthenticatedApiClient {
|
|
340
|
+
get<T>(url: string, config?: RequestConfig): Promise<ApiResponse<T>>;
|
|
341
|
+
post<T>(url: string, data?: any, config?: RequestConfig): Promise<ApiResponse<T>>;
|
|
342
|
+
put<T>(url: string, data?: any, config?: RequestConfig): Promise<ApiResponse<T>>;
|
|
343
|
+
patch<T>(url: string, data?: any, config?: RequestConfig): Promise<ApiResponse<T>>;
|
|
344
|
+
delete<T>(url: string, config?: RequestConfig): Promise<ApiResponse<T>>;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
interface ApiClientOptions {
|
|
348
|
+
/**
|
|
349
|
+
* Send `x-storefront-api-key` from the browser. True for direct calls to the
|
|
350
|
+
* backend's own origin; false for calls routed through the same-origin proxy
|
|
351
|
+
* (`lib/api/proxy.ts`), which attaches the key server-side.
|
|
352
|
+
*/
|
|
353
|
+
withApiKey?: boolean;
|
|
354
|
+
timeout?: number;
|
|
355
|
+
}
|
|
356
|
+
declare class ApiClient implements AuthenticatedApiClient {
|
|
357
|
+
private axiosInstance;
|
|
358
|
+
private withApiKey;
|
|
359
|
+
constructor(baseUrl?: string, options?: ApiClientOptions);
|
|
360
|
+
private setupInterceptors;
|
|
361
|
+
private handleResponse;
|
|
362
|
+
private handleError;
|
|
363
|
+
get<T>(url: string, config?: AxiosRequestConfig): Promise<ApiResponse<T>>;
|
|
364
|
+
post<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse<T>>;
|
|
365
|
+
put<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse<T>>;
|
|
366
|
+
patch<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse<T>>;
|
|
367
|
+
delete<T>(url: string, config?: AxiosRequestConfig): Promise<ApiResponse<T>>;
|
|
368
|
+
getAxiosInstance(): AxiosInstance;
|
|
369
|
+
setBaseURL(baseUrl: string): void;
|
|
370
|
+
setTimeout(timeout: number): void;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Direct, key-authenticated client for the backend's own origin. Public
|
|
374
|
+
* endpoints only — it can never carry a session token (see the request
|
|
375
|
+
* interceptor above).
|
|
376
|
+
*/
|
|
377
|
+
declare const publicApiClient: ApiClient;
|
|
378
|
+
|
|
379
|
+
declare function unwrap<T>(response: ApiResponse<T>): T;
|
|
380
|
+
declare function unwrapOrNull<T>(response: ApiResponse<T>, nullStatusCodes?: number[]): T | null;
|
|
381
|
+
|
|
382
|
+
interface AuthError {
|
|
383
|
+
message: string;
|
|
384
|
+
code?: string;
|
|
385
|
+
}
|
|
386
|
+
interface AuthContextType {
|
|
387
|
+
user: CurrentIdentity | null;
|
|
388
|
+
loading: boolean;
|
|
389
|
+
isAnonymous: boolean;
|
|
390
|
+
signIn: (email: string, password: string) => Promise<{
|
|
391
|
+
error: AuthError | null;
|
|
392
|
+
}>;
|
|
393
|
+
signUp: (email: string, password: string, name?: string) => Promise<{
|
|
394
|
+
error: AuthError | null;
|
|
395
|
+
}>;
|
|
396
|
+
sendLoginCode: (email: string) => Promise<{
|
|
397
|
+
error: AuthError | null;
|
|
398
|
+
}>;
|
|
399
|
+
verifyOtp: (email: string, code: string) => Promise<{
|
|
400
|
+
error: AuthError | null;
|
|
401
|
+
}>;
|
|
402
|
+
signInWithGoogle: (nextPath?: string) => Promise<{
|
|
403
|
+
error: AuthError | null;
|
|
404
|
+
}>;
|
|
405
|
+
signInAnonymously: () => Promise<{
|
|
406
|
+
error: AuthError | null;
|
|
407
|
+
}>;
|
|
408
|
+
signOut: () => Promise<void>;
|
|
409
|
+
refresh: () => Promise<void>;
|
|
410
|
+
}
|
|
411
|
+
declare function useAuth(): AuthContextType;
|
|
412
|
+
|
|
413
|
+
/** Maps a backend error code to Vietnamese copy. */
|
|
414
|
+
declare function errorCopy(code: string | null | undefined, fallback?: string): string;
|
|
415
|
+
/**
|
|
416
|
+
* The same mapping for a caught throwable. `ApiClientError` (thrown by the
|
|
417
|
+
* SDK's `unwrap`) carries `code` and `statusCode`; `lib/auth/context.tsx`
|
|
418
|
+
* surfaces `{ message, code }`. Anything else falls through to `fallback`.
|
|
419
|
+
*
|
|
420
|
+
* The caught value's own `message` is deliberately never used: it is either
|
|
421
|
+
* the backend's English prose or an axios internal string.
|
|
422
|
+
*/
|
|
423
|
+
declare function errorCopyFor(error: unknown, fallback?: string): string;
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* The store's password rules, shared by every place a shopper can set a
|
|
427
|
+
* password (the login/register form and the checkout "create an account"
|
|
428
|
+
* option). The policy is per-store and comes from the public auth config, so
|
|
429
|
+
* nothing here may hardcode a minimum length.
|
|
430
|
+
*
|
|
431
|
+
* The backend enforces the same rules and rejects a bad password with
|
|
432
|
+
* `AUTH_WEAK_PASSWORD`; validating here only spares the shopper a round trip.
|
|
433
|
+
*/
|
|
434
|
+
declare function getPasswordPolicyHints(policy: PasswordPolicy): string[];
|
|
435
|
+
/**
|
|
436
|
+
* Returns a Vietnamese validation message, or `null` when the password
|
|
437
|
+
* satisfies the store's policy.
|
|
438
|
+
*/
|
|
439
|
+
declare function validatePasswordAgainstPolicy(password: string, policy: PasswordPolicy): string | null;
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* The one post-auth redirect guard.
|
|
443
|
+
*
|
|
444
|
+
* Both places a shopper can name where to go after signing in — the `?next=`
|
|
445
|
+
* on `/login` and `/register` (`components/auth/auth-form.tsx`) and the `next=`
|
|
446
|
+
* that survives the Google round trip (`lib/auth/route-handlers.ts`) — resolve
|
|
447
|
+
* the candidate here. A second copy is exactly how the two drifted apart in the
|
|
448
|
+
* first place, so there is deliberately only one.
|
|
449
|
+
*
|
|
450
|
+
* The rule is "resolve, then compare origins", not "reject a list of prefixes":
|
|
451
|
+
*
|
|
452
|
+
* - `//evil.com` and `/\evil.com` both resolve to `https://evil.com/`, the
|
|
453
|
+
* second because the WHATWG parser normalises `\` to `/` for special
|
|
454
|
+
* schemes exactly as browsers do. A `startsWith("//")` check catches the
|
|
455
|
+
* first and misses the second.
|
|
456
|
+
* - `http:/evil.com` resolves to `http://evil.com/` — a scheme-carrying value
|
|
457
|
+
* that starts with neither `//` nor `http://`.
|
|
458
|
+
* - `javascript:alert(1)` parses with an opaque origin (`"null"`), which is
|
|
459
|
+
* never equal to the page origin.
|
|
460
|
+
*
|
|
461
|
+
* Comparing the resolved origin covers all of them without enumerating any.
|
|
462
|
+
*/
|
|
463
|
+
/** Where a rejected or missing candidate goes. Relative, so it stays inside
|
|
464
|
+
* whichever store path or custom domain the caller is already on. */
|
|
465
|
+
declare const DEFAULT_REDIRECT = "/";
|
|
466
|
+
/**
|
|
467
|
+
* A same-origin, relative path safe to hand to `router.push()` or
|
|
468
|
+
* `NextResponse.redirect(new URL(path, request.url))`.
|
|
469
|
+
*
|
|
470
|
+
* @param candidate the shopper-supplied value (`?next=`), or null/undefined
|
|
471
|
+
* @param base an absolute URL of the current page — `window.location.origin`
|
|
472
|
+
* in the browser, `request.url` on the server
|
|
473
|
+
*/
|
|
474
|
+
declare function safeRedirectPath(candidate: string | null | undefined, base: string): string;
|
|
475
|
+
|
|
476
|
+
interface CartState {
|
|
477
|
+
items: CartItem[];
|
|
478
|
+
total: number;
|
|
479
|
+
subtotal: number;
|
|
480
|
+
originalSubtotal: number;
|
|
481
|
+
discountAmount: number;
|
|
482
|
+
shippingFee: number;
|
|
483
|
+
itemCount: number;
|
|
484
|
+
isLoading: boolean;
|
|
485
|
+
cartId?: string;
|
|
486
|
+
sessionId?: string;
|
|
487
|
+
}
|
|
488
|
+
interface CartContextType extends CartState {
|
|
489
|
+
addItem: (item: Omit<CartItem, "quantity"> & {
|
|
490
|
+
quantity?: number;
|
|
491
|
+
}) => Promise<void>;
|
|
492
|
+
removeItem: (id: string) => Promise<void>;
|
|
493
|
+
updateQuantity: (id: string, quantity: number) => Promise<void>;
|
|
494
|
+
/** Pass false to skip the "cart cleared" notice (e.g. after checkout). */
|
|
495
|
+
clearCart: (notifyCleared?: boolean) => Promise<void>;
|
|
496
|
+
refreshCart: () => Promise<void>;
|
|
497
|
+
mergeCarts: (userId: string) => Promise<void>;
|
|
498
|
+
reorderItems: (items: {
|
|
499
|
+
product_id: number;
|
|
500
|
+
variant_id?: number;
|
|
501
|
+
quantity: number;
|
|
502
|
+
}[]) => Promise<{
|
|
503
|
+
added: number;
|
|
504
|
+
total: number;
|
|
505
|
+
}>;
|
|
506
|
+
}
|
|
507
|
+
declare function useCart(): CartContextType;
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Polling policy for confirming a gateway (OnePay/MoMo) payment after the
|
|
511
|
+
* shopper's browser returns from the provider.
|
|
512
|
+
*
|
|
513
|
+
* The browser almost always beats the gateway's server-to-server callback
|
|
514
|
+
* back to us — so the return URL carries no authority of its own; it only
|
|
515
|
+
* decides which page to show. The order's real state comes from the API,
|
|
516
|
+
* which the callback updates asynchronously. This module is the policy that
|
|
517
|
+
* keeps asking the API until that state resolves, or gives up gracefully.
|
|
518
|
+
*
|
|
519
|
+
* A late callback is NORMAL, not exceptional — it is exactly the case the
|
|
520
|
+
* expiry design elsewhere in this system accommodates by expiring an
|
|
521
|
+
* abandoned attempt to reversible `ON_HOLD` rather than terminal
|
|
522
|
+
* `CANCELLED`. So exhausting the attempt budget here must resolve to a
|
|
523
|
+
* "pending" outcome, never "failed": a shopper who actually paid and is
|
|
524
|
+
* merely waiting on a slow callback must never be told their payment
|
|
525
|
+
* failed.
|
|
526
|
+
*
|
|
527
|
+
* Deliberately pure and timer-free: no `setTimeout`/`setInterval`, no
|
|
528
|
+
* `window`, no React state. The delay mechanism (`wait`) is injected by the
|
|
529
|
+
* caller, so tests can run this to completion synchronously (or nearly so)
|
|
530
|
+
* with a `wait` that resolves immediately and simply records the delay it
|
|
531
|
+
* was asked for — no fake timers required. `computeBackoffDelay` is also
|
|
532
|
+
* exported standalone so the backoff schedule itself can be asserted on
|
|
533
|
+
* without invoking the poll loop at all.
|
|
534
|
+
*/
|
|
535
|
+
/** The shape mirrored from the intended `sdk.payments.getPaymentState`
|
|
536
|
+
* response — see `lib/checkout/get-payment-state.ts` for the temporary
|
|
537
|
+
* adapter that fetches it. */
|
|
538
|
+
interface PaymentStateResult {
|
|
539
|
+
payment_status: string;
|
|
540
|
+
order_status: string;
|
|
541
|
+
}
|
|
542
|
+
type PollOutcome = {
|
|
543
|
+
status: "paid";
|
|
544
|
+
result: PaymentStateResult;
|
|
545
|
+
} | {
|
|
546
|
+
status: "failed";
|
|
547
|
+
result: PaymentStateResult;
|
|
548
|
+
}
|
|
549
|
+
/** Attempts ran out (or every attempt errored) without a terminal status.
|
|
550
|
+
* `result` is the last successful poll's payload, if any attempt
|
|
551
|
+
* succeeded at all. */
|
|
552
|
+
| {
|
|
553
|
+
status: "pending";
|
|
554
|
+
result: PaymentStateResult | null;
|
|
555
|
+
};
|
|
556
|
+
interface PollPaymentStateOptions {
|
|
557
|
+
/** Performs one status check against the API. May reject — a single
|
|
558
|
+
* rejection must not abort the sequence. */
|
|
559
|
+
poll: () => Promise<PaymentStateResult>;
|
|
560
|
+
/** Delay mechanism, injected by the caller (e.g. a `setTimeout`-based
|
|
561
|
+
* wait in the page component). Deliberately has no default here so this
|
|
562
|
+
* module never touches a timer API directly. */
|
|
563
|
+
wait: (ms: number) => Promise<void>;
|
|
564
|
+
/** Total number of poll attempts before giving up. Default 6. */
|
|
565
|
+
maxAttempts?: number;
|
|
566
|
+
/** Delay before the 2nd attempt, ms. Default 2000. */
|
|
567
|
+
baseDelayMs?: number;
|
|
568
|
+
/** Multiplier applied to the delay on each subsequent gap. Default 1.5. */
|
|
569
|
+
backoffFactor?: number;
|
|
570
|
+
/** Ceiling on any single delay, ms. Default 15000. */
|
|
571
|
+
maxDelayMs?: number;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Repeatedly polls payment state until it resolves to a terminal status
|
|
575
|
+
* (`paid`, `failed`), or the attempt budget is exhausted — in which case
|
|
576
|
+
* the outcome is `"pending"`, never `"failed"`. A single poll error is
|
|
577
|
+
* swallowed and counted as a used (non-terminal) attempt so a transient
|
|
578
|
+
* network blip cannot abort the sequence.
|
|
579
|
+
*/
|
|
580
|
+
declare function pollPaymentState(options: PollPaymentStateOptions): Promise<PollOutcome>;
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* TEMPORARY adapter standing in for `sdk.payments.getPaymentState`.
|
|
584
|
+
*
|
|
585
|
+
* `sdk.payments` was added by this plan's Task 2
|
|
586
|
+
* (`ecomweb-sdk` branch `feat/payment-client-methods`, commit `58c2c76`),
|
|
587
|
+
* but that SDK PR is not yet merged to `main`, and this repo's lockfile
|
|
588
|
+
* pins an older `@ecomweb/sdk` commit that has no `payments` module at all
|
|
589
|
+
* — see `lib/checkout/initiate-gateway-payment.ts` for the full story
|
|
590
|
+
* (same pinned commit, same missing module). Running
|
|
591
|
+
* `pnpm update @ecomweb/sdk` here is explicitly out of scope for this task
|
|
592
|
+
* — it is a deliberate later step in the plan.
|
|
593
|
+
*
|
|
594
|
+
* This function mirrors `PaymentsApi.getPaymentState()`'s request/response
|
|
595
|
+
* shape exactly (same path, same result shape) so that once the SDK bump
|
|
596
|
+
* lands, the fix is: delete this file, delete this import in
|
|
597
|
+
* `payment-state-poll`'s caller, and call
|
|
598
|
+
* `sdk.payments.getPaymentState(...)` instead — the call site already reads
|
|
599
|
+
* as if it were doing that today.
|
|
600
|
+
*
|
|
601
|
+
* FOLLOW-UP: delete this file once `@ecomweb/sdk` is bumped past
|
|
602
|
+
* `feat/payment-client-methods` and exposes `sdk.payments.getPaymentState`.
|
|
603
|
+
*/
|
|
604
|
+
declare function getPaymentState(storeRef: string, order_code: string): Promise<PaymentStateResult>;
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Turns the API's 409 INSUFFICIENT_INVENTORY into per-line shopper copy.
|
|
608
|
+
*
|
|
609
|
+
* The error's `details.items` names variants, not cart lines, and only
|
|
610
|
+
* variants whose stock is short; lines are matched by `variantId`. Duck-typed
|
|
611
|
+
* rather than `instanceof ApiClientError`: the storefront and the SDK each
|
|
612
|
+
* define an ApiClientError class.
|
|
613
|
+
*/
|
|
614
|
+
declare const STOCK_ISSUE_TOAST_DESCRIPTION = "Vui l\u00F2ng \u0111i\u1EC1u ch\u1EC9nh c\u00E1c s\u1EA3n ph\u1EA9m \u0111\u01B0\u1EE3c \u0111\u00E1nh d\u1EA5u trong \u0111\u01A1n h\u00E0ng r\u1ED3i \u0111\u1EB7t l\u1EA1i.";
|
|
615
|
+
|
|
616
|
+
declare const STATUS_OPTIONS: {
|
|
617
|
+
label: string;
|
|
618
|
+
value: "all" | OrderStatus;
|
|
619
|
+
}[];
|
|
620
|
+
|
|
621
|
+
declare const publicOrdersApi: {
|
|
622
|
+
getPublicOrderStatus(storeRef: string, orderNumber: number, contactInfo: {
|
|
623
|
+
email?: string;
|
|
624
|
+
phone?: string;
|
|
625
|
+
}): Promise<Order | null>;
|
|
626
|
+
/**
|
|
627
|
+
* `isAuth` comes from the caller's auth context — this module is not a
|
|
628
|
+
* component and must not read context or an httpOnly cookie itself.
|
|
629
|
+
*
|
|
630
|
+
* When true the SDK posts to `/tenant/stores/:ref/customers/orders`, which
|
|
631
|
+
* derives BOTH `customer_id` (ResolveCustomerInterceptor) and `user_id`
|
|
632
|
+
* (JWT) server-side; the payload carries neither, and `CreateOrderDto` has no
|
|
633
|
+
* such fields to carry them in. The request reaches that guarded route
|
|
634
|
+
* through the same-origin proxy, which attaches the bearer token.
|
|
635
|
+
*/
|
|
636
|
+
createStoreOrder(storeRef: string, data: Omit<CreateOrderRequest, "store_id">, isAuth: boolean): Promise<OrderWithItems>;
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
type CreatedOrder = Awaited<ReturnType<typeof publicOrdersApi.createStoreOrder>>;
|
|
640
|
+
interface OrderSuccessInfo {
|
|
641
|
+
order: CreatedOrder;
|
|
642
|
+
email?: string;
|
|
643
|
+
phone?: string;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* The shopper-facing journey. The API tracks four independent status fields;
|
|
648
|
+
* only order_status and shipping_status are meaningful to a customer.
|
|
649
|
+
* fulfillment_status is warehouse vocabulary ("unfulfilled",
|
|
650
|
+
* "return_requested") and is never shown.
|
|
651
|
+
*/
|
|
652
|
+
declare const ORDER_PROGRESS_STEPS: readonly ["Đã đặt", "Đã xác nhận", "Đang chuẩn bị", "Đang giao", "Đã giao"];
|
|
653
|
+
type OrderProgress = {
|
|
654
|
+
kind: "steps";
|
|
655
|
+
current_index: number;
|
|
656
|
+
} | {
|
|
657
|
+
kind: "terminal";
|
|
658
|
+
label: string;
|
|
659
|
+
tone: "cancelled" | "on_hold";
|
|
660
|
+
};
|
|
661
|
+
/**
|
|
662
|
+
* Rules are evaluated in order and the first match wins, so a cancelled order
|
|
663
|
+
* shows the terminal state no matter how far shipping had progressed.
|
|
664
|
+
*/
|
|
665
|
+
declare function deriveOrderProgress(order: Order): OrderProgress;
|
|
666
|
+
|
|
667
|
+
interface PromotionDescriptionData {
|
|
668
|
+
type: string;
|
|
669
|
+
discount_method?: string;
|
|
670
|
+
discount_value?: number;
|
|
671
|
+
description?: string;
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Generate promotion description based on promotion type and discount method
|
|
675
|
+
* @param promotion - Promotion data containing type, discount_method, discount_value, description
|
|
676
|
+
* @param locale - Locale for formatting (e.g., "vi-VN", "en-US")
|
|
677
|
+
* @param currency - Currency code (e.g., "VND", "USD")
|
|
678
|
+
* @returns Formatted promotion description string
|
|
679
|
+
*/
|
|
680
|
+
declare function getPromotionDescription(promotion: PromotionDescriptionData, locale?: string, currency?: string): string;
|
|
681
|
+
|
|
682
|
+
declare const sdk: {
|
|
683
|
+
products: _ecomweb_sdk.ProductsApi;
|
|
684
|
+
collections: _ecomweb_sdk.CollectionsApi;
|
|
685
|
+
categories: _ecomweb_sdk.CategoriesApi;
|
|
686
|
+
cart: _ecomweb_sdk.CartApi;
|
|
687
|
+
orders: _ecomweb_sdk.OrdersApi;
|
|
688
|
+
blog: _ecomweb_sdk.BlogApi;
|
|
689
|
+
stores: _ecomweb_sdk.StoresApi;
|
|
690
|
+
search: _ecomweb_sdk.SearchApi;
|
|
691
|
+
banners: _ecomweb_sdk.BannersApi;
|
|
692
|
+
shipping: _ecomweb_sdk.ShippingApi;
|
|
693
|
+
promotions: _ecomweb_sdk.PromotionsApi;
|
|
694
|
+
paymentMethods: _ecomweb_sdk.PaymentMethodsApi;
|
|
695
|
+
payments: _ecomweb_sdk.PaymentsApi;
|
|
696
|
+
reviews: _ecomweb_sdk.ReviewsApi;
|
|
697
|
+
customers: _ecomweb_sdk.CustomersApi;
|
|
698
|
+
addresses: _ecomweb_sdk.AddressesApi;
|
|
699
|
+
contentPages: _ecomweb_sdk.ContentPagesApi;
|
|
700
|
+
menus: _ecomweb_sdk.MenusApi;
|
|
701
|
+
redirects: _ecomweb_sdk.RedirectsApi;
|
|
702
|
+
};
|
|
703
|
+
/**
|
|
704
|
+
* SDK bound to one store, whose authenticated modules (cart, orders, blog
|
|
705
|
+
* comments, customers, addresses) go through the same-origin proxy route so the
|
|
706
|
+
* httpOnly session cookie can be turned into a bearer token server-side.
|
|
707
|
+
*
|
|
708
|
+
* Memoised per store so the returned object is referentially stable and safe to
|
|
709
|
+
* use in a React dependency array.
|
|
710
|
+
*/
|
|
711
|
+
declare function createStoreSdk(storeRef: string): {
|
|
712
|
+
products: _ecomweb_sdk.ProductsApi;
|
|
713
|
+
collections: _ecomweb_sdk.CollectionsApi;
|
|
714
|
+
categories: _ecomweb_sdk.CategoriesApi;
|
|
715
|
+
cart: _ecomweb_sdk.CartApi;
|
|
716
|
+
orders: _ecomweb_sdk.OrdersApi;
|
|
717
|
+
blog: _ecomweb_sdk.BlogApi;
|
|
718
|
+
stores: _ecomweb_sdk.StoresApi;
|
|
719
|
+
search: _ecomweb_sdk.SearchApi;
|
|
720
|
+
banners: _ecomweb_sdk.BannersApi;
|
|
721
|
+
shipping: _ecomweb_sdk.ShippingApi;
|
|
722
|
+
promotions: _ecomweb_sdk.PromotionsApi;
|
|
723
|
+
paymentMethods: _ecomweb_sdk.PaymentMethodsApi;
|
|
724
|
+
payments: _ecomweb_sdk.PaymentsApi;
|
|
725
|
+
reviews: _ecomweb_sdk.ReviewsApi;
|
|
726
|
+
customers: _ecomweb_sdk.CustomersApi;
|
|
727
|
+
addresses: _ecomweb_sdk.AddressesApi;
|
|
728
|
+
contentPages: _ecomweb_sdk.ContentPagesApi;
|
|
729
|
+
menus: _ecomweb_sdk.MenusApi;
|
|
730
|
+
redirects: _ecomweb_sdk.RedirectsApi;
|
|
731
|
+
};
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* Renders a shipping option's price for display: the free label when the
|
|
735
|
+
* rate is free, the house money format otherwise.
|
|
736
|
+
*
|
|
737
|
+
* `freeLabel` is passed in (see lib/shipping/messages.ts) rather than
|
|
738
|
+
* hardcoded here, so this stays a pure, generic function reachable from a
|
|
739
|
+
* plain vitest spec.
|
|
740
|
+
*/
|
|
741
|
+
declare function formatShippingAmount(amount: number, freeLabel: string, locale: string, currency: SupportedCurrency | string): string;
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* Location data is fetched in two steps rather than all at once: the 34
|
|
745
|
+
* provinces up front, then one province's wards when something actually
|
|
746
|
+
* needs them. Fetching everything cost 751 KB and every ward in the
|
|
747
|
+
* country on a page that shows at most one province's worth.
|
|
748
|
+
*
|
|
749
|
+
* Caches hold the in-flight promise, not just the result, so concurrent
|
|
750
|
+
* callers (a checkout page mounts several) share one request. A failure
|
|
751
|
+
* is evicted so it is never cached.
|
|
752
|
+
*/
|
|
753
|
+
type Fetcher = (storeRef: string, query: {
|
|
754
|
+
level?: number;
|
|
755
|
+
parent_code?: string;
|
|
756
|
+
}) => Promise<LocationAreasResponse>;
|
|
757
|
+
declare function loadWards(storeRef: string, provinceCode: string | undefined, fetcher?: Fetcher): Promise<LocationArea[]>;
|
|
758
|
+
declare function useProvinces(storeRef: string): {
|
|
759
|
+
provinces: LocationArea[];
|
|
760
|
+
loading: boolean;
|
|
761
|
+
};
|
|
762
|
+
declare function useWards(storeRef: string, provinceCode: string | undefined): {
|
|
763
|
+
wards: LocationArea[];
|
|
764
|
+
loading: boolean;
|
|
765
|
+
};
|
|
766
|
+
/**
|
|
767
|
+
* The string a combobox filters on. cmdk matches against a CommandItem's
|
|
768
|
+
* `value`, so putting the code there alone meant a shopper had to type
|
|
769
|
+
* "12" to find Hồ Chí Minh. This puts the names in, plus the slug with
|
|
770
|
+
* its hyphens spaced out so typing without accents ("ho chi minh") still
|
|
771
|
+
* matches. The code stays searchable for anyone who knows it.
|
|
772
|
+
*/
|
|
773
|
+
declare function searchTextFor(area: LocationArea): string;
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* Checkout shipping strings, extracted from the components that used to
|
|
777
|
+
* hardcode them inline. This is NOT an i18n layer -- the storefront has no
|
|
778
|
+
* i18n infrastructure (no next-intl, no locale routing, no message
|
|
779
|
+
* catalog), and bootstrapping one is out of scope for a shipping-vocabulary
|
|
780
|
+
* change. This just gets every shipping string into one place so a real
|
|
781
|
+
* i18n task can source from here later instead of hunting through JSX.
|
|
782
|
+
*
|
|
783
|
+
* `estimate` is a template: interpolate `{min}` and `{max}` yourself.
|
|
784
|
+
*/
|
|
785
|
+
declare const SHIPPING_MESSAGES: Readonly<{
|
|
786
|
+
title: "Phương thức vận chuyển";
|
|
787
|
+
prompt: "Vui lòng nhập địa chỉ giao hàng để xem phương thức vận chuyển";
|
|
788
|
+
empty: "Không có phương thức vận chuyển phù hợp";
|
|
789
|
+
emptyHint: "Vui lòng kiểm tra lại địa chỉ giao hàng hoặc liên hệ với cửa hàng.";
|
|
790
|
+
failed: "Không thể tính phí vận chuyển";
|
|
791
|
+
failedHint: "Kết nối có thể đang gián đoạn. Vui lòng thử lại.";
|
|
792
|
+
retry: "Thử lại";
|
|
793
|
+
free: "Miễn phí";
|
|
794
|
+
estimate: "Dự kiến giao: {min} đến {max} ngày";
|
|
795
|
+
}>;
|
|
796
|
+
/** Fills in the `{min}`/`{max}` placeholders in `SHIPPING_MESSAGES.estimate`. */
|
|
797
|
+
declare function formatShippingEstimate(min: number, max: number): string;
|
|
798
|
+
|
|
799
|
+
/**
|
|
800
|
+
* Find an area by code or name, for values stored before codes were used
|
|
801
|
+
* consistently. The old `id` match arm is gone with the field itself: it
|
|
802
|
+
* was the source JSON object's own key, never a stable identifier.
|
|
803
|
+
* @param identifier - Area code, name, or name_with_type
|
|
804
|
+
* @param provinces - Areas to search
|
|
805
|
+
* @returns The area if found, undefined otherwise
|
|
806
|
+
*/
|
|
807
|
+
declare function findProvinceByCodeOrName(identifier: string | undefined, provinces: LocationArea[]): LocationArea | undefined;
|
|
808
|
+
/**
|
|
809
|
+
* Find a ward by code or name, optionally restricted to one province.
|
|
810
|
+
* @param identifier - Area code, name, name_with_type, or a path fragment
|
|
811
|
+
* @param wards - Areas to search
|
|
812
|
+
* @param provinceCode - Optional province code to filter wards
|
|
813
|
+
* @returns The area if found, undefined otherwise
|
|
814
|
+
*/
|
|
815
|
+
declare function findWardByCodeOrName(identifier: string | undefined, wards: LocationArea[], provinceCode?: string): LocationArea | undefined;
|
|
816
|
+
|
|
817
|
+
type ResizeMode = "cover" | "contain" | "inside" | "outside";
|
|
818
|
+
type ImageFormat = "origin" | "avif" | "webp" | "png" | "jpeg";
|
|
819
|
+
interface ImageTransformOptions {
|
|
820
|
+
width?: number;
|
|
821
|
+
height?: number;
|
|
822
|
+
resize?: ResizeMode;
|
|
823
|
+
quality?: number;
|
|
824
|
+
format?: ImageFormat;
|
|
825
|
+
}
|
|
826
|
+
/**
|
|
827
|
+
* Resolve any image reference (full URL or bare storage object path) into a
|
|
828
|
+
* URL, applying transformation params when enabled.
|
|
829
|
+
*
|
|
830
|
+
* - A full http(s) URL that is not a storage URL is returned as-is.
|
|
831
|
+
* - A storage URL is rebuilt against the configured base, ignoring its host.
|
|
832
|
+
* - A bare object path is resolved within `defaultBucket`.
|
|
833
|
+
*/
|
|
834
|
+
declare function resolveTransformedImageUrl(imagePathOrUrl: string | null | undefined, opts?: ImageTransformOptions, defaultBucket?: "public-cdn"): string | null;
|
|
835
|
+
declare function resolveTransformedProductImageUrl(imagePathOrUrl: string | null | undefined, opts?: ImageTransformOptions): string | null;
|
|
836
|
+
declare function resolveTransformedStoreLogoUrl(imagePathOrUrl: string | null | undefined, opts?: ImageTransformOptions): string | null;
|
|
837
|
+
/**
|
|
838
|
+
* Null-safe wrapper for use directly in `src={...}`.
|
|
839
|
+
*
|
|
840
|
+
* Rebuilds against the current NEXT_PUBLIC_ASSET_BASE_URL regardless of the
|
|
841
|
+
* stored host, and passes non-storage URLs through untouched.
|
|
842
|
+
*/
|
|
843
|
+
declare function resolveAssetSrc(url: string | null | undefined, opts?: ImageTransformOptions): string;
|
|
844
|
+
|
|
845
|
+
declare function resolveStoreRef(params?: Promise<{
|
|
846
|
+
storeRef?: string;
|
|
847
|
+
}>): Promise<string>;
|
|
848
|
+
declare function storePath(storeRef: string, path?: string): string;
|
|
849
|
+
declare function storeApiPath(storeRef: string, subpath: string): string;
|
|
850
|
+
|
|
851
|
+
/**
|
|
852
|
+
* Build store URL based on environment and store configuration
|
|
853
|
+
* @param store - Store object with subdomain and custom_domain
|
|
854
|
+
* @param path - Optional path to append to the store URL
|
|
855
|
+
* @returns Complete store URL
|
|
856
|
+
*
|
|
857
|
+
* @example
|
|
858
|
+
* // Local development
|
|
859
|
+
* buildStoreUrl({ id: 1, subdomain: 'mystore', custom_domain: null })
|
|
860
|
+
* // Returns: 'http://localhost:3000/store/1'
|
|
861
|
+
*
|
|
862
|
+
* // Production with subdomain
|
|
863
|
+
* buildStoreUrl({ id: 1, subdomain: 'mystore', custom_domain: null })
|
|
864
|
+
* // Returns: 'https://mystore.{NEXT_PUBLIC_PLATFORM_DOMAIN_SUFFIX}'
|
|
865
|
+
*
|
|
866
|
+
* // Production with custom domain
|
|
867
|
+
* buildStoreUrl({ id: 1, subdomain: 'mystore', custom_domain: 'mystore.com' })
|
|
868
|
+
* // Returns: 'https://mystore.com'
|
|
869
|
+
*
|
|
870
|
+
* // With path
|
|
871
|
+
* buildStoreUrl({ id: 1, subdomain: 'mystore', custom_domain: null }, 'about')
|
|
872
|
+
* // Returns: 'https://mystore.{NEXT_PUBLIC_PLATFORM_DOMAIN_SUFFIX}/about'
|
|
873
|
+
*/
|
|
874
|
+
declare function buildStoreUrl(store: {
|
|
875
|
+
ref: string;
|
|
876
|
+
subdomain: string | null;
|
|
877
|
+
custom_domain: string | null;
|
|
878
|
+
} | StoreConfig | StoreConfigurationInfo, path?: string): string;
|
|
879
|
+
|
|
880
|
+
/**
|
|
881
|
+
* The store settings that survive the page-builder retirement, read from their
|
|
882
|
+
* new `settings.*` homes with a fallback to the legacy `theme_config` paths.
|
|
883
|
+
* The fallbacks go once the API migration (spec §6) has run everywhere.
|
|
884
|
+
*/
|
|
885
|
+
type Loose = Record<string, any>;
|
|
886
|
+
interface StoreLike {
|
|
887
|
+
settings?: Loose | null;
|
|
888
|
+
theme_config?: Loose | null;
|
|
889
|
+
}
|
|
890
|
+
interface BasicSettings {
|
|
891
|
+
brandColor: string | null;
|
|
892
|
+
backgroundColor: string | null;
|
|
893
|
+
navigation: {
|
|
894
|
+
mainMenuRef: string | null;
|
|
895
|
+
topbarMessage: string | null;
|
|
896
|
+
customerSupportMenuRef: string | null;
|
|
897
|
+
aboutUsMenuRef: string | null;
|
|
898
|
+
copyrightText: string | null;
|
|
899
|
+
};
|
|
900
|
+
legalLinks: {
|
|
901
|
+
termsLink: string | null;
|
|
902
|
+
policyLink: string | null;
|
|
903
|
+
};
|
|
904
|
+
homepageCollectionSlug: string | null;
|
|
905
|
+
}
|
|
906
|
+
declare function readBasicSettings(store: StoreLike): BasicSettings;
|
|
907
|
+
declare function resolveLegalHref(link: string | null, fallbackPath: string | null, toStoreUrl: (path: string) => string): string;
|
|
908
|
+
|
|
909
|
+
declare const getCachedStore: (storeRef: string, options?: {
|
|
910
|
+
includeConfig?: boolean;
|
|
911
|
+
includeSettings?: boolean;
|
|
912
|
+
includeBlogSettings?: boolean;
|
|
913
|
+
}) => Promise<StoreConfigurationInfo | null>;
|
|
914
|
+
|
|
915
|
+
/**
|
|
916
|
+
* The metadata Next.js shows for a store's suspended pages. Never includes
|
|
917
|
+
* `platform_suspension_reason` -- it's internal, written by an admin for
|
|
918
|
+
* other admins.
|
|
919
|
+
*/
|
|
920
|
+
declare const SUSPENDED_STORE_METADATA: Metadata;
|
|
921
|
+
/**
|
|
922
|
+
* Wraps a leaf page's `generateMetadata` body so a STORE_SUSPENDED lookup
|
|
923
|
+
* doesn't crash the whole route.
|
|
924
|
+
*
|
|
925
|
+
* Next.js resolves every matched segment's `generateMetadata` unconditionally
|
|
926
|
+
* to build <head> -- this happens independently of whatever the parent
|
|
927
|
+
* `StoreLayout` eventually renders (it can skip rendering `children` for a
|
|
928
|
+
* suspended store, but that doesn't stop Next from having already awaited
|
|
929
|
+
* this segment's own `generateMetadata`). So each page that calls
|
|
930
|
+
* `getCachedStore` in its own `generateMetadata` needs this, not just the
|
|
931
|
+
* layout's.
|
|
932
|
+
*/
|
|
933
|
+
declare function withSuspendedStoreMetadataFallback(fn: () => Promise<Metadata>): Promise<Metadata>;
|
|
934
|
+
|
|
935
|
+
/**
|
|
936
|
+
* The `<ref>` in `/store/<ref>/...`, or null anywhere else.
|
|
937
|
+
*
|
|
938
|
+
* Used only as a fallback for navigation callers that did not pass a storeRef
|
|
939
|
+
* explicitly. An explicit ref always wins, since a caller may legitimately
|
|
940
|
+
* link to a different store than the one currently being viewed.
|
|
941
|
+
*/
|
|
942
|
+
declare function refFromPathname(pathname: string): string | null;
|
|
943
|
+
|
|
944
|
+
/**
|
|
945
|
+
* True when `error` is the backend's signal that a PLATFORM admin suspended
|
|
946
|
+
* this store (`STORE_SUSPENDED`, 403) — distinct from the store simply not
|
|
947
|
+
* existing or not being active (`STORE_UNAVAILABLE`, 404), which the SDK's
|
|
948
|
+
* `unwrapOrNull` already collapses to a plain `null` return, no throw.
|
|
949
|
+
*
|
|
950
|
+
* Both `getCachedStore` (path-based `/store/<ref>` resolution) and
|
|
951
|
+
* `getStoreByHost` (custom-domain / subdomain resolution) call
|
|
952
|
+
* `sdk.stores.getById` / `getByDomain`, which throw an `ApiClientError` for
|
|
953
|
+
* any non-2xx, non-404 response — so this is the single place that decides
|
|
954
|
+
* "was that a suspension" for both entry points.
|
|
955
|
+
*/
|
|
956
|
+
declare function isStoreSuspendedError(error: unknown): boolean;
|
|
957
|
+
|
|
958
|
+
/**
|
|
959
|
+
* Lightweight logging helpers: log levels + console filtering.
|
|
960
|
+
* - Defaults to 'warn' in production and 'debug' in development
|
|
961
|
+
* - Honors `NEXT_PUBLIC_LOG_LEVEL` (client) or `LOG_LEVEL` (server)
|
|
962
|
+
*/
|
|
963
|
+
type LogLevel = "silent" | "error" | "warn" | "info" | "debug";
|
|
964
|
+
declare function getLogLevel(): LogLevel;
|
|
965
|
+
declare function shouldLog(currentLevel: LogLevel, messageLevel: LogLevel): boolean;
|
|
966
|
+
declare function applyConsoleFilter(level?: LogLevel): void;
|
|
967
|
+
|
|
968
|
+
export { type BankAccountView, DEFAULT_REDIRECT, type Notice, type NoticeHandler, type NoticeKind, ORDER_PROGRESS_STEPS, type OrderHandoff, type OrderSuccessInfo, type PaymentConfirmationState, type PlaceOrderInput, type PlaceOrderResult, type PlacedOrder, SHIPPING_MESSAGES, STATUS_OPTIONS, STOCK_ISSUE_TOAST_DESCRIPTION, SUSPENDED_STORE_METADATA, StorefrontKitProvider, type StorefrontKitProviderProps, type SupportedCurrency, type UseCheckoutOptions, applyConsoleFilter, buildStoreUrl, createStoreSdk, deriveOrderProgress, errorCopy, errorCopyFor, findProvinceByCodeOrName, findWardByCodeOrName, formatNumber, formatPrice, formatPriceRange, formatPriceWithoutSymbol, formatShippingAmount, formatShippingEstimate, getCachedStore, getDefaultLocale, getLogLevel, getPasswordPolicyHints, getPaymentState, getPromotionDescription, isStoreSuspendedError, loadWards, money, pollPaymentState, publicApiClient, readBasicSettings, readOrderHandoff, refFromPathname, resolveAssetSrc, resolveInitialState, resolveLegalHref, resolveStoreRef, resolveTransformedImageUrl, resolveTransformedProductImageUrl, resolveTransformedStoreLogoUrl, safeRedirectPath, sdk, searchTextFor, shouldLog, shouldPollForConfirmation, storeApiPath, storePath, unwrap, unwrapOrNull, useAuth, useCart, useCheckout, useNotify, useOptionalStoreId, useOrderLookup, useOrderStatus, useProvinces, usePublicAuthConfig, useStore, useStoreCurrency, useStoreData, useStoreId, useStoreLocale, useWards, validatePasswordAgainstPolicy, withSuspendedStoreMetadataFallback };
|