import { pick, omitBy, isUndefined } from 'lodash'; import type com from '../../proto/index.js'; import { ConverterSet, ConverterType } from '../domain.js'; import type { Address } from './types.js'; const sharedFieldNames = ['city', 'subdivision', 'country', 'postalCode']; const sharedStreetAddressFieldNames = ['name', 'number', 'apt']; export const address: ConverterSet = { types: ['wix.common.Address'], [ConverterType.FROM_JSON]: { /** * currently wix.common.Address represents the deserialized object, so we're good. * in case it contains some serializable stuff (e.g. int64), we'll have to make some * modifications in the type. */ transform: (val: com.wix.common.Address): Address => { return ( val && omitBy( { formatted: val.formattedAddress, location: val.geocode, addressLine1: val.addressLine, addressLine2: val.addressLine2, streetAddress: val.streetAddress && pick(val.streetAddress, sharedStreetAddressFieldNames), ...pick(val, sharedFieldNames), }, isUndefined, ) ); }, }, [ConverterType.TO_JSON]: { transform: (val: Address): com.wix.common.Address => { return ( val && omitBy( { ...pick(val, sharedFieldNames), formattedAddress: val.formatted, geocode: val.location, addressLine: val.addressLine1, addressLine2: val.addressLine2, streetAddress: val.streetAddress && pick(val.streetAddress, sharedStreetAddressFieldNames), }, isUndefined, ) ); }, }, };