UNPKG

trade360-nodejs-sdk

Version:
61 lines 2.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransformerUtil = void 0; const class_transformer_1 = require("class-transformer"); const conversion_error_1 = require("../entities/errors/conversion.error"); class TransformerUtil { /** * Deserialize a plain object to an instance of the * specified class. * @param plainObject The plain object to deserialize * @param targetClass The class to instantiate with the * properties set according to the plain object provided * @returns An instance of the specified class, TEntity, * with the properties set according to the plain object * provided */ static transform(plainObject, targetClass) { try { return (0, class_transformer_1.plainToInstance)(targetClass, plainObject, { excludeExtraneousValues: true, // Change this to false if you want to keep all properties exposeUnsetFields: false, }); } catch (err) { throw new conversion_error_1.ConversionError(targetClass.name, err); } } /** * Deserialize an array of plain objects to an array of * instances of the specified class. * @param plainArray The array of plain objects to * deserialize * @param targetClass The class to instantiate with the * properties set according to the plain objects provided * @returns An array of instances of the specified class, * TEntity, with the properties set according to the plain * objects provided */ static transformArray(plainArray, targetClass) { return (0, class_transformer_1.plainToInstance)(targetClass, plainArray, { excludeExtraneousValues: true, // Change this to false if you want to keep all properties exposeUnsetFields: false, }); } /** * Serialize an instance to a plain object using the @Expose * decorator names (PascalCase) for API compatibility. * This converts TypeScript objects back to the API format. * @param instance The instance to serialize * @returns A plain object with PascalCase field names as * defined by @Expose decorators */ static serializeToApiFormat(instance) { return (0, class_transformer_1.instanceToPlain)(instance, { excludeExtraneousValues: true, exposeUnsetFields: false, }); } } exports.TransformerUtil = TransformerUtil; //# sourceMappingURL=transformer-util.js.map