Is there a way to map fields with different names in NestJS?

Solution 1:

You can try something like this. All you need to do is to add the keys from SAP in key and set value as the key you want to use in your application.

const mappings = {
  PLNT_FABRIZIEREN_C: "factoryPlantCode",
  MRKD_PRODUCTS: "markedProducts",
  NEUES_PRDKT: "isNewProduct"
}

const getFormattedVersion = (data) => {
  const returnData = {};
  Object.keys(data).forEach(key => {
    returnData[mappings[key]] = data[key];
  })
  return returnData
}

const getSAPSupportedVersion = (data) =>{
  const returnData = {};
  Object.keys(data).forEach(key => {
    const mappingKeys = Object.keys(mappings)
    const mappingValues = Object.values(mappings)
    const mappedKey = mappingKeys[mappingValues.indexOf(key)];
    
    returnData[mappedKey] = data[key];
  })

  return returnData
}

console.log(
   getFormattedVersion({ 
      PLNT_FABRIZIEREN_C: '253D',
      MRKD_PRODUCTS: ['PRODUCT1', 'PRODUCT2'],
            NEUES_PRDKT: true 
    })
)

console.log(
   getSAPSupportedVersion({
     factoryPlantCode: "253D",
     isNewProduct: true,
     markedProducts: ["PRODUCT1", "PRODUCT2"]
  })
)