UNPKG

fitbit-api-client

Version:
101 lines 4.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SleepResponseFromJson = SleepResponseFromJson; const types_utils_1 = require("../utils/types.utils"); const date_utils_1 = require("../utils/date.utils"); function SleepResponseFromJson(offsetFromUTCMillis, json) { const sleeps = (0, types_utils_1.get)(json, 'sleep').map((data) => SleepDataFromJson(offsetFromUTCMillis, data)); return { sleeps, summary: SleepSummaryFromJson((0, types_utils_1.get)(json, 'summary')), }; } function SleepDataFromJson(offsetFromUTCMillis, json) { return { dateOfSleep: (0, types_utils_1.get)(json, 'dateOfSleep'), duration: (0, types_utils_1.get)(json, 'duration'), efficiency: (0, types_utils_1.get)(json, 'efficiency'), endTime: (0, date_utils_1.convertToOffsetDate)(new Date(`${(0, types_utils_1.get)(json, 'endTime')}Z`), offsetFromUTCMillis), infoCode: (0, types_utils_1.get)(json, 'infoCode'), isMainSleep: (0, types_utils_1.get)(json, 'isMainSleep'), levels: SleepLevelsFromJson(offsetFromUTCMillis, (0, types_utils_1.get)(json, 'levels')), logId: (0, types_utils_1.get)(json, 'logId'), logType: (0, types_utils_1.get)(json, 'logType'), minutesAfterWakeup: (0, types_utils_1.getOptionalValue)(json, 'minutesAfterWakeup'), minutesAsleep: (0, types_utils_1.getOptionalValue)(json, 'minutesAsleep'), minutesAwake: (0, types_utils_1.getOptionalValue)(json, 'minutesAwake'), minutesToFallAsleep: (0, types_utils_1.getOptionalValue)(json, 'minutesToFallAsleep'), startTime: (0, date_utils_1.convertToOffsetDate)(new Date(`${(0, types_utils_1.get)(json, 'startTime')}Z`), offsetFromUTCMillis), timeInBed: (0, types_utils_1.get)(json, 'timeInBed'), type: (0, types_utils_1.get)(json, 'type'), }; } function SleepLevelsFromJson(offsetFromUTCMillis, json) { const summaryJson = (0, types_utils_1.get)(json, 'summary'); const summary = (0, types_utils_1.exists)(summaryJson, 'asleep') ? SleepLevelSummaryClassicFromJson(summaryJson) : SleepLevelSummaryStagesFromJson(summaryJson); const shortData = (0, types_utils_1.exists)(json, 'shortData') ? (0, types_utils_1.get)(json, 'shortData').map((data) => SleepLevelDataFromJson(offsetFromUTCMillis, data)) : undefined; return { data: (0, types_utils_1.get)(json, 'data').map((data) => SleepLevelDataFromJson(offsetFromUTCMillis, data)), shortData, summary, }; } function SleepLevelDataFromJson(offsetFromUTCMillis, json) { return { level: (0, types_utils_1.get)(json, 'level'), dateTime: (0, date_utils_1.convertToOffsetDate)(new Date(`${(0, types_utils_1.get)(json, 'dateTime')}Z`), offsetFromUTCMillis), seconds: (0, types_utils_1.get)(json, 'seconds'), }; } function SleepLevelSummaryClassicFromJson(json) { return { asleep: SleepLevelSummaryClassicItemFromJson((0, types_utils_1.get)(json, 'asleep')), awake: SleepLevelSummaryClassicItemFromJson((0, types_utils_1.get)(json, 'awake')), restless: SleepLevelSummaryClassicItemFromJson((0, types_utils_1.get)(json, 'restless')), }; } function SleepLevelSummaryStagesFromJson(json) { return { deep: SleepLevelSummaryStagesItemFromJson((0, types_utils_1.get)(json, 'deep')), light: SleepLevelSummaryStagesItemFromJson((0, types_utils_1.get)(json, 'light')), rem: SleepLevelSummaryStagesItemFromJson((0, types_utils_1.get)(json, 'rem')), wake: SleepLevelSummaryStagesItemFromJson((0, types_utils_1.get)(json, 'wake')), }; } function SleepLevelSummaryStagesItemFromJson(json) { return { count: (0, types_utils_1.get)(json, 'count'), minutes: (0, types_utils_1.get)(json, 'minutes'), thirtyDayAvgMinutes: (0, types_utils_1.get)(json, 'thirtyDayAvgMinutes'), }; } function SleepLevelSummaryClassicItemFromJson(json) { return { count: (0, types_utils_1.get)(json, 'count'), minutes: (0, types_utils_1.get)(json, 'minutes'), }; } function SleepSummaryFromJson(json) { const stages = (0, types_utils_1.exists)(json, 'stages') ? SleepSummaryStagesFromJson((0, types_utils_1.get)(json, 'stages')) : undefined; return { stages, totalMinutesAsleep: (0, types_utils_1.get)(json, 'totalMinutesAsleep'), totalSleepRecords: (0, types_utils_1.get)(json, 'totalSleepRecords'), totalTimeInBed: (0, types_utils_1.get)(json, 'totalTimeInBed'), }; } function SleepSummaryStagesFromJson(json) { return { deep: (0, types_utils_1.get)(json, 'deep'), light: (0, types_utils_1.get)(json, 'light'), rem: (0, types_utils_1.get)(json, 'rem'), wake: (0, types_utils_1.get)(json, 'wake'), }; } //# sourceMappingURL=sleep.js.map