diff --git a/example/index.ts b/example/index.ts index cee4fee..f077d9e 100644 --- a/example/index.ts +++ b/example/index.ts @@ -15,6 +15,12 @@ const read = async () => { return await client.read(); } +const fetchReading = async () => { + console.log("\n\Fetch the reading =>\n"); + + return await client.fetchReading(); +} + // This is an example of how to use the LibreLinkClient class. const main = async () => { console.clear(); @@ -23,6 +29,7 @@ const main = async () => { console.log(client.me); console.log(await read()); + // console.log(JSON.stringify(await fetchReading(), null, 2)); }; main(); diff --git a/src/client.ts b/src/client.ts index 03083b9..4cdc33b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -92,20 +92,37 @@ export class LibreLinkClient { * @returns The latest glucose measurement from the Libre Link Up API. */ public async read() { + try { + const response = await this.fetchReading(); + + // Parse and return the latest glucose item from the response. + return parseGlucoseReading(response.data?.connection.glucoseItem, response.data.connection); + } catch(err) { + const error = err as Error; + + console.error(error); + throw new Error(`Error reading data from Libre Link Up API. ${error.message}`); + } + } + + /** + * @description Fetch the reading from the Libre Link Up API. Use to obtain the raw reading and more. + * @returns The response from the Libre Link Up API. + */ + public async fetchReading() { try { const patientId = await this.getPatientId(); const response = await this._fetcher(`${LibreLinkUpEndpoints.Connections}/${patientId}/graph`); - this.verbose("Fetched data from Libre Link Up API.", JSON.stringify(response, null, 2)); + this.verbose("Fetched reading from Libre Link Up API.", JSON.stringify(response, null, 2)); - // Parse and return the latest glucose item from the response. - return parseGlucoseReading(response.data?.connection.glucoseItem, response.data.connection); + return response; } catch(err) { const error = err as Error; console.error(error); - throw new Error(`Error reading data from Libre Link Up API. ${error.message}`); + throw new Error(`Error fetching reading from Libre Link Up API. ${error.message}`); } }