@@ -53,7 +53,13 @@ import {
5353 type CheckoutCompleteResponse ,
5454 type OrderSummaryDTO
5555} from './types' ;
56- import type { Address , Checkout , CheckoutItem , CompleteCheckoutInput } from '$lib/types/checkout' ;
56+ import type {
57+ Address ,
58+ Checkout ,
59+ CheckoutItem ,
60+ CompleteCheckoutInput ,
61+ SetAddressInput
62+ } from '$lib/types/checkout' ;
5763import type { CreateDiscount , Discount } from '$lib/types/discount' ;
5864import type { CommercifyUser } from '$lib/types/user' ;
5965
@@ -141,15 +147,7 @@ export class CommercifyClient {
141147 headers : Object . keys ( headers )
142148 } ) ;
143149
144- // Try to get the response body for more details
145- try {
146- const errorBody = await response . text ( ) ;
147- console . error ( 'Error response body:' , errorBody ) ;
148- } catch ( e ) {
149- console . error ( 'Could not read error response body' ) ;
150- }
151-
152- throw new Error ( `API request failed: ${ response . status } ${ response . statusText } ` ) ;
150+ throw new Error ( response . statusText || 'API request failed' ) ;
153151 }
154152
155153 return await response . json ( ) ;
@@ -1305,12 +1303,21 @@ export class CommercifyClient {
13051303 * Set shipping address for the checkout
13061304 */
13071305 async setShippingAddress (
1308- data : SetShippingAddressRequest
1306+ data : SetAddressInput
13091307 ) : Promise < { success : boolean ; data ?: Checkout ; error ?: string } > {
1308+ const requestBody : SetShippingAddressRequest = {
1309+ address_line1 : data . addressLine1 ,
1310+ address_line2 : data . addressLine2 || '' ,
1311+ city : data . city ,
1312+ state : data . state || '' ,
1313+ postal_code : data . postalCode ,
1314+ country : data . country
1315+ } ;
1316+
13101317 try {
13111318 const response = await this . request < ResponseDTO < CheckoutDTO > > ( '/checkout/shipping-address' , {
13121319 method : 'PUT' ,
1313- body : JSON . stringify ( data )
1320+ body : JSON . stringify ( requestBody )
13141321 } ) ;
13151322
13161323 if ( ! response . success || ! response . data ) {
@@ -1340,12 +1347,21 @@ export class CommercifyClient {
13401347 * Set billing address for the checkout
13411348 */
13421349 async setBillingAddress (
1343- data : SetBillingAddressRequest
1350+ data : SetAddressInput
13441351 ) : Promise < { success : boolean ; data ?: Checkout ; error ?: string } > {
1352+ const requestBody : SetShippingAddressRequest = {
1353+ address_line1 : data . addressLine1 ,
1354+ address_line2 : data . addressLine2 || '' ,
1355+ city : data . city ,
1356+ state : data . state || '' ,
1357+ postal_code : data . postalCode ,
1358+ country : data . country
1359+ } ;
1360+
13451361 try {
13461362 const response = await this . request < ResponseDTO < CheckoutDTO > > ( '/checkout/billing-address' , {
13471363 method : 'PUT' ,
1348- body : JSON . stringify ( data )
1364+ body : JSON . stringify ( requestBody )
13491365 } ) ;
13501366
13511367 if ( ! response . success || ! response . data ) {
@@ -1650,6 +1666,43 @@ export class CommercifyClient {
16501666 } ;
16511667 }
16521668
1669+ async getOrderById ( orderId : string ) : Promise < { success : boolean ; data ?: Order ; error ?: string } > {
1670+ if ( ! orderId ) {
1671+ console . warn ( 'No order ID provided, returning null' ) ;
1672+ return {
1673+ success : false ,
1674+ error : 'No order ID provided'
1675+ } ;
1676+ }
1677+
1678+ try {
1679+ const response = await this . request < ResponseDTO < OrderDTO > > ( `/orders/${ orderId } ` ) ;
1680+
1681+ if ( ! response . success || ! response . data ) {
1682+ return {
1683+ success : false ,
1684+ error : response . error || 'Failed to retrieve order'
1685+ } ;
1686+ }
1687+
1688+ return {
1689+ success : true ,
1690+ data : this . mapOrder ( response . data )
1691+ } ;
1692+ } catch ( error ) {
1693+ console . error (
1694+ `Error fetching order ${ orderId } :` ,
1695+ error instanceof Error ? error . message : String ( error )
1696+ ) ;
1697+ return {
1698+ success : false ,
1699+ error : `Error fetching order ${ orderId } : ${
1700+ error instanceof Error ? error . message : String ( error )
1701+ } `
1702+ } ;
1703+ }
1704+ }
1705+
16531706 async createDiscount ( input : CreateDiscount ) : Promise < {
16541707 success : boolean ;
16551708 data ?: Discount ;
0 commit comments