UNPKG

@sap/xsodata

Version:

Expose data from a HANA database as OData V2 service with help of .xsodata files.

181 lines (162 loc) 3.31 kB
'use strict'; const xmlValueToODataJson = (exports.xmlValueToODataJson = {}); const XsODataTypeError = require('./../errors/typeError'); const dateTimeParser = require('./../../parsers/dateTimeParser'); /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Binary'] = function xmlEdmBinaryToODataJson( jsonValue ) { /** * in: base64 encoded string * out: base64 encoded string */ return jsonValue; }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Boolean'] = function xmlEdmBooleanToODataJson( jsonValue ) { /** * in: true / false or 1 / 0 * out: true / false */ return ( jsonValue === 'true' || jsonValue === true || jsonValue === '1' || jsonValue === 1 ); }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Byte'] = function xmlEdmByteToODataJson(jsonValue) { /** * in: "0" - "255" * out: 0 - 255 */ return parseInt(jsonValue); }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.DateTime'] = function xmlEdmDateTimeToODataJson( jsonValue ) { /** * in: "YYYY-[M]M-[D]DTHH:mm[:ss.zzzzzzz]" --> z = DIGIT * See https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC * --> Date.UTC(year, month[, day[, hour[, minute[, second[, millisecond]]]]]) * out: /Date(<millis since 1970-01-01T00:00:00.0000000>)/ */ try { const result = dateTimeParser.parse(jsonValue); const month = result.month - 1; const date = new Date( Date.UTC( result.year, month, result.day, result.hour, result.minute, result.second, result.millisecond ) ); return '/Date(' + date.getTime() + ')/'; } catch (err) { throw new XsODataTypeError( 'Provided Edm.DateTime property is not valid' ); } }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Decimal'] = function xmlEdmDecimalToDbJson(jsonValue) { return jsonValue; }; /** * * @param jsonValue * @param dbDataType * @returns {*} */ xmlValueToODataJson['Edm.Double'] = noop; /** * * @param jsonValue * @param dbDataType * @returns {*} */ xmlValueToODataJson['Edm.Guid'] = noop; /** * * @param jsonValue * @param dbDataType * @returns {*} */ xmlValueToODataJson['Edm.Single'] = noop; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Int16'] = function (jsonValue) { return parseInt(jsonValue); }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Int32'] = function (jsonValue) { return parseInt(jsonValue); }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Int64'] = noop; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.String'] = noop; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.SByte'] = function (jsonValue) { return parseInt(jsonValue); }; /** * * @param jsonValue * @returns {*} */ xmlValueToODataJson['Edm.Time'] = noop; /** * * @param jsonValue */ function noop(jsonValue) { return jsonValue; }