UNPKG

@softchef/cdk-iot-device-management

Version:

IoT device management is composed of things, thing types, thing groups, jobs, files API services. The constructs can be used independently, that are based on full-managed service to create an API Gateway & Lambda function.

64 lines (63 loc) 2.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertToNative = void 0; const convertToNative = (data, options) => { for (const [key, value] of Object.entries(data)) { if (value !== undefined) { switch (key) { case "NULL": return null; case "BOOL": return Boolean(value); case "N": return convertNumber(value, options); case "B": return convertBinary(value); case "S": return convertString(value); case "L": return convertList(value, options); case "M": return convertMap(value, options); case "NS": return new Set(value.map((item) => convertNumber(item, options))); case "BS": return new Set(value.map(convertBinary)); case "SS": return new Set(value.map(convertString)); default: throw new Error(`Unsupported type passed: ${key}`); } } } throw new Error(`No value defined: ${JSON.stringify(data)}`); }; exports.convertToNative = convertToNative; const convertNumber = (numString, options) => { if (options === null || options === void 0 ? void 0 : options.wrapNumbers) { return { value: numString }; } const num = Number(numString); const infinityValues = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]; if ((num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) && !infinityValues.includes(num)) { if (typeof BigInt === "function") { try { return BigInt(numString); } catch (error) { throw new Error(`${numString} can't be converted to BigInt. Set options.wrapNumbers to get string value.`); } } else { throw new Error(`${numString} is outside SAFE_INTEGER bounds. Set options.wrapNumbers to get string value.`); } } return num; }; const convertString = (stringValue) => stringValue; const convertBinary = (binaryValue) => binaryValue; const convertList = (list, options) => list.map((item) => exports.convertToNative(item, options)); const convertMap = (map, options) => Object.entries(map).reduce((acc, [key, value]) => ({ ...acc, [key]: exports.convertToNative(value, options), }), {});