@thorprovider/medusa-extended 1.0.0 → 1.1.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.
@@ -0,0 +1,1078 @@
1
+ /**
2
+ * @fileoverview Dropshipper Client — Thor Commerce Extended
3
+ * @module @thorprovider/medusa-extended/dropshipper
4
+ *
5
+ * HTTP client for all `/admin/thor/dropshipper/` endpoints.
6
+ *
7
+ * Used by both:
8
+ * - X (ThorProvider admin) — authenticated via admin JWT, role "Admin"
9
+ * - Y (Dropshipper panel) — authenticated via dropshipper JWT, role "Dropshipper"
10
+ *
11
+ * Access control is enforced server-side via `withDropshipperScope` middleware.
12
+ * This client always sends `Authorization: Bearer <token>`.
13
+ *
14
+ * Endpoint reference: `dropshipping-api-contract.md`
15
+ */
16
+
17
+ import type {
18
+ // Onboarding
19
+ OnboardDropshipperBody,
20
+ OnboardDropshipperResponse,
21
+ // Variant Costs
22
+ GetVariantCostsOptions,
23
+ GetVariantCostsResponse,
24
+ CreateVariantCostBody,
25
+ CreateVariantCostResponse,
26
+ BatchVariantCostsBody,
27
+ BatchVariantCostsResponse,
28
+ DeleteVariantCostResponse,
29
+ // Products & Prices
30
+ GetDropshipperProductsOptions,
31
+ GetDropshipperProductsResponse,
32
+ UpdateDropshipperPricesBody,
33
+ UpdateDropshipperPricesResponse,
34
+ // Orders
35
+ GetDropshipperOrdersOptions,
36
+ GetDropshipperOrdersResponse,
37
+ GetDropshipperOrderDetailResponse,
38
+ CreateDropshipperOrderBody,
39
+ CreateDropshipperOrderResponse,
40
+ SetPaymentCollectorBody,
41
+ SetPaymentCollectorResponse,
42
+ // Order Cancel & Edits
43
+ CancelDropshipperOrderResponse,
44
+ CreateOrderEditBody,
45
+ CreateOrderEditResponse,
46
+ AddOrderEditItemBody,
47
+ AddOrderEditItemResponse,
48
+ ConfirmOrderEditResponse,
49
+ // Order Notes
50
+ OrderNote,
51
+ CreateOrderNoteBody,
52
+ GetOrderNotesResponse,
53
+ DeleteOrderNoteResponse,
54
+ // Dropshipper Addresses
55
+ DropshipperAddressBody,
56
+ DropshipperAddressResponse,
57
+ GetDropshipperAddressesResponse,
58
+ // Categories
59
+ GetDropshipperCategoriesOptions,
60
+ GetDropshipperCategoriesResponse,
61
+ CreateDropshipperCategoryBody,
62
+ UpdateDropshipperCategoryBody,
63
+ DeleteDropshipperCategoryResponse,
64
+ CreateCategoryMappingBody,
65
+ CreateCategoryMappingResponse,
66
+ DeleteCategoryMappingResponse,
67
+ // Customers
68
+ GetDropshipperCustomersOptions,
69
+ GetDropshipperCustomersResponse,
70
+ GetDropshipperCustomerDetailResponse,
71
+ CreateDropshipperCustomerBody,
72
+ CreateDropshipperCustomerResponse,
73
+ // Account & Settlements (Y)
74
+ GetDropshipperAccountResponse,
75
+ GetDropshipperPayableResponse,
76
+ GetDropshipperReceivableResponse,
77
+ GetSettlementsOptions,
78
+ GetSettlementsResponse,
79
+ GetSettlementDetailResponse,
80
+ // Admin Settlements (X)
81
+ CreateSettlementBody,
82
+ CreateSettlementResponse,
83
+ ConfirmSettlementBody,
84
+ CancelSettlementBody,
85
+ UpdateSettlementStatusResponse,
86
+ // Admin Account Management (X)
87
+ GetAdminDropshipperAccountsResponse,
88
+ GetAdminAccountBalanceResponse,
89
+ GetAdminPriceListsResponse,
90
+ GetUserChannelResponse,
91
+ // Promotions
92
+ GetDropshipperPromotionsOptions,
93
+ GetDropshipperPromotionsResponse,
94
+ CreateDropshipperPromotionBody,
95
+ UpdateDropshipperPromotionBody,
96
+ DeleteDropshipperPromotionResponse,
97
+ // Dashboard
98
+ GetDashboardStatsOptions,
99
+ GetDashboardStatsResponse,
100
+ // Storefront (V)
101
+ GetStorefrontDropshipperCategoriesResponse,
102
+ // Inline return types
103
+ DropshipperCategory,
104
+ DropshipperPromotion,
105
+ } from '@thorprovider/types';
106
+ import { createLogger, ProviderAPIError } from '@thorprovider/adapters';
107
+
108
+ // ---------------------------------------------------------------------------
109
+ // Types
110
+ // ---------------------------------------------------------------------------
111
+
112
+ type FetchMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
113
+
114
+ /**
115
+ * Configuration required to instantiate the dropshipper client.
116
+ */
117
+ export interface DropshipperClientConfig {
118
+ /** Medusa backend base URL */
119
+ baseUrl: string;
120
+ /**
121
+ * JWT token obtained from `POST /auth/user/emailpass`.
122
+ * Used as `Authorization: Bearer <token>`.
123
+ */
124
+ token: string;
125
+ /** Enable debug logging */
126
+ debug?: boolean;
127
+ }
128
+
129
+ // ---------------------------------------------------------------------------
130
+ // DropshipperClient
131
+ // ---------------------------------------------------------------------------
132
+
133
+ /**
134
+ * HTTP client for Thor Commerce dropshipping endpoints (`/admin/thor/dropshipper/`).
135
+ *
136
+ * All methods throw `ProviderAPIError` on non-2xx responses.
137
+ *
138
+ * @example
139
+ * ```typescript
140
+ * const client = new DropshipperClient({
141
+ * baseUrl: process.env.MEDUSA_BACKEND_URL!,
142
+ * token: jwtFromLogin,
143
+ * });
144
+ *
145
+ * // Y: list own products
146
+ * const { products } = await client.getProducts();
147
+ *
148
+ * // X: onboard a new dropshipper
149
+ * const { dropshipper } = await client.onboardDropshipper({ ... });
150
+ * ```
151
+ */
152
+ export class DropshipperClient {
153
+ private baseUrl: string;
154
+ private token: string;
155
+ private logger: ReturnType<typeof createLogger>;
156
+
157
+ constructor(config: DropshipperClientConfig) {
158
+ this.baseUrl = config.baseUrl.replace(/\/$/, '');
159
+ this.token = config.token;
160
+ this.logger = createLogger('DropshipperClient', config.debug);
161
+ }
162
+
163
+ // -------------------------------------------------------------------------
164
+ // Private — HTTP helper
165
+ // -------------------------------------------------------------------------
166
+
167
+ private async request<T>(
168
+ method: FetchMethod,
169
+ path: string,
170
+ body?: unknown,
171
+ ): Promise<T> {
172
+ const url = `${this.baseUrl}${path}`;
173
+ this.logger.debug(`[dropshipper] ${method} ${path}`);
174
+
175
+ const headers: Record<string, string> = {
176
+ Authorization: `Bearer ${this.token}`,
177
+ 'Content-Type': 'application/json',
178
+ };
179
+
180
+ const init: RequestInit = {
181
+ method,
182
+ headers,
183
+ ...(body !== undefined ? { body: JSON.stringify(body) } : {}),
184
+ };
185
+
186
+ let response: Response;
187
+ try {
188
+ response = await fetch(url, init);
189
+ } catch (networkError: any) {
190
+ throw new ProviderAPIError(
191
+ `[dropshipper] Network error on ${method} ${path}: ${networkError.message}`,
192
+ 'NETWORK_ERROR',
193
+ networkError,
194
+ );
195
+ }
196
+
197
+ if (!response.ok) {
198
+ let message = `HTTP ${response.status}`;
199
+ try {
200
+ const json = await response.json();
201
+ message = json?.message ?? message;
202
+ } catch {
203
+ // ignore parse errors
204
+ }
205
+ throw new ProviderAPIError(
206
+ `[dropshipper] ${method} ${path} failed: ${message}`,
207
+ `DROPSHIPPER_API_${response.status}`,
208
+ response.status,
209
+ );
210
+ }
211
+
212
+ return response.json() as Promise<T>;
213
+ }
214
+
215
+ // -------------------------------------------------------------------------
216
+ // Onboarding (Solo X)
217
+ // -------------------------------------------------------------------------
218
+
219
+ /**
220
+ * Create a new dropshipper account (sales channel + user + price list).
221
+ *
222
+ * `POST /admin/thor/dropshipper/onboard`
223
+ */
224
+ async onboardDropshipper(
225
+ body: OnboardDropshipperBody,
226
+ ): Promise<OnboardDropshipperResponse> {
227
+ return this.request<OnboardDropshipperResponse>(
228
+ 'POST',
229
+ '/admin/thor/dropshipper/onboard',
230
+ body,
231
+ );
232
+ }
233
+
234
+ // -------------------------------------------------------------------------
235
+ // Variant Costs (Solo X)
236
+ // -------------------------------------------------------------------------
237
+
238
+ /**
239
+ * List variant cost records, optionally filtered by account/variant/currency.
240
+ *
241
+ * `GET /admin/thor/dropshipper/variant-costs`
242
+ */
243
+ async getVariantCosts(
244
+ options: GetVariantCostsOptions = {},
245
+ ): Promise<GetVariantCostsResponse> {
246
+ const { account_id, variant_id, currency_code, limit = 20, offset = 0 } = options;
247
+ const qs = new URLSearchParams({
248
+ limit: String(limit),
249
+ offset: String(offset),
250
+ ...(account_id ? { account_id } : {}),
251
+ ...(variant_id ? { variant_id } : {}),
252
+ ...(currency_code ? { currency_code } : {}),
253
+ });
254
+ return this.request<GetVariantCostsResponse>(
255
+ 'GET',
256
+ `/admin/thor/dropshipper/variant-costs?${qs}`,
257
+ );
258
+ }
259
+
260
+ /**
261
+ * Upsert a single variant cost record.
262
+ *
263
+ * `POST /admin/thor/dropshipper/variant-costs`
264
+ */
265
+ async createVariantCost(
266
+ body: CreateVariantCostBody,
267
+ ): Promise<CreateVariantCostResponse> {
268
+ return this.request<CreateVariantCostResponse>(
269
+ 'POST',
270
+ '/admin/thor/dropshipper/variant-costs',
271
+ body,
272
+ );
273
+ }
274
+
275
+ /**
276
+ * Batch-upsert variant costs for a given account and currency.
277
+ *
278
+ * `POST /admin/thor/dropshipper/variant-costs/batch`
279
+ */
280
+ async batchVariantCosts(
281
+ body: BatchVariantCostsBody,
282
+ ): Promise<BatchVariantCostsResponse> {
283
+ return this.request<BatchVariantCostsResponse>(
284
+ 'POST',
285
+ '/admin/thor/dropshipper/variant-costs/batch',
286
+ body,
287
+ );
288
+ }
289
+
290
+ /**
291
+ * Delete a variant cost record by ID.
292
+ *
293
+ * `DELETE /admin/thor/dropshipper/variant-costs/:id`
294
+ */
295
+ async deleteVariantCost(id: string): Promise<DeleteVariantCostResponse> {
296
+ return this.request<DeleteVariantCostResponse>(
297
+ 'DELETE',
298
+ `/admin/thor/dropshipper/variant-costs/${id}`,
299
+ );
300
+ }
301
+
302
+ // -------------------------------------------------------------------------
303
+ // Products (Solo Y)
304
+ // -------------------------------------------------------------------------
305
+
306
+ /**
307
+ * List products available in Y's channel with cost/margin data.
308
+ *
309
+ * `GET /admin/thor/dropshipper/products`
310
+ */
311
+ async getProducts(
312
+ options: GetDropshipperProductsOptions = {},
313
+ ): Promise<GetDropshipperProductsResponse> {
314
+ const { q, category_id, status, in_stock, limit = 20, offset = 0 } = options;
315
+ const qs = new URLSearchParams({
316
+ limit: String(limit),
317
+ offset: String(offset),
318
+ ...(q ? { q } : {}),
319
+ ...(category_id ? { category_id } : {}),
320
+ ...(status ? { status } : {}),
321
+ ...(in_stock !== undefined ? { in_stock: String(in_stock) } : {}),
322
+ });
323
+ return this.request<GetDropshipperProductsResponse>(
324
+ 'GET',
325
+ `/admin/thor/dropshipper/products?${qs}`,
326
+ );
327
+ }
328
+
329
+ // -------------------------------------------------------------------------
330
+ // Prices (Solo Y)
331
+ // -------------------------------------------------------------------------
332
+
333
+ /**
334
+ * Bulk-update sale prices on Y's channel price list.
335
+ *
336
+ * `PUT /admin/thor/dropshipper/prices`
337
+ */
338
+ async updatePrices(
339
+ body: UpdateDropshipperPricesBody,
340
+ ): Promise<UpdateDropshipperPricesResponse> {
341
+ return this.request<UpdateDropshipperPricesResponse>(
342
+ 'PUT',
343
+ '/admin/thor/dropshipper/prices',
344
+ body,
345
+ );
346
+ }
347
+
348
+ // -------------------------------------------------------------------------
349
+ // Orders (Y + X for set-payment-collector)
350
+ // -------------------------------------------------------------------------
351
+
352
+ /**
353
+ * Paginated list of orders in Y's channel.
354
+ *
355
+ * `GET /admin/thor/dropshipper/orders`
356
+ */
357
+ async getOrders(
358
+ options: GetDropshipperOrdersOptions = {},
359
+ ): Promise<GetDropshipperOrdersResponse> {
360
+ const { status, payment_collector, settlement_status, from, to, q, limit = 20, offset = 0 } = options;
361
+ const qs = new URLSearchParams({
362
+ limit: String(limit),
363
+ offset: String(offset),
364
+ ...(status ? { status } : {}),
365
+ ...(payment_collector ? { payment_collector } : {}),
366
+ ...(settlement_status ? { settlement_status } : {}),
367
+ ...(from ? { from } : {}),
368
+ ...(to ? { to } : {}),
369
+ ...(q ? { q } : {}),
370
+ });
371
+ return this.request<GetDropshipperOrdersResponse>(
372
+ 'GET',
373
+ `/admin/thor/dropshipper/orders?${qs}`,
374
+ );
375
+ }
376
+
377
+ /**
378
+ * Full detail for a single order with profit breakdown.
379
+ *
380
+ * `GET /admin/thor/dropshipper/orders/:id`
381
+ */
382
+ async getOrder(orderId: string): Promise<GetDropshipperOrderDetailResponse> {
383
+ return this.request<GetDropshipperOrderDetailResponse>(
384
+ 'GET',
385
+ `/admin/thor/dropshipper/orders/${orderId}`,
386
+ );
387
+ }
388
+
389
+ /**
390
+ * Manually create an order on behalf of a customer.
391
+ *
392
+ * `POST /admin/thor/dropshipper/orders`
393
+ */
394
+ async createOrder(
395
+ body: CreateDropshipperOrderBody,
396
+ ): Promise<CreateDropshipperOrderResponse> {
397
+ return this.request<CreateDropshipperOrderResponse>(
398
+ 'POST',
399
+ '/admin/thor/dropshipper/orders',
400
+ body,
401
+ );
402
+ }
403
+
404
+ /**
405
+ * Set who collects payment for an order (Solo X).
406
+ *
407
+ * `POST /admin/thor/dropshipper/orders/:id/set-payment-collector`
408
+ */
409
+ async setPaymentCollector(
410
+ orderId: string,
411
+ body: SetPaymentCollectorBody,
412
+ ): Promise<SetPaymentCollectorResponse> {
413
+ return this.request<SetPaymentCollectorResponse>(
414
+ 'POST',
415
+ `/admin/thor/dropshipper/orders/${orderId}/set-payment-collector`,
416
+ body,
417
+ );
418
+ }
419
+
420
+ // -------------------------------------------------------------------------
421
+ // Order Cancel & Edits (Solo Y) — B-04
422
+ // -------------------------------------------------------------------------
423
+
424
+ /**
425
+ * Cancel a dropshipper order.
426
+ *
427
+ * `POST /admin/thor/dropshipper/orders/:id/cancel`
428
+ */
429
+ async cancelOrder(orderId: string): Promise<CancelDropshipperOrderResponse> {
430
+ return this.request<CancelDropshipperOrderResponse>(
431
+ 'POST',
432
+ `/admin/thor/dropshipper/orders/${orderId}/cancel`,
433
+ );
434
+ }
435
+
436
+ /**
437
+ * Create an order edit for a dropshipper order.
438
+ *
439
+ * `POST /admin/thor/dropshipper/orders/:id/edit`
440
+ */
441
+ async createOrderEdit(
442
+ orderId: string,
443
+ body: CreateOrderEditBody = {},
444
+ ): Promise<CreateOrderEditResponse> {
445
+ return this.request<CreateOrderEditResponse>(
446
+ 'POST',
447
+ `/admin/thor/dropshipper/orders/${orderId}/edit`,
448
+ body,
449
+ );
450
+ }
451
+
452
+ /**
453
+ * Add an item to the active order edit.
454
+ *
455
+ * `POST /admin/thor/dropshipper/orders/:id/edit/items`
456
+ */
457
+ async addOrderEditItem(
458
+ orderId: string,
459
+ body: AddOrderEditItemBody,
460
+ ): Promise<AddOrderEditItemResponse> {
461
+ return this.request<AddOrderEditItemResponse>(
462
+ 'POST',
463
+ `/admin/thor/dropshipper/orders/${orderId}/edit/items`,
464
+ body,
465
+ );
466
+ }
467
+
468
+ /**
469
+ * Confirm the active order edit, applying changes to the order.
470
+ *
471
+ * `POST /admin/thor/dropshipper/orders/:id/edit/confirm`
472
+ */
473
+ async confirmOrderEdit(
474
+ orderId: string,
475
+ ): Promise<ConfirmOrderEditResponse> {
476
+ return this.request<ConfirmOrderEditResponse>(
477
+ 'POST',
478
+ `/admin/thor/dropshipper/orders/${orderId}/edit/confirm`,
479
+ );
480
+ }
481
+
482
+ /**
483
+ * Get all notes for an order.
484
+ *
485
+ * `GET /admin/thor/dropshipper/orders/:id/notes`
486
+ */
487
+ async getOrderNotes(orderId: string): Promise<GetOrderNotesResponse> {
488
+ return this.request<GetOrderNotesResponse>(
489
+ 'GET',
490
+ `/admin/thor/dropshipper/orders/${orderId}/notes`,
491
+ );
492
+ }
493
+
494
+ /**
495
+ * Create a new note for an order.
496
+ *
497
+ * `POST /admin/thor/dropshipper/orders/:id/notes`
498
+ */
499
+ async createOrderNote(
500
+ orderId: string,
501
+ body: CreateOrderNoteBody,
502
+ ): Promise<OrderNote> {
503
+ return this.request<OrderNote>(
504
+ 'POST',
505
+ `/admin/thor/dropshipper/orders/${orderId}/notes`,
506
+ body,
507
+ );
508
+ }
509
+
510
+ /**
511
+ * Delete a note from an order.
512
+ *
513
+ * `DELETE /admin/thor/dropshipper/orders/:id/notes/:noteId`
514
+ */
515
+ async deleteOrderNote(
516
+ orderId: string,
517
+ noteId: string,
518
+ ): Promise<DeleteOrderNoteResponse> {
519
+ return this.request<DeleteOrderNoteResponse>(
520
+ 'DELETE',
521
+ `/admin/thor/dropshipper/orders/${orderId}/notes/${noteId}`,
522
+ );
523
+ }
524
+
525
+ // -------------------------------------------------------------------------
526
+ // Custom Categories (Solo Y)
527
+ // -------------------------------------------------------------------------
528
+
529
+ /**
530
+ * List Y's custom product categories.
531
+ *
532
+ * `GET /admin/thor/dropshipper/categories`
533
+ */
534
+ async getCategories(
535
+ options: GetDropshipperCategoriesOptions = {},
536
+ ): Promise<GetDropshipperCategoriesResponse> {
537
+ const { parent_id, include_children } = options;
538
+ const qs = new URLSearchParams({
539
+ ...(parent_id ? { parent_id } : {}),
540
+ ...(include_children !== undefined ? { include_children: String(include_children) } : {}),
541
+ });
542
+ const query = qs.toString() ? `?${qs}` : '';
543
+ return this.request<GetDropshipperCategoriesResponse>(
544
+ 'GET',
545
+ `/admin/thor/dropshipper/categories${query}`,
546
+ );
547
+ }
548
+
549
+ /**
550
+ * Create a custom category in Y's channel.
551
+ *
552
+ * `POST /admin/thor/dropshipper/categories`
553
+ */
554
+ async createCategory(
555
+ body: CreateDropshipperCategoryBody,
556
+ ): Promise<DropshipperCategory> {
557
+ return this.request<DropshipperCategory>(
558
+ 'POST',
559
+ '/admin/thor/dropshipper/categories',
560
+ body,
561
+ );
562
+ }
563
+
564
+ /**
565
+ * Update a custom category.
566
+ *
567
+ * `PUT /admin/thor/dropshipper/categories/:id`
568
+ */
569
+ async updateCategory(
570
+ categoryId: string,
571
+ body: UpdateDropshipperCategoryBody,
572
+ ): Promise<DropshipperCategory> {
573
+ return this.request<DropshipperCategory>(
574
+ 'PUT',
575
+ `/admin/thor/dropshipper/categories/${categoryId}`,
576
+ body,
577
+ );
578
+ }
579
+
580
+ /**
581
+ * Delete a custom category.
582
+ *
583
+ * `DELETE /admin/thor/dropshipper/categories/:id`
584
+ */
585
+ async deleteCategory(
586
+ categoryId: string,
587
+ ): Promise<DeleteDropshipperCategoryResponse> {
588
+ return this.request<DeleteDropshipperCategoryResponse>(
589
+ 'DELETE',
590
+ `/admin/thor/dropshipper/categories/${categoryId}`,
591
+ );
592
+ }
593
+
594
+ /**
595
+ * Map a product to a custom category.
596
+ *
597
+ * `POST /admin/thor/dropshipper/category-mappings`
598
+ */
599
+ async createCategoryMapping(
600
+ body: CreateCategoryMappingBody,
601
+ ): Promise<CreateCategoryMappingResponse> {
602
+ return this.request<CreateCategoryMappingResponse>(
603
+ 'POST',
604
+ '/admin/thor/dropshipper/category-mappings',
605
+ body,
606
+ );
607
+ }
608
+
609
+ /**
610
+ * Remove a product-to-category mapping.
611
+ *
612
+ * `DELETE /admin/thor/dropshipper/category-mappings/:id`
613
+ */
614
+ async deleteCategoryMapping(
615
+ mappingId: string,
616
+ ): Promise<DeleteCategoryMappingResponse> {
617
+ return this.request<DeleteCategoryMappingResponse>(
618
+ 'DELETE',
619
+ `/admin/thor/dropshipper/category-mappings/${mappingId}`,
620
+ );
621
+ }
622
+
623
+ // -------------------------------------------------------------------------
624
+ // Customers (Solo Y)
625
+ // -------------------------------------------------------------------------
626
+
627
+ /**
628
+ * Paginated list of customers in Y's channel.
629
+ *
630
+ * `GET /admin/thor/dropshipper/customers`
631
+ */
632
+ async getCustomers(
633
+ options: GetDropshipperCustomersOptions = {},
634
+ ): Promise<GetDropshipperCustomersResponse> {
635
+ const { q, has_orders, from, limit = 20, offset = 0 } = options;
636
+ const qs = new URLSearchParams({
637
+ limit: String(limit),
638
+ offset: String(offset),
639
+ ...(q ? { q } : {}),
640
+ ...(has_orders !== undefined ? { has_orders: String(has_orders) } : {}),
641
+ ...(from ? { from } : {}),
642
+ });
643
+ return this.request<GetDropshipperCustomersResponse>(
644
+ 'GET',
645
+ `/admin/thor/dropshipper/customers?${qs}`,
646
+ );
647
+ }
648
+
649
+ /**
650
+ * Full detail for a single customer.
651
+ *
652
+ * `GET /admin/thor/dropshipper/customers/:id`
653
+ */
654
+ async getCustomer(
655
+ customerId: string,
656
+ ): Promise<GetDropshipperCustomerDetailResponse> {
657
+ return this.request<GetDropshipperCustomerDetailResponse>(
658
+ 'GET',
659
+ `/admin/thor/dropshipper/customers/${customerId}`,
660
+ );
661
+ }
662
+
663
+ /**
664
+ * Create a customer scoped to Y's channel.
665
+ *
666
+ * `POST /admin/thor/dropshipper/customers`
667
+ */
668
+ async createCustomer(
669
+ body: CreateDropshipperCustomerBody,
670
+ ): Promise<CreateDropshipperCustomerResponse> {
671
+ return this.request<CreateDropshipperCustomerResponse>(
672
+ 'POST',
673
+ '/admin/thor/dropshipper/customers',
674
+ body,
675
+ );
676
+ }
677
+
678
+ // -------------------------------------------------------------------------
679
+ // Customer Addresses (Solo Y) — B-06
680
+ // -------------------------------------------------------------------------
681
+
682
+ /**
683
+ * List addresses for a customer in Y's channel.
684
+ *
685
+ * `GET /admin/thor/dropshipper/customers/:id/addresses`
686
+ */
687
+ async getCustomerAddresses(
688
+ customerId: string,
689
+ ): Promise<GetDropshipperAddressesResponse> {
690
+ return this.request<GetDropshipperAddressesResponse>(
691
+ 'GET',
692
+ `/admin/thor/dropshipper/customers/${customerId}/addresses`,
693
+ );
694
+ }
695
+
696
+ /**
697
+ * Create a new address for a customer.
698
+ *
699
+ * `POST /admin/thor/dropshipper/customers/:id/addresses`
700
+ */
701
+ async createCustomerAddress(
702
+ customerId: string,
703
+ body: DropshipperAddressBody,
704
+ ): Promise<DropshipperAddressResponse> {
705
+ return this.request<DropshipperAddressResponse>(
706
+ 'POST',
707
+ `/admin/thor/dropshipper/customers/${customerId}/addresses`,
708
+ body,
709
+ );
710
+ }
711
+
712
+ /**
713
+ * Update an existing customer address.
714
+ *
715
+ * `PUT /admin/thor/dropshipper/customers/:id/addresses/:addressId`
716
+ */
717
+ async updateCustomerAddress(
718
+ customerId: string,
719
+ addressId: string,
720
+ body: Partial<DropshipperAddressBody>,
721
+ ): Promise<DropshipperAddressResponse> {
722
+ return this.request<DropshipperAddressResponse>(
723
+ 'PUT',
724
+ `/admin/thor/dropshipper/customers/${customerId}/addresses/${addressId}`,
725
+ body,
726
+ );
727
+ }
728
+
729
+ /**
730
+ * Delete a customer address.
731
+ *
732
+ * `DELETE /admin/thor/dropshipper/customers/:id/addresses/:addressId`
733
+ */
734
+ async deleteCustomerAddress(
735
+ customerId: string,
736
+ addressId: string,
737
+ ): Promise<{ deleted: true; id: string }> {
738
+ return this.request<{ deleted: true; id: string }>(
739
+ 'DELETE',
740
+ `/admin/thor/dropshipper/customers/${customerId}/addresses/${addressId}`,
741
+ );
742
+ }
743
+
744
+ // -------------------------------------------------------------------------
745
+ // Account & Settlements (Y read-only)
746
+ // -------------------------------------------------------------------------
747
+
748
+ /**
749
+ * Y's account info with current balance.
750
+ *
751
+ * `GET /admin/thor/dropshipper/account`
752
+ */
753
+ async getAccount(): Promise<GetDropshipperAccountResponse> {
754
+ return this.request<GetDropshipperAccountResponse>(
755
+ 'GET',
756
+ '/admin/thor/dropshipper/account',
757
+ );
758
+ }
759
+
760
+ /**
761
+ * Orders where Y owes money to X (Y collected payment, needs to pay costs).
762
+ *
763
+ * `GET /admin/thor/dropshipper/account/payable`
764
+ */
765
+ async getPayable(): Promise<GetDropshipperPayableResponse> {
766
+ return this.request<GetDropshipperPayableResponse>(
767
+ 'GET',
768
+ '/admin/thor/dropshipper/account/payable',
769
+ );
770
+ }
771
+
772
+ /**
773
+ * Orders where X owes money to Y (X collected COD, needs to transfer profit).
774
+ *
775
+ * `GET /admin/thor/dropshipper/account/receivable`
776
+ */
777
+ async getReceivable(): Promise<GetDropshipperReceivableResponse> {
778
+ return this.request<GetDropshipperReceivableResponse>(
779
+ 'GET',
780
+ '/admin/thor/dropshipper/account/receivable',
781
+ );
782
+ }
783
+
784
+ /**
785
+ * List settlements (Y sees own; X can filter by account_id).
786
+ *
787
+ * `GET /admin/thor/dropshipper/settlements`
788
+ */
789
+ async getSettlements(
790
+ options: GetSettlementsOptions = {},
791
+ ): Promise<GetSettlementsResponse> {
792
+ const { status, type, account_id, limit = 20, offset = 0 } = options;
793
+ const qs = new URLSearchParams({
794
+ limit: String(limit),
795
+ offset: String(offset),
796
+ ...(status ? { status } : {}),
797
+ ...(type ? { type } : {}),
798
+ ...(account_id ? { account_id } : {}),
799
+ });
800
+ return this.request<GetSettlementsResponse>(
801
+ 'GET',
802
+ `/admin/thor/dropshipper/settlements?${qs}`,
803
+ );
804
+ }
805
+
806
+ /**
807
+ * Full detail for a single settlement record.
808
+ *
809
+ * `GET /admin/thor/dropshipper/settlements/:id`
810
+ */
811
+ async getSettlement(
812
+ settlementId: string,
813
+ ): Promise<GetSettlementDetailResponse> {
814
+ return this.request<GetSettlementDetailResponse>(
815
+ 'GET',
816
+ `/admin/thor/dropshipper/settlements/${settlementId}`,
817
+
818
+ );
819
+ }
820
+
821
+ // -------------------------------------------------------------------------
822
+ // Admin Settlements (Solo X)
823
+ // -------------------------------------------------------------------------
824
+
825
+ /**
826
+ * Create a settlement record for a dropshipper account.
827
+ *
828
+ * `POST /admin/thor/dropshipper/admin/settlements`
829
+ */
830
+ async createSettlement(
831
+ body: CreateSettlementBody,
832
+ ): Promise<CreateSettlementResponse> {
833
+ return this.request<CreateSettlementResponse>(
834
+ 'POST',
835
+ '/admin/thor/dropshipper/admin/settlements',
836
+ body,
837
+ );
838
+ }
839
+
840
+ /**
841
+ * Confirm a pending settlement (marks it as paid/received).
842
+ *
843
+ * `POST /admin/thor/dropshipper/admin/settlements/:id/confirm`
844
+ */
845
+ async confirmSettlement(
846
+ settlementId: string,
847
+ body: ConfirmSettlementBody = {},
848
+ ): Promise<UpdateSettlementStatusResponse> {
849
+ return this.request<UpdateSettlementStatusResponse>(
850
+ 'POST',
851
+ `/admin/thor/dropshipper/admin/settlements/${settlementId}/confirm`,
852
+ body,
853
+ );
854
+ }
855
+
856
+ /**
857
+ * Cancel a pending settlement.
858
+ *
859
+ * `POST /admin/thor/dropshipper/admin/settlements/:id/cancel`
860
+ */
861
+ async cancelSettlement(
862
+ settlementId: string,
863
+ body: CancelSettlementBody = {},
864
+ ): Promise<UpdateSettlementStatusResponse> {
865
+ return this.request<UpdateSettlementStatusResponse>(
866
+ 'POST',
867
+ `/admin/thor/dropshipper/admin/settlements/${settlementId}/cancel`,
868
+ body,
869
+ );
870
+ }
871
+
872
+ // -------------------------------------------------------------------------
873
+ // Admin Settlements — list (Solo X)
874
+ // -------------------------------------------------------------------------
875
+
876
+ /**
877
+ * X views all settlement records across all dropshipper accounts.
878
+ *
879
+ * `GET /admin/thor/dropshipper/admin/settlements`
880
+ */
881
+ async getAdminSettlements(
882
+ options: GetSettlementsOptions = {},
883
+ ): Promise<GetSettlementsResponse> {
884
+ const { status, type, account_id, limit = 20, offset = 0 } = options;
885
+ const qs = new URLSearchParams({
886
+ limit: String(limit),
887
+ offset: String(offset),
888
+ ...(status ? { status } : {}),
889
+ ...(type ? { type } : {}),
890
+ ...(account_id ? { account_id } : {}),
891
+ });
892
+ return this.request<GetSettlementsResponse>(
893
+ 'GET',
894
+ `/admin/thor/dropshipper/admin/settlements?${qs}`,
895
+ );
896
+ }
897
+
898
+ // -------------------------------------------------------------------------
899
+ // Admin Account Management (Solo X)
900
+ // -------------------------------------------------------------------------
901
+
902
+ /**
903
+ * List all dropshipper accounts.
904
+ *
905
+ * `GET /admin/thor/dropshipper/admin/accounts`
906
+ */
907
+ async getAdminAccounts(): Promise<GetAdminDropshipperAccountsResponse> {
908
+ return this.request<GetAdminDropshipperAccountsResponse>(
909
+ 'GET',
910
+ '/admin/thor/dropshipper/admin/accounts',
911
+ );
912
+ }
913
+
914
+ /**
915
+ * Full balance detail for a specific dropshipper account.
916
+ *
917
+ * `GET /admin/thor/dropshipper/admin/accounts/:id/balance`
918
+ */
919
+ async getAdminAccountBalance(
920
+ accountId: string,
921
+ ): Promise<GetAdminAccountBalanceResponse> {
922
+ return this.request<GetAdminAccountBalanceResponse>(
923
+ 'GET',
924
+ `/admin/thor/dropshipper/admin/accounts/${accountId}/balance`,
925
+ );
926
+ }
927
+
928
+ /**
929
+ * List all dropshipper price lists.
930
+ *
931
+ * `GET /admin/thor/dropshipper/admin/price-lists`
932
+ */
933
+ async getAdminPriceLists(): Promise<GetAdminPriceListsResponse> {
934
+ return this.request<GetAdminPriceListsResponse>(
935
+ 'GET',
936
+ '/admin/thor/dropshipper/admin/price-lists',
937
+ );
938
+ }
939
+
940
+ /**
941
+ * Utility: look up which sales channel a user belongs to.
942
+ *
943
+ * `GET /admin/thor/dropshipper/admin/user-channel`
944
+ */
945
+ async getUserChannel(): Promise<GetUserChannelResponse> {
946
+ return this.request<GetUserChannelResponse>(
947
+ 'GET',
948
+ '/admin/thor/dropshipper/admin/user-channel',
949
+ );
950
+ }
951
+
952
+ // -------------------------------------------------------------------------
953
+ // Promotions (Solo Y)
954
+ // -------------------------------------------------------------------------
955
+
956
+ /**
957
+ * List Y's promotions (discount codes).
958
+ *
959
+ * `GET /admin/thor/dropshipper/promotions`
960
+ */
961
+ async getPromotions(
962
+ options: GetDropshipperPromotionsOptions = {},
963
+ ): Promise<GetDropshipperPromotionsResponse> {
964
+ const { type, limit = 20, offset = 0 } = options;
965
+ const qs = new URLSearchParams({
966
+ limit: String(limit),
967
+ offset: String(offset),
968
+ ...(type ? { type } : {}),
969
+ });
970
+ return this.request<GetDropshipperPromotionsResponse>(
971
+ 'GET',
972
+ `/admin/thor/dropshipper/promotions?${qs}`,
973
+ );
974
+ }
975
+
976
+ /**
977
+ * Create a promotion for Y's channel.
978
+ *
979
+ * `POST /admin/thor/dropshipper/promotions`
980
+ */
981
+ async createPromotion(
982
+ body: CreateDropshipperPromotionBody,
983
+ ): Promise<DropshipperPromotion> {
984
+ return this.request<DropshipperPromotion>(
985
+ 'POST',
986
+ '/admin/thor/dropshipper/promotions',
987
+ body,
988
+ );
989
+ }
990
+
991
+ /**
992
+ * Get a single promotion by ID.
993
+ *
994
+ * `GET /admin/thor/dropshipper/promotions/:id`
995
+ */
996
+ async getPromotion(promotionId: string): Promise<DropshipperPromotion> {
997
+ return this.request<DropshipperPromotion>(
998
+ 'GET',
999
+ `/admin/thor/dropshipper/promotions/${promotionId}`,
1000
+ );
1001
+ }
1002
+
1003
+ /**
1004
+ * Update an existing promotion.
1005
+ *
1006
+ * `PUT /admin/thor/dropshipper/promotions/:id`
1007
+ */
1008
+ async updatePromotion(
1009
+ promotionId: string,
1010
+ body: UpdateDropshipperPromotionBody,
1011
+ ): Promise<DropshipperPromotion> {
1012
+ return this.request<DropshipperPromotion>(
1013
+ 'PUT',
1014
+ `/admin/thor/dropshipper/promotions/${promotionId}`,
1015
+ body,
1016
+ );
1017
+ }
1018
+
1019
+ /**
1020
+ * Delete a promotion.
1021
+ *
1022
+ * `DELETE /admin/thor/dropshipper/promotions/:id`
1023
+ */
1024
+ async deletePromotion(
1025
+ promotionId: string,
1026
+ ): Promise<DeleteDropshipperPromotionResponse> {
1027
+ return this.request<DeleteDropshipperPromotionResponse>(
1028
+ 'DELETE',
1029
+ `/admin/thor/dropshipper/promotions/${promotionId}`,
1030
+ );
1031
+ }
1032
+
1033
+ // -------------------------------------------------------------------------
1034
+ // Dashboard (Solo Y)
1035
+ // -------------------------------------------------------------------------
1036
+
1037
+ /**
1038
+ * Aggregate stats for Y's dashboard (revenue, profit, top products).
1039
+ *
1040
+ * `GET /admin/thor/dropshipper/dashboard/stats`
1041
+ */
1042
+ async getDashboardStats(
1043
+ options: GetDashboardStatsOptions = {},
1044
+ ): Promise<GetDashboardStatsResponse> {
1045
+ const { period, from, to, currency_code } = options;
1046
+ const qs = new URLSearchParams({
1047
+ ...(period ? { period } : {}),
1048
+ ...(from ? { from } : {}),
1049
+ ...(to ? { to } : {}),
1050
+ ...(currency_code ? { currency_code } : {}),
1051
+ });
1052
+ const query = qs.toString() ? `?${qs}` : '';
1053
+ return this.request<GetDashboardStatsResponse>(
1054
+ 'GET',
1055
+ `/admin/thor/dropshipper/dashboard/stats${query}`,
1056
+ );
1057
+ }
1058
+
1059
+ // -------------------------------------------------------------------------
1060
+ // Storefront (V — end customer, uses publishable API key)
1061
+ // -------------------------------------------------------------------------
1062
+
1063
+ /**
1064
+ * Public endpoint: dropshipper-scoped categories for the storefront.
1065
+ * Typically called with a publishable API key rather than a JWT.
1066
+ *
1067
+ * `GET /store/thor/dropshipper-categories`
1068
+ */
1069
+ async getStorefrontCategories(): Promise<GetStorefrontDropshipperCategoriesResponse> {
1070
+ return this.request<GetStorefrontDropshipperCategoriesResponse>(
1071
+ 'GET',
1072
+ '/store/thor/dropshipper-categories',
1073
+ );
1074
+ }
1075
+ }
1076
+
1077
+ // Re-export types used by callers so they don't need to import from @thorprovider/types directly.
1078
+ export type { DropshipperCategory, DropshipperPromotion } from '@thorprovider/types';