Skip to content

Commit

Permalink
✨ Expose the raw reading via fetchReading method (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
DRFR0ST authored May 17, 2024
1 parent c6a5c74 commit 0255b71
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
7 changes: 7 additions & 0 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -23,6 +29,7 @@ const main = async () => {

console.log(client.me);
console.log(await read());
// console.log(JSON.stringify(await fetchReading(), null, 2));
};

main();
25 changes: 21 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LibreConnectionResponse>(`${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}`);
}
}

Expand Down

0 comments on commit 0255b71

Please sign in to comment.