From 035b052482c20fd7c1147e93a089e3aa8a288673 Mon Sep 17 00:00:00 2001 From: Eric Lee Date: Wed, 25 Feb 2026 10:42:09 +0900 Subject: [PATCH] fix weight data always null issue --- src/garmin-client.ts | 67 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 8 deletions(-) diff --git a/src/garmin-client.ts b/src/garmin-client.ts index 8471605..5aea149 100644 --- a/src/garmin-client.ts +++ b/src/garmin-client.ts @@ -13,6 +13,45 @@ export interface SleepDuration { minutes: number; } +export interface GarminDateWeight { + samplePk: number; + date: number; + calendarDate: string; + weight: number; // in grams + bmi: number | null; + bodyFat: number | null; + bodyWater: number | null; + boneMass: number | null; + muscleMass: number | null; + physiqueRating: number | null; + visceralFat: number | null; + metabolicAge: number | null; + sourceType: string; + timestampGMT: number; + weightDelta: number; +} + +export interface GarminWeightTotalAverage { + from: number; + until: number; + weight: number; // in grams + bmi: number | null; + bodyFat: number | null; + bodyWater: number | null; + boneMass: number | null; + muscleMass: number | null; + physiqueRating: number | null; + visceralFat: number | null; + metabolicAge: number | null; +} + +export interface GarminWeightData { + startDate: string; + endDate: string; + dateWeightList: GarminDateWeight[]; + totalAverage: GarminWeightTotalAverage; +} + export interface CaloriesData { total: number | null; // 總消耗卡路里 (BMR + 活動) active: number | null; // 活動消耗卡路里 @@ -176,12 +215,13 @@ export class GarminClient { } /** - * Get daily weight data + * Get daily weight data (in kg) * @param date - Date to get weight for (defaults to today) */ async getDailyWeightData(date?: Date): Promise { try { - return await this.client.getDailyWeightData(date); + const weightData = await this.client.getDailyWeightData(date) as GarminWeightData; + return this.parseWeightInKg(weightData) ?? 0; } catch (error) { // Weight data might not be available return 0; @@ -292,6 +332,21 @@ export class GarminClient { return date.toISOString().split('T')[0]; } + /** + * Helper to extract weight in kg from GarminWeightData + * Garmin API returns weight in grams, this converts to kg with 2 decimal places + * @returns weight in kg, or null if no valid weight data + */ + private parseWeightInKg(weightData: GarminWeightData): number | null { + const weightInGrams = weightData.totalAverage?.weight + ?? weightData.dateWeightList?.[0]?.weight + ?? null; + if (typeof weightInGrams === 'number' && weightInGrams > 0) { + return Math.round((weightInGrams / 1000) * 100) / 100; + } + return null; + } + /** * Helper to add delay between API calls to avoid rate limiting */ @@ -366,12 +421,8 @@ export class GarminClient { */ private async safeGetWeight(date: Date): Promise { try { - const weight = await this.client.getDailyWeightData(date) as any; - // Weight is returned in grams, convert to kg - if (typeof weight === 'number' && weight > 0) { - return weight; - } - return null; + const weightData = await this.client.getDailyWeightData(date) as GarminWeightData; + return this.parseWeightInKg(weightData); } catch { return null; }