@nymbalhq/sdk 1.4.0 → 1.6.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.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +6 -0
  3. package/dist/chunk-2EEZZXZX.js +601 -0
  4. package/dist/chunk-2EEZZXZX.js.map +1 -0
  5. package/dist/chunk-4GP43MKT.js +19 -0
  6. package/dist/chunk-4GP43MKT.js.map +1 -0
  7. package/dist/chunk-7LQGOJIT.js +133 -0
  8. package/dist/chunk-7LQGOJIT.js.map +1 -0
  9. package/dist/chunk-CJVA46FR.js +52 -0
  10. package/dist/chunk-CJVA46FR.js.map +1 -0
  11. package/dist/chunk-PDZKR65O.js +148 -0
  12. package/dist/chunk-PDZKR65O.js.map +1 -0
  13. package/dist/chunk-U6QF2DRY.js +30 -0
  14. package/dist/chunk-U6QF2DRY.js.map +1 -0
  15. package/dist/chunk-VYX72KGY.js +116 -0
  16. package/dist/chunk-VYX72KGY.js.map +1 -0
  17. package/dist/chunk-XS7RNJSY.js +118 -0
  18. package/dist/chunk-XS7RNJSY.js.map +1 -0
  19. package/dist/chunk-YHXC7DS5.js +92 -0
  20. package/dist/chunk-YHXC7DS5.js.map +1 -0
  21. package/dist/core-BviXjOsX.d.ts +560 -0
  22. package/dist/core.d.ts +2 -0
  23. package/dist/core.js +21 -0
  24. package/dist/core.js.map +1 -0
  25. package/dist/index.d.ts +4 -483
  26. package/dist/index.js +33 -1048
  27. package/dist/index.js.map +1 -1
  28. package/dist/ns/auth.d.ts +2 -0
  29. package/dist/ns/auth.js +4 -0
  30. package/dist/ns/auth.js.map +1 -0
  31. package/dist/ns/cart.d.ts +2 -0
  32. package/dist/ns/cart.js +4 -0
  33. package/dist/ns/cart.js.map +1 -0
  34. package/dist/ns/checkout.d.ts +2 -0
  35. package/dist/ns/checkout.js +5 -0
  36. package/dist/ns/checkout.js.map +1 -0
  37. package/dist/ns/product-list.d.ts +2 -0
  38. package/dist/ns/product-list.js +4 -0
  39. package/dist/ns/product-list.js.map +1 -0
  40. package/dist/ns/product.d.ts +2 -0
  41. package/dist/ns/product.js +4 -0
  42. package/dist/ns/product.js.map +1 -0
  43. package/dist/ns/search.d.ts +2 -0
  44. package/dist/ns/search.js +4 -0
  45. package/dist/ns/search.js.map +1 -0
  46. package/package.json +17 -2
package/dist/index.d.ts CHANGED
@@ -1,486 +1,7 @@
1
- import { Customer, CartItem, Address, PaymentIntent, OrderStatus, CheckoutPayment, VariantOption, ProductSortKey, Facet, Order, StoreCapabilities, Cart, CustomerAddress, ShopperOrder, ReviewsResult, ReviewResult, AlertKind } from '@nymbalhq/types';
2
- export { AlertKind, PaymentClient, ProductSortKey, ReviewResult as ReviewSubmitResult, ReviewsResult as ReviewsListResult, StoreCapabilities } from '@nymbalhq/types';
3
-
4
- interface DenormalisedVariant {
5
- id: string;
6
- sku: string;
7
- name: string;
8
- priceMinor: number;
9
- stock: number;
10
- options: VariantOption[];
11
- }
12
- interface DenormalisedProduct {
13
- id: string;
14
- slug: string;
15
- name: string;
16
- description: string;
17
- shortDescription: string;
18
- status: string;
19
- type: string;
20
- media: unknown;
21
- variantCount: number;
22
- inStock: boolean;
23
- priceRange: {
24
- minMinor: number;
25
- maxMinor: number;
26
- } | null;
27
- currency: string | null;
28
- priceMinor: number | null;
29
- variants: DenormalisedVariant[];
30
- categoryIds: string[];
31
- categories: Array<{
32
- id: string;
33
- name: string;
34
- slug: string;
35
- }>;
36
- createdAt: string;
37
- updatedAt: string;
38
- }
39
- interface CartState {
40
- items: CartItem[];
41
- subtotalMinor: number;
42
- currency: string;
43
- itemCount: number;
44
- loading: boolean;
45
- error: string | null;
46
- }
47
- interface ProductState {
48
- product: DenormalisedProduct | null;
49
- selectedVariant: DenormalisedVariant | null;
50
- loading: boolean;
51
- error: string | null;
52
- }
53
- interface ProductListState {
54
- products: DenormalisedProduct[];
55
- facets: Facet[];
56
- filters: Record<string, string | string[]>;
57
- sort: ProductSortKey;
58
- pagination: {
59
- cursor: string | null;
60
- hasMore: boolean;
61
- };
62
- loading: boolean;
63
- error: string | null;
64
- }
65
- interface SearchState {
66
- query: string;
67
- /** The category slug the search is narrowed to, or null for every category. */
68
- category: string | null;
69
- /** Every page loaded so far, in rank order. */
70
- results: DenormalisedProduct[];
71
- /** The API's count of every match, not of the results loaded. */
72
- total: number;
73
- /** The next page's cursor; null when every match is loaded. */
74
- nextCursor: string | null;
75
- /** The search's facets: the category facet when search reports `facet` and the query matches. */
76
- facets: Facet[];
77
- loading: boolean;
78
- }
79
- type CheckoutStep = 'contact' | 'shipping' | 'payment' | 'confirmation';
80
- interface ShippingMethod {
81
- carrier: string;
82
- service: string;
83
- amountMinor: number;
84
- currency: string;
85
- estimatedDays: number;
86
- }
87
- interface CheckoutState {
88
- step: CheckoutStep;
89
- email: string;
90
- shippingAddress: Address | null;
91
- billingAddress: Address | null;
92
- shippingMethod: ShippingMethod | null;
93
- /** What the server said about the payment; `succeeded` only once the order is paid. */
94
- paymentStatus: 'idle' | 'processing' | 'requires_action' | 'succeeded' | 'failed';
95
- error: string | null;
96
- order: {
97
- orderId: string;
98
- orderNumber: string;
99
- paymentIntent: PaymentIntent;
100
- } | null;
101
- /** The order this cart already became, when the server refused a resubmit and named it. */
102
- placedOrderNumber: string | null;
103
- /** That order's key, when the refusal carried one. */
104
- placedOrderKey: string | null;
105
- }
106
- interface AuthState {
107
- customer: Customer | null;
108
- isAuthenticated: boolean;
109
- loading: boolean;
110
- error: string | null;
111
- }
112
- interface ProductListParams {
113
- limit?: number;
114
- cursor?: string;
115
- category?: string;
116
- sort?: ProductSortKey;
117
- }
118
- interface ProductListResult {
119
- items: DenormalisedProduct[];
120
- nextCursor: string | null;
121
- facets?: Facet[];
122
- }
123
- interface ProductSearchParams {
124
- limit?: number;
125
- cursor?: string;
126
- /** A category slug, as on the listing. */
127
- category?: string;
128
- }
129
- interface ProductSearchResult {
130
- items: DenormalisedProduct[];
131
- nextCursor: string | null;
132
- total: number;
133
- facets: Facet[];
134
- }
135
- interface CheckoutDetails {
136
- email: string;
137
- billingAddress: Address;
138
- shippingAddress: Address;
139
- notes?: string;
140
- }
141
- interface CheckoutResult {
142
- orderId: string;
143
- orderNumber: string;
144
- paymentIntent: PaymentIntent;
145
- currency: string;
146
- totalMinor: number;
147
- orderStatus: OrderStatus;
148
- payment: CheckoutPayment;
149
- /** Reads the order with no session (`customer.orders.get(n, { key })`); absent from an API that mints none. */
150
- orderKey?: string;
151
- }
152
- interface RegisterParams {
153
- email: string;
154
- password: string;
155
- firstName?: string;
156
- lastName?: string;
157
- phone?: string;
158
- }
159
- interface AuthResult {
160
- customer: Customer;
161
- accessToken: string;
162
- expiresIn: number;
163
- }
164
- type NewAddress = Address & {
165
- isDefaultBilling?: boolean;
166
- isDefaultShipping?: boolean;
167
- };
168
- interface OrderPage {
169
- items: Order[];
170
- nextCursor: string | null;
171
- }
172
-
173
- interface CommerceAdapter {
174
- /** What the store can do (GET /api/capabilities); an adapter without it cannot tell the storefront. */
175
- capabilities?: {
176
- get(): Promise<StoreCapabilities>;
177
- };
178
- /** The shopper's recently viewed products; an adapter without it shows none. */
179
- recentlyViewed?: {
180
- record(productId: string): Promise<void>;
181
- list(): Promise<{
182
- items: DenormalisedProduct[];
183
- }>;
184
- };
185
- cart: {
186
- get(): Promise<Cart>;
187
- addItem(variantId: string, qty: number): Promise<Cart>;
188
- removeItem(variantId: string): Promise<Cart>;
189
- updateQuantity(variantId: string, qty: number): Promise<Cart>;
190
- clear(): Promise<Cart>;
191
- };
192
- products: {
193
- getBySlug(slug: string): Promise<DenormalisedProduct>;
194
- list(params?: ProductListParams): Promise<ProductListResult>;
195
- search(query: string, params?: ProductSearchParams): Promise<ProductSearchResult>;
196
- };
197
- checkout: {
198
- create(details: CheckoutDetails): Promise<CheckoutResult>;
199
- };
200
- customers: {
201
- register(params: RegisterParams): Promise<AuthResult>;
202
- login(email: string, password: string): Promise<AuthResult>;
203
- logout(): Promise<void>;
204
- refresh(): Promise<{
205
- accessToken: string;
206
- expiresIn: number;
207
- }>;
208
- getProfile(): Promise<Customer>;
209
- updateProfile(updates: Partial<Pick<Customer, 'firstName' | 'lastName' | 'phone'>>): Promise<Customer>;
210
- listAddresses(): Promise<CustomerAddress[]>;
211
- addAddress(address: NewAddress): Promise<CustomerAddress>;
212
- removeAddress(id: string): Promise<void>;
213
- listOrders(params?: {
214
- cursor?: string;
215
- }): Promise<OrderPage>;
216
- /** `key` is the order's key from its checkout, for a reader with no session. */
217
- getOrder(orderNumber: string, options?: {
218
- key?: string;
219
- }): Promise<ShopperOrder>;
220
- };
221
- reviews: {
222
- getForProduct(productId: string, params?: {
223
- limit?: number;
224
- cursor?: string;
225
- }): Promise<ReviewsResult>;
226
- submit(review: {
227
- productId: string;
228
- rating: number;
229
- title: string;
230
- body: string;
231
- }): Promise<ReviewResult>;
232
- };
233
- alerts: {
234
- /**
235
- * Asks to be emailed when a variant is back; the store mails a confirmation first. The answer is `pending` whether
236
- * or not the address has an account or an alert already.
237
- */
238
- subscribe(request: {
239
- email: string;
240
- variantId: string;
241
- kind: AlertKind;
242
- }): Promise<{
243
- status: 'pending';
244
- }>;
245
- };
246
- }
247
-
248
- interface CartStore {
249
- getState(): CartState;
250
- subscribe(listener: () => void): () => void;
251
- addItem(variantId: string, qty?: number): Promise<void>;
252
- removeItem(variantId: string): Promise<void>;
253
- updateQuantity(variantId: string, qty: number): Promise<void>;
254
- clear(): Promise<void>;
255
- load(): Promise<void>;
256
- destroy(): void;
257
- }
258
- declare function createCartStore(adapter: CommerceAdapter): CartStore;
259
-
260
- interface ProductStore {
261
- getState(): ProductState;
262
- subscribe(listener: () => void): () => void;
263
- loadBySlug(slug: string): Promise<void>;
264
- selectVariant(variant: DenormalisedVariant | null): void;
265
- destroy(): void;
266
- }
267
- declare function createProductStore(adapter: CommerceAdapter): ProductStore;
268
-
269
- interface ProductListStore {
270
- getState(): ProductListState;
271
- subscribe(listener: () => void): () => void;
272
- load(params?: ProductListParams): Promise<void>;
273
- loadMore(): Promise<void>;
274
- applyFilter(name: string, value: string | string[]): Promise<void>;
275
- removeFilter(name: string): Promise<void>;
276
- setSort(sort: ProductSortKey): Promise<void>;
277
- destroy(): void;
278
- }
279
- declare function createProductListStore(adapter: CommerceAdapter): ProductListStore;
280
-
281
- interface SearchStore {
282
- getState(): SearchState;
283
- subscribe(listener: () => void): () => void;
284
- /** Page 1 of `query`, in `options.category` (a slug) when given, otherwise in every category. */
285
- search(query: string, options?: {
286
- category?: string | null;
287
- }): Promise<void>;
288
- /** Appends the next page; a no-op when every match is loaded or a page is loading. */
289
- loadMore(): Promise<void>;
290
- /** Page 1 of the current query in `category` (a slug), or in every category when null. */
291
- setCategory(category: string | null): Promise<void>;
292
- clearSearch(): void;
293
- destroy(): void;
294
- }
295
- declare function createSearchStore(adapter: CommerceAdapter): SearchStore;
296
-
297
- interface CheckoutStore {
298
- getState(): CheckoutState;
299
- subscribe(listener: () => void): () => void;
300
- setEmail(email: string): void;
301
- setShippingAddress(address: Address): void;
302
- setBillingAddress(address: Address): void;
303
- setShippingMethod(method: ShippingMethod): void;
304
- /** Places the order; resolves to the server's result, or null when there is none. */
305
- submitPayment(): Promise<CheckoutResult | null>;
306
- /** The provider confirmed the payment on the client (Stripe's `confirmCardPayment`). */
307
- markConfirmed(): void;
308
- reset(): void;
309
- destroy(): void;
310
- }
311
- /**
312
- * `cart`, when given, is loaded again once the server has answered a submit: placing an order changes the cart on the
313
- * server (emptied when confirmed, kept while awaiting payment), and whatever renders the cart store must show that.
314
- */
315
- declare function createCheckoutStore(adapter: CommerceAdapter, cart?: CartStore): CheckoutStore;
316
-
317
- interface TokenManagerOptions {
318
- storage?: 'memory' | 'localStorage';
319
- storageKey?: string;
320
- }
321
- interface TokenManager {
322
- getAccessToken(): string | null;
323
- setTokens(accessToken: string, expiresIn: number): void;
324
- clear(): void;
325
- scheduleRefresh(refreshFn: () => Promise<{
326
- accessToken: string;
327
- expiresIn: number;
328
- }>): void;
329
- destroy(): void;
330
- }
331
- declare function createTokenManager(options?: TokenManagerOptions): TokenManager;
332
-
333
- interface AuthStore {
334
- getState(): AuthState;
335
- subscribe(listener: () => void): () => void;
336
- login(email: string, password: string): Promise<void>;
337
- register(params: RegisterParams): Promise<void>;
338
- logout(): Promise<void>;
339
- refreshToken(): Promise<void>;
340
- updateProfile(updates: Partial<Pick<Customer, 'firstName' | 'lastName' | 'phone'>>): Promise<void>;
341
- loadProfile(): Promise<void>;
342
- destroy(): void;
343
- }
344
- declare function createAuthStore(adapter: CommerceAdapter, tokenManager: TokenManager): AuthStore;
345
-
346
- interface NymbalClientOptions {
347
- baseUrl: string;
348
- adapter?: CommerceAdapter;
349
- tokenStorage?: 'memory' | 'localStorage';
350
- credentials?: RequestCredentials;
351
- getCartToken?: () => string | null;
352
- }
353
- /** The signed-in customer's own account; every call sends the session's bearer token. */
354
- interface CustomerAccount {
355
- addresses: {
356
- list(): Promise<CustomerAddress[]>;
357
- create(address: NewAddress): Promise<CustomerAddress>;
358
- remove(id: string): Promise<void>;
359
- };
360
- orders: {
361
- list(opts?: {
362
- cursor?: string;
363
- }): Promise<OrderPage>;
364
- /** `key`, the order's key from its checkout, reads it with no session; without one, only its owner can. */
365
- get(orderNumber: string, options?: {
366
- key?: string;
367
- }): Promise<ShopperOrder>;
368
- };
369
- }
370
- /** The store's capabilities report, read once per client: what it can do does not change while it runs. */
371
- interface StoreCapabilitiesReader {
372
- get(): Promise<StoreCapabilities>;
373
- }
374
- /** The shopper's recently viewed products, kept by the store under the signed-in customer or the browser's visitor. */
375
- interface RecentlyViewed {
376
- /** Records a view of the product; the store ignores a product that is not active. */
377
- record(productId: string): Promise<void>;
378
- /** The shopper's active products, newest first. */
379
- list(): Promise<{
380
- items: DenormalisedProduct[];
381
- }>;
382
- }
383
- interface NymbalClient {
384
- capabilities: StoreCapabilitiesReader;
385
- recentlyViewed: RecentlyViewed;
386
- cart: CartStore;
387
- product: ProductStore;
388
- productList: ProductListStore;
389
- search: SearchStore;
390
- checkout: CheckoutStore;
391
- auth: AuthStore;
392
- customer: CustomerAccount;
393
- adapter: CommerceAdapter;
394
- destroy(): void;
395
- }
396
- declare function createNymbalClient(options: NymbalClientOptions): NymbalClient;
397
-
398
- /** What a checkout already knows about the shopper, which a driver may hand its processor with the payment. */
399
- interface PaymentConfirmContext {
400
- email?: string;
401
- billing?: Address;
402
- }
403
- /** What a payment driver's `confirm` answers: paid now, accepted without a payment yet, or refused with a reason. */
404
- type PaymentConfirmation = {
405
- ok: true;
406
- confirmed: boolean;
407
- } | {
408
- ok: false;
409
- error: string;
410
- };
411
- /**
412
- * The browser half of a payments adapter, named by the `driver` of GET /api/capabilities' payments client. `mount`
413
- * renders whatever the shopper pays with inside `el`; `confirm` completes a payment the API left `requires_action`,
414
- * with the shopper's email and billing address when the checkout has them.
415
- */
416
- interface PaymentDriver {
417
- mount(el: HTMLElement, options: Record<string, string>): Promise<void> | void;
418
- confirm(clientSecret: string, context?: PaymentConfirmContext): Promise<PaymentConfirmation>;
419
- }
420
- /** Registers `driver` under `name`, replacing any driver of that name. */
421
- declare function registerPaymentDriver(name: string, driver: PaymentDriver): void;
422
- declare function getPaymentDriver(name: string): PaymentDriver | undefined;
423
-
424
- type Listener = () => void;
425
- interface Store<TState> {
426
- getState(): TState;
427
- setState(partial: Partial<TState> | ((prev: TState) => Partial<TState>)): void;
428
- subscribe(listener: Listener): () => void;
429
- destroy(): void;
430
- }
431
- declare function createStore<TState extends object>(initialState: TState): Store<TState>;
432
-
433
- declare class NymbalApiError extends Error {
434
- readonly code: string;
435
- readonly status: number;
436
- readonly context?: Record<string, unknown> | undefined;
437
- constructor(code: string, message: string, status: number, context?: Record<string, unknown> | undefined);
438
- }
439
- interface FetchClientOptions {
440
- baseUrl: string;
441
- credentials?: RequestCredentials;
442
- getAccessToken?: () => string | null;
443
- getCartToken?: () => string | null;
444
- onUnauthorized?: () => Promise<void>;
445
- }
446
- /** Per request: headers sent beside the client's own. */
447
- interface RequestOptions {
448
- headers?: Record<string, string>;
449
- /**
450
- * The token refresh itself (what `onUnauthorized` sends through this client): its 401 is thrown, never waited on or
451
- * sent again, so a refused refresh cannot wait on itself. An unmarked one that is refused is cut off after
452
- * 10 s with the original 401.
453
- */
454
- authRefresh?: boolean;
455
- }
456
- declare class FetchClient {
457
- private readonly baseUrl;
458
- private readonly credentials;
459
- private readonly getAccessToken;
460
- private readonly getCartToken;
461
- private readonly onUnauthorized;
462
- /** The refresh a refused request started, which every request refused meanwhile waits on rather than its own. */
463
- private refreshing;
464
- /** The token `GET /api/csrf` answered: the API's `nymbal.csrf` cookie, which this origin may not be able to read. */
465
- private csrfToken;
466
- constructor(options: FetchClientOptions);
467
- request<T>(method: string, path: string, body?: unknown, options?: RequestOptions): Promise<T>;
468
- /** Like `request`, keeping the envelope's `meta` (pagination cursors live there). */
469
- requestWithMeta<T>(method: string, path: string, body?: unknown, options?: RequestOptions): Promise<{
470
- data: T;
471
- meta: Record<string, unknown>;
472
- }>;
473
- /** Whether the refresh ended within `REFRESH_WAIT_MS`; a refresh that fails throws here, as it always has. */
474
- private waitFor;
475
- private refresh;
476
- private csrf;
477
- private fetchCsrfToken;
478
- private isCsrfRejection;
479
- private doFetch;
480
- private parseResponse;
481
- }
1
+ import { F as FetchClient, C as CommerceAdapter } from './core-BviXjOsX.js';
2
+ export { A as AuthResult, a as AuthState, b as AuthStore, c as CartState, d as CartStore, e as CheckoutDetails, f as CheckoutResult, g as CheckoutState, h as CheckoutStep, i as CheckoutStore, j as CustomerAccount, D as DenormalisedProduct, k as DenormalisedVariant, l as FetchClientOptions, G as GuestOrders, L as Listener, N as NewAddress, m as NymbalApiError, n as NymbalClient, o as NymbalClientOptions, O as OrderPage, P as PaymentConfirmContext, p as PaymentConfirmation, q as PaymentDriver, r as ProductListParams, s as ProductListResult, t as ProductListState, u as ProductListStore, v as ProductSearchParams, w as ProductSearchResult, x as ProductState, y as ProductStore, R as RecentlyViewed, z as RegisterParams, S as SESSION_HINT_KEY, B as SearchState, E as SearchStore, H as ShippingMethod, I as Store, J as StoreCapabilitiesReader, T as TokenManager, K as TokenManagerOptions, M as createAuthStore, Q as createCartStore, U as createCheckoutStore, V as createNymbalClient, W as createProductListStore, X as createProductStore, Y as createSearchStore, Z as createStore, _ as createTokenManager, $ as getPaymentDriver, a0 as registerPaymentDriver } from './core-BviXjOsX.js';
3
+ export { AlertKind, PaymentClient, ProductRating, ProductSortKey, PublicReview, ReviewResult as ReviewSubmitResult, PublicReviewsResult as ReviewsListResult, StoreCapabilities } from '@nymbalhq/types';
482
4
 
483
5
  declare function createNymbalApiAdapter(fetchClient: FetchClient): CommerceAdapter;
484
6
 
485
- export { FetchClient, NymbalApiError, createAuthStore, createCartStore, createCheckoutStore, createNymbalApiAdapter, createNymbalClient, createProductListStore, createProductStore, createSearchStore, createStore, createTokenManager, getPaymentDriver, registerPaymentDriver };
486
- export type { AuthResult, AuthState, AuthStore, CartState, CartStore, CheckoutDetails, CheckoutResult, CheckoutState, CheckoutStep, CheckoutStore, CommerceAdapter, CustomerAccount, DenormalisedProduct, DenormalisedVariant, FetchClientOptions, Listener, NewAddress, NymbalClient, NymbalClientOptions, OrderPage, PaymentConfirmContext, PaymentConfirmation, PaymentDriver, ProductListParams, ProductListResult, ProductListState, ProductListStore, ProductSearchParams, ProductSearchResult, ProductState, ProductStore, RecentlyViewed, RegisterParams, SearchState, SearchStore, ShippingMethod, Store, StoreCapabilitiesReader, TokenManager, TokenManagerOptions };
7
+ export { CommerceAdapter, FetchClient, createNymbalApiAdapter };