diff --git a/packages/js/api/src/framework/transformations/model-transformer-transformation.ts b/packages/js/api/src/framework/transformations/model-transformer-transformation.ts index f5b53085987..11b85bae761 100644 --- a/packages/js/api/src/framework/transformations/model-transformer-transformation.ts +++ b/packages/js/api/src/framework/transformations/model-transformer-transformation.ts @@ -43,6 +43,10 @@ export class ModelTransformerTransformation< T extends Model > implements ModelT * @template T */ public constructor( property: string, modelClass: ModelConstructor< T >, transformer: ModelTransformer< T > ) { + // Developer-friendly error to make sure this doesn't go unnoticed. + if (property.includes('_')) { + throw new Error('The property must be camelCase'); + } this.property = property; this.modelClass = modelClass; this.transformer = transformer; @@ -74,27 +78,12 @@ export class ModelTransformerTransformation< T extends Model > implements ModelT * @return {*} The transformed properties. */ public toModel( properties: any ): any { - let propertyName = this.property; - let val = properties[ propertyName ]; - - if ( ! val ) { - /* - * Properties are defined in snake_case format, but the properties in the models are camelCase. - * Due to how the hydration of the model works, using TypeScript's Partial, the properties object - * might have been transformed from snake_case to camelCase already, so we try to convert - * the property name to camelCase before giving up. - */ - propertyName = propertyName.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, function (_match, chr) { - return chr.toUpperCase(); - }); - val = properties[ propertyName ]; - } - + const val = properties[ this.property ]; if ( val ) { if ( Array.isArray( val ) ) { - properties[ propertyName ] = val.map( ( v ) => this.transformer.toModel( this.modelClass, v ) ); + properties[ this.property ] = val.map( ( v ) => this.transformer.toModel( this.modelClass, v ) ); } else { - properties[ propertyName ] = this.transformer.toModel( this.modelClass, val ); + properties[ this.property ] = this.transformer.toModel( this.modelClass, val ); } } diff --git a/packages/js/api/src/repositories/rest/orders/transformer.ts b/packages/js/api/src/repositories/rest/orders/transformer.ts index 78f6a327ea6..e050506199f 100644 --- a/packages/js/api/src/repositories/rest/orders/transformer.ts +++ b/packages/js/api/src/repositories/rest/orders/transformer.ts @@ -30,13 +30,13 @@ export function createOrderTransformer(): ModelTransformer< Order > { new IgnorePropertyTransformation( [ 'date_created', 'date_modified' ] ), new ModelTransformerTransformation( 'billing', BillingOrderAddress, createBillingAddressTransformer() ), new ModelTransformerTransformation( 'shipping', ShippingOrderAddress, createShippingAddressTransformer() ), - new ModelTransformerTransformation( 'tax_lines', OrderTaxRate, createOrderTaxRateTransformer() ), + new ModelTransformerTransformation( 'taxLines', OrderTaxRate, createOrderTaxRateTransformer() ), new ModelTransformerTransformation( 'refunds', OrderRefundLine, createOrderRefundLineTransformer() ), - new ModelTransformerTransformation( 'coupon_lines', OrderCouponLine, createOrdeCouponLineTransformer() ), - new ModelTransformerTransformation( 'fee_lines', OrderFeeLine, createOrderFeeLineTransformer() ), - new ModelTransformerTransformation( 'line_items', OrderLineItem, createOrderLineItemTransformer() ), - new ModelTransformerTransformation( 'shipping_lines', OrderShippingLine, createOrderShippingItemTransformer() ), - new ModelTransformerTransformation( 'meta_data', MetaData, createMetaDataTransformer() ), + new ModelTransformerTransformation( 'couponLines', OrderCouponLine, createOrdeCouponLineTransformer() ), + new ModelTransformerTransformation( 'feeLines', OrderFeeLine, createOrderFeeLineTransformer() ), + new ModelTransformerTransformation( 'lineItems', OrderLineItem, createOrderLineItemTransformer() ), + new ModelTransformerTransformation( 'shippingLines', OrderShippingLine, createOrderShippingItemTransformer() ), + new ModelTransformerTransformation( 'metaData', MetaData, createMetaDataTransformer() ), new PropertyTypeTransformation( { @@ -212,7 +212,7 @@ function createOrderRefundLineTransformer(): ModelTransformer< OrderRefundLine > function createOrdeCouponLineTransformer(): ModelTransformer< OrderCouponLine > { return new ModelTransformer( [ - new ModelTransformerTransformation( 'meta_data', MetaData, createMetaDataTransformer() ), + new ModelTransformerTransformation( 'metaData', MetaData, createMetaDataTransformer() ), new PropertyTypeTransformation( { code: PropertyType.String,