Skip to content

Commit

Permalink
feat: connection identifier added
Browse files Browse the repository at this point in the history
  • Loading branch information
khskekec committed Jun 7, 2022
1 parent 2169e24 commit 5078a77
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
54 changes: 42 additions & 12 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { LibreCgmData } from './types/client';
import { ActiveSensor, Connection, GlucoseItem } from './types/connection';
import { ConnectionsResponse } from './types/connections';
import { ConnectionsResponse, Datum } from './types/connections';
import { GraphData } from './types/graph';
import { LoginResponse } from './types/#';
import { mapData } from './utils';
Expand All @@ -11,6 +11,7 @@ const LIBRE_LINK_SERVER = 'https://api-eu.libreview.io';
type ClientArgs = {
username: string;
password: string;
connectionIdentifier?: string | ((connections: Datum[]) => string)
};

type ReadRawResponse = {
Expand All @@ -29,7 +30,7 @@ const urlMap = {
connections: '/llu/connections',
};

export const LibreLinkUpClient = ({ username, password }: ClientArgs) => {
export const LibreLinkUpClient = ({ username, password, connectionIdentifier }: ClientArgs) => {
let jwtToken: string | null = null;
let connectionId: string | null = null;

Expand Down Expand Up @@ -69,15 +70,15 @@ export const LibreLinkUpClient = ({ username, password }: ClientArgs) => {

const loginWrapper =
<Return>(func: () => Promise<Return>) =>
async (): Promise<Return> => {
try {
if (!jwtToken) await login();
return func();
} catch (e) {
await login();
return func();
}
};
async (): Promise<Return> => {
try {
if (!jwtToken) await login();
return func();
} catch (e) {
await login();
return func();
}
};

const getConnections = loginWrapper<ConnectionsResponse>(async () => {
const response = await instance.get<ConnectionsResponse>(
Expand All @@ -87,10 +88,33 @@ export const LibreLinkUpClient = ({ username, password }: ClientArgs) => {
return response.data;
});

const getConnection = (connections: Datum[]): string => {
if (typeof connectionIdentifier === 'string') {
const match = connections.find(({ firstName, lastName }) => `${firstName} ${lastName}`.toLowerCase() === connectionIdentifier.toLowerCase());

if (!match) {
throw new Error(`Unable to identify connection by given name '${connectionIdentifier}'.`);
}

return match.patientId;
} else if (typeof connectionIdentifier === 'function') {
const match = connectionIdentifier.call(null, connections)

if (!match) {
throw new Error(`Unable to identify connection by given name function`);
}

return match;
}

return connections[0].patientId;
}

const readRaw = loginWrapper<ReadRawResponse>(async () => {
if (!connectionId) {
const connections = await getConnections();
connectionId = connections.data[0].patientId;

connectionId = getConnection(connections.data);
}

const response = await instance.get<GraphData>(
Expand All @@ -109,9 +133,15 @@ export const LibreLinkUpClient = ({ username, password }: ClientArgs) => {
};
};

const observe = async () => {
// @todo
}

return {
observe,
readRaw,
read,
login,
};
};

4 changes: 2 additions & 2 deletions src/types/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface ConnectionsResponse {
ticket: Ticket;
}

interface Datum {
export interface Datum {
id: string;
patientId: string;
country: string;
Expand Down Expand Up @@ -60,7 +60,7 @@ interface Nd {
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Std {}
interface Std { }

interface Glucose {
FactoryTimestamp: string;
Expand Down

0 comments on commit 5078a77

Please # to comment.