@asteby/storefront 0.0.0-stage → 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 +201 -0
- package/README.md +164 -2
- package/dist/index.cjs +326 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +261 -0
- package/dist/index.d.ts +261 -0
- package/dist/index.js +314 -0
- package/dist/index.js.map +1 -0
- package/dist/mock.cjs +998 -0
- package/dist/mock.cjs.map +1 -0
- package/dist/mock.d.cts +95 -0
- package/dist/mock.d.ts +95 -0
- package/dist/mock.js +993 -0
- package/dist/mock.js.map +1 -0
- package/dist/types-CTXB8Dnl.d.cts +465 -0
- package/dist/types-CTXB8Dnl.d.ts +465 -0
- package/package.json +59 -3
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { V as ProductSort, a1 as Shop, T as ProductConnection, Q as Product, q as Connection, p as Collection, a5 as StockLevel, i as CartLineInput, C as Cart, k as CartLineUpdateInput, d as CartBuyerInput, a as AddressInput, w as DeliveryOptions, L as PaymentMethods, D as DeliveryInput, m as CheckoutInput, o as CheckoutResult, s as CustomerIdentifier, u as CustomerSessionInput, t as CustomerSession, r as Customer, O as Order, Y as ReturnRequestInput, X as ReturnRequest, y as InvoiceRequest, z as InvoiceResult, A as Address, a8 as Theme, P as Page, M as Menu, b as Article, a7 as StorefrontErrorCode, E as Money } from './types-CTXB8Dnl.cjs';
|
|
2
|
+
export { c as CartBuyer, e as CartCost, f as CartDelivery, g as CartDiscountCode, h as CartLine, j as CartLineMerchandise, l as CheckoutInfo, n as CheckoutPayment, v as DeliveryMethod, F as Facet, x as FacetValue, I as Image, B as MenuItem, G as MoneyRange, H as OrderLine, J as PageInfo, K as PaymentMethod, N as PickupOption, R as ProductAttributeValue, S as ProductCategory, U as ProductOption, W as ProductVariant, Z as SEO, _ as Section, $ as SelectedOption, a0 as ShippingRate, a2 as ShopBranch, a3 as ShopContact, a4 as ShopPolicies, a6 as StorefrontErrorBody, a9 as UnavailableRateProvider } from './types-CTXB8Dnl.cjs';
|
|
3
|
+
|
|
4
|
+
type QueryValue = string | number | boolean | null | undefined;
|
|
5
|
+
type QueryParams = Record<string, QueryValue | QueryValue[]>;
|
|
6
|
+
type PaginationParams = {
|
|
7
|
+
/** Page size (1–100; articles accept at most 50). */
|
|
8
|
+
first?: number;
|
|
9
|
+
/** Cursor from `page_info.end_cursor`. */
|
|
10
|
+
after?: string | null;
|
|
11
|
+
};
|
|
12
|
+
type ProductListParams = PaginationParams & {
|
|
13
|
+
/** Free-text search. */
|
|
14
|
+
q?: string;
|
|
15
|
+
/** Collection handle. */
|
|
16
|
+
collection?: string;
|
|
17
|
+
sort?: ProductSort;
|
|
18
|
+
/** Only products available for sale. */
|
|
19
|
+
available?: boolean;
|
|
20
|
+
price_min?: number | string;
|
|
21
|
+
price_max?: number | string;
|
|
22
|
+
/**
|
|
23
|
+
* Attribute filters, serialized as repeated `filter.<key>=<value>` params.
|
|
24
|
+
* Values of one key are OR-ed; different keys are AND-ed.
|
|
25
|
+
* e.g. `{ vendor: ['A', 'B'], rim: 16 }`.
|
|
26
|
+
*/
|
|
27
|
+
filters?: Record<string, QueryValue | QueryValue[]>;
|
|
28
|
+
/** Restrict to these handles (max 100). Serialized comma-separated. */
|
|
29
|
+
handles?: string[];
|
|
30
|
+
};
|
|
31
|
+
type CollectionListParams = PaginationParams;
|
|
32
|
+
type ArticleListParams = Omit<PaginationParams, 'first'> & {
|
|
33
|
+
/** Page size (1–50). The API answers 400 `validation` above 50. */
|
|
34
|
+
first?: number;
|
|
35
|
+
tag?: string;
|
|
36
|
+
q?: string;
|
|
37
|
+
};
|
|
38
|
+
/** Appends params to a URLSearchParams, repeating keys for arrays and skipping nullish values. */
|
|
39
|
+
declare function appendParams(search: URLSearchParams, params: QueryParams | undefined): URLSearchParams;
|
|
40
|
+
/** Serializes product list params per the contract (`filter.<attr>` repeated, `handles` comma-separated). */
|
|
41
|
+
declare function productListQuery(params?: ProductListParams): URLSearchParams;
|
|
42
|
+
/** Reads `filter.<key>` params back into a filters record (useful in route loaders). */
|
|
43
|
+
declare function parseProductFilters(search: URLSearchParams): Record<string, string[]>;
|
|
44
|
+
|
|
45
|
+
type FetchLike = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
46
|
+
/** Context passed to `onRequest` / `onResponse` hooks. */
|
|
47
|
+
type StorefrontRequestContext = {
|
|
48
|
+
method: string;
|
|
49
|
+
/** Absolute URL, including the query string. */
|
|
50
|
+
url: string;
|
|
51
|
+
/** Route path relative to `baseUrl`, e.g. `/storefront/products/abc`. */
|
|
52
|
+
path: string;
|
|
53
|
+
/** Final `RequestInit` (headers already merged). Hooks may mutate it. */
|
|
54
|
+
init: RequestInit & {
|
|
55
|
+
headers: Headers;
|
|
56
|
+
};
|
|
57
|
+
/** Per-call options as given by the caller. */
|
|
58
|
+
options: RequestOptions;
|
|
59
|
+
};
|
|
60
|
+
type StorefrontResponseContext = StorefrontRequestContext & {
|
|
61
|
+
response: Response;
|
|
62
|
+
/** Milliseconds spent waiting for the response. */
|
|
63
|
+
durationMs: number;
|
|
64
|
+
};
|
|
65
|
+
type StorefrontClientOptions = {
|
|
66
|
+
/** Base URL of the gateway, e.g. `https://ops.example.com/api/gw`. Routes are appended as `/storefront/...`. */
|
|
67
|
+
baseUrl: string;
|
|
68
|
+
/** `pk_...` (browser) or `sk_...` (server only). Sent as `Authorization: Bearer <token>`. */
|
|
69
|
+
token: string;
|
|
70
|
+
/** Buyer session (`X-Customer-Token`) for `/storefront/customer/*` routes. */
|
|
71
|
+
customerToken?: string | null;
|
|
72
|
+
/** Custom fetch (defaults to `globalThis.fetch`). */
|
|
73
|
+
fetch?: FetchLike;
|
|
74
|
+
/** Sent as `Accept-Language`. */
|
|
75
|
+
locale?: string;
|
|
76
|
+
/** Extra headers on every request. */
|
|
77
|
+
headers?: Record<string, string>;
|
|
78
|
+
/** Abort requests that take longer than this (default: no timeout). */
|
|
79
|
+
timeoutMs?: number;
|
|
80
|
+
/**
|
|
81
|
+
* Runs before every request. Return a `Response` to short-circuit the
|
|
82
|
+
* network (e.g. an SSR cache hit); return nothing to continue.
|
|
83
|
+
*/
|
|
84
|
+
onRequest?: (ctx: StorefrontRequestContext) => void | Response | Promise<void | Response>;
|
|
85
|
+
/** Runs after every response (including short-circuited ones), before parsing. */
|
|
86
|
+
onResponse?: (ctx: StorefrontResponseContext) => void | Promise<void>;
|
|
87
|
+
};
|
|
88
|
+
/** Per-call options. Unknown keys (e.g. Next.js `next`) are forwarded to `fetch` as-is. */
|
|
89
|
+
type RequestOptions = {
|
|
90
|
+
cache?: RequestCache;
|
|
91
|
+
signal?: AbortSignal;
|
|
92
|
+
/** Framework fetch extensions, forwarded verbatim (e.g. `{ revalidate: 60, tags: ['products'] }`). */
|
|
93
|
+
next?: Record<string, unknown>;
|
|
94
|
+
headers?: Record<string, string>;
|
|
95
|
+
/** Overrides the client-level timeout for this call. */
|
|
96
|
+
timeoutMs?: number;
|
|
97
|
+
/** Overrides the client-level customer token for this call. */
|
|
98
|
+
customerToken?: string | null;
|
|
99
|
+
[key: string]: unknown;
|
|
100
|
+
};
|
|
101
|
+
type WriteOptions = RequestOptions & {
|
|
102
|
+
/** `Idempotency-Key` header. Auto-generated for checkout when omitted. */
|
|
103
|
+
idempotencyKey?: string;
|
|
104
|
+
};
|
|
105
|
+
type PreviewOptions = RequestOptions & {
|
|
106
|
+
/** Signed preview token: returns the draft (`private, no-store`). */
|
|
107
|
+
preview?: string;
|
|
108
|
+
};
|
|
109
|
+
type HttpMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
|
|
110
|
+
type RequestSpec = {
|
|
111
|
+
method: HttpMethod;
|
|
112
|
+
path: string;
|
|
113
|
+
query?: URLSearchParams | QueryParams;
|
|
114
|
+
body?: unknown;
|
|
115
|
+
options?: WriteOptions;
|
|
116
|
+
/** Resolve `null` instead of throwing on 404. */
|
|
117
|
+
nullOn404?: boolean;
|
|
118
|
+
};
|
|
119
|
+
/** Generates an idempotency key (crypto.randomUUID when available). */
|
|
120
|
+
declare function generateIdempotencyKey(): string;
|
|
121
|
+
declare function createStorefrontClient(config: StorefrontClientOptions): {
|
|
122
|
+
/** Resolved configuration (read-only). */
|
|
123
|
+
config: Readonly<StorefrontClientOptions>;
|
|
124
|
+
/** Low-level escape hatch: any route under `baseUrl`. */
|
|
125
|
+
request: {
|
|
126
|
+
<T>(spec: RequestSpec & {
|
|
127
|
+
nullOn404: true;
|
|
128
|
+
}): Promise<T | null>;
|
|
129
|
+
<T>(spec: RequestSpec): Promise<T>;
|
|
130
|
+
};
|
|
131
|
+
/** Returns a new client that sends the given buyer session token. */
|
|
132
|
+
withCustomerToken(customerToken: string | null): /*elided*/ any;
|
|
133
|
+
shop: {
|
|
134
|
+
get: (options?: RequestOptions) => Promise<Shop>;
|
|
135
|
+
};
|
|
136
|
+
products: {
|
|
137
|
+
list: (params?: ProductListParams, options?: RequestOptions) => Promise<ProductConnection>;
|
|
138
|
+
/** `null` when the product does not exist or is unpublished. */
|
|
139
|
+
get: (handle: string, options?: RequestOptions) => Promise<Product | null>;
|
|
140
|
+
recommendations: (handle: string, options?: RequestOptions) => Promise<{
|
|
141
|
+
nodes: Product[];
|
|
142
|
+
}>;
|
|
143
|
+
};
|
|
144
|
+
collections: {
|
|
145
|
+
list: (params?: CollectionListParams, options?: RequestOptions) => Promise<Connection<Collection>>;
|
|
146
|
+
/** `null` when the collection does not exist. */
|
|
147
|
+
get: (handle: string, options?: RequestOptions) => Promise<Collection | null>;
|
|
148
|
+
};
|
|
149
|
+
stock: {
|
|
150
|
+
/** Stock for up to 250 SKUs. */
|
|
151
|
+
get: (skus: string[], options?: RequestOptions) => Promise<{
|
|
152
|
+
nodes: StockLevel[];
|
|
153
|
+
}>;
|
|
154
|
+
};
|
|
155
|
+
carts: {
|
|
156
|
+
create: (input?: {
|
|
157
|
+
lines?: CartLineInput[];
|
|
158
|
+
}, options?: WriteOptions) => Promise<Cart>;
|
|
159
|
+
/** `null` when the cart does not exist (e.g. expired cookie). */
|
|
160
|
+
get: (id: string, options?: RequestOptions) => Promise<Cart | null>;
|
|
161
|
+
addLines: (id: string, lines: CartLineInput[], options?: WriteOptions) => Promise<Cart>;
|
|
162
|
+
/** Quantity `0` removes the line. */
|
|
163
|
+
updateLines: (id: string, lines: CartLineUpdateInput[], options?: WriteOptions) => Promise<Cart>;
|
|
164
|
+
removeLines: (id: string, lineIds: string[], options?: WriteOptions) => Promise<Cart>;
|
|
165
|
+
setDiscountCodes: (id: string, codes: string[], options?: WriteOptions) => Promise<Cart>;
|
|
166
|
+
setBuyer: (id: string, buyer: CartBuyerInput, options?: WriteOptions) => Promise<Cart>;
|
|
167
|
+
setShippingAddress: (id: string, address: AddressInput, options?: WriteOptions) => Promise<Cart>;
|
|
168
|
+
deliveryOptions: (id: string, options?: RequestOptions) => Promise<DeliveryOptions>;
|
|
169
|
+
/** Payment providers active for the cart's market; send the chosen `key` as `payment_method`. */
|
|
170
|
+
paymentMethods: (id: string, options?: RequestOptions) => Promise<PaymentMethods>;
|
|
171
|
+
setDelivery: (id: string, delivery: DeliveryInput, options?: WriteOptions) => Promise<Cart>;
|
|
172
|
+
/** Reserves stock and creates the order. Sends `Idempotency-Key` (auto-generated if omitted). */
|
|
173
|
+
checkout: (id: string, input: CheckoutInput, options?: WriteOptions) => Promise<CheckoutResult>;
|
|
174
|
+
};
|
|
175
|
+
customer: {
|
|
176
|
+
/** Sends a one-time code by email or SMS/WhatsApp. */
|
|
177
|
+
requestOtp: (identifier: CustomerIdentifier, options?: WriteOptions) => Promise<void>;
|
|
178
|
+
/** Exchanges the code for a buyer session (`token` goes in `customerToken`). */
|
|
179
|
+
createSession: (input: CustomerSessionInput, options?: WriteOptions) => Promise<CustomerSession>;
|
|
180
|
+
get: (options?: RequestOptions) => Promise<Customer>;
|
|
181
|
+
orders: (params?: PaginationParams, options?: RequestOptions) => Promise<Connection<Order>>;
|
|
182
|
+
/** `null` when the order does not exist or belongs to someone else. */
|
|
183
|
+
order: (id: string, options?: RequestOptions) => Promise<Order | null>;
|
|
184
|
+
requestReturn: (orderId: string, input: ReturnRequestInput, options?: WriteOptions) => Promise<ReturnRequest>;
|
|
185
|
+
requestInvoice: (orderId: string, input: InvoiceRequest, options?: WriteOptions) => Promise<InvoiceResult>;
|
|
186
|
+
addresses: {
|
|
187
|
+
list: (options?: RequestOptions) => Promise<{
|
|
188
|
+
nodes: Address[];
|
|
189
|
+
}>;
|
|
190
|
+
create: (address: AddressInput, options?: WriteOptions) => Promise<Address>;
|
|
191
|
+
update: (id: string, address: Partial<AddressInput>, options?: WriteOptions) => Promise<Address>;
|
|
192
|
+
delete: (id: string, options?: WriteOptions) => Promise<void>;
|
|
193
|
+
};
|
|
194
|
+
};
|
|
195
|
+
content: {
|
|
196
|
+
theme: (options?: RequestOptions) => Promise<Theme>;
|
|
197
|
+
/** `null` when the page does not exist. */
|
|
198
|
+
page: (handle: string, options?: PreviewOptions) => Promise<Page | null>;
|
|
199
|
+
/** `null` when the menu does not exist. */
|
|
200
|
+
menu: (handle: string, options?: RequestOptions) => Promise<Menu | null>;
|
|
201
|
+
articles: (params?: ArticleListParams, options?: RequestOptions) => Promise<Connection<Article>>;
|
|
202
|
+
/** `null` when the article does not exist. */
|
|
203
|
+
article: (handle: string, options?: PreviewOptions) => Promise<Article | null>;
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
type StorefrontClient = ReturnType<typeof createStorefrontClient>;
|
|
207
|
+
|
|
208
|
+
type StorefrontErrorInit = {
|
|
209
|
+
status: number;
|
|
210
|
+
code: StorefrontErrorCode;
|
|
211
|
+
message: string;
|
|
212
|
+
field?: string;
|
|
213
|
+
/** Request URL that failed (without credentials). */
|
|
214
|
+
url?: string;
|
|
215
|
+
cause?: unknown;
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* Error thrown by the client for any non-2xx response, network failure,
|
|
219
|
+
* timeout or abort. `status` is `0` when no HTTP response was received.
|
|
220
|
+
*/
|
|
221
|
+
declare class StorefrontError extends Error {
|
|
222
|
+
readonly name = "StorefrontError";
|
|
223
|
+
readonly status: number;
|
|
224
|
+
readonly code: StorefrontErrorCode;
|
|
225
|
+
readonly field: string | undefined;
|
|
226
|
+
readonly url: string | undefined;
|
|
227
|
+
constructor(init: StorefrontErrorInit);
|
|
228
|
+
toJSON(): {
|
|
229
|
+
name: string;
|
|
230
|
+
status: number;
|
|
231
|
+
code: StorefrontErrorCode;
|
|
232
|
+
message: string;
|
|
233
|
+
field: string | undefined;
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
declare function isStorefrontError(err: unknown): err is StorefrontError;
|
|
237
|
+
|
|
238
|
+
type FormatMoneyOptions = {
|
|
239
|
+
/** Drop `.00` when the amount is a whole number. */
|
|
240
|
+
withoutTrailingZeros?: boolean;
|
|
241
|
+
/** Format as a plain number, without the currency symbol. */
|
|
242
|
+
withoutCurrency?: boolean;
|
|
243
|
+
/** `symbol` (default), `narrowSymbol`, `code` or `name`. */
|
|
244
|
+
currencyDisplay?: 'symbol' | 'narrowSymbol' | 'code' | 'name';
|
|
245
|
+
};
|
|
246
|
+
/** Parses a contract `Money` amount into a JS number (`"1899.00"` → `1899`). */
|
|
247
|
+
declare function parseMoney(money: Money | string | null | undefined): number;
|
|
248
|
+
/**
|
|
249
|
+
* Formats `Money` with `Intl.NumberFormat`. Works in every runtime (no DOM).
|
|
250
|
+
* Falls back to `"<amount> <code>"` when the runtime lacks the currency.
|
|
251
|
+
*/
|
|
252
|
+
declare function formatMoney(money: Money, locale?: string, options?: FormatMoneyOptions): string;
|
|
253
|
+
/** Builds a `Money` from a number, rounding to cents. */
|
|
254
|
+
declare function toMoney(value: number, currencyCode: string): Money;
|
|
255
|
+
/**
|
|
256
|
+
* True for public (`pk_`) tokens, the only ones that may reach a browser.
|
|
257
|
+
* Private (`sk_`) tokens must stay on the server.
|
|
258
|
+
*/
|
|
259
|
+
declare function isTokenPublishable(token: string | null | undefined): boolean;
|
|
260
|
+
|
|
261
|
+
export { Address, AddressInput, Article, type ArticleListParams, Cart, CartBuyerInput, CartLineInput, CartLineUpdateInput, CheckoutInput, CheckoutResult, Collection, type CollectionListParams, Connection, Customer, CustomerIdentifier, CustomerSession, CustomerSessionInput, DeliveryInput, DeliveryOptions, type FetchLike, type FormatMoneyOptions, InvoiceRequest, InvoiceResult, Menu, Money, Order, Page, type PaginationParams, PaymentMethods, type PreviewOptions, Product, ProductConnection, type ProductListParams, ProductSort, type QueryParams, type QueryValue, type RequestOptions, ReturnRequest, ReturnRequestInput, Shop, StockLevel, type StorefrontClient, type StorefrontClientOptions, StorefrontError, StorefrontErrorCode, type StorefrontErrorInit, type StorefrontRequestContext, type StorefrontResponseContext, Theme, type WriteOptions, appendParams, createStorefrontClient, formatMoney, generateIdempotencyKey, isStorefrontError, isTokenPublishable, parseMoney, parseProductFilters, productListQuery, toMoney };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { V as ProductSort, a1 as Shop, T as ProductConnection, Q as Product, q as Connection, p as Collection, a5 as StockLevel, i as CartLineInput, C as Cart, k as CartLineUpdateInput, d as CartBuyerInput, a as AddressInput, w as DeliveryOptions, L as PaymentMethods, D as DeliveryInput, m as CheckoutInput, o as CheckoutResult, s as CustomerIdentifier, u as CustomerSessionInput, t as CustomerSession, r as Customer, O as Order, Y as ReturnRequestInput, X as ReturnRequest, y as InvoiceRequest, z as InvoiceResult, A as Address, a8 as Theme, P as Page, M as Menu, b as Article, a7 as StorefrontErrorCode, E as Money } from './types-CTXB8Dnl.js';
|
|
2
|
+
export { c as CartBuyer, e as CartCost, f as CartDelivery, g as CartDiscountCode, h as CartLine, j as CartLineMerchandise, l as CheckoutInfo, n as CheckoutPayment, v as DeliveryMethod, F as Facet, x as FacetValue, I as Image, B as MenuItem, G as MoneyRange, H as OrderLine, J as PageInfo, K as PaymentMethod, N as PickupOption, R as ProductAttributeValue, S as ProductCategory, U as ProductOption, W as ProductVariant, Z as SEO, _ as Section, $ as SelectedOption, a0 as ShippingRate, a2 as ShopBranch, a3 as ShopContact, a4 as ShopPolicies, a6 as StorefrontErrorBody, a9 as UnavailableRateProvider } from './types-CTXB8Dnl.js';
|
|
3
|
+
|
|
4
|
+
type QueryValue = string | number | boolean | null | undefined;
|
|
5
|
+
type QueryParams = Record<string, QueryValue | QueryValue[]>;
|
|
6
|
+
type PaginationParams = {
|
|
7
|
+
/** Page size (1–100; articles accept at most 50). */
|
|
8
|
+
first?: number;
|
|
9
|
+
/** Cursor from `page_info.end_cursor`. */
|
|
10
|
+
after?: string | null;
|
|
11
|
+
};
|
|
12
|
+
type ProductListParams = PaginationParams & {
|
|
13
|
+
/** Free-text search. */
|
|
14
|
+
q?: string;
|
|
15
|
+
/** Collection handle. */
|
|
16
|
+
collection?: string;
|
|
17
|
+
sort?: ProductSort;
|
|
18
|
+
/** Only products available for sale. */
|
|
19
|
+
available?: boolean;
|
|
20
|
+
price_min?: number | string;
|
|
21
|
+
price_max?: number | string;
|
|
22
|
+
/**
|
|
23
|
+
* Attribute filters, serialized as repeated `filter.<key>=<value>` params.
|
|
24
|
+
* Values of one key are OR-ed; different keys are AND-ed.
|
|
25
|
+
* e.g. `{ vendor: ['A', 'B'], rim: 16 }`.
|
|
26
|
+
*/
|
|
27
|
+
filters?: Record<string, QueryValue | QueryValue[]>;
|
|
28
|
+
/** Restrict to these handles (max 100). Serialized comma-separated. */
|
|
29
|
+
handles?: string[];
|
|
30
|
+
};
|
|
31
|
+
type CollectionListParams = PaginationParams;
|
|
32
|
+
type ArticleListParams = Omit<PaginationParams, 'first'> & {
|
|
33
|
+
/** Page size (1–50). The API answers 400 `validation` above 50. */
|
|
34
|
+
first?: number;
|
|
35
|
+
tag?: string;
|
|
36
|
+
q?: string;
|
|
37
|
+
};
|
|
38
|
+
/** Appends params to a URLSearchParams, repeating keys for arrays and skipping nullish values. */
|
|
39
|
+
declare function appendParams(search: URLSearchParams, params: QueryParams | undefined): URLSearchParams;
|
|
40
|
+
/** Serializes product list params per the contract (`filter.<attr>` repeated, `handles` comma-separated). */
|
|
41
|
+
declare function productListQuery(params?: ProductListParams): URLSearchParams;
|
|
42
|
+
/** Reads `filter.<key>` params back into a filters record (useful in route loaders). */
|
|
43
|
+
declare function parseProductFilters(search: URLSearchParams): Record<string, string[]>;
|
|
44
|
+
|
|
45
|
+
type FetchLike = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
46
|
+
/** Context passed to `onRequest` / `onResponse` hooks. */
|
|
47
|
+
type StorefrontRequestContext = {
|
|
48
|
+
method: string;
|
|
49
|
+
/** Absolute URL, including the query string. */
|
|
50
|
+
url: string;
|
|
51
|
+
/** Route path relative to `baseUrl`, e.g. `/storefront/products/abc`. */
|
|
52
|
+
path: string;
|
|
53
|
+
/** Final `RequestInit` (headers already merged). Hooks may mutate it. */
|
|
54
|
+
init: RequestInit & {
|
|
55
|
+
headers: Headers;
|
|
56
|
+
};
|
|
57
|
+
/** Per-call options as given by the caller. */
|
|
58
|
+
options: RequestOptions;
|
|
59
|
+
};
|
|
60
|
+
type StorefrontResponseContext = StorefrontRequestContext & {
|
|
61
|
+
response: Response;
|
|
62
|
+
/** Milliseconds spent waiting for the response. */
|
|
63
|
+
durationMs: number;
|
|
64
|
+
};
|
|
65
|
+
type StorefrontClientOptions = {
|
|
66
|
+
/** Base URL of the gateway, e.g. `https://ops.example.com/api/gw`. Routes are appended as `/storefront/...`. */
|
|
67
|
+
baseUrl: string;
|
|
68
|
+
/** `pk_...` (browser) or `sk_...` (server only). Sent as `Authorization: Bearer <token>`. */
|
|
69
|
+
token: string;
|
|
70
|
+
/** Buyer session (`X-Customer-Token`) for `/storefront/customer/*` routes. */
|
|
71
|
+
customerToken?: string | null;
|
|
72
|
+
/** Custom fetch (defaults to `globalThis.fetch`). */
|
|
73
|
+
fetch?: FetchLike;
|
|
74
|
+
/** Sent as `Accept-Language`. */
|
|
75
|
+
locale?: string;
|
|
76
|
+
/** Extra headers on every request. */
|
|
77
|
+
headers?: Record<string, string>;
|
|
78
|
+
/** Abort requests that take longer than this (default: no timeout). */
|
|
79
|
+
timeoutMs?: number;
|
|
80
|
+
/**
|
|
81
|
+
* Runs before every request. Return a `Response` to short-circuit the
|
|
82
|
+
* network (e.g. an SSR cache hit); return nothing to continue.
|
|
83
|
+
*/
|
|
84
|
+
onRequest?: (ctx: StorefrontRequestContext) => void | Response | Promise<void | Response>;
|
|
85
|
+
/** Runs after every response (including short-circuited ones), before parsing. */
|
|
86
|
+
onResponse?: (ctx: StorefrontResponseContext) => void | Promise<void>;
|
|
87
|
+
};
|
|
88
|
+
/** Per-call options. Unknown keys (e.g. Next.js `next`) are forwarded to `fetch` as-is. */
|
|
89
|
+
type RequestOptions = {
|
|
90
|
+
cache?: RequestCache;
|
|
91
|
+
signal?: AbortSignal;
|
|
92
|
+
/** Framework fetch extensions, forwarded verbatim (e.g. `{ revalidate: 60, tags: ['products'] }`). */
|
|
93
|
+
next?: Record<string, unknown>;
|
|
94
|
+
headers?: Record<string, string>;
|
|
95
|
+
/** Overrides the client-level timeout for this call. */
|
|
96
|
+
timeoutMs?: number;
|
|
97
|
+
/** Overrides the client-level customer token for this call. */
|
|
98
|
+
customerToken?: string | null;
|
|
99
|
+
[key: string]: unknown;
|
|
100
|
+
};
|
|
101
|
+
type WriteOptions = RequestOptions & {
|
|
102
|
+
/** `Idempotency-Key` header. Auto-generated for checkout when omitted. */
|
|
103
|
+
idempotencyKey?: string;
|
|
104
|
+
};
|
|
105
|
+
type PreviewOptions = RequestOptions & {
|
|
106
|
+
/** Signed preview token: returns the draft (`private, no-store`). */
|
|
107
|
+
preview?: string;
|
|
108
|
+
};
|
|
109
|
+
type HttpMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
|
|
110
|
+
type RequestSpec = {
|
|
111
|
+
method: HttpMethod;
|
|
112
|
+
path: string;
|
|
113
|
+
query?: URLSearchParams | QueryParams;
|
|
114
|
+
body?: unknown;
|
|
115
|
+
options?: WriteOptions;
|
|
116
|
+
/** Resolve `null` instead of throwing on 404. */
|
|
117
|
+
nullOn404?: boolean;
|
|
118
|
+
};
|
|
119
|
+
/** Generates an idempotency key (crypto.randomUUID when available). */
|
|
120
|
+
declare function generateIdempotencyKey(): string;
|
|
121
|
+
declare function createStorefrontClient(config: StorefrontClientOptions): {
|
|
122
|
+
/** Resolved configuration (read-only). */
|
|
123
|
+
config: Readonly<StorefrontClientOptions>;
|
|
124
|
+
/** Low-level escape hatch: any route under `baseUrl`. */
|
|
125
|
+
request: {
|
|
126
|
+
<T>(spec: RequestSpec & {
|
|
127
|
+
nullOn404: true;
|
|
128
|
+
}): Promise<T | null>;
|
|
129
|
+
<T>(spec: RequestSpec): Promise<T>;
|
|
130
|
+
};
|
|
131
|
+
/** Returns a new client that sends the given buyer session token. */
|
|
132
|
+
withCustomerToken(customerToken: string | null): /*elided*/ any;
|
|
133
|
+
shop: {
|
|
134
|
+
get: (options?: RequestOptions) => Promise<Shop>;
|
|
135
|
+
};
|
|
136
|
+
products: {
|
|
137
|
+
list: (params?: ProductListParams, options?: RequestOptions) => Promise<ProductConnection>;
|
|
138
|
+
/** `null` when the product does not exist or is unpublished. */
|
|
139
|
+
get: (handle: string, options?: RequestOptions) => Promise<Product | null>;
|
|
140
|
+
recommendations: (handle: string, options?: RequestOptions) => Promise<{
|
|
141
|
+
nodes: Product[];
|
|
142
|
+
}>;
|
|
143
|
+
};
|
|
144
|
+
collections: {
|
|
145
|
+
list: (params?: CollectionListParams, options?: RequestOptions) => Promise<Connection<Collection>>;
|
|
146
|
+
/** `null` when the collection does not exist. */
|
|
147
|
+
get: (handle: string, options?: RequestOptions) => Promise<Collection | null>;
|
|
148
|
+
};
|
|
149
|
+
stock: {
|
|
150
|
+
/** Stock for up to 250 SKUs. */
|
|
151
|
+
get: (skus: string[], options?: RequestOptions) => Promise<{
|
|
152
|
+
nodes: StockLevel[];
|
|
153
|
+
}>;
|
|
154
|
+
};
|
|
155
|
+
carts: {
|
|
156
|
+
create: (input?: {
|
|
157
|
+
lines?: CartLineInput[];
|
|
158
|
+
}, options?: WriteOptions) => Promise<Cart>;
|
|
159
|
+
/** `null` when the cart does not exist (e.g. expired cookie). */
|
|
160
|
+
get: (id: string, options?: RequestOptions) => Promise<Cart | null>;
|
|
161
|
+
addLines: (id: string, lines: CartLineInput[], options?: WriteOptions) => Promise<Cart>;
|
|
162
|
+
/** Quantity `0` removes the line. */
|
|
163
|
+
updateLines: (id: string, lines: CartLineUpdateInput[], options?: WriteOptions) => Promise<Cart>;
|
|
164
|
+
removeLines: (id: string, lineIds: string[], options?: WriteOptions) => Promise<Cart>;
|
|
165
|
+
setDiscountCodes: (id: string, codes: string[], options?: WriteOptions) => Promise<Cart>;
|
|
166
|
+
setBuyer: (id: string, buyer: CartBuyerInput, options?: WriteOptions) => Promise<Cart>;
|
|
167
|
+
setShippingAddress: (id: string, address: AddressInput, options?: WriteOptions) => Promise<Cart>;
|
|
168
|
+
deliveryOptions: (id: string, options?: RequestOptions) => Promise<DeliveryOptions>;
|
|
169
|
+
/** Payment providers active for the cart's market; send the chosen `key` as `payment_method`. */
|
|
170
|
+
paymentMethods: (id: string, options?: RequestOptions) => Promise<PaymentMethods>;
|
|
171
|
+
setDelivery: (id: string, delivery: DeliveryInput, options?: WriteOptions) => Promise<Cart>;
|
|
172
|
+
/** Reserves stock and creates the order. Sends `Idempotency-Key` (auto-generated if omitted). */
|
|
173
|
+
checkout: (id: string, input: CheckoutInput, options?: WriteOptions) => Promise<CheckoutResult>;
|
|
174
|
+
};
|
|
175
|
+
customer: {
|
|
176
|
+
/** Sends a one-time code by email or SMS/WhatsApp. */
|
|
177
|
+
requestOtp: (identifier: CustomerIdentifier, options?: WriteOptions) => Promise<void>;
|
|
178
|
+
/** Exchanges the code for a buyer session (`token` goes in `customerToken`). */
|
|
179
|
+
createSession: (input: CustomerSessionInput, options?: WriteOptions) => Promise<CustomerSession>;
|
|
180
|
+
get: (options?: RequestOptions) => Promise<Customer>;
|
|
181
|
+
orders: (params?: PaginationParams, options?: RequestOptions) => Promise<Connection<Order>>;
|
|
182
|
+
/** `null` when the order does not exist or belongs to someone else. */
|
|
183
|
+
order: (id: string, options?: RequestOptions) => Promise<Order | null>;
|
|
184
|
+
requestReturn: (orderId: string, input: ReturnRequestInput, options?: WriteOptions) => Promise<ReturnRequest>;
|
|
185
|
+
requestInvoice: (orderId: string, input: InvoiceRequest, options?: WriteOptions) => Promise<InvoiceResult>;
|
|
186
|
+
addresses: {
|
|
187
|
+
list: (options?: RequestOptions) => Promise<{
|
|
188
|
+
nodes: Address[];
|
|
189
|
+
}>;
|
|
190
|
+
create: (address: AddressInput, options?: WriteOptions) => Promise<Address>;
|
|
191
|
+
update: (id: string, address: Partial<AddressInput>, options?: WriteOptions) => Promise<Address>;
|
|
192
|
+
delete: (id: string, options?: WriteOptions) => Promise<void>;
|
|
193
|
+
};
|
|
194
|
+
};
|
|
195
|
+
content: {
|
|
196
|
+
theme: (options?: RequestOptions) => Promise<Theme>;
|
|
197
|
+
/** `null` when the page does not exist. */
|
|
198
|
+
page: (handle: string, options?: PreviewOptions) => Promise<Page | null>;
|
|
199
|
+
/** `null` when the menu does not exist. */
|
|
200
|
+
menu: (handle: string, options?: RequestOptions) => Promise<Menu | null>;
|
|
201
|
+
articles: (params?: ArticleListParams, options?: RequestOptions) => Promise<Connection<Article>>;
|
|
202
|
+
/** `null` when the article does not exist. */
|
|
203
|
+
article: (handle: string, options?: PreviewOptions) => Promise<Article | null>;
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
type StorefrontClient = ReturnType<typeof createStorefrontClient>;
|
|
207
|
+
|
|
208
|
+
type StorefrontErrorInit = {
|
|
209
|
+
status: number;
|
|
210
|
+
code: StorefrontErrorCode;
|
|
211
|
+
message: string;
|
|
212
|
+
field?: string;
|
|
213
|
+
/** Request URL that failed (without credentials). */
|
|
214
|
+
url?: string;
|
|
215
|
+
cause?: unknown;
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* Error thrown by the client for any non-2xx response, network failure,
|
|
219
|
+
* timeout or abort. `status` is `0` when no HTTP response was received.
|
|
220
|
+
*/
|
|
221
|
+
declare class StorefrontError extends Error {
|
|
222
|
+
readonly name = "StorefrontError";
|
|
223
|
+
readonly status: number;
|
|
224
|
+
readonly code: StorefrontErrorCode;
|
|
225
|
+
readonly field: string | undefined;
|
|
226
|
+
readonly url: string | undefined;
|
|
227
|
+
constructor(init: StorefrontErrorInit);
|
|
228
|
+
toJSON(): {
|
|
229
|
+
name: string;
|
|
230
|
+
status: number;
|
|
231
|
+
code: StorefrontErrorCode;
|
|
232
|
+
message: string;
|
|
233
|
+
field: string | undefined;
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
declare function isStorefrontError(err: unknown): err is StorefrontError;
|
|
237
|
+
|
|
238
|
+
type FormatMoneyOptions = {
|
|
239
|
+
/** Drop `.00` when the amount is a whole number. */
|
|
240
|
+
withoutTrailingZeros?: boolean;
|
|
241
|
+
/** Format as a plain number, without the currency symbol. */
|
|
242
|
+
withoutCurrency?: boolean;
|
|
243
|
+
/** `symbol` (default), `narrowSymbol`, `code` or `name`. */
|
|
244
|
+
currencyDisplay?: 'symbol' | 'narrowSymbol' | 'code' | 'name';
|
|
245
|
+
};
|
|
246
|
+
/** Parses a contract `Money` amount into a JS number (`"1899.00"` → `1899`). */
|
|
247
|
+
declare function parseMoney(money: Money | string | null | undefined): number;
|
|
248
|
+
/**
|
|
249
|
+
* Formats `Money` with `Intl.NumberFormat`. Works in every runtime (no DOM).
|
|
250
|
+
* Falls back to `"<amount> <code>"` when the runtime lacks the currency.
|
|
251
|
+
*/
|
|
252
|
+
declare function formatMoney(money: Money, locale?: string, options?: FormatMoneyOptions): string;
|
|
253
|
+
/** Builds a `Money` from a number, rounding to cents. */
|
|
254
|
+
declare function toMoney(value: number, currencyCode: string): Money;
|
|
255
|
+
/**
|
|
256
|
+
* True for public (`pk_`) tokens, the only ones that may reach a browser.
|
|
257
|
+
* Private (`sk_`) tokens must stay on the server.
|
|
258
|
+
*/
|
|
259
|
+
declare function isTokenPublishable(token: string | null | undefined): boolean;
|
|
260
|
+
|
|
261
|
+
export { Address, AddressInput, Article, type ArticleListParams, Cart, CartBuyerInput, CartLineInput, CartLineUpdateInput, CheckoutInput, CheckoutResult, Collection, type CollectionListParams, Connection, Customer, CustomerIdentifier, CustomerSession, CustomerSessionInput, DeliveryInput, DeliveryOptions, type FetchLike, type FormatMoneyOptions, InvoiceRequest, InvoiceResult, Menu, Money, Order, Page, type PaginationParams, PaymentMethods, type PreviewOptions, Product, ProductConnection, type ProductListParams, ProductSort, type QueryParams, type QueryValue, type RequestOptions, ReturnRequest, ReturnRequestInput, Shop, StockLevel, type StorefrontClient, type StorefrontClientOptions, StorefrontError, StorefrontErrorCode, type StorefrontErrorInit, type StorefrontRequestContext, type StorefrontResponseContext, Theme, type WriteOptions, appendParams, createStorefrontClient, formatMoney, generateIdempotencyKey, isStorefrontError, isTokenPublishable, parseMoney, parseProductFilters, productListQuery, toMoney };
|